stateful (they have memory); to ensure that this state isn't shared between dataset runs, we will pass in a chain_factory (aka a constructor) function to initialize for each call.In this case, we will test an agent that uses OpenAI's function calling endpoints.from langchain.chat_models import ChatOpenAIfrom langchain.agents import AgentType, initialize_agent, load_tools, AgentExecutorfrom langchain.agents.format_scratchpad import format_to_openai_functionsfrom langchain.agents.output_parsers import OpenAIFunctionsAgentOutputParserfrom langchain.tools.render import format_tool_to_openai_functionfrom langchain import hub# Since chains can be stateful (e.g. they can have memory), we provide# a way to initialize a new chain for each row in the dataset. This is done# by passing in a factory function that returns a new chain for each row.def agent_factory(prompt):        llm_with_tools = llm.bind(        functions=[format_tool_to_openai_function(t) for t in tools]    )    runnable_agent = (            {                "input": lambda x: x["input"],                "agent_scratchpad": lambda x: format_to_openai_functions(x['intermediate_steps'])            }              | prompt              | llm_with_tools              | OpenAIFunctionsAgentOutputParser()    )    return  AgentExecutor(agent=runnable_agent, tools=tools, handle_parsing_errors=True)3. Configure evaluation‚ÄãManually comparing the results of chains in the UI is effective, but it can be time consuming.
Open In Colab
Open In Colab ->: stateful (they have memory); to ensure that this state isn't shared between dataset runs, we will pass in a chain_factory (aka a constructor) function to initialize for each call.In this case, we will test an agent that uses OpenAI's function calling endpoints.from langchain.chat_models import ChatOpenAIfrom langchain.agents import AgentType, initialize_agent, load_tools, AgentExecutorfrom langchain.agents.format_scratchpad import format_to_openai_functionsfrom langchain.agents.output_parsers import OpenAIFunctionsAgentOutputParserfrom langchain.tools.render import format_tool_to_openai_functionfrom langchain import hub# Since chains can be stateful (e.g. they can have memory), we provide# a way to initialize a new chain for each row in the dataset. This is done# by passing in a factory function that returns a new chain for each row.def agent_factory(prompt):        llm_with_tools = llm.bind(        functions=[format_tool_to_openai_function(t) for t in tools]    )    runnable_agent = (            {                "input": lambda x: x["input"],                "agent_scratchpad": lambda x: format_to_openai_functions(x['intermediate_steps'])            }              | prompt              | llm_with_tools              | OpenAIFunctionsAgentOutputParser()    )    return  AgentExecutor(agent=runnable_agent, tools=tools, handle_parsing_errors=True)3. Configure evaluation‚ÄãManually comparing the results of chains in the UI is effective, but it can be time consuming.