Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap-up retrieval chain into LangGraph #50

Merged
merged 9 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions bin/chat-chainlit.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import chainlit as cl
from dotenv import load_dotenv

from conversational_chain.graph import RAGGraphWithMemory
from retreival_chain import initialize_retrieval_chain
from util.embedding_environment import EmbeddingEnvironment

Expand Down Expand Up @@ -57,14 +58,14 @@ async def start() -> None:
chat_profile = cl.user_session.get("chat_profile")

embeddings_directory = EmbeddingEnvironment.get_dir(env)
llm_chain = initialize_retrieval_chain(
llm_graph = initialize_retrieval_chain(
env,
embeddings_directory,
False,
False,
hf_model=EmbeddingEnvironment.get_model(env),
)
cl.user_session.set("llm_chain", llm_chain)
cl.user_session.set("llm_graph", llm_graph)

initial_message: str = f"""Welcome to {chat_profile} your interactive chatbot for exploring Reactome!
Ask me about biological pathways and processes"""
Expand All @@ -73,13 +74,15 @@ async def start() -> None:

@cl.on_message
async def main(message: cl.Message) -> None:
llm_chain = cl.user_session.get("llm_chain")
llm_graph: RAGGraphWithMemory = cl.user_session.get("llm_graph")
cb = cl.AsyncLangchainCallbackHandler(
stream_final_answer=True, answer_prefix_tokens=["FINAL", "ANSWER"]
)
cb.answer_reached = True

res = await llm_chain.ainvoke(message.content, callbacks=[cb])
res = await llm_graph.ainvoke(
message.content,
callbacks = [cb],
configurable = {"thread_id": "0"} # single thread
)
if cb.has_streamed_final_answer and cb.final_stream is not None:
await cb.final_stream.update()
else:
Expand Down
169 changes: 150 additions & 19 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ torch = {version = "^2.4.0+cpu", source = "pytorch_cpu"}
langchain-chroma = "^0.1.4"
langchain-ollama = "^0.2.0"
lark = "^1.2.2"
langgraph = "^0.2.39"

[tool.poetry.group.dev.dependencies]
ruff = "^0.7.1"
Expand Down
11 changes: 8 additions & 3 deletions src/conversational_chain/chain.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
from langchain.chains import (create_history_aware_retriever,
create_retrieval_chain)
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain.chains.history_aware_retriever import \
create_history_aware_retriever
from langchain.chains.retrieval import create_retrieval_chain
from langchain_core.language_models import LanguageModelLike
from langchain_core.retrievers import RetrieverLike

from system_prompt.reactome_prompt import contextualize_q_prompt, qa_prompt


class RAGChainWithMemory:
def __init__(self, memory, retriever, llm):
def __init__(
self, memory, retriever: RetrieverLike, llm: LanguageModelLike
):
"""
Initializes the Retrieval-Augmented Generation (RAG) chain with memory.
"""
Expand Down
Loading
Loading