forked from xszyou/Fay
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1、 更换gpt对接方式; 2、增加chatglm2对接。
- Loading branch information
Showing
6 changed files
with
118 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import json | ||
import requests | ||
from core.content_db import Content_Db | ||
content_db = Content_Db() | ||
list = content_db.get_list('all','desc',10) | ||
answer_info = dict() | ||
chat_list = [] | ||
for val in list: | ||
answer_info = dict() | ||
if val[0] == "member": | ||
answer_info["role"] = "user" | ||
answer_info["content"] = val[2] | ||
elif val[0] == "fay": | ||
answer_info["role"] = "bot" | ||
answer_info["content"] = val[2] | ||
chat_list.append(answer_info) | ||
|
||
def question(cont): | ||
print(chat_list) | ||
content = { | ||
"prompt":"请简单回复我。" + cont, | ||
"history":chat_list} | ||
url = "http://192.168.1.23:8000" | ||
req = json.dumps(content) | ||
headers = {'content-type': 'application/json'} | ||
r = requests.post(url, headers=headers, data=req) | ||
res = json.loads(r.text).get('response') | ||
return res | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,78 @@ | ||
from revChatGPT.V3 import Chatbot | ||
""" | ||
此代码由fay开源开发者社区 江湖墨明 提供 | ||
通过此代码的修改,可以实现对接本地clash代理或远程代理,clash无需设置成系统代理。以解决在开系统代理后无法使用部分功能的问题 | ||
""" | ||
|
||
import requests | ||
import time | ||
|
||
from utils import config_util as cfg | ||
import time | ||
from urllib3.exceptions import InsecureRequestWarning | ||
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning) | ||
|
||
#代理服务器的配置方式,参考链接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' | ||
|
||
count = 0 | ||
def question(cont): | ||
global count | ||
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字内。" | ||
|
||
message=[ | ||
{"role": "system", "content": prompt}, | ||
{"role": "user", "content": cont} | ||
] | ||
|
||
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: | ||
if cfg.proxy_config != '' : | ||
chatbot = Chatbot(proxy = cfg.proxy_config, api_key = cfg.key_chatgpt_api_key) | ||
else: | ||
chatbot = Chatbot(api_key = cfg.key_chatgpt_api_key) | ||
response = chatbot.ask(cont) | ||
count = 0 | ||
return response | ||
except Exception as e: | ||
count += 1 | ||
if count < 3: | ||
time.sleep(15) | ||
return question(cont) | ||
return 'gpt当前繁忙,请稍后重试' + e | ||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters