-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
156 lines (125 loc) · 5.29 KB
/
agent.py
File metadata and controls
156 lines (125 loc) · 5.29 KB
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
from typing import TypedDict,Sequence,Annotated
from dotenv import load_dotenv
from langgraph.graph import StateGraph,START,END
from langchain_core.messages import BaseMessage,SystemMessage,HumanMessage
from langchain_core.tools import tool
from langgraph.prebuilt import ToolNode
from langgraph.graph.message import add_messages
from langchain_google_genai import ChatGoogleGenerativeAI
import asyncio
from client import main
from google import genai
from gemini import run_tools,extract_filename_for_instruction_following,extract_steps_for_doc_agent
import os
import requests
client = genai.Client()
load_dotenv()
class AgentState(TypedDict):
messages:Annotated[Sequence[BaseMessage],add_messages]
@tool
def system_agent_tool(message:str):
"""Use this tool for any file system operations like copying or moving or executing any command line operations, capture the user's message
When it comes to file system operations just extract the text from the user's query, do not use any command line operations
This tool also has the power to generate various files with formatting.
You can use this tool to open folders.
Don't use this tool for the opening of files but you can use this for opening programs.
This tool has the power of encryption files.
When capturing the input donot use quotes in the captured input
This tool has the power to open programs and interact with various GUI components
"""
print(message)
print('invoked')
res = asyncio.run(run_tools(message,"C:\\Anish\\Computer_Science\\AI\\Agentic AI\\mcp\\mcp_servers\\system_agent\\server.py"))
print(res)
'''with open('data.txt','w') as f:
f.write(res)
f.close()'''
@tool
def open_file_tool(path:str):
"""
This tool is used when the user wants to open a specific file of any type
ARGS: path
"""
print(path)
os.startfile(path)
@tool
def deep_research_tool(topic:str):
"""
This tool is used when the user wants to conduct deep research on a specific topic
ARGS: topic
"""
res = asyncio.run(main("What is " + topic,'C:\\Anish\\Computer_Science\\AI\\Agentic AI\\mcp\\mcp_servers\\deep_research\\server.py'))
return str(res)
@tool
def execute_instructions_from_file(filename:str):
"""
This tool is used when the user wants the agent to execute instructions from the file
ARGS: filename
"""
with open(filename,'r') as f:
content = f.read()
for c in content.split('\n'):
instructions.append(HumanMessage(content=c))
res = agent.invoke({"messages":c})
instructions = res['messages']
return instructions
@tool
def doc_agent(task:str):
"""
This tool is used when the user wants the agent to perform any task related to documents
ARGS: task
"""
res = []
# Get the list of steps from reasoning model server
steps = requests.post("http://127.0.0.1:5000/",json={"request":task})
if steps.status_code == 200:
steps = steps.json()
steps = (steps['response'].replace('assistant','').replace('<think>','').replace('</think>','').replace('<|im_end|>',''))
steps = extract_steps_for_doc_agent(steps)
steps = steps['steps']
for step in steps:
if not res:
response = asyncio.run(run_tools(step,"C:\\Anish\\Computer_Science\\AI\\Agentic AI\\mcp\\mcp_servers\\doc_agent\\server.py"))
else:
response = asyncio.run(run_tools("The previous response from you was " + res[-1] + "\n" + step,"C:\\Anish\\Computer_Science\\AI\\Agentic AI\\mcp\\mcp_servers\\doc_agent\\server.py"))
res.append(response)
else:
return "An error occured, please try again later"
return "Done"
tools = [system_agent_tool,open_file_tool,deep_research_tool,doc_agent]
llm = ChatGoogleGenerativeAI(model='gemini-2.5-flash').bind_tools(tools)
def agent(state:AgentState):
res = extract_filename_for_instruction_following(state['messages'])
if res == "False":
instruction = SystemMessage(content='You are my AI Assistant, answer to your best ability, your abilities are now extended with the help of various tools. At any cost do not give any error type messages return the tool messages')
response = llm.invoke([instruction] + state['messages'])
return {"messages":response}
else:
with open(res,'r') as f:
content = f.read()
content_str = []
for c in content.split('\n'):
content_str.append(c)
instruction = SystemMessage(content='You are my AI Assistant, answer to your best ability, your abilities are now extended with the help of various tools. At any cost do not give any error type messages return the tool messages')
response = llm.invoke([instruction] + content_str)
return {"messages":response}
def should_continue(state:AgentState):
if state['messages'][-1].tool_calls:
return "continue"
else:
return "end"
graph = StateGraph(AgentState)
graph.add_node("agent",agent)
tool_node = ToolNode(tools=tools)
graph.add_node("tool_node",tool_node)
graph.add_edge(START,'agent')
graph.add_conditional_edges(
'agent',
should_continue,
{
"continue":"tool_node",
"end":END
}
)
graph.add_edge('tool_node','agent')
app = graph.compile()