add: operations

This commit is contained in:
h z
2025-05-28 16:34:20 +01:00
parent 8933b13000
commit 9779630028
25 changed files with 776 additions and 231 deletions

107
l1.py
View File

@@ -1,13 +1,11 @@
import asyncio
from langchain.agents import create_react_agent, AgentExecutor
from langchain.chat_models import init_chat_model
from langchain_core.prompts import PromptTemplate
from langchain_mcp_adapters.client import MultiServerMCPClient
from langchain_core.tools import tool
from langchain.memory import MongoDBChatMessageHistory, ConversationBufferWindowMemory
from agents.agent_tasks.scan_file import tools
from agents.agent_templates import AgentTemplate
from agents.output_parser.MCPReactParser import MCPReactParser
from utils.db_connections import init_db
from agents.prompts import general_prompt_param_builder
from utils.ssh_connections.ssh_operations import is_file_exist
@tool
@@ -21,8 +19,9 @@ def get_daily_string(salt: str):
async def main():
init_db()
temp1 = AgentTemplate()
temp1.set_model("gpt-4.1-nano", "openai", "")
temp1.set_model("gpt-4.1-nano", "openai")
temp1.set_mcp_server("terminal_tools", {
'transport': 'sse',
'headers': {
@@ -30,89 +29,21 @@ async def main():
},
'url': 'http://localhost:5050/sse'
})
temp1.set_builtin_tools(tools)
temp1.set_template_params(general_prompt_param_builder(
agent_role="assistant",
task_description="test, call update_code_result_to_db tool with arbitrary valid input"
))
mcp_client = MultiServerMCPClient({
'terminal_tool':{
'transport': 'sse',
'headers': {
"X-Api-Key": "bd303c2f2a515b4ce70c1f07ee85530c2"
},
'url': 'http://localhost:5050/sse'
}
agent = await temp1.async_get_instance()
r1 = await agent.ainvoke({
'user_msg': 'test'
})
tools = await mcp_client.get_tools()
xtools = []
for t in tools:
if t.name == 'CreateTerminalSession':
xtools.append(t)
xtools.append(get_daily_string)
print(xtools)
prompt = PromptTemplate.from_template("""
{system}
You have access to the following tools:
{tools}
Use the following format:
Question: the question you must answer
If you want to use tools:
Thought: always reason what to do
Action: the action to take, must be one of [{tool_names}]
Action Input: a **JSON object** with named arguments required by the tool
Observation: the result of the action
If no tool is needed:
Thought: what you are thinking
... (this Thought/Action/... can repeat N times)
Final Answer: the final answer to the original question
Here is the conversation history:
{chat_history}
User message: {user}
{agent_scratchpad}
""")
op = MCPReactParser()
agent = create_react_agent(model_openai, xtools, prompt, output_parser=op)
message_history = MongoDBChatMessageHistory(
connection_string=MONGO_CONNECTION_STRING,
session_id=USER_SESSION_ID,
database_name=MONGO_DB_NAME,
collection_name=MONGO_COLLECTION_NAME
)
mongodb_memory = ConversationBufferWindowMemory(
chat_memory=message_history,
memory_key=MEMORY_KEY,
k=10,
return_messages=False,
)
agent_executor = AgentExecutor(
agent=agent,
tools=xtools,
memory=mongodb_memory,
handle_parsing_errors=True,
verbose=True,
)
response = await agent_executor.ainvoke({
'system': 'you are a helpful assistant',
'user': 'hello, please create a terminal session with label \'x\' and show me the session id',
})
print(response)
r2 = await agent_executor.ainvoke({
'system': 'you are a helpful assistant',
'user': 'hello, please show me today\'s daily string'
})
print(r2)
#print(s1)
#print(s2)
#print(s3)
#print(s4)
# for t in tools:
# if t.name == 'update_code_result_to_db':
# print(t)
print(r1)
print(is_file_exist("./Guide"))
if __name__ == '__main__':
asyncio.run(main())