Convert Figma logo to code with AI

langchain-ai logolangchain

🦜🔗 Build context-aware reasoning applications

98,640
16,035
98,640
436

Top Related Projects

98,623

🦜🔗 Build context-aware reasoning applications

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

19,922

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 the leading framework for building LLM-powered agents over your data.

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

98,623

🦜🔗 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.

19,922

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 the leading framework for building LLM-powered agents over your data.

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 Logo

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

[!NOTE] Looking for the JS/TS library? Check out LangChain.js.

LangChain is a framework for building LLM-powered applications. It helps you chain together interoperable components and third-party integrations to simplify AI application development — all while future-proofing decisions as the underlying technology evolves.

pip install -U langchain

To learn more about LangChain, check out the docs. If you’re looking for more advanced customization or agent orchestration, check out LangGraph, our framework for building controllable agent workflows.

Why use LangChain?

LangChain helps developers build applications powered by LLMs through a standard interface for models, embeddings, vector stores, and more.

Use LangChain for:

  • Real-time data augmentation. Easily connect LLMs to diverse data sources and external / internal systems, drawing from LangChain’s vast library of integrations with model providers, tools, vector stores, retrievers, and more.
  • Model interoperability. Swap models in and out as your engineering team experiments to find the best choice for your application’s needs. As the industry frontier evolves, adapt quickly — LangChain’s abstractions keep you moving without losing momentum.

LangChain’s ecosystem

While the LangChain framework can be used standalone, it also integrates seamlessly with any LangChain product, giving developers a full suite of tools when building LLM applications.

To improve your LLM application development, pair LangChain with:

  • LangSmith - Helpful for agent evals and observability. Debug poor-performing LLM app runs, evaluate agent trajectories, gain visibility in production, and improve performance over time.
  • LangGraph - Build agents that can reliably handle complex tasks with LangGraph, our low-level agent orchestration framework. LangGraph offers customizable architecture, long-term memory, and human-in-the-loop workflows — and is trusted in production by companies like LinkedIn, Uber, Klarna, and GitLab.
  • LangGraph Platform - Deploy and scale agents effortlessly with a purpose-built deployment platform for long running, stateful workflows. Discover, reuse, configure, and share agents across teams — and iterate quickly with visual prototyping in LangGraph Studio.

Additional resources

  • Tutorials: Simple walkthroughs with guided examples on getting started with LangChain.
  • How-to Guides: Quick, actionable code snippets for topics such as tool calling, RAG use cases, and more.
  • Conceptual Guides: Explanations of key concepts behind the LangChain framework.
  • API Reference: Detailed reference on navigating base packages and integrations for LangChain.

NPM DownloadsLast 30 Days