File size: 5,464 Bytes
4b2b389
 
 
32c39ee
4b2b389
 
 
 
 
32c39ee
4b2b389
 
 
 
32c39ee
 
4b2b389
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32c39ee
4b2b389
 
 
 
 
 
 
 
 
 
 
32c39ee
4b2b389
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32c39ee
 
 
4b2b389
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32c39ee
4b2b389
 
 
 
 
 
 
 
 
32c39ee
 
 
 
 
 
 
 
 
4b2b389
 
 
 
 
 
32c39ee
4b2b389
 
 
 
 
32c39ee
4b2b389
 
 
 
 
32c39ee
 
 
 
 
 
4b2b389
32c39ee
4b2b389
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import os
from dotenv import load_dotenv

from langchain_core.messages import HumanMessage, SystemMessage
from langchain_core.tools import tool
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_community.document_loaders import WikipediaLoader

from langgraph.graph import StateGraph, START, MessagesState
from langgraph.prebuilt import ToolNode, tools_condition

load_dotenv()

SYSTEM_PROMPT = """You are a general AI assistant. I will ask you a question. Report your thoughts, and output only your final answer, no prefixes, suffixes, or extra text. 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."""


@tool
def add(a: float, b: float) -> float:
    """Add two numbers together.

    Args:
        a: First number
        b: Second number
    """
    return a + b


@tool
def subtract(a: float, b: float) -> float:
    """Subtract b from a.

    Args:
        a: Number to subtract from
        b: Number to subtract
    """
    return a - b


@tool
def multiply(a: float, b: float) -> float:
    """Multiply two numbers together.

    Args:
        a: First number
        b: Second number
    """
    return a * b


@tool
def divide(a: float, b: float) -> float:
    """Divide a by b.

    Args:
        a: Dividend
        b: Divisor
    """
    if b == 0:
        return "Error: Division by zero"
    return a / b


@tool
def modulo(a: float, b: float) -> float:
    """Return the remainder of a divided by b.

    Args:
        a: Dividend
        b: Divisor
    """
    if b == 0:
        return "Error: Division by zero"
    return a % b


@tool
def power(a: float, b: float) -> float:
    """Raise a to the power of b.

    Args:
        a: Base number
        b: Exponent
    """
    return a**b


@tool
def square_root(a: float) -> float:
    """Calculate the square root of a number.

    Args:
        a: Number to calculate square root of
    """
    if a < 0:
        return "Error: Cannot calculate square root of negative number"
    return a**0.5


@tool
def web_search(query: str) -> str:
    """Search the web for current information and facts.

    Args:
        query: Search query string
    """
    try:
        search_tool = TavilySearchResults(max_results=3)
        results = search_tool.invoke(query)

        if not results:
            return "No search results found."

        formatted_results = []
        for i, result in enumerate(results, 1):
            title = result.get("title", "No title")
            content = result.get("content", "No content")
            url = result.get("url", "No URL")
            formatted_results.append(f"{i}. {title}\n{content}\nSource: {url}")

        return "\n\n ==== \n\n".join(formatted_results)
    except Exception as e:
        return f"Error performing search: {str(e)}"


@tool
def wikipedia_search(query: str) -> str:
    """Search Wikipedia for factual information.

    Args:
        query: Wikipedia search query
    """
    try:
        loader = WikipediaLoader(query=query, load_max_docs=2)
        docs = loader.load()

        if not docs:
            return "No Wikipedia results found."

        formatted_docs = []
        for i, doc in enumerate(docs, 1):
            title = doc.metadata.get("title", "No title")
            content = doc.page_content
            formatted_docs.append(f"{i}. {title}\n{content}")

        return "\n\n ==== \n\n".join(formatted_docs)
    except Exception as e:
        return f"Error searching Wikipedia: {str(e)}"


tools = [
    add,
    subtract,
    multiply,
    divide,
    modulo,
    power,
    square_root,
    web_search,
    wikipedia_search,
]


def get_llm():
    """Initialize the llm"""
    return ChatGoogleGenerativeAI(
        model="gemini-2.5-flash", temperature=0, api_key=os.getenv("GEMINI_API_KEY")
    )


def call_model(state: MessagesState):
    """Call the LLM with the current state.
    
    Args:
        state: Current state containing messages
    """
    llm = get_llm()
    llm_with_tools = llm.bind_tools(tools)
    
    messages = state["messages"]
    
    if not messages or not isinstance(messages[0], SystemMessage):
        messages = [SystemMessage(content=SYSTEM_PROMPT)] + messages
    
    response = llm_with_tools.invoke(messages)
    
    return {"messages": [response]}


def build_graph():
    """Build and return the LangGraph workflow."""
    workflow = StateGraph(MessagesState)

    workflow.add_node("agent", call_model)
    workflow.add_node("tools", ToolNode(tools))

    workflow.add_edge(START, "agent")
    workflow.add_conditional_edges("agent", tools_condition)
    workflow.add_edge("tools", "agent")

    return workflow.compile()


if __name__ == "__main__":
    graph = build_graph()
    test_message = [HumanMessage(content="What is 15 + 27?")]
    result = graph.invoke({"messages": test_message})
    print(f"Test result: {result['messages'][-1].content}")