diff options
author | H Lohaus <hlohaus@users.noreply.github.com> | 2024-01-04 20:30:43 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-04 20:30:43 +0100 |
commit | 4a55337e9e3f14879bdbe4aaaf5b83b4f6840e23 (patch) | |
tree | 5666afdfd278b8b376c755b325369667dcdb7f41 | |
parent | Merge pull request #1430 from ramonvc/main (diff) | |
parent | Update openai example (diff) | |
download | gpt4free-4a55337e9e3f14879bdbe4aaaf5b83b4f6840e23.tar gpt4free-4a55337e9e3f14879bdbe4aaaf5b83b4f6840e23.tar.gz gpt4free-4a55337e9e3f14879bdbe4aaaf5b83b4f6840e23.tar.bz2 gpt4free-4a55337e9e3f14879bdbe4aaaf5b83b4f6840e23.tar.lz gpt4free-4a55337e9e3f14879bdbe4aaaf5b83b4f6840e23.tar.xz gpt4free-4a55337e9e3f14879bdbe4aaaf5b83b4f6840e23.tar.zst gpt4free-4a55337e9e3f14879bdbe4aaaf5b83b4f6840e23.zip |
-rw-r--r-- | README.md | 19 |
1 files changed, 11 insertions, 8 deletions
@@ -369,17 +369,19 @@ python -m g4f.api.run ``` ```python -import openai +from openai import OpenAI -# Set your Hugging Face token as the API key if you use embeddings -# If you don't use embeddings, leave it empty -openai.api_key = "YOUR_HUGGING_FACE_TOKEN" # Replace with your actual token +client = OpenAI( + # Set your Hugging Face token as the API key if you use embeddings + api_key="YOUR_HUGGING_FACE_TOKEN", + + # Set the API base URL if needed, e.g., for a local development environment + base_url="http://localhost:1337/v1" +) -# Set the API base URL if needed, e.g., for a local development environment -openai.api_base = "http://localhost:1337/v1" def main(): - chat_completion = openai.ChatCompletion.create( + chat_completion = client.chat.completions.create( model="gpt-3.5-turbo", messages=[{"role": "user", "content": "write a poem about a tree"}], stream=True, @@ -391,10 +393,11 @@ def main(): else: # Streaming for token in chat_completion: - content = token["choices"][0]["delta"].get("content") + content = token.choices[0].delta.content if content is not None: print(content, end="", flush=True) + if __name__ == "__main__": main() ``` |