Skip to main content

AI RAG bot utilities for OpenAI + LangChain with FAISS

Project description

python-ai-ragbot

python-ai-ragbot is a modular and framework-agnostic Python package for building intelligent chatbots and voicebots with Retrieval-Augmented Generation (RAG), powered by OpenAI and LangChain.

It provides a simple interface to attach ready-made request handlers into popular frameworks (FastAPI, Flask, Django, Starlette, WSGI, ASGI). You can quickly add both text chat (/chat) and voice (/voice) endpoints into your app.


Features

  • Supports knowledge sources:
    • Local files (.pdf, .docx, .txt, .md)
    • Website scraping (URLs, sitemaps)
  • /chat endpoint (text query → answer)
  • /voice endpoint (speech-to-text via Whisper, TTS for responses)
  • Fully configurable models, embeddings, voices, chunking, logging
  • In-memory FAISS vector store via LangChain
  • Adapters for:
    • FastAPI
    • Starlette
    • Flask
    • Django
    • Raw WSGI
    • Raw ASGI
  • Sync (init_rag_voice_bot) and Async (init_rag_voice_bot_async) APIs

Requirements

  • Python 3.9+
  • An OpenAI API key (OPENAI_API_KEY in .env)

Installation

pip install python-ai-ragbot

For local development:

git clone https://github.com/your-org/python-ai-ragbot.git
cd python-ai-ragbot
pip install -e .

Quick Start (FastAPI)

# examples/server.py
from fastapi import FastAPI
from python_ai_ragbot import init_rag_voice_bot_async
from python_ai_ragbot.http.adapters import use_in_fastapi
from fastapi.middleware.cors import CORSMiddleware
from dotenv import load_dotenv
import os

load_dotenv()

app = FastAPI()
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.on_event("startup")
async def startup_event():
    bot = await init_rag_voice_bot_async({
        "sources": {"files": ["examples/knowledge.txt"]},
        "openai": {
            "apiKey": os.getenv("OPENAI_API_KEY"),
            "chat": {"model": "gpt-4o"},
            "whisper": {"model": "whisper-1"},
            "tts": {"model": "tts-1-hd", "voice": "nova"},
        }
    })
    use_in_fastapi(app, bot["chat_handler"], bot["voice_handler"], prefix="/api/bot")

Run:

uvicorn examples.server:app --reload --port 3001

Endpoints:

  • POST /api/bot/chat
  • POST /api/bot/voice

Usage with Other Frameworks

Starlette

from starlette.applications import Starlette
from starlette.middleware.cors import CORSMiddleware
from python_ai_ragbot import init_rag_voice_bot_async
from python_ai_ragbot.http.adapters import use_in_starlette
import os, asyncio

app = Starlette()
app.add_middleware(CORSMiddleware, allow_origins=["*"])

@app.on_event("startup")
async def startup():
    bot = await init_rag_voice_bot_async({
        "sources": {"files": ["examples/knowledge.txt"]},
        "openai": {"apiKey": os.getenv("OPENAI_API_KEY")},
    })
    use_in_starlette(app, bot["chat_handler"], bot["voice_handler"], prefix="/api/bot")

Flask

from flask import Flask
from python_ai_ragbot import init_rag_voice_bot
from python_ai_ragbot.http.adapters import use_in_flask
import os
from dotenv import load_dotenv

load_dotenv()
app = Flask(__name__)

bot = init_rag_voice_bot({
    "sources": {"files": ["examples/knowledge.txt"]},
    "openai": {"apiKey": os.getenv("OPENAI_API_KEY")},
})
use_in_flask(app, bot["chat_handler"], bot["voice_handler"], prefix="/api/bot")

if __name__ == "__main__":
    app.run(port=3001)

Django

# myproject/urls.py
from django.urls import path
from python_ai_ragbot import init_rag_voice_bot
from python_ai_ragbot.http.adapters import use_in_django
import os
from dotenv import load_dotenv

load_dotenv()
urlpatterns = []

bot = init_rag_voice_bot({
    "sources": {"files": ["examples/knowledge.txt"]},
    "openai": {"apiKey": os.getenv("OPENAI_API_KEY")},
})
use_in_django(urlpatterns, bot["chat_handler"], bot["voice_handler"], prefix="/api/bot")

Raw WSGI

from wsgiref.simple_server import make_server
from python_ai_ragbot import init_rag_voice_bot
from python_ai_ragbot.http.adapters import use_in_wsgi
import os

bot = init_rag_voice_bot({
    "sources": {"files": ["examples/knowledge.txt"]},
    "openai": {"apiKey": os.getenv("OPENAI_API_KEY")},
})

app = {}
use_in_wsgi(app, bot["chat_handler"], bot["voice_handler"], prefix="/api/bot")

with make_server("", 3001, app["wsgi"]) as httpd:
    print("Serving on port 3001...")
    httpd.serve_forever()

Raw ASGI

import uvicorn
from python_ai_ragbot import init_rag_voice_bot_async
from python_ai_ragbot.http.adapters import use_in_starlette
from starlette.applications import Starlette
import os

app = Starlette()

@app.on_event("startup")
async def startup():
    bot = await init_rag_voice_bot_async({
        "sources": {"files": ["examples/knowledge.txt"]},
        "openai": {"apiKey": os.getenv("OPENAI_API_KEY")},
    })
    use_in_starlette(app, bot["chat_handler"], bot["voice_handler"], prefix="/api/bot")

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=3001)

Configuration

{
  "sources": {
    "files": ["knowledge.txt", "knowledge.pdf"],
    "urls": ["https://docs.example.com"]
  },
  "rag": {
    "textSplit": {"chunkSize": 1000, "chunkOverlap": 200},
    "topK": 3
  },
  "openai": {
    "apiKey": "...",
    "embeddings": {"model": "text-embedding-3-small"},
    "chat": {"model": "gpt-4o", "temperature": 0.3},
    "whisper": {"model": "whisper-1"},
    "tts": {"model": "tts-1-hd", "voice": "nova"}
  },
  "logger": "console"
}

Endpoints

/chat

  • POST JSON
{"question": "What is in the knowledge base?"}

/voice

  • POST raw audio (audio/webm, audio/wav, etc.)

Example Project Structure

my-app/
├── examples/
│   ├── server.py
│   ├── knowledge.txt
├── src/
│   └── python_ai_ragbot/
├── .env
└── pyproject.toml

Notes

  • Use init_rag_voice_bot_async in ASGI frameworks (FastAPI, Starlette).
  • Use init_rag_voice_bot in WSGI frameworks (Flask, Django, raw WSGI).
  • Vector store is in-memory only; data is reloaded on each startup.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_ai_ragbot-0.1.0.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_ai_ragbot-0.1.0-py3-none-any.whl (14.5 kB view details)

Uploaded Python 3

File details

Details for the file python_ai_ragbot-0.1.0.tar.gz.

File metadata

  • Download URL: python_ai_ragbot-0.1.0.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for python_ai_ragbot-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2bf2507a199fb7179a4bef1e315b5afc95a69e734f5de8faa1163757f90c27cc
MD5 984cdd17e3bb17acaf6f7e52bd6cf25d
BLAKE2b-256 faf35fdcaf90f29d4b0a4e8f9438affc9767fae6bba58bcf7324abbd0f5148d3

See more details on using hashes here.

File details

Details for the file python_ai_ragbot-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for python_ai_ragbot-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d47adc255313abc6479884902be3593a4e8458b04a9e51af29db888e77f78889
MD5 321c36e9bb658fcaedcea9f2fd6ed27a
BLAKE2b-256 6c222a1f089bf9820e48382aebf278582d8c42d2e77bd36bb9a8d860e7a0af87

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page