openai/openai-python
Publicmirrored fromhttps://github.com/openai/openai-pythonAvailable
examples/module_client.py
25lines · modecode
| 1 | import openai |
| 2 | |
| 3 | # will default to `os.environ['OPENAI_API_KEY']` if not explicitly set |
| 4 | openai.api_key = "..." |
| 5 | |
| 6 | # all client options can be configured just like the `OpenAI` instantiation counterpart |
| 7 | openai.base_url = "https://..." |
| 8 | openai.default_headers = {"x-foo": "true"} |
| 9 | |
| 10 | # all API calls work in the exact same fashion as well |
| 11 | stream = 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 | |
| 22 | for chunk in stream: |
| 23 | print(chunk.choices[0].delta.content or "", end="", flush=True) |
| 24 | |
| 25 | print() |
| 26 | |