Skip to content

Commit 94608bb

Browse files
initial samples (#489)
* initial samples * added global.json to force .NET 10 usage in this folder * updated readme * added link * changes s3 to use system message * changed folder structure * moved readme
1 parent 83d6bef commit 94608bb

10 files changed

+147
-0
lines changed

docs/guides/README.MD

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# OpenAI Documentation Examples for .NET
2+
3+
## Running the Samples
4+
5+
The samples use a new .NET 10 feature that allows [running single C# file applications](https://devblogs.microsoft.com/dotnet/announcing-dotnet-run-app/). Each file in this folder is a standalone application.
6+
7+
To run them, you need to install the .NET 10 preview.
8+
9+
Then, from the command line, you can run them using the `dotnet run <sample_name>` command, for example: `dotnet run s1-chat_simpleprompt.cs`.
10+

docs/guides/global.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"sdk": {
3+
"version": "10.*-preview"
4+
}
5+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// SAMPLE: Generate text with messages using different roles
2+
#:package OpenAI@2.2.*-*
3+
#:property PublishAot=false
4+
5+
using OpenAI.Chat;
6+
7+
string key = Environment.GetEnvironmentVariable("OPENAI_KEY")!;
8+
ChatClient client = new("gpt-4.1", key);
9+
ChatCompletion acompletion = client.CompleteChat([
10+
ChatMessage.CreateSystemMessage("Talk like a pirate."),
11+
ChatMessage.CreateUserMessage("Are semicolons optional in JavaScript?")
12+
]);
13+
Console.WriteLine(acompletion.Content[0].Text);

docs/guides/text/chat/chat_roles.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// SAMPLE: Generate text with messages using different roles
2+
#:package OpenAI@2.2.*-*
3+
#:property PublishAot=false
4+
5+
using OpenAI.Chat;
6+
7+
string key = Environment.GetEnvironmentVariable("OPENAI_KEY")!;
8+
ChatClient client = new("gpt-4.1", key);
9+
ChatCompletion acompletion = client.CompleteChat([
10+
ChatMessage.CreateDeveloperMessage("Talk like a pirate."),
11+
ChatMessage.CreateUserMessage("Are semicolons optional in JavaScript?")
12+
]);
13+
Console.WriteLine(acompletion.Content[0].Text);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// SAMPLE: Generate text from a simple prompt
2+
#:package OpenAI@2.2.*-*
3+
#:property PublishAot=false
4+
5+
using OpenAI.Chat;
6+
7+
string key = Environment.GetEnvironmentVariable("OPENAI_KEY")!;
8+
ChatClient client = new("gpt-4.1", key);
9+
ChatCompletion acompletion = client.CompleteChat("Write a one-sentence bedtime story about a unicorn.");
10+
Console.WriteLine(acompletion.Content[0].Text);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// SAMPLE: Prompt template with file input variable
2+
#:package OpenAI@2.2.*-*
3+
#:property PublishAot=false
4+
5+
using OpenAI.Responses;
6+
using OpenAI.Files;
7+
using System.ClientModel;
8+
9+
string key = Environment.GetEnvironmentVariable("OPENAI_KEY")!;
10+
OpenAIResponseClient client = new("gpt-4.1", key);
11+
12+
// Upload a PDF we will reference in the variables
13+
OpenAIFileClient files = new(key);
14+
OpenAIFile file = files.UploadFile("draconomicon.pdf", FileUploadPurpose.UserData);
15+
16+
OpenAIResponse response = (OpenAIResponse)client.CreateResponse(
17+
BinaryContent.Create(BinaryData.FromObjectAsJson(
18+
new {
19+
model = "gpt-4.1",
20+
prompt = new {
21+
id = "pmpt_abc123",
22+
variables = new {
23+
topic = "Dragons",
24+
reference_pdf = new {
25+
type = "input_file",
26+
file_id = file.Id,
27+
}
28+
}
29+
}
30+
}
31+
)));
32+
Console.WriteLine(response.GetOutputText());
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// SAMPLE: Generate text with instructions
2+
#:package OpenAI@2.2.*-*
3+
#:property PublishAot=false
4+
5+
using OpenAI.Responses;
6+
7+
string key = Environment.GetEnvironmentVariable("OPENAI_KEY")!;
8+
OpenAIResponseClient client = new("gpt-4.1", key);
9+
OpenAIResponse response = client.CreateResponse(
10+
"Are semicolons optional in JavaScript?",
11+
new() { Instructions = "Talk like a pirate." }
12+
);
13+
14+
15+
Console.WriteLine(response.GetOutputText());
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// SAMPLE: Generate text with a prompt template
2+
#:package OpenAI@2.2.*-*
3+
#:property PublishAot=false
4+
5+
using OpenAI.Responses;
6+
using System.ClientModel;
7+
8+
string key = Environment.GetEnvironmentVariable("OPENAI_KEY")!;
9+
OpenAIResponseClient client = new("gpt-4.1", key);
10+
OpenAIResponse response = (OpenAIResponse)client.CreateResponse(
11+
BinaryContent.Create(BinaryData.FromObjectAsJson(
12+
new {
13+
model = "gpt-4.1",
14+
prompt = new {
15+
id = "pmpt_abc123",
16+
version = "2",
17+
variables = new {
18+
customer_name = "Jane Doe",
19+
product = "40oz juice box"
20+
}
21+
}
22+
}
23+
)));
24+
Console.WriteLine(response.GetOutputText());
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// SAMPLE: Generate text with messages using different roles
2+
#:package OpenAI@2.2.*-*
3+
#:property PublishAot=false
4+
5+
using OpenAI.Responses;
6+
7+
string key = Environment.GetEnvironmentVariable("OPENAI_KEY")!;
8+
OpenAIResponseClient client = new("gpt-4.1", key);
9+
OpenAIResponse response = client.CreateResponse([
10+
ResponseItem.CreateDeveloperMessageItem("Talk like a pirate."),
11+
ResponseItem.CreateUserMessageItem("Are semicolons optional in JavaScript?")
12+
]);
13+
14+
15+
Console.WriteLine(response.GetOutputText());
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// SAMPLE: Generate text from a simple prompt
2+
#:package OpenAI@2.2.*-*
3+
#:property PublishAot=false
4+
5+
using OpenAI.Responses;
6+
7+
string key = Environment.GetEnvironmentVariable("OPENAI_KEY")!;
8+
OpenAIResponseClient client = new("gpt-4.1", key);
9+
OpenAIResponse response = client.CreateResponse("Write a one-sentence bedtime story about a unicorn.");
10+
Console.WriteLine(response.GetOutputText());

0 commit comments

Comments
 (0)