From ce229cf5151d54caf1000f4fd1aa2f75424841e7 Mon Sep 17 00:00:00 2001 From: Krzysztof Cwalina Date: Thu, 26 Jun 2025 14:40:47 -0700 Subject: [PATCH 1/7] initial samples --- docs/examples/text-and-prompting/README.MD | 9 ++++++ .../s1-chat_simpleprompt.cs | 10 ++++++ .../s2-responses_simpleprompt.cs | 10 ++++++ .../s3-chat_instructions.cs | 13 ++++++++ .../s4-responses_instructions.cs | 15 +++++++++ .../text-and-prompting/s5-chat_roles.cs | 13 ++++++++ .../text-and-prompting/s6-responses_roles.cs | 15 +++++++++ .../s7-responses_prompttemplate.cs | 24 ++++++++++++++ .../s8-responses_fileinput.cs | 32 +++++++++++++++++++ 9 files changed, 141 insertions(+) create mode 100644 docs/examples/text-and-prompting/README.MD create mode 100644 docs/examples/text-and-prompting/s1-chat_simpleprompt.cs create mode 100644 docs/examples/text-and-prompting/s2-responses_simpleprompt.cs create mode 100644 docs/examples/text-and-prompting/s3-chat_instructions.cs create mode 100644 docs/examples/text-and-prompting/s4-responses_instructions.cs create mode 100644 docs/examples/text-and-prompting/s5-chat_roles.cs create mode 100644 docs/examples/text-and-prompting/s6-responses_roles.cs create mode 100644 docs/examples/text-and-prompting/s7-responses_prompttemplate.cs create mode 100644 docs/examples/text-and-prompting/s8-responses_fileinput.cs diff --git a/docs/examples/text-and-prompting/README.MD b/docs/examples/text-and-prompting/README.MD new file mode 100644 index 000000000..975cb2955 --- /dev/null +++ b/docs/examples/text-and-prompting/README.MD @@ -0,0 +1,9 @@ +# OpenAI Documentation Examples for .NET + +## Running the Samples + +The samples use new .NET 10 feature which allows running single *.cs file applications. +Each of the files *.cs in this folder is a standalone application. +To run them you need to install .NET 10 preview +Then from the command line run them using `dotnet run `, e.g. `dotnet run s1-chat_simpleprompt.cs` + diff --git a/docs/examples/text-and-prompting/s1-chat_simpleprompt.cs b/docs/examples/text-and-prompting/s1-chat_simpleprompt.cs new file mode 100644 index 000000000..9f2eeb6e2 --- /dev/null +++ b/docs/examples/text-and-prompting/s1-chat_simpleprompt.cs @@ -0,0 +1,10 @@ +// SAMPLE: Generate text from a simple prompt +#:package OpenAI@2.2.*-* +#: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); \ No newline at end of file diff --git a/docs/examples/text-and-prompting/s2-responses_simpleprompt.cs b/docs/examples/text-and-prompting/s2-responses_simpleprompt.cs new file mode 100644 index 000000000..2d115e1a3 --- /dev/null +++ b/docs/examples/text-and-prompting/s2-responses_simpleprompt.cs @@ -0,0 +1,10 @@ +// SAMPLE: Generate text from a simple prompt +#:package OpenAI@2.2.*-* +#: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()); \ No newline at end of file diff --git a/docs/examples/text-and-prompting/s3-chat_instructions.cs b/docs/examples/text-and-prompting/s3-chat_instructions.cs new file mode 100644 index 000000000..39bb54097 --- /dev/null +++ b/docs/examples/text-and-prompting/s3-chat_instructions.cs @@ -0,0 +1,13 @@ +// SAMPLE: Generate text with messages using different roles +#:package OpenAI@2.2.*-* +#: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); \ No newline at end of file diff --git a/docs/examples/text-and-prompting/s4-responses_instructions.cs b/docs/examples/text-and-prompting/s4-responses_instructions.cs new file mode 100644 index 000000000..ef7b8212a --- /dev/null +++ b/docs/examples/text-and-prompting/s4-responses_instructions.cs @@ -0,0 +1,15 @@ +// SAMPLE: Generate text with instructions +#:package OpenAI@2.2.*-* +#: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()); \ No newline at end of file diff --git a/docs/examples/text-and-prompting/s5-chat_roles.cs b/docs/examples/text-and-prompting/s5-chat_roles.cs new file mode 100644 index 000000000..39bb54097 --- /dev/null +++ b/docs/examples/text-and-prompting/s5-chat_roles.cs @@ -0,0 +1,13 @@ +// SAMPLE: Generate text with messages using different roles +#:package OpenAI@2.2.*-* +#: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); \ No newline at end of file diff --git a/docs/examples/text-and-prompting/s6-responses_roles.cs b/docs/examples/text-and-prompting/s6-responses_roles.cs new file mode 100644 index 000000000..4e3a91669 --- /dev/null +++ b/docs/examples/text-and-prompting/s6-responses_roles.cs @@ -0,0 +1,15 @@ +// SAMPLE: Generate text with messages using different roles +#:package OpenAI@2.2.*-* +#: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()); \ No newline at end of file diff --git a/docs/examples/text-and-prompting/s7-responses_prompttemplate.cs b/docs/examples/text-and-prompting/s7-responses_prompttemplate.cs new file mode 100644 index 000000000..bd7f7a65d --- /dev/null +++ b/docs/examples/text-and-prompting/s7-responses_prompttemplate.cs @@ -0,0 +1,24 @@ +// SAMPLE: Generate text with a prompt template +#:package OpenAI@2.2.*-* +#: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()); \ No newline at end of file diff --git a/docs/examples/text-and-prompting/s8-responses_fileinput.cs b/docs/examples/text-and-prompting/s8-responses_fileinput.cs new file mode 100644 index 000000000..40eee1ce1 --- /dev/null +++ b/docs/examples/text-and-prompting/s8-responses_fileinput.cs @@ -0,0 +1,32 @@ +// SAMPLE: Prompt template with file input variable +#:package OpenAI@2.2.*-* +#: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()); \ No newline at end of file From 2649ea576d2ca90b5ce19d5faf91fc8b880b5c57 Mon Sep 17 00:00:00 2001 From: Krzysztof Cwalina Date: Thu, 26 Jun 2025 14:47:37 -0700 Subject: [PATCH 2/7] added global.json to force .NET 10 usage in this folder --- docs/examples/global.json | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 docs/examples/global.json diff --git a/docs/examples/global.json b/docs/examples/global.json new file mode 100644 index 000000000..c0facce53 --- /dev/null +++ b/docs/examples/global.json @@ -0,0 +1,5 @@ +{ + "sdk": { + "version": "10.*-preview" + } +} \ No newline at end of file From baf69ba17c4514b63562e3a949a1a955e91dd1a4 Mon Sep 17 00:00:00 2001 From: Krzysztof Cwalina Date: Thu, 26 Jun 2025 14:49:37 -0700 Subject: [PATCH 3/7] updated readme --- docs/examples/text-and-prompting/README.MD | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/docs/examples/text-and-prompting/README.MD b/docs/examples/text-and-prompting/README.MD index 975cb2955..df5339aaa 100644 --- a/docs/examples/text-and-prompting/README.MD +++ b/docs/examples/text-and-prompting/README.MD @@ -2,8 +2,9 @@ ## Running the Samples -The samples use new .NET 10 feature which allows running single *.cs file applications. -Each of the files *.cs in this folder is a standalone application. -To run them you need to install .NET 10 preview -Then from the command line run them using `dotnet run `, e.g. `dotnet run s1-chat_simpleprompt.cs` +The samples use a new .NET 10 feature that allows running single *.cs file applications. Each *.cs 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 ` command, for example: `dotnet run s1-chat_simpleprompt.cs`. From c7b845ff344bb3d2d73928ba7de139b1e27a4f17 Mon Sep 17 00:00:00 2001 From: Krzysztof Cwalina Date: Thu, 26 Jun 2025 14:53:22 -0700 Subject: [PATCH 4/7] added link --- docs/examples/text-and-prompting/README.MD | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/examples/text-and-prompting/README.MD b/docs/examples/text-and-prompting/README.MD index df5339aaa..8dd091395 100644 --- a/docs/examples/text-and-prompting/README.MD +++ b/docs/examples/text-and-prompting/README.MD @@ -2,7 +2,7 @@ ## Running the Samples -The samples use a new .NET 10 feature that allows running single *.cs file applications. Each *.cs file in this folder is a standalone application. +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. From eb94c20d9175f2edfc246a9ce6c72269cfc879e2 Mon Sep 17 00:00:00 2001 From: Krzysztof Cwalina Date: Thu, 26 Jun 2025 14:56:57 -0700 Subject: [PATCH 5/7] changes s3 to use system message --- docs/examples/text-and-prompting/s3-chat_instructions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/examples/text-and-prompting/s3-chat_instructions.cs b/docs/examples/text-and-prompting/s3-chat_instructions.cs index 39bb54097..d6e5b1abf 100644 --- a/docs/examples/text-and-prompting/s3-chat_instructions.cs +++ b/docs/examples/text-and-prompting/s3-chat_instructions.cs @@ -7,7 +7,7 @@ string key = Environment.GetEnvironmentVariable("OPENAI_KEY")!; ChatClient client = new("gpt-4.1", key); ChatCompletion acompletion = client.CompleteChat([ - ChatMessage.CreateDeveloperMessage("Talk like a pirate."), + ChatMessage.CreateSystemMessage("Talk like a pirate."), ChatMessage.CreateUserMessage("Are semicolons optional in JavaScript?") ]); Console.WriteLine(acompletion.Content[0].Text); \ No newline at end of file From 8bdea9f23f3f392d419b87204e0fba22083fd0aa Mon Sep 17 00:00:00 2001 From: Krzysztof Cwalina Date: Thu, 26 Jun 2025 15:00:53 -0700 Subject: [PATCH 6/7] changed folder structure --- docs/{examples => guides}/global.json | 0 docs/{examples/text-and-prompting => guides/text}/README.MD | 0 .../text/chat/chat_instructions.cs} | 0 .../s5-chat_roles.cs => guides/text/chat/chat_roles.cs} | 0 .../text/chat/chat_simpleprompt.cs} | 0 .../text/responses/responses_fileinput.cs} | 0 .../text/responses/responses_instructions.cs} | 0 .../text/responses/responses_prompttemplate.cs} | 0 .../text/responses/responses_roles.cs} | 0 .../text/responses/responses_simpleprompt.cs} | 0 10 files changed, 0 insertions(+), 0 deletions(-) rename docs/{examples => guides}/global.json (100%) rename docs/{examples/text-and-prompting => guides/text}/README.MD (100%) rename docs/{examples/text-and-prompting/s3-chat_instructions.cs => guides/text/chat/chat_instructions.cs} (100%) rename docs/{examples/text-and-prompting/s5-chat_roles.cs => guides/text/chat/chat_roles.cs} (100%) rename docs/{examples/text-and-prompting/s1-chat_simpleprompt.cs => guides/text/chat/chat_simpleprompt.cs} (100%) rename docs/{examples/text-and-prompting/s8-responses_fileinput.cs => guides/text/responses/responses_fileinput.cs} (100%) rename docs/{examples/text-and-prompting/s4-responses_instructions.cs => guides/text/responses/responses_instructions.cs} (100%) rename docs/{examples/text-and-prompting/s7-responses_prompttemplate.cs => guides/text/responses/responses_prompttemplate.cs} (100%) rename docs/{examples/text-and-prompting/s6-responses_roles.cs => guides/text/responses/responses_roles.cs} (100%) rename docs/{examples/text-and-prompting/s2-responses_simpleprompt.cs => guides/text/responses/responses_simpleprompt.cs} (100%) diff --git a/docs/examples/global.json b/docs/guides/global.json similarity index 100% rename from docs/examples/global.json rename to docs/guides/global.json diff --git a/docs/examples/text-and-prompting/README.MD b/docs/guides/text/README.MD similarity index 100% rename from docs/examples/text-and-prompting/README.MD rename to docs/guides/text/README.MD diff --git a/docs/examples/text-and-prompting/s3-chat_instructions.cs b/docs/guides/text/chat/chat_instructions.cs similarity index 100% rename from docs/examples/text-and-prompting/s3-chat_instructions.cs rename to docs/guides/text/chat/chat_instructions.cs diff --git a/docs/examples/text-and-prompting/s5-chat_roles.cs b/docs/guides/text/chat/chat_roles.cs similarity index 100% rename from docs/examples/text-and-prompting/s5-chat_roles.cs rename to docs/guides/text/chat/chat_roles.cs diff --git a/docs/examples/text-and-prompting/s1-chat_simpleprompt.cs b/docs/guides/text/chat/chat_simpleprompt.cs similarity index 100% rename from docs/examples/text-and-prompting/s1-chat_simpleprompt.cs rename to docs/guides/text/chat/chat_simpleprompt.cs diff --git a/docs/examples/text-and-prompting/s8-responses_fileinput.cs b/docs/guides/text/responses/responses_fileinput.cs similarity index 100% rename from docs/examples/text-and-prompting/s8-responses_fileinput.cs rename to docs/guides/text/responses/responses_fileinput.cs diff --git a/docs/examples/text-and-prompting/s4-responses_instructions.cs b/docs/guides/text/responses/responses_instructions.cs similarity index 100% rename from docs/examples/text-and-prompting/s4-responses_instructions.cs rename to docs/guides/text/responses/responses_instructions.cs diff --git a/docs/examples/text-and-prompting/s7-responses_prompttemplate.cs b/docs/guides/text/responses/responses_prompttemplate.cs similarity index 100% rename from docs/examples/text-and-prompting/s7-responses_prompttemplate.cs rename to docs/guides/text/responses/responses_prompttemplate.cs diff --git a/docs/examples/text-and-prompting/s6-responses_roles.cs b/docs/guides/text/responses/responses_roles.cs similarity index 100% rename from docs/examples/text-and-prompting/s6-responses_roles.cs rename to docs/guides/text/responses/responses_roles.cs diff --git a/docs/examples/text-and-prompting/s2-responses_simpleprompt.cs b/docs/guides/text/responses/responses_simpleprompt.cs similarity index 100% rename from docs/examples/text-and-prompting/s2-responses_simpleprompt.cs rename to docs/guides/text/responses/responses_simpleprompt.cs From c7d497c3fa11b7741e5894646bb5aabb391c7ee3 Mon Sep 17 00:00:00 2001 From: Krzysztof Cwalina Date: Thu, 26 Jun 2025 15:02:06 -0700 Subject: [PATCH 7/7] moved readme --- docs/guides/{text => }/README.MD | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename docs/guides/{text => }/README.MD (100%) diff --git a/docs/guides/text/README.MD b/docs/guides/README.MD similarity index 100% rename from docs/guides/text/README.MD rename to docs/guides/README.MD