Sunday, May 12, 2024

What would be a simple Langchain Tool calling


from langchain import hub

from langchain.agents import AgentExecutor, create_openai_functions_agent

from langchain_community.tools.tavily_search import TavilySearchResults

from langchain_openai import ChatOpenAI


tools = [TavilySearchResults(max_results=1)]


# Get the prompt to use - you can modify this!

prompt = hub.pull("hwchase17/openai-functions-agent")


prompt.messages


# Choose the LLM that will drive the agent

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


# Construct the OpenAI Functions agent

agent = create_openai_functions_agent(llm, tools, prompt)


# Create an agent executor by passing in the agent and tools

agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)


agent_executor.invoke({"input": "what is LangChain?"})


from langchain_core.messages import AIMessage, HumanMessage

agent_executor.invoke(

    {

        "input": "what's my name?",

        "chat_history": [

            HumanMessage(content="hi! my name is bob"),

            AIMessage(content="Hello Bob! How can I assist you today?"),

        ],

    }

)


from langchain_core.messages import AIMessage, HumanMessage

agent_executor.invoke(

    {

        "input": "what's my name?",

        "chat_history": [

            HumanMessage(content="hi! my name is bob"),

            AIMessage(content="Hello Bob! How can I assist you today?"),

        ],

    }

)


references:

https://python.langchain.com/v0.1/docs/modules/agents/agent_types/openai_functions_agent/



No comments:

Post a Comment