Skip to content

Commit a1132b6

Browse files
committed
Add more samples
1 parent 08c4123 commit a1132b6

6 files changed

+135
-13
lines changed

examples/Chat/Example10_InputAdditionalPropertyValue.cs renamed to examples/Chat/Example10_AdditionalProperties.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,27 @@
11
using NUnit.Framework;
22
using OpenAI.Chat;
33
using System;
4-
using System.Collections.Generic;
54

65
namespace OpenAI.Examples;
76

87
#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
8+
99
public partial class ChatExamples
1010
{
1111
[Test]
12-
public void Example07_InputAdditionalPropertyValue()
12+
public void Example10_AdditionalProperties()
1313
{
1414
ChatClient client = new(model: "gpt-5", apiKey: Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
1515

16-
// you can use the Patch property to set additional properties in the input
16+
// You can use the Patch property to set additional properties in the request
1717
ChatCompletionOptions options = new();
1818
options.Patch.Set("$.reasoning_effort"u8, "minimal");
1919

20-
List<ChatMessage> messages =
21-
[
22-
new UserChatMessage("What's the weather like today?"),
23-
];
24-
ChatCompletion completion = client.CompleteChat(messages, options);
20+
ChatCompletion completion = client.CompleteChat([new UserChatMessage("Say 'this is a test.'")], options);
2521

2622
Console.WriteLine($"[ASSISTANT]: {completion.Content[0].Text}");
2723

28-
// you can also read those properties back from the response
24+
// You can also read additional properties back from the response
2925
var effort = completion.Patch.GetString("$.reasoning_effort"u8);
3026
Console.WriteLine($"effort={effort}");
3127
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using NUnit.Framework;
2+
using OpenAI.Chat;
3+
using System;
4+
using System.Threading.Tasks;
5+
6+
namespace OpenAI.Examples;
7+
8+
#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
9+
10+
public partial class ChatExamples
11+
{
12+
[Test]
13+
public async Task Example10_AdditionalPropertiesAsync()
14+
{
15+
ChatClient client = new(model: "gpt-5", apiKey: Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
16+
17+
// You can use the Patch property to set additional properties in the request
18+
ChatCompletionOptions options = new();
19+
options.Patch.Set("$.reasoning_effort"u8, "minimal");
20+
21+
ChatCompletion completion = await client.CompleteChatAsync([new UserChatMessage("Say 'this is a test.'")], options);
22+
23+
Console.WriteLine($"[ASSISTANT]: {completion.Content[0].Text}");
24+
25+
// You can also read additional properties back from the response
26+
var effort = completion.Patch.GetString("$.reasoning_effort"u8);
27+
Console.WriteLine($"effort={effort}");
28+
}
29+
}
30+
31+
#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.

examples/Responses/Example07_InputAdditionalProperties.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public void Example07_InputAdditionalProperties()
1616
{
1717
OpenAIResponseClient client = new(model: "gpt-5", apiKey: Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
1818

19-
// you can use the Patch property to set additional properties in the input
19+
// You can use the Patch property to set additional properties in the request
2020
ResponseCreationOptions options = new();
2121
options.Patch.Set("$.reasoning.effort"u8, "high");
2222
options.Patch.Set("$.text.verbosity"u8, "medium");
@@ -25,7 +25,7 @@ public void Example07_InputAdditionalProperties()
2525

2626
Console.WriteLine($"[ASSISTANT]: {response.GetOutputText()}");
2727

28-
// you can also read those properties back from the response
28+
// You can also read additional properties back from the response
2929
var effort = response.Patch.GetString("$.reasoning.effort"u8);
3030
var verbosity = response.Patch.GetString("$.text.verbosity"u8);
3131
Console.WriteLine($"effort={effort}, verbosity={verbosity}");

examples/Responses/Example07_InputAdditionalPropertiesAsync.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public async Task Example07_InputAdditionalPropertiesAsync()
1717
{
1818
OpenAIResponseClient client = new(model: "gpt-5", apiKey: Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
1919

20-
// you can use the Patch property to set additional properties in the input
20+
// You can use the Patch property to set additional properties in the request
2121
ResponseCreationOptions options = new();
2222
options.Patch.Set("$.reasoning.effort"u8, "high");
2323
options.Patch.Set("$.text.verbosity"u8, "medium");
@@ -26,7 +26,7 @@ public async Task Example07_InputAdditionalPropertiesAsync()
2626

2727
Console.WriteLine($"[ASSISTANT]: {response.GetOutputText()}");
2828

29-
// you can also read those properties back from the response
29+
// You can also read additional properties back from the response
3030
var effort = response.Patch.GetString("$.reasoning.effort"u8);
3131
var verbosity = response.Patch.GetString("$.text.verbosity"u8);
3232
Console.WriteLine($"effort={effort}, verbosity={verbosity}");
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using NUnit.Framework;
2+
using OpenAI.Responses;
3+
using System;
4+
using System.IO;
5+
6+
namespace OpenAI.Examples;
7+
8+
// This example uses experimental APIs which are subject to change. To use experimental APIs,
9+
// please acknowledge their experimental status by suppressing the corresponding warning.
10+
#pragma warning disable OPENAI001
11+
#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
12+
13+
public partial class ResponseExamples
14+
{
15+
[Test]
16+
public void Example08_OutputAdditionalProperties()
17+
{
18+
OpenAIResponseClient client = new(model: "gpt-5", apiKey: Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
19+
20+
ResponseCreationOptions options = new()
21+
{
22+
Tools =
23+
{
24+
ResponseTool.CreateImageGenerationTool(
25+
model: "gpt-image-1",
26+
outputFileFormat: ImageGenerationToolOutputFileFormat.Png,
27+
inputFidelityLevel: ImageGenerationToolInputFidelityLevel.High)
28+
}
29+
};
30+
31+
OpenAIResponse response = client.CreateResponse("Generate an image of gray tabby cat hugging an otter with an orange scarf", options);
32+
ImageGenerationCallResponseItem imageGenResponse = (ImageGenerationCallResponseItem)response.OutputItems[1];
33+
BinaryData bytes = imageGenResponse.GeneratedImageBytes;
34+
35+
using FileStream stream = File.OpenWrite($"{Guid.NewGuid()}.png");
36+
bytes.ToStream().CopyTo(stream);
37+
38+
// You can use the Patch property to read additional properties from the response
39+
var outputFormat = imageGenResponse.Patch.GetString("$.output_format"u8);
40+
var quality = imageGenResponse.Patch.GetString("$.quality"u8);
41+
var size = imageGenResponse.Patch.GetString("$.size"u8);
42+
Console.WriteLine($"outputFormat={outputFormat}, quality={quality}, size={size}");
43+
}
44+
}
45+
46+
#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
47+
#pragma warning restore OPENAI001
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using NUnit.Framework;
2+
using System;
3+
using OpenAI.Responses;
4+
using System.IO;
5+
using System.Threading.Tasks;
6+
7+
namespace OpenAI.Examples;
8+
9+
// This example uses experimental APIs which are subject to change. To use experimental APIs,
10+
// please acknowledge their experimental status by suppressing the corresponding warning.
11+
#pragma warning disable OPENAI001
12+
#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
13+
14+
public partial class ResponseExamples
15+
{
16+
[Test]
17+
public async Task Example08_OutputAdditionalPropertiesAsync()
18+
{
19+
OpenAIResponseClient client = new(model: "gpt-5", apiKey: Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
20+
21+
ResponseCreationOptions options = new()
22+
{
23+
Tools =
24+
{
25+
ResponseTool.CreateImageGenerationTool(
26+
model: "gpt-image-1",
27+
outputFileFormat: ImageGenerationToolOutputFileFormat.Png,
28+
inputFidelityLevel: ImageGenerationToolInputFidelityLevel.High)
29+
}
30+
};
31+
32+
OpenAIResponse response = await client.CreateResponseAsync("Generate an image of gray tabby cat hugging an otter with an orange scarf", options);
33+
ImageGenerationCallResponseItem imageGenResponse = (ImageGenerationCallResponseItem)response.OutputItems[1];
34+
BinaryData bytes = imageGenResponse.GeneratedImageBytes;
35+
36+
using FileStream stream = File.OpenWrite($"{Guid.NewGuid()}.png");
37+
bytes.ToStream().CopyTo(stream);
38+
39+
// You can use the Patch property to read additional properties from the response
40+
var outputFormat = imageGenResponse.Patch.GetString("$.output_format"u8);
41+
var quality = imageGenResponse.Patch.GetString("$.quality"u8);
42+
var size = imageGenResponse.Patch.GetString("$.size"u8);
43+
Console.WriteLine($"outputFormat={outputFormat}, quality={quality}, size={size}");
44+
}
45+
}
46+
47+
#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
48+
#pragma warning restore OPENAI001

0 commit comments

Comments
 (0)