Skip to main content
Based on the refreshed ai-agent-crew-ai-examples codebase, here’s how to run the knowledge_agent FastAPI service, ingest docs, and stream answers into CometChat.

What you’ll build

  • A CrewAI agent that grounds replies in your ingested docs (per namespace).
  • A FastAPI service with ingest/search/generate endpoints plus a /stream SSE.
  • CometChat AI Agent wiring that consumes newline-delimited JSON chunks (text_start, text_delta, text_end, done).

Prerequisites

  • Python 3.10+ with pip (or uv)
  • OPENAI_API_KEY (optionally OPENAI_BASE_URL, KNOWLEDGE_OPENAI_MODEL, KNOWLEDGE_EMBEDDING_MODEL)
  • A CometChat app + AI Agent entry

Run the updated sample

1

Install & start

In ai-agent-crew-ai-examples/:
python3 -m venv .venv
source .venv/bin/activate
pip install -e .
uvicorn knowledge_agent.main:app —host 0.0.0.0 —port 8000 —reload
Env supports .env at repo root or inside knowledge_agent/.env.
2

Set env

OPENAI_API_KEY is required. Optional: OPENAI_BASE_URL, KNOWLEDGE_OPENAI_MODEL (default gpt-4o-mini), KNOWLEDGE_EMBEDDING_MODEL (default text-embedding-3-small).
3

Storage

Ingested files land in knowledge_agent/data/knowledge/<namespace>/ and embeddings persist to knowledge_agent/data/chroma/<namespace>/. Duplicate hashes are skipped automatically.

API surface (FastAPI)

  • POST /api/tools/ingest — accept JSON or multipart/form-data with sources (text/markdown/url) and optional file uploads; returns saved, skipped, errors.
  • POST /api/tools/searchDocs — semantic search via Chroma; accepts namespace, query, max_results.
  • POST /api/agents/knowledge/generate — single, non-streaming completion (requires at least one message).
  • POST /stream — newline-delimited JSON over SSE (text_start, text_delta, text_end, done; error on failure) ready for CometChat BYOA.
  • Validation/behavior: /ingest dedupes by content hash (skips duplicates) and returns 207 when mixed errors/saved; /stream rejects empty messages.

Ingest examples

Multipart uploads are also supported:

Search + answer

Streaming payload shape:

Crew internals (for reference)

knowledge_agent/knowledge_manager.py builds a search tool per namespace, wired into a CrewAI agent:

Wire it to CometChat

  • Dashboard → AI Agent → BYO Agents and then Get Started / Integrate → Choose CrewAI. → Agent ID (e.g., knowledge) → Deployment URL = your public /stream.
  • The SSE stream is newline-delimited JSON; CometChat AG-UI adapters parse text_start/text_delta/text_end and stop on done. Message IDs, thread IDs, and run IDs are included for threading.
  • Use namespaces to keep customer/workspace data separate; pass namespace in the payload or inside tool_params.namespace (either works; defaults to default if omitted).
  • Keep secrets server-side; add auth headers on the FastAPI route if needed.