-
Notifications
You must be signed in to change notification settings - Fork 5
/
mlxClient.py
34 lines (28 loc) · 1.15 KB
/
mlxClient.py
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
import requests
import json
class MlxClient():
def __init__(self):
self.messages = []
self.llmHost = 'http://127.0.0.1:5000/serve'
def clear_history(self):
self.messages.clear()
def append_history(self, message):
self.messages.append(message)
def chat(self, prompt:str, model: str, temp: float, system:str = 'default') -> str:
message = {}
message['role'] = 'user'
message['content'] = prompt
self.messages.append(message)
data = {'model': model, 'prompt': prompt, 'temp' : temp}
try:
response = requests.post(self.llmHost, data=json.dumps(data), headers={'Content-Type': 'application/json'})
print(f'response code {response.status_code}')
if response.status_code != 200:
raise requests.ConnectionError
except Exception as e:
raise requests.ConnectionError
ai_message = dict({'role' : 'assistant', 'content' : response.text})
self.messages.append(ai_message)
return response.text
def chat_stream(self, prompt:str, model: str, temp: float) -> str:
pass