Document SDK layout, API mapping, and links to FastGPT OpenAPI (应用管理). Co-authored-by: Cursor <cursoragent@cursor.com>
4.9 KiB
AGENTS.md — FastGPT Python SDK
Guidance for AI agents working in this repository.
What this repo is
Python client (fastgpt_client) for FastGPT OpenAPI. It wraps HTTP calls with httpx, returns httpx.Response objects, and provides sync/async clients plus streaming helpers.
Package layout
| Path | Role |
|---|---|
fastgpt_client/client.py |
ChatClient, AppClient (sync) |
fastgpt_client/async_client.py |
AsyncChatClient, AsyncAppClient |
fastgpt_client/base_client.py |
Auth, retries, validation |
fastgpt_client/streaming.py |
iter_stream_events, aiter_stream_events |
docs/ |
MkDocs user docs (source of truth for examples) |
tests/ |
pytest suite |
Official API reference
Use these when adding or verifying endpoints (do not guess paths or payloads):
| Area | Interactive OpenAPI | Written docs |
|---|---|---|
| 应用管理 (app logs, analytics) | cloud.fastgpt.cn/openapi — 应用管理 | 应用接口 |
| Chat / conversations | Chat API | docs/api/chat_client.md |
Auth: Authorization: Bearer {api_key} on every request.
Base URLs
- SaaS (cloud):
https://cloud.fastgpt.cn— app analytics routes live under/api/proApi/... - Self-hosted: your deployment origin (default in code:
http://localhost:3000)
Pass base_url to the client; paths in code are relative (e.g. /api/proApi/core/app/logs/getChartData).
SDK ↔ API mapping
Chat (ChatClient / AsyncChatClient)
| SDK method | HTTP | Notes |
|---|---|---|
create_chat_completion |
POST /api/v1/chat/completions |
stream=True for SSE; optional chatId, variables, detail |
get_chat_histories |
POST /api/core/chat/getHistories |
Requires appId |
get_chat_init |
GET /api/core/chat/init |
|
get_chat_records |
POST /api/core/chat/getPaginationRecords |
|
get_record_detail |
GET /api/core/chat/getResData |
|
update_chat_history |
POST /api/core/chat/updateHistory |
|
delete_chat_history |
DELETE /api/core/chat/delHistory |
|
clear_chat_histories |
DELETE /api/core/chat/clearHistories |
|
delete_chat_record |
DELETE /api/core/chat/item/delete |
|
send_feedback |
POST /api/core/chat/feedback/updateUserFeedback |
|
get_suggested_questions |
POST /api/core/ai/agent/v2/createQuestionGuide |
App / 应用管理 (AppClient / AsyncAppClient)
| SDK method | HTTP | OpenAPI tag |
|---|---|---|
get_app_logs_chart |
POST /api/proApi/core/app/logs/getChartData |
应用管理 — log dashboard |
Not implemented in SDK yet (see official app docs): e.g. GET /api/proApi/core/app/logs/getTotalData. Add methods in client.py and async_client.py, mirror in tests, and document in docs/api/app_client.md.
Conventions for changes
- Match existing style: Google docstrings, type hints on public APIs, camelCase parameter names where the API uses them (
appId,chatId,dateStart). - Responses: Methods return
httpx.Response; callers use.raise_for_status()and.json(). Map HTTP errors viafastgpt_client.exceptionsin_handle_error_response. - Sync + async: Implement both when adding a new endpoint.
- Tests: Add/update tests under
tests/test_*_client.py; runpytestbefore finishing. - Docs: Update
docs/api/*.mdandREADME.mdonly when behavior or public API changes. - Scope: Minimal diffs; no unrelated refactors. Do not commit secrets (
.env, API keys).
Typical usage (for generated code)
from fastgpt_client import ChatClient, AppClient
with ChatClient(api_key="fastgpt-...", base_url="https://cloud.fastgpt.cn") as chat:
r = chat.create_chat_completion(
messages=[{"role": "user", "content": "Hello"}],
stream=False,
)
r.raise_for_status()
print(r.json()["choices"][0]["message"]["content"])
with AppClient(api_key="fastgpt-...", base_url="https://cloud.fastgpt.cn") as app:
r = app.get_app_logs_chart(
appId="your-app-id",
dateStart="2025-01-01T00:00:00.000Z",
dateEnd="2025-01-31T23:59:59.999Z",
source=["api", "online"],
)
r.raise_for_status()
Streaming: prefer iter_stream_events / aiter_stream_events over hand-parsing SSE when possible (see docs/advanced/streaming_events.md).
Development commands
pip install -e ".[dev]"
pytest
ruff check fastgpt_client/
ruff format fastgpt_client/
mkdocs serve # optional: local docs
When unsure
- Read the matching method in
fastgpt_client/client.py. - Cross-check the cloud OpenAPI or doc.fastgpt.io for request/response shapes.
- Follow patterns in
tests/andexamples/.