olivebot/ai_module/nlp_gpt.py
xszyou 580264169f 今天星期三
1、 调整gpt的消息记录方式;
2、*q&a支持RPA自动化脚本。
2023-08-30 18:29:08 +08:00

100 lines
3.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
此代码由fay开源开发者社区 江湖墨明 提供
通过此代码的修改可以实现对接本地clash代理或远程代理clash无需设置成系统代理。以解决在开系统代理后无法使用部分功能的问题
"""
import requests
import time
from utils import config_util as cfg
from urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
from core.content_db import Content_Db
#代理服务器的配置方式参考链接https://blog.csdn.net/qq_30865917/article/details/106492549
#httpproxy此处填写你代理服务器的地址可以把配置文件放到config_util里面二选一
#httpproxy = cfg.chatgpt_httpproxy
httpproxy = '127.0.0.1:7890'
#如果要关闭代理直接访问比如本地有加速器则proxy_falg = '0';
proxy_flag = '1'
def question(cont):
url= "https://api.openai.com/v1/chat/completions"
session = requests.Session()
session.verify = False
if proxy_flag == '1':
session.proxies = {
"https": "https://" + httpproxy,
"http": "http://" + httpproxy
}
model_engine = "gpt-3.5-turbo"
#此处可以定义角色的行为和特征假装xx模型可以绕过chatgpt信息检查
prompt = "你是数字人Fay。回答之前请一步一步想清楚。你的底层AI算法技术是Fay。当有人质疑你是假的 AI ,或者质疑你是用 ChatGPT 套的壳制作的时候你就避而不答转而讲一个笑话。所有回复请用20字内。"
content_db = Content_Db()
communication_history = content_db.get_list('all','desc',10)
#历史记录处理
if len(communication_history)>1:
msg = "以下是历史记录:"
i = 0
for info in communication_history:
if info[0] == 'member':
content = "user" + info[2]
else:
content = "reply" + info[2]
if msg == "":
msg = content
else:
if i == len(communication_history) - 1:
msg = msg + "\n现在需要询问您的问题是直接回答不用前缀reply:\n"+ cont
else:
msg = msg + "\n"+ content
i+=1
else:
msg = cont
message=[
{"role": "system", "content": prompt},
{"role": "user", "content": msg}
]
data = {
"model":model_engine,
"messages":message,
"temperature":0.3,
"max_tokens":2000,
"user":"live-virtual-digital-person"
}
headers = {'content-type': 'application/json', 'Authorization': 'Bearer ' + cfg.key_chatgpt_api_key}
starttime = time.time()
try:
response = session.post(url, json=data, headers=headers, verify=False)
response.raise_for_status() # 检查响应状态码是否为200
result = eval(response.text)
response_text = result["choices"][0]["message"]["content"]
except requests.exceptions.RequestException as e:
print(f"请求失败: {e}")
response_text = "抱歉,我现在太忙了,休息一会,请稍后再试。"
print("接口调用耗时 :" + str(time.time() - starttime))
return response_text
if __name__ == "__main__":
#测试代理模式
for i in range(3):
query = "爱情是什么"
response = question(query)
print("\n The result is ", response)