Skip to content

Commit 5431269

Browse files
HavenDVclaude
andcommitted
docs: Add MEAI guide to mkdocs site
Comprehensive guide covering IChatClient, IEmbeddingGenerator, streaming, tool calling, structured output, images, and CustomProviders. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 9132508 commit 5431269

2 files changed

Lines changed: 209 additions & 0 deletions

File tree

docs/guides/meai.md

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
# Microsoft.Extensions.AI Integration
2+
3+
The `tryAGI.OpenAI` SDK natively implements [Microsoft.Extensions.AI](https://learn.microsoft.com/dotnet/ai/microsoft-extensions-ai/) interfaces, providing a unified abstraction layer for AI services across .NET applications.
4+
5+
## Supported Interfaces
6+
7+
| Interface | Description |
8+
|-----------|-------------|
9+
| `IChatClient` | Chat completions with text, streaming, tool calling, images, and structured output |
10+
| `IEmbeddingGenerator<string, Embedding<float>>` | Text embeddings with custom dimensions |
11+
12+
## IChatClient
13+
14+
### Basic Chat
15+
16+
```csharp
17+
using OpenAI;
18+
using Microsoft.Extensions.AI;
19+
20+
using var client = new OpenAiClient("API_KEY");
21+
IChatClient chatClient = client;
22+
23+
var response = await chatClient.GetResponseAsync(
24+
"Say hello!",
25+
new ChatOptions { ModelId = "gpt-4o-mini" });
26+
27+
Console.WriteLine(response.Messages[0].Text);
28+
```
29+
30+
### Streaming
31+
32+
```csharp
33+
await foreach (var update in chatClient.GetStreamingResponseAsync(
34+
"Count from 1 to 10.",
35+
new ChatOptions { ModelId = "gpt-4o-mini" }))
36+
{
37+
Console.Write(string.Concat(update.Contents.OfType<TextContent>().Select(c => c.Text)));
38+
}
39+
```
40+
41+
### System Messages
42+
43+
```csharp
44+
var messages = new List<ChatMessage>
45+
{
46+
new(ChatRole.System, "You are a pirate. Always respond with 'Arrr!'."),
47+
new(ChatRole.User, "Hello!"),
48+
};
49+
50+
var response = await chatClient.GetResponseAsync(
51+
messages,
52+
new ChatOptions { ModelId = "gpt-4o-mini" });
53+
```
54+
55+
### Tool Calling
56+
57+
```csharp
58+
var weatherTool = AIFunctionFactory.Create(
59+
(string city) => city switch
60+
{
61+
"Paris" => "22C, sunny",
62+
"London" => "15C, cloudy",
63+
_ => "Unknown",
64+
},
65+
name: "GetWeather",
66+
description: "Gets the current weather for a city");
67+
68+
var response = await chatClient.GetResponseAsync(
69+
"What's the weather in Paris?",
70+
new ChatOptions
71+
{
72+
ModelId = "gpt-4o-mini",
73+
Tools = [weatherTool],
74+
});
75+
```
76+
77+
### Structured Output (JSON Schema)
78+
79+
```csharp
80+
var schema = System.Text.Json.JsonDocument.Parse("""
81+
{
82+
"type": "object",
83+
"properties": {
84+
"name": { "type": "string" },
85+
"age": { "type": "integer" }
86+
},
87+
"required": ["name", "age"],
88+
"additionalProperties": false
89+
}
90+
""").RootElement;
91+
92+
var response = await chatClient.GetResponseAsync(
93+
"Return info about Alice who is 30.",
94+
new ChatOptions
95+
{
96+
ModelId = "gpt-4o-mini",
97+
ResponseFormat = new ChatResponseFormatJson(schema, "person", "A person object"),
98+
});
99+
```
100+
101+
### Image Content
102+
103+
```csharp
104+
var message = new ChatMessage(ChatRole.User, []);
105+
message.Contents.Add(new TextContent("What is in this image?"));
106+
message.Contents.Add(new UriContent(
107+
new Uri("https://example.com/photo.png"),
108+
mediaType: "image/png"));
109+
110+
var response = await chatClient.GetResponseAsync(
111+
[message],
112+
new ChatOptions { ModelId = "gpt-4o-mini" });
113+
```
114+
115+
### Temperature, TopP, and Stop Sequences
116+
117+
```csharp
118+
var response = await chatClient.GetResponseAsync(
119+
"What is 2+2?",
120+
new ChatOptions
121+
{
122+
ModelId = "gpt-4o-mini",
123+
Temperature = 0f,
124+
TopP = 1f,
125+
StopSequences = ["5"],
126+
});
127+
```
128+
129+
### Additional Properties
130+
131+
Pass provider-specific parameters via `AdditionalProperties`:
132+
133+
```csharp
134+
var options = new ChatOptions
135+
{
136+
ModelId = "gpt-4o-mini",
137+
Temperature = 0f,
138+
};
139+
options.AdditionalProperties ??= new AdditionalPropertiesDictionary();
140+
options.AdditionalProperties["seed"] = 42;
141+
142+
var response = await chatClient.GetResponseAsync(messages, options);
143+
```
144+
145+
## IEmbeddingGenerator
146+
147+
### Basic Embeddings
148+
149+
```csharp
150+
using OpenAI;
151+
using Microsoft.Extensions.AI;
152+
153+
using var client = new OpenAiClient("API_KEY");
154+
IEmbeddingGenerator<string, Embedding<float>> generator = client;
155+
156+
var result = await generator.GenerateAsync(
157+
["Hello, world!"],
158+
new EmbeddingGenerationOptions { ModelId = "text-embedding-3-small" });
159+
160+
Console.WriteLine($"Dimensions: {result[0].Vector.Length}");
161+
```
162+
163+
### Batch Embeddings
164+
165+
```csharp
166+
var result = await generator.GenerateAsync(
167+
["Hello", "How are you?", "Goodbye"],
168+
new EmbeddingGenerationOptions { ModelId = "text-embedding-3-small" });
169+
170+
Console.WriteLine($"Generated {result.Count} embeddings");
171+
```
172+
173+
### Custom Dimensions
174+
175+
```csharp
176+
var result = await generator.GenerateAsync(
177+
["Hello, world!"],
178+
new EmbeddingGenerationOptions
179+
{
180+
ModelId = "text-embedding-3-small",
181+
Dimensions = 256,
182+
});
183+
184+
Console.WriteLine($"Dimensions: {result[0].Vector.Length}"); // 256
185+
```
186+
187+
## Custom Providers
188+
189+
All 15+ custom providers support the same MEAI interfaces:
190+
191+
```csharp
192+
using OpenAI;
193+
using Microsoft.Extensions.AI;
194+
195+
// Any CustomProvider works as IChatClient and IEmbeddingGenerator
196+
using var client = CustomProviders.Groq("API_KEY");
197+
// or: CustomProviders.DeepInfra("API_KEY")
198+
// or: CustomProviders.Together("API_KEY")
199+
// or: CustomProviders.Azure("API_KEY", "ENDPOINT")
200+
// or: CustomProviders.DeepSeek("API_KEY")
201+
// or: any other CustomProvider
202+
203+
IChatClient chatClient = client;
204+
var response = await chatClient.GetResponseAsync(
205+
"Hello!",
206+
new ChatOptions { ModelId = "llama-3.3-70b-versatile" });
207+
```

mkdocs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
site_name: OpenAI .NET Documentation
22
nav:
33
- Overview: index.md
4+
- Guides:
5+
- Microsoft.Extensions.AI: guides/meai.md
46
# EXAMPLES:START
57
- Examples:
68
- Assistants:

0 commit comments

Comments
 (0)