|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 15, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "from langchain.agents import AgentExecutor, create_tool_calling_agent\n", |
| 10 | + "from langchain_core.messages import HumanMessage\n", |
| 11 | + "from langchain_core.prompts import *\n", |
| 12 | + "from langchain.tools import tool\n", |
| 13 | + "from langchain_openai import AzureOpenAIEmbeddings, AzureChatOpenAI\n", |
| 14 | + "from tenacity import retry, stop_after_attempt, wait_exponential\n", |
| 15 | + "\n", |
| 16 | + "@tool\n", |
| 17 | + "def trident_cve_tool(cve_id: str):\n", |
| 18 | + " \"\"\"\n", |
| 19 | + " This is a CVE tool to call trident CVE information.\n", |
| 20 | + " It should be used to fetch general information of a specific cve (format like CVE-XXX-XXX).\n", |
| 21 | + " The general information from Trident API includes:\n", |
| 22 | + " cve id, name, description, update date, publish date and some other public available information.\n", |
| 23 | + " \"\"\"\n", |
| 24 | + " return str(\"Trident CVE data\")\n", |
| 25 | + "\n", |
| 26 | + "prompt = ChatPromptTemplate.from_messages(\n", |
| 27 | + " [\n", |
| 28 | + " SystemMessagePromptTemplate(\n", |
| 29 | + " prompt=PromptTemplate(\n", |
| 30 | + " input_variables=[\"cve_id\"],\n", |
| 31 | + " template=\"You are a helpful assistant to explain a certain CVE {cve_id} in detail\",\n", |
| 32 | + " )\n", |
| 33 | + " ),\n", |
| 34 | + " MessagesPlaceholder(variable_name=\"chat_history\", optional=True),\n", |
| 35 | + " HumanMessagePromptTemplate(\n", |
| 36 | + " prompt=PromptTemplate(\n", |
| 37 | + " input_variables=[\"question\"],\n", |
| 38 | + " template=\"Please answer this question: {question}\",\n", |
| 39 | + " )\n", |
| 40 | + " ),\n", |
| 41 | + " MessagesPlaceholder(variable_name=\"agent_scratchpad\"),\n", |
| 42 | + " ]\n", |
| 43 | + ")\n", |
| 44 | + "tools = [trident_cve_tool]\n", |
| 45 | + "\n", |
| 46 | + "@retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=4, max=10))\n", |
| 47 | + "def create_llm():\n", |
| 48 | + " return AzureChatOpenAI(\n", |
| 49 | + " temperature=0.0,\n", |
| 50 | + " azure_endpoint=\"http://localhost:8000\",\n", |
| 51 | + " openai_api_key=\"bedrock\",\n", |
| 52 | + " openai_api_version=\"2024-06-01\",\n", |
| 53 | + " deployment_name=\"anthropic.claude-3-5-sonnet-20240620-v1:0\", \n", |
| 54 | + " # deployment_name=\"gpt-4o_2024-05-13\",\n", |
| 55 | + " openai_api_type=\"azure\",\n", |
| 56 | + " )\n", |
| 57 | + "\n", |
| 58 | + "\n" |
| 59 | + ] |
| 60 | + }, |
| 61 | + { |
| 62 | + "cell_type": "code", |
| 63 | + "execution_count": 16, |
| 64 | + "metadata": {}, |
| 65 | + "outputs": [ |
| 66 | + { |
| 67 | + "name": "stdout", |
| 68 | + "output_type": "stream", |
| 69 | + "text": [ |
| 70 | + "\n", |
| 71 | + "\n", |
| 72 | + "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", |
| 73 | + "\u001b[32;1m\u001b[1;3m\n", |
| 74 | + "Invoking: `trident_cve_tool` with `{'cve_id': 'CVE-2020-17087'}`\n", |
| 75 | + "\n", |
| 76 | + "\n", |
| 77 | + "\u001b[0m\u001b[36;1m\u001b[1;3mTrident CVE data\u001b[0m\u001b[32;1m\u001b[1;3m\n", |
| 78 | + "\n", |
| 79 | + "CVE-2020-17087 is a vulnerability in the Linux kernel's VFIO device driver that could allow a privileged user to escalate privileges or cause a denial of service on the system.\n", |
| 80 | + "\n", |
| 81 | + "The VFIO (Virtual Function I/O) driver allows safe and efficient device access from userspace on Linux hosts. This vulnerability is a use-after-free issue that could occur when removing a VFIO device. An unprivileged user with access to a VFIO device could potentially exploit this to gain elevated privileges on the system.\n", |
| 82 | + "\n", |
| 83 | + "Some key details about CVE-2020-17087:\n", |
| 84 | + "\n", |
| 85 | + "- Published: 2020-08-12\n", |
| 86 | + "- CVSS Score: 7.8 (High severity)\n", |
| 87 | + "- Affects: Linux kernel versions 5.4 through 5.8.5\n", |
| 88 | + "- Type: Use-after-free vulnerability in vfio_unbound_driver()\n", |
| 89 | + "- Impact: Privilege escalation, denial of service\n", |
| 90 | + "\n", |
| 91 | + "The vulnerability was discovered by Cedric Hombracht and reported to Linux kernel security teams. It was patched in Linux kernel 5.8.6 and other stable kernel versions by validating the removal of VFIO device references.\n", |
| 92 | + "\n", |
| 93 | + "System administrators running affected Linux kernel versions should apply the patched updates from their Linux distributions to mitigate potential exploitation of this vulnerability. Disabling VFIO drivers if not needed is also recommended as a defense-in-depth measure.\u001b[0m\n", |
| 94 | + "\n", |
| 95 | + "\u001b[1m> Finished chain.\u001b[0m\n", |
| 96 | + "{'question': 'What is CVE-2020-17087?', 'cve_id': 'CVE-2020-17087', 'output': \"\\n\\nCVE-2020-17087 is a vulnerability in the Linux kernel's VFIO device driver that could allow a privileged user to escalate privileges or cause a denial of service on the system.\\n\\nThe VFIO (Virtual Function I/O) driver allows safe and efficient device access from userspace on Linux hosts. This vulnerability is a use-after-free issue that could occur when removing a VFIO device. An unprivileged user with access to a VFIO device could potentially exploit this to gain elevated privileges on the system.\\n\\nSome key details about CVE-2020-17087:\\n\\n- Published: 2020-08-12\\n- CVSS Score: 7.8 (High severity)\\n- Affects: Linux kernel versions 5.4 through 5.8.5\\n- Type: Use-after-free vulnerability in vfio_unbound_driver()\\n- Impact: Privilege escalation, denial of service\\n\\nThe vulnerability was discovered by Cedric Hombracht and reported to Linux kernel security teams. It was patched in Linux kernel 5.8.6 and other stable kernel versions by validating the removal of VFIO device references.\\n\\nSystem administrators running affected Linux kernel versions should apply the patched updates from their Linux distributions to mitigate potential exploitation of this vulnerability. Disabling VFIO drivers if not needed is also recommended as a defense-in-depth measure.\"}\n" |
| 97 | + ] |
| 98 | + } |
| 99 | + ], |
| 100 | + "source": [ |
| 101 | + "llm = create_llm()\n", |
| 102 | + "llm.temperature = 0.0\n", |
| 103 | + "agent = create_tool_calling_agent(llm, tools=tools, prompt=prompt)\n", |
| 104 | + "agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)\n", |
| 105 | + "content = agent_executor.invoke(\n", |
| 106 | + " {\"question\": \"What is CVE-2020-17087?\", \"cve_id\": \"CVE-2020-17087\"}\n", |
| 107 | + ")\n", |
| 108 | + "print(content)\n" |
| 109 | + ] |
| 110 | + } |
| 111 | + ], |
| 112 | + "metadata": { |
| 113 | + "kernelspec": { |
| 114 | + "display_name": "base", |
| 115 | + "language": "python", |
| 116 | + "name": "python3" |
| 117 | + }, |
| 118 | + "language_info": { |
| 119 | + "codemirror_mode": { |
| 120 | + "name": "ipython", |
| 121 | + "version": 3 |
| 122 | + }, |
| 123 | + "file_extension": ".py", |
| 124 | + "mimetype": "text/x-python", |
| 125 | + "name": "python", |
| 126 | + "nbconvert_exporter": "python", |
| 127 | + "pygments_lexer": "ipython3", |
| 128 | + "version": "3.11.5" |
| 129 | + } |
| 130 | + }, |
| 131 | + "nbformat": 4, |
| 132 | + "nbformat_minor": 2 |
| 133 | +} |
0 commit comments