From 8d9bd619c499c399537feeba4a06229783839f50 Mon Sep 17 00:00:00 2001 From: Kakune55 Date: Sat, 9 Dec 2023 15:06:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AF=B9GPT4.0-Turbo?= =?UTF-8?q?=E6=A8=A1=E5=9E=8B=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Server/gpt4Turbo.py | 28 ++++++++++++++++++++++++++++ src/Server/main.py | 8 +++++++- src/Web/index.html | 1 + 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 src/Server/gpt4Turbo.py 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 @@ +

当前UserKey