返回提交历史
Modified
README.md
+11
-8
XFEstudio/gpt4free
Update openai example
f3fb6f8a
代码差异
1 个文件
+11
-8
@@ -369,17 +369,19 @@ python -m g4f.api.run
369
369
```
370
370
371
371
```python
372
import openai
372
from openai import OpenAI
373
373
374
# Set your Hugging Face token as the API key if you use embeddings
375
# If you don't use embeddings, leave it empty
376
openai.api_key = "YOUR_HUGGING_FACE_TOKEN" # Replace with your actual token
374
client = OpenAI(
375
# Set your Hugging Face token as the API key if you use embeddings
376
api_key="YOUR_HUGGING_FACE_TOKEN",
377
378
# Set the API base URL if needed, e.g., for a local development environment
379
base_url="http://localhost:1337/v1"
380
)
377
381
378
# Set the API base URL if needed, e.g., for a local development environment
379
openai.api_base = "http://localhost:1337/v1"
380
382
381
383
def main():
382
chat_completion = openai.ChatCompletion.create(
384
chat_completion = client.chat.completions.create(
383
385
model="gpt-3.5-turbo",
384
386
messages=[{"role": "user", "content": "write a poem about a tree"}],
385
387
stream=True,
@@ -391,10 +393,11 @@ def main():
391
393
else:
392
394
# Streaming
393
395
for token in chat_completion:
394
content = token["choices"][0]["delta"].get("content")
396
content = token.choices[0].delta.content
395
397
if content is not None:
396
398
print(content, end="", flush=True)
397
399
400
398
401
if __name__ == "__main__":
399
402
main()
400
403
```