Catatan
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba masuk atau mengubah direktori.
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba mengubah direktori.
Gunakan sesi untuk mempertahankan konteks percakapan sehingga agen mengingat apa yang dikatakan sebelumnya.
Gunakan AgentSession untuk mempertahankan konteks di beberapa panggilan:
using System;
using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Agents.AI;
var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT")
?? throw new InvalidOperationException("Set AZURE_OPENAI_ENDPOINT");
var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini";
AIAgent agent = new AzureOpenAIClient(new Uri(endpoint), new AzureCliCredential())
.GetChatClient(deploymentName)
.AsAIAgent(instructions: "You are a friendly assistant. Keep your answers brief.", name: "ConversationAgent");
// Create a session to maintain conversation history
AgentSession session = await agent.CreateSessionAsync();
// First turn
Console.WriteLine(await agent.RunAsync("My name is Alice and I love hiking.", session));
// Second turn — the agent remembers the user's name and hobby
Console.WriteLine(await agent.RunAsync("What do you remember about me?", session));
Petunjuk / Saran
Lihat sampel lengkap untuk file lengkap yang dapat dijalankan.
Gunakan AgentSession untuk mempertahankan konteks di beberapa panggilan:
credential = AzureCliCredential()
client = AzureOpenAIResponsesClient(
project_endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"],
deployment_name=os.environ["AZURE_OPENAI_RESPONSES_DEPLOYMENT_NAME"],
credential=credential,
)
agent = client.as_agent(
name="ConversationAgent",
instructions="You are a friendly assistant. Keep your answers brief.",
)
# 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}")
Petunjuk / Saran
Lihat sampel lengkap untuk file lengkap yang dapat dijalankan.
Langkah selanjutnya
Masuk lebih dalam:
- Percakapan multi-giliran — pola percakapan tingkat lanjut
- Middleware — mencegat dan memodifikasi interaksi agen