https://github.com/persteenolsen/fastapi-jwt-auth-ai-agent-one
Python FastAPI with JWT Auth serving a LangChain ReAct AI Agent running at Vercel
https://github.com/persteenolsen/fastapi-jwt-auth-ai-agent-one
ai ai-agent fastapi jwt langchain python
Last synced: 12 days ago
JSON representation
Python FastAPI with JWT Auth serving a LangChain ReAct AI Agent running at Vercel
- Host: GitHub
- URL: https://github.com/persteenolsen/fastapi-jwt-auth-ai-agent-one
- Owner: persteenolsen
- Created: 2026-06-07T06:26:34.000Z (22 days ago)
- Default Branch: main
- Last Pushed: 2026-06-08T16:26:02.000Z (21 days ago)
- Last Synced: 2026-06-08T17:30:04.220Z (21 days ago)
- Topics: ai, ai-agent, fastapi, jwt, langchain, python
- Language: Python
- Homepage: https://fastapi-jwt-auth-ai-agent-one.vercel.app/docs
- Size: 22.5 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ๐ FastAPI with JWT Auth serving a LangChain ReAct AI Agent
A production-style AI Agent API built with **FastAPI**, featuring **JWT authentication**, **Groq-powered LLMs**, and a **LangChain ReAct agent** with tool usage (Wikipedia).
This project demonstrates how to build a tool-using AI system that can:
- ๐ง Reason about user intent
- ๐ Use external tools when needed
- ๐ฌ Respond directly for conversational and creative tasks
---
## ๐ Project Info
- ๐ฆ Version: 0.0.2
- ๐ Python: 3.12
- ๐
Last Updated: 10-06-2026
---
## โจ Features
### ๐ Authentication (JWT)
- Secure login system using JWT (HS256)
- Protected endpoints with Bearer token
- Token expiration support
- Environment-based credentials (.env)
---
### ๐ค AI Agent (LangChain ReAct)
- ๐ง Reasoning + Acting loop (ReAct pattern)
- ๐ Multi-step decision making
- ๐ฏ Smart tool selection (Wikipedia only when needed)
- ๐ Controlled iterations (prevents infinite loops)
- ๐งฏ Parsing error handling for stability
---
### ๐ง LLM Integration (Groq)
- โก Model: `llama-3.3-70b-versatile`
- ๐ Ultra-fast inference via Groq API
- ๐๏ธ Deterministic outputs (`temperature = 0`)
---
### ๐ Wikipedia Tool
- ๐ Wikipedia API integration via custom wrapper
- ๐ Factual knowledge retrieval
- ๐ Retry support for transient API failures
- ๐งฏ Graceful fallback on errors
- ๐ง Used only for factual queries
---
### ๐งฉ Agent Capabilities
- โ General question answering
- ๐ Factual lookup (Wikipedia)
- ๐ฌ Conversational chat
- ๐ Joke generation
- โ๏ธ Creative writing (poems, stories)
- ๐ง Tool-augmented reasoning
---
## ๐ก API Endpoints
| Method | Endpoint | Description |
|--------|----------|-------------|
| ๐ POST | `/login` | Get JWT token |
| ๐ฌ POST | `/chat` | Chat with AI agent (protected) |
| โค๏ธ GET | `/health` | Service health check |
| ๐ง GET | `/test-groq` | Test LLM connection |
| ๐ GET | `/test-wikipedia` | Test Wikipedia tool |
---
## โ๏ธ Getting Started
### 1๏ธโฃ Clone Repository
Clone the project from GitHub and enter the project directory.
---
### 2๏ธโฃ Create Virtual Environment
Windows:
venv\Scripts\activate
Linux / macOS:
source venv/bin/activate
---
### 3๏ธโฃ Install Dependencies
pip install -r requirements.txt
---
## ๐ Environment Variables
Create a `.env` file:
SECRET_KEY=your_secret_key_here
GROQ_API_KEY=your_groq_api_key
FAKE_USERNAME=admin
FAKE_PASSWORD=password
Generate a secure key:
python -c "import secrets; print(secrets.token_hex(32))"
---
## โถ๏ธ Run the Application
uvicorn main:app --reload
App:
http://127.0.0.1:8000
Docs:
http://127.0.0.1:8000/docs
---
## ๐ Authentication Flow
1. Call `/login`
2. Receive JWT token
3. Use token in requests:
Authorization: Bearer YOUR_TOKEN
---
## ๐ง How the Agent Works
User Input
โ
Agent Reasoning
โ
Wikipedia Tool (if needed)
โ
Direct Response (jokes, chat, creative writing)
โ
Final Answer
---
## ๐๏ธ Architecture
LLM:
Groq - llama-3.3-70b-versatile
Agent:
LangChain ReAct Agent
Tools:
Wikipedia API
Execution Settings:
Max iterations: 2
Parsing error handling enabled
Verbose logging enabled
---
## ๐ฌ Example Requests
### ๐ Joke Request
{
"message": "Tell me a joke"
}
Response:
{
"response": "Here's one: Why couldn't the bicycle stand up by itself? Because it was two-tired."
}
---
### ๐ Factual Question
{
"message": "What is the capital of France?"
}
Response:
{
"response": "Paris"
}
---
### โ๏ธ Creative Request
{
"message": "Write a short poem"
}
Response:
{
"response": "The sun sets slow and paints the sky..."
}
---
### ๐ Wikipedia Tool Usage
{
"message": "Who is Albert Einstein?"
}
Response:
{
"response": "Albert Einstein was a theoretical physicist known for the theory of relativity."
}
---
## ๐ Use Cases
- ๐ค AI assistants
- ๐ Educational tools
- ๐งช LangChain experimentation
- ๐ง Tool-augmented LLM systems
- โก FastAPI backend AI services
---
## ๐ง Limitations
- ๐ Only Wikipedia as external tool
- ๐ง No long-term memory
- ๐ Demo authentication system
- ๐ Limited external knowledge sources
---
## ๐ Future Improvements
- ๐ง Conversation memory
- ๐ More tools (search, APIs, DB integration)
- ๐ฅ Multi-user system
- ๐ Token refresh system
- ๐งฉ Modular tool registry
- ๐ Analytics dashboard
---
## ๐ License
MIT License
---
## ๐ Summary
This project combines:
โก FastAPI
๐ JWT Authentication
๐ง Groq LLM (Llama 3.3 70B)
๐ค LangChain ReAct Agent
๐ Wikipedia Tool
to build a **working tool-augmented AI system** that can reason, act, and respond intelligently depending on user intent.