completion.py 665 B

12345678910111213141516171819202122232425262728293031
  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. # Completion API
  13. stream = False
  14. completion = client.completions.create(
  15. model=model,
  16. prompt="A robot may not injure a human being",
  17. echo=False,
  18. n=2,
  19. stream=stream,
  20. logprobs=3)
  21. print("Completion results:")
  22. if stream:
  23. for c in completion:
  24. print(c)
  25. else:
  26. print(completion)