openai/openai-dotnet

Public

mirrored from https://github.com/openai/openai-dotnetAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
copilot/sub-pr-1135

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

docs/quickstart/responses/extend_model_with_tools_function_calling.cs

48lines · modecode

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
9using System.Text.Json;
10using System.Text.Json.Serialization.Metadata;
11using OpenAI.Responses;
12
13string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
14ResponsesClient client = new(key);
15
16CreateResponseOptions options = new() { Model = "gpt-5.2" };
17options.Tools.Add(
18 ResponseTool.CreateFunctionTool(
19 functionName: "get_weather",
20 functionDescription: "Get current temperature for a given location.",
21 functionParameters: BinaryData.FromString(
22 """
23 {
24 "type": "object",
25 "properties": {
26 "location": {
27 "type": "string",
28 "description": "City and country e.g. Bogotá, Colombia"
29 }
30 },
31 "required": ["location"],
32 "additionalProperties": false
33 }
34 """),
35 strictModeEnabled: true
36 )
37);
38options.InputItems.Add(
39 ResponseItem.CreateUserMessageItem("What is the weather like in Paris today?")
40);
41
42ResponseResult response = client.CreateResponse(options);
43JsonSerializerOptions serializerOptions = new()
44{
45 TypeInfoResolver = new DefaultJsonTypeInfoResolver()
46};
47
48Console.WriteLine(JsonSerializer.Serialize(response.OutputItems[0], serializerOptions));
49