diff --git a/src/Server/gpt4Turbo.py b/src/Server/gpt4Turbo.py new file mode 100644 index 0000000..078ee05 --- /dev/null +++ b/src/Server/gpt4Turbo.py @@ -0,0 +1,28 @@ +import openai , config + +openai.api_key = config.readConf()["gpt3.5turbo"]["Authorization"] +openai.base_url = config.readConf()["gpt3.5turbo"]["url"] + +def service(prompt,history = ""): + if history == "": + response = openai.chat.completions.create( + model="gpt-4-1106-preview", + messages=[ + {"role": "user", "content": prompt}, + ] + ) + else: + response = openai.chat.completions.create( + model="gpt-4-1106-preview", + messages=[ + {"role": "user", "content": history[1]["user"]}, + {"role": "assistant", "content": history[1]["bot"]}, + {"role": "user", "content": history[0]["user"]}, + {"role": "assistant", "content": history[0]["bot"]}, + {"role": "user", "content": prompt}, + ] + ) + if response.choices[0].finish_reason == "stop": + return 200, response.choices[0].message.content, int(response.usage.total_tokens*45) #45倍tokens消耗 + else: + return 50 , "API Error!", 0 \ No newline at end of file diff --git a/src/Server/main.py b/src/Server/main.py index a6a0476..23d970e 100644 --- a/src/Server/main.py +++ b/src/Server/main.py @@ -1,6 +1,6 @@ import flask , requests , json from flask_cors import CORS -import db , qwenTurbo , chatglmTurbo , gpt35Turbo +import db , qwenTurbo , chatglmTurbo , gpt35Turbo , gpt4Turbo @@ -42,6 +42,12 @@ def post_data(): elif userRequest["context"] == 0: code , output , tokenUsed = gpt35Turbo.service(userRequest['prompt']) + if userRequest["model"] == "gpt4.0-turbo": # 调用gpt4.0-turbo + if userRequest["context"] == 1: # 是否使用上文关联 + code , output , tokenUsed = gpt4Turbo.service(userRequest['prompt'],userRequest['history']) + elif userRequest["context"] == 0: + code , output , tokenUsed = gpt4Turbo.service(userRequest['prompt']) + diff --git a/src/Web/index.html b/src/Web/index.html index 79a0987..5b3b9b8 100644 --- a/src/Web/index.html +++ b/src/Web/index.html @@ -111,6 +111,7 @@ +