Skip to content

Commit 6a4c07f

Browse files
Instructions prop and conversations (#807)
* Fix instructions option code and improve conversations explanations * Fix more Instructions options * Apply suggestions from code review Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com> --------- Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>
1 parent 75ee862 commit 6a4c07f

5 files changed

Lines changed: 45 additions & 46 deletions

File tree

agent-framework/tutorials/agents/memory.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ This tutorial shows how to add memory to an agent by implementing an `AIContextP
1717
> [!IMPORTANT]
1818
> Not all agent types support `AIContextProvider`. This step uses a `ChatClientAgent`, which does support `AIContextProvider`.
1919
20-
2120
## Prerequisites
2221

2322
For prerequisites and installing NuGet packages, see the [Create and run a simple agent](./run-agent.md) step in this tutorial.
@@ -161,7 +160,7 @@ ChatClient chatClient = new AzureOpenAIClient(
161160

162161
AIAgent agent = chatClient.CreateAIAgent(new ChatClientAgentOptions()
163162
{
164-
Instructions = "You are a friendly assistant. Always address the user by their name.",
163+
ChatOptions = new() { Instructions = "You are a friendly assistant. Always address the user by their name." },
165164
AIContextProviderFactory = ctx => new UserInfoMemory(
166165
chatClient.AsIChatClient(),
167166
ctx.SerializedState,

agent-framework/tutorials/agents/third-party-chat-history-storage.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -177,20 +177,20 @@ using OpenAI;
177177
AIAgent agent = new AzureOpenAIClient(
178178
new Uri("https://<myresource>.openai.azure.com"),
179179
new AzureCliCredential())
180-
.GetChatClient("gpt-4o-mini")
181-
.CreateAIAgent(new ChatClientAgentOptions
182-
{
183-
Name = "Joker",
184-
Instructions = "You are good at telling jokes.",
185-
ChatMessageStoreFactory = ctx =>
186-
{
187-
// Create a new chat message store for this agent that stores the messages in a vector store.
188-
return new VectorChatMessageStore(
180+
.GetChatClient("gpt-4o-mini")
181+
.CreateAIAgent(new ChatClientAgentOptions
182+
{
183+
Name = "Joker",
184+
ChatOptions = new() { Instructions = "You are good at telling jokes." },
185+
ChatMessageStoreFactory = ctx =>
186+
{
187+
// Create a new chat message store for this agent that stores the messages in a vector store.
188+
return new VectorChatMessageStore(
189189
new InMemoryVectorStore(),
190190
ctx.SerializedState,
191191
ctx.JsonSerializerOptions);
192-
}
193-
});
192+
}
193+
});
194194
```
195195

196196
::: zone-end

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ AIAgent agent = new OpenAIClient("<your_api_key>")
6767
.CreateAIAgent(new ChatClientAgentOptions
6868
{
6969
Name = JokerName,
70-
Instructions = JokerInstructions,
70+
ChatOptions = new() { Instructions = JokerInstructions },
7171
ChatMessageStoreFactory = ctx => new InMemoryChatMessageStore(
7272
new MessageCountingChatReducer(2),
7373
ctx.SerializedState,
@@ -114,19 +114,19 @@ Here is an example showing how to pass the custom implementation of `ChatMessage
114114
AIAgent agent = new AzureOpenAIClient(
115115
new Uri(endpoint),
116116
new AzureCliCredential())
117-
.GetChatClient(deploymentName)
118-
.CreateAIAgent(new ChatClientAgentOptions
119-
{
120-
Name = JokerName,
121-
Instructions = JokerInstructions,
122-
ChatMessageStoreFactory = ctx =>
123-
{
124-
// Create a new chat message store for this agent that stores the messages in a custom store.
125-
// Each thread must get its own copy of the CustomMessageStore, since the store
126-
// also contains the ID that the thread is stored under.
127-
return new CustomMessageStore(vectorStore, ctx.SerializedState, ctx.JsonSerializerOptions);
128-
}
129-
});
117+
.GetChatClient(deploymentName)
118+
.CreateAIAgent(new ChatClientAgentOptions
119+
{
120+
Name = JokerName,
121+
ChatOptions = new() { Instructions = JokerInstructions },
122+
ChatMessageStoreFactory = ctx =>
123+
{
124+
// Create a new chat message store for this agent that stores the messages in a custom store.
125+
// Each thread must get its own copy of the CustomMessageStore, since the store
126+
// also contains the ID that the thread is stored under.
127+
return new CustomMessageStore(vectorStore, ctx.SerializedState, ctx.JsonSerializerOptions);
128+
}
129+
});
130130
```
131131

132132
> [!TIP]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ AIAgent agent = azureOpenAIClient
2727
.GetChatClient(deploymentName)
2828
.CreateAIAgent(new ChatClientAgentOptions
2929
{
30-
Instructions = "You are a helpful support specialist for Contoso Outdoors. Answer questions using the provided context and cite the source document when available.",
30+
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." },
3131
AIContextProviderFactory = ctx => new TextSearchProvider(SearchAdapter, ctx.SerializedState, ctx.JsonSerializerOptions, textSearchOptions)
3232
});
3333
```

agent-framework/user-guide/agents/multi-turn-conversation.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ The Microsoft Agent Framework provides built-in support for managing multi-turn
1717

1818
For example, when using a ChatClientAgent based on a foundry agent, the conversation history is persisted in the service. While, when using a ChatClientAgent based on chat completion with gpt-4.1 the conversation history is in-memory and managed by the agent.
1919

20-
The `AgentThread` type is the abstraction that represents a conversation thread with an agent.
20+
The `AgentThread` type is the abstraction that represents conversation history and other state of an agent.
2121
`AIAgent` instances are stateless and the same agent instance can be used with multiple `AgentThread` instances. All state is therefore preserved in the `AgentThread`.
2222
An `AgentThread` can both represent chat history plus any other state that the agent needs to preserve across multiple interactions.
23-
The chat history may be stored in the thread itself, or remotely, with the `AgentThread` only containing a reference to the remote chat history.
23+
The conversation history may be stored in the `AgentThread` object itself, or remotely, with the `AgentThread` only containing a reference to the remote conversation history.
2424
The `AgentThread` state may also include memories or references to memories stored remotely.
2525

2626
> [!TIP]
@@ -31,9 +31,9 @@ The `AgentThread` state may also include memories or references to memories stor
3131
`AgentThread` instances can be created in two ways:
3232

3333
1. By calling `GetNewThread` on the agent.
34-
1. By running the agent and not providing an `AgentThread`. In this case the agent will create a throwaway `AgentThread` with an underlying thread which will only be used for the duration of the run.
34+
1. By running the agent and not providing an `AgentThread`. In this case the agent will create a throwaway `AgentThread` which will only be used for the duration of the run.
3535

36-
Some underlying threads might be persistently created in an underlying service, where the service requires this, for example, Foundry Agents or OpenAI Responses. Any cleanup or deletion of these threads is the responsibility of the user.
36+
Some underlying service stored conversations/threads/responses might be persistently created in an underlying service, where the service requires this, for example, Foundry Agents or OpenAI Responses. Any cleanup or deletion of these is the responsibility of the user.
3737

3838
::: zone pivot="programming-language-csharp"
3939

@@ -54,7 +54,7 @@ response = await agent.RunAsync("Hello, how are you?");
5454
`AgentThread` instances can be serialized and stored for later use. This allows for the preservation of conversation context across different sessions or service calls.
5555

5656
For cases where the conversation history is stored in a service, the serialized `AgentThread` will contain an
57-
id of the thread in the service.
57+
id that points to the conversation history in the service.
5858
For cases where the conversation history is managed in-memory, the serialized `AgentThread` will contain the messages
5959
themselves.
6060

@@ -85,21 +85,20 @@ The differences between the underlying threading models are abstracted away via
8585

8686
`AIAgent` instances are stateless and the same agent instance can be used with multiple `AgentThread` instances.
8787

88-
Not all agents support all thread types though. For example if you are using a `ChatClientAgent` with the responses service, `AgentThread` instances created by this agent, will not work with a `ChatClientAgent` using the Foundry Agent service.
89-
This is because these services both support saving the conversation history in the service, and the `AgentThread`
90-
only has a reference to this service managed thread.
88+
Not all agents support all `AgentThread` types though. For example if you are using a `ChatClientAgent` with the responses service, `AgentThread` instances created by this agent, will not work with a `ChatClientAgent` using the Foundry Agent service.
89+
This is because these services both support saving the conversation history in the service, and while the two `AgentThread` instances will have references to each service stored conversation, the id from the responses service cannot be used with the Foundry Agent service, and vice versa.
9190

9291
It is therefore considered unsafe to use an `AgentThread` instance that was created by one agent with a different agent instance, unless you are aware of the underlying threading model and its implications.
9392

94-
## Threading support by service / protocol
93+
## Conversation history support by service / protocol
9594

96-
| Service | Threading Support |
95+
| Service | Conversation History Support |
9796
|---------|--------------------|
98-
| Foundry Agents | Service managed persistent threads |
99-
| OpenAI Responses | Service managed persistent threads OR in-memory threads |
100-
| OpenAI ChatCompletion | In-memory threads |
101-
| OpenAI Assistants | Service managed threads |
102-
| A2A | Service managed threads |
97+
| Foundry Agents | Service stored persistent conversation history |
98+
| OpenAI Responses | Service stored response chains OR in-memory conversation history |
99+
| OpenAI ChatCompletion | In-memory conversation history |
100+
| OpenAI Assistants | Service stored persistent conversation history |
101+
| A2A | Service stored persistent conversation history |
103102

104103
::: zone-end
105104

@@ -112,7 +111,7 @@ It is therefore considered unsafe to use an `AgentThread` instance that was crea
112111
1. By calling `get_new_thread()` on the agent.
113112
1. By running the agent and not providing an `AgentThread`. In this case the agent will create a throwaway `AgentThread` with an underlying thread which will only be used for the duration of the run.
114113

115-
Some underlying threads might be persistently created in an underlying service, where the service requires this, for example, Azure AI Agents or OpenAI Responses. Any cleanup or deletion of these threads is the responsibility of the user.
114+
Some underlying service stored conversations/threads/responses might be persistently created in an underlying service, where the service requires this, for example, Azure AI Agents or OpenAI Responses. Any cleanup or deletion of these is the responsibility of the user.
116115

117116
```python
118117
# Create a new thread.
@@ -129,7 +128,7 @@ response = await agent.run("Hello, how are you?")
129128
`AgentThread` instances can be serialized and stored for later use. This allows for the preservation of conversation context across different sessions or service calls.
130129

131130
For cases where the conversation history is stored in a service, the serialized `AgentThread` will contain an
132-
id of the thread in the service.
131+
id that points to the conversation history in the service.
133132
For cases where the conversation history is managed in-memory, the serialized `AgentThread` will contain the messages
134133
themselves.
135134

@@ -180,7 +179,8 @@ async with AzureCliCredential() as credential:
180179

181180
`Agents` are stateless and the same agent instance can be used with multiple `AgentThread` instances.
182181

183-
Not all agents support all thread types though. For example if you are using a `ChatAgent` with the OpenAI Responses service and `store=True`, `AgentThread` instances used by this agent, will not work with a `ChatAgent` using the Azure AI Agent service, because the thread_ids are not compatible.
182+
Not all agents support all `AgentThread` types though. For example if you are using a `ChatAgent` with the OpenAI Responses service and `store=True`, `AgentThread` instances used by this agent, will not work with a `ChatAgent` using the Azure AI Agent service.
183+
This is because these services both support saving the conversation history in the service, and while the two `AgentThread` instances will have references to each service stored conversation, the id from the OpenAI Responses service cannot be used with the Foundry Agent service, and vice versa.
184184

185185
It is therefore considered unsafe to use an `AgentThread` instance that was created by one agent with a different agent instance, unless you are aware of the underlying threading model and its implications.
186186

0 commit comments

Comments
 (0)