Skip to content

Commit 78b8c3e

Browse files
moved CM connections to app config (Azure#48603)
* moved to app config * renamed extensions back due to conflick * updated apis * added NET9.0 to web project * added NET9.0 to web.test project * added NET 462 to web.test project * removed web tests * removed web tests * removed web tests * removed web tests
1 parent 23b666e commit 78b8c3e

File tree

83 files changed

+1488
-1584
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+1488
-1584
lines changed

sdk/cloudmachine/Azure.Projects.OpenAI/api/Azure.Projects.OpenAI.net8.0.cs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
namespace Azure.Projects.OpenAI
1+
namespace Azure.AI.OpenAI
22
{
3-
public enum AIModelKind
4-
{
5-
Chat = 0,
6-
Embedding = 1,
7-
}
83
public static partial class AzureOpenAIExtensions
94
{
105
public static void Add(this System.Collections.Generic.List<OpenAI.Chat.ChatMessage> messages, OpenAI.Chat.ChatCompletion completion) { }
@@ -13,10 +8,18 @@ public static void Add(this System.Collections.Generic.List<OpenAI.Chat.ChatMess
138
public static string AsText(this OpenAI.Chat.ChatCompletion completion) { throw null; }
149
public static string AsText(this OpenAI.Chat.ChatMessageContent content) { throw null; }
1510
public static string AsText(this System.ClientModel.ClientResult<OpenAI.Chat.ChatCompletion> completionResult) { throw null; }
16-
public static OpenAI.Chat.ChatClient GetOpenAIChatClient(this System.ClientModel.Primitives.ConnectionProvider workspace, string? deploymentName = null) { throw null; }
17-
public static OpenAI.Embeddings.EmbeddingClient GetOpenAIEmbeddingsClient(this System.ClientModel.Primitives.ConnectionProvider workspace, string? deploymentName = null) { throw null; }
11+
public static OpenAI.Chat.ChatClient GetOpenAIChatClient(this System.ClientModel.Primitives.ConnectionProvider provider, string? deploymentName = null) { throw null; }
12+
public static OpenAI.Embeddings.EmbeddingClient GetOpenAIEmbeddingClient(this System.ClientModel.Primitives.ConnectionProvider provider, string? deploymentName = null) { throw null; }
1813
public static void Trim(this System.Collections.Generic.List<OpenAI.Chat.ChatMessage> messages) { }
1914
}
15+
}
16+
namespace Azure.Projects.OpenAI
17+
{
18+
public enum AIModelKind
19+
{
20+
Chat = 0,
21+
Embedding = 1,
22+
}
2023
public partial class ChatProcessor
2124
{
2225
public ChatProcessor(OpenAI.Chat.ChatClient chat) { }
@@ -28,6 +31,7 @@ protected virtual void OnGround(System.Collections.Generic.List<OpenAI.Chat.Chat
2831
protected virtual void OnLength(System.Collections.Generic.List<OpenAI.Chat.ChatMessage> conversation, OpenAI.Chat.ChatCompletion completion) { }
2932
protected virtual void OnStop(System.Collections.Generic.List<OpenAI.Chat.ChatMessage> conversation, OpenAI.Chat.ChatCompletion completion) { }
3033
protected virtual void OnToolCalls(System.Collections.Generic.List<OpenAI.Chat.ChatMessage> conversation, OpenAI.Chat.ChatCompletion completion) { }
34+
protected virtual void OnToolError(System.Collections.Generic.List<string> failed, System.Collections.Generic.List<OpenAI.Chat.ChatMessage> conversation, OpenAI.Chat.ChatCompletion completion) { }
3135
public OpenAI.Chat.ChatCompletion TakeTurn(System.Collections.Generic.List<OpenAI.Chat.ChatMessage> conversation, string prompt) { throw null; }
3236
}
3337
public partial class ChatTools
@@ -39,6 +43,7 @@ public void Add(System.Type functions) { }
3943
public string Call(OpenAI.Chat.ChatToolCall call) { throw null; }
4044
public string Call(string name, object[] arguments) { throw null; }
4145
public System.Collections.Generic.IEnumerable<OpenAI.Chat.ToolChatMessage> CallAll(System.Collections.Generic.IEnumerable<OpenAI.Chat.ChatToolCall> toolCalls) { throw null; }
46+
public System.Collections.Generic.IEnumerable<OpenAI.Chat.ToolChatMessage> CallAll(System.Collections.Generic.IEnumerable<OpenAI.Chat.ChatToolCall> toolCalls, out System.Collections.Generic.List<string>? failed) { throw null; }
4247
public static implicit operator OpenAI.Chat.ChatCompletionOptions (Azure.Projects.OpenAI.ChatTools tools) { throw null; }
4348
public OpenAI.Chat.ChatCompletionOptions ToOptions() { throw null; }
4449
}
@@ -55,14 +60,21 @@ public FindOptions() { }
5560
public int MaxEntries { get { throw null; } set { } }
5661
public float Threshold { get { throw null; } set { } }
5762
}
63+
public partial class OpenAIChatFeature : Azure.Projects.OpenAI.OpenAIModelFeature
64+
{
65+
public OpenAIChatFeature(string model, string modelVersion) : base (default(string), default(string), default(Azure.Projects.OpenAI.AIModelKind)) { }
66+
}
67+
public partial class OpenAIEmbeddingFeature : Azure.Projects.OpenAI.OpenAIModelFeature
68+
{
69+
public OpenAIEmbeddingFeature(string model, string modelVersion) : base (default(string), default(string), default(Azure.Projects.OpenAI.AIModelKind)) { }
70+
}
5871
public partial class OpenAIModelFeature : Azure.Projects.Core.AzureProjectFeature
5972
{
6073
public OpenAIModelFeature(string model, string modelVersion, Azure.Projects.OpenAI.AIModelKind kind = Azure.Projects.OpenAI.AIModelKind.Chat) { }
6174
public string Model { get { throw null; } }
6275
public string ModelVersion { get { throw null; } }
76+
protected override void AddImplicitFeatures(Azure.Projects.Core.FeatureCollection features, string cmId) { }
6377
public System.ClientModel.Primitives.ClientConnection CreateConnection(string cmId) { throw null; }
64-
protected override void EmitConnections(System.Collections.Generic.ICollection<System.ClientModel.Primitives.ClientConnection> connections, string cmId) { }
65-
protected override void EmitFeatures(Azure.Projects.Core.FeatureCollection features, string cmId) { }
6678
protected override Azure.Provisioning.Primitives.ProvisionableResource EmitResources(Azure.Projects.ProjectInfrastructure cm) { throw null; }
6779
}
6880
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]

sdk/cloudmachine/Azure.Projects.OpenAI/api/Azure.Projects.OpenAI.netstandard2.0.cs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
namespace Azure.Projects.OpenAI
1+
namespace Azure.AI.OpenAI
22
{
3-
public enum AIModelKind
4-
{
5-
Chat = 0,
6-
Embedding = 1,
7-
}
83
public static partial class AzureOpenAIExtensions
94
{
105
public static void Add(this System.Collections.Generic.List<OpenAI.Chat.ChatMessage> messages, OpenAI.Chat.ChatCompletion completion) { }
@@ -13,10 +8,18 @@ public static void Add(this System.Collections.Generic.List<OpenAI.Chat.ChatMess
138
public static string AsText(this OpenAI.Chat.ChatCompletion completion) { throw null; }
149
public static string AsText(this OpenAI.Chat.ChatMessageContent content) { throw null; }
1510
public static string AsText(this System.ClientModel.ClientResult<OpenAI.Chat.ChatCompletion> completionResult) { throw null; }
16-
public static OpenAI.Chat.ChatClient GetOpenAIChatClient(this System.ClientModel.Primitives.ConnectionProvider workspace, string? deploymentName = null) { throw null; }
17-
public static OpenAI.Embeddings.EmbeddingClient GetOpenAIEmbeddingsClient(this System.ClientModel.Primitives.ConnectionProvider workspace, string? deploymentName = null) { throw null; }
11+
public static OpenAI.Chat.ChatClient GetOpenAIChatClient(this System.ClientModel.Primitives.ConnectionProvider provider, string? deploymentName = null) { throw null; }
12+
public static OpenAI.Embeddings.EmbeddingClient GetOpenAIEmbeddingClient(this System.ClientModel.Primitives.ConnectionProvider provider, string? deploymentName = null) { throw null; }
1813
public static void Trim(this System.Collections.Generic.List<OpenAI.Chat.ChatMessage> messages) { }
1914
}
15+
}
16+
namespace Azure.Projects.OpenAI
17+
{
18+
public enum AIModelKind
19+
{
20+
Chat = 0,
21+
Embedding = 1,
22+
}
2023
public partial class ChatProcessor
2124
{
2225
public ChatProcessor(OpenAI.Chat.ChatClient chat) { }
@@ -28,6 +31,7 @@ protected virtual void OnGround(System.Collections.Generic.List<OpenAI.Chat.Chat
2831
protected virtual void OnLength(System.Collections.Generic.List<OpenAI.Chat.ChatMessage> conversation, OpenAI.Chat.ChatCompletion completion) { }
2932
protected virtual void OnStop(System.Collections.Generic.List<OpenAI.Chat.ChatMessage> conversation, OpenAI.Chat.ChatCompletion completion) { }
3033
protected virtual void OnToolCalls(System.Collections.Generic.List<OpenAI.Chat.ChatMessage> conversation, OpenAI.Chat.ChatCompletion completion) { }
34+
protected virtual void OnToolError(System.Collections.Generic.List<string> failed, System.Collections.Generic.List<OpenAI.Chat.ChatMessage> conversation, OpenAI.Chat.ChatCompletion completion) { }
3135
public OpenAI.Chat.ChatCompletion TakeTurn(System.Collections.Generic.List<OpenAI.Chat.ChatMessage> conversation, string prompt) { throw null; }
3236
}
3337
public partial class ChatTools
@@ -39,6 +43,7 @@ public void Add(System.Type functions) { }
3943
public string Call(OpenAI.Chat.ChatToolCall call) { throw null; }
4044
public string Call(string name, object[] arguments) { throw null; }
4145
public System.Collections.Generic.IEnumerable<OpenAI.Chat.ToolChatMessage> CallAll(System.Collections.Generic.IEnumerable<OpenAI.Chat.ChatToolCall> toolCalls) { throw null; }
46+
public System.Collections.Generic.IEnumerable<OpenAI.Chat.ToolChatMessage> CallAll(System.Collections.Generic.IEnumerable<OpenAI.Chat.ChatToolCall> toolCalls, out System.Collections.Generic.List<string>? failed) { throw null; }
4247
public static implicit operator OpenAI.Chat.ChatCompletionOptions (Azure.Projects.OpenAI.ChatTools tools) { throw null; }
4348
public OpenAI.Chat.ChatCompletionOptions ToOptions() { throw null; }
4449
}
@@ -55,14 +60,21 @@ public FindOptions() { }
5560
public int MaxEntries { get { throw null; } set { } }
5661
public float Threshold { get { throw null; } set { } }
5762
}
63+
public partial class OpenAIChatFeature : Azure.Projects.OpenAI.OpenAIModelFeature
64+
{
65+
public OpenAIChatFeature(string model, string modelVersion) : base (default(string), default(string), default(Azure.Projects.OpenAI.AIModelKind)) { }
66+
}
67+
public partial class OpenAIEmbeddingFeature : Azure.Projects.OpenAI.OpenAIModelFeature
68+
{
69+
public OpenAIEmbeddingFeature(string model, string modelVersion) : base (default(string), default(string), default(Azure.Projects.OpenAI.AIModelKind)) { }
70+
}
5871
public partial class OpenAIModelFeature : Azure.Projects.Core.AzureProjectFeature
5972
{
6073
public OpenAIModelFeature(string model, string modelVersion, Azure.Projects.OpenAI.AIModelKind kind = Azure.Projects.OpenAI.AIModelKind.Chat) { }
6174
public string Model { get { throw null; } }
6275
public string ModelVersion { get { throw null; } }
76+
protected override void AddImplicitFeatures(Azure.Projects.Core.FeatureCollection features, string cmId) { }
6377
public System.ClientModel.Primitives.ClientConnection CreateConnection(string cmId) { throw null; }
64-
protected override void EmitConnections(System.Collections.Generic.ICollection<System.ClientModel.Primitives.ClientConnection> connections, string cmId) { }
65-
protected override void EmitFeatures(Azure.Projects.Core.FeatureCollection features, string cmId) { }
6678
protected override Azure.Provisioning.Primitives.ProvisionableResource EmitResources(Azure.Projects.ProjectInfrastructure cm) { throw null; }
6779
}
6880
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]

sdk/cloudmachine/Azure.Projects.OpenAI/src/AzureOpenAIExtensions.cs

Lines changed: 3 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -7,58 +7,18 @@
77
using System.Collections.Generic;
88
using System.Text;
99
using Azure.AI.OpenAI;
10-
using Azure.AI.Projects;
1110
using Azure.Core;
11+
using Azure.Projects.OpenAI;
1212
using OpenAI.Chat;
1313
using OpenAI.Embeddings;
1414

15-
namespace Azure.Projects.OpenAI;
15+
namespace Azure.AI.OpenAI;
1616

1717
/// <summary>
1818
/// The Azure OpenAI extensions.
1919
/// </summary>
20-
public static class AzureOpenAIExtensions
20+
public static partial class AzureOpenAIExtensions
2121
{
22-
/// <summary>
23-
/// Gets the OpenAI chat client.
24-
/// </summary>
25-
/// <param name="workspace"></param>
26-
/// <param name="deploymentName"></param>
27-
/// <returns></returns>
28-
public static ChatClient GetOpenAIChatClient(this ConnectionProvider workspace, string? deploymentName = null)
29-
{
30-
string name = deploymentName ?? "default";
31-
32-
// TODO: nest the calls after new SCM merges
33-
AzureOpenAIClient aoiaClient = workspace.Subclients.GetClient(() => CreateAzureOpenAIClient(workspace), null);
34-
ChatClient chatClient = workspace.Subclients.GetClient(() =>
35-
{
36-
return workspace.CreateChatClient(aoiaClient, deploymentName);
37-
}, name);
38-
39-
return chatClient;
40-
}
41-
42-
/// <summary>
43-
/// Gets the OpenAI embeddings client.
44-
/// </summary>
45-
/// <param name="workspace"></param>
46-
/// <param name="deploymentName"></param>
47-
/// <returns></returns>
48-
public static EmbeddingClient GetOpenAIEmbeddingsClient(this ConnectionProvider workspace, string? deploymentName = null)
49-
{
50-
string name = deploymentName ?? "default";
51-
52-
// TODO: nest the calls after new SCM merges
53-
AzureOpenAIClient aoiaClient = workspace.Subclients.GetClient(() => CreateAzureOpenAIClient(workspace), null);
54-
EmbeddingClient embeddingsClient = workspace.Subclients.GetClient(() =>
55-
{
56-
return workspace.CreateEmbeddingsClient(aoiaClient, deploymentName);
57-
}, name);
58-
59-
return embeddingsClient;
60-
}
61-
6222
/// <summary>
6323
/// returns full text of all parts.
6424
/// </summary>
@@ -97,39 +57,6 @@ public static string AsText(this ChatMessageContent content)
9757
return sb.ToString();
9858
}
9959

100-
private static AzureOpenAIClient CreateAzureOpenAIClient(this ConnectionProvider workspace)
101-
{
102-
ClientConnection connection = workspace.GetConnection(typeof(AzureOpenAIClient).FullName!);
103-
104-
if (!connection.TryGetLocatorAsUri(out Uri? uri))
105-
{
106-
throw new InvalidOperationException("The connection is not a valid URI.");
107-
}
108-
109-
if (connection.Authentication == ClientAuthenticationMethod.Credential)
110-
{
111-
return new(uri, (TokenCredential)connection.Credential!);
112-
}
113-
else
114-
{
115-
return new(uri, new ApiKeyCredential(connection.ApiKeyCredential!));
116-
}
117-
}
118-
119-
private static ChatClient CreateChatClient(this ConnectionProvider workspace, AzureOpenAIClient client, string? deploymentName = null)
120-
{
121-
ClientConnection connection = workspace.GetConnection(typeof(ChatClient).FullName!);
122-
ChatClient chat = client.GetChatClient(deploymentName ?? connection.Locator);
123-
return chat;
124-
}
125-
126-
private static EmbeddingClient CreateEmbeddingsClient(this ConnectionProvider workspace, AzureOpenAIClient client, string? deploymentName = null)
127-
{
128-
ClientConnection connection = workspace.GetConnection(typeof(EmbeddingClient).FullName!);
129-
EmbeddingClient embeddings = client.GetEmbeddingClient(deploymentName ?? connection.Locator);
130-
return embeddings;
131-
}
132-
13360
/// <summary>
13461
/// Trims list of chat messages.
13562
/// </summary>
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
#nullable enable
5+
6+
using System;
7+
using System.ClientModel;
8+
using System.ClientModel.Primitives;
9+
using Azure.Core;
10+
using OpenAI.Chat;
11+
using OpenAI.Embeddings;
12+
13+
namespace Azure.AI.OpenAI;
14+
15+
/// <summary>
16+
/// The Azure OpenAI extensions.
17+
/// </summary>
18+
public static partial class AzureOpenAIExtensions
19+
{
20+
/// <summary>
21+
/// Gets the OpenAI chat client.
22+
/// </summary>
23+
/// <param name="provider"></param>
24+
/// <param name="deploymentName"></param>
25+
/// <returns></returns>
26+
public static ChatClient GetOpenAIChatClient(this ConnectionProvider provider, string? deploymentName = null)
27+
{
28+
AzureOpenAIClient aoaiClient = provider.Subclients.GetClient(() => CreateAzureOpenAIClient(provider), null!);
29+
ChatClient chatClient = provider.Subclients.GetClient(() =>
30+
{
31+
return provider.CreateChatClient(aoaiClient, deploymentName);
32+
}, deploymentName!);
33+
34+
return chatClient;
35+
}
36+
37+
/// <summary>
38+
/// Gets the OpenAI embedding client.
39+
/// </summary>
40+
/// <param name="provider"></param>
41+
/// <param name="deploymentName"></param>
42+
/// <returns></returns>
43+
public static EmbeddingClient GetOpenAIEmbeddingClient(this ConnectionProvider provider, string? deploymentName = null)
44+
{
45+
AzureOpenAIClient aoaiClient = provider.Subclients.GetClient(() => CreateAzureOpenAIClient(provider), null!);
46+
EmbeddingClient embeddingClient = provider.Subclients.GetClient(() =>
47+
{
48+
return provider.CreateEmbeddingClient(aoaiClient, deploymentName);
49+
}, deploymentName!);
50+
51+
return embeddingClient;
52+
}
53+
54+
private static AzureOpenAIClient CreateAzureOpenAIClient(this ConnectionProvider provider)
55+
{
56+
ClientConnection connection = provider.GetConnection(typeof(AzureOpenAIClient).FullName!);
57+
58+
if (!connection.TryGetLocatorAsUri(out Uri? uri) || uri is null)
59+
{
60+
throw new InvalidOperationException("Invalid URI.");
61+
}
62+
63+
return connection.Authentication == ClientAuthenticationMethod.Credential
64+
? new AzureOpenAIClient(uri, connection.Credential as TokenCredential)
65+
: new AzureOpenAIClient(uri, new ApiKeyCredential(connection.ApiKeyCredential!));
66+
}
67+
68+
private static ChatClient CreateChatClient(this ConnectionProvider provider, AzureOpenAIClient client, string? deploymentName = null)
69+
{
70+
ClientConnection connection = provider.GetConnection(typeof(ChatClient).FullName!);
71+
ChatClient chat = client.GetChatClient(deploymentName ?? connection.Locator);
72+
return chat;
73+
}
74+
75+
private static EmbeddingClient CreateEmbeddingClient(this ConnectionProvider provider, AzureOpenAIClient client, string? deploymentName = null)
76+
{
77+
ClientConnection connection = provider.GetConnection(typeof(EmbeddingClient).FullName!);
78+
EmbeddingClient embedding = client.GetEmbeddingClient(deploymentName ?? connection.Locator);
79+
return embedding;
80+
}
81+
}

sdk/cloudmachine/Azure.Projects.OpenAI/src/CHatProcessor.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using Azure.AI.OpenAI;
67
using OpenAI.Chat;
78
using OpenAI.Embeddings;
89

@@ -145,9 +146,30 @@ protected virtual void OnToolCalls(List<ChatMessage> conversation, ChatCompletio
145146
{
146147
if (Tools == null)
147148
throw new InvalidOperationException("No tools defined.");
148-
conversation.Add(completion);
149-
IEnumerable<ToolChatMessage> toolResults = Tools.CallAll(completion.ToolCalls);
150-
conversation.AddRange(toolResults);
149+
150+
// for some reason I am getting tool calls for tools that dont exist.
151+
IEnumerable<ToolChatMessage> toolResults = Tools.CallAll(completion.ToolCalls, out List<string>? failed);
152+
if (failed != null)
153+
{
154+
OnToolError(failed, conversation, completion);
155+
}
156+
else
157+
{
158+
conversation.Add(completion);
159+
conversation.AddRange(toolResults);
160+
}
161+
}
162+
163+
/// <summary>
164+
/// Handles the error when a tool call fails.
165+
/// </summary>
166+
/// <param name="failed"></param>
167+
/// <param name="conversation"></param>
168+
/// <param name="completion"></param>
169+
protected virtual void OnToolError(List<string> failed, List<ChatMessage> conversation, ChatCompletion completion)
170+
{
171+
failed.ForEach(toolName => Console.WriteLine($"Failed to call tool: {toolName}"));
172+
conversation.Add(ChatMessage.CreateUserMessage("don't call tools that dont exist"));
151173
}
152174
}
153175
}

0 commit comments

Comments
 (0)