test_api.py 882 B

123456789101112131415161718192021222324252627
  1. import openai
  2. # Set your Hugging Face token as the API key if you use embeddings
  3. # If you don't use embeddings, leave it empty
  4. openai.api_key = "YOUR_HUGGING_FACE_TOKEN" # Replace with your actual token
  5. # Set the API base URL if needed, e.g., for a local development environment
  6. openai.api_base = "http://localhost:1337/v1"
  7. def main():
  8. response = openai.ChatCompletion.create(
  9. model="gpt-3.5-turbo",
  10. messages=[{"role": "user", "content": "write a poem about a tree"}],
  11. stream=True,
  12. )
  13. if isinstance(response, dict):
  14. # Not streaming
  15. print(response.choices[0].message.content)
  16. else:
  17. # Streaming
  18. for token in response:
  19. content = token["choices"][0]["delta"].get("content")
  20. if content is not None:
  21. print(content, end="", flush=True)
  22. if __name__ == "__main__":
  23. main()