Shekarss commited on
Commit
4a15930
·
verified ·
1 Parent(s): 0ec6073

Delete agent.py

Browse files
Files changed (1) hide show
  1. agent.py +0 -198
agent.py DELETED
@@ -1,198 +0,0 @@
1
- from llama_index.core import SimpleDirectoryReader
2
- from langgraph.prebuilt import ToolNode
3
- from langchain_community.document_loaders import WikipediaLoader
4
- from langchain_community.tools import DuckDuckGoSearchRun
5
- from langchain_core.tools import tool
6
- from typing import TypedDict, Annotated, List, Any, Dict
7
- from langchain_core.messages import BaseMessage, HumanMessage, AIMessage, ToolMessage, AnyMessage, SystemMessage
8
- from langgraph.graph.message import add_messages
9
- from langchain_groq import ChatGroq
10
- from langgraph.graph import StateGraph, START, END
11
- from langgraph.prebuilt import ToolNode, tools_condition
12
- import os
13
-
14
- @tool
15
- def read_file(directory:str)->str:
16
- """
17
- Reads text from files in a directory
18
- Args:
19
- directory (string): Takes in a directory name.
20
- output_type: text
21
- """
22
- docs = SimpleDirectoryReader(directory).load_data()
23
- return ' '.join([doc.text for doc in docs])
24
-
25
- @tool
26
- def web_search(query:str)->str:
27
- """
28
- Inputs a query and use DuckDuckGoSearchRun to search and fetch information from web.
29
- Args:
30
- query (string): Takes a query
31
- output_type: text
32
- """
33
- search = DuckDuckGoSearchRun()
34
- return search.invoke(query)
35
-
36
- @tool
37
- def multiply(a:float, b:float)->float:
38
- """
39
- Multiplies two numbers.
40
- Args:
41
- a (float): the first number
42
- b (float): the second number
43
- output_type: float
44
- """
45
- return a*b
46
-
47
- @tool
48
- def divide(a:float, b:float)->float:
49
- """
50
- Divides two numbers.
51
- Args:
52
- a (float): the first number
53
- b (float): the second number
54
- output_type: float
55
- """
56
- if b != 0:
57
- return a / b
58
- else:
59
- raise ValueError('Cannot divide a number by zero')
60
-
61
- @tool
62
- def subtract(a:float, b:float)->float:
63
- """
64
- Subtracts two numbers.
65
- Args:
66
- a (float): the first number
67
- b (float): the second number
68
- output_type: float
69
- """
70
- return a - b
71
-
72
- @tool
73
- def add(a:float, b:float)->float:
74
- """
75
- Adds two numbers.
76
- Args:
77
- a (float): the first number
78
- b (float): the second number
79
- output_type: float
80
- """
81
- return a + b
82
-
83
- @tool
84
- def modulus(a:int, b:int)->int:
85
- """
86
- Get the modulus of two numbers.
87
- Args:
88
- a (int): the first number
89
- b (int): the second number
90
- output_type: int
91
- """
92
- return a % b
93
-
94
- @tool
95
- def power(a:float, b:float)->float:
96
- """
97
- Get the power of two numbers.
98
- Args:
99
- a (float): the first number
100
- b (float): the second number
101
- output_type: float
102
- """
103
- return a**b
104
-
105
- @tool
106
- def square_root(a:float)->float | complex:
107
- """
108
- Get the square root of a numbers.
109
- Args:
110
- a (float): the first number
111
- output_type: float
112
- """
113
- if a >= 0:
114
- return a**0.5
115
- return cmath.sqrt(a)
116
-
117
- @tool
118
- def wikipedia_fetcher(query:str)->str:
119
- """
120
- Inputs a query and use WikipediaLoader to fetch query realted information.
121
- Args:
122
- query (string): Takes a query
123
- output_type: text
124
- """
125
- docs = WikipediaLoader(query=query, load_max_docs=3).load()
126
- doc = docs[0].page_content
127
- return doc
128
-
129
- @tool
130
- def query_csv_first_match(csv_file: str, filters: dict, column: str) -> str:
131
- """
132
- Returns the first value from `column` in CSV matching the filters.
133
-
134
- Args:
135
- csv_file (str): Path to CSV
136
- filters (dict): Column-value filters, e.g. {"Year": 1983, "Country": "SU"}
137
- column (str): Column to return value from
138
-
139
- Returns:
140
- str: First matching value
141
- """
142
- df = pd.read_csv(csv_file)
143
- for key, val in filters.items():
144
- if val is not None:
145
- df = df[df[key] == val]
146
- if not df.empty:
147
- return str(df.iloc[0][column])
148
- return "Unknown"
149
-
150
- @tool
151
- def query_csv_min(csv_file: str, column_min: str, return_column: str, tie_break_column: str = None) -> str:
152
- """
153
- Returns the value of `return_column` from the row with the minimum `column_min`.
154
- If tie, use `tie_break_column` to select first alphabetically.
155
-
156
- Args:
157
- csv_file (str): Path to CSV
158
- column_min (str): Column to find minimum
159
- return_column (str): Column to return
160
- tie_break_column (str, optional): Column to break tie alphabetically
161
-
162
- Returns:
163
- str: Result value
164
- """
165
- df = pd.read_csv(csv_file)
166
- min_value = df[column_min].min()
167
- candidates = df[df[column_min] == min_value]
168
- if tie_break_column:
169
- candidates = candidates.sort_values(tie_break_column)
170
- return str(candidates.iloc[0][return_column])
171
-
172
- tools = [read_file, wikipedia_fetcher, web_search, add, subtract, divide, multiply, modulus, power, square_root, query_csv_first_match, query_csv_min]
173
-
174
- class AgentState(TypedDict):
175
- messages: Annotated[List[AnyMessage], add_messages]
176
-
177
- api_key = os.getenv('GROQ_API_KEY')
178
-
179
- def call_model(state: AgentState):
180
- llm = ChatGroq(model="qwen/qwen3-32b", temperature=0, api_key=api_key)
181
- llm_with_tools = llm.bind_tools(tools)
182
- response = llm_with_tools.invoke(state['messages'])
183
- return {'messages': [response]}
184
-
185
- def build_graph():
186
- workflow = StateGraph(AgentState)
187
- workflow.add_node('llm', call_model)
188
- workflow.add_node("call_tool", ToolNode(tools))
189
- workflow.add_edge(START, 'llm')
190
- workflow.add_conditional_edges(
191
- 'llm',
192
- tools_condition,
193
- {'tools':'call_tool', '__end__':END}
194
- )
195
- workflow.add_edge('call_tool', 'llm')
196
-
197
- app = workflow.compile()
198
- return app