Skip to content

Commit

Permalink
Merge branch 'main' into andrewldesousa/anthropic-function-calling-up…
Browse files Browse the repository at this point in the history
…date
  • Loading branch information
andrewldesousa authored Oct 1, 2024
2 parents bdc7994 + cdb2d59 commit baee280
Show file tree
Hide file tree
Showing 92 changed files with 2,265 additions and 697 deletions.
9 changes: 4 additions & 5 deletions dotnet/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
<ItemGroup>
<PackageVersion Include="Azure.AI.Inference" Version="1.0.0-beta.1" />
<PackageVersion Include="Microsoft.VisualStudio.Threading" Version="17.11.20" />
<PackageVersion Include="OpenAI" Version="2.0.0-beta.11" />
<PackageVersion Include="System.ClientModel" Version="1.1.0-beta.7" />
<PackageVersion Include="OpenAI" Version="[2.0.0-beta.12]" />
<PackageVersion Include="Azure.AI.ContentSafety" Version="1.0.0" />
<PackageVersion Include="Azure.AI.OpenAI" Version="2.0.0-beta.5" />
<PackageVersion Include="Azure.AI.OpenAI" Version="[2.0.0-beta.6]" />
<PackageVersion Include="Azure.Identity" Version="1.12.0" />
<PackageVersion Include="Azure.Monitor.OpenTelemetry.Exporter" Version="1.3.0" />
<PackageVersion Include="Azure.Search.Documents" Version="11.6.0" />
Expand Down Expand Up @@ -39,7 +38,7 @@
<PackageVersion Include="System.Memory.Data" Version="8.0.0" />
<PackageVersion Include="System.Numerics.Tensors" Version="8.0.0" />
<PackageVersion Include="System.Text.Json" Version="8.0.4" />
<PackageVersion Include="OllamaSharp" Version="3.0.1" />
<PackageVersion Include="OllamaSharp" Version="3.0.8" />
<!-- Tokenizers -->
<PackageVersion Include="Microsoft.ML.Tokenizers" Version="0.22.0-preview.24378.1" />
<PackageVersion Include="Microsoft.DeepDev.TokenizerLib" Version="1.3.3" />
Expand Down Expand Up @@ -102,7 +101,7 @@
<!-- Symbols -->
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
<!-- Toolset -->
<PackageVersion Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="9.0.0-preview.24324.1" />
<PackageVersion Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="9.0.0-preview.24415.1" />
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public async Task NestedChatWithAggregatorAgentAsync()
{
Console.WriteLine($"! {Model}");

OpenAIPromptExecutionSettings jsonSettings = new() { ResponseFormat = ChatResponseFormat.JsonObject };
OpenAIPromptExecutionSettings jsonSettings = new() { ResponseFormat = ChatResponseFormat.CreateJsonObjectFormat() };
OpenAIPromptExecutionSettings autoInvokeSettings = new() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() };

ChatCompletionAgent internalLeaderAgent = CreateAgent(InternalLeaderName, InternalLeaderInstructions);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.

using System.ClientModel;
using System.ClientModel.Primitives;
using Azure.AI.OpenAI;
using Microsoft.SemanticKernel;
Expand Down Expand Up @@ -29,7 +30,7 @@ public async Task RunAsync()
RetryPolicy = new ClientRetryPolicy()
};

var customClient = new AzureOpenAIClient(new Uri(TestConfiguration.AzureOpenAI.Endpoint), TestConfiguration.AzureOpenAI.ApiKey, clientOptions);
var customClient = new AzureOpenAIClient(new Uri(TestConfiguration.AzureOpenAI.Endpoint), new ApiKeyCredential(TestConfiguration.AzureOpenAI.ApiKey), clientOptions);

var kernel = Kernel.CreateBuilder()
.AddAzureOpenAIChatCompletion(TestConfiguration.AzureOpenAI.ChatDeploymentName, customClient)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ private void OutputInnerContent(OpenAI.Chat.ChatCompletion innerContent)
Console.WriteLine($"Created At: {innerContent.CreatedAt}");

Console.WriteLine($"Finish reason: {innerContent.FinishReason}");
Console.WriteLine($"Input tokens usage: {innerContent.Usage.InputTokens}");
Console.WriteLine($"Output tokens usage: {innerContent.Usage.OutputTokens}");
Console.WriteLine($"Total tokens usage: {innerContent.Usage.TotalTokens}");
Console.WriteLine($"Input tokens usage: {innerContent.Usage.InputTokenCount}");
Console.WriteLine($"Output tokens usage: {innerContent.Usage.OutputTokenCount}");
Console.WriteLine($"Total tokens usage: {innerContent.Usage.TotalTokenCount}");
Console.WriteLine($"Refusal: {innerContent.Refusal} ");
Console.WriteLine($"Id: {innerContent.Id}");
Console.WriteLine($"System fingerprint: {innerContent.SystemFingerprint}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ private void OutputInnerContent(OpenAI.Chat.StreamingChatCompletionUpdate stream
{
Console.WriteLine($" Image uri: {contentUpdate.ImageUri}");
Console.WriteLine($" Image media type: {contentUpdate.ImageBytesMediaType}");
Console.WriteLine($" Image detail: {contentUpdate.ImageDetail}");
Console.WriteLine($" Image detail: {contentUpdate.ImageDetailLevel}");
Console.WriteLine($" Image bytes: {contentUpdate.ImageBytes}");
Console.WriteLine(" =======");
}
Expand Down Expand Up @@ -355,9 +355,9 @@ private void OutputInnerContent(OpenAI.Chat.StreamingChatCompletionUpdate stream
// https://platform.openai.com/docs/api-reference/chat/create#chat-create-stream_options
if (streamChunk.Usage is not null)
{
Console.WriteLine($"Usage input tokens: {streamChunk.Usage.InputTokens}");
Console.WriteLine($"Usage output tokens: {streamChunk.Usage.OutputTokens}");
Console.WriteLine($"Usage total tokens: {streamChunk.Usage.TotalTokens}");
Console.WriteLine($"Usage input tokens: {streamChunk.Usage.InputTokenCount}");
Console.WriteLine($"Usage output tokens: {streamChunk.Usage.OutputTokenCount}");
Console.WriteLine($"Usage total tokens: {streamChunk.Usage.TotalTokenCount}");
}
Console.WriteLine("------------------------");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public async Task StructuredOutputsWithChatResponseFormatAsync()

// Initialize ChatResponseFormat object with JSON schema of desired response format.
ChatResponseFormat chatResponseFormat = ChatResponseFormat.CreateJsonSchemaFormat(
name: "movie_result",
jsonSchemaFormatName: "movie_result",
jsonSchema: BinaryData.FromString("""
{
"type": "object",
Expand All @@ -67,7 +67,7 @@ public async Task StructuredOutputsWithChatResponseFormatAsync()
"additionalProperties": false
}
"""),
strictSchemaEnabled: true);
jsonSchemaIsStrict: true);

// Specify response format by setting ChatResponseFormat object in prompt execution settings.
var executionSettings = new OpenAIPromptExecutionSettings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ public FunctionResultTestDataGen(FunctionResult functionResult, long executionTi
var usage = FunctionResult.Metadata?["Usage"] as ChatTokenUsage;

return new TokenCounts(
completionTokens: usage?.OutputTokens ?? 0,
promptTokens: usage?.InputTokens ?? 0,
totalTokens: usage?.TotalTokens ?? 0);
completionTokens: usage?.OutputTokenCount ?? 0,
promptTokens: usage?.InputTokenCount ?? 0,
totalTokens: usage?.TotalTokenCount ?? 0);
}

private static readonly JsonSerializerOptions s_jsonSerializerOptions = new()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ private int GetChatHistoryTokens(ChatHistory? chatHistory)
usage is ChatTokenUsage completionsUsage &&
completionsUsage is not null)
{
tokens += completionsUsage.TotalTokens;
tokens += completionsUsage.TotalTokenCount;
}
}

Expand Down
184 changes: 184 additions & 0 deletions dotnet/samples/Concepts/TextToImage/OpenAI_TextToImage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
// Copyright (c) Microsoft. All rights reserved.

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Http.Resilience;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Microsoft.SemanticKernel.TextToImage;

namespace TextToImage;

// The following example shows how to use Semantic Kernel with OpenAI DALL-E 2 to create images
public class OpenAI_TextToImage(ITestOutputHelper output) : BaseTest(output)
{
[Fact]
public async Task OpenAIDallE2Async()
{
Console.WriteLine("======== OpenAI DALL-E 2 Text To Image ========");

Kernel kernel = Kernel.CreateBuilder()
.AddOpenAITextToImage(TestConfiguration.OpenAI.ApiKey) // Add your text to image service
.AddOpenAIChatCompletion(TestConfiguration.OpenAI.ChatModelId, TestConfiguration.OpenAI.ApiKey) // Add your chat completion service
.Build();

ITextToImageService dallE = kernel.GetRequiredService<ITextToImageService>();

var imageDescription = "A cute baby sea otter";
var images = await dallE.GetImageContentsAsync(imageDescription, new OpenAITextToImageExecutionSettings { Size = (256, 256) });
var image = images[0].Uri!.ToString();
Console.WriteLine(imageDescription);
Console.WriteLine("Image URL: " + image);

/* Output:
A cute baby sea otter
Image URL: https://oaidalleapiprodscus.blob.core.windows.net/private/....
*/

Console.WriteLine("======== Chat with images ========");

var chatGPT = kernel.GetRequiredService<IChatCompletionService>();
var chatHistory = new ChatHistory(
"You're chatting with a user. Instead of replying directly to the user" +
" provide the description of an image that expresses what you want to say." +
" The user won't see your message, they will see only the image. The system " +
" generates an image using your description, so it's important you describe the image with details.");

var msg = "Hi, I'm from Tokyo, where are you from?";
chatHistory.AddUserMessage(msg);
Console.WriteLine("User: " + msg);

var reply = await chatGPT.GetChatMessageContentAsync(chatHistory);
chatHistory.Add(reply);
images = await dallE.GetImageContentsAsync(reply.Content!, new OpenAITextToImageExecutionSettings { Size = (256, 256) });
image = images[0].Uri!.ToString();
Console.WriteLine("Bot: " + image);
Console.WriteLine("Img description: " + reply);

msg = "Oh, wow. Not sure where that is, could you provide more details?";
chatHistory.AddUserMessage(msg);
Console.WriteLine("User: " + msg);

reply = await chatGPT.GetChatMessageContentAsync(chatHistory);
chatHistory.Add(reply);
images = await dallE.GetImageContentsAsync(reply.Content!, new OpenAITextToImageExecutionSettings { Size = (256, 256) });
image = images[0].Uri!.ToString();
Console.WriteLine("Bot: " + image);
Console.WriteLine("Img description: " + reply);

/* Output:
User: Hi, I'm from Tokyo, where are you from?
Bot: https://oaidalleapiprodscus.blob.core.windows.net/private/...
Img description: [An image of a globe with a pin dropped on a location in the middle of the ocean]
User: Oh, wow. Not sure where that is, could you provide more details?
Bot: https://oaidalleapiprodscus.blob.core.windows.net/private/...
Img description: [An image of a map zooming in on the pin location, revealing a small island with a palm tree on it]
*/
}

[Fact]
public async Task SimpleTextToImageExampleAsync()
{
var builder = Kernel.CreateBuilder()
.AddAzureOpenAITextToImage( // Add your text to image service
deploymentName: TestConfiguration.AzureOpenAI.ImageDeploymentName,
endpoint: TestConfiguration.AzureOpenAI.ImageEndpoint,
apiKey: TestConfiguration.AzureOpenAI.ImageApiKey,
modelId: TestConfiguration.AzureOpenAI.ImageModelId);

var kernel = builder.Build();
var service = kernel.GetRequiredService<ITextToImageService>();

var generatedImages = await service.GetImageContentsAsync(new TextContent("A cute baby sea otter"), new OpenAITextToImageExecutionSettings { Size = (Width: 1792, Height: 1024) });

this.Output.WriteLine(generatedImages[0].Uri!.ToString());
}

[Fact]
public async Task OpenAIDallE3Async()
{
Console.WriteLine("======== OpenAI DALL-E 3 Text To Image ========");

var builder = Kernel.CreateBuilder()
.AddOpenAITextToImage( // Add your text to image service
modelId: "dall-e-3",
apiKey: TestConfiguration.OpenAI.ApiKey) //DALL-E 3 is only supported in this version
.AddOpenAIChatCompletion( // Add your chat completion service
modelId: TestConfiguration.OpenAI.ChatModelId,
apiKey: TestConfiguration.OpenAI.ApiKey);

builder.Services.ConfigureHttpClientDefaults(c =>
{
// Use a standard resiliency policy, augmented to retry 5 times
c.AddStandardResilienceHandler().Configure(o =>
{
o.Retry.MaxRetryAttempts = 5;
o.TotalRequestTimeout.Timeout = TimeSpan.FromSeconds(120);
});
});

var kernel = builder.Build();

ITextToImageService dallE = kernel.GetRequiredService<ITextToImageService>();
var imageDescription = "A cute baby sea otter";
var images = await dallE.GetImageContentsAsync(imageDescription, new OpenAITextToImageExecutionSettings { Size = (1024, 1024) });

Console.WriteLine(imageDescription);
Console.WriteLine("Image URL: " + images[0].Uri!);

/* Output:
A cute baby sea otter
Image URL: https://oaidalleapiprodscus.blob.core.windows.net/private/org-/....
*/

Console.WriteLine("======== Chat with images ========");

var chatGPT = kernel.GetRequiredService<IChatCompletionService>();
var chatHistory = new ChatHistory(
"You're chatting with a user. Instead of replying directly to the user" +
" provide the description of an image that expresses what you want to say." +
" The user won't see your message, they will see only the image. The system " +
" generates an image using your description, so it's important you describe the image with details.");

var msg = "Hi, I'm from Tokyo, where are you from?";
chatHistory.AddUserMessage(msg);
Console.WriteLine("User: " + msg);

var reply = await chatGPT.GetChatMessageContentAsync(chatHistory);
chatHistory.Add(reply);
images = await dallE.GetImageContentsAsync(reply.Content!, new OpenAITextToImageExecutionSettings { Size = (1024, 1024) });
var image = images[0].Uri!.ToString();
Console.WriteLine("Bot: " + image);
Console.WriteLine("Img description: " + reply);

msg = "Oh, wow. Not sure where that is, could you provide more details?";
chatHistory.AddUserMessage(msg);
Console.WriteLine("User: " + msg);

reply = await chatGPT.GetChatMessageContentAsync(chatHistory);
chatHistory.Add(reply);
images = await dallE.GetImageContentsAsync(reply.Content!, new OpenAITextToImageExecutionSettings { Size = (1024, 1024) });
image = images[0].Uri!.ToString();
Console.WriteLine("Bot: " + image);
Console.WriteLine("Img description: " + reply);

/* Output:
User: Hi, I'm from Tokyo, where are you from?
Bot: https://dalleproduse.blob.core.windows.net/private/images/......
Img description: [An image of a globe with a pin dropped on a location in the middle of the ocean]
User: Oh, wow. Not sure where that is, could you provide more details?
Bot: https://dalleproduse.blob.core.windows.net/private/images/......
Img description: [An image of a map zooming in on the pin location, revealing a small island with a palm tree on it]
*/
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

namespace TextToImage;

// The following example shows how to use Semantic Kernel with OpenAI DALL-E 2 to create images
public class OpenAI_TextToImageDalle3(ITestOutputHelper output) : BaseTest(output)
/// <summary>
/// The following example shows how you can still use the previous "ITextToImageService.GenerateImageAsync" API to generate images.
/// </summary>
public class OpenAI_TextToImageLegacy(ITestOutputHelper output) : BaseTest(output)
{
[Fact]
public async Task OpenAIDallEAsync()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) Microsoft. All rights reserved.

using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents.OpenAI;
using Microsoft.SemanticKernel.ChatCompletion;
Expand Down Expand Up @@ -39,7 +40,7 @@ await OpenAIAssistantAgent.CreateAsync(
await vectorStoreClient.CreateVectorStoreAsync(
new VectorStoreCreationOptions()
{
FileIds = [fileInfo.Id],
FileIds = { fileInfo.Id },
Metadata = { { AssistantSampleMetadataKey, bool.TrueString } }
});

Expand Down
4 changes: 2 additions & 2 deletions dotnet/samples/GettingStartedWithProcesses/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ The getting started with agents examples include:

Example|Description
---|---
[Step01_Processes](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/GettingStartedWithProcesses/Step01_Processes.cs)|How to create a simple process with a loop and a conditional exit
[Step02_AccountOpening](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/GettingStartedWithProcesses/Step02_AccountOpening.cs)|Showcasing processes cycles, fan in, fan out for opening an account.
[Step01_Processes](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/GettingStartedWithProcesses/Step01/Step01_Processes.cs)|How to create a simple process with a loop and a conditional exit
[Step02_AccountOpening](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/GettingStartedWithProcesses/Step02/Step02_AccountOpening.cs)|Showcasing processes cycles, fan in, fan out for opening an account.

### Step01_Processes

Expand Down
Loading

0 comments on commit baee280

Please sign in to comment.