mirror of
https://github.com/RYDE-WORK/Langchain-Chatchat.git
synced 2026-02-06 06:49:48 +08:00
百川API示例处理
百川API调用示例
This commit is contained in:
parent
5c74a28de9
commit
c6d91c3e3d
@ -1,7 +1,7 @@
|
|||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
import hashlib
|
import hashlib
|
||||||
|
import requests
|
||||||
from fastchat.conversation import Conversation
|
from fastchat.conversation import Conversation
|
||||||
from server.model_workers.base import *
|
from server.model_workers.base import *
|
||||||
from server.utils import get_httpx_client
|
from server.utils import get_httpx_client
|
||||||
@ -32,61 +32,58 @@ class BaiChuanWorker(ApiModelWorker):
|
|||||||
kwargs.setdefault("context_len", 32768)
|
kwargs.setdefault("context_len", 32768)
|
||||||
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:
|
||||||
params.load_config(self.model_names[0])
|
params.load_config(self.model_names[0])
|
||||||
|
|
||||||
url = "https://api.baichuan-ai.com/v1/stream/chat"
|
url = "https://api.baichuan-ai.com/v1/chat/completions"
|
||||||
data = {
|
data = {
|
||||||
"model": params.version,
|
"model": params.version,
|
||||||
"messages": params.messages,
|
"messages": params.messages,
|
||||||
"parameters": {"temperature": params.temperature}
|
"stream": False,
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
json_data = json.dumps(data)
|
|
||||||
time_stamp = int(time.time())
|
|
||||||
signature = calculate_md5(params.secret_key + json_data + str(time_stamp))
|
|
||||||
headers = {
|
headers = {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"Authorization": "Bearer " + params.api_key,
|
"Authorization": "Bearer " + params.api_key,
|
||||||
"X-BC-Request-Id": "your requestId",
|
|
||||||
"X-BC-Timestamp": str(time_stamp),
|
|
||||||
"X-BC-Signature": signature,
|
|
||||||
"X-BC-Sign-Algo": "MD5",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
text = ""
|
response = requests.post(url, headers=headers, json=data)
|
||||||
if log_verbose:
|
if response.status_code == 200:
|
||||||
logger.info(f'{self.__class__.__name__}:json_data: {json_data}')
|
print("请求成功!"+response.text)
|
||||||
logger.info(f'{self.__class__.__name__}:url: {url}')
|
result = json.loads(response.text)
|
||||||
logger.info(f'{self.__class__.__name__}:headers: {headers}')
|
textMsg=""
|
||||||
|
result["choices"][0]["delta"]=result["choices"][0]["message"]
|
||||||
with get_httpx_client() as client:
|
if 'choices' in result:
|
||||||
with client.stream("POST", url, headers=headers, json=data) as response:
|
textMsg += result["choices"][0]["message"]["content"]
|
||||||
for line in response.iter_lines():
|
data = {
|
||||||
if not line.strip():
|
"error_code": response.status_code,
|
||||||
continue
|
"text": textMsg,
|
||||||
resp = json.loads(line)
|
"choices":result["choices"],
|
||||||
if resp["code"] == 0:
|
"model":result["model"],
|
||||||
text += resp["data"]["messages"][-1]["content"]
|
"object":result["object"],
|
||||||
yield {
|
"object":result["object"],
|
||||||
"error_code": resp["code"],
|
"created":result["created"],
|
||||||
"text": text
|
"id":result["id"],
|
||||||
}
|
}
|
||||||
else:
|
|
||||||
data = {
|
yield data
|
||||||
"error_code": resp["code"],
|
|
||||||
"text": resp["msg"],
|
else:
|
||||||
|
|
||||||
|
data = {
|
||||||
|
"error_code": response.status_code,
|
||||||
|
"text":response.text,
|
||||||
"error": {
|
"error": {
|
||||||
"message": resp["msg"],
|
"message": response.text,
|
||||||
"type": "invalid_request_error",
|
"type": "invalid_request_error",
|
||||||
"param": None,
|
"param": None,
|
||||||
"code": None,
|
"code": None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.logger.error(f"请求百川 API 时发生错误:{data}")
|
self.logger.error(f"请求百川 API 时发生错误:{data}")
|
||||||
yield data
|
yield data
|
||||||
|
|
||||||
def get_embeddings(self, params):
|
def get_embeddings(self, params):
|
||||||
print("embedding")
|
print("embedding")
|
||||||
print(params)
|
print(params)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user