ChatGPT/API/dotnet
をテンプレートにして作成
[
トップ
] [
タイトル一覧
|
ページ一覧
|
新規
|
検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
** [[dotnet>.NET]]
- for OpenAI v2.x
- https://github.com/openai/openai-dotnet
*** Programming
**** Project の作成
<pre>
dotnet new console -o Net8 -f net8.0
cd Net8
dotnet add package OpenAI
vi Program.cs
dotnet run
</pre>
** Sample
- https://github.com/openai/openai-dotnet
*** 基本
<pre>
using OpenAI.Chat;
ChatClient client = new(model: "gpt-4o-mini", apiKey: Env...
//ChatClient client = new(model: "gpt-4o-mini", apiKey: "...
ChatCompletion completion = client.CompleteChat("こんにち...
Console.WriteLine($"[ASSISTANT]: {completion.Content[0].T...
</pre>
*** 非同期によるチャット補完
<pre>
using static System.Console;
using System.ClientModel; // for CollectionResult
using OpenAI;
using OpenAI.Chat;
ChatClient client = new(model: "gpt-4o-mini", apiKey: "sk...
CollectionResult<StreamingChatCompletionUpdate> completio...
foreach (StreamingChatCompletionUpdate completionUpdate i...
if (completionUpdate.ContentUpdate.Count > 0) {
Console.Write(completionUpdate.ContentUpdate[0].T...
}
}
</pre>
*** 構造化された出力によるチャット補完
dotnet add package System.Text.Json
dotnet list package
<pre>
using static System.Console;
using System.ClientModel; // for CollectionResult
using OpenAI;
using OpenAI.Chat;
using System.Text.Json;
ChatClient client = new(model: "gpt-4o-mini", apiKey: "sk...
List<ChatMessage> messages =
[
new UserChatMessage("How can I solve 8x + 7 = -23?"),
];
ChatCompletionOptions options = new()
{
ResponseFormat = ChatResponseFormat.CreateJsonSchemaF...
jsonSchemaFormatName: "math_reasoning",
jsonSchema: BinaryData.FromBytes("""
{
"type": "object",
"properties": {
"steps": {
"type": "array",
"items": {
"type": "object",
"properties": {
"explanation": { "type": "string"...
"output": { "type": "string" }
},
"required": ["explanation", "output"],
"additionalProperties": false
}
},
"final_answer": { "type": "string" }
},
"required": ["steps", "final_answer"],
"additionalProperties": false
}
"""u8.ToArray()),
jsonSchemaIsStrict: true)
};
ChatCompletion completion = client.CompleteChat(messages,...
using JsonDocument structuredJson = JsonDocument.Parse(co...
Console.WriteLine($"Final answer: {structuredJson.RootEle...
Console.WriteLine("Reasoning steps:");
foreach (JsonElement stepElement in structuredJson.RootEl...
{
Console.WriteLine($" - Explanation: {stepElement.Get...
Console.WriteLine($" Output: {stepElement.GetPrope...
}
</pre>
終了行:
** [[dotnet>.NET]]
- for OpenAI v2.x
- https://github.com/openai/openai-dotnet
*** Programming
**** Project の作成
<pre>
dotnet new console -o Net8 -f net8.0
cd Net8
dotnet add package OpenAI
vi Program.cs
dotnet run
</pre>
** Sample
- https://github.com/openai/openai-dotnet
*** 基本
<pre>
using OpenAI.Chat;
ChatClient client = new(model: "gpt-4o-mini", apiKey: Env...
//ChatClient client = new(model: "gpt-4o-mini", apiKey: "...
ChatCompletion completion = client.CompleteChat("こんにち...
Console.WriteLine($"[ASSISTANT]: {completion.Content[0].T...
</pre>
*** 非同期によるチャット補完
<pre>
using static System.Console;
using System.ClientModel; // for CollectionResult
using OpenAI;
using OpenAI.Chat;
ChatClient client = new(model: "gpt-4o-mini", apiKey: "sk...
CollectionResult<StreamingChatCompletionUpdate> completio...
foreach (StreamingChatCompletionUpdate completionUpdate i...
if (completionUpdate.ContentUpdate.Count > 0) {
Console.Write(completionUpdate.ContentUpdate[0].T...
}
}
</pre>
*** 構造化された出力によるチャット補完
dotnet add package System.Text.Json
dotnet list package
<pre>
using static System.Console;
using System.ClientModel; // for CollectionResult
using OpenAI;
using OpenAI.Chat;
using System.Text.Json;
ChatClient client = new(model: "gpt-4o-mini", apiKey: "sk...
List<ChatMessage> messages =
[
new UserChatMessage("How can I solve 8x + 7 = -23?"),
];
ChatCompletionOptions options = new()
{
ResponseFormat = ChatResponseFormat.CreateJsonSchemaF...
jsonSchemaFormatName: "math_reasoning",
jsonSchema: BinaryData.FromBytes("""
{
"type": "object",
"properties": {
"steps": {
"type": "array",
"items": {
"type": "object",
"properties": {
"explanation": { "type": "string"...
"output": { "type": "string" }
},
"required": ["explanation", "output"],
"additionalProperties": false
}
},
"final_answer": { "type": "string" }
},
"required": ["steps", "final_answer"],
"additionalProperties": false
}
"""u8.ToArray()),
jsonSchemaIsStrict: true)
};
ChatCompletion completion = client.CompleteChat(messages,...
using JsonDocument structuredJson = JsonDocument.Parse(co...
Console.WriteLine($"Final answer: {structuredJson.RootEle...
Console.WriteLine("Reasoning steps:");
foreach (JsonElement stepElement in structuredJson.RootEl...
{
Console.WriteLine($" - Explanation: {stepElement.Get...
Console.WriteLine($" Output: {stepElement.GetPrope...
}
</pre>
ページ名: