api.py 547 B

12345678910111213141516171819
  1. import requests
  2. import json
  3. url = "http://localhost:1337/v1/chat/completions"
  4. body = {
  5. "model": "",
  6. "provider": "",
  7. "stream": True,
  8. "messages": [
  9. {"role": "assistant", "content": "What can you do? Who are you?"}
  10. ]
  11. }
  12. lines = requests.post(url, json=body, stream=True).iter_lines()
  13. for line in lines:
  14. if line.startswith(b"data: "):
  15. try:
  16. print(json.loads(line[6:]).get("choices", [{"delta": {}}])[0]["delta"].get("content", ""), end="")
  17. except json.JSONDecodeError:
  18. pass
  19. print()