-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllm.py
68 lines (62 loc) · 2.2 KB
/
llm.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import config
import os
import json
import requests
from typing import List
from datetime import datetime
class LLM():
def __init__(self,
base_address = config.BASE_ADDRESS,
) -> None:
self.url = base_address
def run(self, prompt):
try:
payload = {
"prompt": prompt,
"max_length": 8192, # This is truely max length of chatglm
"top_p": 0.9,
"temperature": 0.7
}
response = requests.post(url=self.url, json=payload)
if response.status_code == 200:
return json.loads(response.content)
else:
return {
"response": "抱歉,服务器出现了点问题。",
"history": [],
"status": 500,
"time": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
}
except Exception as e:
return {
"response": f"发生了一个错误:{str(e)}",
"history": [],
"status": 500,
"time": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
}
def run_with_history(self, prompt: str, history: List):
try:
payload = {
"prompt": prompt,
"history": history,
"max_length": 8192, # This is truely max length of chatglm
"top_p": 0.9,
"temperature": 0.7
}
response = requests.post(url=self.url, json=payload)
if response.status_code == 200:
return json.loads(response.content)
else:
return {
"response": "抱歉,服务器出现了点问题。",
"history": [],
"status": 500,
"time": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
}
except Exception as e:
return {
"response": f"发生了一个错误:{str(e)}",
"history": [],
"status": 500,
"time": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
}