Однако, когда я использую оператор |, я получаю следующую ошибку:
Код: Выделить всё
ValueError: Invalid input type . Must be a PromptValue, str, or list of BaseMessages.
Код: Выделить всё
def chatbot(input_user_message):
# creating a prompt template
chat_prompt = ChatPromptTemplate.from_messages(
[
("system", "You are a helpful AI assistant."),
MessagesPlaceholder(variable_name="history_messages"),
("human", "{input_user_message}"),
]
)
# initializing OpenAI Chat model
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0.7)
trimmer = trim_messages(
max_tokens=100,
strategy="last",
token_counter=llm,
# Usually, we want to keep the SystemMessage
include_system=True,
# start_on="human" makes sure we produce a valid chat history
start_on="human",
)
def get_session_history(session_id):
if session_id not in st.session_state.store:
st.session_state.store[session_id] = ChatMessageHistory()
else:
st.session_state.store[session_id].messages = trimmer.invoke(st.session_state.store[session_id].messages)
return st.session_state.store[session_id]
# Initializing the output parser
output_parser = StrOutputParser()
# Creating an LLMChain with the prompt and memory
#conversation_chain = LLMChain(
# llm=llm,
# prompt=chat_prompt,
# output_parser=output_parser, # To parse the LLM's response into a string for display
# verbose=True, # Displays detailed logs for debugging
#)
conversation_chain = llm | chat_prompt | output_parser
model_with_memory = RunnableWithMessageHistory(
conversation_chain,
get_session_history,
input_messages_key="input_user_message",
history_messages_key="history_messages",
)
session_id = "1234"
# config = {"configurable": {"session_id": session_id}}
response = model_with_memory.invoke(
{"input_user_message": input_user_message},
{"configurable": {"session_id": session_id}},
)
print(response)
return response["text"]
Любые рекомендации по правильному использованию оператора | с ChatOpenAI и ChatPromptTemplate.
Я использую python==3.12.7 langchain==0.3.9 openai==1.55.0 langchain-openai==0.2.10
Подробнее здесь: https://stackoverflow.com/questions/792 ... chatopenai