[Python] Why the Langchain placeholder is not being called?

Discussão em 'Python' iniciado por Stack, Outubro 6, 2024.

  1. Stack

    Stack Membro Participativo

    My goal is to insert the language placeholder into the invoke method but currently the response is always in english. I follow this tutorial -> https://python.langchain.com/v0.2/docs/tutorials/chatbot/

    Any reason why?

    # Securely input the OpenAI API key
    api_key = os.getenv("OPENAI_API_KEY")

    # Initialize the ChatOpenAI model with the API key
    model = ChatOpenAI(model="gpt-3.5-turbo", api_key=api_key, verbose=True)


    def invoke_model(session_id: str, message: str):
    # Define the prompt with ChatPromptTemplate
    prompt = ChatPromptTemplate.from_messages(
    [
    ("system", "You are a helpful assistant. Answer all questions to the best of your ability in {language}."),
    MessagesPlaceholder(variable_name="messages"),
    ]
    )

    # Create the chain using the prompt and the model
    chain = prompt | model

    # Use with_message_history to handle messages
    with_message_history = RunnableWithMessageHistory(chain, get_session_history, input_messages_key="messages")

    human_message = HumanMessage(content=message)
    session_history = get_session_history(session_id)
    session_history.add_message(human_message) # Add the human message to the history
    config = {"configurable": {"session_id": session_id}}
    response = with_message_history.invoke({"messages": [human_message], "language": "Spanish"}, config=config)
    assistant_message = AIMessage(content=response.content)
    session_history.add_message(assistant_message) # Add the assistant's response to the history
    return response.content

    Continue reading...

Compartilhe esta Página