File size: 2,115 Bytes
6cbca40
61e4b1e
 
 
 
6cbca40
 
 
61e4b1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6cbca40
 
 
 
 
 
 
 
 
7f15e1c
6cbca40
 
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
from langchain_core.tools import tool
from langchain_core.messages import ToolMessage
from langgraph.types import Command
from typing import Annotated, Any
from langgraph_swarm.handoff import _get_field, InjectedState
from loguru import logger


def create_end_conversation_tool(
    name: str = "end_conversation",
    description: str = "End the conversation naturally when the discussion has reached its conclusion or the user indicates they want to stop",
) -> callable:
    """Create a tool that can end the conversation.

    Args:
        name: Name of the tool to use for ending the conversation.
        description: Description for the end conversation tool.

    Returns:
        A tool that when called will end the conversation.
    """

    @tool(name, description=description)
    def end_conversation(
        reason: str,
        # Annotation is typed as Any instead of StateLike. StateLike
        # trigger validation issues from Pydantic / langchain_core interaction.
        # https://github.com/langchain-ai/langchain/issues/32067
        state: Annotated[Any, InjectedState],
        tool_call_id: str,
    ) -> Command:
        """End the conversation with the given reason.

        Args:
            reason: The reason for ending the conversation
        """
        logger.info(f"Conversation ended. Reason: {reason}")

        tool_message = ToolMessage(
            content=f"Conversation ended: {reason}",
            name=name,
            tool_call_id=tool_call_id,
        )

        # Return a Command that goes to END node
        return Command(
            # goto="END",
            graph=Command.PARENT,
            update={
                "messages": [*_get_field(state, "messages"), tool_message],
            },
        )

    return end_conversation


@tool
def function_name(
    input: str,
) -> str:
    """
    Mô tả chức năng của hàm này.
    """
    logger.info(f"Received input: {input}")
    # Thực hiện các thao tác cần thiết với input
    result: str = f"Processed: {input}"
    logger.info(f"Returning result: {result}")
    return result