|
|
@ -1,48 +1,66 @@
|
|
|
|
|
|
|
|
import re
|
|
|
|
import time
|
|
|
|
import time
|
|
|
|
import os
|
|
|
|
import requests
|
|
|
|
|
|
|
|
import json
|
|
|
|
from basereal import BaseReal
|
|
|
|
from basereal import BaseReal
|
|
|
|
from logger import logger
|
|
|
|
from logger import logger
|
|
|
|
|
|
|
|
|
|
|
|
def llm_response(message,nerfreal:BaseReal):
|
|
|
|
def llm_response(message: str, nerfreal: BaseReal):
|
|
|
|
start = time.perf_counter()
|
|
|
|
start = time.perf_counter()
|
|
|
|
from openai import OpenAI
|
|
|
|
logger.info(f"message:{message}")
|
|
|
|
client = OpenAI(
|
|
|
|
url = "http://192.168.10.70:11434/api/chat"
|
|
|
|
# 如果您没有配置环境变量,请在此处用您的API Key进行替换
|
|
|
|
data = {
|
|
|
|
api_key=os.getenv("DASHSCOPE_API_KEY"),
|
|
|
|
"model": "qwen3:30b-a3b",
|
|
|
|
# 填写DashScope SDK的base_url
|
|
|
|
"messages": [
|
|
|
|
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
|
|
|
|
{"role": "system", "content": "你叫小苏,贵州人,带有轻微贵州口音,现在正在直播,你的人设是带货主播,你现在卖的产品是酱酒,任务是消除消费者对假酒的顾虑。"},
|
|
|
|
)
|
|
|
|
{"role": "user", "content": f"{message}\n不可以输出表情包/no_think"}
|
|
|
|
end = time.perf_counter()
|
|
|
|
],
|
|
|
|
logger.info(f"llm Time init: {end-start}s")
|
|
|
|
"stream": True
|
|
|
|
completion = client.chat.completions.create(
|
|
|
|
}
|
|
|
|
model="qwen-plus",
|
|
|
|
|
|
|
|
messages=[{'role': 'system', 'content': 'You are a helpful assistant.'},
|
|
|
|
response = requests.post(url, json=data, stream=True)
|
|
|
|
{'role': 'user', 'content': message}],
|
|
|
|
logger.info(f"Ollama init time: {time.perf_counter() - start:.2f}s")
|
|
|
|
stream=True,
|
|
|
|
|
|
|
|
# 通过以下设置,在流式输出的最后一行展示token使用信息
|
|
|
|
|
|
|
|
stream_options={"include_usage": True}
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
result = ""
|
|
|
|
result = ""
|
|
|
|
|
|
|
|
full_answer = ""
|
|
|
|
first = True
|
|
|
|
first = True
|
|
|
|
for chunk in completion:
|
|
|
|
think_tag_pattern = re.compile(r"</?think>", re.IGNORECASE)
|
|
|
|
if len(chunk.choices)>0:
|
|
|
|
punctuation = ",.!;:,。!?:;"
|
|
|
|
#print(chunk.choices[0].delta.content)
|
|
|
|
|
|
|
|
|
|
|
|
for line in response.iter_lines():
|
|
|
|
|
|
|
|
if not line:
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
json_data = json.loads(line.decode("utf-8"))
|
|
|
|
|
|
|
|
except json.JSONDecodeError:
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 如果有结束标志,可以在此处判断并 break
|
|
|
|
|
|
|
|
# if json_data.get("done"):
|
|
|
|
|
|
|
|
# break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
msg = json_data.get("message", {}).get("content", "")
|
|
|
|
|
|
|
|
msg = think_tag_pattern.sub("", msg)
|
|
|
|
|
|
|
|
if not msg:
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
full_answer += msg
|
|
|
|
|
|
|
|
|
|
|
|
if first:
|
|
|
|
if first:
|
|
|
|
end = time.perf_counter()
|
|
|
|
logger.info(f"Ollama time to first chunk: {time.perf_counter() - start:.2f}s")
|
|
|
|
logger.info(f"llm Time to first chunk: {end-start}s")
|
|
|
|
|
|
|
|
first = False
|
|
|
|
first = False
|
|
|
|
msg = chunk.choices[0].delta.content
|
|
|
|
|
|
|
|
lastpos = 0
|
|
|
|
lastpos = 0
|
|
|
|
#msglist = re.split('[,.!;:,。!?]',msg)
|
|
|
|
|
|
|
|
for i, char in enumerate(msg):
|
|
|
|
for i, char in enumerate(msg):
|
|
|
|
if char in ",.!;:,。!?:;" :
|
|
|
|
if char in punctuation:
|
|
|
|
result = result+msg[lastpos:i+1]
|
|
|
|
result += msg[lastpos:i+1]
|
|
|
|
lastpos = i+1
|
|
|
|
lastpos = i+1
|
|
|
|
if len(result) > 10:
|
|
|
|
if len(result) > 10:
|
|
|
|
logger.info(result)
|
|
|
|
logger.info(result)
|
|
|
|
nerfreal.put_msg_txt(result)
|
|
|
|
nerfreal.put_msg_txt(result)
|
|
|
|
result = ""
|
|
|
|
result = ""
|
|
|
|
result = result+msg[lastpos:]
|
|
|
|
result += msg[lastpos:]
|
|
|
|
end = time.perf_counter()
|
|
|
|
|
|
|
|
logger.info(f"llm Time to last chunk: {end-start}s")
|
|
|
|
if result:
|
|
|
|
nerfreal.put_msg_txt(result)
|
|
|
|
nerfreal.put_msg_txt(result)
|