File size: 1,108 Bytes
75ddbd7
 
 
 
 
 
 
949c5db
75ddbd7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests
import json

url = "http://127.0.0.1:8000/chat"

payload = {
    "model": "your-model-name",
    "message": "Create a json object of top 10 anime! answer in json only!",
    "messages": []
}

headers = {
    "Content-Type": "application/json"
}

# Send streaming POST request
with requests.post(url, data=json.dumps(payload), headers=headers, stream=True) as response:
    if response.status_code == 200:
        for line in response.iter_lines(decode_unicode=True):
            if line and line.startswith('data: '):
                try:
                    # Remove 'data: ' prefix and parse JSON
                    json_data = json.loads(line[6:])
                    # Extract text from choices if available
                    if json_data.get('choices') and len(json_data['choices']) > 0:
                        text = json_data['choices'][0].get('text', '')
                        if text:
                            print(text, end='')
                except json.JSONDecodeError:
                    continue
    else:
        print("Error:", response.status_code, response.text)