适配ai网关应用

This commit is contained in:
w_xiaolizu
2023-06-08 20:17:38 +08:00
parent 1ac5f934f2
commit 955367b0bc
4 changed files with 49 additions and 78 deletions

View File

@ -591,69 +591,5 @@ class JsonHandle:
if __name__ == '__main__':
num = num_tokens_from_string([
"""
You are jvs, 帮我做一篇关于chatgpt的分享,大纲为ChatGPT 的基本原理和应用、Prompt 的作用和优化、ChatGPT 和 Prompt 的案例研究、ChatGPT 和 Prompt 的挑战和限制、ChatGPT 和 Prompt 的未来发展
Your decisions must always be made independently without seeking user assistance. Play to your strengths as an LLM and pursue simple strategies with no legal complications.
GOALS:
1. 在文后将市面上的AI应用作比较对每个部分进行适当调整和拓展
Constraints: # 确定目标
1. ~4000 word limit for short term memory. Your short term memory is short, so immediately save important information to files.
2. If you are unsure how you previously did something or want to recall past events, thinking about similar events will help you remember.
3. No user assistance
4. Exclusively use the commands listed below e.g. command_name
Commands: # 可执行的行动
1. analyze_code: Analyze Code, args: "code": "<full_code_string>"
2. execute_python_file: Execute Python File, args: "filename": "<filename>"
3. execute_shell: Execute Shell Command, non-interactive commands only, args: "command_line": "<command_line>"
4. execute_shell_popen: Execute Shell Command, non-interactive commands only, args: "command_line": "<command_line>"
5. append_to_file: Append to file, args: "filename": "<filename>", "text": "<text>"
6. delete_file: Delete file, args: "filename": "<filename>"
7. list_files: List Files in Directory, args: "directory": "<directory>"
8. read_file: Read file, args: "filename": "<filename>"
9. write_to_file: Write to file, args: "filename": "<filename>", "text": "<text>"
10. google: Google Search, args: "query": "<query>"
11. generate_image: Generate Image, args: "prompt": "<prompt>"
12. improve_code: Get Improved Code, args: "suggestions": "<list_of_suggestions>", "code": "<full_code_string>"
13. send_tweet: Send Tweet, args: "tweet_text": "<tweet_text>"
14. browse_website: Browse Website, args: "url": "<url>", "question": "<what_you_want_to_find_on_website>"
15. write_tests: Write Tests, args: "code": "<full_code_string>", "focus": "<list_of_focus_areas>"
16. delete_agent: Delete GPT Agent, args: "key": "<key>"
17. get_hyperlinks: Get text summary, args: "url": "<url>"
18. get_text_summary: Get text summary, args: "url": "<url>", "question": "<question>"
19. list_agents: List GPT Agents, args: () -> str
20. message_agent: Message GPT Agent, args: "key": "<key>", "message": "<message>"
21. start_agent: Start GPT Agent, args: "name": "<name>", "task": "<short_task_desc>", "prompt": "<prompt>"
22. task_complete: Task Complete (Shutdown), args: "reason": "<reason>"
Resources: # 可用资源
1. Internet access for searches and information gathering.
2. Long Term memory management.
3. GPT-3.5 powered Agents for delegation of simple tasks.
4. File output.
Performance Evaluation: # 自我评估
1. Continuously review and analyze your actions to ensure you are performing to the best of your abilities.
2. Constructively self-criticize your big-picture behavior constantly.
3. Reflect on past decisions and strategies to refine your approach.
4. Every command has a cost, so be smart and efficient. Aim to complete tasks in the least number of steps.
5. Write all code to a file.
You should only respond in JSON format as described below
Response Format: # 限制GPT的回答
{
"thoughts": {
"text": "thought",
"reasoning": "reasoning",
"plan": "- short bulleted\n- list that conveys\n- long-term plan",
"criticism": "constructive self-criticism",
"speak": "thoughts summary to say to user"
},
"command": {
"name": "command name",
"args": {
"arg name": "value"
}
}
}
Ensure the response can be parsed by Python json.loads
"""
])
num = num_tokens_from_string(['你好', '您好,请问有什么可以帮助您的吗?'])
print(num)

View File

@ -48,10 +48,11 @@ class LazyloadTiktoken(object):
return encoder.decode(*args, **kwargs)
# Endpoint 重定向
API_URL_REDIRECT, = get_conf("API_URL_REDIRECT")
API_URL_REDIRECT, PROXY_API_URL = get_conf("API_URL_REDIRECT", 'PROXY_API_URL')
openai_endpoint = "https://api.openai.com/v1/chat/completions"
api2d_endpoint = "https://openai.api2d.net/v1/chat/completions"
newbing_endpoint = "wss://sydney.bing.com/sydney/ChatHub"
proxy_endpoint = PROXY_API_URL
# 兼容旧版的配置
try:
API_URL, = get_conf("API_URL")
@ -66,6 +67,7 @@ if api2d_endpoint in API_URL_REDIRECT: api2d_endpoint = API_URL_REDIRECT[api2d_e
if newbing_endpoint in API_URL_REDIRECT: newbing_endpoint = API_URL_REDIRECT[newbing_endpoint]
# 获取tokenizer
tokenizer_gpt35 = LazyloadTiktoken("gpt-3.5-turbo")
tokenizer_gpt4 = LazyloadTiktoken("gpt-4")
@ -93,6 +95,24 @@ model_info = {
"token_cnt": get_token_num_gpt4,
},
"proxy-gpt-4": {
"fn_with_ui": chatgpt_ui,
"fn_without_ui": chatgpt_noui,
"endpoint": proxy_endpoint.replace('%v', 'gpt-4'),
"max_token": 8192,
"tokenizer": tokenizer_gpt4,
"token_cnt": get_token_num_gpt4,
},
"proxy-gpt-4-32k": {
"fn_with_ui": chatgpt_ui,
"fn_without_ui": chatgpt_noui,
"endpoint": proxy_endpoint.replace('%v', 'gpt-4-32k'),
"max_token": 32000,
"tokenizer": tokenizer_gpt4,
"token_cnt": get_token_num_gpt4,
},
# api_2d
"api2d-gpt-3.5-turbo": {
"fn_with_ui": chatgpt_ui,

View File

@ -208,7 +208,7 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp
error_msg = chunk_decoded
if "reduce the length" in error_msg:
if len(history) >= 2: history[-1] = ""; history[-2] = "" # 清除当前溢出的输入history[-2] 是本次输入, history[-1] 是本次输出
history = clip_history(inputs=inputs, history=history, tokenizer=model_info[llm_kwargs['llm_model']]['tokenizer'],
history = clip_history(inputs=inputs, history=history, tokenizer=model_info[llm_kwargs['llm_model']]['tokenizer'],
max_token_limit=(model_info[llm_kwargs['llm_model']]['max_token'])) # history至少释放二分之一
chatbot[-1] = (chatbot[-1][0], "[Local Message] Reduce the length. 本次输入过长, 或历史数据过长. 历史缓存数据已部分释放, 您可以请再次尝试. (若再次失败则更可能是因为输入过长.)")
# history = [] # 清除历史
@ -238,13 +238,18 @@ def generate_payload(inputs, llm_kwargs, history, system_prompt, stream):
"""
if not is_any_api_key(llm_kwargs['api_key']):
raise AssertionError("你提供了错误的API_KEY。\n\n1. 临时解决方案直接在输入区键入api_key然后回车提交。\n\n2. 长效解决方案在config.py中配置。")
api_key = select_api_key(llm_kwargs['api_key'], llm_kwargs['llm_model'])
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
if llm_kwargs['llm_model'].startswith('proxy-'):
api_key = select_api_key(llm_kwargs['api_key'], llm_kwargs['llm_model'])
headers = {
"Content-Type": "application/json",
"api-key": f"{api_key}"
}
else:
api_key = select_api_key(llm_kwargs['api_key'], llm_kwargs['llm_model'])
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
conversation_cnt = len(history) // 2

View File

@ -54,10 +54,13 @@ def ArgsGeneralWrapper(f):
""""""
# 引入一个有cookie的chatbot
start_time = time.time()
encrypt, private = get_conf('switch_model')[0]['key']
private_key, = get_conf('private_key')
cookies.update({
'top_p':top_p,
'temperature':temperature,
})
llm_kwargs = {
'api_key': cookies['api_key'],
'llm_model': llm_model,
@ -70,8 +73,6 @@ def ArgsGeneralWrapper(f):
plugin_kwargs = {
"advanced_arg": plugin_advanced_arg
}
encrypt, private = get_conf('switch_model')[0]['key']
private_key = get_conf('private_key')[0]
if private in models:
if chatbot == []:
chatbot.append([None, f'隐私模式, 你的对话记录无法被他人检索 <p style="display:none;">\n{private_key}\n{ipaddr.client.host}\n</p>'])
@ -573,6 +574,12 @@ def is_api2d_key(key):
else:
return False
def is_proxy_key(key):
if 'proxy' in key:
return True
else:
return False
def is_any_api_key(key):
if ',' in key:
keys = key.split(',')
@ -580,7 +587,7 @@ def is_any_api_key(key):
if is_any_api_key(k): return True
return False
else:
return is_openai_api_key(key) or is_api2d_key(key)
return is_openai_api_key(key) or is_api2d_key(key) or is_proxy_key(key)
def what_keys(keys):
avail_key_list = {'OpenAI Key':0, "API2D Key":0}
@ -609,6 +616,10 @@ def select_api_key(keys, llm_model):
for k in key_list:
if is_api2d_key(k): avail_key_list.append(k)
if llm_model.startswith('proxy'):
for k in key_list:
if is_proxy_key(k): avail_key_list.append(k.replace('proxy-', ''))
if len(avail_key_list) == 0:
raise RuntimeError(f"您提供的api-key不满足要求不包含任何可用于{llm_model}的api-key。您可能选择了错误的模型或请求源。")
@ -905,4 +916,3 @@ def objload(file='objdump.tmp'):
return
with open(file, 'rb') as f:
return pickle.load(f)