[Python] How to Use Template in Langchain to Insert Results from Chain for Further Reasoning?

Discussão em 'Python' iniciado por Stack, Março 14, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working with Langchain and OpenAI to develop a conversational AI. I've integrated multiple tools into the chain and am using a template to structure the conversation. However, I'm stuck on how to use the results from the chain (chain.invoke(...)) in the template to allow the agent to continue reasoning based on these results. Here's the relevant part of my code:

    from langchain_openai import ChatOpenAI
    from dotenv import load_dotenv
    import os
    from langchain_core.prompts import ChatPromptTemplate
    from langchain_core.tools import tool

    load_dotenv()
    api_key = os.getenv("OPENAI_API_KEY")
    if api_key is not None:
    os.environ["OPENAI_API_KEY"] = api_key
    else:
    raise ValueError("OPENAI_API_KEY environment variable is not set.")

    llm = ChatOpenAI(
    model="gpt-4o",
    temperature=0,
    )

    template = ChatPromptTemplate([
    ("system", "You are a helpful AI bot. Your name is Bob."),
    ("human", "Hello, how are you doing?"),
    ("ai", "I'm doing well, thanks!"),
    ("human", "{user_input}"),
    ("placeholder", "{conversation}")
    ])

    @tool
    def weather(city: str) -> str:
    """Gives the weather in a given city"""
    return f"The weather in {city} is sunny"

    @tool
    def sum_numbers(numbers: str) -> str:
    """Sums two numbers"""
    return str(sum(map(int, numbers.split())))

    llm_with_tools = llm.bind_tools([weather, sum_numbers])

    chain = template | llm_with_tools

    res = chain.invoke({"user_input": "What is the weather in Tokyo? also what is 3 + 1? Give me the answer as if you are a cat"})


    How can I modify the template or the invocation so that Bob can use the results from chain.invoke(...) for further reasoning in a continued conversation? For instance, after obtaining the weather and the sum, I want the AI to use these results in its next interactions.

    I'm using

    langchain==0.3.20
    langchain-community==0.3.19
    langchain-openai==0.3.8
    openai==1.66.3
    python-dotenv==1.0.1

    Continue reading...

Compartilhe esta Página