Skip to content

Commit 8a4b09b

Browse files
authored
Add code samples for the quickstart page (#684)
This PR is intended to add code samples for the [quickstart page](https://platform.openai.com/docs/quickstart) including: - developer quickstart - Analyze images and files - image URL - Analyze images and files - file URL - Analyze images and files - file upload - Extend the model with tools - web search - Extend the model with tools - file search - Extend the model with tools - function calling - Extend the model with tools - remote MCP - Stream responses and build realtime apps This PR doesn't include the sample code for: - Build agents
1 parent f76be13 commit 8a4b09b

10 files changed

+235
-0
lines changed

docs/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.*-rc*"
4+
}
5+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// SAMPLE: Analyzes file from a file URL through Responses API
2+
// PAGE: https://platform.openai.com/docs/quickstart?input-type=file-url#analyze-images-and-files
3+
// GUIDANCE: Instructions to run this code: https://aka.ms/oai/net/start
4+
#pragma warning disable OPENAI001
5+
6+
#:package OpenAI@2.*
7+
#:property PublishAot=false
8+
9+
using OpenAI.Files;
10+
using OpenAI.Responses;
11+
12+
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
13+
OpenAIResponseClient client = new(model: "gpt-5", apiKey: key);
14+
15+
using HttpClient http = new();
16+
using Stream stream = await http.GetStreamAsync("https://www.berkshirehathaway.com/letters/2024ltr.pdf");
17+
OpenAIFileClient files = new(key);
18+
OpenAIFile file = files.UploadFile(stream, "2024ltr.pdf", FileUploadPurpose.UserData);
19+
20+
OpenAIResponse response = (OpenAIResponse)client.CreateResponse([
21+
ResponseItem.CreateUserMessageItem([
22+
ResponseContentPart.CreateInputTextPart("Analyze the letter and provide a summary of the key points."),
23+
ResponseContentPart.CreateInputFilePart(file.Id)
24+
])
25+
]);
26+
27+
Console.WriteLine(response.GetOutputText());
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// SAMPLE: Analyzes image from an image URL through Responses API
2+
// PAGE: https://platform.openai.com/docs/quickstart?input-type=image-url#analyze-images-and-files
3+
// GUIDANCE: Instructions to run this code: https://aka.ms/oai/net/start
4+
#pragma warning disable OPENAI001
5+
6+
#:package OpenAI@2.*
7+
#:property PublishAot=false
8+
9+
using OpenAI.Responses;
10+
11+
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
12+
OpenAIResponseClient client = new(model: "gpt-5", apiKey: key);
13+
OpenAIResponse response = (OpenAIResponse)client.CreateResponse([
14+
ResponseItem.CreateUserMessageItem([
15+
ResponseContentPart.CreateInputTextPart("What is in this image?"),
16+
ResponseContentPart.CreateInputImagePart(new Uri("https://openai-documentation.vercel.app/images/cat_and_otter.png"))
17+
])
18+
]);
19+
20+
Console.WriteLine(response.GetOutputText());
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// SAMPLE: Analyzes file from a file upload through Responses API
2+
// PAGE: https://platform.openai.com/docs/quickstart?input-type=file-upload#analyze-images-and-files
3+
// GUIDANCE: Instructions to run this code: https://aka.ms/oai/net/start
4+
#pragma warning disable OPENAI001
5+
6+
#:package OpenAI@2.*
7+
#:property PublishAot=false
8+
9+
using OpenAI.Files;
10+
using OpenAI.Responses;
11+
12+
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
13+
OpenAIResponseClient client = new(model: "gpt-5", apiKey: key);
14+
15+
OpenAIFileClient files = new(key);
16+
OpenAIFile file = files.UploadFile("draconomicon.pdf", FileUploadPurpose.UserData);
17+
18+
OpenAIResponse response = (OpenAIResponse)client.CreateResponse([
19+
ResponseItem.CreateUserMessageItem([
20+
ResponseContentPart.CreateInputFilePart(file.Id),
21+
ResponseContentPart.CreateInputTextPart("What is the first dragon in the book?")
22+
])
23+
]);
24+
25+
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 from a simple prompt through Responses API
2+
// PAGE: https://platform.openai.com/docs/quickstart
3+
// GUIDANCE: Instructions to run this code: https://aka.ms/oai/net/start
4+
#pragma warning disable OPENAI001
5+
6+
#:package OpenAI@2.*
7+
#:property PublishAot=false
8+
9+
using OpenAI.Responses;
10+
11+
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
12+
OpenAIResponseClient client = new(model: "gpt-5", apiKey: key);
13+
OpenAIResponse response = client.CreateResponse("Write a one-sentence bedtime story about a unicorn.");
14+
15+
Console.WriteLine(response.GetOutputText());
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// SAMPLE: Get information from file search through Responses API
2+
// PAGE: https://platform.openai.com/docs/quickstart?tool-type=file-search#extend-the-model-with-tools
3+
// GUIDANCE: Instructions to run this code: https://aka.ms/oai/net/start
4+
#pragma warning disable OPENAI001
5+
6+
#:package OpenAI@2.*
7+
#:property PublishAot=false
8+
9+
using OpenAI.Responses;
10+
11+
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
12+
OpenAIResponseClient client = new(model: "gpt-5", apiKey: key);
13+
14+
ResponseCreationOptions options = new();
15+
options.Tools.Add(ResponseTool.CreateFileSearchTool([ "<vector_store_id>" ]));
16+
17+
OpenAIResponse response = (OpenAIResponse)client.CreateResponse([
18+
ResponseItem.CreateUserMessageItem([
19+
ResponseContentPart.CreateInputTextPart("What is deep research by OpenAI?")
20+
])
21+
], options);
22+
23+
Console.WriteLine(response.GetOutputText());
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// SAMPLE: Generate response from function calling through Responses API
2+
// PAGE: https://platform.openai.com/docs/quickstart?tool-type=function-calling#extend-the-model-with-tools
3+
// GUIDANCE: Instructions to run this code: https://aka.ms/oai/net/start
4+
#pragma warning disable OPENAI001
5+
6+
#:package OpenAI@2.*
7+
#:property PublishAot=false
8+
9+
using System.Text.Json;
10+
using OpenAI.Responses;
11+
12+
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
13+
OpenAIResponseClient client = new(model: "gpt-5", apiKey: key);
14+
15+
ResponseCreationOptions options = new();
16+
options.Tools.Add(ResponseTool.CreateFunctionTool(
17+
functionName: "get_weather",
18+
functionDescription: "Get current temperature for a given location.",
19+
functionParameters: BinaryData.FromObjectAsJson(new
20+
{
21+
type = "object",
22+
properties = new
23+
{
24+
location = new
25+
{
26+
type = "string",
27+
description = "City and country e.g. Bogotá, Colombia",
28+
}
29+
},
30+
required = new[] { "location" },
31+
additionalProperties = false
32+
}),
33+
strictModeEnabled: true
34+
)
35+
);
36+
37+
OpenAIResponse response = (OpenAIResponse)client.CreateResponse([
38+
ResponseItem.CreateUserMessageItem([
39+
ResponseContentPart.CreateInputTextPart("What is the weather like in Paris today?")
40+
])
41+
], options);
42+
43+
Console.WriteLine(JsonSerializer.Serialize(response.OutputItems[0]));
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// SAMPLE: Generate response from remote MCP through Responses API
2+
// PAGE: https://platform.openai.com/docs/quickstart?tool-type=remote-mcp#extend-the-model-with-tools
3+
// GUIDANCE: Instructions to run this code: https://aka.ms/oai/net/start
4+
#pragma warning disable OPENAI001
5+
6+
#:package OpenAI@2.*
7+
#:property PublishAot=false
8+
9+
using OpenAI.Responses;
10+
11+
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
12+
OpenAIResponseClient client = new(model: "gpt-5", apiKey: key);
13+
14+
ResponseCreationOptions options = new();
15+
options.Tools.Add(ResponseTool.CreateMcpTool(
16+
serverLabel: "dmcp",
17+
serverUri: new Uri("https://dmcp-server.deno.dev/sse"),
18+
toolCallApprovalPolicy: new McpToolCallApprovalPolicy(GlobalMcpToolCallApprovalPolicy.NeverRequireApproval)
19+
));
20+
21+
OpenAIResponse response = (OpenAIResponse)client.CreateResponse([
22+
ResponseItem.CreateUserMessageItem([
23+
ResponseContentPart.CreateInputTextPart("Roll 2d4+1")
24+
])
25+
], options);
26+
27+
Console.WriteLine(response.GetOutputText());
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// SAMPLE: Get information from web search through Responses API
2+
// PAGE: https://platform.openai.com/docs/quickstart?tool-type=web-search#extend-the-model-with-tools
3+
// GUIDANCE: Instructions to run this code: https://aka.ms/oai/net/start
4+
#pragma warning disable OPENAI001
5+
6+
#:package OpenAI@2.*
7+
#:property PublishAot=false
8+
9+
using OpenAI.Responses;
10+
11+
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
12+
OpenAIResponseClient client = new(model: "gpt-5", apiKey: key);
13+
14+
ResponseCreationOptions options = new();
15+
options.Tools.Add(ResponseTool.CreateWebSearchTool());
16+
17+
OpenAIResponse response = (OpenAIResponse)client.CreateResponse([
18+
ResponseItem.CreateUserMessageItem([
19+
ResponseContentPart.CreateInputTextPart("What was a positive news story from today?")
20+
])
21+
], options);
22+
23+
Console.WriteLine(response.GetOutputText());
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// SAMPLE: Generate streaming response for realtime apps through Responses API
2+
// PAGE: https://platform.openai.com/docs/quickstart#stream-responses-and-build-realtime-apps
3+
// GUIDANCE: Instructions to run this code: https://aka.ms/oai/net/start
4+
#pragma warning disable OPENAI001
5+
6+
#:package System.Linq.Async@6.*
7+
#:package OpenAI@2.*
8+
#:property PublishAot=false
9+
10+
using OpenAI.Responses;
11+
12+
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
13+
OpenAIResponseClient client = new(model: "gpt-5", apiKey: key);
14+
15+
var responses = client.CreateResponseStreamingAsync([
16+
ResponseItem.CreateUserMessageItem([
17+
ResponseContentPart.CreateInputTextPart("Say 'double bubble bath' ten times fast.")
18+
])
19+
]);
20+
21+
await foreach (var response in responses)
22+
{
23+
if (response is StreamingResponseOutputTextDeltaUpdate delta)
24+
{
25+
Console.Write(delta.Delta);
26+
}
27+
}

0 commit comments

Comments
 (0)