mirror of
https://github.com/xtekky/gpt4free.git
synced 2025-12-06 02:30:41 -08:00
55 lines
1.6 KiB
Python
Executable file
55 lines
1.6 KiB
Python
Executable file
import requests
|
|
import json
|
|
import uuid
|
|
|
|
url = "http://localhost:1337/v1/chat/completions"
|
|
conversation_id = str(uuid.uuid4())
|
|
body = {
|
|
"model": "",
|
|
"provider": "Copilot",
|
|
"stream": True,
|
|
"messages": [
|
|
{"role": "user", "content": "Hello, i am Heiner. How are you?"}
|
|
],
|
|
"conversation_id": conversation_id
|
|
}
|
|
response = requests.post(url, json=body, stream=True)
|
|
response.raise_for_status()
|
|
for line in response.iter_lines():
|
|
if line.startswith(b"data: "):
|
|
try:
|
|
json_data = json.loads(line[6:])
|
|
if json_data.get("error"):
|
|
print(json_data)
|
|
break
|
|
content = json_data.get("choices", [{"delta": {}}])[0]["delta"].get("content", "")
|
|
if content:
|
|
print(content, end="")
|
|
except json.JSONDecodeError:
|
|
pass
|
|
print()
|
|
print()
|
|
print()
|
|
body = {
|
|
"model": "",
|
|
"provider": "Copilot",
|
|
"stream": True,
|
|
"messages": [
|
|
{"role": "user", "content": "Tell me somethings about my name"}
|
|
],
|
|
"conversation_id": conversation_id
|
|
}
|
|
response = requests.post(url, json=body, stream=True)
|
|
response.raise_for_status()
|
|
for line in response.iter_lines():
|
|
if line.startswith(b"data: "):
|
|
try:
|
|
json_data = json.loads(line[6:])
|
|
if json_data.get("error"):
|
|
print(json_data)
|
|
break
|
|
content = json_data.get("choices", [{"delta": {}}])[0]["delta"].get("content", "")
|
|
if content:
|
|
print(content, end="")
|
|
except json.JSONDecodeError:
|
|
pass
|