Spaces:
Runtime error
Runtime error
update
Browse files- colorful.py +91 -0
- config.py +3 -0
- functional_crazy.py +15 -24
- main.py +1 -1
- predict.py +66 -116
colorful.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import platform
|
2 |
+
from sys import stdout
|
3 |
+
|
4 |
+
if platform.system()=="Linux":
|
5 |
+
pass
|
6 |
+
else:
|
7 |
+
from colorama import init
|
8 |
+
init()
|
9 |
+
|
10 |
+
# Do you like the elegance of Chinese characters?
|
11 |
+
def print红(*kw,**kargs):
|
12 |
+
print("\033[0;31m",*kw,"\033[0m",**kargs)
|
13 |
+
def print绿(*kw,**kargs):
|
14 |
+
print("\033[0;32m",*kw,"\033[0m",**kargs)
|
15 |
+
def print黄(*kw,**kargs):
|
16 |
+
print("\033[0;33m",*kw,"\033[0m",**kargs)
|
17 |
+
def print蓝(*kw,**kargs):
|
18 |
+
print("\033[0;34m",*kw,"\033[0m",**kargs)
|
19 |
+
def print紫(*kw,**kargs):
|
20 |
+
print("\033[0;35m",*kw,"\033[0m",**kargs)
|
21 |
+
def print靛(*kw,**kargs):
|
22 |
+
print("\033[0;36m",*kw,"\033[0m",**kargs)
|
23 |
+
|
24 |
+
def print亮红(*kw,**kargs):
|
25 |
+
print("\033[1;31m",*kw,"\033[0m",**kargs)
|
26 |
+
def print亮绿(*kw,**kargs):
|
27 |
+
print("\033[1;32m",*kw,"\033[0m",**kargs)
|
28 |
+
def print亮黄(*kw,**kargs):
|
29 |
+
print("\033[1;33m",*kw,"\033[0m",**kargs)
|
30 |
+
def print亮蓝(*kw,**kargs):
|
31 |
+
print("\033[1;34m",*kw,"\033[0m",**kargs)
|
32 |
+
def print亮紫(*kw,**kargs):
|
33 |
+
print("\033[1;35m",*kw,"\033[0m",**kargs)
|
34 |
+
def print亮靛(*kw,**kargs):
|
35 |
+
print("\033[1;36m",*kw,"\033[0m",**kargs)
|
36 |
+
|
37 |
+
|
38 |
+
|
39 |
+
def print亮红(*kw,**kargs):
|
40 |
+
print("\033[1;31m",*kw,"\033[0m",**kargs)
|
41 |
+
def print亮绿(*kw,**kargs):
|
42 |
+
print("\033[1;32m",*kw,"\033[0m",**kargs)
|
43 |
+
def print亮黄(*kw,**kargs):
|
44 |
+
print("\033[1;33m",*kw,"\033[0m",**kargs)
|
45 |
+
def print亮蓝(*kw,**kargs):
|
46 |
+
print("\033[1;34m",*kw,"\033[0m",**kargs)
|
47 |
+
def print亮紫(*kw,**kargs):
|
48 |
+
print("\033[1;35m",*kw,"\033[0m",**kargs)
|
49 |
+
def print亮靛(*kw,**kargs):
|
50 |
+
print("\033[1;36m",*kw,"\033[0m",**kargs)
|
51 |
+
|
52 |
+
print_red = print红
|
53 |
+
print_green = print绿
|
54 |
+
print_yellow = print黄
|
55 |
+
print_blue = print蓝
|
56 |
+
print_purple = print紫
|
57 |
+
print_indigo = print靛
|
58 |
+
|
59 |
+
print_bold_red = print亮红
|
60 |
+
print_bold_green = print亮绿
|
61 |
+
print_bold_yellow = print亮黄
|
62 |
+
print_bold_blue = print亮蓝
|
63 |
+
print_bold_purple = print亮紫
|
64 |
+
print_bold_indigo = print亮靛
|
65 |
+
|
66 |
+
if not stdout.isatty():
|
67 |
+
# redirection, avoid a fucked up log file
|
68 |
+
print红 = print
|
69 |
+
print绿 = print
|
70 |
+
print黄 = print
|
71 |
+
print蓝 = print
|
72 |
+
print紫 = print
|
73 |
+
print靛 = print
|
74 |
+
print亮红 = print
|
75 |
+
print亮绿 = print
|
76 |
+
print亮黄 = print
|
77 |
+
print亮蓝 = print
|
78 |
+
print亮紫 = print
|
79 |
+
print亮靛 = print
|
80 |
+
print_red = print
|
81 |
+
print_green = print
|
82 |
+
print_yellow = print
|
83 |
+
print_blue = print
|
84 |
+
print_purple = print
|
85 |
+
print_indigo = print
|
86 |
+
print_bold_red = print
|
87 |
+
print_bold_green = print
|
88 |
+
print_bold_yellow = print
|
89 |
+
print_bold_blue = print
|
90 |
+
print_bold_purple = print
|
91 |
+
print_bold_indigo = print
|
config.py
CHANGED
@@ -21,6 +21,9 @@ WEB_PORT = -1
|
|
21 |
# 如果OpenAI不响应(网络卡顿、代理失败、KEY失效),重试的次数限制
|
22 |
MAX_RETRY = 2
|
23 |
|
|
|
|
|
|
|
24 |
# 检查一下是不是忘了改config
|
25 |
if API_KEY == "sk-此处填API秘钥":
|
26 |
assert False, "请在config文件中修改API密钥, 添加海外代理之后再运行"
|
|
|
21 |
# 如果OpenAI不响应(网络卡顿、代理失败、KEY失效),重试的次数限制
|
22 |
MAX_RETRY = 2
|
23 |
|
24 |
+
# 选择的OpenAI模型是(gpt4现在只对申请成功的人开放)
|
25 |
+
LLM_MODEL = "gpt-3.5-turbo"
|
26 |
+
|
27 |
# 检查一下是不是忘了改config
|
28 |
if API_KEY == "sk-此处填API秘钥":
|
29 |
assert False, "请在config文件中修改API密钥, 添加海外代理之后再运行"
|
functional_crazy.py
CHANGED
@@ -1,26 +1,18 @@
|
|
1 |
-
|
2 |
-
# 'primary' for main call-to-action,
|
3 |
-
# 'secondary' for a more subdued style,
|
4 |
-
# 'stop' for a stop button.
|
5 |
-
# """
|
6 |
-
|
7 |
fast_debug = False
|
8 |
|
9 |
-
def
|
10 |
-
import time
|
11 |
-
from predict import predict_no_ui_no_history
|
12 |
for i in range(5):
|
13 |
i_say = f'我给出一个数字,你给出该数字的平方。我给出数字:{i}'
|
14 |
-
gpt_say =
|
15 |
chatbot.append((i_say, gpt_say))
|
16 |
history.append(i_say)
|
17 |
history.append(gpt_say)
|
18 |
yield chatbot, history, '正常'
|
19 |
-
|
20 |
|
21 |
def 解析项目本身(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
|
22 |
import time, glob, os
|
23 |
-
from predict import predict_no_ui
|
24 |
file_manifest = [f for f in glob.glob('*.py')]
|
25 |
|
26 |
for index, fp in enumerate(file_manifest):
|
@@ -30,7 +22,7 @@ def 解析项目本身(txt, top_p, temperature, chatbot, history, systemPromptTx
|
|
30 |
前言 = "接下来请你分析自己的程序构成,别紧张," if index==0 else ""
|
31 |
i_say = 前言 + f'请对下面的程序文件做一个概述文件名是{fp},文件代码是 ```{file_content}```'
|
32 |
i_say_show_user = 前言 + f'[{index}/{len(file_manifest)}] 请对下面的程序文件做一个概述: {os.path.abspath(fp)}'
|
33 |
-
chatbot.append((i_say_show_user, "[
|
34 |
yield chatbot, history, '正常'
|
35 |
|
36 |
if not fast_debug:
|
@@ -43,7 +35,7 @@ def 解析项目本身(txt, top_p, temperature, chatbot, history, systemPromptTx
|
|
43 |
time.sleep(2)
|
44 |
|
45 |
i_say = f'根据以上你自己的分析,对程序的整体功能和构架做出概括。然后用一张markdown表格整理每个文件的功能(包括{file_manifest})。'
|
46 |
-
chatbot.append((i_say, "[
|
47 |
yield chatbot, history, '正常'
|
48 |
|
49 |
if not fast_debug:
|
@@ -64,7 +56,6 @@ def report_execption(chatbot, history, a, b):
|
|
64 |
|
65 |
def 解析源代码(file_manifest, project_folder, top_p, temperature, chatbot, history, systemPromptTxt):
|
66 |
import time, glob, os
|
67 |
-
from predict import predict_no_ui
|
68 |
print('begin analysis on:', file_manifest)
|
69 |
for index, fp in enumerate(file_manifest):
|
70 |
with open(fp, 'r', encoding='utf-8') as f:
|
@@ -73,7 +64,7 @@ def 解析源代码(file_manifest, project_folder, top_p, temperature, chatbot,
|
|
73 |
前言 = "接下来请你逐文件分析下面的工程" if index==0 else ""
|
74 |
i_say = 前言 + f'请对下面的程序文件做一个概述文件名是{os.path.relpath(fp, project_folder)},文件代码是 ```{file_content}```'
|
75 |
i_say_show_user = 前言 + f'[{index}/{len(file_manifest)}] 请对下面的程序文件做一个概述: {os.path.abspath(fp)}'
|
76 |
-
chatbot.append((i_say_show_user, "[
|
77 |
print('[1] yield chatbot, history')
|
78 |
yield chatbot, history, '正常'
|
79 |
|
@@ -98,7 +89,7 @@ def 解析源代码(file_manifest, project_folder, top_p, temperature, chatbot,
|
|
98 |
|
99 |
all_file = ', '.join([os.path.relpath(fp, project_folder) for index, fp in enumerate(file_manifest)])
|
100 |
i_say = f'根据以上你自己的分析,对程序的整体功能和构架做出概括。然后用一张markdown表格整理每个文件的功能(包括{all_file})。'
|
101 |
-
chatbot.append((i_say, "[
|
102 |
yield chatbot, history, '正常'
|
103 |
|
104 |
if not fast_debug:
|
@@ -159,22 +150,22 @@ def 解析一个C项目的头文件(txt, top_p, temperature, chatbot, history, s
|
|
159 |
|
160 |
def get_crazy_functionals():
|
161 |
return {
|
162 |
-
"
|
163 |
-
"Color": "stop", # 按钮颜色
|
164 |
-
"Function": 自我程序解构简单案例
|
165 |
-
},
|
166 |
-
"请解析并解构此项目本身": {
|
167 |
"Color": "stop", # 按钮颜色
|
168 |
"Function": 解析项目本身
|
169 |
},
|
170 |
-
"解析一整个Python项目(输入栏给定项目完整目录)": {
|
171 |
"Color": "stop", # 按钮颜色
|
172 |
"Function": 解析一个Python项目
|
173 |
},
|
174 |
-
"解析一整个C++项目的头文件(输入栏给定项目完整目录)": {
|
175 |
"Color": "stop", # 按钮颜色
|
176 |
"Function": 解析一个C项目的头文件
|
177 |
},
|
|
|
|
|
|
|
|
|
178 |
|
179 |
}
|
180 |
|
|
|
1 |
+
from predict import predict_no_ui
|
|
|
|
|
|
|
|
|
|
|
2 |
fast_debug = False
|
3 |
|
4 |
+
def 高阶功能模板函数(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
|
|
|
|
|
5 |
for i in range(5):
|
6 |
i_say = f'我给出一个数字,你给出该数字的平方。我给出数字:{i}'
|
7 |
+
gpt_say = predict_no_ui(inputs=i_say, top_p=top_p, temperature=temperature)
|
8 |
chatbot.append((i_say, gpt_say))
|
9 |
history.append(i_say)
|
10 |
history.append(gpt_say)
|
11 |
yield chatbot, history, '正常'
|
12 |
+
|
13 |
|
14 |
def 解析项目本身(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
|
15 |
import time, glob, os
|
|
|
16 |
file_manifest = [f for f in glob.glob('*.py')]
|
17 |
|
18 |
for index, fp in enumerate(file_manifest):
|
|
|
22 |
前言 = "接下来请你分析自己的程序构成,别紧张," if index==0 else ""
|
23 |
i_say = 前言 + f'请对下面的程序文件做一个概述文件名是{fp},文件代码是 ```{file_content}```'
|
24 |
i_say_show_user = 前言 + f'[{index}/{len(file_manifest)}] 请对下面的程序文件做一个概述: {os.path.abspath(fp)}'
|
25 |
+
chatbot.append((i_say_show_user, "[waiting gpt response]"))
|
26 |
yield chatbot, history, '正常'
|
27 |
|
28 |
if not fast_debug:
|
|
|
35 |
time.sleep(2)
|
36 |
|
37 |
i_say = f'根据以上你自己的分析,对程序的整体功能和构架做出概括。然后用一张markdown表格整理每个文件的功能(包括{file_manifest})。'
|
38 |
+
chatbot.append((i_say, "[waiting gpt response]"))
|
39 |
yield chatbot, history, '正常'
|
40 |
|
41 |
if not fast_debug:
|
|
|
56 |
|
57 |
def 解析源代码(file_manifest, project_folder, top_p, temperature, chatbot, history, systemPromptTxt):
|
58 |
import time, glob, os
|
|
|
59 |
print('begin analysis on:', file_manifest)
|
60 |
for index, fp in enumerate(file_manifest):
|
61 |
with open(fp, 'r', encoding='utf-8') as f:
|
|
|
64 |
前言 = "接下来请你逐文件分析下面的工程" if index==0 else ""
|
65 |
i_say = 前言 + f'请对下面的程序文件做一个概述文件名是{os.path.relpath(fp, project_folder)},文件代码是 ```{file_content}```'
|
66 |
i_say_show_user = 前言 + f'[{index}/{len(file_manifest)}] 请对下面的程序文件做一个概述: {os.path.abspath(fp)}'
|
67 |
+
chatbot.append((i_say_show_user, "[waiting gpt response]"))
|
68 |
print('[1] yield chatbot, history')
|
69 |
yield chatbot, history, '正常'
|
70 |
|
|
|
89 |
|
90 |
all_file = ', '.join([os.path.relpath(fp, project_folder) for index, fp in enumerate(file_manifest)])
|
91 |
i_say = f'根据以上你自己的分析,对程序的整体功能和构架做出概括。然后用一张markdown表格整理每个文件的功能(包括{all_file})。'
|
92 |
+
chatbot.append((i_say, "[waiting gpt response]"))
|
93 |
yield chatbot, history, '正常'
|
94 |
|
95 |
if not fast_debug:
|
|
|
150 |
|
151 |
def get_crazy_functionals():
|
152 |
return {
|
153 |
+
"[实验功能] 请解析并解构此项目本身": {
|
|
|
|
|
|
|
|
|
154 |
"Color": "stop", # 按钮颜色
|
155 |
"Function": 解析项目本身
|
156 |
},
|
157 |
+
"[实验功能] 解析一整个Python项目(输入栏给定项目完整目录)": {
|
158 |
"Color": "stop", # 按钮颜色
|
159 |
"Function": 解析一个Python项目
|
160 |
},
|
161 |
+
"[实验功能] 解析一整个C++项目的头文件(输入栏给定项目完整目录)": {
|
162 |
"Color": "stop", # 按钮颜色
|
163 |
"Function": 解析一个C项目的头文件
|
164 |
},
|
165 |
+
"[实验功能] 高阶功能模板函数": {
|
166 |
+
"Color": "stop", # 按钮颜色
|
167 |
+
"Function": 高阶功能模板函数
|
168 |
+
},
|
169 |
|
170 |
}
|
171 |
|
main.py
CHANGED
@@ -106,7 +106,7 @@ with gr.Blocks() as demo:
|
|
106 |
# submitBtn.click(reset_textbox, [], [txt])
|
107 |
for k in functional:
|
108 |
functional[k]["Button"].click(predict,
|
109 |
-
[txt, top_p, temperature, chatbot, history, systemPromptTxt,
|
110 |
for k in crazy_functional:
|
111 |
crazy_functional[k]["Button"].click(crazy_functional[k]["Function"],
|
112 |
[txt, top_p, temperature, chatbot, history, systemPromptTxt, gr.State(PORT)], [chatbot, history, statusDisplay])
|
|
|
106 |
# submitBtn.click(reset_textbox, [], [txt])
|
107 |
for k in functional:
|
108 |
functional[k]["Button"].click(predict,
|
109 |
+
[txt, top_p, temperature, chatbot, history, systemPromptTxt, TRUE, gr.State(k)], [chatbot, history, statusDisplay], show_progress=True)
|
110 |
for k in crazy_functional:
|
111 |
crazy_functional[k]["Button"].click(crazy_functional[k]["Function"],
|
112 |
[txt, top_p, temperature, chatbot, history, systemPromptTxt, gr.State(PORT)], [chatbot, history, statusDisplay])
|
predict.py
CHANGED
@@ -6,11 +6,12 @@ import logging
|
|
6 |
import traceback
|
7 |
import requests
|
8 |
import importlib
|
|
|
9 |
|
10 |
# config_private.py放自己的秘密如API和代理网址
|
11 |
# 读取时首先看是否存在私密的config_private配置文件(不受git管控),如果有,则覆盖原config文件
|
12 |
-
try: from config_private import proxies, API_URL, API_KEY, TIMEOUT_SECONDS, MAX_RETRY
|
13 |
-
except: from config import proxies, API_URL, API_KEY, TIMEOUT_SECONDS, MAX_RETRY
|
14 |
|
15 |
timeout_bot_msg = '[local] Request timeout, network error. please check proxy settings in config.py.'
|
16 |
|
@@ -23,51 +24,12 @@ def get_full_error(chunk, stream_response):
|
|
23 |
return chunk
|
24 |
|
25 |
def predict_no_ui(inputs, top_p, temperature, history=[]):
|
26 |
-
|
27 |
-
|
28 |
-
#
|
29 |
-
chat_counter = len(history) // 2
|
30 |
-
if chat_counter > 0:
|
31 |
-
for index in range(0, 2*chat_counter, 2):
|
32 |
-
what_i_have_asked = {}
|
33 |
-
what_i_have_asked["role"] = "user"
|
34 |
-
what_i_have_asked["content"] = history[index]
|
35 |
-
what_gpt_answer = {}
|
36 |
-
what_gpt_answer["role"] = "assistant"
|
37 |
-
what_gpt_answer["content"] = history[index+1]
|
38 |
-
if what_i_have_asked["content"] != "":
|
39 |
-
messages.append(what_i_have_asked)
|
40 |
-
messages.append(what_gpt_answer)
|
41 |
-
else:
|
42 |
-
messages[-1]['content'] = what_gpt_answer['content']
|
43 |
-
|
44 |
-
what_i_ask_now = {}
|
45 |
-
what_i_ask_now["role"] = "user"
|
46 |
-
what_i_ask_now["content"] = inputs
|
47 |
-
messages.append(what_i_ask_now)
|
48 |
-
|
49 |
-
# messages
|
50 |
-
payload = {
|
51 |
-
"model": "gpt-3.5-turbo",
|
52 |
-
# "model": "gpt-4",
|
53 |
-
"messages": messages,
|
54 |
-
"temperature": temperature, # 1.0,
|
55 |
-
"top_p": top_p, # 1.0,
|
56 |
-
"n": 1,
|
57 |
-
"stream": False,
|
58 |
-
"presence_penalty": 0,
|
59 |
-
"frequency_penalty": 0,
|
60 |
-
}
|
61 |
-
|
62 |
-
headers = {
|
63 |
-
"Content-Type": "application/json",
|
64 |
-
"Authorization": f"Bearer {API_KEY}"
|
65 |
-
}
|
66 |
|
67 |
retry = 0
|
68 |
while True:
|
69 |
try:
|
70 |
-
# make a POST request to the API endpoint
|
71 |
response = requests.post(API_URL, headers=headers, proxies=proxies,
|
72 |
json=payload, stream=False, timeout=TIMEOUT_SECONDS*2); break
|
73 |
except TimeoutError as e:
|
@@ -84,9 +46,7 @@ def predict_no_ui(inputs, top_p, temperature, history=[]):
|
|
84 |
raise ConnectionAbortedError("Json解析不合常规,可能是文本过长" + response.text)
|
85 |
|
86 |
|
87 |
-
|
88 |
-
|
89 |
-
def predict(inputs, top_p, temperature, chatbot=[], history=[], system_prompt='', retry=False,
|
90 |
stream = True, additional_fn=None):
|
91 |
|
92 |
if additional_fn is not None:
|
@@ -101,60 +61,13 @@ def predict(inputs, top_p, temperature, chatbot=[], history=[], system_prompt=''
|
|
101 |
chatbot.append((inputs, ""))
|
102 |
yield chatbot, history, "等待响应"
|
103 |
|
104 |
-
headers =
|
105 |
-
|
106 |
-
"Authorization": f"Bearer {API_KEY}"
|
107 |
-
}
|
108 |
-
|
109 |
-
chat_counter = len(history) // 2
|
110 |
-
|
111 |
-
print(f"chat_counter - {chat_counter}")
|
112 |
-
|
113 |
-
messages = [{"role": "system", "content": system_prompt}]
|
114 |
-
if chat_counter:
|
115 |
-
for index in range(0, 2*chat_counter, 2):
|
116 |
-
what_i_have_asked = {}
|
117 |
-
what_i_have_asked["role"] = "user"
|
118 |
-
what_i_have_asked["content"] = history[index]
|
119 |
-
what_gpt_answer = {}
|
120 |
-
what_gpt_answer["role"] = "assistant"
|
121 |
-
what_gpt_answer["content"] = history[index+1]
|
122 |
-
if what_i_have_asked["content"] != "":
|
123 |
-
if not (what_gpt_answer["content"] != "" or retry): continue
|
124 |
-
if what_gpt_answer["content"] == timeout_bot_msg: continue
|
125 |
-
messages.append(what_i_have_asked)
|
126 |
-
messages.append(what_gpt_answer)
|
127 |
-
else:
|
128 |
-
messages[-1]['content'] = what_gpt_answer['content']
|
129 |
-
|
130 |
-
if retry and chat_counter:
|
131 |
-
messages.pop()
|
132 |
-
else:
|
133 |
-
what_i_ask_now = {}
|
134 |
-
what_i_ask_now["role"] = "user"
|
135 |
-
what_i_ask_now["content"] = inputs
|
136 |
-
messages.append(what_i_ask_now)
|
137 |
-
chat_counter += 1
|
138 |
-
|
139 |
-
# messages
|
140 |
-
payload = {
|
141 |
-
"model": "gpt-3.5-turbo",
|
142 |
-
# "model": "gpt-4",
|
143 |
-
"messages": messages,
|
144 |
-
"temperature": temperature, # 1.0,
|
145 |
-
"top_p": top_p, # 1.0,
|
146 |
-
"n": 1,
|
147 |
-
"stream": stream,
|
148 |
-
"presence_penalty": 0,
|
149 |
-
"frequency_penalty": 0,
|
150 |
-
}
|
151 |
-
|
152 |
-
history.append(inputs)
|
153 |
|
154 |
retry = 0
|
155 |
while True:
|
156 |
try:
|
157 |
-
# make a POST request to the API endpoint
|
158 |
response = requests.post(API_URL, headers=headers, proxies=proxies,
|
159 |
json=payload, stream=True, timeout=TIMEOUT_SECONDS);break
|
160 |
except:
|
@@ -164,37 +77,30 @@ def predict(inputs, top_p, temperature, chatbot=[], history=[], system_prompt=''
|
|
164 |
yield chatbot, history, "请求超时"+retry_msg
|
165 |
if retry > MAX_RETRY: raise TimeoutError
|
166 |
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
counter = 0
|
171 |
if stream:
|
172 |
stream_response = response.iter_lines()
|
173 |
while True:
|
174 |
chunk = next(stream_response)
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
counter += 1
|
180 |
-
continue
|
181 |
-
counter += 1
|
182 |
-
# check whether each line is non-empty
|
183 |
if chunk:
|
184 |
-
# decode each line as response data is in bytes
|
185 |
try:
|
186 |
if len(json.loads(chunk.decode()[6:])['choices'][0]["delta"]) == 0:
|
187 |
-
|
|
|
188 |
break
|
|
|
189 |
chunkjson = json.loads(chunk.decode()[6:])
|
190 |
status_text = f"finish_reason: {chunkjson['choices'][0]['finish_reason']}"
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
else:
|
195 |
-
history[-1] = partial_words
|
196 |
chatbot[-1] = (history[-2], history[-1])
|
197 |
-
token_counter += 1
|
198 |
yield chatbot, history, status_text
|
199 |
|
200 |
except Exception as e:
|
@@ -207,4 +113,48 @@ def predict(inputs, top_p, temperature, chatbot=[], history=[], system_prompt=''
|
|
207 |
yield chatbot, history, "Json解析不合常规,很可能是文本过长" + error_msg
|
208 |
return
|
209 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
210 |
|
|
|
6 |
import traceback
|
7 |
import requests
|
8 |
import importlib
|
9 |
+
from colorful import *
|
10 |
|
11 |
# config_private.py放自己的秘密如API和代理网址
|
12 |
# 读取时首先看是否存在私密的config_private配置文件(不受git管控),如果有,则覆盖原config文件
|
13 |
+
try: from config_private import proxies, API_URL, API_KEY, TIMEOUT_SECONDS, MAX_RETRY, LLM_MODEL
|
14 |
+
except: from config import proxies, API_URL, API_KEY, TIMEOUT_SECONDS, MAX_RETRY, LLM_MODEL
|
15 |
|
16 |
timeout_bot_msg = '[local] Request timeout, network error. please check proxy settings in config.py.'
|
17 |
|
|
|
24 |
return chunk
|
25 |
|
26 |
def predict_no_ui(inputs, top_p, temperature, history=[]):
|
27 |
+
headers, payload = generate_payload(inputs, top_p, temperature, history, system_prompt="", stream=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
retry = 0
|
30 |
while True:
|
31 |
try:
|
32 |
+
# make a POST request to the API endpoint, stream=False
|
33 |
response = requests.post(API_URL, headers=headers, proxies=proxies,
|
34 |
json=payload, stream=False, timeout=TIMEOUT_SECONDS*2); break
|
35 |
except TimeoutError as e:
|
|
|
46 |
raise ConnectionAbortedError("Json解析不合常规,可能是文本过长" + response.text)
|
47 |
|
48 |
|
49 |
+
def predict(inputs, top_p, temperature, chatbot=[], history=[], system_prompt='',
|
|
|
|
|
50 |
stream = True, additional_fn=None):
|
51 |
|
52 |
if additional_fn is not None:
|
|
|
61 |
chatbot.append((inputs, ""))
|
62 |
yield chatbot, history, "等待响应"
|
63 |
|
64 |
+
headers, payload = generate_payload(inputs, top_p, temperature, history, system_prompt, stream)
|
65 |
+
history.append(inputs); history.append(" ")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
retry = 0
|
68 |
while True:
|
69 |
try:
|
70 |
+
# make a POST request to the API endpoint, stream=True
|
71 |
response = requests.post(API_URL, headers=headers, proxies=proxies,
|
72 |
json=payload, stream=True, timeout=TIMEOUT_SECONDS);break
|
73 |
except:
|
|
|
77 |
yield chatbot, history, "请求超时"+retry_msg
|
78 |
if retry > MAX_RETRY: raise TimeoutError
|
79 |
|
80 |
+
gpt_replying_buffer = ""
|
81 |
+
|
82 |
+
is_head_of_the_stream = True
|
|
|
83 |
if stream:
|
84 |
stream_response = response.iter_lines()
|
85 |
while True:
|
86 |
chunk = next(stream_response)
|
87 |
+
# print(chunk.decode()[6:])
|
88 |
+
if is_head_of_the_stream:
|
89 |
+
is_head_of_the_stream = False; continue
|
90 |
+
|
|
|
|
|
|
|
|
|
91 |
if chunk:
|
|
|
92 |
try:
|
93 |
if len(json.loads(chunk.decode()[6:])['choices'][0]["delta"]) == 0:
|
94 |
+
# 判定为数据流的结束,gpt_replying_buffer也写完了
|
95 |
+
logging.info(f'[response] {gpt_replying_buffer}')
|
96 |
break
|
97 |
+
# 处理数据流的主体
|
98 |
chunkjson = json.loads(chunk.decode()[6:])
|
99 |
status_text = f"finish_reason: {chunkjson['choices'][0]['finish_reason']}"
|
100 |
+
# 如果这里抛出异常,一般是文本过长,详情见get_full_error的输出
|
101 |
+
gpt_replying_buffer = gpt_replying_buffer + json.loads(chunk.decode()[6:])['choices'][0]["delta"]["content"]
|
102 |
+
history[-1] = gpt_replying_buffer
|
|
|
|
|
103 |
chatbot[-1] = (history[-2], history[-1])
|
|
|
104 |
yield chatbot, history, status_text
|
105 |
|
106 |
except Exception as e:
|
|
|
113 |
yield chatbot, history, "Json解析不合常规,很可能是文本过长" + error_msg
|
114 |
return
|
115 |
|
116 |
+
def generate_payload(inputs, top_p, temperature, history, system_prompt, stream):
|
117 |
+
headers = {
|
118 |
+
"Content-Type": "application/json",
|
119 |
+
"Authorization": f"Bearer {API_KEY}"
|
120 |
+
}
|
121 |
+
|
122 |
+
conversation_cnt = len(history) // 2
|
123 |
+
|
124 |
+
messages = [{"role": "system", "content": system_prompt}]
|
125 |
+
if conversation_cnt:
|
126 |
+
for index in range(0, 2*conversation_cnt, 2):
|
127 |
+
what_i_have_asked = {}
|
128 |
+
what_i_have_asked["role"] = "user"
|
129 |
+
what_i_have_asked["content"] = history[index]
|
130 |
+
what_gpt_answer = {}
|
131 |
+
what_gpt_answer["role"] = "assistant"
|
132 |
+
what_gpt_answer["content"] = history[index+1]
|
133 |
+
if what_i_have_asked["content"] != "":
|
134 |
+
if what_gpt_answer["content"] == "": continue
|
135 |
+
if what_gpt_answer["content"] == timeout_bot_msg: continue
|
136 |
+
messages.append(what_i_have_asked)
|
137 |
+
messages.append(what_gpt_answer)
|
138 |
+
else:
|
139 |
+
messages[-1]['content'] = what_gpt_answer['content']
|
140 |
+
|
141 |
+
what_i_ask_now = {}
|
142 |
+
what_i_ask_now["role"] = "user"
|
143 |
+
what_i_ask_now["content"] = inputs
|
144 |
+
messages.append(what_i_ask_now)
|
145 |
+
|
146 |
+
payload = {
|
147 |
+
"model": LLM_MODEL,
|
148 |
+
"messages": messages,
|
149 |
+
"temperature": temperature, # 1.0,
|
150 |
+
"top_p": top_p, # 1.0,
|
151 |
+
"n": 1,
|
152 |
+
"stream": stream,
|
153 |
+
"presence_penalty": 0,
|
154 |
+
"frequency_penalty": 0,
|
155 |
+
}
|
156 |
+
|
157 |
+
print(f" {LLM_MODEL} : {conversation_cnt} : {inputs}")
|
158 |
+
return headers,payload
|
159 |
+
|
160 |
|