-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathStep03_Assistant_Vision.cs
73 lines (62 loc) · 3 KB
/
Step03_Assistant_Vision.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents.OpenAI;
using Microsoft.SemanticKernel.ChatCompletion;
using OpenAI.Assistants;
using Resources;
namespace GettingStarted.OpenAIAssistants;
/// <summary>
/// Demonstrate providing image input to <see cref="OpenAIAssistantAgent"/> .
/// </summary>
public class Step03_Assistant_Vision(ITestOutputHelper output) : BaseAssistantTest(output)
{
/// <summary>
/// Azure currently only supports message of type=text.
/// </summary>
protected override bool ForceOpenAI => true;
[Fact]
public async Task UseImageContentWithAssistantAsync()
{
// Define the assistant
Assistant assistant =
await this.AssistantClient.CreateAssistantAsync(
this.Model,
metadata: SampleMetadata);
// Create the agent
OpenAIAssistantAgent agent = new(assistant, this.AssistantClient);
// Upload an image
await using Stream imageStream = EmbeddedResource.ReadStream("cat.jpg")!;
string fileId = await this.Client.UploadAssistantFileAsync(imageStream, "cat.jpg");
// Create a thread for the agent conversation.
string threadId = await this.AssistantClient.CreateThreadAsync(metadata: SampleMetadata);
// Respond to user input
try
{
// Refer to public image by url
await InvokeAgentAsync(CreateMessageWithImageUrl("Describe this image.", "https://upload.wikimedia.org/wikipedia/commons/thumb/4/47/New_york_times_square-terabass.jpg/1200px-New_york_times_square-terabass.jpg"));
await InvokeAgentAsync(CreateMessageWithImageUrl("What are is the main color in this image?", "https://upload.wikimedia.org/wikipedia/commons/5/56/White_shark.jpg"));
// Refer to uploaded image by file-id.
await InvokeAgentAsync(CreateMessageWithImageReference("Is there an animal in this image?", fileId));
}
finally
{
await this.AssistantClient.DeleteThreadAsync(threadId);
await this.AssistantClient.DeleteAssistantAsync(agent.Id);
await this.Client.DeleteFileAsync(fileId);
}
// Local function to invoke agent and display the conversation messages.
async Task InvokeAgentAsync(ChatMessageContent message)
{
await agent.AddChatMessageAsync(threadId, message);
this.WriteAgentChatMessage(message);
await foreach (ChatMessageContent response in agent.InvokeAsync(threadId))
{
this.WriteAgentChatMessage(response);
}
}
}
private ChatMessageContent CreateMessageWithImageUrl(string input, string url)
=> new(AuthorRole.User, [new TextContent(input), new ImageContent(new Uri(url))]);
private ChatMessageContent CreateMessageWithImageReference(string input, string fileId)
=> new(AuthorRole.User, [new TextContent(input), new FileReferenceContent(fileId)]);
}