mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-05-23 09:28:06 +08:00
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)
90 lines
3.3 KiB
Bash
90 lines
3.3 KiB
Bash
#!/bin/bash
|
|
#
|
|
# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
#
|
|
|
|
# Variables
|
|
HOST_ADDRESS="${RAGFLOW_HOST_ADDRESS:-http://localhost:9380}"
|
|
API_KEY="${RAGFLOW_API_KEY:-ragflow-IzZmY1MGVhYTBhMjExZWZiYTdjMDI0Mm}"
|
|
|
|
# Check for jq
|
|
if ! command -v jq &> /dev/null; then
|
|
echo "jq could not be found, please install it to run this example."
|
|
exit 1
|
|
fi
|
|
|
|
# 0. Setup: Create a dataset and upload a document to get IDs
|
|
echo -e "\n-- Creating a dataset"
|
|
DATASET_ID=$(curl -s --request POST \
|
|
--url "${HOST_ADDRESS}/api/v1/datasets" \
|
|
--header 'Content-Type: application/json' \
|
|
--header "Authorization: Bearer ${API_KEY}" \
|
|
--data '{"name": "chunk_shell_example"}' | jq -r '.data.id')
|
|
echo "Dataset ID: ${DATASET_ID}"
|
|
|
|
echo -e "\n-- Uploading a document"
|
|
DOC_ID=$(curl -s --request POST \
|
|
--url "${HOST_ADDRESS}/api/v1/datasets/${DATASET_ID}/documents" \
|
|
--header "Authorization: Bearer ${API_KEY}" \
|
|
--form 'file=@sample.txt;type=text/plain' \
|
|
--form 'display_name=sample.txt' | jq -r '.data[0].id')
|
|
echo "Document ID: ${DOC_ID}"
|
|
|
|
# 1. Add a chunk to a document
|
|
echo -e "\n-- Add a chunk to a document"
|
|
CHUNK_ID=$(curl -s --request POST \
|
|
--url "${HOST_ADDRESS}/api/v1/datasets/${DATASET_ID}/documents/${DOC_ID}/chunks" \
|
|
--header 'Content-Type: application/json' \
|
|
--header "Authorization: Bearer ${API_KEY}" \
|
|
--data '{
|
|
"content": "RAGFlow is an open-source RAG engine.",
|
|
"important_keywords": ["RAGFlow", "open-source"]
|
|
}' | jq -r '.data.chunk.id')
|
|
echo "Chunk ID: ${CHUNK_ID}"
|
|
|
|
# 2. List chunks of a document
|
|
echo -e "\n-- List chunks of a document"
|
|
curl -s --request GET \
|
|
--url "${HOST_ADDRESS}/api/v1/datasets/${DATASET_ID}/documents/${DOC_ID}/chunks?page=1&page_size=10" \
|
|
--header "Authorization: Bearer ${API_KEY}" | jq .
|
|
|
|
# 3. Update a chunk
|
|
echo -e "\n-- Update a chunk"
|
|
curl -s --request PUT \
|
|
--url "${HOST_ADDRESS}/api/v1/datasets/${DATASET_ID}/documents/${DOC_ID}/chunks/${CHUNK_ID}" \
|
|
--header 'Content-Type: application/json' \
|
|
--header "Authorization: Bearer ${API_KEY}" \
|
|
--data '{
|
|
"content": "RAGFlow is a powerful open-source RAG engine."
|
|
}' | jq .
|
|
|
|
# 4. Delete chunks
|
|
echo -e "\n-- Delete chunks"
|
|
curl -s --request DELETE \
|
|
--url "${HOST_ADDRESS}/api/v1/datasets/${DATASET_ID}/documents/${DOC_ID}/chunks" \
|
|
--header 'Content-Type: application/json' \
|
|
--header "Authorization: Bearer ${API_KEY}" \
|
|
--data "{
|
|
\"chunk_ids\": [\"${CHUNK_ID}\"]
|
|
}" | jq .
|
|
|
|
# Cleanup
|
|
echo -e "\n-- Cleaning up dataset"
|
|
curl -s --request DELETE \
|
|
--url "${HOST_ADDRESS}/api/v1/datasets" \
|
|
--header 'Content-Type: application/json' \
|
|
--header "Authorization: Bearer ${API_KEY}" \
|
|
--data "{\"ids\": [\"${DATASET_ID}\"]}" | jq .
|