Skip to content

Commit ac568d7

Browse files
authored
Fix for csharp declarative agent docs (#955)
1 parent dc2bad0 commit ac568d7

1 file changed

Lines changed: 42 additions & 14 deletions

File tree

agent-framework/agents/declarative.md

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,53 @@ Declarative agents allow you to define agent configuration using YAML or JSON fi
1818
The following example shows how to create a declarative agent from a YAML configuration:
1919

2020
```csharp
21-
using System;
22-
using System.IO;
2321
using Azure.AI.OpenAI;
2422
using Azure.Identity;
2523
using Microsoft.Agents.AI;
2624
using Microsoft.Extensions.AI;
2725

28-
// Load agent configuration from a YAML file
29-
var yamlContent = File.ReadAllText("agent-config.yaml");
30-
31-
// Create the agent from the YAML definition
32-
AIAgent agent = AgentFactory.CreateFromYaml(
33-
yamlContent,
34-
new AzureOpenAIClient(
35-
new Uri("https://<myresource>.openai.azure.com"),
36-
new AzureCliCredential()));
37-
38-
// Run the declarative agent
39-
Console.WriteLine(await agent.RunAsync("Why is the sky blue?"));
26+
// Create the chat client
27+
IChatClient chatClient = new AzureOpenAIClient(
28+
new Uri("https://<myresource>.openai.azure.com"),
29+
new DefaultAzureCredential())
30+
.GetChatClient("gpt-4o-mini")
31+
.AsIChatClient();
32+
33+
// Define the agent using a YAML definition.
34+
var yamlDefinition =
35+
"""
36+
kind: Prompt
37+
name: Assistant
38+
description: Helpful assistant
39+
instructions: You are a helpful assistant. You answer questions in the language specified by the user. You return your answers in a JSON format.
40+
model:
41+
options:
42+
temperature: 0.9
43+
topP: 0.95
44+
outputSchema:
45+
properties:
46+
language:
47+
type: string
48+
required: true
49+
description: The language of the answer.
50+
answer:
51+
type: string
52+
required: true
53+
description: The answer text.
54+
""";
55+
56+
// Create the agent from the YAML definition.
57+
var agentFactory = new ChatClientPromptAgentFactory(chatClient);
58+
var agent = await agentFactory.CreateFromYamlAsync(yamlDefinition);
59+
60+
// Invoke the agent and output the text result.
61+
Console.WriteLine(await agent!.RunAsync("Tell me a joke about a pirate in English."));
62+
63+
// Invoke the agent with streaming support.
64+
await foreach (var update in agent!.RunStreamingAsync("Tell me a joke about a pirate in French."))
65+
{
66+
Console.WriteLine(update);
67+
}
4068
```
4169

4270
:::zone-end

0 commit comments

Comments
 (0)