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