base.py — langchain Source File
Architecture documentation for base.py, a python file in the langchain codebase. 8 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR 3b8bdb2a_b739_e54c_2053_d5002bf4c100["base.py"] 2485b66a_3839_d0b6_ad9c_a4ff40457dc6["langchain_core._api"] 3b8bdb2a_b739_e54c_2053_d5002bf4c100 --> 2485b66a_3839_d0b6_ad9c_a4ff40457dc6 435e49bf_bb2e_2016_ead7_0afb9d57ad71["langchain_core.prompts"] 3b8bdb2a_b739_e54c_2053_d5002bf4c100 --> 435e49bf_bb2e_2016_ead7_0afb9d57ad71 dd5e7909_a646_84f1_497b_cae69735550e["pydantic"] 3b8bdb2a_b739_e54c_2053_d5002bf4c100 --> dd5e7909_a646_84f1_497b_cae69735550e f85fae70_1011_eaec_151c_4083140ae9e5["typing_extensions"] 3b8bdb2a_b739_e54c_2053_d5002bf4c100 --> f85fae70_1011_eaec_151c_4083140ae9e5 8d1ab66e_47c1_1140_c3a5_5112af3b1cac["langchain_classic.base_memory"] 3b8bdb2a_b739_e54c_2053_d5002bf4c100 --> 8d1ab66e_47c1_1140_c3a5_5112af3b1cac f3ae3b3c_9ec6_d546_215d_dddc9cc8994f["langchain_classic.chains.conversation.prompt"] 3b8bdb2a_b739_e54c_2053_d5002bf4c100 --> f3ae3b3c_9ec6_d546_215d_dddc9cc8994f 4044d59c_c0a5_a371_f49b_bea3da4e20ac["langchain_classic.chains.llm"] 3b8bdb2a_b739_e54c_2053_d5002bf4c100 --> 4044d59c_c0a5_a371_f49b_bea3da4e20ac 9a4995a1_3b78_3602_ba67_6a2f310fb821["langchain_classic.memory.buffer"] 3b8bdb2a_b739_e54c_2053_d5002bf4c100 --> 9a4995a1_3b78_3602_ba67_6a2f310fb821 style 3b8bdb2a_b739_e54c_2053_d5002bf4c100 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
"""Chain that carries on a conversation and calls an LLM."""
from langchain_core._api import deprecated
from langchain_core.prompts import BasePromptTemplate
from pydantic import ConfigDict, Field, model_validator
from typing_extensions import Self, override
from langchain_classic.base_memory import BaseMemory
from langchain_classic.chains.conversation.prompt import PROMPT
from langchain_classic.chains.llm import LLMChain
from langchain_classic.memory.buffer import ConversationBufferMemory
@deprecated(
since="0.2.7",
alternative="langchain_core.runnables.history.RunnableWithMessageHistory",
removal="1.0",
)
class ConversationChain(LLMChain):
"""Chain to have a conversation and load context from memory.
This class is deprecated in favor of `RunnableWithMessageHistory`. Please refer
to this tutorial for more detail: https://python.langchain.com/docs/tutorials/chatbot/
`RunnableWithMessageHistory` offers several benefits, including:
- Stream, batch, and async support;
- More flexible memory handling, including the ability to manage memory
outside the chain;
- Support for multiple threads.
Below is a minimal implementation, analogous to using `ConversationChain` with
the default `ConversationBufferMemory`:
```python
from langchain_core.chat_history import InMemoryChatMessageHistory
from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain_openai import ChatOpenAI
store = {} # memory is maintained outside the chain
def get_session_history(session_id: str) -> InMemoryChatMessageHistory:
if session_id not in store:
store[session_id] = InMemoryChatMessageHistory()
return store[session_id]
model = ChatOpenAI(model="gpt-3.5-turbo-0125")
chain = RunnableWithMessageHistory(model, get_session_history)
chain.invoke(
"Hi I'm Bob.",
config={"configurable": {"session_id": "1"}},
) # session_id determines thread
```
Memory objects can also be incorporated into the `get_session_history` callable:
// ... (90 more lines)
Domain
Subdomains
Classes
Dependencies
- langchain_classic.base_memory
- langchain_classic.chains.conversation.prompt
- langchain_classic.chains.llm
- langchain_classic.memory.buffer
- langchain_core._api
- langchain_core.prompts
- pydantic
- typing_extensions
Source
Frequently Asked Questions
What does base.py do?
base.py is a source file in the langchain codebase, written in python. It belongs to the AgentOrchestration domain, ClassicChains subdomain.
What does base.py depend on?
base.py imports 8 module(s): langchain_classic.base_memory, langchain_classic.chains.conversation.prompt, langchain_classic.chains.llm, langchain_classic.memory.buffer, langchain_core._api, langchain_core.prompts, pydantic, typing_extensions.
Where is base.py in the architecture?
base.py is located at libs/langchain/langchain_classic/chains/conversation/base.py (domain: AgentOrchestration, subdomain: ClassicChains, directory: libs/langchain/langchain_classic/chains/conversation).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free