agentsclimarketplace

Fastapi llmindex rag

Skill kocicjelena/claude-plugin-fastapi/skills/fastapi-llmindex-rag

Scaffold a production Python FastAPI + LlamaIndex RAG service through a guided interview, with token-cost efficiency built in: provider fallback (paid → secondary key → free local Ollama), response/embedding caching ("memory"), cheap-model routing (small model classifies/rewrites, strong model only for the final answer), and model-aware tactics (prompt caching, Batch API, context trimming). Covers SQLModel models, async routes and coroutines, parallel processing (asyncio.gather, run_in_threadpool, BackgroundTasks, SSE streaming), and a persist/reload query engine. Use this skill whenever the user builds or extends a FastAPI RAG API over their own documents, especially when they mention cutting token cost, fallback credits, caching, or model budgets — even if they don't name LlamaIndex. Trigger on: FastAPI, LlamaIndex, RAG API, query engine, aquery, async endpoints, asyncio.gather, SQLModel, pgvector, SSE, token cost, cheap model, provider fallback, Ollama fallback. NOT for Next.js/ TypeScript or MCP servers.From its SKILL.md

Install
npx -y skills add kocicjelena/claude-plugin-fastapi --skill fastapi-llmindex-rag

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

2 things to look at

  • no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.
  • 0 stars0 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.

What its file declares

Copied from the file, not written here

The file declares its own license as MIT. That is the author’s claim about this one file, and it is not the same thing as the license GitHub reports for the repository, which is listed with the other numbers below.

SKILL.md

5.3 KB, as published. Nobody here has run it

fastapi-llmindex-rag

Build a FastAPI + LlamaIndex RAG service that is async-correct and cost-aware. This skill runs a short interview first (the user must answer), then scaffolds from copy-and-adapt templates. Do not scaffold from memory — the async + LlamaIndex + fallback wiring is easy to get subtly wrong.

STEP 1 — Interview the user (REQUIRED, do this before writing any code)

Ask the questions below and wait for answers. Do not assume defaults silently; if the user says "you pick", state the default you're using and why. The full question set with the reasoning and how each answer maps to code is in references/interview.md — read it and ask from there. The four blocking prompts:

  1. Vector store & data. File-based (persist to disk, no DB — default) or pgvector (Postgres, for scale/shared state)? Where do the documents come from (a directory, uploads, URLs)? Embedding model + dimensions (default text-embedding-3-small, 1536)?

  2. LLM provider & fallback. Primary provider/model (OpenAI gpt-4o-mini, Ollama, …)? Wire a fallback chain (secondary key → local Ollama) for when credits run out or the API rate-limits/errors? Any monthly cost budget to respect?

  3. Endpoints & async. Which endpoints — single query, batched query-batch (parallel via asyncio.gather), streaming query-stream (SSE), background reindex? Public or JWT-authenticated?

  4. Cost controls. Which to enable: response/embedding caching ("memory"), cheap-model routing (skip RAG for non-questions; rewrite with a small model), provider fallback, a per-request output-token cap? Model-aware tactics (prompt caching, OpenAI Batch API) if the provider supports them?

Echo back the chosen configuration in one short summary, then proceed.

STEP 2 — Scaffold from templates

Copy templates/* into app/ and adapt names. Map answers → files:

AnswerFiles
alwaysconfig.py db.py deps.py models.py main.py api_main.py
RAG enginerag_service.py (persist/reload, query, aquery_many, cheap-routing, fallback retry)
endpointsroutes_rag.py (query / batch / stream / reindex)
cost: fallback + routingllm.py (provider fallback chain + should_use_rag + cached chat)
cost: caching / memorycache.py (persistent response/embedding cache)
pgvectormodels.py Chunk (swap in the Vector(1536) column; add pgvector + psycopg)

Install templates/requirements.txt, fill .env from .env.example, run uvicorn app.main:app --reload.

STEP 3 — Apply the cost recipes

references/cost-recipes.md has 20 concrete token/cost-saving recipes (persist the index, cache responses, cheap-model routing, batch embeddings, prompt caching, semantic cache, output-token caps, usage metering, …), each with when/why. Enable the ones the user picked in the interview; mention the rest so they know the levers.

Non-negotiable patterns

  • Lazy-import heavy libs (llama_index, openai, DB drivers) inside functions, so the app boots for /docs/health even without a key or a heavy dep installed.
  • Never block the event loop. query_engine.query() is synchronous — await aquery(...) or wrap in run_in_threadpool. Parallelism = asyncio.gather. See references/async-parallel.md.
  • Fallback + cache are the cost core. Route non-questions away from the LLM, serve cache hits for free, and degrade paid → local instead of failing. See templates/llm.py + templates/cache.py and recipes 1–4, 12–14.
  • Persist and reload the index (recipe 1) — never re-embed on restart. Cache guarded by a threading.Lock (routes run in a threadpool).
  • Config + secrets only via pydantic-settings + .env (gitignored); provider/model/ budget are all settings, so tuning cost is an env change, not a code change.

Read before writing

  • the interview → references/interview.md (ask these; don't skip)
  • anything async/parallel/streaming → references/async-parallel.md
  • the LlamaIndex engine → references/llamaindex.md
  • cost levers → references/cost-recipes.md

Keep looking

Skills are one crate of 607,533. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.