Langchain RAG с историей и цитатамиPython

Программы на Python
Ответить Пред. темаСлед. тема
Anonymous
 Langchain RAG с историей и цитатами

Сообщение Anonymous »

Я пытаюсь создать RAG с помощью langchain. Я хотел бы просмотреть историю чата и иметь возможность цитировать. Я просмотрел руководство по Langchain, но мне сложно собрать воедино историю и цитаты.
Вот мой код:

Код: Выделить всё

### Contextualize question ### This is used to forumlate the right question to the retriever
contextualize_q_system_prompt = """Given a chat history and the latest user question \
which might reference context in the chat history, formulate a standalone question \
which can be understood without the chat history. Do NOT answer the question, \
just reformulate it if needed and otherwise return it as is."""
contextualize_q_prompt = ChatPromptTemplate.from_messages(
[
("system", contextualize_q_system_prompt),
MessagesPlaceholder("chat_history"),
("human", "{input}"),
]
)
history_aware_retriever = create_history_aware_retriever(
llm_retriever, retriever, contextualize_q_prompt
)
### Answer question ### This is used to answer the question based on the context
qa_system_prompt = """You are an assistant for question-answering tasks. \
Use the following pieces of retrieved context to answer the question. \
If you don't know the answer, just say that you don't know. \
Use five sentences maximum and keep the answer concise.\

Here it is the context: {context}"""
qa_prompt = ChatPromptTemplate.from_messages(
[
("system", qa_system_prompt),
MessagesPlaceholder("chat_history"),
("human", "{input}"),
]
)
question_answer_chain = create_stuff_documents_chain(llm__main, qa_prompt)

rag_chain = create_retrieval_chain(history_aware_retriever, question_answer_chain)### Statefully manage chat history ###
store = {}

def get_session_history(session_id: str) -> BaseChatMessageHistory:
if session_id not in store:
store[session_id] = session_history #ChatMessageHistory() #hack found for now as the RunnableWithMessageHistory requires a function. To be changed when we'll handle multiple sessions
return store[session_id]

conversational_rag_chain = RunnableWithMessageHistory(
rag_chain_2,
get_session_history,
input_messages_key="input",
history_messages_key="chat_history",
output_messages_key="answer",
)

res = conversational_rag_chain.invoke(
{"input": query},
config={
"configurable": {"session_id": "abc123"}
},  # constructs a key "abc123" in `store`.
)
Пока все работает хорошо, но я не нашел способа вставить изменение в контекст, чтобы можно было использоватьbind_tools и извлечь цитату, как показано здесь https://python .langchain.com/docs/use_cases/question_answering/citations/#cite-snippets
Я пробовал несколько подходов, но не нашел хороших решений, так как у меня их недостаточно. опыт работы с langchain.

Подробнее здесь: https://stackoverflow.com/questions/784 ... -citations
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Python»