{"repo": "bigai-ai/adasociety", "files": {"project/tasks/llm/utils.py": {"ee720e9979a401649f7ffbd97bdc728c35785ab5": {"t": 1785674421.581709, "n": 8039, "h": "b43518cb3e45343d", "c": "import openai\nfrom rich import print as rprint\nimport time\nfrom typing import Union\nfrom openai import OpenAI\n\n# Refer to https://platform.openai.com/docs/models/overview\nTOKEN_LIMIT_TABLE = {\n \"text-davinci-003\": 4080,\n \"gpt-3.5-turbo\": 4096,\n \"gpt-3.5-turbo-0301\": 4096,\n \"gpt-3.5-turbo-16k\": 16384,\n \"gpt-4\": 8192,\n \"gpt-4-0314\": 8192,\n \"gpt-4-32k\": 32768,\n \"gpt-4-32k-0314\": 32768,\n}\n\n\ndef convert_messages_to_prompt(messages):\n \"\"\"\n Converts a list of messages(for chat) to a prompt (for completion) for OpenAI's API.\n\n :param messages:\n :return: prompt\n \"\"\"\n prompt = \"\"\n for message in messages:\n prompt += f\"{message['content']}\\n\"\n\n return prompt\n\n\ndef retry_with_exponential_backoff(\n func,\n initial_delay: float = 1,\n exponential_base: float = 2,\n jitter: bool = True,\n max_retries: int = 10,\n # errors: tuple = (openai.error.RateLimitError,),\n):\n def wrapper(*args, **kwargs):\n num_retries = 0\n delay = initial_delay\n while True:\n # try:\n return func(*args, **kwargs)\n\n # except errors as e:\n # print(e)\n # num_retries += 1\n # if num_retries > max_retries:\n # raise Exception(\n # f\"Maximum number of retries ({max_retries}) exceeded.\"\n # )\n # delay *= exponential_base * (1 + jitter * random.random())\n # time.sleep(delay)\n # except Exception as e:\n # raise e\n\n return wrapper\n\n\nclass Module(object):\n \"\"\"\n This module is responsible for communicating with GPTs.\n \"\"\"\n\n def __init__(self,\n role_messages,\n model=\"gpt-3.5-turbo-0301\",\n retrival_method=\"recent_k\",\n K=3):\n '''\n args:\n use_similarity:\n dia_num: the num of dia use need retrival from dialog history\n '''\n\n self.model = model\n self.retrival_method = retrival_method\n self.K = K\n\n self.chat_model = True if \"gpt\" in self.model else False\n self.instruction_head_list = role_messages\n self.dialog_history_list = []\n self.current_user_message = None\n self.cache_list = None\n if self.model == 'claude':\n self.client = anthropic.Client(\n api_key=\"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\")\n\n def add_msgs_to_instruction_head(self, messages: Union[list, dict]):\n if isinstance(messages, list):\n self.instruction_head_list += messages\n elif isinstance(messages, dict):\n self.instruction_head_list += [messages]\n\n def add_msg_to_dialog_history(self, message: dict):\n self.dialog_history_list.append(message)\n\n def get_cache(self) -> list:\n if self.retrival_method == \"recent_k\":\n if self.K > 0:\n return self.dialog_history_list[-self.K:]\n else:\n return []\n else:\n return None\n\n @property\n def query_messages(self) -> list:\n return self.instruction_head_list + self.cache_list + [self.current_user_message]\n\n @retry_with_exponential_backoff\n def query(self, key, stop=None, temperature=0.0, debug_mode='Y', trace=True):\n openai.api_key = key\n rec = self.K\n if trace == True:\n self.K = 0\n self.cache_list = self.get_cache()\n messages = self.query_messages\n if trace == False:\n messages[len(messages) - 1][\n 'content'] += \" Based on the failure explanation and scene description, analyze and plan again.\"\n self.K = rec\n response = \"\"\n # print('\\n\\nmessages = \\n\\n{}\\n\\n'.format(messages))\n get_response = False\n retry_count = 0\n\n while not get_response:\n if retry_count > 3:\n rprint(\"[red][ERROR][/red]: Query GPT failed for over 3 times!\")\n return {}\n if 'claude' in self.model:\n response = self.client.messages.create(\n model=\"claude-3-haiku-20240307\",\n max_tokens=1000,\n temperature=0.0,\n system=messages[0]['content'], # <-- system prompt\n messages=[messages[1]]\n )\n try:\n if self.model in ['text-davinci-003']:\n prompt = convert_messages_to_prompt(messages)\n response = openai.Completion.create(\n model=self.model,\n prompt=prompt,\n stop=stop,\n temperature=temperature,\n max_tokens=256\n )\n time.sleep(10)\n elif 'gpt' in self.model:\n while True:\n try:\n response = openai.chat.completions.create(\n model=self.model,\n messages=messages,\n stop=stop,\n temperature=temperature,\n max_tokens=256\n )\n test = response.choices[0].message.content\n break\n except:\n print(\"try again!!!\")\n time.sleep(5)\n continue\n elif 'deepseek' in self.model:\n client = OpenAI(api_key=key, base_url=\"https://api.deepseek.com\")\n while True:\n try:\n response = client.chat.completions.create(\n model=\"deepseek-chat\",\n messages=messages,\n stream=False\n )\n test = response.choices[0].message.content\n break\n except:\n print(\"try again!!!\")\n time.sleep(10)\n continue\n elif 'claude' in self.model:\n response = self.client.messages.create(\n model=\"claude-2.1\",\n system=messages[0]['content'], # <-- system prompt\n messages=[messages[1]]\n )\n else:\n raise Exception(f\"Model {self.model} not supported.\")\n\n get_response = True\n\n except Exception as e:\n retry_count += 1\n rprint(\"[red][OPENAI ERROR][/red]:\", e)\n time.sleep(20)\n return self.parse_response(response)\n\n def parse_response(self, response):\n if self.model == 'claude':\n return response.message\n elif self.model in ['text-davinci-003']:\n return response.choices[0].text\n elif self.model in ['gpt-3.5-turbo-16k', 'gpt-3.5-turbo-0301', 'gpt-3.5-turbo', 'gpt-4', 'gpt-4-0314',\n \"deepseek-chat\", \"deepseek-reasoner\"]:\n return response.choices[0].message.content\n\n def restrict_dialogue(self):\n \"\"\"\n The limit on token length for gpt-3.5-turbo-0301 is 4096.\n If token length exceeds the limit, we will remove the oldest messages.\n \"\"\"\n limit = TOKEN_LIMIT_TABLE[self.model]\n print(f'Current token: {self.prompt_token_length}')\n while self.prompt_token_length >= limit:\n self.cache_list.pop(0)\n self.cache_list.pop(0)\n self.cache_list.pop(0)\n self.cache_list.pop(0)\n print(f'Update token: {self.prompt_token_length}')\n\n def reset(self):\n self.dialog_history_list = []"}}}}