Skip to content

Commit bcfe261

Browse files
HavenDVclaude
andcommitted
feat: Add MEAI examples, restore structured outputs, and clean up Mystic refs
- Add 4 MEAI examples (chat, streaming, tool calling, embeddings) - Add structured outputs example - Uncomment and update CreateChatCompletionAsAsync<T> library method (adapt to CreateChatCompletionRequestVariant2 structure, remove deprecated Temperature/TopP/User params) - Uncomment and update structured outputs tests (4 test methods) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent fc4fb86 commit bcfe261

13 files changed

Lines changed: 605 additions & 342 deletions
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# MEAI Chat Completion
2+
3+
Use the Microsoft.Extensions.AI IChatClient interface for chat completions.
4+
5+
This example assumes `using tryAGI.OpenAI;` is in scope and `apiKey` contains your tryAGI.OpenAI API key.
6+
7+
```csharp
8+
using var client = new OpenAiClient(apiKey);
9+
10+
// using Meai = Microsoft.Extensions.AI;
11+
Meai.IChatClient chatClient = client;
12+
13+
var messages = new List<Meai.ChatMessage>
14+
{
15+
new(Meai.ChatRole.User, "Say hello in exactly 3 words."),
16+
};
17+
18+
var response = await chatClient.GetResponseAsync(
19+
messages,
20+
new Meai.ChatOptions { ModelId = "gpt-4o-mini" });
21+
22+
Console.WriteLine(response.Messages[0].Text);
23+
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# MEAI Chat Streaming
2+
3+
Stream a chat completion using the Microsoft.Extensions.AI IChatClient interface.
4+
5+
This example assumes `using tryAGI.OpenAI;` is in scope and `apiKey` contains your tryAGI.OpenAI API key.
6+
7+
```csharp
8+
using var client = new OpenAiClient(apiKey);
9+
10+
// using Meai = Microsoft.Extensions.AI;
11+
Meai.IChatClient chatClient = client;
12+
13+
var messages = new List<Meai.ChatMessage>
14+
{
15+
new(Meai.ChatRole.User, "Count from 1 to 5."),
16+
};
17+
18+
await foreach (var update in chatClient.GetStreamingResponseAsync(
19+
messages,
20+
new Meai.ChatOptions { ModelId = "gpt-4o-mini" }))
21+
{
22+
var text = string.Concat(update.Contents.OfType<Meai.TextContent>().Select(c => c.Text));
23+
if (!string.IsNullOrEmpty(text))
24+
{
25+
Console.Write(text);
26+
}
27+
}
28+
```

docs/examples/meai-embeddings.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# MEAI Embeddings
2+
3+
Generate embeddings using the Microsoft.Extensions.AI IEmbeddingGenerator interface.
4+
5+
This example assumes `using tryAGI.OpenAI;` is in scope and `apiKey` contains your tryAGI.OpenAI API key.
6+
7+
```csharp
8+
using var client = new OpenAiClient(apiKey);
9+
10+
// using Meai = Microsoft.Extensions.AI;
11+
Meai.IEmbeddingGenerator<string, Meai.Embedding<float>> generator = client;
12+
13+
var result = await generator.GenerateAsync(
14+
new List<string> { "Hello, world!" },
15+
new Meai.EmbeddingGenerationOptions
16+
{
17+
ModelId = "text-embedding-3-small",
18+
});
19+
20+
Console.WriteLine($"Embedding dimension: {result[0].Vector.Length}");
21+
```

docs/examples/meai-tool-calling.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# MEAI Tool Calling
2+
3+
Use function/tool calling via the Microsoft.Extensions.AI IChatClient interface.
4+
5+
This example assumes `using tryAGI.OpenAI;` is in scope and `apiKey` contains your tryAGI.OpenAI API key.
6+
7+
```csharp
8+
using var client = new OpenAiClient(apiKey);
9+
10+
// using Meai = Microsoft.Extensions.AI;
11+
Meai.IChatClient chatClient = client;
12+
13+
var tool = Meai.AIFunctionFactory.Create(
14+
(string city) => city switch
15+
{
16+
"Paris" => "22C, sunny",
17+
"London" => "15C, cloudy",
18+
_ => "Unknown",
19+
},
20+
name: "GetWeather",
21+
description: "Gets the current weather for a city");
22+
23+
var chatOptions = new Meai.ChatOptions
24+
{
25+
ModelId = "gpt-4o-mini",
26+
Tools = [tool],
27+
};
28+
29+
var messages = new List<Meai.ChatMessage>
30+
{
31+
new(Meai.ChatRole.User, "What's the weather in Paris? Respond with the temperature only."),
32+
};
33+
34+
// First turn: get tool call
35+
var response = await chatClient.GetResponseAsync(
36+
(IEnumerable<Meai.ChatMessage>)messages, chatOptions);
37+
38+
var functionCall = response.Messages
39+
.SelectMany(m => m.Contents)
40+
.OfType<Meai.FunctionCallContent>()
41+
.First();
42+
43+
// Execute tool and add result
44+
var toolResult = await tool.InvokeAsync(
45+
functionCall.Arguments is { } args
46+
? new Meai.AIFunctionArguments(args)
47+
: null);
48+
messages.AddRange(response.Messages);
49+
messages.Add(new Meai.ChatMessage(Meai.ChatRole.Tool,
50+
new Meai.AIContent[]
51+
{
52+
new Meai.FunctionResultContent(functionCall.CallId, toolResult),
53+
}));
54+
55+
// Second turn: get final response
56+
var finalResponse = await chatClient.GetResponseAsync(
57+
(IEnumerable<Meai.ChatMessage>)messages, chatOptions);
58+
59+
Console.WriteLine(finalResponse.Messages[0].Text);
60+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Structured Outputs
2+
3+
Get structured JSON responses using a C# type as the schema.
4+
5+
This example assumes `using tryAGI.OpenAI;` is in scope and `apiKey` contains your tryAGI.OpenAI API key.
6+
7+
```csharp
8+
using var client = new OpenAiClient(apiKey);
9+
10+
var response = await client.Chat.CreateChatCompletionAsAsync<WordsResponse>(
11+
messages: ["Generate five random words as json."],
12+
model: "gpt-4o-mini");
13+
14+
Console.WriteLine("Words:");
15+
foreach (var word in response.Value1!.Words)
16+
{
17+
Console.WriteLine(word);
18+
}
19+
```

mkdocs.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@ nav:
99
- Chat Completion Streaming: examples/chat-completion-streaming.md
1010
- Chat With Vision: examples/chat-with-vision.md
1111
- JSON Response Format: examples/json-response-format.md
12+
- Structured Outputs: examples/structured-outputs.md
1213
- Embeddings: examples/embeddings.md
1314
- Image Generation: examples/image-generation.md
1415
- Text To Speech: examples/text-to-speech.md
1516
- List Models: examples/list-models.md
1617
- Moderation: examples/moderation.md
18+
- MEAI Chat Completion: examples/meai-chat-completion.md
19+
- MEAI Chat Streaming: examples/meai-chat-streaming.md
20+
- MEAI Tool Calling: examples/meai-tool-calling.md
21+
- MEAI Embeddings: examples/meai-embeddings.md
1722
# EXAMPLES:END
1823

1924
# - Quick Start: QuickStart.md

0 commit comments

Comments
 (0)