- Backend: A FastAPI server with LangChain agents and SQL tools
- Frontend: A React interface to interact with the agent
This guide assumes you have basic knowledge of Python, LangChain, and React. You’ll also need a Thesys API key from the C1 Console.
Part 1: Backend Implementation
The backend uses LangChain to create an intelligent agent that can execute SQL queries on a Chinook database using C1 as the underlying LLM.1
Set up the project structure
Create a new directory for your LangChain project and set up the basic structure:
2
Install dependencies
Create a Install the dependencies:
requirements.txt file with the necessary dependencies:backend/requirements.txt
3
Create the sample database
For this example, we’ll use the Chinook database. Create it in your
db folder using the SQL script:4
Create the backend server
Create the main backend file with LangChain integration:
backend/main.py
5
Set up environment variables
Export your Thesys API key:
6
Test the backend
Run the backend server:You can test the API at
http://localhost:4001/chain/playground to see the interactive documentation.Part 2: Frontend Implementation
Now let’s create a React frontend that integrates with our LangChain backend and uses C1 for the generative UI.1
Set up the frontend project
Create a new React project with Vite:
2
Install dependencies
Install the necessary packages for C1 integration:
3
Create the main App component
Create the main App.tsx file that connects to your LangChain backend:
src/App.tsx
4
Set up the API proxy
Create a Vite configuration to proxy API calls to your backend:
vite.config.ts
5
Run the frontend
With your backend server already running, start the frontend:Visit
http://localhost:5173 to interact with your LangChain + C1 application!Example Queries
Try these example queries to test your application:- “Show me the top 5 best-selling albums”
- “What are the most popular genres in the store?”
- “List all employees and their roles”
- “Show me customers from Canada”
- “What’s the total revenue by country?”
Key Features
- LangChain Integration: Uses LangChain’s agent framework for tool calling
- SQL Tool: Executes dynamic SQL queries on the Chinook database
- C1 Visualization: Automatically visualizes responses with rich UI components
- Context Awareness: Maintains conversation context across interactions
- Error Handling: Robust error handling for both database and API calls
This example demonstrates the power of combining LangChain’s agent capabilities with C1’s generative UI. You can extend this by adding more tools, different databases, or custom UI components.
Next Steps
- Add more tools to your LangChain agent (web search, calculations, etc.)
- Implement user authentication and session management
- Add streaming responses for better UX
- Deploy your application to production
View the code
Find more examples and complete code on our GitHub repository.