chat_completion.py 873 B

123456789101112131415161718192021222324252627282930313233343536
  1. from openai import OpenAI
  2. # Modify OpenAI's API key and API base to use Aphrodite's API server.
  3. openai_api_key = "EMPTY"
  4. openai_api_base = "http://localhost:2242/v1"
  5. client = OpenAI(
  6. # defaults to os.environ.get("OPENAI_API_KEY")
  7. api_key=openai_api_key,
  8. base_url=openai_api_base,
  9. )
  10. models = client.models.list()
  11. model = models.data[0].id
  12. chat_completion = client.chat.completions.create(
  13. messages=[{
  14. "role": "system",
  15. "content": "You are a helpful assistant."
  16. }, {
  17. "role": "user",
  18. "content": "Who won the world series in 2020?"
  19. }, {
  20. "role":
  21. "assistant",
  22. "content":
  23. "The Los Angeles Dodgers won the World Series in 2020."
  24. }, {
  25. "role": "user",
  26. "content": "Where was it played?"
  27. }],
  28. model=model,
  29. )
  30. print("Chat completion results:")
  31. print(chat_completion)