Skip to content

Commit 488b0a2

Browse files
committed
Fix further getnewthread and deserializethread instances
1 parent 7229d31 commit 488b0a2

13 files changed

Lines changed: 38 additions & 31 deletions

File tree

agent-framework/integrations/ag-ui/getting-started.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ AIAgent agent = chatClient.AsAIAgent(
178178
name: "agui-client",
179179
description: "AG-UI Client Agent");
180180

181-
AgentThread thread = agent.GetNewThread();
181+
AgentThread thread = await agent.GetNewThreadAsync();
182182
List<ChatMessage> messages =
183183
[
184184
new(ChatRole.System, "You are a helpful assistant.")

agent-framework/migration-guide/from-semantic-kernel/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ The agent is responsible for creating the thread.
103103

104104
```csharp
105105
// New.
106-
AgentThread thread = agent.GetNewThread();
106+
AgentThread thread = await agent.GetNewThreadAsync();
107107
```
108108

109109
## 4. Hosted Agent Thread Cleanup

agent-framework/tutorials/agents/function-tools-approvals.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ Since you now have a function that requires approval, the agent might respond wi
6666
You can check the response content for any `FunctionApprovalRequestContent` instances, which indicates that the agent requires user approval for a function.
6767

6868
```csharp
69-
AgentThread thread = agent.GetNewThread();
69+
AgentThread thread = await agent.GetNewThreadAsync();
7070
AgentResponse response = await agent.RunAsync("What is the weather like in Amsterdam?", thread);
7171

7272
var functionApprovalRequests = response.Messages

agent-framework/tutorials/agents/memory.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ To use the custom `AIContextProvider`, you need to provide an `AIContextProvider
146146

147147
When creating a `ChatClientAgent` it is possible to provide a `ChatClientAgentOptions` object that allows providing the `AIContextProviderFactory` in addition to all other agent options.
148148

149+
The factory is an async function that receives a context object and a cancellation token.
150+
149151
```csharp
150152
using System;
151153
using Azure.AI.OpenAI;
@@ -161,19 +163,20 @@ ChatClient chatClient = new AzureOpenAIClient(
161163
AIAgent agent = chatClient.AsAIAgent(new ChatClientAgentOptions()
162164
{
163165
ChatOptions = new() { Instructions = "You are a friendly assistant. Always address the user by their name." },
164-
AIContextProviderFactory = ctx => new UserInfoMemory(
165-
chatClient.AsIChatClient(),
166-
ctx.SerializedState,
167-
ctx.JsonSerializerOptions)
166+
AIContextProviderFactory = (ctx, ct) => new ValueTask<AIContextProvider>(
167+
new UserInfoMemory(
168+
chatClient.AsIChatClient(),
169+
ctx.SerializedState,
170+
ctx.JsonSerializerOptions))
168171
});
169172
```
170173

171-
When creating a new thread, the `AIContextProvider` will be created by `GetNewThread`
174+
When creating a new thread, the `AIContextProvider` will be created by `GetNewThreadAsync`
172175
and attached to the thread. Once memories are extracted it is therefore possible to access the memory component via the thread's `GetService` method and inspect the memories.
173176

174177
```csharp
175178
// Create a new thread for the conversation.
176-
AgentThread thread = agent.GetNewThread();
179+
AgentThread thread = await agent.GetNewThreadAsync();
177180

178181
Console.WriteLine(await agent.RunAsync("Hello, what is the square root of 9?", thread));
179182
Console.WriteLine(await agent.RunAsync("My name is Ruaidhrí", thread));

agent-framework/tutorials/agents/multi-turn-conversation.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ For prerequisites and creating the agent, see the [Create and run a simple agent
2727
Agents are stateless and do not maintain any state internally between calls.
2828
To have a multi-turn conversation with an agent, you need to create an object to hold the conversation state and pass this object to the agent when running it.
2929

30-
To create the conversation state object, call the `GetNewThread` method on the agent instance.
30+
To create the conversation state object, call the `GetNewThreadAsync` method on the agent instance.
3131

3232
```csharp
33-
AgentThread thread = agent.GetNewThread();
33+
AgentThread thread = await agent.GetNewThreadAsync();
3434
```
3535

3636
You can then pass this thread object to the `RunAsync` and `RunStreamingAsync` methods on the agent instance, along with the user input.
@@ -52,8 +52,8 @@ These threads can then be used to maintain separate conversation states for each
5252
The conversations will be fully independent of each other, since the agent does not maintain any state internally.
5353

5454
```csharp
55-
AgentThread thread1 = agent.GetNewThread();
56-
AgentThread thread2 = agent.GetNewThread();
55+
AgentThread thread1 = await agent.GetNewThreadAsync();
56+
AgentThread thread2 = await agent.GetNewThreadAsync();
5757
Console.WriteLine(await agent.RunAsync("Tell me a joke about a pirate.", thread1));
5858
Console.WriteLine(await agent.RunAsync("Tell me a joke about a robot.", thread2));
5959
Console.WriteLine(await agent.RunAsync("Now add some emojis to the joke and tell it in the voice of a pirate's parrot.", thread1));

agent-framework/tutorials/agents/persisted-conversation.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ AIAgent agent = new AzureOpenAIClient(
3838
.GetChatClient("gpt-4o-mini")
3939
.AsAIAgent(instructions: "You are a helpful assistant.", name: "Assistant");
4040

41-
AgentThread thread = agent.GetNewThread();
41+
AgentThread thread = await agent.GetNewThreadAsync();
4242
```
4343

4444
Run the agent, passing in the thread, so that the `AgentThread` includes this exchange.
@@ -75,7 +75,7 @@ string loadedJson = await File.ReadAllTextAsync(filePath);
7575
JsonElement reloaded = JsonSerializer.Deserialize<JsonElement>(loadedJson, JsonSerializerOptions.Web);
7676

7777
// Deserialize the thread into an AgentThread tied to the same agent type
78-
AgentThread resumedThread = agent.DeserializeThread(reloaded, JsonSerializerOptions.Web);
78+
AgentThread resumedThread = await agent.DeserializeThreadAsync(reloaded, JsonSerializerOptions.Web);
7979
```
8080

8181
Use the resumed thread to continue the conversation.

agent-framework/user-guide/agents/agent-background-responses.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ AgentRunOptions options = new()
6666
AllowBackgroundResponses = true
6767
};
6868

69-
AgentThread thread = agent.GetNewThread();
69+
AgentThread thread = await agent.GetNewThreadAsync();
7070

7171
// Get initial response - may return with or without a continuation token
7272
AgentResponse response = await agent.RunAsync("Write a very long novel about otters in space.", thread, options);
@@ -108,7 +108,7 @@ AgentRunOptions options = new()
108108
AllowBackgroundResponses = true
109109
};
110110

111-
AgentThread thread = agent.GetNewThread();
111+
AgentThread thread = await agent.GetNewThreadAsync();
112112

113113
AgentResponseUpdate? latestReceivedUpdate = null;
114114

agent-framework/user-guide/agents/agent-rag.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@ The `TextSearchProvider` class is an out-of-the-box implementation of a RAG cont
2121

2222
It can easily be attached to a `ChatClientAgent` using the `AIContextProviderFactory` option to provide RAG capabilities to the agent.
2323

24+
The factory is an async function that receives a context object and a cancellation token.
25+
2426
```csharp
2527
// Create the AI agent with the TextSearchProvider as the AI context provider.
2628
AIAgent agent = azureOpenAIClient
2729
.GetChatClient(deploymentName)
2830
.AsAIAgent(new ChatClientAgentOptions
2931
{
3032
ChatOptions = new() { Instructions = "You are a helpful support specialist for Contoso Outdoors. Answer questions using the provided context and cite the source document when available." },
31-
AIContextProviderFactory = ctx => new TextSearchProvider(SearchAdapter, ctx.SerializedState, ctx.JsonSerializerOptions, textSearchOptions)
33+
AIContextProviderFactory = (ctx, ct) => new ValueTask<AIContextProvider>(
34+
new TextSearchProvider(SearchAdapter, ctx.SerializedState, ctx.JsonSerializerOptions, textSearchOptions))
3235
});
3336
```
3437

agent-framework/user-guide/agents/agent-types/custom-agent.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,11 @@ Agents can therefore attach any additional state or behaviors needed to the thre
6868
Two methods are required to be implemented:
6969

7070
```csharp
71-
public override AgentThread GetNewThread() => new CustomAgentThread();
71+
public override Task<AgentThread> GetNewThreadAsync(CancellationToken cancellationToken = default)
72+
=> Task.FromResult<AgentThread>(new CustomAgentThread());
7273

73-
public override AgentThread DeserializeThread(JsonElement serializedThread, JsonSerializerOptions? jsonSerializerOptions = null)
74-
=> new CustomAgentThread(serializedThread, jsonSerializerOptions);
74+
public override Task<AgentThread> DeserializeThreadAsync(JsonElement serializedThread, JsonSerializerOptions? jsonSerializerOptions = null, CancellationToken cancellationToken = default)
75+
=> Task.FromResult<AgentThread>(new CustomAgentThread(serializedThread, jsonSerializerOptions));
7576
```
7677

7778
### Core agent logic
@@ -110,7 +111,7 @@ If you don't do this, the user won't be able to have a multi-turn conversation w
110111
```csharp
111112
public override async Task<AgentResponse> RunAsync(IEnumerable<ChatMessage> messages, AgentThread? thread = null, AgentRunOptions? options = null, CancellationToken cancellationToken = default)
112113
{
113-
thread ??= this.GetNewThread();
114+
thread ??= await this.GetNewThreadAsync(cancellationToken);
114115
List<ChatMessage> responseMessages = CloneAndToUpperCase(messages, this.DisplayName).ToList();
115116
await NotifyThreadOfNewMessagesAsync(thread, messages.Concat(responseMessages), cancellationToken);
116117
return new AgentResponse
@@ -123,7 +124,7 @@ If you don't do this, the user won't be able to have a multi-turn conversation w
123124

124125
public override async IAsyncEnumerable<AgentResponseUpdate> RunStreamingAsync(IEnumerable<ChatMessage> messages, AgentThread? thread = null, AgentRunOptions? options = null, [EnumeratorCancellation] CancellationToken cancellationToken = default)
125126
{
126-
thread ??= this.GetNewThread();
127+
thread ??= await this.GetNewThreadAsync(cancellationToken);
127128
List<ChatMessage> responseMessages = CloneAndToUpperCase(messages, this.DisplayName).ToList();
128129
await NotifyThreadOfNewMessagesAsync(thread, messages.Concat(responseMessages), cancellationToken);
129130
foreach (var message in responseMessages)

agent-framework/user-guide/agents/agent-types/durable-agent/features.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static async Task<string> SpamDetectionOrchestration(
4242

4343
// Check if the email is spam
4444
DurableAIAgent spamDetectionAgent = context.GetAgent("SpamDetectionAgent");
45-
AgentThread spamThread = spamDetectionAgent.GetNewThread();
45+
AgentThread spamThread = await spamDetectionAgent.GetNewThreadAsync();
4646

4747
AgentResponse<DetectionResult> spamDetectionResponse = await spamDetectionAgent.RunAsync<DetectionResult>(
4848
message: $"Analyze this email for spam: {email.EmailContent}",
@@ -56,7 +56,7 @@ public static async Task<string> SpamDetectionOrchestration(
5656

5757
// Generate response for legitimate email
5858
DurableAIAgent emailAssistantAgent = context.GetAgent("EmailAssistantAgent");
59-
AgentThread emailThread = emailAssistantAgent.GetNewThread();
59+
AgentThread emailThread = await emailAssistantAgent.GetNewThreadAsync();
6060

6161
AgentResponse<EmailResponse> emailAssistantResponse = await emailAssistantAgent.RunAsync<EmailResponse>(
6262
message: $"Draft a professional response to: {email.EmailContent}",

0 commit comments

Comments
 (0)