update
This commit is contained in:
		
							parent
							
								
									ecdeda8e92
								
							
						
					
					
						commit
						44b40ff726
					
				
							
								
								
									
										91
									
								
								colorful.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										91
									
								
								colorful.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,91 @@
 | 
				
			|||||||
 | 
					import platform
 | 
				
			||||||
 | 
					from sys import stdout
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if platform.system()=="Linux":
 | 
				
			||||||
 | 
					    pass
 | 
				
			||||||
 | 
					else: 
 | 
				
			||||||
 | 
					    from colorama import init
 | 
				
			||||||
 | 
					    init()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Do you like the elegance of Chinese characters?
 | 
				
			||||||
 | 
					def print红(*kw,**kargs):
 | 
				
			||||||
 | 
					    print("\033[0;31m",*kw,"\033[0m",**kargs)
 | 
				
			||||||
 | 
					def print绿(*kw,**kargs):
 | 
				
			||||||
 | 
					    print("\033[0;32m",*kw,"\033[0m",**kargs)
 | 
				
			||||||
 | 
					def print黄(*kw,**kargs):
 | 
				
			||||||
 | 
					    print("\033[0;33m",*kw,"\033[0m",**kargs)
 | 
				
			||||||
 | 
					def print蓝(*kw,**kargs):
 | 
				
			||||||
 | 
					    print("\033[0;34m",*kw,"\033[0m",**kargs)
 | 
				
			||||||
 | 
					def print紫(*kw,**kargs):
 | 
				
			||||||
 | 
					    print("\033[0;35m",*kw,"\033[0m",**kargs)
 | 
				
			||||||
 | 
					def print靛(*kw,**kargs):
 | 
				
			||||||
 | 
					    print("\033[0;36m",*kw,"\033[0m",**kargs)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def print亮红(*kw,**kargs):
 | 
				
			||||||
 | 
					    print("\033[1;31m",*kw,"\033[0m",**kargs)
 | 
				
			||||||
 | 
					def print亮绿(*kw,**kargs):
 | 
				
			||||||
 | 
					    print("\033[1;32m",*kw,"\033[0m",**kargs)
 | 
				
			||||||
 | 
					def print亮黄(*kw,**kargs):
 | 
				
			||||||
 | 
					    print("\033[1;33m",*kw,"\033[0m",**kargs)
 | 
				
			||||||
 | 
					def print亮蓝(*kw,**kargs):
 | 
				
			||||||
 | 
					    print("\033[1;34m",*kw,"\033[0m",**kargs)
 | 
				
			||||||
 | 
					def print亮紫(*kw,**kargs):
 | 
				
			||||||
 | 
					    print("\033[1;35m",*kw,"\033[0m",**kargs)
 | 
				
			||||||
 | 
					def print亮靛(*kw,**kargs):
 | 
				
			||||||
 | 
					    print("\033[1;36m",*kw,"\033[0m",**kargs)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def print亮红(*kw,**kargs):
 | 
				
			||||||
 | 
					    print("\033[1;31m",*kw,"\033[0m",**kargs)
 | 
				
			||||||
 | 
					def print亮绿(*kw,**kargs):
 | 
				
			||||||
 | 
					    print("\033[1;32m",*kw,"\033[0m",**kargs)
 | 
				
			||||||
 | 
					def print亮黄(*kw,**kargs):
 | 
				
			||||||
 | 
					    print("\033[1;33m",*kw,"\033[0m",**kargs)
 | 
				
			||||||
 | 
					def print亮蓝(*kw,**kargs):
 | 
				
			||||||
 | 
					    print("\033[1;34m",*kw,"\033[0m",**kargs)
 | 
				
			||||||
 | 
					def print亮紫(*kw,**kargs):
 | 
				
			||||||
 | 
					    print("\033[1;35m",*kw,"\033[0m",**kargs)
 | 
				
			||||||
 | 
					def print亮靛(*kw,**kargs):
 | 
				
			||||||
 | 
					    print("\033[1;36m",*kw,"\033[0m",**kargs)
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					print_red = print红
 | 
				
			||||||
 | 
					print_green = print绿
 | 
				
			||||||
 | 
					print_yellow = print黄
 | 
				
			||||||
 | 
					print_blue = print蓝
 | 
				
			||||||
 | 
					print_purple = print紫
 | 
				
			||||||
 | 
					print_indigo = print靛
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					print_bold_red = print亮红
 | 
				
			||||||
 | 
					print_bold_green = print亮绿
 | 
				
			||||||
 | 
					print_bold_yellow = print亮黄
 | 
				
			||||||
 | 
					print_bold_blue = print亮蓝
 | 
				
			||||||
 | 
					print_bold_purple = print亮紫
 | 
				
			||||||
 | 
					print_bold_indigo = print亮靛
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					if not stdout.isatty():
 | 
				
			||||||
 | 
					    # redirection, avoid a fucked up log file
 | 
				
			||||||
 | 
					    print红 = print
 | 
				
			||||||
 | 
					    print绿 = print
 | 
				
			||||||
 | 
					    print黄 = print
 | 
				
			||||||
 | 
					    print蓝 = print
 | 
				
			||||||
 | 
					    print紫 = print
 | 
				
			||||||
 | 
					    print靛 = print
 | 
				
			||||||
 | 
					    print亮红 = print
 | 
				
			||||||
 | 
					    print亮绿 = print
 | 
				
			||||||
 | 
					    print亮黄 = print
 | 
				
			||||||
 | 
					    print亮蓝 = print
 | 
				
			||||||
 | 
					    print亮紫 = print
 | 
				
			||||||
 | 
					    print亮靛 = print
 | 
				
			||||||
 | 
					    print_red = print
 | 
				
			||||||
 | 
					    print_green = print
 | 
				
			||||||
 | 
					    print_yellow = print
 | 
				
			||||||
 | 
					    print_blue = print
 | 
				
			||||||
 | 
					    print_purple = print
 | 
				
			||||||
 | 
					    print_indigo = print
 | 
				
			||||||
 | 
					    print_bold_red = print
 | 
				
			||||||
 | 
					    print_bold_green = print
 | 
				
			||||||
 | 
					    print_bold_yellow = print
 | 
				
			||||||
 | 
					    print_bold_blue = print
 | 
				
			||||||
 | 
					    print_bold_purple = print
 | 
				
			||||||
 | 
					    print_bold_indigo = print
 | 
				
			||||||
@ -21,6 +21,9 @@ WEB_PORT = -1
 | 
				
			|||||||
# 如果OpenAI不响应(网络卡顿、代理失败、KEY失效),重试的次数限制
 | 
					# 如果OpenAI不响应(网络卡顿、代理失败、KEY失效),重试的次数限制
 | 
				
			||||||
MAX_RETRY = 2
 | 
					MAX_RETRY = 2
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# 选择的OpenAI模型是(gpt4现在只对申请成功的人开放)
 | 
				
			||||||
 | 
					LLM_MODEL = "gpt-3.5-turbo"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# 检查一下是不是忘了改config
 | 
					# 检查一下是不是忘了改config
 | 
				
			||||||
if API_KEY == "sk-此处填API秘钥":
 | 
					if API_KEY == "sk-此处填API秘钥":
 | 
				
			||||||
    assert False, "请在config文件中修改API密钥, 添加海外代理之后再运行"
 | 
					    assert False, "请在config文件中修改API密钥, 添加海外代理之后再运行"
 | 
				
			||||||
@ -1,26 +1,18 @@
 | 
				
			|||||||
# """
 | 
					from predict import predict_no_ui
 | 
				
			||||||
# 'primary' for main call-to-action, 
 | 
					 | 
				
			||||||
# 'secondary' for a more subdued style, 
 | 
					 | 
				
			||||||
# 'stop' for a stop button.
 | 
					 | 
				
			||||||
# """
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
fast_debug = False
 | 
					fast_debug = False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def 自我程序解构简单案例(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
 | 
					def 高阶功能模板函数(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
 | 
				
			||||||
    import time
 | 
					 | 
				
			||||||
    from predict import predict_no_ui_no_history
 | 
					 | 
				
			||||||
    for i in range(5):
 | 
					    for i in range(5):
 | 
				
			||||||
        i_say = f'我给出一个数字,你给出该数字的平方。我给出数字:{i}'
 | 
					        i_say = f'我给出一个数字,你给出该数字的平方。我给出数字:{i}'
 | 
				
			||||||
        gpt_say = predict_no_ui_no_history(inputs=i_say, top_p=top_p, temperature=temperature)
 | 
					        gpt_say = predict_no_ui(inputs=i_say, top_p=top_p, temperature=temperature)
 | 
				
			||||||
        chatbot.append((i_say, gpt_say))
 | 
					        chatbot.append((i_say, gpt_say))
 | 
				
			||||||
        history.append(i_say)
 | 
					        history.append(i_say)
 | 
				
			||||||
        history.append(gpt_say)
 | 
					        history.append(gpt_say)
 | 
				
			||||||
        yield chatbot, history, '正常'
 | 
					        yield chatbot, history, '正常'
 | 
				
			||||||
        time.sleep(10)
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def 解析项目本身(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
 | 
					def 解析项目本身(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
 | 
				
			||||||
    import time, glob, os
 | 
					    import time, glob, os
 | 
				
			||||||
    from predict import predict_no_ui
 | 
					 | 
				
			||||||
    file_manifest = [f for f in glob.glob('*.py')]
 | 
					    file_manifest = [f for f in glob.glob('*.py')]
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    for index, fp in enumerate(file_manifest):
 | 
					    for index, fp in enumerate(file_manifest):
 | 
				
			||||||
@ -30,7 +22,7 @@ def 解析项目本身(txt, top_p, temperature, chatbot, history, systemPromptTx
 | 
				
			|||||||
        前言 = "接下来请你分析自己的程序构成,别紧张," if index==0 else ""
 | 
					        前言 = "接下来请你分析自己的程序构成,别紧张," if index==0 else ""
 | 
				
			||||||
        i_say = 前言 + f'请对下面的程序文件做一个概述文件名是{fp},文件代码是 ```{file_content}```'
 | 
					        i_say = 前言 + f'请对下面的程序文件做一个概述文件名是{fp},文件代码是 ```{file_content}```'
 | 
				
			||||||
        i_say_show_user = 前言 + f'[{index}/{len(file_manifest)}] 请对下面的程序文件做一个概述: {os.path.abspath(fp)}'
 | 
					        i_say_show_user = 前言 + f'[{index}/{len(file_manifest)}] 请对下面的程序文件做一个概述: {os.path.abspath(fp)}'
 | 
				
			||||||
        chatbot.append((i_say_show_user, "[local] waiting gpt response."))
 | 
					        chatbot.append((i_say_show_user, "[waiting gpt response]"))
 | 
				
			||||||
        yield chatbot, history, '正常'
 | 
					        yield chatbot, history, '正常'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if not fast_debug: 
 | 
					        if not fast_debug: 
 | 
				
			||||||
@ -43,7 +35,7 @@ def 解析项目本身(txt, top_p, temperature, chatbot, history, systemPromptTx
 | 
				
			|||||||
            time.sleep(2)
 | 
					            time.sleep(2)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    i_say = f'根据以上你自己的分析,对程序的整体功能和构架做出概括。然后用一张markdown表格整理每个文件的功能(包括{file_manifest})。'
 | 
					    i_say = f'根据以上你自己的分析,对程序的整体功能和构架做出概括。然后用一张markdown表格整理每个文件的功能(包括{file_manifest})。'
 | 
				
			||||||
    chatbot.append((i_say, "[local] waiting gpt response."))
 | 
					    chatbot.append((i_say, "[waiting gpt response]"))
 | 
				
			||||||
    yield chatbot, history, '正常'
 | 
					    yield chatbot, history, '正常'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if not fast_debug: 
 | 
					    if not fast_debug: 
 | 
				
			||||||
@ -64,7 +56,6 @@ def report_execption(chatbot, history, a, b):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
def 解析源代码(file_manifest, project_folder, top_p, temperature, chatbot, history, systemPromptTxt):
 | 
					def 解析源代码(file_manifest, project_folder, top_p, temperature, chatbot, history, systemPromptTxt):
 | 
				
			||||||
    import time, glob, os
 | 
					    import time, glob, os
 | 
				
			||||||
    from predict import predict_no_ui
 | 
					 | 
				
			||||||
    print('begin analysis on:', file_manifest)
 | 
					    print('begin analysis on:', file_manifest)
 | 
				
			||||||
    for index, fp in enumerate(file_manifest):
 | 
					    for index, fp in enumerate(file_manifest):
 | 
				
			||||||
        with open(fp, 'r', encoding='utf-8') as f:
 | 
					        with open(fp, 'r', encoding='utf-8') as f:
 | 
				
			||||||
@ -73,7 +64,7 @@ def 解析源代码(file_manifest, project_folder, top_p, temperature, chatbot,
 | 
				
			|||||||
        前言 = "接下来请你逐文件分析下面的工程" if index==0 else ""
 | 
					        前言 = "接下来请你逐文件分析下面的工程" if index==0 else ""
 | 
				
			||||||
        i_say = 前言 + f'请对下面的程序文件做一个概述文件名是{os.path.relpath(fp, project_folder)},文件代码是 ```{file_content}```'
 | 
					        i_say = 前言 + f'请对下面的程序文件做一个概述文件名是{os.path.relpath(fp, project_folder)},文件代码是 ```{file_content}```'
 | 
				
			||||||
        i_say_show_user = 前言 + f'[{index}/{len(file_manifest)}] 请对下面的程序文件做一个概述: {os.path.abspath(fp)}'
 | 
					        i_say_show_user = 前言 + f'[{index}/{len(file_manifest)}] 请对下面的程序文件做一个概述: {os.path.abspath(fp)}'
 | 
				
			||||||
        chatbot.append((i_say_show_user, "[local] waiting gpt response."))
 | 
					        chatbot.append((i_say_show_user, "[waiting gpt response]"))
 | 
				
			||||||
        print('[1] yield chatbot, history')
 | 
					        print('[1] yield chatbot, history')
 | 
				
			||||||
        yield chatbot, history, '正常'
 | 
					        yield chatbot, history, '正常'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -98,7 +89,7 @@ def 解析源代码(file_manifest, project_folder, top_p, temperature, chatbot,
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    all_file = ', '.join([os.path.relpath(fp, project_folder) for index, fp in enumerate(file_manifest)])
 | 
					    all_file = ', '.join([os.path.relpath(fp, project_folder) for index, fp in enumerate(file_manifest)])
 | 
				
			||||||
    i_say = f'根据以上你自己的分析,对程序的整体功能和构架做出概括。然后用一张markdown表格整理每个文件的功能(包括{all_file})。'
 | 
					    i_say = f'根据以上你自己的分析,对程序的整体功能和构架做出概括。然后用一张markdown表格整理每个文件的功能(包括{all_file})。'
 | 
				
			||||||
    chatbot.append((i_say, "[local] waiting gpt response."))
 | 
					    chatbot.append((i_say, "[waiting gpt response]"))
 | 
				
			||||||
    yield chatbot, history, '正常'
 | 
					    yield chatbot, history, '正常'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if not fast_debug: 
 | 
					    if not fast_debug: 
 | 
				
			||||||
@ -159,22 +150,22 @@ def 解析一个C项目的头文件(txt, top_p, temperature, chatbot, history, s
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
def get_crazy_functionals():
 | 
					def get_crazy_functionals():
 | 
				
			||||||
    return {
 | 
					    return {
 | 
				
			||||||
        "程序解构简单案例": {
 | 
					        "[实验功能] 请解析并解构此项目本身": {
 | 
				
			||||||
            "Color": "stop",    # 按钮颜色
 | 
					 | 
				
			||||||
            "Function": 自我程序解构简单案例
 | 
					 | 
				
			||||||
        },
 | 
					 | 
				
			||||||
        "请解析并解构此项目本身": {
 | 
					 | 
				
			||||||
            "Color": "stop",    # 按钮颜色
 | 
					            "Color": "stop",    # 按钮颜色
 | 
				
			||||||
            "Function": 解析项目本身
 | 
					            "Function": 解析项目本身
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
        "解析一整个Python项目(输入栏给定项目完整目录)": {
 | 
					        "[实验功能] 解析一整个Python项目(输入栏给定项目完整目录)": {
 | 
				
			||||||
            "Color": "stop",    # 按钮颜色
 | 
					            "Color": "stop",    # 按钮颜色
 | 
				
			||||||
            "Function": 解析一个Python项目
 | 
					            "Function": 解析一个Python项目
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
        "解析一整个C++项目的头文件(输入栏给定项目完整目录)": {
 | 
					        "[实验功能] 解析一整个C++项目的头文件(输入栏给定项目完整目录)": {
 | 
				
			||||||
            "Color": "stop",    # 按钮颜色
 | 
					            "Color": "stop",    # 按钮颜色
 | 
				
			||||||
            "Function": 解析一个C项目的头文件
 | 
					            "Function": 解析一个C项目的头文件
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
 | 
					        "[实验功能] 高阶功能模板函数": {
 | 
				
			||||||
 | 
					            "Color": "stop",    # 按钮颜色
 | 
				
			||||||
 | 
					            "Function": 高阶功能模板函数
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										2
									
								
								main.py
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								main.py
									
									
									
									
									
								
							@ -106,7 +106,7 @@ with gr.Blocks() as demo:
 | 
				
			|||||||
    # submitBtn.click(reset_textbox, [], [txt])
 | 
					    # submitBtn.click(reset_textbox, [], [txt])
 | 
				
			||||||
    for k in functional:
 | 
					    for k in functional:
 | 
				
			||||||
        functional[k]["Button"].click(predict, 
 | 
					        functional[k]["Button"].click(predict, 
 | 
				
			||||||
            [txt, top_p, temperature, chatbot, history, systemPromptTxt, FALSE, TRUE, gr.State(k)], [chatbot, history, statusDisplay], show_progress=True)
 | 
					            [txt, top_p, temperature, chatbot, history, systemPromptTxt, TRUE, gr.State(k)], [chatbot, history, statusDisplay], show_progress=True)
 | 
				
			||||||
    for k in crazy_functional:
 | 
					    for k in crazy_functional:
 | 
				
			||||||
        crazy_functional[k]["Button"].click(crazy_functional[k]["Function"], 
 | 
					        crazy_functional[k]["Button"].click(crazy_functional[k]["Function"], 
 | 
				
			||||||
            [txt, top_p, temperature, chatbot, history, systemPromptTxt, gr.State(PORT)], [chatbot, history, statusDisplay])
 | 
					            [txt, top_p, temperature, chatbot, history, systemPromptTxt, gr.State(PORT)], [chatbot, history, statusDisplay])
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										182
									
								
								predict.py
									
									
									
									
									
								
							
							
						
						
									
										182
									
								
								predict.py
									
									
									
									
									
								
							@ -6,11 +6,12 @@ import logging
 | 
				
			|||||||
import traceback
 | 
					import traceback
 | 
				
			||||||
import requests
 | 
					import requests
 | 
				
			||||||
import importlib
 | 
					import importlib
 | 
				
			||||||
 | 
					from colorful import *
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# config_private.py放自己的秘密如API和代理网址
 | 
					# config_private.py放自己的秘密如API和代理网址
 | 
				
			||||||
# 读取时首先看是否存在私密的config_private配置文件(不受git管控),如果有,则覆盖原config文件
 | 
					# 读取时首先看是否存在私密的config_private配置文件(不受git管控),如果有,则覆盖原config文件
 | 
				
			||||||
try: from config_private import proxies, API_URL, API_KEY, TIMEOUT_SECONDS, MAX_RETRY
 | 
					try: from config_private import proxies, API_URL, API_KEY, TIMEOUT_SECONDS, MAX_RETRY, LLM_MODEL
 | 
				
			||||||
except: from config import proxies, API_URL, API_KEY, TIMEOUT_SECONDS, MAX_RETRY
 | 
					except: from config import proxies, API_URL, API_KEY, TIMEOUT_SECONDS, MAX_RETRY, LLM_MODEL
 | 
				
			||||||
 | 
					
 | 
				
			||||||
timeout_bot_msg = '[local] Request timeout, network error. please check proxy settings in config.py.'
 | 
					timeout_bot_msg = '[local] Request timeout, network error. please check proxy settings in config.py.'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -23,51 +24,12 @@ def get_full_error(chunk, stream_response):
 | 
				
			|||||||
    return chunk
 | 
					    return chunk
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def predict_no_ui(inputs, top_p, temperature, history=[]):
 | 
					def predict_no_ui(inputs, top_p, temperature, history=[]):
 | 
				
			||||||
    messages = [{"role": "system", "content": ""}]
 | 
					    headers, payload = generate_payload(inputs, top_p, temperature, history, system_prompt="", stream=False)
 | 
				
			||||||
 | 
					 | 
				
			||||||
    # 
 | 
					 | 
				
			||||||
    chat_counter = len(history) // 2
 | 
					 | 
				
			||||||
    if chat_counter > 0:
 | 
					 | 
				
			||||||
        for index in range(0, 2*chat_counter, 2):
 | 
					 | 
				
			||||||
            what_i_have_asked = {}
 | 
					 | 
				
			||||||
            what_i_have_asked["role"] = "user"
 | 
					 | 
				
			||||||
            what_i_have_asked["content"] = history[index]
 | 
					 | 
				
			||||||
            what_gpt_answer = {}
 | 
					 | 
				
			||||||
            what_gpt_answer["role"] = "assistant"
 | 
					 | 
				
			||||||
            what_gpt_answer["content"] = history[index+1]
 | 
					 | 
				
			||||||
            if what_i_have_asked["content"] != "":
 | 
					 | 
				
			||||||
                messages.append(what_i_have_asked)
 | 
					 | 
				
			||||||
                messages.append(what_gpt_answer)
 | 
					 | 
				
			||||||
            else:
 | 
					 | 
				
			||||||
                messages[-1]['content'] = what_gpt_answer['content']
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    what_i_ask_now = {}
 | 
					 | 
				
			||||||
    what_i_ask_now["role"] = "user"
 | 
					 | 
				
			||||||
    what_i_ask_now["content"] = inputs
 | 
					 | 
				
			||||||
    messages.append(what_i_ask_now)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    # messages
 | 
					 | 
				
			||||||
    payload = {
 | 
					 | 
				
			||||||
        "model": "gpt-3.5-turbo",
 | 
					 | 
				
			||||||
        # "model": "gpt-4",
 | 
					 | 
				
			||||||
        "messages": messages, 
 | 
					 | 
				
			||||||
        "temperature": temperature,  # 1.0,
 | 
					 | 
				
			||||||
        "top_p": top_p,  # 1.0,
 | 
					 | 
				
			||||||
        "n": 1,
 | 
					 | 
				
			||||||
        "stream": False,
 | 
					 | 
				
			||||||
        "presence_penalty": 0,
 | 
					 | 
				
			||||||
        "frequency_penalty": 0,
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    
 | 
					 | 
				
			||||||
    headers = {
 | 
					 | 
				
			||||||
        "Content-Type": "application/json",
 | 
					 | 
				
			||||||
        "Authorization": f"Bearer {API_KEY}"
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    retry = 0
 | 
					    retry = 0
 | 
				
			||||||
    while True:
 | 
					    while True:
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
            # make a POST request to the API endpoint using the requests.post method, passing in stream=True
 | 
					            # make a POST request to the API endpoint, stream=False
 | 
				
			||||||
            response = requests.post(API_URL, headers=headers, proxies=proxies,
 | 
					            response = requests.post(API_URL, headers=headers, proxies=proxies,
 | 
				
			||||||
                                    json=payload, stream=False, timeout=TIMEOUT_SECONDS*2); break
 | 
					                                    json=payload, stream=False, timeout=TIMEOUT_SECONDS*2); break
 | 
				
			||||||
        except TimeoutError as e:
 | 
					        except TimeoutError as e:
 | 
				
			||||||
@ -84,9 +46,7 @@ def predict_no_ui(inputs, top_p, temperature, history=[]):
 | 
				
			|||||||
        raise ConnectionAbortedError("Json解析不合常规,可能是文本过长" + response.text)
 | 
					        raise ConnectionAbortedError("Json解析不合常规,可能是文本过长" + response.text)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def predict(inputs, top_p, temperature, chatbot=[], history=[], system_prompt='', 
 | 
				
			||||||
 | 
					 | 
				
			||||||
def predict(inputs, top_p, temperature, chatbot=[], history=[], system_prompt='', retry=False, 
 | 
					 | 
				
			||||||
            stream = True, additional_fn=None):
 | 
					            stream = True, additional_fn=None):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if additional_fn is not None:
 | 
					    if additional_fn is not None:
 | 
				
			||||||
@ -101,60 +61,13 @@ def predict(inputs, top_p, temperature, chatbot=[], history=[], system_prompt=''
 | 
				
			|||||||
        chatbot.append((inputs, ""))
 | 
					        chatbot.append((inputs, ""))
 | 
				
			||||||
        yield chatbot, history, "等待响应"
 | 
					        yield chatbot, history, "等待响应"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    headers = {
 | 
					    headers, payload = generate_payload(inputs, top_p, temperature, history, system_prompt, stream)
 | 
				
			||||||
        "Content-Type": "application/json",
 | 
					    history.append(inputs); history.append(" ")
 | 
				
			||||||
        "Authorization": f"Bearer {API_KEY}"
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    chat_counter = len(history) // 2
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    print(f"chat_counter - {chat_counter}")
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    messages = [{"role": "system", "content": system_prompt}]
 | 
					 | 
				
			||||||
    if chat_counter:
 | 
					 | 
				
			||||||
        for index in range(0, 2*chat_counter, 2):
 | 
					 | 
				
			||||||
            what_i_have_asked = {}
 | 
					 | 
				
			||||||
            what_i_have_asked["role"] = "user"
 | 
					 | 
				
			||||||
            what_i_have_asked["content"] = history[index]
 | 
					 | 
				
			||||||
            what_gpt_answer = {}
 | 
					 | 
				
			||||||
            what_gpt_answer["role"] = "assistant"
 | 
					 | 
				
			||||||
            what_gpt_answer["content"] = history[index+1]
 | 
					 | 
				
			||||||
            if what_i_have_asked["content"] != "":
 | 
					 | 
				
			||||||
                if not (what_gpt_answer["content"] != "" or retry): continue
 | 
					 | 
				
			||||||
                if what_gpt_answer["content"] == timeout_bot_msg: continue
 | 
					 | 
				
			||||||
                messages.append(what_i_have_asked)
 | 
					 | 
				
			||||||
                messages.append(what_gpt_answer)
 | 
					 | 
				
			||||||
            else:
 | 
					 | 
				
			||||||
                messages[-1]['content'] = what_gpt_answer['content']
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    if retry and chat_counter:
 | 
					 | 
				
			||||||
        messages.pop()
 | 
					 | 
				
			||||||
    else:
 | 
					 | 
				
			||||||
        what_i_ask_now = {}
 | 
					 | 
				
			||||||
        what_i_ask_now["role"] = "user"
 | 
					 | 
				
			||||||
        what_i_ask_now["content"] = inputs
 | 
					 | 
				
			||||||
        messages.append(what_i_ask_now)
 | 
					 | 
				
			||||||
        chat_counter += 1
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    # messages
 | 
					 | 
				
			||||||
    payload = {
 | 
					 | 
				
			||||||
        "model": "gpt-3.5-turbo",
 | 
					 | 
				
			||||||
        # "model": "gpt-4",
 | 
					 | 
				
			||||||
        "messages": messages, 
 | 
					 | 
				
			||||||
        "temperature": temperature,  # 1.0,
 | 
					 | 
				
			||||||
        "top_p": top_p,  # 1.0,
 | 
					 | 
				
			||||||
        "n": 1,
 | 
					 | 
				
			||||||
        "stream": stream,
 | 
					 | 
				
			||||||
        "presence_penalty": 0,
 | 
					 | 
				
			||||||
        "frequency_penalty": 0,
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    history.append(inputs)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    retry = 0
 | 
					    retry = 0
 | 
				
			||||||
    while True:
 | 
					    while True:
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
            # make a POST request to the API endpoint using the requests.post method, passing in stream=True
 | 
					            # make a POST request to the API endpoint, stream=True
 | 
				
			||||||
            response = requests.post(API_URL, headers=headers, proxies=proxies,
 | 
					            response = requests.post(API_URL, headers=headers, proxies=proxies,
 | 
				
			||||||
                                    json=payload, stream=True, timeout=TIMEOUT_SECONDS);break
 | 
					                                    json=payload, stream=True, timeout=TIMEOUT_SECONDS);break
 | 
				
			||||||
        except:
 | 
					        except:
 | 
				
			||||||
@ -164,37 +77,30 @@ def predict(inputs, top_p, temperature, chatbot=[], history=[], system_prompt=''
 | 
				
			|||||||
            yield chatbot, history, "请求超时"+retry_msg
 | 
					            yield chatbot, history, "请求超时"+retry_msg
 | 
				
			||||||
            if retry > MAX_RETRY: raise TimeoutError
 | 
					            if retry > MAX_RETRY: raise TimeoutError
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    token_counter = 0
 | 
					    gpt_replying_buffer = ""
 | 
				
			||||||
    partial_words = ""
 | 
					    
 | 
				
			||||||
 | 
					    is_head_of_the_stream = True
 | 
				
			||||||
    counter = 0
 | 
					 | 
				
			||||||
    if stream:
 | 
					    if stream:
 | 
				
			||||||
        stream_response =  response.iter_lines()
 | 
					        stream_response =  response.iter_lines()
 | 
				
			||||||
        while True:
 | 
					        while True:
 | 
				
			||||||
            chunk = next(stream_response)
 | 
					            chunk = next(stream_response)
 | 
				
			||||||
            if chunk == b'data: [DONE]':
 | 
					            # print(chunk.decode()[6:])
 | 
				
			||||||
                break
 | 
					            if is_head_of_the_stream:
 | 
				
			||||||
 | 
					                is_head_of_the_stream = False; continue
 | 
				
			||||||
            if counter == 0:
 | 
					            
 | 
				
			||||||
                counter += 1
 | 
					 | 
				
			||||||
                continue
 | 
					 | 
				
			||||||
            counter += 1
 | 
					 | 
				
			||||||
            # check whether each line is non-empty
 | 
					 | 
				
			||||||
            if chunk:
 | 
					            if chunk:
 | 
				
			||||||
                # decode each line as response data is in bytes
 | 
					 | 
				
			||||||
                try:
 | 
					                try:
 | 
				
			||||||
                    if len(json.loads(chunk.decode()[6:])['choices'][0]["delta"]) == 0:
 | 
					                    if len(json.loads(chunk.decode()[6:])['choices'][0]["delta"]) == 0:
 | 
				
			||||||
                        logging.info(f'[response] {chatbot[-1][-1]}')
 | 
					                        # 判定为数据流的结束,gpt_replying_buffer也写完了
 | 
				
			||||||
 | 
					                        logging.info(f'[response] {gpt_replying_buffer}')
 | 
				
			||||||
                        break
 | 
					                        break
 | 
				
			||||||
 | 
					                    # 处理数据流的主体
 | 
				
			||||||
                    chunkjson = json.loads(chunk.decode()[6:])
 | 
					                    chunkjson = json.loads(chunk.decode()[6:])
 | 
				
			||||||
                    status_text = f"finish_reason: {chunkjson['choices'][0]['finish_reason']}"
 | 
					                    status_text = f"finish_reason: {chunkjson['choices'][0]['finish_reason']}"
 | 
				
			||||||
                    partial_words = partial_words + json.loads(chunk.decode()[6:])['choices'][0]["delta"]["content"]
 | 
					                    # 如果这里抛出异常,一般是文本过长,详情见get_full_error的输出
 | 
				
			||||||
                    if token_counter == 0:
 | 
					                    gpt_replying_buffer = gpt_replying_buffer + json.loads(chunk.decode()[6:])['choices'][0]["delta"]["content"]
 | 
				
			||||||
                        history.append(" " + partial_words)
 | 
					                    history[-1] = gpt_replying_buffer
 | 
				
			||||||
                    else:
 | 
					 | 
				
			||||||
                        history[-1] = partial_words
 | 
					 | 
				
			||||||
                    chatbot[-1] = (history[-2], history[-1])
 | 
					                    chatbot[-1] = (history[-2], history[-1])
 | 
				
			||||||
                    token_counter += 1
 | 
					 | 
				
			||||||
                    yield chatbot, history, status_text
 | 
					                    yield chatbot, history, status_text
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                except Exception as e:
 | 
					                except Exception as e:
 | 
				
			||||||
@ -207,4 +113,48 @@ def predict(inputs, top_p, temperature, chatbot=[], history=[], system_prompt=''
 | 
				
			|||||||
                    yield chatbot, history, "Json解析不合常规,很可能是文本过长" + error_msg
 | 
					                    yield chatbot, history, "Json解析不合常规,很可能是文本过长" + error_msg
 | 
				
			||||||
                    return
 | 
					                    return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def generate_payload(inputs, top_p, temperature, history, system_prompt, stream):
 | 
				
			||||||
 | 
					    headers = {
 | 
				
			||||||
 | 
					        "Content-Type": "application/json",
 | 
				
			||||||
 | 
					        "Authorization": f"Bearer {API_KEY}"
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    conversation_cnt = len(history) // 2
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    messages = [{"role": "system", "content": system_prompt}]
 | 
				
			||||||
 | 
					    if conversation_cnt:
 | 
				
			||||||
 | 
					        for index in range(0, 2*conversation_cnt, 2):
 | 
				
			||||||
 | 
					            what_i_have_asked = {}
 | 
				
			||||||
 | 
					            what_i_have_asked["role"] = "user"
 | 
				
			||||||
 | 
					            what_i_have_asked["content"] = history[index]
 | 
				
			||||||
 | 
					            what_gpt_answer = {}
 | 
				
			||||||
 | 
					            what_gpt_answer["role"] = "assistant"
 | 
				
			||||||
 | 
					            what_gpt_answer["content"] = history[index+1]
 | 
				
			||||||
 | 
					            if what_i_have_asked["content"] != "":
 | 
				
			||||||
 | 
					                if what_gpt_answer["content"] == "": continue
 | 
				
			||||||
 | 
					                if what_gpt_answer["content"] == timeout_bot_msg: continue
 | 
				
			||||||
 | 
					                messages.append(what_i_have_asked)
 | 
				
			||||||
 | 
					                messages.append(what_gpt_answer)
 | 
				
			||||||
 | 
					            else:
 | 
				
			||||||
 | 
					                messages[-1]['content'] = what_gpt_answer['content']
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    what_i_ask_now = {}
 | 
				
			||||||
 | 
					    what_i_ask_now["role"] = "user"
 | 
				
			||||||
 | 
					    what_i_ask_now["content"] = inputs
 | 
				
			||||||
 | 
					    messages.append(what_i_ask_now)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    payload = {
 | 
				
			||||||
 | 
					        "model": LLM_MODEL,
 | 
				
			||||||
 | 
					        "messages": messages, 
 | 
				
			||||||
 | 
					        "temperature": temperature,  # 1.0,
 | 
				
			||||||
 | 
					        "top_p": top_p,  # 1.0,
 | 
				
			||||||
 | 
					        "n": 1,
 | 
				
			||||||
 | 
					        "stream": stream,
 | 
				
			||||||
 | 
					        "presence_penalty": 0,
 | 
				
			||||||
 | 
					        "frequency_penalty": 0,
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    print(f" {LLM_MODEL} : {conversation_cnt} : {inputs}")
 | 
				
			||||||
 | 
					    return headers,payload
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user