1 line
6.4 KiB
JSON
1 line
6.4 KiB
JSON
{"repo": "alufron/pingpong3d", "files": {"LLaDA/opencompass/opencompass/models/ai360_api.py": {"7f482acec185f6fecb7ad8c0aa08b9e2376bbf23": {"t": 1785768161.859849, "n": 6105, "h": "f0d3a1c18c6df379", "c": "from concurrent.futures import ThreadPoolExecutor\nfrom typing import Dict, List, Optional, Union\n\nimport requests\n\nfrom opencompass.utils.prompt import PromptList\n\nfrom .base_api import BaseAPIModel\n\nPromptType = Union[PromptList, str]\n\n\nclass AI360GPT(BaseAPIModel):\n \"\"\"Model wrapper around 360 GPT.\n\n Documentations: https://ai.360.com/platform/docs/overview\n\n Args:\n path (str): Model name\n key (str): Provide API Key\n url (str): Provided URL\n query_per_second (int): The maximum queries allowed per second\n between two consecutive calls of the API. Defaults to 2.\n max_seq_len (int): Unused here.\n meta_template (Dict, optional): The model's meta prompt\n template if needed, in case the requirement of injecting or\n wrapping of any meta instructions.\n retry (int): Number of retires if the API call fails. Defaults to 2.\n \"\"\"\n\n def __init__(\n self,\n path: str, # model name, e.g.: 360GPT_S2_V9\n key: str,\n url: str = 'https://api.360.cn/v1/chat/completions',\n query_per_second: int = 2,\n max_seq_len: int = 2048,\n meta_template: Optional[Dict] = None,\n retry: int = 2,\n generation_kwargs: Dict = {\n 'temperature': 0.9,\n 'max_tokens': 2048,\n 'top_p': 0.5,\n 'tok_k': 0,\n 'repetition_penalty': 1.05,\n }): # noqa E125\n super().__init__(path=path,\n max_seq_len=max_seq_len,\n query_per_second=query_per_second,\n meta_template=meta_template,\n retry=retry,\n generation_kwargs=generation_kwargs)\n self.headers = {\n 'Authorization': f'Bearer {key}',\n 'Content-Type': 'application/json',\n }\n self.model = path\n self.url = url\n\n def generate(\n self,\n inputs: List[PromptType],\n max_out_len: int = 512,\n ) -> List[str]:\n \"\"\"Generate results given a list of inputs.\n\n Args:\n inputs (List[PromptType]): A list of strings or PromptDicts.\n The PromptDict should be organized in OpenCompass'\n API format.\n max_out_len (int): The maximum length of the output.\n\n Returns:\n List[str]: A list of generated strings.\n \"\"\"\n with ThreadPoolExecutor() as executor:\n results = list(\n executor.map(self._generate, inputs,\n [max_out_len] * len(inputs)))\n self.flush()\n return results\n\n def _generate(\n self,\n input: PromptType,\n max_out_len: int = 512,\n ) -> str:\n \"\"\"Generate results given an input.\n\n Args:\n inputs (PromptType): A string or PromptDict.\n The PromptDict should be organized in OpenCompass'\n API format.\n max_out_len (int): The maximum length of the output.\n\n Returns:\n str: The generated string.\n \"\"\"\n assert isinstance(input, (str, PromptList))\n\n if isinstance(input, str):\n messages = [{'role': 'user', 'content': input}]\n else:\n messages = []\n for item in input:\n msg = {'content': item['prompt']}\n if item['role'] == 'HUMAN':\n msg['role'] = 'user'\n elif item['role'] == 'BOT':\n msg['role'] = 'assistant'\n elif item['role'] == 'SYSTEM':\n msg['role'] = 'system'\n messages.append(msg)\n\n data = {\n 'model': self.model,\n 'messages': messages,\n 'stream': False,\n # \"user\": \"OpenCompass\"\n }\n\n data.update(self.generation_kwargs)\n\n max_num_retries = 0\n while max_num_retries < self.retry:\n self.acquire()\n # payload = json.dumps(data)\n try:\n raw_response = requests.request('POST',\n url=self.url,\n headers=self.headers,\n json=data)\n except Exception as e:\n self.release()\n print(e)\n max_num_retries += 1\n continue\n response = raw_response.json()\n self.release()\n\n if response is None:\n print('Connection error, reconnect.')\n # if connect error, frequent requests will casuse\n # continuous unstable network, therefore wait here\n # to slow down the request\n self.wait()\n continue\n if raw_response.status_code == 200:\n msg = response['choices'][0]['message']['content'].strip()\n self.logger.debug(f'Generated: {msg}')\n return msg\n\n # sensitive content, prompt overlength, network error\n # or illegal prompt\n if raw_response.status_code in [400, 401, 402, 429, 500]:\n if 'error' not in response:\n print(raw_response.status_code)\n print(raw_response.text)\n continue\n print(response)\n # tpm(token per minitue) limit\n if response['error']['code'] == '1005':\n self.logger.debug('tpm limit, ignoring')\n continue\n elif response['error']['code'] == '1001':\n msg = '参数错误:messages参数过长或max_tokens参数值过大'\n self.logger.debug(f'Generated: {msg}')\n return msg\n else:\n print(response)\n\n self.logger.error('Find error message in response: ',\n str(response['error']))\n\n print(raw_response)\n max_num_retries += 1\n\n raise RuntimeError(raw_response.text)\n"}}}} |