livetalking/llm/VllmGPT.py

82 lines
2.7 KiB
Python
Raw Permalink Normal View History

2024-04-03 18:25:03 +08:00
import json
import requests
# from core import content_db
2024-04-03 19:39:16 +08:00
class VllmGPT:
2024-04-03 18:25:03 +08:00
2024-04-03 20:33:40 +08:00
def __init__(self, host="192.168.1.3",
port="8101",
2024-04-03 19:39:16 +08:00
model="THUDM/chatglm3-6b",
max_tokens="1024"):
self.host = host
self.port = port
self.model=model
self.max_tokens=max_tokens
self.__URL = "http://{}:{}/v1/completions".format(self.host, self.port)
self.__URL2 = "http://{}:{}/v1/chat/completions".format(self.host, self.port)
2024-04-03 18:25:03 +08:00
2024-08-03 08:26:17 +08:00
def chat(self,cont):
2024-04-03 19:39:16 +08:00
chat_list = []
# contentdb = content_db.new_instance()
# list = contentdb.get_list('all','desc',11)
# answer_info = dict()
# chat_list = []
# i = len(list)-1
# while i >= 0:
# answer_info = dict()
# if list[i][0] == "member":
# answer_info["role"] = "user"
# answer_info["content"] = list[i][2]
# elif list[i][0] == "fay":
# answer_info["role"] = "bot"
# answer_info["content"] = list[i][2]
# chat_list.append(answer_info)
# i -= 1
content = {
"model": self.model,
"prompt":"请简单回复我。" + cont,
"history":chat_list}
url = self.__URL
req = json.dumps(content)
headers = {'content-type': 'application/json'}
r = requests.post(url, headers=headers, data=req)
res = json.loads(r.text)
return res['choices'][0]['text']
def question2(self,cont):
chat_list = []
# contentdb = content_db.new_instance()
# list = contentdb.get_list('all','desc',11)
# answer_info = dict()
# chat_list = []
# i = len(list)-1
# while i >= 0:
# answer_info = dict()
# if list[i][0] == "member":
# answer_info["role"] = "user"
# answer_info["content"] = list[i][2]
# elif list[i][0] == "fay":
# answer_info["role"] = "bot"
# answer_info["content"] = list[i][2]
# chat_list.append(answer_info)
# i -= 1
content = {
"model": self.model,
"prompt":"请简单回复我。" + cont,
"history":chat_list}
url = self.__URL2
req = json.dumps(content)
headers = {'content-type': 'application/json'}
r = requests.post(url, headers=headers, data=req)
res = json.loads(r.text)
return res['choices'][0]['message']['content']
2024-04-03 18:25:03 +08:00
if __name__ == "__main__":
2024-04-03 19:39:16 +08:00
vllm = VllmGPT('192.168.1.3','8101')
2024-08-03 08:26:17 +08:00
req = vllm.chat("你叫什么名字啊今年多大了")
2024-04-03 19:39:16 +08:00
print(req)