Skip to content

Commit 1c36c76

Browse files
Add code samples for web search (#695)
* Add code samples for web search * Update document link * Update docs/guides/tools-web-search/responses/user-location.cs --------- Co-authored-by: Christopher Scott <[email protected]>
1 parent 7197911 commit 1c36c76

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
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-web-search?api-mode=responses
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());
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// SAMPLE: Get information from web search by specifying user location through Responses API
2+
// PAGE: https://platform.openai.com/docs/guides/tools-web-search?api-mode=responses#user-location
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+
userLocation: WebSearchToolLocation.CreateApproximateLocation(
17+
country: "GB",
18+
city: "London",
19+
region: "Granary Square"
20+
)
21+
));
22+
23+
OpenAIResponse response = (OpenAIResponse)client.CreateResponse([
24+
ResponseItem.CreateUserMessageItem([
25+
ResponseContentPart.CreateInputTextPart(
26+
"What are the best restaurants near me?"
27+
)
28+
])
29+
], options);
30+
31+
Console.WriteLine(response.GetOutputText());

0 commit comments

Comments
 (0)