Getting Started with LangChain: Building Your First AI Application
·2 min read·40 views
Getting Started with LangChain: Build Your First AI Application
LangChain is currently the most popular AI application development framework. This article will guide you from scratch to build a simple RAG Q&A system.
What is LangChain?
LangChain is a framework for building AI applications. It provides:
- Chaining: Connect multiple AI steps in a sequence
- RAG Support: Connect to external data sources
- Agent Capabilities: Let AI make autonomous decisions
- Tool Integration: Call external APIs and tools
Installation
pip install langchain langchain-openai chromadb
Building a RAG Q&A System
1. Prepare Documents
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.vectorstores import Chroma
from langchain_openai import OpenAIEmbeddings
# Prepare your documents
docs = [
"Ciyuano Ciyuano is a OpenAI compatible API API relay service...",
"supports DeepSeek、GLM、Qwen and other domestic LLMs...",
"Usage:Modify base_url and api_key that's it...",
]
# Split documents
splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
chunks = splitter.create_documents(docs)
# Create vector database
vectorstore = Chroma.from_documents(chunks, OpenAIEmbeddings(
base_url="https://www.ciyuano.com/v1",
api_key="sk-relay-Your API key"
))
2. Build the Q&A Chain
from langchain.chains import RetrievalQA
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
model="deepseek-v4-flash",
base_url="https://www.ciyuano.com/v1",
api_key="sk-relay-Your API key"
)
qa_chain = RetrievalQA.from_chain_type(
llm=llm,
retriever=vectorstore.as_retriever(),
)
# Query
result = qa_chain.invoke({"query": "Which models does Ciyuano support?"})
print(result["result"])
Core Concepts
Prompt Template
Prompt template, used to build structured prompts.
Chain
Connect multiple steps to form a complete processing flow.
Retriever
Retrieve relevant documents from the vector database.
Agent
Let the AI autonomously decide which tools to use to complete tasks.
Next Steps
- Try connecting to different models
- Build more complex Agents
- Integrate external tools (search, database, etc.)
- Deploy as a Web service
Resources
Tags
← Previous
Using OpenAI SDK to seamlessly switch to domestic large models
Next →
The 10 most noteworthy AI open-source projects on GitHub
💬 Comments are not yet available, stay tuned