An open API service indexing awesome lists of open source software.

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

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.