Skip to content

Commit 0fb959a

Browse files
authored
refactor: Replace Chains with Runnables across applicable classes (#84)
This pull request refactors the codebase to replace the legacy "Chain" terminology with the Langchains "Runnable" terminology. It updates class names, file names, and dependency injections throughout the project, improving code clarity and consistency. The changes also rename tracer classes and their usage to match the new naming convention. ### Core Terminology Refactor * Replaced all instances of `AsyncChain` with `AsyncRunnable` across the codebase, updating class definitions in files such as `information_enhancer.py`, `summarizer.py`, `graph_base.py`, and answer generation/rephrasing chains. (`[[1]](diffhunk://#diff-9dc8459159718f57a3fa0bf9957a7371f3a048f48316801a73dc308e41f2bb1bL9-R15)`, `[[2]](diffhunk://#diff-667a3ff4405d85b37364dacfd6010fc86d7f54378759061f54ea92a94bb28046L8-R14)`, `[[3]](diffhunk://#diff-5339bc54ab240f4fc8ff429c1eb10b9426078d68eb135d4c5d07762da0b3d6a4L10-R13)`, `[[4]](diffhunk://#diff-383d1b454a8f86bcd6e83561f9404bf94b3fe9877d1a045bcb3586105b0fa5cdL10-R17)`, `[[5]](diffhunk://#diff-8133279f7c0d324fdfc28838bb487748c091179dc11fc23c79c4f3f489bc3827L9-R16)`) * Renamed the file `async_chain.py` to `async_runnable.py` and updated the base class name accordingly. (`[libs/rag-core-lib/src/rag_core_lib/runnables/async_runnable.pyL10-R10](diffhunk://#diff-ab5426af1a107aabc1976b764b6cacb37827d2196a730c172d03f5da64ad39c9L10-R10)`) ### Tracer Class and Dependency Updates * Replaced `TracedGraph` with `TracedRunnable` and updated the corresponding file name from `traced_chain.py` to `traced_runnable.py`. (`[libs/rag-core-lib/src/rag_core_lib/tracers/traced_runnable.pyL10-R16](diffhunk://#diff-d6877e89effd4fb71dd24a4ce13a5ded94fac87eab9ff78eee0345d6f7100b83L10-R16)`) * Replaced `LangfuseTracedGraph` with `LangfuseTracedRunnable` and updated the file name from `langfuse_traced_chain.py` to `langfuse_traced_runnable.py`. (`[libs/rag-core-lib/src/rag_core_lib/impl/tracers/langfuse_traced_runnable.pyL9-R12](diffhunk://#diff-b9a916afc928c0acd5b57f6bd46a1213dc898d37f01bd2372d0033cb1941b7caL9-R12)`) * Updated dependency containers in both `admin-api-lib` and `rag-core-api` to use `LangfuseTracedRunnable` instead of `LangfuseTracedGraph`. (`[[1]](diffhunk://#diff-8b7c1816cb3e0a40b7965721c550eefdc184c5d914ec023e36527255613381e7L68-R68)`, `[[2]](diffhunk://#diff-8b7c1816cb3e0a40b7965721c550eefdc184c5d914ec023e36527255613381e7L150-R150)`, `[[3]](diffhunk://#diff-483b37f4ebbc24c973c3b170542171d90c65f3c6b68f1a6d598ce8964a94be7bL67-R67)`, `[[4]](diffhunk://#diff-483b37f4ebbc24c973c3b170542171d90c65f3c6b68f1a6d598ce8964a94be7bL221-R221)`) ### API and Service Integration * Updated constructor parameters in API endpoint and service classes (`DefaultChat`, `UseCaseChat`) to accept `TracedRunnable` instead of `TracedGraph`. (`[[1]](diffhunk://#diff-3a92501b36d164b12ce941a944afcbfc440f7fb7093635cb1a405b714ecc6345L8-R14)`, `[[2]](diffhunk://#diff-3532c3cdfb76374337d778f1aeef03f010fb44852319f3e09dcbfc1025852d97L7-R13)`) These changes ensure the codebase is up-to-date with the latest LangChain abstractions and make the tracing and runnable logic clearer and more maintainable.
1 parent d0fb145 commit 0fb959a

File tree

13 files changed

+23
-23
lines changed

13 files changed

+23
-23
lines changed

libs/admin-api-lib/src/admin_api_lib/dependency_container.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
from rag_core_lib.impl.settings.ollama_llm_settings import OllamaSettings
6666
from rag_core_lib.impl.settings.rag_class_types_settings import RAGClassTypeSettings
6767
from rag_core_lib.impl.settings.stackit_vllm_settings import StackitVllmSettings
68-
from rag_core_lib.impl.tracers.langfuse_traced_chain import LangfuseTracedGraph
68+
from rag_core_lib.impl.tracers.langfuse_traced_runnable import LangfuseTracedRunnable
6969
from rag_core_lib.impl.utils.async_threadsafe_semaphore import AsyncThreadsafeSemaphore
7070

7171

@@ -147,7 +147,7 @@ class DependencyContainer(DeclarativeContainer):
147147
summary_enhancer,
148148
)
149149
information_enhancer = Singleton(
150-
LangfuseTracedGraph,
150+
LangfuseTracedRunnable,
151151
inner_chain=untraced_information_enhancer,
152152
settings=langfuse_settings,
153153
)

libs/admin-api-lib/src/admin_api_lib/information_enhancer/information_enhancer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
from langchain_core.documents import Document
77
from langchain_core.runnables import RunnableConfig
88

9-
from rag_core_lib.chains.async_chain import AsyncChain
9+
from rag_core_lib.runnables.async_runnable import AsyncRunnable
1010

1111
RetrieverInput = list[Document]
1212
RetrieverOutput = list[Document]
1313

1414

15-
class InformationEnhancer(AsyncChain[RetrieverInput, RetrieverOutput], ABC):
15+
class InformationEnhancer(AsyncRunnable[RetrieverInput, RetrieverOutput], ABC):
1616
"""The base class for an information enhancer."""
1717

1818
@abstractmethod

libs/admin-api-lib/src/admin_api_lib/summarizer/summarizer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55

66
from langchain_core.runnables import RunnableConfig
77

8-
from rag_core_lib.chains.async_chain import AsyncChain
8+
from rag_core_lib.runnables.async_runnable import AsyncRunnable
99

1010
SummarizerInput = str
1111
SummarizerOutput = str
1212

1313

14-
class Summarizer(AsyncChain[SummarizerInput, SummarizerOutput], ABC):
14+
class Summarizer(AsyncRunnable[SummarizerInput, SummarizerOutput], ABC):
1515
"""Baseclass for summarizers."""
1616

1717
@abstractmethod

libs/rag-core-api/src/rag_core_api/dependency_container.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
from rag_core_lib.impl.settings.ollama_llm_settings import OllamaSettings
6565
from rag_core_lib.impl.settings.rag_class_types_settings import RAGClassTypeSettings
6666
from rag_core_lib.impl.settings.stackit_vllm_settings import StackitVllmSettings
67-
from rag_core_lib.impl.tracers.langfuse_traced_chain import LangfuseTracedGraph
67+
from rag_core_lib.impl.tracers.langfuse_traced_runnable import LangfuseTracedRunnable
6868
from rag_core_lib.impl.utils.async_threadsafe_semaphore import AsyncThreadsafeSemaphore
6969

7070

@@ -218,7 +218,7 @@ class DependencyContainer(DeclarativeContainer):
218218

219219
# wrap graph in tracer
220220
traced_chat_graph = Singleton(
221-
LangfuseTracedGraph,
221+
LangfuseTracedRunnable,
222222
inner_chain=chat_graph,
223223
settings=langfuse_settings,
224224
)

libs/rag-core-api/src/rag_core_api/graph/graph_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
from langchain_core.runnables.utils import Input, Output
88
from langgraph.graph import StateGraph
99

10-
from rag_core_lib.chains.async_chain import AsyncChain
10+
from rag_core_lib.runnables.async_runnable import AsyncRunnable
1111

1212

13-
class GraphBase(AsyncChain[Input, Output], ABC):
13+
class GraphBase(AsyncRunnable[Input, Output], ABC):
1414
"""
1515
Base class for a langgraph graph.
1616

libs/rag-core-api/src/rag_core_api/impl/answer_generation_chains/answer_generation_chain.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
from langchain_core.output_parsers import StrOutputParser
88

99
from rag_core_api.impl.graph.graph_state.graph_state import AnswerGraphState
10-
from rag_core_lib.chains.async_chain import AsyncChain
10+
from rag_core_lib.runnables.async_runnable import AsyncRunnable
1111
from rag_core_lib.impl.langfuse_manager.langfuse_manager import LangfuseManager
1212

1313
RunnableInput = AnswerGraphState
1414
RunnableOutput = str
1515

1616

17-
class AnswerGenerationChain(AsyncChain[RunnableInput, RunnableOutput]):
17+
class AnswerGenerationChain(AsyncRunnable[RunnableInput, RunnableOutput]):
1818
"""Base class for LLM answer generation chain."""
1919

2020
def __init__(self, langfuse_manager: LangfuseManager):

libs/rag-core-api/src/rag_core_api/impl/answer_generation_chains/rephrasing_chain.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
from langchain_core.runnables import Runnable, RunnableConfig
77

88
from rag_core_api.impl.graph.graph_state.graph_state import AnswerGraphState
9-
from rag_core_lib.chains.async_chain import AsyncChain
9+
from rag_core_lib.runnables.async_runnable import AsyncRunnable
1010
from rag_core_lib.impl.langfuse_manager.langfuse_manager import LangfuseManager
1111

1212
RunnableInput = AnswerGraphState
1313
RunnableOutput = str
1414

1515

16-
class RephrasingChain(AsyncChain[RunnableInput, RunnableOutput]):
16+
class RephrasingChain(AsyncRunnable[RunnableInput, RunnableOutput]):
1717
"""Base class for rephrasing of the input question."""
1818

1919
def __init__(self, langfuse_manager: LangfuseManager):

libs/rag-core-api/src/rag_core_api/impl/api_endpoints/default_chat.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
from rag_core_api.api_endpoints.chat import Chat
66
from rag_core_api.models.chat_request import ChatRequest
77
from rag_core_api.models.chat_response import ChatResponse
8-
from rag_core_lib.tracers.traced_chain import TracedGraph
8+
from rag_core_lib.tracers.traced_runnable import TracedRunnable
99

1010

1111
class DefaultChat(Chat):
1212
"""DefaultChat is a class that handles chat interactions using a traced graph."""
1313

14-
def __init__(self, chat_graph: TracedGraph):
14+
def __init__(self, chat_graph: TracedRunnable):
1515
"""
1616
Initialize the DefaultChat instance.
1717

libs/rag-core-lib/src/rag_core_lib/impl/tracers/langfuse_traced_chain.py renamed to libs/rag-core-lib/src/rag_core_lib/impl/tracers/langfuse_traced_runnable.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
from langfuse.langchain import CallbackHandler
77

88
from rag_core_lib.impl.settings.langfuse_settings import LangfuseSettings
9-
from rag_core_lib.tracers.traced_chain import TracedGraph
9+
from rag_core_lib.tracers.traced_runnable import TracedRunnable
1010

1111

12-
class LangfuseTracedGraph(TracedGraph):
12+
class LangfuseTracedRunnable(TracedRunnable):
1313
"""A class to trace the execution of a Runnable using Langfuse.
1414
1515
This class wraps an inner Runnable and adds tracing capabilities using the Langfuse tracer.

0 commit comments

Comments
 (0)