Skip to content

Commit a69f0d7

Browse files
authored
Create ImageFileUpload.md
Original file: Sample18_PersistentAgents_ImageFileInputs.md Fixed up example and added the file upload cleanup that was missing.
1 parent f7281a3 commit a69f0d7

File tree

1 file changed

+209
-0
lines changed

1 file changed

+209
-0
lines changed
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
# Sample using agents with Image File as an input in Azure.AI.Agents
2+
3+
Demonstrates examples of sending an image file (along with optional text) as a structured content block in a single message. The examples shows how to create an agent, open a thread, post content blocks combining text and image inputs, and then run the agent to see how it interprets the multimedia input.
4+
5+
1. First get `ProjectEndpoint` and `ModelDeploymentName` from config and create a `PersistentAgentsClient`.
6+
7+
```C# Snippet:AgentsImageFileInMessageCreateClient
8+
var projectEndpoint = configuration["ProjectEndpoint"];
9+
var modelDeploymentName = configuration["ModelDeploymentName"];
10+
PersistentAgentsClient client = new(new Uri(projectEndpoint), new DefaultAzureCredential());
11+
```
12+
13+
2. Upload a file for referencing in your message:
14+
15+
Synchronous sample:
16+
```C# Snippet:AgentsImageFileInMessageUpload_Sync
17+
var completeFilePath = "C:\\Users\\username\\Downloads\\image.jpg";
18+
19+
PersistentAgentFile uploadedFile = client.UploadFile(
20+
filePath: completeFilePath,
21+
purpose: PersistentAgentFilePurpose.Agents
22+
);
23+
```
24+
25+
Asynchronous sample:
26+
```C# Snippet:AgentsImageFileInMessageUpload
27+
var completeFilePath = "C:\\Users\\username\\Downloads\\image.jpg";
28+
29+
PersistentAgentFile uploadedFile = await client.UploadFileAsync(
30+
filePath: completeFilePath,
31+
purpose: PersistentAgentFilePurpose.Agents
32+
);
33+
```
34+
35+
3. Create an agent.
36+
37+
Synchronous sample:
38+
```C# Snippet:AgentsImageFileInMessageCreateAgent_Sync
39+
PersistentAgent agent = client.CreateAgent(
40+
model: modelDeploymentName,
41+
name: "File Image Understanding Agent",
42+
instructions: "Analyze images from internally uploaded files."
43+
);
44+
```
45+
46+
Asynchronous sample:
47+
```C# Snippet:AgentsImageFileInMessageCreateAgent
48+
PersistentAgent agent = await client.CreateAgentAsync(
49+
model: modelDeploymentName,
50+
name: "File Image Understanding Agent",
51+
instructions: "Analyze images from internally uploaded files."
52+
);
53+
```
54+
55+
4. Create a thread.
56+
57+
Synchronous sample:
58+
```C# Snippet:AgentsImageFileInMessageCreateThread_Sync
59+
PersistentAgentThread thread = client.CreateThread();
60+
```
61+
62+
Asynchronous sample:
63+
```C# Snippet:AgentsImageFileInMessageCreateThread
64+
PersistentAgentThread thread = await client.CreateThreadAsync();
65+
```
66+
67+
5. Create a message referencing the uploaded file.
68+
69+
Synchronous sample:
70+
```C# Snippet:AgentsImageFileInMessageCreateMessage_Sync
71+
var contentBlocks = new List<MessageInputContentBlock>
72+
{
73+
new MessageInputTextBlock("Here is an uploaded file. Please describe it:"),
74+
new MessageInputImageFileBlock(new MessageImageFileParam(uploadedFile.Id))
75+
};
76+
77+
client.CreateMessage(
78+
threadId: thread.Id,
79+
role: MessageRole.User,
80+
contentBlocks: contentBlocks
81+
);
82+
```
83+
84+
Asynchronous sample:
85+
```C# Snippet:AgentsImageFileInMessageCreateMessage
86+
var contentBlocks = new List<MessageInputContentBlock>
87+
{
88+
new MessageInputTextBlock("Here is an uploaded file. Please describe it:"),
89+
new MessageInputImageFileBlock(new MessageImageFileParam(uploadedFile.Id))
90+
};
91+
92+
await client.CreateMessageAsync(
93+
thread.Id,
94+
MessageRole.User,
95+
contentBlocks: contentBlocks
96+
);
97+
```
98+
99+
6. Run the agent.
100+
101+
Synchronous sample:
102+
```C# Snippet:AgentsImageFileInMessageCreateRun_Sync
103+
ThreadRun run = client.CreateRun(
104+
threadId: thread.Id,
105+
assistantId: agent.Id
106+
);
107+
```
108+
109+
Asynchronous sample:
110+
```C# Snippet:AgentsImageFileInMessageCreateRun
111+
ThreadRun run = await client.CreateRunAsync(
112+
threadId: thread.Id,
113+
assistantId: agent.Id
114+
);
115+
```
116+
117+
7. Wait for the run to complete.
118+
119+
Synchronous sample:
120+
```C# Snippet:AgentsImageFileInMessageWaitForRun_Sync
121+
do
122+
{
123+
Thread.Sleep(500);
124+
run = client.GetRun(thread.Id, run.Id);
125+
}
126+
while (run.Status == RunStatus.Queued
127+
|| run.Status == RunStatus.InProgress);
128+
```
129+
130+
Asynchronous sample:
131+
```C# Snippet:AgentsImageFileInMessageWaitForRun
132+
do
133+
{
134+
await Task.Delay(500);
135+
run = await client.GetRunAsync(thread.Id, run.Id);
136+
}
137+
while (run.Status == RunStatus.Queued
138+
|| run.Status == RunStatus.InProgress);
139+
```
140+
141+
8. Retrieve messages (including any agent responses) and print them.
142+
143+
Synchronous sample:
144+
```C# Snippet:AgentsImageFileInMessageReview_Sync
145+
PageableList<ThreadMessage> messages = client.GetMessages(
146+
thread.Id,
147+
order: ListSortOrder.Ascending);
148+
149+
foreach (ThreadMessage threadMessage in messages)
150+
{
151+
foreach (MessageContent content in threadMessage.ContentItems)
152+
{
153+
switch (content)
154+
{
155+
case MessageTextContent textItem:
156+
Console.Write($"{threadMessage.Role}: {textItem.Text}");
157+
Console.WriteLine();
158+
break;
159+
160+
case MessageImageFileContent fileItem:
161+
Console.WriteLine($"{threadMessage.Role}: Image File (internal ID): {fileItem.FileId}");
162+
Console.WriteLine();
163+
break;
164+
}
165+
}
166+
}
167+
```
168+
169+
Asynchronous sample:
170+
```C# Snippet:AgentsImageFileInMessageReview
171+
PageableList<ThreadMessage> messages = await client.GetMessagesAsync(
172+
thread.Id,
173+
order: ListSortOrder.Ascending);
174+
175+
foreach (ThreadMessage threadMessage in messages)
176+
{
177+
foreach (MessageContent content in threadMessage.ContentItems)
178+
{
179+
switch (content)
180+
{
181+
case MessageTextContent textItem:
182+
Console.Write($"{threadMessage.Role}: {textItem.Text}");
183+
Console.WriteLine();
184+
break;
185+
186+
case MessageImageFileContent fileItem:
187+
Console.WriteLine($"{threadMessage.Role}: Image File (internal ID): {fileItem.FileId}");
188+
Console.WriteLine();
189+
break;
190+
}
191+
}
192+
}
193+
```
194+
195+
9. Finally, we delete all the resources, we have created in this sample.
196+
197+
Synchronous sample:
198+
```C# Snippet:AgentsImageFileInMessageCleanup_Sync
199+
client.DeleteThread(thread.Id);
200+
client.DeleteAgent(agent.Id);
201+
client.DeleteFile(uploadedFile.Id);
202+
```
203+
204+
Asynchronous sample:
205+
```C# Snippet:AgentsImageFileInMessageCleanup
206+
await client.DeleteFileAsync(uploadedFile.Id);
207+
await client.DeleteThreadAsync(thread.Id);
208+
await client.DeleteAgentAsync(agent.Id);
209+
```

0 commit comments

Comments
 (0)