Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[.Net] Update OpenAI, semantic kernel to latest version #3792

Merged
merged 4 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion dotnet/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="./eng/Version.props" />
<Import Project="./eng/MetaInfo.props" />
<Import Project="./eng/Sign.props" />
<PropertyGroup>
Expand Down
18 changes: 10 additions & 8 deletions dotnet/Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<Project>
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
<MicrosoftSemanticKernelVersion>1.22.0</MicrosoftSemanticKernelVersion>
<MicrosoftSemanticKernelExperimentalVersion>1.22.0-alpha</MicrosoftSemanticKernelExperimentalVersion>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="AspNetCore.Authentication.ApiKey" Version="8.0.1" />
Expand All @@ -12,7 +14,7 @@
<PackageVersion Include="Aspire.Hosting.Orleans" Version="8.2.0" />
<PackageVersion Include="Aspire.Hosting.Qdrant" Version="8.2.0" />
<PackageVersion Include="Aspire.Hosting.Redis" Version="8.2.0" />
<PackageVersion Include="Azure.AI.OpenAI" Version=" 2.0.0-beta.3" />
<PackageVersion Include="Azure.AI.OpenAI" Version=" 2.1.0-beta.1" />
<PackageVersion Include="Azure.AI.Inference" Version="1.0.0-beta.1" />
<PackageVersion Include="Azure.Data.Tables" Version="12.8.3" />
<PackageVersion Include="Azure.Identity" Version="1.12.0" />
Expand Down Expand Up @@ -61,12 +63,12 @@
<PackageVersion Include="Microsoft.Orleans.Server" Version="8.2.0" />
<PackageVersion Include="Microsoft.Orleans.Streaming" Version="8.2.0" />
<PackageVersion Include="Microsoft.Orleans.Streaming.EventHubs" Version="8.2.0" />
<PackageVersion Include="Microsoft.SemanticKernel" Version="1.18.1-rc" />
<PackageVersion Include="Microsoft.SemanticKernel.Agents.Core" Version="1.18.1-alpha" />
<PackageVersion Include="Microsoft.SemanticKernel.Connectors.AzureOpenAI" Version="1.18.1-rc" />
<PackageVersion Include="Microsoft.SemanticKernel.Connectors.Qdrant" Version="1.18.1-alpha" />
<PackageVersion Include="Microsoft.SemanticKernel.Plugins.Memory" Version="1.18.1-alpha" />
<PackageVersion Include="Microsoft.SemanticKernel.Plugins.Web" Version="1.18.1-alpha" />
<PackageVersion Include="Microsoft.SemanticKernel" Version="$(MicrosoftSemanticKernelVersion)" />
<PackageVersion Include="Microsoft.SemanticKernel.Agents.Core" Version="$(MicrosoftSemanticKernelExperimentalVersion)" />
<PackageVersion Include="Microsoft.SemanticKernel.Connectors.AzureOpenAI" Version="$(MicrosoftSemanticKernelVersion)" />
<PackageVersion Include="Microsoft.SemanticKernel.Connectors.Qdrant" Version="$(MicrosoftSemanticKernelExperimentalVersion)" />
<PackageVersion Include="Microsoft.SemanticKernel.Plugins.Memory" Version="$(MicrosoftSemanticKernelExperimentalVersion)" />
<PackageVersion Include="Microsoft.SemanticKernel.Plugins.Web" Version="$(MicrosoftSemanticKernelExperimentalVersion)" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="Octokit" Version="13.0.1" />
<PackageVersion Include="Octokit.Webhooks.AspNetCore" Version="2.2.2" />
Expand All @@ -86,7 +88,7 @@
<PackageVersion Include="Microsoft.DotNet.Interactive.Jupyter" Version="1.0.0-beta.24229.4" />
<PackageVersion Include="Microsoft.DotNet.Interactive.PackageManagement" Version="1.0.0-beta.24229.4" />
<PackageVersion Include="Google.Cloud.AIPlatform.V1" Version="3.0.0" />
<PackageVersion Include="OpenAI" Version="2.0.0-beta.10" />
<PackageVersion Include="OpenAI" Version="2.1.0-beta.1" />
<PackageVersion Include="System.CodeDom" Version="5.0.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.3.0" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
Expand Down
2 changes: 1 addition & 1 deletion dotnet/eng/MetaInfo.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VersionPrefix>0.2.1</VersionPrefix>
<VersionPrefix>0.2.2</VersionPrefix>
<Authors>AutoGen</Authors>
<PackageProjectUrl>https://microsoft.github.io/autogen-for-net/</PackageProjectUrl>
<RepositoryUrl>https://github.com/microsoft/autogen</RepositoryUrl>
Expand Down
17 changes: 0 additions & 17 deletions dotnet/eng/Version.props

This file was deleted.

3 changes: 2 additions & 1 deletion dotnet/samples/AutoGen.BasicSamples/Example08_LMStudio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Example08_LMStudio.cs

#region lmstudio_using_statements
using System.ClientModel;
using AutoGen.Core;
using AutoGen.OpenAI;
using AutoGen.OpenAI.Extension;
Expand All @@ -16,7 +17,7 @@ public static async Task RunAsync()
{
#region lmstudio_example_1
var endpoint = "http://localhost:1234";
var openaiClient = new OpenAIClient("api-key", new OpenAIClientOptions
var openaiClient = new OpenAIClient(new ApiKeyCredential("api-key"), new OpenAIClientOptions
{
Endpoint = new Uri(endpoint),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static async Task RunTokenCountAsync()
var reply = await innerAgent.GenerateReplyAsync(messages, option, ct);
if (reply is MessageEnvelope<ChatCompletion> chatCompletions)
{
var tokenCount = chatCompletions.Content.Usage.TotalTokens;
var tokenCount = chatCompletions.Content.Usage.TotalTokenCount;
totalTokenCount += tokenCount;
}
return reply;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Connect_To_Azure_OpenAI.cs
// Copyright (c) Microsoft. All rights reserved.

#region using_statement
using System.ClientModel;
using AutoGen.Core;
using AutoGen.OpenAI.Extension;
using Azure;
using Azure.AI.OpenAI;
#endregion using_statement

Expand All @@ -21,7 +20,7 @@ public static async Task RunAsync()

// Use AzureOpenAIClient to connect to openai model deployed on azure.
// The AzureOpenAIClient comes from Azure.AI.OpenAI package
var openAIClient = new AzureOpenAIClient(new Uri(endpoint), new AzureKeyCredential(apiKey));
var openAIClient = new AzureOpenAIClient(new Uri(endpoint), new ApiKeyCredential(apiKey));

var agent = new OpenAIChatAgent(
chatClient: openAIClient.GetChatClient(model),
Expand Down
3 changes: 2 additions & 1 deletion dotnet/samples/AutoGen.OpenAI.Sample/Connect_To_Ollama.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Connect_To_Ollama.cs

#region using_statement
using System.ClientModel;
using AutoGen.Core;
using AutoGen.OpenAI.Extension;
using OpenAI;
Expand All @@ -16,7 +17,7 @@ public static async Task RunAsync()
#region create_agent
// api-key is not required for local server
// so you can use any string here
var openAIClient = new OpenAIClient("api-key", new OpenAIClientOptions
var openAIClient = new OpenAIClient(new ApiKeyCredential("api-key"), new OpenAIClientOptions
{
Endpoint = new Uri("http://localhost:11434/v1/"), // remember to add /v1/ at the end to connect to Ollama openai server
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Tool_Call_With_Ollama_And_LiteLLM.cs

using System.ClientModel;
using AutoGen.Core;
using AutoGen.OpenAI.Extension;
using OpenAI;
Expand Down Expand Up @@ -45,7 +46,7 @@ public static async Task RunAsync()

// api-key is not required for local server
// so you can use any string here
var openAIClient = new OpenAIClient("api-key", new OpenAIClientOptions
var openAIClient = new OpenAIClient(new ApiKeyCredential("api-key"), new OpenAIClientOptions
{
Endpoint = new Uri("http://localhost:4000"),
});
Expand Down
2 changes: 1 addition & 1 deletion dotnet/samples/AutoGen.OpenAI.Sample/Use_Json_Mode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static async Task RunAsync()
name: "assistant",
systemMessage: "You are a helpful assistant designed to output JSON.",
seed: 0, // explicitly set a seed to enable deterministic output
responseFormat: ChatResponseFormat.JsonObject) // set response format to JSON object to enable JSON mode
responseFormat: ChatResponseFormat.CreateJsonObjectFormat()) // set response format to JSON object to enable JSON mode
.RegisterMessageConnector()
.RegisterPrintMessage();
#endregion create_agent
Expand Down
13 changes: 6 additions & 7 deletions dotnet/src/AutoGen.OpenAI/Agent/OpenAIChatAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class OpenAIChatAgent : IStreamingAgent
/// <param name="systemMessage">system message</param>
/// <param name="temperature">temperature</param>
/// <param name="maxTokens">max tokens to generated</param>
/// <param name="responseFormat">response format, set it to <see cref="ChatResponseFormat.JsonObject"/> to enable json mode.</param>
/// <param name="responseFormat">response format, set it to <see cref="ChatResponseFormat"/> to enable json mode.</param>
/// <param name="seed">seed to use, set it to enable deterministic output</param>
/// <param name="functions">functions</param>
public OpenAIChatAgent(
Expand Down Expand Up @@ -138,12 +138,11 @@ private ChatCompletionOptions CreateChatCompletionsOptions(GenerateReplyOptions?
{
Seed = this.options.Seed,
Temperature = options?.Temperature ?? this.options.Temperature,
MaxTokens = options?.MaxToken ?? this.options.MaxTokens,
MaxOutputTokenCount = options?.MaxToken ?? this.options.MaxOutputTokenCount,
ResponseFormat = this.options.ResponseFormat,
FrequencyPenalty = this.options.FrequencyPenalty,
FunctionChoice = this.options.FunctionChoice,
IncludeLogProbabilities = this.options.IncludeLogProbabilities,
ParallelToolCallsEnabled = this.options.ParallelToolCallsEnabled,
AllowParallelToolCalls = this.options.AllowParallelToolCalls,
PresencePenalty = this.options.PresencePenalty,
ToolChoice = this.options.ToolChoice,
TopLogProbabilityCount = this.options.TopLogProbabilityCount,
Expand Down Expand Up @@ -183,9 +182,9 @@ private ChatCompletionOptions CreateChatCompletionsOptions(GenerateReplyOptions?
if (options?.OutputSchema is not null)
{
option.ResponseFormat = ChatResponseFormat.CreateJsonSchemaFormat(
name: options.OutputSchema.GetTitle() ?? throw new ArgumentException("Output schema must have a title"),
jsonSchemaFormatName: options.OutputSchema.GetTitle() ?? throw new ArgumentException("Output schema must have a title"),
jsonSchema: BinaryData.FromObjectAsJson(options.OutputSchema),
description: options.OutputSchema.GetDescription());
jsonSchemaFormatDescription: options.OutputSchema.GetDescription());
}

return option;
Expand All @@ -201,7 +200,7 @@ private static ChatCompletionOptions CreateChatCompletionOptions(
var options = new ChatCompletionOptions
{
Temperature = temperature,
MaxTokens = maxTokens,
MaxOutputTokenCount = maxTokens,
Seed = seed,
ResponseFormat = responseFormat,
};
Expand Down
3 changes: 2 additions & 1 deletion dotnet/src/AutoGen.OpenAI/AutoGen.OpenAI.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(PackageTargetFrameworks)</TargetFrameworks>
<RootNamespace>AutoGen.OpenAI</RootNamespace>
<NoWarn>$(NoWarn);OPENAI001</NoWarn>
</PropertyGroup>

<Import Project="$(RepoRoot)/nuget/nuget-package.props" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,16 @@ public async IAsyncEnumerable<IMessage> InvokeAsync(
{
currentToolName += toolCall.FunctionName;
currentToolArguments += toolCall.FunctionArgumentsUpdate;
currentToolId += toolCall.Id;
currentToolId += toolCall.ToolCallId;

yield return new ToolCallMessageUpdate(currentToolName, currentToolArguments, from: agent.Name);
}
else
{
toolCalls.Add(new ToolCall(currentToolName, currentToolArguments) { ToolCallId = currentToolId });
currentToolName = toolCall.FunctionName;
currentToolArguments = toolCall.FunctionArgumentsUpdate;
currentToolId = toolCall.Id;
currentToolArguments = toolCall.FunctionArgumentsUpdate.ToString();
currentToolId = toolCall.ToolCallId;
currentIndex = toolCall.Index;

yield return new ToolCallMessageUpdate(currentToolName, currentToolArguments, from: agent.Name);
Expand Down Expand Up @@ -185,7 +185,7 @@ private IMessage PostProcessChatResponseMessage(ChatCompletion chatCompletion, s
// if tool calls is not empty, return ToolCallMessage
if (chatCompletion.ToolCalls is { Count: > 0 })
{
var toolCalls = chatCompletion.ToolCalls.Select(tc => new ToolCall(tc.FunctionName, tc.FunctionArguments) { ToolCallId = tc.Id });
var toolCalls = chatCompletion.ToolCalls.Select(tc => new ToolCall(tc.FunctionName, tc.FunctionArguments.ToString()) { ToolCallId = tc.Id });
return new ToolCallMessage(toolCalls, from)
{
Content = textContent?.Kind switch
Expand All @@ -196,20 +196,6 @@ private IMessage PostProcessChatResponseMessage(ChatCompletion chatCompletion, s
};
}

// else, process function call.
// This is deprecated and will be removed in the future.
if (chatCompletion.FunctionCall is ChatFunctionCall fc)
{
return new ToolCallMessage(fc.FunctionName, fc.FunctionArguments, from)
{
Content = textContent?.Kind switch
{
_ when textContent?.Kind == ChatMessageContentPartKind.Text => textContent.Text,
_ => null,
},
};
}

// if the content is text, return TextMessage
if (textContent?.Kind == ChatMessageContentPartKind.Text)
{
Expand Down Expand Up @@ -298,7 +284,7 @@ private IEnumerable<ChatMessage> ProcessMultiModalMessage(IAgent agent, MultiMod

IEnumerable<ChatMessageContentPart> items = message.Content.Select<IMessage, ChatMessageContentPart>(ci => ci switch
{
TextMessage text => ChatMessageContentPart.CreateTextMessageContentPart(text.Content),
TextMessage text => ChatMessageContentPart.CreateTextPart(text.Content),
ImageMessage image => this.CreateChatMessageImageContentItemFromImageMessage(image),
_ => throw new NotImplementedException(),
});
Expand All @@ -309,8 +295,8 @@ private IEnumerable<ChatMessage> ProcessMultiModalMessage(IAgent agent, MultiMod
private ChatMessageContentPart CreateChatMessageImageContentItemFromImageMessage(ImageMessage message)
{
return message.Data is null && message.Url is not null
? ChatMessageContentPart.CreateImageMessageContentPart(new Uri(message.Url))
: ChatMessageContentPart.CreateImageMessageContentPart(message.Data, message.Data?.MediaType);
? ChatMessageContentPart.CreateImagePart(new Uri(message.Url))
: ChatMessageContentPart.CreateImagePart(message.Data, message.Data?.MediaType);
}

private IEnumerable<ChatMessage> ProcessToolCallMessage(IAgent agent, ToolCallMessage message)
Expand All @@ -320,12 +306,26 @@ private IEnumerable<ChatMessage> ProcessToolCallMessage(IAgent agent, ToolCallMe
throw new ArgumentException("ToolCallMessage is not supported when message.From is not the same with agent");
}

var toolCallParts = message.ToolCalls.Select((tc, i) => ChatToolCall.CreateFunctionToolCall(tc.ToolCallId ?? $"{tc.FunctionName}_{i}", tc.FunctionName, tc.FunctionArguments));
var toolCallParts = message.ToolCalls.Select((tc, i) => ChatToolCall.CreateFunctionToolCall(tc.ToolCallId ?? $"{tc.FunctionName}_{i}", tc.FunctionName, BinaryData.FromString(tc.FunctionArguments)));
var textContent = message.GetContent() ?? null;

// Don't set participant name for assistant when it is tool call
// fix https://github.com/microsoft/autogen/issues/3437
var chatRequestMessage = new AssistantChatMessage(toolCallParts, textContent);
AssistantChatMessage chatRequestMessage;

if (string.IsNullOrEmpty(textContent) is true)
{
chatRequestMessage = new AssistantChatMessage(toolCallParts);
}
else
{
chatRequestMessage = new AssistantChatMessage(textContent);

foreach (var toolCallPart in toolCallParts)
{
chatRequestMessage.ToolCalls.Add(toolCallPart);
}
}

return [chatRequestMessage];
}
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/AutoGen/AzureOpenAIConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public AzureOpenAIConfig(string endpoint, string deploymentName, string apiKey)

internal ChatClient CreateChatClient()
{
var client = new AzureOpenAIClient(new System.Uri(this.Endpoint), this.ApiKey);
var client = new AzureOpenAIClient(new System.Uri(this.Endpoint), new System.ClientModel.ApiKeyCredential(this.ApiKey));

return client.GetChatClient(DeploymentName);
}
Expand Down
3 changes: 2 additions & 1 deletion dotnet/src/AutoGen/LMStudioConfig.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// LMStudioConfig.cs
using System;
using System.ClientModel;
using OpenAI;
using OpenAI.Chat;

Expand Down Expand Up @@ -33,7 +34,7 @@ public LMStudioConfig(Uri uri)

internal ChatClient CreateChatClient()
{
var client = new OpenAIClient("api-key", new OpenAIClientOptions
var client = new OpenAIClient(new ApiKeyCredential("api-key"), new OpenAIClientOptions
{
Endpoint = this.Uri,
});
Expand Down
Loading
Loading