WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/application/flow/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import re
import threading
from typing import Iterator

from maxkb.const import CONFIG
from django.http import StreamingHttpResponse
from langchain_core.messages import BaseMessageChunk, BaseMessage, ToolMessage, AIMessageChunk
from langchain_mcp_adapters.client import MultiServerMCPClient
Expand Down Expand Up @@ -313,7 +313,7 @@ async def _yield_mcp_response(chat_model, message_list, mcp_servers, mcp_output_
try:
client = MultiServerMCPClient(json.loads(mcp_servers))
tools = await client.get_tools()
agent = create_react_agent(chat_model, tools)
agent = create_react_agent(chat_model, tools).configure(recursion_limit=(int(CONFIG.get("LANGCHAIN_GRAPH_RECURSION_LIMIT", '25'))))
response = agent.astream({"messages": message_list}, stream_mode='messages')

# 用于存储工具调用信息(按 tool_id)以及按 index 聚合分片
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks generally structured correctly but has a few areas where improvements can be made:

  1. Imports: The maxkb.const module is imported, which suggests that you have a custom module named const.py. Ensure this file exists and contains the necessary configuration variables.

  2. Tool Configuration: In the _yield_mcp_response function, there's an attempt to configure the reaction agent with a maximum recursion limit fetched from the CONFIG dictionary using CONFIG.get("LANGCHAIN_GRAPH_RECURSION_LIMIT", '25'). Make sure that "LANGCHAIN_GRAPH_RECURSION_LIMIT" exists in your CONFIG settings or default it appropriately if not set.

  3. Code Comments: Add comments describing what each section of the code does, especially around the recursive limiting logic and how the response is being streamed.

  4. Error Handling: Consider adding more detailed error handling to manage cases such as failed tool retrieval from MCP servers or issues during streaming.

Here’s a revised version of the key part of your code with these suggestions:

import re
import threading
from typing import Iterator

# Import your CONFIG constants from the appropriate module
from maxkb.const import CONFIG
from django.http import StreamingHttpResponse
from langchain_core.messages import BaseMessageChunk, BaseMessage, ToolMessage, AIMessageChunk
from langchain_mcp_adapters.client import MultiServerMCPClient

async def _yield_mcp_response(chat_model, message_list, mcp_servers, mcp_output_queue):
    try:
        # Initialize theMulti Server Management Console (MCP) client
        client = MultiServerMCPClient(json.loads(mcp_servers))

        # Fetch available tools from the MCP servers
        tools = await client.get_tools()

        // Create a reaction agent with specific configurations
        # Set recursion limit based on the configuration
       agent = create_react_agent(
            chat_model,
            tools,
            config={
                "RECURSION_LIMIT": int(CONFIG.get("LANGCHAIN_GRAPH_RECURSION_LIMIT", '25'))
            }
        )

        # Stream responses from the agent
        response = agent.astream(
            {
                "messages": message_list,
                "stream_mode": 'messages'
            },
            stream_mode='message_chunks'  # Adjust mode as needed for better performance
        )
        
        # Process each chunk received from the streaming generator
        async for content in response.stream():
            if isinstance(content, BaseMessageChunk):
                yield StreamingHttpResponse(chunk_to_bytes(content.content), status=200)

This revision ensures clarity about the purpose of variable assignments, provides adequate comments explaining critical parts of the code, and includes default values and logging that could help diagnose problems when running the application.

Expand Down
Loading