Function createOpenAIFunctionsAgent

  • Create an agent that uses OpenAI-style function calling.

    Parameters

    Returns Promise<RunnableSequence<{
        steps: AgentStep[];
    }, AgentAction | AgentFinish>>

    A runnable sequence representing an agent. It takes as input all the same input variables as the prompt passed in does. It returns as output either an AgentAction or AgentFinish.

    Example

    import { AgentExecutor, createOpenAIFunctionsAgent } from "langchain/agents";
    import { pull } from "langchain/hub";
    import type { ChatPromptTemplate } from "@langchain/core/prompts";
    import { AIMessage, HumanMessage } from "@langchain/core/messages";

    import { ChatOpenAI } from "@langchain/openai";

    // Define the tools the agent will have access to.
    const tools = [...];

    // Get the prompt to use - you can modify this!
    // If you want to see the prompt in full, you can at:
    // https://smith.langchain.com/hub/hwchase17/openai-functions-agent
    const prompt = await pull<ChatPromptTemplate>(
    "hwchase17/openai-functions-agent"
    );

    const llm = new ChatOpenAI({
    temperature: 0,
    });

    const agent = await createOpenAIFunctionsAgent({
    llm,
    tools,
    prompt,
    });

    const agentExecutor = new AgentExecutor({
    agent,
    tools,
    });

    const result = await agentExecutor.invoke({
    input: "what is LangChain?",
    });

    // With chat history
    const result2 = await agentExecutor.invoke({
    input: "what's my name?",
    chat_history: [
    new HumanMessage("hi! my name is cob"),
    new AIMessage("Hello Cob! How can I assist you today?"),
    ],
    });

Generated using TypeDoc