LangChain¶
LangChain is an open-source framework designed to build applications like RAG and Agents with Large Language Models (LLMs). It provides following key components:
- Chat models and prompts: Build LLM applications that can scale across resources, using prompt templates and chat models.
- Semantic search: Process large research datasets and PDFs efficiently by combining document loaders, embedding models, and vector stores.
- Classification: Classify text into categories or labels using chat models with structured outputs.
- Extraction: Extract structured data from text and other unstructured media using chat models and few-shot examples.
Quick guide¶
The LangChain module comes with following Extensions: jsonpatch
, jsonpointer
, LangChain
, langchain-core
, langchain-text-splitters
, langsmith
, orjson
, packaging
, tenacity
and gets loaded by loading LangChain module:
module load LangChain
Simple python example¶
from langchain.text_splitter import CharacterTextSplitter
def main():
# Example text
text = """
LangChain is a framework for developing applications powered by language models.
It enables developers to chain together different components to create more complex applications.
"""
# Initialize a text splitter
text_splitter = CharacterTextSplitter(
separator="\n", # Split by newlines
chunk_size=50, # Maximum size of each chunk
chunk_overlap=10 # Overlap between chunks
)
# Split the text into chunks
chunks = text_splitter.split_text(text)
# Print the resulting chunks
print("Text Chunks:")
for i, chunk in enumerate(chunks):
print(f"Chunk {i + 1}: {chunk}")
if __name__ == "__main__":
main()