Xây Dựng AI Agents với LangChain và VibeCodeCheap
← Quay lại Blog

Xây Dựng AI Agents với LangChain và VibeCodeCheap

3 phút đọc
langchain ai-agents huong-dan python

AI agents đang thay đổi cách chúng ta xây dựng ứng dụng thông minh. Trong hướng dẫn này, chúng ta sẽ xây dựng một AI agent mạnh mẽ sử dụng LangChain và API giá rẻ của VibeCodeCheap.

Chúng Ta Sẽ Xây Gì

Một research assistant agent có thể:

  • Tìm kiếm thông tin trên web
  • Tóm tắt nội dung
  • Trả lời câu hỏi tiếp theo
  • Duy trì context hội thoại

Yêu Cầu

  • Python 3.9+
  • API key VibeCodeCheap
  • Kiến thức Python cơ bản

Bước 1: Cài Đặt Dependencies

pip install langchain langchain-openai python-dotenv

Bước 2: Cấu Hình API

Tạo file .env:

OPENAI_API_KEY=your-vibecodecheap-key
OPENAI_API_BASE=https://api.vibecodecheap.com/v1

Bước 3: Tạo Agent

from langchain_openai import ChatOpenAI
from langchain.agents import AgentExecutor, create_openai_functions_agent
from langchain.tools import tool
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
import os
from dotenv import load_dotenv

load_dotenv()

# Khởi tạo LLM với VibeCodeCheap
llm = ChatOpenAI(
    model="claude-sonnet-4.5",
    openai_api_key=os.getenv("OPENAI_API_KEY"),
    openai_api_base=os.getenv("OPENAI_API_BASE"),
)

@tool
def search_web(query: str) -> str:
    """Tìm kiếm thông tin trên web."""
    # Implementation tìm kiếm của bạn
    return f"Kết quả tìm kiếm cho: {query}"

@tool
def summarize(text: str) -> str:
    """Tóm tắt văn bản được cung cấp."""
    return llm.invoke(f"Tóm tắt nội dung này: {text}").content

# Tạo agent
tools = [search_web, summarize]

prompt = ChatPromptTemplate.from_messages([
    ("system", "Bạn là trợ lý nghiên cứu hữu ích."),
    MessagesPlaceholder(variable_name="chat_history"),
    ("human", "{input}"),
    MessagesPlaceholder(variable_name="agent_scratchpad"),
])

agent = create_openai_functions_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

Bước 4: Chạy Agent

result = agent_executor.invoke({
    "input": "Nghiên cứu xu hướng mới nhất về AI coding assistants",
    "chat_history": []
})

print(result["output"])

Mở Rộng Agent

Thêm Memory cho Hội Thoại Dài

from langchain.memory import ConversationBufferMemory

memory = ConversationBufferMemory(
    memory_key="chat_history",
    return_messages=True
)

Thêm Công Cụ Tùy Chỉnh

@tool
def calculate(expression: str) -> str:
    """Tính toán biểu thức toán học."""
    try:
        return str(eval(expression))
    except Exception as e:
        return f"Lỗi: {str(e)}"

Tại Sao Chọn VibeCodeCheap cho AI Agents?

Xây dựng agents đòi hỏi nhiều API call. Giá của chúng tôi giúp bạn tiết kiệm:

  • Giảm 50% chi phí - Nhiều thử nghiệm hơn, ít lo lắng hơn
  • Tương thích OpenAI - Hoạt động với LangChain ngay lập tức
  • Nhiều models - Thử Claude, GPT-4, Gemini
  • Rate limits cao - Xử lý workload production

Bước Tiếp Theo

  • Thêm nhiều tools cho agent
  • Implement memory cho hội thoại dài
  • Deploy như web service
  • Tích hợp với ứng dụng hiện có

Bắt đầu xây dựng AI agents ngay hôm nay

API cho AI Agents của bạn

Build AI agents với Claude Code, LangChain, CrewAI. Một API key cho Claude, Gemini models.

API cho AI Agents của bạn

Bài viết liên quan

Build AI Agents của bạn ngay

API proxy cho Claude Code, LangChain, CrewAI & hơn thế. Một API key cho Claude, Gemini models.