Wednesday, May 15, 2024

What would be a simple tool

from langchain_core.messages import HumanMessage, ToolMessage

from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-3.5-turbo-0125")

from langchain_core.tools import tool


@tool

def add(a: int, b: int) -> int:

    """Adds a and b.


    Args:

        a: first int

        b: second int

    """

    return a + b



@tool

def multiply(a: int, b: int) -> int:

    """Multiplies a and b.


    Args:

        a: first int

        b: second int

    """

    return a * b



@tool

def ssh_tool(un:str, pwd:str) -> 


print ("About to add the tools")

tools = [add, multiply]

llm_with_tools = llm.bind_tools(tools)

print("Tools are bound with the LLM")


messages = [HumanMessage("How much is 3 & 300 added upto?")]

print("Message created invoking LLM with the tools")

ai_msg = llm_with_tools.invoke(messages)

print("Tool invoked and got the response as ai")

messages.append(ai_msg)


for tool_call in ai_msg.tool_calls:

    selected_tool = {"add": add, "multiply": multiply}[tool_call["name"].lower()]

    tool_output = selected_tool.invoke(tool_call["args"])

    print("tool_output is ",tool_output)

    messages.append(ToolMessage(tool_output, tool_call_id=tool_call["id"]))

print("Total messages retrieved are ", messages)


references:


No comments:

Post a Comment