האם ה-LLM שלי יודע שנאסראללה חוסל?

מבוא קצר לשימוש בכלים עבור RAG

נסיונות לשאול את מודל ה LLM שלכם שאלות על מידע שהוא לא אומן עליו, יסתיימו בד"כ בתשובות "אינני יודע" למיניהן.

לדוגמה הקוד הזה:

import ollama
model = "llama3.1"
question = '''What the IDF did on September 28, 2024?'''
response = ollama.chat(model=model,
messages=[{"role":"system", "content":""},
{"role":"user", "content":question}])
print(response)
view raw main.py hosted with ❤ by GitHub

אם נריץ את הקוד הזה נקבל תשובות כגון:

"I'm unable to tell you what the IDF (Israel Defense Forces) did on September 28, 2024."
"I cannot provide real-time information or specifics regarding future events. Is there anything else I can help you with?"

כדי לאפשר למודל ה LLM שלנו (llama3.1 בדוגמה שלנו) לענות על השאלה בהצלחה, ניתן להוסיף כלים – כגון כלי שאילתה וחיפוש באינטרנט, שה RAG יוכל להשתמש בהם.

נוסיף לדוגמה כלי חיפוש באינטרנט באמצעות DuckDuckGo.

pip install duckduckgo-search

והקוד שמגדיר את כלי החיפוש:

from langchain_core.tools import tool
from langchain_community.tools import DuckDuckGoSearchRun
question = '''What the IDF did on September 28, 2024?'''
@tool("tool_browser")
def tool_browser(q: str) -> str:
"""Search on DuckDuckGo browser by passing the input `q`"""
return DuckDuckGoSearchRun().run(q)
# test
print(tool_browser(question))
view raw main.py hosted with ❤ by GitHub

ואם נריץ את זה נקבל:

Smoke rises from the rubble of a building that was levelled in overnight Israeli strike on Beirut's southern suburbs, on September 28, 2024. (Photo by ANWAR AMRO / AFP) The Times of Israel is … His death marks a devastating blow to Hezbollah as it reels from Israeli attacks. It is also a huge blow to Iran. … [1/17] Beirut's southern suburbs, Lebanon September 28, 2024. Smoke rises from Israeli airstrikes in Beirut's southern suburbs, on Saturday, September 28. Hassan Ammar/AP The Israel Defense Forces is currently striking buildings in Beirut that it alleges are … Hezbollah has confirmed the death of its leader Hassan Nasrallah, after Israel said he was killed in an airstrike in Beirut, Lebanon on Friday. Retired Colonel Cedric Leighton joins CNN to discuss. A man stands near damaged buildings in the aftermath of Israeli air strikes on Beirut's southern suburbs, Lebanon September 28, 2024. REUTERS/Ali Alloush Purchase Licensing Rights, opens new tab

נגדיר עוד כלי שיספק את התשובה הסופית, ונריץ הכל יחדיו:

import ollama
import json
from langchain_core.tools import tool
from langchain_community.tools import DuckDuckGoSearchRun
model = "llama3.1"
question = '''What the IDF did on September 28, 2024?'''
response = ollama.chat(model=model,
messages=[{"role":"system", "content":""},
{"role":"user", "content":question}])
print(response)
@tool("tool_browser")
def tool_browser(q: str) -> str:
"""Search on DuckDuckGo browser by passing the input `q`"""
return DuckDuckGoSearchRun().run(q)
# test
print(tool_browser(question))
@tool("final_answer")
def final_answer(text:str) -> str:
"""Returns a natural language response to the user by passing the input `text`.
You should provide as much context as possible and specify the source of the information.
"""
return text
prompt = """
You know everything, you must answer every question from the user, you can use the list of tools provided to you.
Your goal is to provide the user with the best possible answer, including key information about the sources and tools used.
Note, when using a tool, you provide the tool name and the arguments to use in JSON format.
For each call, you MUST ONLY use one tool AND the response format must ALWAYS be in the pattern:
```json
{"name":"", "parameters": {"":}}
```
Remember, do NOT use any tool with the same query more than once.
Remember, if the user doesn't ask a specific question, you MUST use the `final_answer` tool directly.
Every time the user asks a question, you take note of some keywords in the memory.
Every time you find some information related to the user's question, you take note of some keywords in the memory.
You should aim to collect information from a diverse range of sources before providing the answer to the user.
Once you have collected plenty of information to answer the user's question use the `final_answer` tool.
"""
dic_tools = {"tool_browser": tool_browser,
"final_answer":final_answer}
str_tools = "\n".join([str(n+1)+". `"+str(v.name)+"`: "+str(v.description) for n,v in enumerate(dic_tools.values())])
prompt_tools = f"You can use the following tools:\n{str_tools}"
print(prompt_tools)
response = ollama.chat(
model=model,
messages=[{"role":"system", "content":prompt+"\n"+prompt_tools},
{"role":"user", "content":"hello"}
], format="json")
print(response)
response = ollama.chat(
model=model,
messages=[{"role":"system", "content":prompt+"\n"+prompt_tools},
{"role":"user", "content":question}
], format="json")
print(response["message"]["content"])
tool_input = json.loads(response["message"]["content"])["parameters"]["q"]
context = tool_browser(tool_input)
print("tool output:\n", context)
response = ollama.chat(
model=model,
messages=[{"role":"system", "content":"Give the most accurate answer using the folling information:\n"+context},
{"role":"user", "content":question}
])
print("\nllm output:\n", response["message"]["content"])
view raw main.py hosted with ❤ by GitHub

בשורה 51 אנו מייצרים את הפרומפט שמורה למודל להשתמש בכלים:

You can use the following tools:
1. `tool_browser`: Search on DuckDuckGo browser by passing the input `q`
2. `final_answer`: Returns a natural language response to the user by passing the input `text`.
    You should provide as much context as possible and specify the source of the information.

משורה 54 מריצים שיחה עם המודל שמתחילה ב"הלו" ומסתיימת באותה שאלה ששאלנו בתחילת הפוסט.

התשובה:

According to the provided information, on September 28, 2024, the Israeli army (IDF) carried out an airstrike on Hezbollah's central command in southern Beirut, killing Hassan Nasrallah, the longtime leader of Hezbollah, along with approximately 300 people, including other members of the group.