markqiu commited on
Commit
fb25388
·
1 Parent(s): 886091f

调试采用brnie-bot-sdk,未完成

Browse files
ChuanhuChatbot.py CHANGED
@@ -788,7 +788,7 @@ with gr.Blocks(theme=small_and_beautiful_theme) as demo:
788
 
789
  logging.info(
790
  colorama.Back.GREEN
791
- + "\n小原同学的温馨提示:访问 http://localhost:7860 查看界面"
792
  + colorama.Style.RESET_ALL
793
  )
794
  # 默认开启本地服务器,默认可以直接从IP访问,默认不创建公开分享链接
 
788
 
789
  logging.info(
790
  colorama.Back.GREEN
791
+ + f"\n小原同学的温馨提示:访问 http://localhost:{server_port} 查看界面"
792
  + colorama.Style.RESET_ALL
793
  )
794
  # 默认开启本地服务器,默认可以直接从IP访问,默认不创建公开分享链接
modules/config.py CHANGED
@@ -1,6 +1,5 @@
1
  from collections import defaultdict
2
  from contextlib import contextmanager
3
- from json import JSONDecodeError
4
  import os
5
  import logging
6
  import sys
@@ -39,11 +38,10 @@ with open("config.json" if os.path.exists("config.json") else "config_example.js
39
  config = json.load(f)
40
 
41
 
42
- def load_config_to_environ(key_list):
43
  global config
44
- for key in key_list:
45
- if key in config:
46
- os.environ[key.upper()] = os.environ.get(key.upper(), config[key])
47
 
48
 
49
  def load_environ_to_config():
@@ -155,8 +153,7 @@ ernie_secret_key = config.get("ernie_secret_key", "")
155
  os.environ["ERNIE_SECRETKEY"] = ernie_secret_key
156
 
157
  load_environ_to_config()
158
- load_config_to_environ(["openai_api_type", "azure_openai_api_key", "azure_openai_api_base_url",
159
- "azure_openai_api_version", "azure_deployment_name", "azure_embedding_deployment_name", "azure_embedding_model_name"])
160
 
161
 
162
  usage_limit = os.environ.get("USAGE_LIMIT", config.get("usage_limit", 120))
 
1
  from collections import defaultdict
2
  from contextlib import contextmanager
 
3
  import os
4
  import logging
5
  import sys
 
38
  config = json.load(f)
39
 
40
 
41
+ def load_config_to_environ():
42
  global config
43
+ for key in config:
44
+ os.environ[key.upper()] = os.environ.get(key.upper(), str(config[key]))
 
45
 
46
 
47
  def load_environ_to_config():
 
153
  os.environ["ERNIE_SECRETKEY"] = ernie_secret_key
154
 
155
  load_environ_to_config()
156
+ load_config_to_environ()
 
157
 
158
 
159
  usage_limit = os.environ.get("USAGE_LIMIT", config.get("usage_limit", 120))
modules/models/ERNIE.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import erniebot as eb
2
 
3
  from ..presets import *
@@ -9,40 +10,11 @@ from .base_model import BaseLLMModel
9
  class ERNIE_Client(BaseLLMModel):
10
  def __init__(self, model_name, api_key, secret_key, api_type: str = "aistudio", access_token: str = None) -> None:
11
  super().__init__(model_name=model_name)
12
- self.auth_config = {
13
- "api_type": api_type,
14
- }
15
- self.auth_config["ak"] = api_key
16
- self.auth_config["sk"] = secret_key
17
  if access_token:
18
  self.auth_config["access_token"] = access_token
19
- self.eb = eb.ChatCompletion
20
-
21
- if self.model_name == "ERNIE-Bot-turbo":
22
- self.ERNIE_url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/eb-instant?access_token="
23
- elif self.model_name == "ERNIE-Bot":
24
- self.ERNIE_url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions?access_token="
25
- elif self.model_name == "ERNIE-Bot-4":
26
- self.ERNIE_url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro?access_token="
27
 
28
- def get_access_token(self):
29
- """
30
- 使用 AK,SK 生成鉴权签名(Access Token)
31
- :return: access_token,或是None(如果错误)
32
- """
33
- url = "https://aip.baidubce.com/oauth/2.0/token?client_id=" + self.api_key + "&client_secret=" + self.api_secret + "&grant_type=client_credentials"
34
-
35
- payload = json.dumps("")
36
- headers = {
37
- 'Content-Type': 'application/json',
38
- 'Accept': 'application/json'
39
- }
40
-
41
- response = requests.request("POST", url, headers=headers, data=payload)
42
-
43
- return response.json()["access_token"]
44
  def get_answer_stream_iter(self):
45
- url = self.ERNIE_url + self.get_access_token()
46
  system_prompt = self.system_prompt
47
  history = self.history
48
  if system_prompt is not None:
@@ -51,30 +23,23 @@ class ERNIE_Client(BaseLLMModel):
51
  # 去除history中 history的role为system的
52
  history = [i for i in history if i["role"] != "system"]
53
 
54
- payload = json.dumps({
55
- "messages":history,
56
- "stream": True
57
- })
58
- headers = {
59
- 'Content-Type': 'application/json'
60
  }
61
 
62
- response = requests.request("POST", url, headers=headers, data=payload, stream=True)
63
-
64
- if response.status_code == 200:
65
- partial_text = ""
66
- for line in response.iter_lines():
67
- if len(line) == 0:
68
- continue
69
- line = json.loads(line[5:])
70
- partial_text += line['result']
71
- yield partial_text
72
  else:
73
- yield STANDARD_ERROR_MSG + GENERAL_ERROR_MSG
 
 
 
 
74
 
75
 
76
  def get_answer_at_once(self):
77
- url = self.ERNIE_url + self.get_access_token()
78
  system_prompt = self.system_prompt
79
  history = self.history
80
  if system_prompt is not None:
@@ -83,20 +48,16 @@ class ERNIE_Client(BaseLLMModel):
83
  # 去除history中 history的role为system的
84
  history = [i for i in history if i["role"] != "system"]
85
 
86
- payload = json.dumps({
 
87
  "messages": history,
88
- "stream": True
89
- })
90
- headers = {
91
- 'Content-Type': 'application/json'
92
  }
93
 
94
- response = requests.request("POST", url, headers=headers, data=payload, stream=True)
95
-
96
- if response.status_code == 200:
97
-
98
- return str(response.json()["result"]),len(response.json()["result"])
99
  else:
100
- return "获取资源错误", 0
101
-
102
-
 
1
+ import os
2
  import erniebot as eb
3
 
4
  from ..presets import *
 
10
  class ERNIE_Client(BaseLLMModel):
11
  def __init__(self, model_name, api_key, secret_key, api_type: str = "aistudio", access_token: str = None) -> None:
12
  super().__init__(model_name=model_name)
13
+ self.auth_config = {"api_type": api_type, "ak": api_key, "sk": secret_key}
 
 
 
 
14
  if access_token:
15
  self.auth_config["access_token"] = access_token
 
 
 
 
 
 
 
 
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  def get_answer_stream_iter(self):
 
18
  system_prompt = self.system_prompt
19
  history = self.history
20
  if system_prompt is not None:
 
23
  # 去除history中 history的role为system的
24
  history = [i for i in history if i["role"] != "system"]
25
 
26
+ data = {
27
+ "messages": history,
28
+ "top_p": self.top_p,
29
+ "temperature": self.temperature,
 
 
30
  }
31
 
32
+ if self.model_name == "chat_file":
33
+ response = eb.ChatFile.create(_config_=self.auth_config, **data, stream=True)
 
 
 
 
 
 
 
 
34
  else:
35
+ response = eb.ChatCompletion.create(
36
+ _config_=self.auth_config, model=self.model_name, **data, stream=True
37
+ )
38
+ for result in response:
39
+ yield result.get_result()
40
 
41
 
42
  def get_answer_at_once(self):
 
43
  system_prompt = self.system_prompt
44
  history = self.history
45
  if system_prompt is not None:
 
48
  # 去除history中 history的role为system的
49
  history = [i for i in history if i["role"] != "system"]
50
 
51
+
52
+ data = {
53
  "messages": history,
54
+ "top_p": self.top_p,
55
+ "temperature": self.temperature,
 
 
56
  }
57
 
58
+ if self.model_name == "chat_file":
59
+ return eb.ChatFile.create(_config_=self.auth_config, **data, stream=False).get_result()
 
 
 
60
  else:
61
+ return eb.ChatCompletion.create(
62
+ _config_=self.auth_config, model=self.model_name, **data, stream=False
63
+ ).get_result()
modules/models/models.py CHANGED
@@ -128,7 +128,7 @@ def get_model(
128
  model = Qwen_Client(model_name, user_name=user_name)
129
  elif model_type == ModelType.ERNIE:
130
  from .ERNIE import ERNIE_Client
131
- model = ERNIE_Client(model_name, api_key=os.getenv("ERNIE_APIKEY"),secret_key=os.getenv("ERNIE_SECRETKEY"))
132
  elif model_type == ModelType.DALLE3:
133
  from .DALLE3 import OpenAI_DALLE3_Client
134
  access_key = os.environ.get("OPENAI_API_KEY", access_key)
 
128
  model = Qwen_Client(model_name, user_name=user_name)
129
  elif model_type == ModelType.ERNIE:
130
  from .ERNIE import ERNIE_Client
131
+ model = ERNIE_Client(model_name, api_key=os.getenv("ERNIE_APIKEY"),secret_key=os.getenv("ERNIE_SECRETKEY"), api_type=os.getenv("ERNIE_API_TYPE"), access_token=os.getenv("ERNIE_ACCESS_TOKEN"))
132
  elif model_type == ModelType.DALLE3:
133
  from .DALLE3 import OpenAI_DALLE3_Client
134
  access_key = os.environ.get("OPENAI_API_KEY", access_key)
modules/presets.py CHANGED
@@ -84,9 +84,9 @@ ONLINE_MODELS = [
84
  "讯飞星火大模型V2.0",
85
  "讯飞星火大模型V1.5",
86
  "Claude",
87
- "ERNIE-Bot-turbo",
88
- "ERNIE-Bot",
89
- "ERNIE-Bot-4",
90
  ]
91
 
92
  LOCAL_MODELS = [
@@ -164,17 +164,17 @@ MODEL_METADATA = {
164
  "model_name": "Claude",
165
  "token_limit": 4096,
166
  },
167
- "ERNIE-Bot-turbo": {
168
- "model_name": "ERNIE-Bot-turbo",
169
- "token_limit": 1024,
170
  },
171
- "ERNIE-Bot": {
172
- "model_name": "ERNIE-Bot",
173
- "token_limit": 1024,
174
  },
175
- "ERNIE-Bot-4": {
176
- "model_name": "ERNIE-Bot-4",
177
- "token_limit": 7000,
178
  },
179
  }
180
 
 
84
  "讯飞星火大模型V2.0",
85
  "讯飞星火大模型V1.5",
86
  "Claude",
87
+ "ernie-bot-turbo",
88
+ "ernie-bot",
89
+ "ernie-bot-4",
90
  ]
91
 
92
  LOCAL_MODELS = [
 
164
  "model_name": "Claude",
165
  "token_limit": 4096,
166
  },
167
+ "ernie-bot-turbo": {
168
+ "model_name": "ernie-bot-turbo",
169
+ "token_limit": 3000,
170
  },
171
+ "ernie-bot": {
172
+ "model_name": "ernie-bot",
173
+ "token_limit": 3000,
174
  },
175
+ "ernie-bot-4": {
176
+ "model_name": "ernie-bot-4",
177
+ "token_limit": 3000,
178
  },
179
  }
180