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

https://github.com/tdiprima/langchain-flask-api

A modular, multi-user chatbot API with memory, personas, and OpenAI integration.
https://github.com/tdiprima/langchain-flask-api

Last synced: 3 months ago
JSON representation

A modular, multi-user chatbot API with memory, personas, and OpenAI integration.

Awesome Lists containing this project

README

          

![OpenAI](https://img.shields.io/badge/OpenAI-API--Powered-blueviolet?logo=openai&logoColor=white)
![Python](https://img.shields.io/badge/Python-3.9%2B-blue?logo=python&logoColor=white)
![Flask](https://img.shields.io/badge/Flask-API-green?logo=flask)
![LangChain](https://img.shields.io/badge/LangChain-Integrated-orange?logo=brain)

# 🧠 LangChain Chatbot API – Multi-User Edition

A modular, Flask-based chatbot API powered by LangChain and OpenAI. Now with multi-user support, persistent chat history, persona control, and built-in authentication. Lightweight, flexible, and ready to roll for experimentation or production.

---

## πŸš€ Features

- πŸ§β€β™‚οΈ **Multi-user support** – Users can authenticate and maintain their own chat sessions
- πŸ’¬ **Persistent conversation memory** – Chats survive app restarts via JSON/db-backed persistence
- 🧠 **LangChain integration** – For advanced prompting and context-aware conversations
- 🎭 **Personas** – Modify chatbot tone/behavior using preset styles
- πŸ” **Authentication** – Token-based auth via `/login` and `/register` endpoints
- πŸ”„ **Modular structure** – Each component is standalone/testable
- πŸ§ͺ **Shell test scripts** – Each major function has a matching `.sh` tester

---

## πŸ“ Project Structure

| File | Description |
|-----------------------------|-------------------------------------------------|
| `app.py` / `app_multiuser.py` | Flask app entry points |
| `chatbot_api.py` | Main API logic and routing |
| `authentication.py` | Handles login, registration, and auth checks |
| `user_authentication.py` | User credential storage + token management |
| `chat_history.py` | In-memory and persistent chat history manager |
| `persistence.py` | Low-level storage logic (files/db/etc) |
| `conversation_persistence.py` | Combines memory + file-backed storage |
| `prompt_engineering.py` | Prompt construction + persona injection |
| `prompt_engineering1.py` | (Optional) Extended prompt engineering module |

> βš™οΈ Each file likely has a companion shell script for curl-based testing.

---

## πŸ” Authentication Flow

```bash
# Register a new user
curl -X POST http://localhost:3000/register \
-H "Content-Type: application/json" \
-d '{"username":"alice", "password":"secret"}'

# Login and receive auth token
curl -X POST http://localhost:3000/login \
-H "Content-Type: application/json" \
-d '{"username":"alice", "password":"secret"}'
```

Use the returned token in all future requests via `Authorization: Bearer `.

---

## 🧠 Example Chat Flow

```bash
# Ask a question
curl -X POST http://localhost:3000/ask \
-H "Authorization: Bearer " \
-H "Content-Type: application/json" \
-d '{"question": "What is the capital of Switzerland?"}'

# Continue with a persona
curl -X POST http://localhost:3000/ask \
-H "Authorization: Bearer " \
-H "Content-Type: application/json" \
-d '{"question": "Tell me something cool about it.", "persona": "friendly"}'
```

---

## βš™οΈ Setup

```bash
# Install dependencies
pip install flask langchain openai

# Set environment variables
export OPENAI_API_KEY="your-key"
export OPENAI_API_VERSION="2023-05-15"

# Start the app
python app_multiuser.py # or app.py for basic version
```

---

## βœ… Shell Test Scripts

Each major module has a matching shell script (not shown here) that includes cURL-based API tests. Great for verifying:
- Authentication
- Chat memory
- Persona behavior
- Conversation persistence

Run them from your terminal to simulate real-world usage.

---

## 🎯 Use Cases

- πŸ§ͺ Prototype chatbot UIs or assistant backends
- 🧰 Develop and test LangChain memory chains
- πŸ§‘β€πŸ’» Practice secure API design with authentication
- 🀹 Experiment with persona-aware LLM behaviors

---

## πŸ“£ Contributions & Feedback

PRs welcome. If it breaks, log it. If it's awesome, share it. Let’s build cool stuff. πŸš€

> ⚠️ **Note:** Some Python modules in this repository are partial implementations or work-in-progress stubs. You may encounter placeholder functions, incomplete classes, or redundant components (e.g., `prompt_engineering1.py`, `user_authentication.py`). These are included for development transparency and future extension.