Я пытаюсь создать 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.
Я пытаюсь создать RAG с помощью langchain. Я хотел бы просмотреть историю чата и иметь возможность цитировать. Я просмотрел руководство по Langchain, но мне сложно собрать воедино историю и цитаты. Вот мой код: [code]### 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]
res = conversational_rag_chain.invoke( {"input": query}, config={ "configurable": {"session_id": "abc123"} }, # constructs a key "abc123" in `store`. ) [/code] Пока все работает хорошо, но я не нашел способа вставить изменение в контекст, чтобы можно было использоватьbind_tools и извлечь цитату, как показано здесь https://python .langchain.com/docs/use_cases/question_answering/citations/#cite-snippets Я пробовал несколько подходов, но не нашел хороших решений, так как у меня их недостаточно. опыт работы с langchain.
Я создал приложение RAG, используя Ollama, Langchain и pgvector. Получение ответа от модели llama3.1:7b занимает около 4–5 секунд.
Я следовал документации Langchain и добавил профилирование в свой код. Результаты профилирования следующие:
Я хотел бы, чтобы токен использовался моей цепочкой RAG каждый раз, когда она вызывается.
Что бы я ни делал, я не могу найти правильный способ вывода общее количество токенов модели Gemini, которую я использую.
import vertexai
from...
Я хотел бы, чтобы токен использовался моей цепочкой RAG каждый раз, когда она вызывается.
Что бы я ни делал, я не могу найти правильный способ вывода общее количество токенов модели Gemini, которую я использую.
import vertexai
from...
У меня есть функция, которая принимает модель языка, хранилище векторов, вопросы и инструменты; и возвращает ответ, в данный момент аргумент инструментов не добавляется, поскольку в этом примере функция .bind_tools не является атрибутом llm -> llm...
Я хотел бы, чтобы токен использовался моей цепочкой RAG каждый раз, когда она вызывается.
Что бы я ни делал, я не могу найти правильный способ вывода общее количество токенов модели Gemini, которую я использую.
import vertexai
from...