openai/openai-python

Public

mirrored fromhttps://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
db069cdacaf7ff8d7c1308db9f2465d38252ea1e

Branches

Tags

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

Clone

HTTPS

Download ZIP

examples/module_client.py

25lines · modecode

1import openai
2
3# will default to `os.environ['OPENAI_API_KEY']` if not explicitly set
4openai.api_key = "..."
5
6# all client options can be configured just like the `OpenAI` instantiation counterpart
7openai.base_url = "https://..."
8openai.default_headers = {"x-foo": "true"}
9
10# all API calls work in the exact same fashion as well
11stream = openai.chat.completions.create(
12 model="gpt-4",
13 messages=[
14 {
15 "role": "user",
16 "content": "How do I output all files in a directory using Python?",
17 },
18 ],
19 stream=True,
20)
21
22for chunk in stream:
23 print(chunk.choices[0].delta.content or "", end="", flush=True)
24
25print()
26