-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
121 lines (104 loc) · 3.86 KB
/
Program.cs
File metadata and controls
121 lines (104 loc) · 3.86 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// NovaPAI C# SDK Example
// Install: dotnet add package OpenAI
// Docs: https://novapai.ai
using OpenAI;
using OpenAI.Chat;
using System.ClientModel;
using System.Text.Json;
// ── Setup Client ────────────────────────────────────────────
var options = new OpenAIClientOptions
{
Endpoint = new Uri("https://api.novapai.ai/router/v1")
};
var client = new ChatClient(
model: "deepseek-v4-pro",
credential: new ApiKeyCredential("your-api-key"),
options: options
);
// ── Basic Chat ──────────────────────────────────────────────
async Task BasicChat()
{
var messages = new List<ChatMessage>
{
new SystemChatMessage("You are a helpful assistant."),
new UserChatMessage("Hello!")
};
ChatCompletion response = await client.CompleteChatAsync(messages);
Console.WriteLine(response.Content[0].Text);
}
// ── Streaming ───────────────────────────────────────────────
async Task StreamChat()
{
var messages = new List<ChatMessage>
{
new UserChatMessage("Tell me a joke")
};
await foreach (StreamingChatCompletionUpdate chunk in
client.CompleteChatStreamingAsync(messages))
{
foreach (var part in chunk.ContentUpdate)
Console.Write(part.Text);
}
Console.WriteLine();
}
// ── Multi-turn Conversation ─────────────────────────────────
async Task MultiTurnChat()
{
var messages = new List<ChatMessage>
{
new SystemChatMessage("You are a helpful assistant.")
};
async Task<string> Chat(string userInput)
{
messages.Add(new UserChatMessage(userInput));
var response = await client.CompleteChatAsync(messages);
var reply = response.Content[0].Text;
messages.Add(new AssistantChatMessage(reply));
return reply;
}
Console.WriteLine(await Chat("What is 1+1?"));
Console.WriteLine(await Chat("Multiply that by 10"));
}
// ── Function Calling ────────────────────────────────────────
async Task FunctionCalling()
{
var getWeatherTool = ChatTool.CreateFunctionTool(
"get_weather",
"Get current weather for a city",
BinaryData.FromString("""
{
"type": "object",
"properties": {
"city": { "type": "string", "description": "City name" }
},
"required": ["city"]
}
""")
);
var chatOptions = new ChatCompletionOptions();
chatOptions.Tools.Add(getWeatherTool);
List<ChatMessage> messages = [new UserChatMessage("What's the weather in Tokyo?")];
ChatCompletion response = await client.CompleteChatAsync(messages, chatOptions);
var toolCall = response.ToolCalls[0];
Console.WriteLine($"Function: {toolCall.FunctionName}");
Console.WriteLine($"Args: {toolCall.FunctionArguments}");
}
// ── JSON Mode (Structured Output) ───────────────────────────
async Task JsonMode()
{
var chatOptions = new ChatCompletionOptions
{
ResponseFormat = ChatResponseFormat.CreateJsonObjectFormat()
};
List<ChatMessage> messages = [
new SystemChatMessage("Extract company info as JSON."),
new UserChatMessage("Apple Inc. is based in Cupertino, founded in 1976.")
];
ChatCompletion response = await client.CompleteChatAsync(messages, chatOptions);
Console.WriteLine(response.Content[0].Text);
}
await BasicChat();
await StreamChat();
await MultiTurnChat();
await FunctionCalling();
await JsonMode();