Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/guides/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# OpenAI Documentation Examples for .NET

## Running the Samples

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.

To run them, you need to install the .NET 10 preview.

Then, from the command line, you can run them using the `dotnet run <sample_name>` command, for example: `dotnet run s1-chat_simpleprompt.cs`.

5 changes: 5 additions & 0 deletions docs/guides/global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"sdk": {
"version": "10.*-preview"
}
}
13 changes: 13 additions & 0 deletions docs/guides/text/chat/chat_instructions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SAMPLE: Generate text with messages using different roles
#:package [email protected].*-*
#:property PublishAot=false

using OpenAI.Chat;

string key = Environment.GetEnvironmentVariable("OPENAI_KEY")!;
ChatClient client = new("gpt-4.1", key);
ChatCompletion acompletion = client.CompleteChat([
ChatMessage.CreateSystemMessage("Talk like a pirate."),
ChatMessage.CreateUserMessage("Are semicolons optional in JavaScript?")
]);
Console.WriteLine(acompletion.Content[0].Text);
13 changes: 13 additions & 0 deletions docs/guides/text/chat/chat_roles.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SAMPLE: Generate text with messages using different roles
#:package [email protected].*-*
#:property PublishAot=false

using OpenAI.Chat;

string key = Environment.GetEnvironmentVariable("OPENAI_KEY")!;
ChatClient client = new("gpt-4.1", key);
ChatCompletion acompletion = client.CompleteChat([
ChatMessage.CreateDeveloperMessage("Talk like a pirate."),
ChatMessage.CreateUserMessage("Are semicolons optional in JavaScript?")
]);
Console.WriteLine(acompletion.Content[0].Text);
10 changes: 10 additions & 0 deletions docs/guides/text/chat/chat_simpleprompt.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// SAMPLE: Generate text from a simple prompt
#:package [email protected].*-*
#:property PublishAot=false

using OpenAI.Chat;

string key = Environment.GetEnvironmentVariable("OPENAI_KEY")!;
ChatClient client = new("gpt-4.1", key);
ChatCompletion acompletion = client.CompleteChat("Write a one-sentence bedtime story about a unicorn.");
Console.WriteLine(acompletion.Content[0].Text);
32 changes: 32 additions & 0 deletions docs/guides/text/responses/responses_fileinput.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SAMPLE: Prompt template with file input variable
#:package [email protected].*-*
#:property PublishAot=false

using OpenAI.Responses;
using OpenAI.Files;
using System.ClientModel;

string key = Environment.GetEnvironmentVariable("OPENAI_KEY")!;
OpenAIResponseClient client = new("gpt-4.1", key);

// Upload a PDF we will reference in the variables
OpenAIFileClient files = new(key);
OpenAIFile file = files.UploadFile("draconomicon.pdf", FileUploadPurpose.UserData);

OpenAIResponse response = (OpenAIResponse)client.CreateResponse(
BinaryContent.Create(BinaryData.FromObjectAsJson(
new {
model = "gpt-4.1",
prompt = new {
id = "pmpt_abc123",
variables = new {
topic = "Dragons",
reference_pdf = new {
type = "input_file",
file_id = file.Id,
}
}
}
}
)));
Console.WriteLine(response.GetOutputText());
15 changes: 15 additions & 0 deletions docs/guides/text/responses/responses_instructions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SAMPLE: Generate text with instructions
#:package [email protected].*-*
#:property PublishAot=false

using OpenAI.Responses;

string key = Environment.GetEnvironmentVariable("OPENAI_KEY")!;
OpenAIResponseClient client = new("gpt-4.1", key);
OpenAIResponse response = client.CreateResponse(
"Are semicolons optional in JavaScript?",
new() { Instructions = "Talk like a pirate." }
);


Console.WriteLine(response.GetOutputText());
24 changes: 24 additions & 0 deletions docs/guides/text/responses/responses_prompttemplate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// SAMPLE: Generate text with a prompt template
#:package [email protected].*-*
#:property PublishAot=false

using OpenAI.Responses;
using System.ClientModel;

string key = Environment.GetEnvironmentVariable("OPENAI_KEY")!;
OpenAIResponseClient client = new("gpt-4.1", key);
OpenAIResponse response = (OpenAIResponse)client.CreateResponse(
BinaryContent.Create(BinaryData.FromObjectAsJson(
new {
model = "gpt-4.1",
prompt = new {
id = "pmpt_abc123",
version = "2",
variables = new {
customer_name = "Jane Doe",
product = "40oz juice box"
}
}
}
)));
Console.WriteLine(response.GetOutputText());
15 changes: 15 additions & 0 deletions docs/guides/text/responses/responses_roles.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SAMPLE: Generate text with messages using different roles
#:package [email protected].*-*
#:property PublishAot=false

using OpenAI.Responses;

string key = Environment.GetEnvironmentVariable("OPENAI_KEY")!;
OpenAIResponseClient client = new("gpt-4.1", key);
OpenAIResponse response = client.CreateResponse([
ResponseItem.CreateDeveloperMessageItem("Talk like a pirate."),
ResponseItem.CreateUserMessageItem("Are semicolons optional in JavaScript?")
]);


Console.WriteLine(response.GetOutputText());
10 changes: 10 additions & 0 deletions docs/guides/text/responses/responses_simpleprompt.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// SAMPLE: Generate text from a simple prompt
#:package [email protected].*-*
#:property PublishAot=false

using OpenAI.Responses;

string key = Environment.GetEnvironmentVariable("OPENAI_KEY")!;
OpenAIResponseClient client = new("gpt-4.1", key);
OpenAIResponse response = client.CreateResponse("Write a one-sentence bedtime story about a unicorn.");
Console.WriteLine(response.GetOutputText());