api.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import requests
  2. import json
  3. import uuid
  4. url = "http://localhost:1337/v1/chat/completions"
  5. conversation_id = str(uuid.uuid4())
  6. body = {
  7. "model": "",
  8. "provider": "Copilot",
  9. "stream": True,
  10. "messages": [
  11. {"role": "user", "content": "Hello, i am Heiner. How are you?"}
  12. ],
  13. "conversation_id": conversation_id
  14. }
  15. response = requests.post(url, json=body, stream=True)
  16. response.raise_for_status()
  17. for line in response.iter_lines():
  18. if line.startswith(b"data: "):
  19. try:
  20. json_data = json.loads(line[6:])
  21. if json_data.get("error"):
  22. print(json_data)
  23. break
  24. print(json_data.get("choices", [{"delta": {}}])[0]["delta"].get("content", ""), end="")
  25. except json.JSONDecodeError:
  26. pass
  27. print()
  28. print()
  29. print()
  30. body = {
  31. "model": "",
  32. "provider": "Copilot",
  33. "stream": True,
  34. "messages": [
  35. {"role": "user", "content": "Tell me somethings about my name"}
  36. ],
  37. "conversation_id": conversation_id
  38. }
  39. response = requests.post(url, json=body, stream=True)
  40. response.raise_for_status()
  41. for line in response.iter_lines():
  42. if line.startswith(b"data: "):
  43. try:
  44. json_data = json.loads(line[6:])
  45. if json_data.get("error"):
  46. print(json_data)
  47. break
  48. print(json_data.get("choices", [{"delta": {}}])[0]["delta"].get("content", ""), end="")
  49. except json.JSONDecodeError:
  50. pass