File size: 1,062 Bytes
f29d474
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import os
import requests


base_url = os.getenv("OPENAI_BASE_URL", "http://localhost:8000/v1")
api_key = os.getenv("OPENAI_API_KEY", "dummy")


def chat_stream():
    url = f"{base_url}/chat/completions"
    payload = {
        "model": "mk-llm",
        "messages": [
            {"role": "system", "content": "Ти си помошник кој зборува на македонски."},
            {"role": "user", "content": "Која е историјата на Охрид?"},
        ],
        "stream": True,
    }
    headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
    with requests.post(url, headers=headers, json=payload, stream=True) as r:
        for line in r.iter_lines():
            if not line:
                continue
            if line.startswith(b"data: "):
                chunk = line[len(b"data: "):]
                if chunk == b"[DONE]":
                    break
                print(chunk.decode("utf-8"))


if __name__ == "__main__":
    chat_stream()