-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy path03_multi_turn.py
More file actions
47 lines (35 loc) · 1.27 KB
/
03_multi_turn.py
File metadata and controls
47 lines (35 loc) · 1.27 KB
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
# Copyright (c) Microsoft. All rights reserved.
import asyncio
from agent_framework import Agent
from agent_framework.foundry import FoundryChatClient
from azure.identity import AzureCliCredential
"""
Multi-Turn Conversations — Use AgentSession to maintain context
This sample shows how to keep conversation history across multiple calls
by reusing the same session object.
"""
async def main() -> None:
# <create_agent>
client = FoundryChatClient(
project_endpoint="https://your-project.services.ai.azure.com",
model="gpt-4o",
credential=AzureCliCredential(),
)
agent = Agent(
client=client,
name="ConversationAgent",
instructions="You are a friendly assistant. Keep your answers brief.",
)
# </create_agent>
# <multi_turn>
# Create a session to maintain conversation history
session = agent.create_session()
# First turn
result = await agent.run("My name is Alice and I love hiking.", session=session)
print(f"Agent: {result}\n")
# Second turn — the agent should remember the user's name and hobby
result = await agent.run("What do you remember about me?", session=session)
print(f"Agent: {result}")
# </multi_turn>
if __name__ == "__main__":
asyncio.run(main())