Selcan Yukcu commited on
Commit ·
4f11b82
1
Parent(s): 8ada35f
fix: langchain buffermemory try
Browse files- langchain_mcp_client.py +18 -11
- postgre_mcp_server.py +1 -4
langchain_mcp_client.py
CHANGED
|
@@ -18,9 +18,10 @@ from langchain_google_genai import ChatGoogleGenerativeAI
|
|
| 18 |
from langchain_core.prompts import PromptTemplate
|
| 19 |
|
| 20 |
|
| 21 |
-
|
| 22 |
logger = logging.getLogger(__name__)
|
| 23 |
load_dotenv()
|
|
|
|
|
|
|
| 24 |
async def lc_mcp_exec(request: str, history) -> str:
|
| 25 |
"""
|
| 26 |
Execute the full PostgreSQL MCP pipeline: load summary, connect session,
|
|
@@ -45,7 +46,9 @@ async def lc_mcp_exec(request: str, history) -> str:
|
|
| 45 |
api_key=api_key,
|
| 46 |
temperature=0.5,
|
| 47 |
)
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
async with stdio_client(server_params) as (read, write):
|
| 50 |
async with ClientSession(read, write) as session:
|
| 51 |
await session.initialize()
|
|
@@ -55,15 +58,15 @@ async def lc_mcp_exec(request: str, history) -> str:
|
|
| 55 |
intent = classify_intent(request)
|
| 56 |
|
| 57 |
memory = ConversationBufferMemory(return_messages=True)
|
| 58 |
-
agent = create_react_agent(llm, tools)
|
| 59 |
-
|
| 60 |
prompt = await build_prompt(session, intent, request, tools, table_summary)
|
|
|
|
|
|
|
| 61 |
agent_executor = AgentExecutor(agent=agent, tools=tools, memory=memory, verbose=True)
|
| 62 |
-
|
| 63 |
result = await agent_executor.ainvoke(
|
| 64 |
-
{"messages": prompt}
|
| 65 |
-
config
|
| 66 |
)
|
|
|
|
| 67 |
response = result.get("output", "No response generated")
|
| 68 |
|
| 69 |
return response
|
|
@@ -84,6 +87,8 @@ def get_server_params() -> StdioServerParameters:
|
|
| 84 |
|
| 85 |
async def load_and_enrich_tools(session: ClientSession):
|
| 86 |
tools = await load_mcp_tools(session)
|
|
|
|
|
|
|
| 87 |
return tools
|
| 88 |
|
| 89 |
async def build_prompt(session, intent, request, tools, summary):
|
|
@@ -97,8 +102,10 @@ async def build_prompt(session, intent, request, tools, summary):
|
|
| 97 |
)
|
| 98 |
else:
|
| 99 |
template = conversation_prompt.contents[0].text
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
prompt = PromptTemplate.from_template(template)
|
| 103 |
-
return prompt
|
|
|
|
|
|
|
| 104 |
|
|
|
|
| 18 |
from langchain_core.prompts import PromptTemplate
|
| 19 |
|
| 20 |
|
|
|
|
| 21 |
logger = logging.getLogger(__name__)
|
| 22 |
load_dotenv()
|
| 23 |
+
|
| 24 |
+
|
| 25 |
async def lc_mcp_exec(request: str, history) -> str:
|
| 26 |
"""
|
| 27 |
Execute the full PostgreSQL MCP pipeline: load summary, connect session,
|
|
|
|
| 46 |
api_key=api_key,
|
| 47 |
temperature=0.5,
|
| 48 |
)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
|
| 52 |
async with stdio_client(server_params) as (read, write):
|
| 53 |
async with ClientSession(read, write) as session:
|
| 54 |
await session.initialize()
|
|
|
|
| 58 |
intent = classify_intent(request)
|
| 59 |
|
| 60 |
memory = ConversationBufferMemory(return_messages=True)
|
|
|
|
|
|
|
| 61 |
prompt = await build_prompt(session, intent, request, tools, table_summary)
|
| 62 |
+
|
| 63 |
+
agent = create_react_agent(llm, tools)
|
| 64 |
agent_executor = AgentExecutor(agent=agent, tools=tools, memory=memory, verbose=True)
|
| 65 |
+
|
| 66 |
result = await agent_executor.ainvoke(
|
| 67 |
+
{"messages": prompt}
|
|
|
|
| 68 |
)
|
| 69 |
+
|
| 70 |
response = result.get("output", "No response generated")
|
| 71 |
|
| 72 |
return response
|
|
|
|
| 87 |
|
| 88 |
async def load_and_enrich_tools(session: ClientSession):
|
| 89 |
tools = await load_mcp_tools(session)
|
| 90 |
+
print("Loaded tools:", tools)
|
| 91 |
+
print("Tool types:", [type(t) for t in tools])
|
| 92 |
return tools
|
| 93 |
|
| 94 |
async def build_prompt(session, intent, request, tools, summary):
|
|
|
|
| 102 |
)
|
| 103 |
else:
|
| 104 |
template = conversation_prompt.contents[0].text
|
| 105 |
+
tools_str = "\n".join([f"- {tool.name}: {tool.description}" for tool in tools])
|
| 106 |
+
return template.format( new_request=request, tool_names=tools_str, descriptions=summary )
|
| 107 |
+
#prompt = PromptTemplate.from_template(template)
|
| 108 |
+
#return prompt
|
| 109 |
+
|
| 110 |
+
|
| 111 |
|
postgre_mcp_server.py
CHANGED
|
@@ -133,7 +133,6 @@ async def base_prompt_query() -> str:
|
|
| 133 |
|
| 134 |
You can use the following FastMCP tools. These allow you to create **read-only** queries, such as `SELECT`, `COUNT`, or queries with `GROUP BY`, `ORDER BY`, and similar clauses. You may chain tools together to gather the necessary information before generating your SQL query.
|
| 135 |
|
| 136 |
-
{tools}
|
| 137 |
{tool_names}
|
| 138 |
|
| 139 |
---
|
|
@@ -179,9 +178,7 @@ async def base_prompt_query() -> str:
|
|
| 179 |
|
| 180 |
Please fulfill the following request based on the above context:
|
| 181 |
|
| 182 |
-
{
|
| 183 |
-
|
| 184 |
-
Thought:{agent_scratchpad}
|
| 185 |
"""
|
| 186 |
|
| 187 |
return base_prompt
|
|
|
|
| 133 |
|
| 134 |
You can use the following FastMCP tools. These allow you to create **read-only** queries, such as `SELECT`, `COUNT`, or queries with `GROUP BY`, `ORDER BY`, and similar clauses. You may chain tools together to gather the necessary information before generating your SQL query.
|
| 135 |
|
|
|
|
| 136 |
{tool_names}
|
| 137 |
|
| 138 |
---
|
|
|
|
| 178 |
|
| 179 |
Please fulfill the following request based on the above context:
|
| 180 |
|
| 181 |
+
{new_request}
|
|
|
|
|
|
|
| 182 |
"""
|
| 183 |
|
| 184 |
return base_prompt
|