Build powerful LLM-based Dart/Flutter applications.
LangChain.dart is a Dart port of the popular LangChain Python framework created by Harrison Chase.
LangChain provides a set of ready-to-use components for working with language models and the concept of chains, which allows to "chain" components together to formulate more advanced use cases around LLMs.
The components can be grouped into a few core modules:
- π Model I/O: streamlines the interaction between the model inputs (prompt templates), the Language Model (abstracting different providers under a unified API), and the model output (output parsers).
- π Retrieval: assists in loading user data (document loaders), transforming (document transformers and embedding models), storing (vector stores), and retrieving it when needed (retrievers).
- π€ Agents: "bots" that harness LLMs to perform tasks. They serve as the link between LLM and the tools (web search, calculators, database lookup, etc.). They determine what has to be accomplished and the tools that are more suitable for the specific task.
The different components can be composed together using the LangChain Expression Language (LCEL).
Large Language Models (LLMs) have revolutionized Natural Language Processing (NLP), serving as essential components in a wide range of applications, such as question-answering, summarization, translation, and text generation.
The adoption of LLMs is creating a new tech stack in its wake. However, emerging libraries and tools are predominantly being developed for the Python and JavaScript ecosystems. As a result, the number of applications leveraging LLMs in these ecosystems has grown exponentially.
In contrast, the Dart / Flutter ecosystem has not experienced similar growth, which can likely be attributed to the scarcity of Dart and Flutter libraries that streamline the complexities associated with working with LLMs.
LangChain.dart aims to fill this gap by abstracting the intricacies of working with LLMs in Dart and Flutter, enabling developers to harness their combined potential effectively.
LangChain.dart has a modular design where the core langchain package provides the LangChain API and each 3rd party integration with a model provider, database, etc. is provided by a separate package.
Package | Version | Description |
---|---|---|
langchain | Core LangChain API (base components abstraction, logic for chaining them (LCEL), etc.) | |
langchain_openai | OpenAI integration (GPT-3.5, GPT-4, Embeddings, Functions, Vision, DALLΒ·E 3, etc.) and OpenAI Compatible services (Together AI, Anyscale, OpenRouter, etc.) | |
langchain_google | Google integration (GoogleAI, VertexAI, Gemini, PaLM 2, Embeddings, Vector Search, etc.) | |
langchain_ollama | Ollama integration (Llama 2, Code Llama, Mistral, LLaVA, Phi, Vicuna, Orca, Starling, etc.) | |
langchain_mistralai | Mistral AI integration (Mistral-7B, Mixtral 8x7B, embeddings, etc.). | |
langchain_pinecone | Pinecone vector database integration | |
langchain_chroma | Chroma vector database integration | |
langchain_supabase | Supabase Vector database integration |
Functionality provided by each package:
Package | LLMs | Chat models | Embeddings | Vector stores | Chains | Agents | Tools |
---|---|---|---|---|---|---|---|
langchain | β | β | β | β | β | β | β |
langchain_openai | β | β | β | β | β | β | |
langchain_google | β | β | β | β | |||
langchain_ollama | β | β | β | ||||
langchain_mistralai | β | β | |||||
langchain_pinecone | β | ||||||
langchain_chroma | β | ||||||
langchain_supabase | β |
The following packages are maintained (and used internally) by LangChain.dart, although they can also be used independently:
Package | Version | Description |
---|---|---|
chromadb | Chroma DB API client | |
googleai_dart | Google AI for Developers (Gemini API) client | |
mistralai_dart | Mistral AI API client | |
ollama_dart | Ollama API client | |
openai_dart | OpenAI API client | |
vertex_ai | GCP Vertex AI API client |
To start using LangChain.dart, add langchain
as a dependency to your pubspec.yaml
file. Also, include the dependencies for the specific integrations you want to use (e.g.langchain_openai
or langchain_google
):
dependencies:
langchain: {version}
langchain_openai: {version}
langchain_google: {version}
...
The most basic building block of LangChain.dart is calling an LLM on some prompt. LangChain.dart provides a unified interface for calling different LLMs. For example, we can use ChatGoogleGenerativeAI
to call Google's Gemini model:
final model = ChatGoogleGenerativeAI(apiKey: googleApiKey);
final prompt = PromptValue.string('Hello world!');
final result = await model.invoke(prompt);
// Hello everyone! I'm new here and excited to be part of this community.
But the power of LangChain.dart comes from chaining together multiple components to implement complex use cases. For example, a RAG (Retrieval-Augmented Generation) pipeline that would accept a user query, retrieve relevant documents from a vector store, format them using prompt templates, invoke the model, and parse the output:
// 1. Create a vector store and add documents to it
final vectorStore = MemoryVectorStore(
embeddings: OpenAIEmbeddings(apiKey: openaiApiKey),
);
await vectorStore.addDocuments(
documents: [
Document(pageContent: 'LangChain was created by Harrison'),
Document(pageContent: 'David ported LangChain to Dart in LangChain.dart'),
],
);
// 2. Construct a RAG prompt template
final promptTemplate = ChatPromptTemplate.fromTemplates([
(ChatMessageType.system, 'Answer the question based on only the following context:\n{context}'),
(ChatMessageType.human, '{question}'),
]);
// 3. Create a Runnable that combines the retrieved documents into a single string
final docCombiner = Runnable.fromFunction<List<Document>, String>((docs, _) {
return docs.map((d) => d.pageContent).join('\n');
});
// 4. Define the RAG pipeline
final chain = Runnable.fromMap<String>({
'context': vectorStore.asRetriever().pipe(docCombiner),
'question': Runnable.passthrough(),
})
.pipe(promptTemplate)
.pipe(ChatOpenAI(apiKey: openaiApiKey))
.pipe(StringOutputParser());
// 5. Run the pipeline
final res = await chain.invoke('Who created LangChain.dart?');
print(res);
// David created LangChain.dart
Stay up-to-date on the latest news and updates on the field, have great discussions, and get help in the official LangChain.dart Discord server.
π’ Call for Collaborators π’ |
---|
We are looking for collaborators to join the core group of maintainers. |
New contributors welcome! Check out our Contributors Guide for help getting started.
Join us on Discord to meet other maintainers. We'll help you get your first contribution in no time!
- LangChain: The original Python LangChain project.
- LangChain.js: A JavaScript port of LangChain.
- LangChain.go: A Go port of LangChain.
- LangChain.rb: A Ruby port of LangChain.
LangChain.dart is licensed under the MIT License.