-
Notifications
You must be signed in to change notification settings - Fork 4k
.Net Kernel Contents Graduation #6319
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
Merged
RogerBarreto
merged 43 commits into
microsoft:main
from
RogerBarreto:adrs/kernel-content
Jun 10, 2024
Merged
Changes from 30 commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
dc5e81e
Content reset
RogerBarreto 0245a5f
Changes to ADRs + POC Binary
RogerBarreto 53354b5
Binary COntent changes
RogerBarreto 2b921b3
Binary Content more changes
RogerBarreto f4a7cf6
Adding constructors and mimetype req
RogerBarreto 4a93b09
Using ImageContentV2
RogerBarreto 1e0697c
Binary Content Provider with MimeTypes update
RogerBarreto 858aabd
Serialization and Examples
RogerBarreto a4fb379
Making ImageContent a bit less of a breaking change, more WIP and tes…
RogerBarreto 1f2e39b
Renaming
RogerBarreto cf17153
Kernel Binary Simplified, No Deferred Factories
RogerBarreto 36cb77e
UT passing for simple binary
RogerBarreto 32712d2
UT for ImageContent passes
RogerBarreto 6c09334
Ensuring Old Image Behavior
RogerBarreto 1a60b45
Adding Tests, BinaryContent with Data + Uri, consolidation
RogerBarreto 06fe63c
Meeting presentation status
RogerBarreto ea7d29e
ImageContent consistency breaking change
RogerBarreto eadda16
Adding DataUri Formatter + Base64 validation + UT
RogerBarreto d17a5a1
Fix usings
RogerBarreto 9251e34
Adjusting Unit Tests
RogerBarreto 639dd7e
Update CallId, bring back Experimental, remove SmartBinaryContent POC…
RogerBarreto 6f5ed3b
Removing draft examples
RogerBarreto 4a11118
Adding Suppressions + Fix Warnings
RogerBarreto 8b4f319
Adding support for dataUri parameters to metadata + UT fix
RogerBarreto 1c5e379
Merge fix
RogerBarreto 3dca78c
Address typos
RogerBarreto 1982557
ADR update + Unit Tests added
RogerBarreto 6cb5ae8
Updating ADR with latest changes
RogerBarreto fe9e83e
Encoding checks + Warning fixes
RogerBarreto 89b128d
Merge branch 'main' into adrs/kernel-content
RogerBarreto db95789
Addressing PR Feedback
RogerBarreto 08bcdc6
Address latest PR feedback
RogerBarreto c0fe577
Breaking changes details
RogerBarreto c34a329
Merge branch 'main' into adrs/kernel-content
RogerBarreto a2797a9
Fixing error constructor
RogerBarreto 407929d
Address PR comments
RogerBarreto 3dc731f
Remove suppresion setting
RogerBarreto 04434bb
Merge branch 'main' into adrs/kernel-content
RogerBarreto a671e36
Merge conflict
RogerBarreto 7aabaec
Merge branch 'adrs/kernel-content' of https://github.com/RogerBarreto…
RogerBarreto 52e7c1c
Adding suppressions
RogerBarreto 4772859
Update suppressions for OpenAI
RogerBarreto 0d05bcb
Merge branch 'main' into adrs/kernel-content
RogerBarreto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
dotnet/samples/Concepts/Agents/OpenAIAssistant_MultipleContents.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
using Azure.AI.OpenAI.Assistants; | ||
using Microsoft.SemanticKernel; | ||
using Microsoft.SemanticKernel.Agents; | ||
using Microsoft.SemanticKernel.Agents.OpenAI; | ||
using Microsoft.SemanticKernel.ChatCompletion; | ||
using Microsoft.SemanticKernel.Connectors.OpenAI; | ||
using Resources; | ||
|
||
namespace Agents; | ||
|
||
/// <summary> | ||
/// Demonstrate using retrieval on <see cref="OpenAIAssistantAgent"/> . | ||
/// </summary> | ||
public class OpenAIAssistant_MultipleContents(ITestOutputHelper output) : BaseTest(output) | ||
{ | ||
/// <summary> | ||
/// Retrieval tool not supported on Azure OpenAI. | ||
/// </summary> | ||
protected override bool ForceOpenAI => true; | ||
|
||
[Fact] | ||
public async Task RunAsync() | ||
{ | ||
OpenAIFileService fileService = new(TestConfiguration.OpenAI.ApiKey); | ||
|
||
BinaryContent[] files = [ | ||
// Audio is not supported by Assistant API | ||
// new AudioContent(await EmbeddedResource.ReadAllAsync("test_audio.wav")!, mimeType:"audio/wav", innerContent: "test_audio.wav"), | ||
new ImageContent(await EmbeddedResource.ReadAllAsync("sample_image.jpg")!, mimeType: "image/jpeg") { InnerContent = "sample_image.jpg" }, | ||
new ImageContent(await EmbeddedResource.ReadAllAsync("test_image.jpg")!, mimeType: "image/jpeg") { InnerContent = "test_image.jpg" }, | ||
new BinaryContent(data: await EmbeddedResource.ReadAllAsync("travelinfo.txt"), mimeType: "text/plain") | ||
{ | ||
InnerContent = "travelinfo.txt" | ||
} | ||
]; | ||
|
||
var fileIds = new List<string>(); | ||
foreach (var file in files) | ||
{ | ||
try | ||
{ | ||
var uploadFile = await fileService.UploadContentAsync(file, | ||
new OpenAIFileUploadExecutionSettings(file.InnerContent!.ToString()!, Microsoft.SemanticKernel.Connectors.OpenAI.OpenAIFilePurpose.Assistants)); | ||
|
||
fileIds.Add(uploadFile.Id); | ||
} | ||
catch (HttpOperationException hex) | ||
{ | ||
Console.WriteLine(hex.ResponseContent); | ||
Assert.Fail($"Failed to upload file: {hex.Message}"); | ||
} | ||
} | ||
|
||
// Define the agent | ||
OpenAIAssistantAgent agent = | ||
await OpenAIAssistantAgent.CreateAsync( | ||
kernel: new(), | ||
config: new(this.ApiKey, this.Endpoint), | ||
new() | ||
{ | ||
EnableRetrieval = true, // Enable retrieval | ||
ModelId = this.Model, | ||
// FileIds = fileIds Currently Assistant API only supports text files, no images or audio. | ||
FileIds = [fileIds.Last()] | ||
}); | ||
|
||
// Create a chat for agent interaction. | ||
var chat = new AgentGroupChat(); | ||
|
||
// Respond to user input | ||
try | ||
{ | ||
await InvokeAgentAsync("Where did sam go?"); | ||
await InvokeAgentAsync("When does the flight leave Seattle?"); | ||
await InvokeAgentAsync("What is the hotel contact info at the destination?"); | ||
} | ||
finally | ||
{ | ||
await agent.DeleteAsync(); | ||
} | ||
|
||
// Local function to invoke agent and display the conversation messages. | ||
async Task InvokeAgentAsync(string input) | ||
{ | ||
chat.AddChatMessage(new ChatMessageContent(AuthorRole.User, input)); | ||
|
||
Console.WriteLine($"# {AuthorRole.User}: '{input}'"); | ||
|
||
await foreach (var content in chat.InvokeAsync(agent)) | ||
{ | ||
Console.WriteLine($"# {content.Role} - {content.AuthorName ?? "*"}: '{content.Content}'"); | ||
} | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task SendingAndRetrievingFilesAsync() | ||
{ | ||
var openAIClient = new AssistantsClient(TestConfiguration.OpenAI.ApiKey); | ||
OpenAIFileService fileService = new(TestConfiguration.OpenAI.ApiKey); | ||
|
||
BinaryContent[] files = [ | ||
new AudioContent(await EmbeddedResource.ReadAllAsync("test_audio.wav")!, mimeType: "audio/wav") { InnerContent = "test_audio.wav" }, | ||
new ImageContent(await EmbeddedResource.ReadAllAsync("sample_image.jpg")!, mimeType: "image/jpeg") { InnerContent = "sample_image.jpg" }, | ||
new ImageContent(await EmbeddedResource.ReadAllAsync("test_image.jpg")!, mimeType: "image/jpeg") { InnerContent = "test_image.jpg" }, | ||
new BinaryContent(data: await EmbeddedResource.ReadAllAsync("travelinfo.txt"), mimeType: "text/plain") { InnerContent = "travelinfo.txt" } | ||
]; | ||
|
||
var fileIds = new Dictionary<string, BinaryContent>(); | ||
foreach (var file in files) | ||
{ | ||
var result = await openAIClient.UploadFileAsync(new BinaryData(file.Data), Azure.AI.OpenAI.Assistants.OpenAIFilePurpose.FineTune); | ||
fileIds.Add(result.Value.Id, file); | ||
} | ||
|
||
foreach (var file in (await openAIClient.GetFilesAsync(Azure.AI.OpenAI.Assistants.OpenAIFilePurpose.FineTune)).Value) | ||
{ | ||
if (!fileIds.ContainsKey(file.Id)) | ||
{ | ||
continue; | ||
} | ||
|
||
var data = (await openAIClient.GetFileContentAsync(file.Id)).Value; | ||
|
||
var mimeType = fileIds[file.Id].MimeType; | ||
var fileName = fileIds[file.Id].InnerContent!.ToString(); | ||
var metadata = new Dictionary<string, object?> { ["id"] = file.Id }; | ||
var uri = new Uri($"https://api.openai.com/v1/files/{file.Id}/content"); | ||
var content = mimeType switch | ||
{ | ||
"image/jpeg" => new ImageContent(data, mimeType) { Uri = uri, InnerContent = fileName, Metadata = metadata }, | ||
"audio/wav" => new AudioContent(data, mimeType) { Uri = uri, InnerContent = fileName, Metadata = metadata }, | ||
_ => new BinaryContent(data, mimeType) { Uri = uri, InnerContent = fileName, Metadata = metadata } | ||
}; | ||
|
||
Console.WriteLine($"File: {fileName} - {mimeType}"); | ||
|
||
// Images tostring are different from the graduated contents for retrocompatibility | ||
Console.WriteLine(content.ToString()); | ||
|
||
// Delete the test file remotely | ||
await openAIClient.DeleteFileAsync(file.Id); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.