Update README.md

Added comments to clarify how to set the Hugging Face token as the API key and also improved the comments for code readability
This commit is contained in:
Aryan4884 2023-10-26 21:05:49 +05:30 committed by Heiner Lohaus
parent 91103e6a19
commit 92a31d8281

View File

@ -331,9 +331,12 @@ python -m g4f.api
```py ```py
import openai import openai
openai.api_key = " Leave Empty if you don't use embeddings, otherwise your Hugging Face token" # Set your Hugging Face token as the API key if you use embeddings
openai.api_base = "http://localhost:1337/v1" # If you don't use embeddings, leave it empty
openai.api_key = "YOUR_HUGGING_FACE_TOKEN" # Replace with your actual token
# Set the API base URL if needed, e.g., for a local development environment
openai.api_base = "http://localhost:1337/v1"
def main(): def main():
chat_completion = openai.ChatCompletion.create( chat_completion = openai.ChatCompletion.create(
@ -343,18 +346,18 @@ def main():
) )
if isinstance(chat_completion, dict): if isinstance(chat_completion, dict):
# not stream # Not streaming
print(chat_completion.choices[0].message.content) print(chat_completion.choices[0].message.content)
else: else:
# stream # Streaming
for token in chat_completion: for token in chat_completion:
content = token["choices"][0]["delta"].get("content") content = token["choices"][0]["delta"].get("content")
if content != None: if content is not None:
print(content, end="", flush=True) print(content, end="", flush=True)
if __name__ == "__main":
if __name__ == "__main__":
main() main()
``` ```
## Models ## Models