Repository navigation
Add Agent API examples and comprehensive README - #13904
princeaeri06-droid wants to merge 2 commits into
Conversation
- Add agent_example.py: Python SDK example for agent CRUD and sessions - Add agent_example.sh: cURL examples for Agent HTTP API - Add README.md: Comprehensive guide for all API examples - Covers agent lifecycle, session management, streaming/non-streaming - Includes troubleshooting, quick start, and API reference Fixes infiniflow#4310 Co-authored-by: Copilot <[email protected]>
|
done some changes |
|
some new changes |
There was a problem hiding this comment.
Pull request overview
Adds new “agent” API examples under example/ to address #4310 by providing runnable SDK and cURL snippets, plus a consolidated README intended to guide developers through common workflows.
Changes:
- Added a Python SDK example for agent CRUD + basic session management.
- Added a cURL script covering agent CRUD, session lifecycle, and agent completion calls.
- Added an
example/README.mdintended as a comprehensive index/quickstart for API examples.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 7 comments.
| File | Description |
|---|---|
| example/sdk/agent_example.py | New Python example demonstrating agent lifecycle + session CRUD. |
| example/http/agent_example.sh | New cURL example covering agent CRUD, sessions, and completions. |
| example/README.md | New “comprehensive” README describing example layout, quickstart, and patterns. |
|
|
||
| HOST_ADDRESS = "http://127.0.0.1" | ||
| API_KEY = "ragflow-IzZmY1MGVhYTBhMjExZWZiYTdjMDI0Mm" |
There was a problem hiding this comment.
The example hard-codes an API key that looks like a real credential. Please replace with a placeholder (e.g., ragflow-{your_api_key}) or read from an environment variable so secrets aren’t committed to the repo.
| HOST_ADDRESS = "http://127.0.0.1" | |
| API_KEY = "ragflow-IzZmY1MGVhYTBhMjExZWZiYTdjMDI0Mm" | |
| import os | |
| HOST_ADDRESS = "http://127.0.0.1" | |
| API_KEY = os.getenv("RAGFLOW_API_KEY", "ragflow-{your_api_key}") |
| from ragflow_sdk import RAGFlow | ||
| import sys | ||
|
|
||
| HOST_ADDRESS = "http://127.0.0.1" |
There was a problem hiding this comment.
HOST_ADDRESS is set to http://127.0.0.1 (no port), but the default Docker deployment (and the cURL examples below) use http://localhost:9380. Consider updating the example to use http://127.0.0.1:9380 (or document why port 80 is expected) to prevent copy/paste failures.
| HOST_ADDRESS = "http://127.0.0.1" | |
| HOST_ADDRESS = "http://127.0.0.1:9380" # Default RAGFlow Docker port |
| # List agents to get the created agent | ||
| print("Listing agents...") | ||
| agents = ragflow_instance.list_agents(page=1, page_size=10, orderby="create_time", desc=True) | ||
| if not agents: | ||
| raise Exception("No agents found after creation") | ||
|
|
||
| agent_instance = agents[0] # Get the most recently created agent | ||
| print(f"Agent created: {agent_instance.id} - {agent_instance.description}") | ||
|
|
There was a problem hiding this comment.
The example finds the created agent by listing agents and taking agents[0], which can select (and later delete) the wrong agent in a non-empty tenant. Prefer using a unique title (e.g., include a timestamp) and filtering via list_agents(title=...), or capture the created agent ID from the create response if available.
|
|
||
| # Create an agent | ||
| echo -e "\n-- Create an agent" | ||
| curl --request POST \ | ||
| --url http://localhost:9380/api/v1/agents \ | ||
| --header 'Content-Type: application/json' \ | ||
| --header 'Authorization: Bearer ragflow-IzZmY1MGVhYTBhMjExZWZiYTdjMDI0Mm' \ |
There was a problem hiding this comment.
This script hard-codes a Bearer token that appears to be an actual API key. Replace it with a placeholder or reference an environment variable (e.g., $API_KEY) to avoid committing credentials and to make the script reusable.
| # Create an agent | |
| echo -e "\n-- Create an agent" | |
| curl --request POST \ | |
| --url http://localhost:9380/api/v1/agents \ | |
| --header 'Content-Type: application/json' \ | |
| --header 'Authorization: Bearer ragflow-IzZmY1MGVhYTBhMjExZWZiYTdjMDI0Mm' \ | |
| # Set API_KEY in your environment before running, e.g.: | |
| # export API_KEY="your-api-key-here" | |
| # Create an agent | |
| echo -e "\n-- Create an agent" | |
| curl --request POST \ | |
| --url http://localhost:9380/api/v1/agents \ | |
| --header 'Content-Type: application/json' \ | |
| --header "Authorization: Bearer ${API_KEY}" \ |
| ├── sdk/ # Python SDK examples | ||
| │ ├── dataset_example.py # Dataset CRUD operations | ||
| │ ├── chat_example.py # Chat management and completions | ||
| │ ├── document_example.py # Document upload and processing | ||
| │ ├── retrieval_example.py # Knowledge base retrieval | ||
| │ └── agent_example.py # Agent CRUD and session management | ||
| │ | ||
| ├── http/ # cURL/HTTP examples | ||
| │ ├── dataset_example.sh | ||
| │ ├── chat_example.sh | ||
| │ ├── document_example.sh | ||
| │ ├── retrieval_example.sh | ||
| │ └── agent_example.sh | ||
| │ | ||
| └── chat_demo/ # Chat demo application |
There was a problem hiding this comment.
The README’s directory structure and guide reference several example files that don’t exist in example/sdk/ or example/http/ (e.g., chat_example.py, document_example.py, retrieval_example.py and corresponding .sh). Either add these files as part of the PR or update the README to only describe the examples that are actually present.
| ├── sdk/ # Python SDK examples | |
| │ ├── dataset_example.py # Dataset CRUD operations | |
| │ ├── chat_example.py # Chat management and completions | |
| │ ├── document_example.py # Document upload and processing | |
| │ ├── retrieval_example.py # Knowledge base retrieval | |
| │ └── agent_example.py # Agent CRUD and session management | |
| │ | |
| ├── http/ # cURL/HTTP examples | |
| │ ├── dataset_example.sh | |
| │ ├── chat_example.sh | |
| │ ├── document_example.sh | |
| │ ├── retrieval_example.sh | |
| │ └── agent_example.sh | |
| │ | |
| └── chat_demo/ # Chat demo application | |
| ├── sdk/ # Python SDK examples (datasets, agents, etc.) | |
| ├── http/ # cURL/HTTP examples corresponding to the SDK examples | |
| └── chat_demo/ # Chat demo application |
| ### 5. Agent Examples ⭐ **NEW** | ||
|
|
||
| **What it demonstrates:** | ||
| - Create and manage AI agents with custom workflows | ||
| - Agent session management | ||
| - Execute agents (streaming/non-streaming) | ||
| - Agent DSL (Domain Specific Language) structure | ||
| - Basic agent components (Begin, LLM) | ||
|
|
||
| **Python SDK:** | ||
| ```bash | ||
| python example/sdk/agent_example.py | ||
| ``` |
There was a problem hiding this comment.
The README claims the Python SDK agent example demonstrates executing agents (streaming/non-streaming), but example/sdk/agent_example.py currently only does agent/session CRUD and never calls Session.ask(...) to run a completion. Please either add an execution example to the Python script or adjust this section to match the actual behavior.
| 1. **Running RAGFlow Instance** | ||
| ```bash | ||
| # Start RAGFlow with Docker | ||
| docker compose up -d | ||
| ``` | ||
|
|
||
| 2. **API Key** | ||
| - Log in to RAGFlow admin panel (default: http://localhost:9380) | ||
| - Go to Settings → API Keys | ||
| - Generate a new API key (format: `ragflow-{token}`) | ||
|
|
||
| 3. **Python SDK** (for SDK examples) | ||
| ```bash | ||
| pip install ragflow-sdk | ||
| ``` | ||
|
|
||
| ### Configuration | ||
|
|
||
| Replace these values in the examples: | ||
|
|
||
| | Variable | Location | How to Find | | ||
| |----------|----------|-------------| | ||
| | `API_KEY` | All examples | RAGFlow admin panel → Settings → API Keys | | ||
| | `HOST_ADDRESS` | All examples | Your RAGFlow server URL (default: `http://127.0.0.1`) | | ||
| | `AGENT_ID` | Agent examples | Response from create agent or list agents | | ||
| | `SESSION_ID` | Agent examples | Response from create session | | ||
| | `DATASET_ID` | Dataset examples | Response from create dataset or list datasets | |
There was a problem hiding this comment.
The README has conflicting defaults for the server address: it suggests the admin panel/API default is on localhost:9380, but the configuration table lists HOST_ADDRESS default as http://127.0.0.1 (no port), and the SDK examples use that value. Please make these defaults consistent (and clarify that the SDK base_url should not include /api/v1 since the SDK appends it).
… and retrieval (#4310) (#14208) Closes #4310 ### What problem does this PR solve? Issue #4310 requests practical examples for the RAGFlow SDK and HTTP API to help developers get started faster. The existing `example/sdk/` folder only contains `dataset_example.py`. This PR fills the remaining gaps by adding examples for three key API areas not yet covered in `main` or by other open PRs (#13904, #13284): - **Chunk management** — add, list, update, delete, and retrieve chunks within a dataset - **Chat assistant** — create a chat assistant, open a session, send messages (streaming and non-streaming), and clean up - **Retrieval** — perform semantic retrieval across one or multiple datasets ### Type of change - [x] Documentation Update - [x] New Feature (non-breaking change which adds functionality)
Fixes #4310
What problem does this PR solve?
Briefly describe what this PR aims to solve. Include background context that will help reviewers understand the purpose of the PR.
Type of change