Convert Figma logo to code with AI

langchain-ai logolangchain

🦜🔗 Build context-aware reasoning applications

92,073
14,665
92,073
802

Top Related Projects

92,071

🦜🔗 Build context-aware reasoning applications

Integrate cutting-edge LLM technology quickly and easily into your apps

16,603

:mag: AI orchestration framework to build customizable, production-ready LLM applications. Connect components (models, vector DBs, file converters) to pipelines or agents that can interact with your data. With advanced retrieval methods, it's best suited for building RAG, question answering, semantic search or conversational agent chatbots.

LlamaIndex is a data framework for your LLM applications

Examples and guides for using the OpenAI API

Build high-quality LLM apps - from prototyping, testing to production deployment and monitoring.

Quick Overview

LangChain is an open-source framework for developing applications powered by language models. It provides a set of tools and abstractions that simplify the process of building complex, context-aware AI applications. LangChain enables developers to create chatbots, question-answering systems, and other AI-driven applications with ease.

Pros

  • Modular and flexible architecture, allowing for easy customization and integration
  • Extensive documentation and active community support
  • Supports multiple language models and integrations with various APIs and tools
  • Provides high-level abstractions for common AI application patterns

Cons

  • Steep learning curve for beginners due to the wide range of features and concepts
  • Rapid development pace may lead to frequent breaking changes
  • Some advanced features may require additional setup or external services
  • Performance overhead for some abstractions compared to direct API calls

Code Examples

  1. Creating a simple chain for question-answering:
from langchain import PromptTemplate, LLMChain
from langchain.llms import OpenAI

llm = OpenAI(temperature=0.9)
prompt = PromptTemplate(
    input_variables=["question"],
    template="Q: {question}\nA:",
)
chain = LLMChain(llm=llm, prompt=prompt)

response = chain.run("What is the capital of France?")
print(response)
  1. Using a vector store for similarity search:
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import FAISS
from langchain.text_splitter import CharacterTextSplitter

with open("document.txt") as f:
    raw_text = f.read()

text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
texts = text_splitter.split_text(raw_text)

embeddings = OpenAIEmbeddings()
docsearch = FAISS.from_texts(texts, embeddings)

query = "What is the main topic of this document?"
docs = docsearch.similarity_search(query)
print(docs[0].page_content)
  1. Creating an agent with tools:
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.agents import AgentType
from langchain.llms import OpenAI

llm = OpenAI(temperature=0)
tools = load_tools(["serpapi", "llm-math"], llm=llm)
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)

agent.run("What was the high temperature in SF yesterday in Fahrenheit? What is that number raised to the .023 power?")

Getting Started

To get started with LangChain, follow these steps:

  1. Install LangChain using pip:

    pip install langchain
    
  2. Set up your API keys as environment variables:

    export OPENAI_API_KEY="your-api-key-here"
    
  3. Create a simple chain:

    from langchain import PromptTemplate, LLMChain
    from langchain.llms import OpenAI
    
    llm = OpenAI(temperature=0.7)
    prompt = PromptTemplate(
        input_variables=["product"],
        template="What is a good name for a company that makes {product}?",
    )
    chain = LLMChain(llm=llm, prompt=prompt)
    
    print(chain.run("eco-friendly water bottles"))
    

This example sets up a basic LangChain application that generates company names based on a given product.

Competitor Comparisons

92,071

🦜🔗 Build context-aware reasoning applications

Pros of langchain

  • More comprehensive and feature-rich, offering a wider range of tools and integrations
  • Larger and more active community, resulting in frequent updates and improvements
  • Extensive documentation and examples, making it easier for developers to get started

Cons of langchain

  • Can be overwhelming for beginners due to its extensive feature set
  • Potentially higher learning curve compared to the simplified version
  • May include unnecessary components for simpler projects, leading to increased complexity

Code Comparison

langchain:

from langchain import OpenAI, LLMChain, PromptTemplate

llm = OpenAI(temperature=0.9)
prompt = PromptTemplate(input_variables=["product"], template="What is a good name for a company that makes {product}?")
chain = LLMChain(llm=llm, prompt=prompt)

langchain>:

from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate

llm = OpenAI(temperature=0.9)
prompt = PromptTemplate.from_template("What is a good name for a company that makes {product}?")

The code comparison shows that langchain> offers a more streamlined and simplified approach, with fewer imports and a more concise syntax. However, langchain provides more flexibility and options for advanced users who require additional functionality.

Integrate cutting-edge LLM technology quickly and easily into your apps

Pros of Semantic Kernel

  • Tighter integration with Azure AI services and Microsoft ecosystem
  • Built-in memory and planner components for more advanced AI orchestration
  • Strong focus on enterprise-grade security and compliance features

Cons of Semantic Kernel

  • Smaller community and ecosystem compared to LangChain
  • Less extensive documentation and tutorials for beginners
  • More limited support for non-Microsoft AI services and models

Code Comparison

LangChain example:

from langchain import OpenAI, LLMChain, PromptTemplate

llm = OpenAI(temperature=0.9)
prompt = PromptTemplate(input_variables=["product"], template="What is a good name for a company that makes {product}?")
chain = LLMChain(llm=llm, prompt=prompt)

print(chain.run("colorful socks"))

Semantic Kernel example:

using Microsoft.SemanticKernel;

var kernel = Kernel.Builder.Build();
kernel.Config.AddOpenAITextCompletionService("davinci", "YOUR_API_KEY");

var prompt = "What is a good name for a company that makes {{$input}}?";
var function = kernel.CreateSemanticFunction(prompt);

var result = await kernel.RunAsync("colorful socks", function);
Console.WriteLine(result);

Both frameworks provide similar functionality for creating and running AI-powered functions, but with different syntax and integration approaches.

16,603

:mag: AI orchestration framework to build customizable, production-ready LLM applications. Connect components (models, vector DBs, file converters) to pipelines or agents that can interact with your data. With advanced retrieval methods, it's best suited for building RAG, question answering, semantic search or conversational agent chatbots.

Pros of Haystack

  • More focused on question answering and document retrieval tasks
  • Offers pre-built pipelines for common NLP workflows
  • Includes built-in document stores and integrations with popular databases

Cons of Haystack

  • Less flexible for general-purpose language AI tasks
  • Smaller community and ecosystem compared to LangChain
  • More limited in terms of supported language models and integrations

Code Comparison

Haystack example:

from haystack import Pipeline
from haystack.nodes import TfidfRetriever, FARMReader

pipeline = Pipeline()
pipeline.add_node(component=TfidfRetriever(document_store), name="Retriever", inputs=["Query"])
pipeline.add_node(component=FARMReader(model_name_or_path="deepset/roberta-base-squad2"), name="Reader", inputs=["Retriever"])

LangChain example:

from langchain import OpenAI, ConversationChain

llm = OpenAI(temperature=0)
conversation = ConversationChain(llm=llm, verbose=True)
conversation.predict(input="Hi there!")

Both libraries offer powerful tools for working with language models and building NLP applications. Haystack excels in document retrieval and question answering tasks, while LangChain provides a more flexible framework for a wider range of language AI applications. The choice between them depends on the specific use case and requirements of your project.

LlamaIndex is a data framework for your LLM applications

Pros of LlamaIndex

  • More focused on data indexing and retrieval, making it potentially more efficient for specific use cases
  • Simpler API and easier learning curve for beginners
  • Better suited for document-based question answering tasks

Cons of LlamaIndex

  • Less versatile compared to LangChain's broader range of applications
  • Smaller community and ecosystem, potentially leading to fewer resources and integrations
  • More limited in terms of advanced features and customization options

Code Comparison

LlamaIndex:

from llama_index import GPTSimpleVectorIndex, Document
documents = [Document('content1'), Document('content2')]
index = GPTSimpleVectorIndex.from_documents(documents)
response = index.query("What is the content about?")

LangChain:

from langchain import VectorDBQA, OpenAI
from langchain.vectorstores import FAISS
documents = ["content1", "content2"]
vectorstore = FAISS.from_texts(documents, OpenAI())
qa = VectorDBQA.from_chain_type(llm=OpenAI(), chain_type="stuff", vectorstore=vectorstore)
response = qa.run("What is the content about?")

Both libraries offer similar functionality for indexing and querying documents, but LangChain provides a more modular approach with separate components for vector stores, language models, and chain types.

Examples and guides for using the OpenAI API

Pros of OpenAI Cookbook

  • Focused specifically on OpenAI's API, providing in-depth examples and best practices
  • Regularly updated with new features and capabilities of OpenAI's models
  • Includes a wide range of practical use cases and applications

Cons of OpenAI Cookbook

  • Limited to OpenAI's ecosystem, not as versatile for other AI providers
  • Less abstraction and higher-level tools compared to LangChain
  • Requires more manual implementation of complex workflows

Code Comparison

OpenAI Cookbook:

import openai

response = openai.Completion.create(
  engine="text-davinci-002",
  prompt="Translate the following English text to French: '{}'",
  max_tokens=60
)

LangChain:

from langchain.llms import OpenAI
from langchain.chains import TranslationChain

llm = OpenAI(model_name="text-davinci-002")
chain = TranslationChain.from_llm(llm, source_language="English", target_language="French")
result = chain.run("Hello, how are you?")

The OpenAI Cookbook provides direct API usage, while LangChain offers higher-level abstractions for complex tasks like translation chains.

Build high-quality LLM apps - from prototyping, testing to production deployment and monitoring.

Pros of PromptFlow

  • Integrated with Azure AI services, offering seamless deployment and scaling
  • Visual flow designer for easier prompt engineering and workflow creation
  • Built-in evaluation tools for assessing and improving prompt performance

Cons of PromptFlow

  • Less extensive community and ecosystem compared to LangChain
  • More focused on Azure ecosystem, potentially limiting flexibility
  • Newer project with fewer established patterns and best practices

Code Comparison

LangChain example:

from langchain import PromptTemplate, LLMChain
from langchain.llms import OpenAI

template = "What is a good name for a company that makes {product}?"
prompt = PromptTemplate(template=template, input_variables=["product"])
llm_chain = LLMChain(prompt=prompt, llm=OpenAI(temperature=0.9))

PromptFlow example:

from promptflow import tool

@tool
def name_generator(product: str):
    return f"What is a good name for a company that makes {product}?"

# Use in a flow
flow.add_node(name_generator, inputs={"product": "${inputs.product}"})

Both frameworks aim to simplify working with language models, but PromptFlow focuses more on visual design and Azure integration, while LangChain offers a broader set of tools and integrations for various LLM tasks.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

🦜️🔗 LangChain

⚡ Build context-aware reasoning applications ⚡

Release Notes CI PyPI - License PyPI - Downloads GitHub star chart Open Issues Open in Dev Containers Open in GitHub Codespaces Twitter

Looking for the JS/TS library? Check out LangChain.js.

To help you ship LangChain apps to production faster, check out LangSmith. LangSmith is a unified developer platform for building, testing, and monitoring LLM applications. Fill out this form to speak with our sales team.

Quick Install

With pip:

pip install langchain

With conda:

conda install langchain -c conda-forge

🤔 What is LangChain?

LangChain is a framework for developing applications powered by large language models (LLMs).

For these applications, LangChain simplifies the entire application lifecycle:

  • Open-source libraries: Build your applications using LangChain's open-source building blocks, components, and third-party integrations. Use LangGraph to build stateful agents with first-class streaming and human-in-the-loop support.
  • Productionization: Inspect, monitor, and evaluate your apps with LangSmith so that you can constantly optimize and deploy with confidence.
  • Deployment: Turn your LangGraph applications into production-ready APIs and Assistants with LangGraph Cloud.

Open-source libraries

  • langchain-core: Base abstractions and LangChain Expression Language.
  • langchain-community: Third party integrations.
    • Some integrations have been further split into partner packages that only rely on langchain-core. Examples include langchain_openai and langchain_anthropic.
  • langchain: Chains, agents, and retrieval strategies that make up an application's cognitive architecture.
  • LangGraph: A library for building robust and stateful multi-actor applications with LLMs by modeling steps as edges and nodes in a graph. Integrates smoothly with LangChain, but can be used without it.

Productionization:

  • LangSmith: A developer platform that lets you debug, test, evaluate, and monitor chains built on any LLM framework and seamlessly integrates with LangChain.

Deployment:

  • LangGraph Cloud: Turn your LangGraph applications into production-ready APIs and Assistants.

Diagram outlining the hierarchical organization of the LangChain framework, displaying the interconnected parts across multiple layers.

🧱 What can you build with LangChain?

❓ Question answering with RAG

🧱 Extracting structured output

🤖 Chatbots

And much more! Head to the Tutorials section of the docs for more.

🚀 How does LangChain help?

The main value props of the LangChain libraries are:

  1. Components: composable building blocks, tools and integrations for working with language models. Components are modular and easy-to-use, whether you are using the rest of the LangChain framework or not
  2. Off-the-shelf chains: built-in assemblages of components for accomplishing higher-level tasks

Off-the-shelf chains make it easy to get started. Components make it easy to customize existing chains and build new ones.

LangChain Expression Language (LCEL)

LCEL is a key part of LangChain, allowing you to build and organize chains of processes in a straightforward, declarative manner. It was designed to support taking prototypes directly into production without needing to alter any code. This means you can use LCEL to set up everything from basic "prompt + LLM" setups to intricate, multi-step workflows.

  • Overview: LCEL and its benefits
  • Interface: The standard Runnable interface for LCEL objects
  • Primitives: More on the primitives LCEL includes
  • Cheatsheet: Quick overview of the most common usage patterns

Components

Components fall into the following modules:

📃 Model I/O

This includes prompt management, prompt optimization, a generic interface for chat models and LLMs, and common utilities for working with model outputs.

📚 Retrieval

Retrieval Augmented Generation involves loading data from a variety of sources, preparing it, then searching over (a.k.a. retrieving from) it for use in the generation step.

🤖 Agents

Agents allow an LLM autonomy over how a task is accomplished. Agents make decisions about which Actions to take, then take that Action, observe the result, and repeat until the task is complete. LangChain provides a standard interface for agents, along with LangGraph for building custom agents.

📖 Documentation

Please see here for full documentation, which includes:

  • Introduction: Overview of the framework and the structure of the docs.
  • Tutorials: If you're looking to build something specific or are more of a hands-on learner, check out our tutorials. This is the best place to get started.
  • How-to guides: Answers to “How do I….?” type questions. These guides are goal-oriented and concrete; they're meant to help you complete a specific task.
  • Conceptual guide: Conceptual explanations of the key parts of the framework.
  • API Reference: Thorough documentation of every class and method.

🌐 Ecosystem

💁 Contributing

As an open-source project in a rapidly developing field, we are extremely open to contributions, whether it be in the form of a new feature, improved infrastructure, or better documentation.

For detailed information on how to contribute, see here.

🌟 Contributors

langchain contributors

NPM DownloadsLast 30 Days