Objective
By the end of this workshop, participants will have created, tested, and deployed an open-source AI agent using LangChain. This workshop includes hands-on activities, from setup to deployment, designed for beginners and intermediate developers.
Agenda
1. Introduction to LangChain (15 minutes)
- Overview of LangChain and its capabilities.
- Use cases of AI agents.
- Workshop goals.
2. Environment Setup (15 minutes)
- Install Python and necessary tools.
- Install LangChain and dependencies.
- Verify setup.
3. Creating an AI Agent with LangChain (45 minutes)
- Build an agent capable of answering questions using an LLM and a document database.
4. Testing the AI Agent (30 minutes)
- Test the agent locally with sample inputs.
- Debugging and improving the agent’s performance.
5. Deploying the AI Agent (45 minutes)
- Deploy as an API using FastAPI.
- Test the API with a REST client (e.g., Postman).
6. Conclusion and Q&A (15 minutes)
- Review key takeaways.
- Discuss potential use cases and enhancements.
- Open floor for questions.
Detailed Steps
1. Environment Setup
Requirements:
- Python 3.8 or later
- VS Code or any text editor
- Postman or cURL for API testing
Commands:
# Install LangChain and dependencies
pip install langchain openai fastapi uvicorn
# Optional: Install libraries for document retrieval
pip install faiss-cpu unstructured
2. Create an AI Agent
Code Snippet: Basic LangChain Agent
from langchain.agents import initialize_agent, Tool
from langchain.chat_models import ChatOpenAI
from langchain.prompts import PromptTemplate
# Define tools
def search_tool(query: str) -> str:
# Example tool: simple search function
return f"Simulated search result for: {query}"
tools = [
Tool(
name="Search",
func=search_tool,
description="Use this tool to search for information."
)
]
# Initialize LLM and agent
llm = ChatOpenAI(temperature=0, model="gpt-4")
prompt = PromptTemplate(template="Answer based on the tools: {query}", input_variables=["query"])
agent = initialize_agent(tools, llm, agent_type="zero-shot-react-description")
# Test agent
response = agent.run("Who is the president of the Philippines?")
print(response)
What This Does:
- Creates a zero-shot agent.
- Adds a custom search tool.
- Interacts with the agent using a natural language query.
3. Test the AI Agent
- Use diverse prompts to test the agent’s responses.
- Add more tools as needed, such as document retrieval or APIs.
4. Deploy the AI Agent
Code Snippet: API Deployment with FastAPI
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Query(BaseModel):
query: str
@app.post("/ask")
async def ask_agent(query: Query):
response = agent.run(query.query)
return {"response": response}
# Run with Uvicorn
# uvicorn filename:app --reload
Steps:
- Save the file as
app.py
. - Run the server:
uvicorn app:app --reload
- Test with Postman or cURL:
curl -X POST "http://127.0.0.1:8000/ask" -H "Content-Type: application/json" -d '{"query": "What is LangChain?"}'
5. Enhance the Agent
- Add a document database for knowledge retrieval (e.g., FAISS or Chroma).
- Integrate additional APIs for multi-functionality.
Materials
- LangChain Documentation
- Preloaded datasets for testing.
- Reference code snippets and slides (provided during the workshop).
Expected Outcome
Participants will:
- Understand LangChain’s structure.
- Build a working AI agent.
- Deploy the agent as a REST API.
- Gain insight into practical applications and potential enhancements.
Would you like additional resources, slides, or marketing materials for this workshop?