Skip to content

Commit

Permalink
[Feature] Update API implementation (#834)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonysy committed Jan 24, 2024
1 parent 2ee8e8a commit 793e32c
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 55 deletions.
48 changes: 18 additions & 30 deletions opencompass/models/baichuan_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import hashlib
import json
import time
from concurrent.futures import ThreadPoolExecutor
from typing import Dict, List, Optional, Union
Expand All @@ -22,7 +20,6 @@ class BaiChuan(BaseAPIModel):
path (str): The name of Baichuan model.
e.g. `Baichuan2-53B`
api_key (str): Provided api key
secretkey (str): secretkey in order to obtain access_token
url (str): Provide url
query_per_second (int): The maximum queries allowed per second
between two consecutive calls of the API. Defaults to 1.
Expand All @@ -37,7 +34,6 @@ def __init__(
self,
path: str,
api_key: str,
secret_key: str,
url: str,
query_per_second: int = 2,
max_seq_len: int = 2048,
Expand All @@ -48,6 +44,7 @@ def __init__(
'top_p': 0.85,
'top_k': 5,
'with_search_enhance': False,
'stream': False,
}): # noqa E125
super().__init__(path=path,
max_seq_len=max_seq_len,
Expand All @@ -57,7 +54,6 @@ def __init__(
generation_kwargs=generation_kwargs)

self.api_key = api_key
self.secret_key = secret_key
self.url = url
self.model = path

Expand Down Expand Up @@ -119,50 +115,42 @@ def _generate(
data = {'model': self.model, 'messages': messages}
data.update(self.generation_kwargs)

def calculate_md5(input_string):
md5 = hashlib.md5()
md5.update(input_string.encode('utf-8'))
encrypted = md5.hexdigest()
return encrypted

json_data = json.dumps(data)
time_stamp = int(time.time())
signature = calculate_md5(self.secret_key + json_data +
str(time_stamp))

headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + self.api_key,
'X-BC-Request-Id': 'your requestId',
'X-BC-Timestamp': str(time_stamp),
'X-BC-Signature': signature,
'X-BC-Sign-Algo': 'MD5',
}

max_num_retries = 0
while max_num_retries < self.retry:
self.acquire()
raw_response = requests.request('POST',
url=self.url,
headers=headers,
json=data)
response = raw_response.json()
self.release()
try:
raw_response = requests.request('POST',
url=self.url,
headers=headers,
json=data)
response = raw_response.json()
except Exception as err:
print('Request Error:{}'.format(err))
time.sleep(3)
continue

self.release()
# print(response.keys())
# print(response['choices'][0]['message']['content'])
if response is None:
print('Connection error, reconnect.')
# if connect error, frequent requests will casuse
# continuous unstable network, therefore wait here
# to slow down the request
self.wait()
continue
if raw_response.status_code == 200 and response['code'] == 0:
if raw_response.status_code == 200:

msg = response['data']['messages'][0]['content']
msg = response['choices'][0]['message']['content']
return msg

if response['code'] != 0:
print(response)
if raw_response.status_code != 200:
print(raw_response)
time.sleep(1)
continue
print(response)
Expand Down
28 changes: 21 additions & 7 deletions opencompass/models/baidu_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ def __init__(self,
self.secretkey = secretkey
self.key = key
self.url = url
access_token, _ = self._generate_access_token()
self.access_token = access_token
print(access_token)

def _generate_access_token(self):
try:
Expand Down Expand Up @@ -154,12 +157,18 @@ def _generate(
max_num_retries = 0
while max_num_retries < self.retry:
self.acquire()
access_token, _ = self._generate_access_token()
raw_response = requests.request('POST',
url=self.url + access_token,
headers=self.headers,
json=data)
response = raw_response.json()
try:
raw_response = requests.request('POST',
url=self.url +
self.access_token,
headers=self.headers,
json=data)
response = raw_response.json()
except Exception as err:
print('Request Error:{}'.format(err))
time.sleep(3)
continue

self.release()

if response is None:
Expand All @@ -176,6 +185,10 @@ def _generate(
except KeyError:
print(response)
self.logger.error(str(response['error_code']))
if response['error_code'] == 336007:
# exceed max length
return ''

time.sleep(1)
continue

Expand All @@ -189,7 +202,8 @@ def _generate(
or response['error_code'] == 216100
or response['error_code'] == 336001
or response['error_code'] == 336003
or response['error_code'] == 336000):
or response['error_code'] == 336000
or response['error_code'] == 336007):
print(response['error_msg'])
return ''
print(response)
Expand Down
23 changes: 14 additions & 9 deletions opencompass/models/minimax_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def _generate(
Args:
inputs (str or PromptList): A string or PromptDict.
The PromptDict should be organized in OpenCompass'
The PromptDict should be organized in Test'
API format.
max_out_len (int): The maximum length of the output.
Expand All @@ -102,7 +102,7 @@ def _generate(
if isinstance(input, str):
messages = [{
'sender_type': 'USER',
'sender_name': 'OpenCompass',
'sender_name': 'Test',
'text': input
}]
else:
Expand All @@ -111,7 +111,7 @@ def _generate(
msg = {'text': item['prompt']}
if item['role'] == 'HUMAN':
msg['sender_type'] = 'USER'
msg['sender_name'] = 'OpenCompass'
msg['sender_name'] = 'Test'
elif item['role'] == 'BOT':
msg['sender_type'] = 'BOT'
msg['sender_name'] = 'MM智能助理'
Expand All @@ -135,15 +135,19 @@ def _generate(
'messages':
messages
}

max_num_retries = 0
while max_num_retries < self.retry:
self.acquire()
raw_response = requests.request('POST',
url=self.url,
headers=self.headers,
json=data)
response = raw_response.json()
try:
raw_response = requests.request('POST',
url=self.url,
headers=self.headers,
json=data)
response = raw_response.json()
except Exception as err:
print('Request Error:{}'.format(err))
time.sleep(3)
continue
self.release()

if response is None:
Expand All @@ -157,6 +161,7 @@ def _generate(
# msg = json.load(response.text)
# response
msg = response['reply']
# msg = response['choices']['messages']['text']
return msg
# sensitive content, prompt overlength, network error
# or illegal prompt
Expand Down
17 changes: 12 additions & 5 deletions opencompass/models/moonshot_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,15 @@ def _generate(
max_num_retries = 0
while max_num_retries < self.retry:
self.acquire()
raw_response = requests.request('POST',
url=self.url,
headers=self.headers,
json=data)
try:
raw_response = requests.request('POST',
url=self.url,
headers=self.headers,
json=data)
except Exception as err:
print('Request Error:{}'.format(err))
time.sleep(2)
continue

response = raw_response.json()
self.release()
Expand All @@ -153,12 +158,14 @@ def _generate(
elif raw_response.status_code == 400:
print(messages, response)
print('请求失败,状态码:', raw_response)
msg = 'The request was rejected because high risk'
return msg
time.sleep(1)
continue
elif raw_response.status_code == 429:
print(messages, response)
print('请求失败,状态码:', raw_response)
time.sleep(3)
time.sleep(5)
continue

max_num_retries += 1
Expand Down
23 changes: 19 additions & 4 deletions opencompass/models/qwen_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ def _generate(
msg['role'] = 'user'
elif item['role'] == 'BOT':
msg['role'] = 'assistant'
elif item['role'] == 'SYSTEM':
msg['role'] = 'system'

messages.append(msg)
data = {'messages': messages}
Expand All @@ -117,10 +119,16 @@ def _generate(
max_num_retries = 0
while max_num_retries < self.retry:
self.acquire()
response = self.dashscope.Generation.call(
model=self.path,
**data,
)
try:
response = self.dashscope.Generation.call(
model=self.path,
**data,
)
except Exception as err:
print('Request Error:{}'.format(err))
time.sleep(1)
continue

self.release()

if response is None:
Expand All @@ -140,6 +148,13 @@ def _generate(
self.logger.error(str(response.status_code))
time.sleep(1)
continue
if response.status_code == 429:
print('Rate limited')
time.sleep(2)
continue
if response.status_code == 400:
msg = 'Output data may contain inappropriate content.'
return msg

if ('Range of input length should be ' in response.message
or # input too long
Expand Down
1 change: 1 addition & 0 deletions requirements/api.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dashscope # Qwen
sseclient-py==1.7.2
volcengine # bytedance
websocket-client
Expand Down

0 comments on commit 793e32c

Please sign in to comment.