Skip to content

Commit

Permalink
docs(openai): simplify tool calling example (#1069)
Browse files Browse the repository at this point in the history
  • Loading branch information
axiomofjoy authored Oct 18, 2024
1 parent 4337aa1 commit 67da961
Showing 1 changed file with 13 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import openai
from openai.types.chat import (
ChatCompletionAssistantMessageParam,
ChatCompletionMessageToolCallParam,
ChatCompletionToolMessageParam,
ChatCompletionUserMessageParam,
)
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk import trace as trace_sdk
Expand All @@ -20,6 +19,12 @@

if __name__ == "__main__":
client = openai.OpenAI()
messages = [
ChatCompletionUserMessageParam(
role="user",
content="What's the weather like in San Francisco?",
)
]
response = client.chat.completions.create(
model="gpt-4",
tools=[
Expand All @@ -41,33 +46,16 @@
},
},
],
messages=[
{
"role": "user",
"content": "What's the weather like in San Francisco?",
},
],
messages=messages,
)
message = response.choices[0].message
assert (tool_calls := message.tool_calls)
tool_call_id = tool_calls[0].id
messages.append(message)
messages.append(
ChatCompletionToolMessageParam(content="sunny", role="tool", tool_call_id=tool_call_id),
)
client.chat.completions.create(
model="gpt-4",
messages=[
{"content": "What's the weather like in San Francisco?", "role": "user"},
ChatCompletionAssistantMessageParam(
role="assistant",
content="",
tool_calls=[
ChatCompletionMessageToolCallParam(
id=tool_call_id,
function={"name": "get_weather", "arguments": '{"city": "San Francisco"}'},
type="function",
),
],
),
ChatCompletionToolMessageParam(
content='{"weather_category": "sunny"}', role="tool", tool_call_id=tool_call_id
),
],
messages=messages,
)

0 comments on commit 67da961

Please sign in to comment.