Update llm_agent.py
Browse files- llm_agent.py +168 -174
llm_agent.py
CHANGED
|
@@ -1,174 +1,168 @@
|
|
| 1 |
-
import os
|
| 2 |
-
from dotenv import load_dotenv
|
| 3 |
-
from langgraph.graph import START, StateGraph, MessagesState
|
| 4 |
-
from langgraph.prebuilt import tools_condition
|
| 5 |
-
from langgraph.prebuilt import ToolNode
|
| 6 |
-
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 7 |
-
from
|
| 8 |
-
from
|
| 9 |
-
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 10 |
-
from langchain_community.
|
| 11 |
-
from
|
| 12 |
-
from
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
@tool
|
| 20 |
-
def
|
| 21 |
-
"""
|
| 22 |
-
|
| 23 |
-
a:int
|
| 24 |
-
b:int
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
Args:
|
| 33 |
-
a:int
|
| 34 |
-
b:int
|
| 35 |
-
"""
|
| 36 |
-
return a
|
| 37 |
-
|
| 38 |
-
@tool
|
| 39 |
-
def
|
| 40 |
-
"""
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
search_docs = WikipediaLoader(query=query,load_max_docs=2).load()
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
""
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
builder
|
| 152 |
-
|
| 153 |
-
builder.
|
| 154 |
-
builder.
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
)
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
graph = build_graph()
|
| 170 |
-
# Run the graph
|
| 171 |
-
messages = [HumanMessage(content=question)]
|
| 172 |
-
messages = graph.invoke({"messages": messages})
|
| 173 |
-
for m in messages["messages"]:
|
| 174 |
-
m.pretty_print()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
from langgraph.graph import START, StateGraph, MessagesState
|
| 4 |
+
from langgraph.prebuilt import tools_condition
|
| 5 |
+
from langgraph.prebuilt import ToolNode
|
| 6 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 7 |
+
from langchain_groq import ChatGroq
|
| 8 |
+
from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint, HuggingFaceEmbeddings
|
| 9 |
+
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 10 |
+
from langchain_community.document_loaders import WikipediaLoader
|
| 11 |
+
from langchain_community.document_loaders import ArxivLoader
|
| 12 |
+
from langchain_community.vectorstores import SupabaseVectorStore
|
| 13 |
+
from langchain_core.messages import SystemMessage, HumanMessage
|
| 14 |
+
from langchain_core.tools import tool
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
load_dotenv()
|
| 18 |
+
os.environ["GROQ_API_KEY"] = os.getenv("GROQ_API_KEY")
|
| 19 |
+
@tool
|
| 20 |
+
def multiply(a: int, b: int) -> int:
|
| 21 |
+
"""Multiply two numbers.
|
| 22 |
+
Args:
|
| 23 |
+
a: first int
|
| 24 |
+
b: second int
|
| 25 |
+
"""
|
| 26 |
+
return a * b
|
| 27 |
+
|
| 28 |
+
@tool
|
| 29 |
+
def add(a: int, b: int) -> int:
|
| 30 |
+
"""Add two numbers.
|
| 31 |
+
|
| 32 |
+
Args:
|
| 33 |
+
a: first int
|
| 34 |
+
b: second int
|
| 35 |
+
"""
|
| 36 |
+
return a + b
|
| 37 |
+
|
| 38 |
+
@tool
|
| 39 |
+
def subtract(a: int, b: int) -> int:
|
| 40 |
+
"""Subtract two numbers.
|
| 41 |
+
|
| 42 |
+
Args:
|
| 43 |
+
a: first int
|
| 44 |
+
b: second int
|
| 45 |
+
"""
|
| 46 |
+
return a - b
|
| 47 |
+
|
| 48 |
+
@tool
|
| 49 |
+
def divide(a: int, b: int) -> int:
|
| 50 |
+
"""Divide two numbers.
|
| 51 |
+
|
| 52 |
+
Args:
|
| 53 |
+
a: first int
|
| 54 |
+
b: second int
|
| 55 |
+
"""
|
| 56 |
+
if b == 0:
|
| 57 |
+
raise ValueError("Cannot divide by zero.")
|
| 58 |
+
return a / b
|
| 59 |
+
|
| 60 |
+
@tool
|
| 61 |
+
def modulus(a: int, b: int) -> int:
|
| 62 |
+
"""Get the modulus of two numbers.
|
| 63 |
+
|
| 64 |
+
Args:
|
| 65 |
+
a: first int
|
| 66 |
+
b: second int
|
| 67 |
+
"""
|
| 68 |
+
return a % b
|
| 69 |
+
|
| 70 |
+
@tool
|
| 71 |
+
def wiki_search(query: str) -> str:
|
| 72 |
+
"""Search Wikipedia for a query and return maximum 2 results.
|
| 73 |
+
|
| 74 |
+
Args:
|
| 75 |
+
query: The search query."""
|
| 76 |
+
search_docs = WikipediaLoader(query=query, load_max_docs=2).load()
|
| 77 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
| 78 |
+
[
|
| 79 |
+
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
|
| 80 |
+
for doc in search_docs
|
| 81 |
+
])
|
| 82 |
+
return {"wiki_results": formatted_search_docs}
|
| 83 |
+
|
| 84 |
+
@tool
|
| 85 |
+
def web_search(query: str) -> str:
|
| 86 |
+
"""Search Tavily for a query and return maximum 3 results.
|
| 87 |
+
|
| 88 |
+
Args:
|
| 89 |
+
query: The search query."""
|
| 90 |
+
search_docs = TavilySearchResults(max_results=3).invoke(query=query)
|
| 91 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
| 92 |
+
[
|
| 93 |
+
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
|
| 94 |
+
for doc in search_docs
|
| 95 |
+
])
|
| 96 |
+
return {"web_results": formatted_search_docs}
|
| 97 |
+
|
| 98 |
+
@tool
|
| 99 |
+
def arvix_search(query: str) -> str:
|
| 100 |
+
"""Search Arxiv for a query and return maximum 3 result.
|
| 101 |
+
|
| 102 |
+
Args:
|
| 103 |
+
query: The search query."""
|
| 104 |
+
search_docs = ArxivLoader(query=query, load_max_docs=3).load()
|
| 105 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
| 106 |
+
[
|
| 107 |
+
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content[:1000]}\n</Document>'
|
| 108 |
+
for doc in search_docs
|
| 109 |
+
])
|
| 110 |
+
return {"arvix_results": formatted_search_docs}
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
system_prompt = """
|
| 115 |
+
You are a helpful assistant tasked with answering questions using a set of tools.
|
| 116 |
+
Now, I will ask you a question. Report your thoughts, and finish your answer with the following template:
|
| 117 |
+
FINAL ANSWER: [YOUR FINAL ANSWER].
|
| 118 |
+
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
|
| 119 |
+
Your answer should only start with "FINAL ANSWER: ", then follows with the answer.
|
| 120 |
+
"""
|
| 121 |
+
sys_msg = SystemMessage(content=system_prompt)
|
| 122 |
+
|
| 123 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2") # dim=768
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
tools = [
|
| 128 |
+
multiply,
|
| 129 |
+
add,
|
| 130 |
+
subtract,
|
| 131 |
+
divide,
|
| 132 |
+
modulus,
|
| 133 |
+
wiki_search,
|
| 134 |
+
web_search,
|
| 135 |
+
arvix_search,
|
| 136 |
+
]
|
| 137 |
+
|
| 138 |
+
# Build graph function
|
| 139 |
+
def build_graph():
|
| 140 |
+
|
| 141 |
+
llm = ChatGroq(model="qwen-qwq-32b", temperature=0) # optional : qwen-qwq-32b gemma2-9b-it
|
| 142 |
+
|
| 143 |
+
llm_with_tools = llm.bind_tools(tools)
|
| 144 |
+
|
| 145 |
+
def assistant(state: MessagesState):
|
| 146 |
+
"""Assistant node"""
|
| 147 |
+
return {"messages": [llm_with_tools.invoke(state["messages"])]}
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
builder = StateGraph(MessagesState)
|
| 151 |
+
builder.add_node("assistant", assistant)
|
| 152 |
+
builder.add_node("tools", ToolNode(tools))
|
| 153 |
+
builder.add_edge(START, "assistant")
|
| 154 |
+
builder.add_conditional_edges(
|
| 155 |
+
"assistant",
|
| 156 |
+
tools_condition,
|
| 157 |
+
)
|
| 158 |
+
builder.add_edge("tools", "assistant")
|
| 159 |
+
|
| 160 |
+
return builder.compile()
|
| 161 |
+
|
| 162 |
+
if __name__ == "__main__":
|
| 163 |
+
question = "What is the first name of the only Malko Competition recipient from the 20th Century (after 1977) whose nationality on record is a country that no longer exists?"
|
| 164 |
+
graph = build_graph()
|
| 165 |
+
messages = [HumanMessage(content=question)]
|
| 166 |
+
messages = graph.invoke({"messages": messages})
|
| 167 |
+
for m in messages["messages"]:
|
| 168 |
+
m.pretty_print()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|