Skip to content

Commit d456cbe

Browse files
authored
Add code samples for the "using tools" page (#698)
1 parent 8b12249 commit d456cbe

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed
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/guides/tools?tool-type=file-search
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/guides/tools?tool-type=function-calling
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/guides/tools?tool-type=remote-mcp
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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// SAMPLE: Get information from web search through Responses API
2+
// PAGE: https://platform.openai.com/docs/guides/tools?tool-type=web-search
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(
20+
"What was a positive news story from today?"
21+
)
22+
])
23+
], options);
24+
25+
Console.WriteLine(response.GetOutputText());

0 commit comments

Comments
 (0)