What You’ll Build
- An AG2 (AutoGen 2) agent that behaves like a documentation expert.
- Triggered only when invoked (e.g.,
@knowledge). - Retrieves & cites snippets from an on-disk knowledge store.
- Integrated into CometChat conversations with streaming responses.
Prerequisites
- Python 3.10+ and
pip. OPENAI_API_KEYin a.envfile (used for embeddings + chat completions).- Optional overrides:
KNOWLEDGE_AGENT_MODEL(defaultgpt-4o-mini)KNOWLEDGE_AGENT_EMBEDDING_MODEL(defaulttext-embedding-3-small)KNOWLEDGE_AGENT_TOP_K,KNOWLEDGE_AGENT_TEMPERATURE, etc.
- CometChat app credentials (App ID, Region, Auth Key).
Quick links
- Repo (examples): ai-agent-ag2-examples/ag2-knowledge-agent
- Agent source: agent.py
- FastAPI server: server.py
- Sample web embed: web/index.html
How it works
This project packages a retrieval-augmented AG2 agent that:- Ingests sources into namespaces using the
KnowledgeStore. POST payloads to/tools/ingestwith URLs, local files, or raw text. Files (.pdf,.md,.txt) are parsed and chunked before embeddings are stored underknowledge/<namespace>/index.json. - Searches namespaces via
/tools/search, ranking chunks with cosine similarity on OpenAI embeddings (defaults to top 4 results). - Streams answers from
/agent. The server wraps AG2’sConversableAgent, emits SSE events for tool calls (tool_call_start,tool_call_result) and text chunks, and always terminates with[DONE]. - Cites sources. The agent compiles the retrieved snippets, instructs the LLM to cite them with bracketed numbers, and appends a
Sources:line. - Hardens operations: namespaces are sanitized, ingestion is thread-safe, and errors are surfaced as SSE
errorevents so the client can render fallbacks.
agent.py— KnowledgeAgent, store helpers, embedding + retrieval logic.server.py— FastAPI app hosting/agent,/tools/ingest,/tools/search,/health.knowledge/— default storage root (persisted chunks + index).web/index.html— CometChat Chat Embed sample that points to your deployed agent.
Setup
1
Clone & install
git clone https://github.com/cometchat/ai-agent-ag2-examples.git then cd ai-agent-ag2-examples/ag2-knowledge-agent. Create a virtualenv and run pip install -r requirements.txt.2
Configure environment
Create a
.env file, set OPENAI_API_KEY, and override other knobs as needed (model, temperature, namespace).3
Prime knowledge
Run
python server.py once (or uvicorn server:app) to initialize folders. Use the ingest endpoint or the provided knowledge samples as a starting point.4
Run locally
Start FastAPI:
uvicorn server:app —reload —host 0.0.0.0 —port 8000. Verify GET /health returns {"status": "healthy"}.5
Create a tunnel
Expose
/agent publicly with ngrok, Cloudflare Tunnel, or Render so CometChat can reach it.Project structure
- Dependencies: requirements.txt
- Agent logic: agent.py
- Knowledge store: knowledge/
- API server: server.py
- Web sample: web/index.html
Core configuration
Environment variables
Environment variables
Knowledge ingestion schema
Knowledge ingestion schema
Streaming response shape
Streaming response shape
Step 1 - Ingest documentation
1
Pick a namespace
Stick with
docs or choose a sanitized string (alphanumeric, -, _).2
POST to /tools/ingest
Supply a mix of URLs, file paths, and inline text. The server fetches, parses (PDF/Markdown/Text), chunks, embeds, and persists everything.
3
Inspect storage
Confirm
knowledge/<namespace> contains generated .md copies and an updated index.json.Step 2 - Ask the knowledge agent
1
Make a request
2
Handle SSE
Streamed events include tool telemetry and text deltas. Buffer them until you receive
[DONE].3
Observe citations
Replies contain bracketed numbers matching the
matches payload returned by the retrieval tool.Step 3 - Deploy the API
- Host the FastAPI app (Render, Fly.io, ECS, etc.) or wrap it in a serverless function.
- Expose
/agent,/tools/ingest, and/tools/searchover HTTPS. - Keep
KNOWLEDGE_AGENT_STORAGE_PATHon a persistent volume so ingested docs survive restarts. - Harden CORS, rate limiting, and authentication before opening the endpoints to the public internet.
Step 4 - Configure CometChat
1
Open Dashboard
Go to app.cometchat.com.
2
Add agent
Navigation: AI Agents → Add Agent.
3
Provider & IDs
Set Provider=AG2 (AutoGen), Agent ID=
knowledge, Deployment URL=public /agent endpoint.4
Optional metadata
Add greeting, intro, or suggested prompts such as “@knowledge Summarize the onboarding guide.”
5
Enable
Save then confirm the toggle shows Enabled.
For more on CometChat AI Agents, see: Overview · Instructions · Custom agents
Step 5 - Customize in UI Kit Builder
1
Open variant
From the Dashboard variant card, click Customize and Deploy.
2
Update UX
Themes, layouts, attachment rules, and action bindings—all reflect in exports.
3
Preview
Run conversations and confirm the knowledge agent responds only when invoked.

Step 6 - Integrate
Once configured, embed the agent in your product using the exported configuration.The AG2 Knowledge agent you connected is bundled automatically. End-users will see it immediately after deployment.
Step 7 - Test end-to-end
1
API generates response
POST to
/agent returns cited answers.2
Search utility works
POST to
/tools/search returns ranked snippets.3
Dashboard check
In CometChat, ensure the agent appears as a selectable bot.
4
Widget/UI verification
Preview in UI Kit Builder or your app and confirm streaming + citations behave as expected.
Security & production checklist
- Protect ingestion routes with auth (API key/JWT) and restrict who can write to each namespace.
- Validate URLs and file paths before fetching to avoid SSRF or untrusted uploads.
- Rate limit the
/agentand/tools/*endpoints to prevent abuse. - Keep OpenAI keys server-side; never ship them in browser code or UI Kit Builder exports.
- Log tool calls (namespace, duration) for debugging and monitoring.
Troubleshooting
- Agent replies without citations: confirm the retrieval step returns matches; check embeddings or namespace.
- Empty or slow results: ensure documents were ingested successfully and the top-k limit isn’t set to 0.
503 Agent not initialized: verify server logs—KnowledgeAgentrequiresOPENAI_API_KEYduring startup.- SSE errors in widget: make sure your reverse proxy supports streaming and disables response buffering.
Next steps
- Add new tools (e.g.,
summarize,link_to_source) by extending the AG2 agent before responding. - Swap in enterprise vector stores by replacing
KnowledgeStorewith Pinecone, Qdrant, or Postgres. - Schedule re-ingestion jobs when docs change, or watch folders for automatic refreshes.
- Layer analytics: log
tool_call_result.matchesfor insight into content coverage.