Files
hack/tools/scripts/llm-key-hunter/results/_cache/content/baobuiquang_nsg.json
T

1 line
7.1 KiB
JSON

{"repo": "baobuiquang/nsg", "files": {"pkg/LLM/LLM.py": {"74c9bbb6bf1fe8e606e3a56c82a0546bbf5fc40e": {"t": 1785674608.0540981, "n": 6776, "h": "4ace84688152aba1", "c": "# def activate_pkg():\n# import os\n# import sys\n# p = os.getcwd()\n# while not os.path.isdir(os.path.join(p, \"pkg\")): p = os.path.dirname(p)\n# sys.path.append(p) if p not in sys.path else None\n# activate_pkg()\n\n# ====================================================================================================\n# ====================================================================================================\n# ====================================================================================================\n\n# LLM - Large-Language-Model Processing\n\n# ----- Usage -----\n# from pkg.LLM.LLM import Process_LLM, Process_LLM_Stream\n# # prompt = string\n# prompt = \"Bạn tên gì? Bạn được huấn luyện bởi ai?\"\n# print(Process_LLM(prompt))\n# # prompt = list\n# prompt = [ { \"role\": \"user\", \"content\": \"Bạn tên gì? Bạn được huấn luyện bởi ai?\" } ]\n# print(Process_LLM(prompt))\n# # prompt = list - streaming\n# prompt = [ { \"role\": \"user\", \"content\": \"Bạn tên gì? Bạn được huấn luyện bởi ai?\" } ]\n# for chunk in Process_LLM_Stream(prompt):\n# print(chunk)\n\n# ====================================================================================================\n# ====================================================================================================\n# ====================================================================================================\n\nDEFAULT_VENDOR = \"openrouter\"\n\nimport pkg.UTILS.UTILS as UTILS\nimport requests\nimport json\n\nKEY_OPENROUTER = \"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\"\nKEY_DEEPINFRA = \"rD6SaD8vai3LxvehBVTyvq2djEjCIzGQ\"\nKEY_OLLAMA = \"ollama\"\nKEY_VNPT = \"ollama\"\nURL_OPENROUTER = \"https://openrouter.ai/api/v1/chat/completions\"\nURL_DEEPINFRA = \"https://api.deepinfra.com/v1/openai/chat/completions\"\nURL_OLLAMA = \"http://localhost:11434/v1/chat/completions\"\nURL_VNPT = \"http://192.168.80.99:11434/v1/chat/completions\"\nMDL_OPENROUTER = \"google/gemini-2.0-flash-001\" # \"qwen/qwen-2.5-7b-instruct\" # \"qwen/qwen3-32b\"\nMDL_DEEPINFRA = \"Qwen/Qwen2.5-7B-Instruct\"\nMDL_OLLAMA = \"qwen2.5:7b-instruct-q4_K_M\"\nMDL_VNPT = \"mygemma:12b\" # \"gemma3:12b\" / \"gemma3:12b-it-qat\"\n\n# ====================================================================================================\n# ====================================================================================================\n# ====================================================================================================\n\nprint(f\"LLM > Default Vendor: {DEFAULT_VENDOR} - Model: {MDL_OPENROUTER if DEFAULT_VENDOR=='openrouter' else MDL_DEEPINFRA if DEFAULT_VENDOR=='deepinfra' else ''}\")\n\n# ====================================================================================================\n# ====================================================================================================\n# ====================================================================================================\n\nclass RequestInput:\n def __init__(self, prompt, stream=False, vendor=DEFAULT_VENDOR):\n if vendor==\"ollama\":\n LLM_API_KEY = KEY_OLLAMA\n LLM_API_URL = URL_OLLAMA\n LLM_API_MDL = MDL_OLLAMA\n elif vendor==\"openrouter\":\n LLM_API_KEY = KEY_OPENROUTER\n LLM_API_URL = URL_OPENROUTER\n LLM_API_MDL = MDL_OPENROUTER\n elif vendor==\"deepinfra\":\n LLM_API_KEY = KEY_DEEPINFRA\n LLM_API_URL = URL_DEEPINFRA\n LLM_API_MDL = MDL_DEEPINFRA\n elif vendor==\"vnpt\":\n LLM_API_KEY = KEY_VNPT\n LLM_API_URL = URL_VNPT\n LLM_API_MDL = MDL_VNPT\n self.url = LLM_API_URL\n self.headers = {\n \"Content-Type\": \"application/json\",\n \"Authorization\": f\"Bearer {LLM_API_KEY}\"\n }\n self.stream = stream\n if isinstance(prompt, str): # Case 1: prompt is a string\n if prompt[0] != \"[\" or prompt[-1] != \"]\": # Case 1a: prompt is a single question (a string)\n self.payload = {\n \"stream\": stream,\n \"model\": LLM_API_MDL,\n \"messages\": [ { \"role\": \"user\", \"content\": prompt } ]\n }\n else: # Case 1b: prompt is a history (a string but represents a list)\n self.payload = {\n \"stream\": stream,\n \"model\": LLM_API_MDL,\n \"messages\": UTILS.str2pydata(prompt)\n }\n elif isinstance(prompt, list): # Case 2: prompt is a history (a list)\n self.payload = {\n \"stream\": stream,\n \"model\": LLM_API_MDL,\n \"messages\": prompt\n }\n else:\n raise ValueError(\"⚠️ LLM > RequestInput > prompt is neither not string nor list\")\n\n# ====================================================================================================\n# ====================================================================================================\n# ====================================================================================================\n\n# Non-streaming\ndef Process_LLM(prompt):\n try:\n reqin = RequestInput(prompt=prompt, stream=False)\n with requests.post(url=reqin.url, headers=reqin.headers, json=reqin.payload, stream=reqin.stream) as req:\n return req.json()[\"choices\"][0][\"message\"][\"content\"]\n except Exception as e:\n print(f\"⚠️ LLM > {e}\")\n return \"⚠️\"\n\n# Streaming\ndef Process_LLM_Stream(prompt, history=[]):\n history += [{\"role\":\"assistant\", \"content\":\"\"}]\n try:\n reqin = RequestInput(prompt=prompt, stream=True)\n with requests.post(url=reqin.url, headers=reqin.headers, json=reqin.payload, stream=reqin.stream) as req:\n for chunk in req.iter_lines():\n if chunk:\n chunk = chunk.decode(\"utf-8\", errors=\"replace\")[6:]\n try:\n chunk = json.loads(chunk)[\"choices\"][0][\"delta\"][\"content\"]\n history[-1][\"content\"] += chunk\n yield history\n except:\n pass\n except Exception as e:\n print(f\"⚠️ LLM > {e}\")\n history[-1][\"content\"] += \"⚠️\"\n yield history\n\n# ====================================================================================================\n# ====================================================================================================\n# ===================================================================================================="}}}}