diff --git a/docs/guides/README.MD b/docs/guides/README.MD new file mode 100644 index 000000000..8dd091395 --- /dev/null +++ b/docs/guides/README.MD @@ -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 ` command, for example: `dotnet run s1-chat_simpleprompt.cs`. + diff --git a/docs/guides/global.json b/docs/guides/global.json new file mode 100644 index 000000000..c0facce53 --- /dev/null +++ b/docs/guides/global.json @@ -0,0 +1,5 @@ +{ + "sdk": { + "version": "10.*-preview" + } +} \ No newline at end of file diff --git a/docs/guides/text/chat/chat_instructions.cs b/docs/guides/text/chat/chat_instructions.cs new file mode 100644 index 000000000..d6e5b1abf --- /dev/null +++ b/docs/guides/text/chat/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.CreateSystemMessage("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/guides/text/chat/chat_roles.cs b/docs/guides/text/chat/chat_roles.cs new file mode 100644 index 000000000..39bb54097 --- /dev/null +++ b/docs/guides/text/chat/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/guides/text/chat/chat_simpleprompt.cs b/docs/guides/text/chat/chat_simpleprompt.cs new file mode 100644 index 000000000..9f2eeb6e2 --- /dev/null +++ b/docs/guides/text/chat/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/guides/text/responses/responses_fileinput.cs b/docs/guides/text/responses/responses_fileinput.cs new file mode 100644 index 000000000..40eee1ce1 --- /dev/null +++ b/docs/guides/text/responses/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 diff --git a/docs/guides/text/responses/responses_instructions.cs b/docs/guides/text/responses/responses_instructions.cs new file mode 100644 index 000000000..ef7b8212a --- /dev/null +++ b/docs/guides/text/responses/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/guides/text/responses/responses_prompttemplate.cs b/docs/guides/text/responses/responses_prompttemplate.cs new file mode 100644 index 000000000..bd7f7a65d --- /dev/null +++ b/docs/guides/text/responses/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/guides/text/responses/responses_roles.cs b/docs/guides/text/responses/responses_roles.cs new file mode 100644 index 000000000..4e3a91669 --- /dev/null +++ b/docs/guides/text/responses/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/guides/text/responses/responses_simpleprompt.cs b/docs/guides/text/responses/responses_simpleprompt.cs new file mode 100644 index 000000000..2d115e1a3 --- /dev/null +++ b/docs/guides/text/responses/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