Skip to content

Commit ce229cf

Browse files
initial samples
1 parent 5b60d27 commit ce229cf

File tree

9 files changed

+141
-0
lines changed

9 files changed

+141
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# OpenAI Documentation Examples for .NET
2+
3+
## Running the Samples
4+
5+
The samples use new .NET 10 feature which allows running single *.cs file applications.
6+
Each of the files *.cs in this folder is a standalone application.
7+
To run them you need to install .NET 10 preview
8+
Then from the command line run them using `dotnet run <sample_name>`, e.g. `dotnet run s1-chat_simpleprompt.cs`
9+
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: 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());
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: 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: 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: 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: 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: 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());

0 commit comments

Comments
 (0)