更新火山方舟调用 (#3564)

* 更新火山方舟调用
This commit is contained in:
An0nymous 2024-04-25 14:33:28 +08:00 committed by GitHub
parent cf049511cf
commit 9c0820c94a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 33 additions and 27 deletions

View File

@ -81,7 +81,7 @@ ONLINE_LLM_MODEL = {
# 火山方舟 API文档参考 https://www.volcengine.com/docs/82379 # 火山方舟 API文档参考 https://www.volcengine.com/docs/82379
"fangzhou-api": { "fangzhou-api": {
"version": "chatglm-6b-model", "version": "", # 对应火山方舟的 endpoint_id
"version_url": "", "version_url": "",
"api_key": "", "api_key": "",
"secret_key": "", "secret_key": "",

View File

@ -43,7 +43,7 @@ llama-index==0.9.35
# beautifulsoup4==4.12.2 # beautifulsoup4==4.12.2
# pysrt==1.1.2 # pysrt==1.1.2
# dashscope==1.13.6 # qwen # dashscope==1.13.6 # qwen
# volcengine==1.0.119 # fangzhou # volcengine==1.0.134 # fangzhou
# uncomment libs if you want to use corresponding vector store # uncomment libs if you want to use corresponding vector store
# pymilvus==2.3.6 # pymilvus==2.3.6
# psycopg2==2.9.9 # psycopg2==2.9.9

View File

@ -50,7 +50,7 @@ pyjwt==2.8.0
# duckduckgo-search~=3.9.9 # duckduckgo-search~=3.9.9
# metaphor-python~=0.1.23 # metaphor-python~=0.1.23
# volcengine>=1.0.119 # volcengine>=1.0.134
# pymilvus==2.3.6 # pymilvus==2.3.6
# psycopg2==2.9.9 # psycopg2==2.9.9
# pgvector>=0.2.4 # pgvector>=0.2.4

View File

@ -30,7 +30,7 @@ youtube-search~=2.1.2
duckduckgo-search~=3.9.9 duckduckgo-search~=3.9.9
metaphor-python~=0.1.23 metaphor-python~=0.1.23
watchdog~=3.0.0 watchdog~=3.0.0
# volcengine>=1.0.119 # volcengine>=1.0.134
# pymilvus>=2.3.4 # pymilvus>=2.3.4
# psycopg2==2.9.9 # psycopg2==2.9.9
# pgvector>=0.2.4 # pgvector>=0.2.4

View File

@ -24,9 +24,8 @@ class FangZhouWorker(ApiModelWorker):
kwargs.setdefault("context_len", 16384) kwargs.setdefault("context_len", 16384)
super().__init__(**kwargs) super().__init__(**kwargs)
self.version = version self.version = version
def do_chat(self, params: ApiChatParams) -> Dict: def do_chat(self, params: ApiChatParams) -> Dict:
from volcengine.maas import MaasService from volcengine.maas.v2 import MaasService
params.load_config(self.model_names[0]) params.load_config(self.model_names[0])
maas = MaasService('maas-api.ml-platform-cn-beijing.volces.com', 'cn-beijing') maas = MaasService('maas-api.ml-platform-cn-beijing.volces.com', 'cn-beijing')
@ -35,9 +34,6 @@ class FangZhouWorker(ApiModelWorker):
# document: "https://www.volcengine.com/docs/82379/1099475" # document: "https://www.volcengine.com/docs/82379/1099475"
req = { req = {
"model": {
"name": params.version,
},
"parameters": { "parameters": {
# 这里的参数仅为示例,具体可用的参数请参考具体模型的 API 说明 # 这里的参数仅为示例,具体可用的参数请参考具体模型的 API 说明
"max_new_tokens": params.max_tokens, "max_new_tokens": params.max_tokens,
@ -49,9 +45,9 @@ class FangZhouWorker(ApiModelWorker):
text = "" text = ""
if log_verbose: if log_verbose:
self.logger.info(f'{self.__class__.__name__}:maas: {maas}') self.logger.info(f'{self.__class__.__name__}:maas: {maas}')
for resp in maas.stream_chat(req): for resp in maas.stream_chat(params.version, unicode_escape_data(req)):
if error := resp.error: error = resp.error
if error.code_n > 0: if error and error.code_n > 0:
data = { data = {
"error_code": error.code_n, "error_code": error.code_n,
"text": error.message, "text": error.message,
@ -64,7 +60,7 @@ class FangZhouWorker(ApiModelWorker):
} }
self.logger.error(f"请求方舟 API 时发生错误:{data}") self.logger.error(f"请求方舟 API 时发生错误:{data}")
yield data yield data
elif chunk := resp.choice.message.content: elif chunk := resp.choices and resp.choices[0].message.content:
text += chunk text += chunk
yield {"error_code": 0, "text": text} yield {"error_code": 0, "text": text}
else: else:
@ -91,6 +87,16 @@ class FangZhouWorker(ApiModelWorker):
) )
def unicode_escape_data(data):
if isinstance(data, str):
return data.encode('unicode_escape').decode('ascii')
elif isinstance(data, dict):
return {key: unicode_escape_data(value) for key, value in data.items()}
elif isinstance(data, list):
return [unicode_escape_data(item) for item in data]
else:
return data
if __name__ == "__main__": if __name__ == "__main__":
import uvicorn import uvicorn
from server.utils import MakeFastAPIOffline from server.utils import MakeFastAPIOffline