Files
hack/tools/scripts/llm-key-hunter/results/_cache/content/cant-access-rediska0123_uncertainty4reasoning.json
T

1 line
21 KiB
JSON

{"repo": "cant-access-rediska0123/uncertainty4reasoning", "files": {"synthetic_dataset_generation/utils/chat.py": {"fac0c16da74d55f71cc9fa469cc08e88c9604ee4": {"t": 1785745988.3887835, "n": 10159, "h": "4f14ecdc93e1b274", "c": "import openai\nimport os\nimport sys\nimport time\nimport logging\nimport diskcache as dc\nimport threading\nfrom openai import OpenAI\n\nlog = logging.getLogger()\n\n\n# Reuse the same generation configuration style as in step_fact_check_thinking\nGENERATION_CONFIGS = {\n 'openai/gpt-oss-120b': {\n 'extra_body': {\"reasoning_effort\": 'medium'},\n },\n \"Qwen/Qwen3-8B\": {\n \"temperature\": 0.6,\n \"top_p\": 0.95,\n \"extra_body\": {\"enable_thinking\": True, \"top_k\": 20},\n },\n \"microsoft/Phi-4-reasoning-plus\": {\n \"temperature\": 0.8,\n \"top_p\": 0.95,\n \"extra_body\": {\"enable_thinking\": True, \"top_k\": 50},\n },\n \"gpt-5.1-2025-11-13\": {\"reasoning_effort\": 'medium'},\n \"gpt-5-mini-2025-08-07\": {\"reasoning_effort\": 'medium'},\n}\n\n\nclass LocalChat:\n \"\"\"\n Local OpenAI-compatible chat client with persistent disk cache.\n \"\"\"\n\n def __init__(\n self,\n model: str = \"openai/gpt-oss-120b\",\n base_url: str = \"http://localhost:8000/v1\",\n cache_path: str = os.path.expanduser(\"~\") + \"/.cache\",\n system_prompt: str = None,\n generation_config: dict = None,\n ):\n self.cache_path = os.path.join(cache_path, \"openai_chat_cache.diskcache\")\n if not os.path.exists(cache_path):\n os.makedirs(cache_path)\n\n self.base_url = base_url\n self.model = model\n self.generation_config = GENERATION_CONFIGS[model] if generation_config is None else generation_config\n self.system_prompt = system_prompt\n self.client = OpenAI(base_url=self.base_url, api_key='EMPTY')\n\n cache_settings = dc.DEFAULT_SETTINGS.copy()\n cache_settings[\"eviction_policy\"] = \"none\"\n cache_settings[\"size_limit\"] = int(1e12)\n cache_settings[\"cull_limit\"] = 0\n self.cache = dc.Cache(self.cache_path, **cache_settings)\n self._lock = threading.Lock()\n\n def ask(self, message: str, **kwargs) -> str:\n reply, reasoning_content = self.cache.get((self.model, message), ('', ''))\n if reply == '':\n if self.system_prompt is not None:\n messages = [\n {\"role\": \"system\", \"content\": self.system_prompt},\n {\"role\": \"user\", \"content\": message},\n ]\n else:\n messages = [\n {\"role\": \"user\", \"content\": message},\n ]\n chat = self._send_request(messages, self.generation_config)\n if chat is None:\n reply, reasoning_content = '', ''\n else:\n reasoning_content = getattr(chat.choices[0].message, \"reasoning_content\", \"\")\n reply = chat.choices[0].message.content\n\n with self._lock:\n self.cache[(self.model, message)] = (reply, reasoning_content)\n else:\n # pass\n print(\"Loaded from cache\")\n\n if not reply:\n print(\"Reply is empty\")\n print(f\"Chat object: {chat}\")\n\n low = reply.lower()\n if \"please provide\" in low or \"to assist you\" in low or \"as an ai language model\" in low:\n return \"\", \"\"\n\n return reply, reasoning_content\n\n def _send_request(self, messages, generation_config):\n try:\n response = self.client.chat.completions.create(\n model=self.model,\n messages=messages,\n **generation_config,\n )\n except Exception as e:\n log.info(f\"Request to OpenAI failed with exception: {e}.\")\n return None\n\n return response\n\n\nclass DeepSeekChat:\n def __init__(\n self,\n cache_path: str,\n api_base: str | None = \"https://api.deepseek.com/v1\",\n model: str = \"deepseek-reasoner\",\n api_key: str | None = None,\n wait_times: tuple = (5, 10),\n ):\n if api_key is None:\n api_key = os.environ.get(\"DEEPSEEK_API_KEY\", None)\n self.api_key = api_key\n self.api_base = api_base\n self.model = model\n if cache_path is None:\n cache_path = '~/.cache'\n self.cache_path = os.path.join(cache_path, \"deepseek_chat_cache.diskcache\")\n if not os.path.exists(cache_path):\n os.makedirs(cache_path)\n self.wait_times = wait_times\n \n # Initialize cache with proper settings\n cache_settings = dc.DEFAULT_SETTINGS.copy()\n cache_settings[\"eviction_policy\"] = \"none\"\n cache_settings[\"size_limit\"] = int(1e12)\n cache_settings[\"cull_limit\"] = 0\n self.cache = dc.Cache(self.cache_path, **cache_settings)\n self._lock = threading.Lock()\n print(f\"Using {self.api_base}\")\n self.client = openai.OpenAI(base_url=self.api_base, api_key=self.api_key)\n\n def ask(self, message: str, json_output=False) -> str:\n # First try to get from cache without lock\n reply = self.cache.get((self.model, message), '')\n \n if reply == '':\n if self.api_key is None:\n raise Exception(\"Cant ask DeepSeek without token.\")\n messages = [\n {\"role\": \"user\", \"content\": message},\n ]\n chat = self._send_request(messages, json_output)\n if chat is None:\n reply = \"\"\n else:\n reply = chat.choices[0].message.content\n # Only lock when writing to cache\n with self._lock:\n self.cache[(self.model, message)] = reply\n\n if any(x in reply.lower() for x in [\"please provide\", \"to assist you\", \"as an ai language model\"]):\n return \"\"\n\n return reply\n\n def _send_request(self, messages, json_output=False):\n chat_args = {\n 'model': self.model,\n 'messages': messages,\n 'temperature': 0.6,\n }\n if json_output:\n chat_args['response_format'] = {'type': 'json_object'}\n for i in range(len(self.wait_times)):\n try:\n return self.client.chat.completions.create(**chat_args)\n except Exception as e:\n sleep_time = self.wait_times[i]\n log.info(\n f\"Request failed with exception: {e}. Retry #{i}/5 after {sleep_time} seconds.\"\n )\n time.sleep(sleep_time)\n try:\n return self.client.chat.completions.create(**chat_args)\n except Exception as e:\n sys.stderr.write(f'Error: {e}')\n return None\n\n def __del__(self):\n \"\"\"Cleanup method to properly close the cache when the instance is destroyed.\"\"\"\n if hasattr(self, 'cache'):\n self.cache.close()\n\n\nclass OpenAIChat:\n \"\"\"\n Allows for the implementation of a singleton class to chat with OpenAI model for dataset marking.\n \"\"\"\n\n def __init__(\n self,\n openai_model: str = \"gpt-4o\",\n base_url: str = None,\n cache_path: str = os.path.expanduser(\"~\") + \"/.cache\",\n generation_config: dict = {},\n system_prompt: str = \"You are an intelligent assistant.\",\n ):\n \"\"\"\n Parameters\n ----------\n openai_model: str\n the model to use in OpenAI to chat.\n \"\"\"\n api_key = os.environ.get(\"OPENAI_API_KEY\", None)\n if api_key is not None:\n openai.api_key = api_key\n self.openai_model = openai_model\n\n self.cache_path = os.path.join(cache_path, \"openai_chat_cache.diskcache\")\n if not os.path.exists(cache_path):\n os.makedirs(cache_path)\n\n self.base_url = base_url\n self.generation_config = GENERATION_CONFIGS[openai_model] if generation_config is None else generation_config\n self.system_prompt = system_prompt\n\n def ask(self, message: str, json_output=False) -> str:\n cache_settings = dc.DEFAULT_SETTINGS.copy()\n cache_settings[\"eviction_policy\"] = \"none\"\n cache_settings[\"size_limit\"] = int(1e12)\n cache_settings[\"cull_limit\"] = 0\n openai_responses = dc.Cache(self.cache_path, **cache_settings)\n\n if (self.openai_model, message) in openai_responses:\n reply = openai_responses[(self.openai_model, message)]\n\n else:\n # Ask openai\n if openai.api_key is None:\n raise Exception(\n \"Cant ask openAI without token. \"\n \"Please specify OPENAI_API_KEY in environment parameters.\"\n )\n if self.system_prompt is not None:\n messages = [\n {\"role\": \"system\", \"content\": self.system_prompt},\n {\"role\": \"user\", \"content\": message},\n ]\n else:\n messages = [\n {\"role\": \"user\", \"content\": message},\n ]\n chat = self._send_request(messages)\n reply = chat.choices[0].message.content\n\n openai_responses[(self.openai_model, message)] = reply\n openai_responses.close()\n\n if \"please provide\" in reply.lower():\n return \"\"\n if \"to assist you\" in reply.lower():\n return \"\"\n if \"as an ai language model\" in reply.lower():\n return \"\"\n\n return reply\n\n def _send_request(self, messages):\n sleep_time_values = (5, 10, 30, 60, 120)\n for i in range(len(sleep_time_values)):\n try:\n return openai.OpenAI(base_url=self.base_url).chat.completions.create(\n model=self.openai_model,\n messages=messages,\n **self.generation_config,\n )\n except Exception as e:\n sleep_time = sleep_time_values[i]\n log.info(\n f\"Request to OpenAI failed with exception: {e}. Retry #{i}/5 after {sleep_time} seconds.\"\n )\n time.sleep(sleep_time)\n\n return openai.OpenAI(base_url=self.base_url).chat.completions.create(\n model=self.openai_model,\n messages=messages,\n **self.generation_config,\n )"}, "986db929177d1416687d4d55e7e72b722c310938": {"t": 1785758562.3558455, "n": 10159, "h": "4f14ecdc93e1b274", "c": "import openai\nimport os\nimport sys\nimport time\nimport logging\nimport diskcache as dc\nimport threading\nfrom openai import OpenAI\n\nlog = logging.getLogger()\n\n\n# Reuse the same generation configuration style as in step_fact_check_thinking\nGENERATION_CONFIGS = {\n 'openai/gpt-oss-120b': {\n 'extra_body': {\"reasoning_effort\": 'medium'},\n },\n \"Qwen/Qwen3-8B\": {\n \"temperature\": 0.6,\n \"top_p\": 0.95,\n \"extra_body\": {\"enable_thinking\": True, \"top_k\": 20},\n },\n \"microsoft/Phi-4-reasoning-plus\": {\n \"temperature\": 0.8,\n \"top_p\": 0.95,\n \"extra_body\": {\"enable_thinking\": True, \"top_k\": 50},\n },\n \"gpt-5.1-2025-11-13\": {\"reasoning_effort\": 'medium'},\n \"gpt-5-mini-2025-08-07\": {\"reasoning_effort\": 'medium'},\n}\n\n\nclass LocalChat:\n \"\"\"\n Local OpenAI-compatible chat client with persistent disk cache.\n \"\"\"\n\n def __init__(\n self,\n model: str = \"openai/gpt-oss-120b\",\n base_url: str = \"http://localhost:8000/v1\",\n cache_path: str = os.path.expanduser(\"~\") + \"/.cache\",\n system_prompt: str = None,\n generation_config: dict = None,\n ):\n self.cache_path = os.path.join(cache_path, \"openai_chat_cache.diskcache\")\n if not os.path.exists(cache_path):\n os.makedirs(cache_path)\n\n self.base_url = base_url\n self.model = model\n self.generation_config = GENERATION_CONFIGS[model] if generation_config is None else generation_config\n self.system_prompt = system_prompt\n self.client = OpenAI(base_url=self.base_url, api_key='EMPTY')\n\n cache_settings = dc.DEFAULT_SETTINGS.copy()\n cache_settings[\"eviction_policy\"] = \"none\"\n cache_settings[\"size_limit\"] = int(1e12)\n cache_settings[\"cull_limit\"] = 0\n self.cache = dc.Cache(self.cache_path, **cache_settings)\n self._lock = threading.Lock()\n\n def ask(self, message: str, **kwargs) -> str:\n reply, reasoning_content = self.cache.get((self.model, message), ('', ''))\n if reply == '':\n if self.system_prompt is not None:\n messages = [\n {\"role\": \"system\", \"content\": self.system_prompt},\n {\"role\": \"user\", \"content\": message},\n ]\n else:\n messages = [\n {\"role\": \"user\", \"content\": message},\n ]\n chat = self._send_request(messages, self.generation_config)\n if chat is None:\n reply, reasoning_content = '', ''\n else:\n reasoning_content = getattr(chat.choices[0].message, \"reasoning_content\", \"\")\n reply = chat.choices[0].message.content\n\n with self._lock:\n self.cache[(self.model, message)] = (reply, reasoning_content)\n else:\n # pass\n print(\"Loaded from cache\")\n\n if not reply:\n print(\"Reply is empty\")\n print(f\"Chat object: {chat}\")\n\n low = reply.lower()\n if \"please provide\" in low or \"to assist you\" in low or \"as an ai language model\" in low:\n return \"\", \"\"\n\n return reply, reasoning_content\n\n def _send_request(self, messages, generation_config):\n try:\n response = self.client.chat.completions.create(\n model=self.model,\n messages=messages,\n **generation_config,\n )\n except Exception as e:\n log.info(f\"Request to OpenAI failed with exception: {e}.\")\n return None\n\n return response\n\n\nclass DeepSeekChat:\n def __init__(\n self,\n cache_path: str,\n api_base: str | None = \"https://api.deepseek.com/v1\",\n model: str = \"deepseek-reasoner\",\n api_key: str | None = None,\n wait_times: tuple = (5, 10),\n ):\n if api_key is None:\n api_key = os.environ.get(\"DEEPSEEK_API_KEY\", None)\n self.api_key = api_key\n self.api_base = api_base\n self.model = model\n if cache_path is None:\n cache_path = '~/.cache'\n self.cache_path = os.path.join(cache_path, \"deepseek_chat_cache.diskcache\")\n if not os.path.exists(cache_path):\n os.makedirs(cache_path)\n self.wait_times = wait_times\n \n # Initialize cache with proper settings\n cache_settings = dc.DEFAULT_SETTINGS.copy()\n cache_settings[\"eviction_policy\"] = \"none\"\n cache_settings[\"size_limit\"] = int(1e12)\n cache_settings[\"cull_limit\"] = 0\n self.cache = dc.Cache(self.cache_path, **cache_settings)\n self._lock = threading.Lock()\n print(f\"Using {self.api_base}\")\n self.client = openai.OpenAI(base_url=self.api_base, api_key=self.api_key)\n\n def ask(self, message: str, json_output=False) -> str:\n # First try to get from cache without lock\n reply = self.cache.get((self.model, message), '')\n \n if reply == '':\n if self.api_key is None:\n raise Exception(\"Cant ask DeepSeek without token.\")\n messages = [\n {\"role\": \"user\", \"content\": message},\n ]\n chat = self._send_request(messages, json_output)\n if chat is None:\n reply = \"\"\n else:\n reply = chat.choices[0].message.content\n # Only lock when writing to cache\n with self._lock:\n self.cache[(self.model, message)] = reply\n\n if any(x in reply.lower() for x in [\"please provide\", \"to assist you\", \"as an ai language model\"]):\n return \"\"\n\n return reply\n\n def _send_request(self, messages, json_output=False):\n chat_args = {\n 'model': self.model,\n 'messages': messages,\n 'temperature': 0.6,\n }\n if json_output:\n chat_args['response_format'] = {'type': 'json_object'}\n for i in range(len(self.wait_times)):\n try:\n return self.client.chat.completions.create(**chat_args)\n except Exception as e:\n sleep_time = self.wait_times[i]\n log.info(\n f\"Request failed with exception: {e}. Retry #{i}/5 after {sleep_time} seconds.\"\n )\n time.sleep(sleep_time)\n try:\n return self.client.chat.completions.create(**chat_args)\n except Exception as e:\n sys.stderr.write(f'Error: {e}')\n return None\n\n def __del__(self):\n \"\"\"Cleanup method to properly close the cache when the instance is destroyed.\"\"\"\n if hasattr(self, 'cache'):\n self.cache.close()\n\n\nclass OpenAIChat:\n \"\"\"\n Allows for the implementation of a singleton class to chat with OpenAI model for dataset marking.\n \"\"\"\n\n def __init__(\n self,\n openai_model: str = \"gpt-4o\",\n base_url: str = None,\n cache_path: str = os.path.expanduser(\"~\") + \"/.cache\",\n generation_config: dict = {},\n system_prompt: str = \"You are an intelligent assistant.\",\n ):\n \"\"\"\n Parameters\n ----------\n openai_model: str\n the model to use in OpenAI to chat.\n \"\"\"\n api_key = os.environ.get(\"OPENAI_API_KEY\", None)\n if api_key is not None:\n openai.api_key = api_key\n self.openai_model = openai_model\n\n self.cache_path = os.path.join(cache_path, \"openai_chat_cache.diskcache\")\n if not os.path.exists(cache_path):\n os.makedirs(cache_path)\n\n self.base_url = base_url\n self.generation_config = GENERATION_CONFIGS[openai_model] if generation_config is None else generation_config\n self.system_prompt = system_prompt\n\n def ask(self, message: str, json_output=False) -> str:\n cache_settings = dc.DEFAULT_SETTINGS.copy()\n cache_settings[\"eviction_policy\"] = \"none\"\n cache_settings[\"size_limit\"] = int(1e12)\n cache_settings[\"cull_limit\"] = 0\n openai_responses = dc.Cache(self.cache_path, **cache_settings)\n\n if (self.openai_model, message) in openai_responses:\n reply = openai_responses[(self.openai_model, message)]\n\n else:\n # Ask openai\n if openai.api_key is None:\n raise Exception(\n \"Cant ask openAI without token. \"\n \"Please specify OPENAI_API_KEY in environment parameters.\"\n )\n if self.system_prompt is not None:\n messages = [\n {\"role\": \"system\", \"content\": self.system_prompt},\n {\"role\": \"user\", \"content\": message},\n ]\n else:\n messages = [\n {\"role\": \"user\", \"content\": message},\n ]\n chat = self._send_request(messages)\n reply = chat.choices[0].message.content\n\n openai_responses[(self.openai_model, message)] = reply\n openai_responses.close()\n\n if \"please provide\" in reply.lower():\n return \"\"\n if \"to assist you\" in reply.lower():\n return \"\"\n if \"as an ai language model\" in reply.lower():\n return \"\"\n\n return reply\n\n def _send_request(self, messages):\n sleep_time_values = (5, 10, 30, 60, 120)\n for i in range(len(sleep_time_values)):\n try:\n return openai.OpenAI(base_url=self.base_url).chat.completions.create(\n model=self.openai_model,\n messages=messages,\n **self.generation_config,\n )\n except Exception as e:\n sleep_time = sleep_time_values[i]\n log.info(\n f\"Request to OpenAI failed with exception: {e}. Retry #{i}/5 after {sleep_time} seconds.\"\n )\n time.sleep(sleep_time)\n\n return openai.OpenAI(base_url=self.base_url).chat.completions.create(\n model=self.openai_model,\n messages=messages,\n **self.generation_config,\n )"}}}}