openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.86.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

examples/parsing_tools_stream.py

38lines · modeblame

bf1ca86cRobert Craigie1 years ago1from __future__ import annotations
2
3import rich
4from pydantic import BaseModel
5
6import openai
7from openai import OpenAI
8
9
10class GetWeather(BaseModel):
11city: str
12country: str
13
14
15client = OpenAI()
16
17
18with client.beta.chat.completions.stream(
19model="gpt-4o-2024-08-06",
20messages=[
21{
22"role": "user",
23"content": "What's the weather like in SF and New York?",
24},
25],
26tools=[
27# because we're using `.parse_stream()`, the returned tool calls
28# will be automatically deserialized into this `GetWeather` type
29openai.pydantic_function_tool(GetWeather, name="get_weather"),
30],
31parallel_tool_calls=True,
32) as stream:
33for event in stream:
34if event.type == "tool_calls.function.arguments.delta" or event.type == "tool_calls.function.arguments.done":
35rich.get_console().print(event, width=80)
36
37print("----\n")
38rich.print(stream.get_final_completion())