Skip to content

Commit 9521362

Browse files
authored
Merging codeblocks
1 parent d34d350 commit 9521362

File tree

1 file changed

+53
-65
lines changed

1 file changed

+53
-65
lines changed

articles/ai-services/openai/includes/assistants-csharp.md

Lines changed: 53 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -74,79 +74,67 @@ In our code we are going to specify the following values:
7474

7575
An individual assistant can access up to 128 tools including `code interpreter`, as well as any custom tools you create via [functions](../how-to/assistant-functions.md).
7676

77-
1. Create the `AssistantClient`:
77+
Create and run an assistant with the following:
7878

79-
```csharp
80-
using Azure;
81-
using Azure.AI.OpenAI.Assistants;
79+
```csharp
80+
using Azure;
81+
using Azure.AI.OpenAI.Assistants;
8282

83-
string endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new ArgumentNullException("AZURE_OPENAI_ENDPOINT");
84-
string key = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY") ?? throw new ArgumentNullException("AZURE_OPENAI_API_KEY");
85-
AssistantsClient client = new AssistantsClient(new Uri(endpoint), new AzureKeyCredential(key));
86-
```
83+
string endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new ArgumentNullException("AZURE_OPENAI_ENDPOINT");
84+
string key = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY") ?? throw new ArgumentNullException("AZURE_OPENAI_API_KEY");
85+
AssistantsClient client = new AssistantsClient(new Uri(endpoint), new AzureKeyCredential(key));
8786

88-
1. Create an assistant:
89-
90-
```csharp
91-
// Create an assistant
92-
Assistant assistant = await client.CreateAssistantAsync(
93-
new AssistantCreationOptions("gpt-4-1106-preview") // Replace this with the name of your model deployment
94-
{
95-
Name = "Math Tutor",
96-
Instructions = "You are a personal math tutor. Write and run code to answer math questions.",
97-
Tools = { new CodeInterpreterToolDefinition() }
98-
});
99-
```
100-
101-
1. Create a thread and add a message to it:
102-
103-
```csharp
104-
// Create a thread
105-
AssistantThread thread = await client.CreateThreadAsync();
106-
107-
// Add a user question to the thread
108-
ThreadMessage message = await client.CreateMessageAsync(
109-
thread.Id,
110-
MessageRole.User,
111-
"I need to solve the equation `3x + 11 = 14`. Can you help me?");
112-
```
113-
114-
1. Execute the thread and display the result:
115-
116-
```csharp
117-
// Run the thread
118-
ThreadRun run = await client.CreateRunAsync(
119-
thread.Id,
120-
new CreateRunOptions(assistant.Id)
121-
);
122-
123-
// Wait for the assistant to respond
124-
do
87+
// Create an assistant
88+
Assistant assistant = await client.CreateAssistantAsync(
89+
new AssistantCreationOptions("gpt-4-1106-preview") // Replace this with the name of your model deployment
12590
{
126-
await Task.Delay(TimeSpan.FromMilliseconds(500));
127-
run = await client.GetRunAsync(thread.Id, run.Id);
128-
}
129-
while (run.Status == RunStatus.Queued
130-
|| run.Status == RunStatus.InProgress);
131-
132-
// Get the messages
133-
PageableList<ThreadMessage> messagesPage = await client.GetMessagesAsync(thread.Id);
134-
IReadOnlyList<ThreadMessage> messages = messagesPage.Data;
135-
136-
// Note: messages iterate from newest to oldest, with the messages[0] being the most recent
137-
foreach (ThreadMessage threadMessage in messages.Reverse())
91+
Name = "Math Tutor",
92+
Instructions = "You are a personal math tutor. Write and run code to answer math questions.",
93+
Tools = { new CodeInterpreterToolDefinition() }
94+
});
95+
96+
// Create a thread
97+
AssistantThread thread = await client.CreateThreadAsync();
98+
99+
// Add a user question to the thread
100+
ThreadMessage message = await client.CreateMessageAsync(
101+
thread.Id,
102+
MessageRole.User,
103+
"I need to solve the equation `3x + 11 = 14`. Can you help me?");
104+
105+
// Run the thread
106+
ThreadRun run = await client.CreateRunAsync(
107+
thread.Id,
108+
new CreateRunOptions(assistant.Id)
109+
);
110+
111+
// Wait for the assistant to respond
112+
do
113+
{
114+
await Task.Delay(TimeSpan.FromMilliseconds(500));
115+
run = await client.GetRunAsync(thread.Id, run.Id);
116+
}
117+
while (run.Status == RunStatus.Queued
118+
|| run.Status == RunStatus.InProgress);
119+
120+
// Get the messages
121+
PageableList<ThreadMessage> messagesPage = await client.GetMessagesAsync(thread.Id);
122+
IReadOnlyList<ThreadMessage> messages = messagesPage.Data;
123+
124+
// Note: messages iterate from newest to oldest, with the messages[0] being the most recent
125+
foreach (ThreadMessage threadMessage in messages.Reverse())
126+
{
127+
Console.Write($"{threadMessage.CreatedAt:yyyy-MM-dd HH:mm:ss} - {threadMessage.Role,10}: ");
128+
foreach (MessageContent contentItem in threadMessage.ContentItems)
138129
{
139-
Console.Write($"{threadMessage.CreatedAt:yyyy-MM-dd HH:mm:ss} - {threadMessage.Role,10}: ");
140-
foreach (MessageContent contentItem in threadMessage.ContentItems)
130+
if (contentItem is MessageTextContent textItem)
141131
{
142-
if (contentItem is MessageTextContent textItem)
143-
{
144-
Console.Write(textItem.Text);
145-
}
146-
Console.WriteLine();
132+
Console.Write(textItem.Text);
147133
}
134+
Console.WriteLine();
148135
}
149-
```
136+
}
137+
```
150138

151139
This will print an output as follows:
152140

0 commit comments

Comments
 (0)