Skip to content

Commit

Permalink
更新火山方舟调用 (#3564)
Browse files Browse the repository at this point in the history
* 更新火山方舟调用
  • Loading branch information
An0nymous0 committed Apr 25, 2024
1 parent cf04951 commit 9c0820c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 27 deletions.
2 changes: 1 addition & 1 deletion configs/model_config.py.example
Expand Up @@ -81,7 +81,7 @@ ONLINE_LLM_MODEL = {

# 火山方舟 API,文档参考 https://www.volcengine.com/docs/82379
"fangzhou-api": {
"version": "chatglm-6b-model",
"version": "", # 对应火山方舟的 endpoint_id
"version_url": "",
"api_key": "",
"secret_key": "",
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Expand Up @@ -43,7 +43,7 @@ llama-index==0.9.35
# beautifulsoup4==4.12.2
# pysrt==1.1.2
# 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
# pymilvus==2.3.6
# psycopg2==2.9.9
Expand Down
2 changes: 1 addition & 1 deletion requirements_api.txt
Expand Up @@ -50,7 +50,7 @@ pyjwt==2.8.0
# duckduckgo-search~=3.9.9
# metaphor-python~=0.1.23

# volcengine>=1.0.119
# volcengine>=1.0.134
# pymilvus==2.3.6
# psycopg2==2.9.9
# pgvector>=0.2.4
Expand Down
2 changes: 1 addition & 1 deletion requirements_lite.txt
Expand Up @@ -30,7 +30,7 @@ youtube-search~=2.1.2
duckduckgo-search~=3.9.9
metaphor-python~=0.1.23
watchdog~=3.0.0
# volcengine>=1.0.119
# volcengine>=1.0.134
# pymilvus>=2.3.4
# psycopg2==2.9.9
# pgvector>=0.2.4
Expand Down
52 changes: 29 additions & 23 deletions server/model_workers/fangzhou.py
Expand Up @@ -24,9 +24,8 @@ def __init__(
kwargs.setdefault("context_len", 16384)
super().__init__(**kwargs)
self.version = version

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])
maas = MaasService('maas-api.ml-platform-cn-beijing.volces.com', 'cn-beijing')
Expand All @@ -35,9 +34,6 @@ def do_chat(self, params: ApiChatParams) -> Dict:

# document: "https://www.volcengine.com/docs/82379/1099475"
req = {
"model": {
"name": params.version,
},
"parameters": {
# 这里的参数仅为示例,具体可用的参数请参考具体模型的 API 说明
"max_new_tokens": params.max_tokens,
Expand All @@ -49,24 +45,24 @@ def do_chat(self, params: ApiChatParams) -> Dict:
text = ""
if log_verbose:
self.logger.info(f'{self.__class__.__name__}:maas: {maas}')
for resp in maas.stream_chat(req):
if error := resp.error:
if error.code_n > 0:
data = {
"error_code": error.code_n,
"text": error.message,
"error": {
"message": error.message,
"type": "invalid_request_error",
"param": None,
"code": None,
}
for resp in maas.stream_chat(params.version, unicode_escape_data(req)):
error = resp.error
if error and error.code_n > 0:
data = {
"error_code": error.code_n,
"text": error.message,
"error": {
"message": error.message,
"type": "invalid_request_error",
"param": None,
"code": None,
}
self.logger.error(f"请求方舟 API 时发生错误:{data}")
yield data
elif chunk := resp.choice.message.content:
text += chunk
yield {"error_code": 0, "text": text}
}
self.logger.error(f"请求方舟 API 时发生错误:{data}")
yield data
elif chunk := resp.choices and resp.choices[0].message.content:
text += chunk
yield {"error_code": 0, "text": text}
else:
data = {
"error_code": 500,
Expand All @@ -91,6 +87,16 @@ def make_conv_template(self, conv_template: str = None, model_path: str = None)
)


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__":
import uvicorn
from server.utils import MakeFastAPIOffline
Expand All @@ -102,4 +108,4 @@ def make_conv_template(self, conv_template: str = None, model_path: str = None)
)
sys.modules["fastchat.serve.model_worker"].worker = worker
MakeFastAPIOffline(app)
uvicorn.run(app, port=21005)
uvicorn.run(app, port=21005)

0 comments on commit 9c0820c

Please sign in to comment.