liuhua
liuhua
commited on
Commit
·
49c21eb
1
Parent(s):
9dc377f
Add test for CI (#3114)
Browse files### What problem does this PR solve?
Add test for CI
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
Co-authored-by: liuhua <[email protected]>
- .github/workflows/tests.yml +10 -0
- api/apps/sdk/chat.py +4 -4
- api/apps/sdk/dataset.py +1 -1
- api/apps/sdk/doc.py +2 -2
- api/utils/api_utils.py +4 -2
- sdk/python/hello_ragflow.py +2 -2
- sdk/python/poetry.lock +96 -1
- sdk/python/pyproject.toml +1 -0
- sdk/python/test/conftest.py +4 -4
- sdk/python/test/t_chat.py +8 -6
- sdk/python/test/t_dataset.py +55 -54
- sdk/python/test/t_document.py +4 -3
- sdk/python/test/t_session.py +95 -93
.github/workflows/tests.yml
CHANGED
|
@@ -70,6 +70,16 @@ jobs:
|
|
| 70 |
echo "RAGFLOW_IMAGE=infiniflow/ragflow:dev" >> docker/.env
|
| 71 |
sudo docker compose -f docker/docker-compose.yml up -d
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
- name: Stop ragflow:dev
|
| 74 |
if: always() # always run this step even if previous steps failed
|
| 75 |
run: |
|
|
|
|
| 70 |
echo "RAGFLOW_IMAGE=infiniflow/ragflow:dev" >> docker/.env
|
| 71 |
sudo docker compose -f docker/docker-compose.yml up -d
|
| 72 |
|
| 73 |
+
- name: Run tests
|
| 74 |
+
run: |
|
| 75 |
+
export http_proxy=""; export https_proxy=""; export no_proxy=""; export HTTP_PROXY=""; export HTTPS_PROXY=""; export NO_PROXY=""
|
| 76 |
+
export HOST_ADDRESS=http://host.docker.internal:9380
|
| 77 |
+
until sudo docker exec ragflow-server curl -s --connect-timeout 5 ${HOST_ADDRESS} > /dev/null; do
|
| 78 |
+
echo "Waiting for service to be available..."
|
| 79 |
+
sleep 5
|
| 80 |
+
done
|
| 81 |
+
cd sdk/python && poetry install && source .venv/bin/activate && cd test && pytest t_dataset.py t_chat.py t_session.py
|
| 82 |
+
|
| 83 |
- name: Stop ragflow:dev
|
| 84 |
if: always() # always run this step even if previous steps failed
|
| 85 |
run: |
|
api/apps/sdk/chat.py
CHANGED
|
@@ -41,8 +41,8 @@ def create(tenant_id):
|
|
| 41 |
if kb.chunk_num == 0:
|
| 42 |
return get_error_data_result(f"The dataset {kb_id} doesn't own parsed file")
|
| 43 |
kbs = KnowledgebaseService.get_by_ids(ids)
|
| 44 |
-
embd_count = list(set(kb.embd_id for kb in kbs))
|
| 45 |
-
if embd_count != 1:
|
| 46 |
return get_result(retmsg='Datasets use different embedding models."',retcode=RetCode.AUTHENTICATION_ERROR)
|
| 47 |
req["kb_ids"] = ids
|
| 48 |
# llm
|
|
@@ -167,8 +167,8 @@ def update(tenant_id,chat_id):
|
|
| 167 |
if kb.chunk_num == 0:
|
| 168 |
return get_error_data_result(f"The dataset {kb_id} doesn't own parsed file")
|
| 169 |
kbs = KnowledgebaseService.get_by_ids(ids)
|
| 170 |
-
embd_count=list(set(kb.embd_id for kb in kbs))
|
| 171 |
-
if embd_count != 1 :
|
| 172 |
return get_result(
|
| 173 |
retmsg='Datasets use different embedding models."',
|
| 174 |
retcode=RetCode.AUTHENTICATION_ERROR)
|
|
|
|
| 41 |
if kb.chunk_num == 0:
|
| 42 |
return get_error_data_result(f"The dataset {kb_id} doesn't own parsed file")
|
| 43 |
kbs = KnowledgebaseService.get_by_ids(ids)
|
| 44 |
+
embd_count = list(set([kb.embd_id for kb in kbs]))
|
| 45 |
+
if len(embd_count) != 1:
|
| 46 |
return get_result(retmsg='Datasets use different embedding models."',retcode=RetCode.AUTHENTICATION_ERROR)
|
| 47 |
req["kb_ids"] = ids
|
| 48 |
# llm
|
|
|
|
| 167 |
if kb.chunk_num == 0:
|
| 168 |
return get_error_data_result(f"The dataset {kb_id} doesn't own parsed file")
|
| 169 |
kbs = KnowledgebaseService.get_by_ids(ids)
|
| 170 |
+
embd_count=list(set([kb.embd_id for kb in kbs]))
|
| 171 |
+
if len(embd_count) != 1 :
|
| 172 |
return get_result(
|
| 173 |
retmsg='Datasets use different embedding models."',
|
| 174 |
retcode=RetCode.AUTHENTICATION_ERROR)
|
api/apps/sdk/dataset.py
CHANGED
|
@@ -120,7 +120,7 @@ def delete(tenant_id):
|
|
| 120 |
if not KnowledgebaseService.delete_by_id(id):
|
| 121 |
return get_error_data_result(
|
| 122 |
retmsg="Delete dataset error.(Database error)")
|
| 123 |
-
|
| 124 |
|
| 125 |
@manager.route('/datasets/<dataset_id>', methods=['PUT'])
|
| 126 |
@token_required
|
|
|
|
| 120 |
if not KnowledgebaseService.delete_by_id(id):
|
| 121 |
return get_error_data_result(
|
| 122 |
retmsg="Delete dataset error.(Database error)")
|
| 123 |
+
return get_result(retcode=RetCode.SUCCESS)
|
| 124 |
|
| 125 |
@manager.route('/datasets/<dataset_id>', methods=['PUT'])
|
| 126 |
@token_required
|
api/apps/sdk/doc.py
CHANGED
|
@@ -509,9 +509,9 @@ def rm_chunk(tenant_id,dataset_id,document_id):
|
|
| 509 |
if chunk_id not in sres.ids:
|
| 510 |
return get_error_data_result(f"Chunk {chunk_id} not found")
|
| 511 |
if not ELASTICSEARCH.deleteByQuery(
|
| 512 |
-
Q("ids", values=
|
| 513 |
return get_error_data_result(retmsg="Index updating failure")
|
| 514 |
-
deleted_chunk_ids =
|
| 515 |
chunk_number = len(deleted_chunk_ids)
|
| 516 |
DocumentService.decrement_chunk_num(doc.id, doc.kb_id, 1, chunk_number, 0)
|
| 517 |
return get_result()
|
|
|
|
| 509 |
if chunk_id not in sres.ids:
|
| 510 |
return get_error_data_result(f"Chunk {chunk_id} not found")
|
| 511 |
if not ELASTICSEARCH.deleteByQuery(
|
| 512 |
+
Q("ids", values=chunk_list), search.index_name(tenant_id)):
|
| 513 |
return get_error_data_result(retmsg="Index updating failure")
|
| 514 |
+
deleted_chunk_ids = chunk_list
|
| 515 |
chunk_number = len(deleted_chunk_ids)
|
| 516 |
DocumentService.decrement_chunk_num(doc.id, doc.kb_id, 1, chunk_number, 0)
|
| 517 |
return get_result()
|
api/utils/api_utils.py
CHANGED
|
@@ -337,7 +337,7 @@ def valid(permission,valid_permission,language,valid_language,chunk_method,valid
|
|
| 337 |
|
| 338 |
def valid_parameter(parameter,valid_values):
|
| 339 |
if parameter and parameter not in valid_values:
|
| 340 |
-
return get_error_data_result(f"
|
| 341 |
|
| 342 |
def get_parser_config(chunk_method,parser_config):
|
| 343 |
if parser_config:
|
|
@@ -354,6 +354,8 @@ def get_parser_config(chunk_method,parser_config):
|
|
| 354 |
"laws":{"raptor":{"use_raptor":False}},
|
| 355 |
"presentation":{"raptor":{"use_raptor":False}},
|
| 356 |
"one":None,
|
| 357 |
-
"knowledge_graph":{"chunk_token_num":8192,"delimiter":"\\n!?;。;!?","entity_types":["organization","person","location","event","time"]}
|
|
|
|
|
|
|
| 358 |
parser_config=key_mapping[chunk_method]
|
| 359 |
return parser_config
|
|
|
|
| 337 |
|
| 338 |
def valid_parameter(parameter,valid_values):
|
| 339 |
if parameter and parameter not in valid_values:
|
| 340 |
+
return get_error_data_result(f"'{parameter}' is not in {valid_values}")
|
| 341 |
|
| 342 |
def get_parser_config(chunk_method,parser_config):
|
| 343 |
if parser_config:
|
|
|
|
| 354 |
"laws":{"raptor":{"use_raptor":False}},
|
| 355 |
"presentation":{"raptor":{"use_raptor":False}},
|
| 356 |
"one":None,
|
| 357 |
+
"knowledge_graph":{"chunk_token_num":8192,"delimiter":"\\n!?;。;!?","entity_types":["organization","person","location","event","time"]},
|
| 358 |
+
"email":None,
|
| 359 |
+
"picture":None}
|
| 360 |
parser_config=key_mapping[chunk_method]
|
| 361 |
return parser_config
|
sdk/python/hello_ragflow.py
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
-
import
|
| 2 |
|
| 3 |
-
print(
|
|
|
|
| 1 |
+
import ragflow_sdk
|
| 2 |
|
| 3 |
+
print(ragflow_sdk.__version__)
|
sdk/python/poetry.lock
CHANGED
|
@@ -125,6 +125,31 @@ files = [
|
|
| 125 |
{file = "charset_normalizer-3.4.0.tar.gz", hash = "sha256:223217c3d4f82c3ac5e29032b3f1c2eb0fb591b72161f86d93f5719079dae93e"},
|
| 126 |
]
|
| 127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
[[package]]
|
| 129 |
name = "idna"
|
| 130 |
version = "3.10"
|
|
@@ -139,6 +164,65 @@ files = [
|
|
| 139 |
[package.extras]
|
| 140 |
all = ["flake8 (>=7.1.1)", "mypy (>=1.11.2)", "pytest (>=8.3.2)", "ruff (>=0.6.2)"]
|
| 141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
[[package]]
|
| 143 |
name = "requests"
|
| 144 |
version = "2.32.3"
|
|
@@ -160,6 +244,17 @@ urllib3 = ">=1.21.1,<3"
|
|
| 160 |
socks = ["PySocks (>=1.5.6,!=1.5.7)"]
|
| 161 |
use-chardet-on-py3 = ["chardet (>=3.0.2,<6)"]
|
| 162 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
[[package]]
|
| 164 |
name = "urllib3"
|
| 165 |
version = "2.2.3"
|
|
@@ -180,4 +275,4 @@ zstd = ["zstandard (>=0.18.0)"]
|
|
| 180 |
[metadata]
|
| 181 |
lock-version = "2.0"
|
| 182 |
python-versions = "^3.10"
|
| 183 |
-
content-hash = "
|
|
|
|
| 125 |
{file = "charset_normalizer-3.4.0.tar.gz", hash = "sha256:223217c3d4f82c3ac5e29032b3f1c2eb0fb591b72161f86d93f5719079dae93e"},
|
| 126 |
]
|
| 127 |
|
| 128 |
+
[[package]]
|
| 129 |
+
name = "colorama"
|
| 130 |
+
version = "0.4.6"
|
| 131 |
+
description = "Cross-platform colored terminal text."
|
| 132 |
+
optional = false
|
| 133 |
+
python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,>=2.7"
|
| 134 |
+
files = [
|
| 135 |
+
{file = "colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6"},
|
| 136 |
+
{file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"},
|
| 137 |
+
]
|
| 138 |
+
|
| 139 |
+
[[package]]
|
| 140 |
+
name = "exceptiongroup"
|
| 141 |
+
version = "1.2.2"
|
| 142 |
+
description = "Backport of PEP 654 (exception groups)"
|
| 143 |
+
optional = false
|
| 144 |
+
python-versions = ">=3.7"
|
| 145 |
+
files = [
|
| 146 |
+
{file = "exceptiongroup-1.2.2-py3-none-any.whl", hash = "sha256:3111b9d131c238bec2f8f516e123e14ba243563fb135d3fe885990585aa7795b"},
|
| 147 |
+
{file = "exceptiongroup-1.2.2.tar.gz", hash = "sha256:47c2edf7c6738fafb49fd34290706d1a1a2f4d1c6df275526b62cbb4aa5393cc"},
|
| 148 |
+
]
|
| 149 |
+
|
| 150 |
+
[package.extras]
|
| 151 |
+
test = ["pytest (>=6)"]
|
| 152 |
+
|
| 153 |
[[package]]
|
| 154 |
name = "idna"
|
| 155 |
version = "3.10"
|
|
|
|
| 164 |
[package.extras]
|
| 165 |
all = ["flake8 (>=7.1.1)", "mypy (>=1.11.2)", "pytest (>=8.3.2)", "ruff (>=0.6.2)"]
|
| 166 |
|
| 167 |
+
[[package]]
|
| 168 |
+
name = "iniconfig"
|
| 169 |
+
version = "2.0.0"
|
| 170 |
+
description = "brain-dead simple config-ini parsing"
|
| 171 |
+
optional = false
|
| 172 |
+
python-versions = ">=3.7"
|
| 173 |
+
files = [
|
| 174 |
+
{file = "iniconfig-2.0.0-py3-none-any.whl", hash = "sha256:b6a85871a79d2e3b22d2d1b94ac2824226a63c6b741c88f7ae975f18b6778374"},
|
| 175 |
+
{file = "iniconfig-2.0.0.tar.gz", hash = "sha256:2d91e135bf72d31a410b17c16da610a82cb55f6b0477d1a902134b24a455b8b3"},
|
| 176 |
+
]
|
| 177 |
+
|
| 178 |
+
[[package]]
|
| 179 |
+
name = "packaging"
|
| 180 |
+
version = "24.1"
|
| 181 |
+
description = "Core utilities for Python packages"
|
| 182 |
+
optional = false
|
| 183 |
+
python-versions = ">=3.8"
|
| 184 |
+
files = [
|
| 185 |
+
{file = "packaging-24.1-py3-none-any.whl", hash = "sha256:5b8f2217dbdbd2f7f384c41c628544e6d52f2d0f53c6d0c3ea61aa5d1d7ff124"},
|
| 186 |
+
{file = "packaging-24.1.tar.gz", hash = "sha256:026ed72c8ed3fcce5bf8950572258698927fd1dbda10a5e981cdf0ac37f4f002"},
|
| 187 |
+
]
|
| 188 |
+
|
| 189 |
+
[[package]]
|
| 190 |
+
name = "pluggy"
|
| 191 |
+
version = "1.5.0"
|
| 192 |
+
description = "plugin and hook calling mechanisms for python"
|
| 193 |
+
optional = false
|
| 194 |
+
python-versions = ">=3.8"
|
| 195 |
+
files = [
|
| 196 |
+
{file = "pluggy-1.5.0-py3-none-any.whl", hash = "sha256:44e1ad92c8ca002de6377e165f3e0f1be63266ab4d554740532335b9d75ea669"},
|
| 197 |
+
{file = "pluggy-1.5.0.tar.gz", hash = "sha256:2cffa88e94fdc978c4c574f15f9e59b7f4201d439195c3715ca9e2486f1d0cf1"},
|
| 198 |
+
]
|
| 199 |
+
|
| 200 |
+
[package.extras]
|
| 201 |
+
dev = ["pre-commit", "tox"]
|
| 202 |
+
testing = ["pytest", "pytest-benchmark"]
|
| 203 |
+
|
| 204 |
+
[[package]]
|
| 205 |
+
name = "pytest"
|
| 206 |
+
version = "8.3.3"
|
| 207 |
+
description = "pytest: simple powerful testing with Python"
|
| 208 |
+
optional = false
|
| 209 |
+
python-versions = ">=3.8"
|
| 210 |
+
files = [
|
| 211 |
+
{file = "pytest-8.3.3-py3-none-any.whl", hash = "sha256:a6853c7375b2663155079443d2e45de913a911a11d669df02a50814944db57b2"},
|
| 212 |
+
{file = "pytest-8.3.3.tar.gz", hash = "sha256:70b98107bd648308a7952b06e6ca9a50bc660be218d53c257cc1fc94fda10181"},
|
| 213 |
+
]
|
| 214 |
+
|
| 215 |
+
[package.dependencies]
|
| 216 |
+
colorama = {version = "*", markers = "sys_platform == \"win32\""}
|
| 217 |
+
exceptiongroup = {version = ">=1.0.0rc8", markers = "python_version < \"3.11\""}
|
| 218 |
+
iniconfig = "*"
|
| 219 |
+
packaging = "*"
|
| 220 |
+
pluggy = ">=1.5,<2"
|
| 221 |
+
tomli = {version = ">=1", markers = "python_version < \"3.11\""}
|
| 222 |
+
|
| 223 |
+
[package.extras]
|
| 224 |
+
dev = ["argcomplete", "attrs (>=19.2)", "hypothesis (>=3.56)", "mock", "pygments (>=2.7.2)", "requests", "setuptools", "xmlschema"]
|
| 225 |
+
|
| 226 |
[[package]]
|
| 227 |
name = "requests"
|
| 228 |
version = "2.32.3"
|
|
|
|
| 244 |
socks = ["PySocks (>=1.5.6,!=1.5.7)"]
|
| 245 |
use-chardet-on-py3 = ["chardet (>=3.0.2,<6)"]
|
| 246 |
|
| 247 |
+
[[package]]
|
| 248 |
+
name = "tomli"
|
| 249 |
+
version = "2.0.2"
|
| 250 |
+
description = "A lil' TOML parser"
|
| 251 |
+
optional = false
|
| 252 |
+
python-versions = ">=3.8"
|
| 253 |
+
files = [
|
| 254 |
+
{file = "tomli-2.0.2-py3-none-any.whl", hash = "sha256:2ebe24485c53d303f690b0ec092806a085f07af5a5aa1464f3931eec36caaa38"},
|
| 255 |
+
{file = "tomli-2.0.2.tar.gz", hash = "sha256:d46d457a85337051c36524bc5349dd91b1877838e2979ac5ced3e710ed8a60ed"},
|
| 256 |
+
]
|
| 257 |
+
|
| 258 |
[[package]]
|
| 259 |
name = "urllib3"
|
| 260 |
version = "2.2.3"
|
|
|
|
| 275 |
[metadata]
|
| 276 |
lock-version = "2.0"
|
| 277 |
python-versions = "^3.10"
|
| 278 |
+
content-hash = "202bfd3e121f1d57a2f9c9d91cd7a50eacf2362cd1995c9f6347bcb100cf9336"
|
sdk/python/pyproject.toml
CHANGED
|
@@ -10,6 +10,7 @@ package-mode = true
|
|
| 10 |
[tool.poetry.dependencies]
|
| 11 |
python = "^3.10"
|
| 12 |
requests = "^2.30.0"
|
|
|
|
| 13 |
|
| 14 |
|
| 15 |
[build-system]
|
|
|
|
| 10 |
[tool.poetry.dependencies]
|
| 11 |
python = "^3.10"
|
| 12 |
requests = "^2.30.0"
|
| 13 |
+
pytest = "^8.0.0"
|
| 14 |
|
| 15 |
|
| 16 |
[build-system]
|
sdk/python/test/conftest.py
CHANGED
|
@@ -1,11 +1,11 @@
|
|
| 1 |
-
import pytest
|
| 2 |
-
import requests
|
| 3 |
import string
|
| 4 |
import random
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
|
| 7 |
-
|
| 8 |
-
HOST_ADDRESS = 'http://127.0.0.1:9380'
|
| 9 |
|
| 10 |
def generate_random_email():
|
| 11 |
return 'user_' + ''.join(random.choices(string.ascii_lowercase + string.digits, k=8))+'@1.com'
|
|
|
|
|
|
|
|
|
|
| 1 |
import string
|
| 2 |
import random
|
| 3 |
+
import os
|
| 4 |
+
import pytest
|
| 5 |
+
import requests
|
| 6 |
|
| 7 |
|
| 8 |
+
HOST_ADDRESS = os.getenv('HOST_ADDRESS', 'http://127.0.0.1:9380')
|
|
|
|
| 9 |
|
| 10 |
def generate_random_email():
|
| 11 |
return 'user_' + ''.join(random.choices(string.ascii_lowercase + string.digits, k=8))+'@1.com'
|
sdk/python/test/t_chat.py
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
|
|
| 1 |
from ragflow_sdk import RAGFlow
|
| 2 |
-
|
|
|
|
| 3 |
|
| 4 |
def test_create_chat_with_name(get_api_key_fixture):
|
| 5 |
API_KEY = get_api_key_fixture
|
| 6 |
rag = RAGFlow(API_KEY, HOST_ADDRESS)
|
| 7 |
kb = rag.create_dataset(name="test_create_chat")
|
| 8 |
displayed_name = "ragflow.txt"
|
| 9 |
-
with open("
|
| 10 |
blob = file.read()
|
| 11 |
document = {"displayed_name":displayed_name,"blob":blob}
|
| 12 |
documents = []
|
|
@@ -22,7 +24,7 @@ def test_update_chat_with_name(get_api_key_fixture):
|
|
| 22 |
rag = RAGFlow(API_KEY, HOST_ADDRESS)
|
| 23 |
kb = rag.create_dataset(name="test_update_chat")
|
| 24 |
displayed_name = "ragflow.txt"
|
| 25 |
-
with open("
|
| 26 |
blob = file.read()
|
| 27 |
document = {"displayed_name": displayed_name, "blob": blob}
|
| 28 |
documents = []
|
|
@@ -39,7 +41,7 @@ def test_delete_chats_with_success(get_api_key_fixture):
|
|
| 39 |
rag = RAGFlow(API_KEY, HOST_ADDRESS)
|
| 40 |
kb = rag.create_dataset(name="test_delete_chat")
|
| 41 |
displayed_name = "ragflow.txt"
|
| 42 |
-
with open("
|
| 43 |
blob = file.read()
|
| 44 |
document = {"displayed_name": displayed_name, "blob": blob}
|
| 45 |
documents = []
|
|
@@ -53,9 +55,9 @@ def test_delete_chats_with_success(get_api_key_fixture):
|
|
| 53 |
def test_list_chats_with_success(get_api_key_fixture):
|
| 54 |
API_KEY = get_api_key_fixture
|
| 55 |
rag = RAGFlow(API_KEY, HOST_ADDRESS)
|
| 56 |
-
kb = rag.create_dataset(name="
|
| 57 |
displayed_name = "ragflow.txt"
|
| 58 |
-
with open("
|
| 59 |
blob = file.read()
|
| 60 |
document = {"displayed_name": displayed_name, "blob": blob}
|
| 61 |
documents = []
|
|
|
|
| 1 |
+
import os
|
| 2 |
from ragflow_sdk import RAGFlow
|
| 3 |
+
|
| 4 |
+
HOST_ADDRESS = os.getenv('HOST_ADDRESS', 'http://127.0.0.1:9380')
|
| 5 |
|
| 6 |
def test_create_chat_with_name(get_api_key_fixture):
|
| 7 |
API_KEY = get_api_key_fixture
|
| 8 |
rag = RAGFlow(API_KEY, HOST_ADDRESS)
|
| 9 |
kb = rag.create_dataset(name="test_create_chat")
|
| 10 |
displayed_name = "ragflow.txt"
|
| 11 |
+
with open("ragflow.txt", "rb") as file:
|
| 12 |
blob = file.read()
|
| 13 |
document = {"displayed_name":displayed_name,"blob":blob}
|
| 14 |
documents = []
|
|
|
|
| 24 |
rag = RAGFlow(API_KEY, HOST_ADDRESS)
|
| 25 |
kb = rag.create_dataset(name="test_update_chat")
|
| 26 |
displayed_name = "ragflow.txt"
|
| 27 |
+
with open("ragflow.txt", "rb") as file:
|
| 28 |
blob = file.read()
|
| 29 |
document = {"displayed_name": displayed_name, "blob": blob}
|
| 30 |
documents = []
|
|
|
|
| 41 |
rag = RAGFlow(API_KEY, HOST_ADDRESS)
|
| 42 |
kb = rag.create_dataset(name="test_delete_chat")
|
| 43 |
displayed_name = "ragflow.txt"
|
| 44 |
+
with open("ragflow.txt", "rb") as file:
|
| 45 |
blob = file.read()
|
| 46 |
document = {"displayed_name": displayed_name, "blob": blob}
|
| 47 |
documents = []
|
|
|
|
| 55 |
def test_list_chats_with_success(get_api_key_fixture):
|
| 56 |
API_KEY = get_api_key_fixture
|
| 57 |
rag = RAGFlow(API_KEY, HOST_ADDRESS)
|
| 58 |
+
kb = rag.create_dataset(name="test_list_chats")
|
| 59 |
displayed_name = "ragflow.txt"
|
| 60 |
+
with open("ragflow.txt", "rb") as file:
|
| 61 |
blob = file.read()
|
| 62 |
document = {"displayed_name": displayed_name, "blob": blob}
|
| 63 |
documents = []
|
sdk/python/test/t_dataset.py
CHANGED
|
@@ -1,54 +1,55 @@
|
|
| 1 |
-
|
| 2 |
-
import random
|
| 3 |
-
import pytest
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
rag
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
ds.
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
rag.
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
rag
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import random
|
| 3 |
+
import pytest
|
| 4 |
+
from ragflow_sdk import RAGFlow
|
| 5 |
+
|
| 6 |
+
HOST_ADDRESS = os.getenv('HOST_ADDRESS', 'http://127.0.0.1:9380')
|
| 7 |
+
|
| 8 |
+
def test_create_dataset_with_name(get_api_key_fixture):
|
| 9 |
+
API_KEY = get_api_key_fixture
|
| 10 |
+
rag = RAGFlow(API_KEY, HOST_ADDRESS)
|
| 11 |
+
rag.create_dataset("test_create_dataset_with_name")
|
| 12 |
+
|
| 13 |
+
def test_create_dataset_with_duplicated_name(get_api_key_fixture):
|
| 14 |
+
API_KEY = get_api_key_fixture
|
| 15 |
+
rag = RAGFlow(API_KEY, HOST_ADDRESS)
|
| 16 |
+
with pytest.raises(Exception) as exc_info:
|
| 17 |
+
rag.create_dataset("test_create_dataset_with_name")
|
| 18 |
+
assert str(exc_info.value) == "Duplicated dataset name in creating dataset."
|
| 19 |
+
|
| 20 |
+
def test_create_dataset_with_random_chunk_method(get_api_key_fixture):
|
| 21 |
+
API_KEY = get_api_key_fixture
|
| 22 |
+
rag = RAGFlow(API_KEY, HOST_ADDRESS)
|
| 23 |
+
valid_chunk_methods = ["naive","manual","qa","table","paper","book","laws","presentation","picture","one","knowledge_graph","email"]
|
| 24 |
+
random_chunk_method = random.choice(valid_chunk_methods)
|
| 25 |
+
rag.create_dataset("test_create_dataset_with_random_chunk_method",chunk_method=random_chunk_method)
|
| 26 |
+
|
| 27 |
+
def test_create_dataset_with_invalid_parameter(get_api_key_fixture):
|
| 28 |
+
API_KEY = get_api_key_fixture
|
| 29 |
+
rag = RAGFlow(API_KEY, HOST_ADDRESS)
|
| 30 |
+
valid_chunk_methods = ["naive", "manual", "qa", "table", "paper", "book", "laws", "presentation", "picture", "one",
|
| 31 |
+
"knowledge_graph", "email"]
|
| 32 |
+
chunk_method = "invalid_chunk_method"
|
| 33 |
+
with pytest.raises(Exception) as exc_info:
|
| 34 |
+
rag.create_dataset("test_create_dataset_with_name",chunk_method=chunk_method)
|
| 35 |
+
assert str(exc_info.value) == f"'{chunk_method}' is not in {valid_chunk_methods}"
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def test_update_dataset_with_name(get_api_key_fixture):
|
| 39 |
+
API_KEY = get_api_key_fixture
|
| 40 |
+
rag = RAGFlow(API_KEY, HOST_ADDRESS)
|
| 41 |
+
ds = rag.create_dataset("test_update_dataset")
|
| 42 |
+
ds.update({"name": "updated_dataset"})
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def test_delete_datasets_with_success(get_api_key_fixture):
|
| 46 |
+
API_KEY = get_api_key_fixture
|
| 47 |
+
rag = RAGFlow(API_KEY, HOST_ADDRESS)
|
| 48 |
+
ds = rag.create_dataset("MA")
|
| 49 |
+
rag.delete_datasets(ids=[ds.id])
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def test_list_datasets_with_success(get_api_key_fixture):
|
| 53 |
+
API_KEY = get_api_key_fixture
|
| 54 |
+
rag = RAGFlow(API_KEY, HOST_ADDRESS)
|
| 55 |
+
rag.list_datasets()
|
sdk/python/test/t_document.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
|
|
| 1 |
from ragflow_sdk import RAGFlow, DataSet, Document, Chunk
|
| 2 |
|
| 3 |
-
HOST_ADDRESS = 'http://127.0.0.1:9380'
|
| 4 |
|
| 5 |
|
| 6 |
def test_upload_document_with_success(get_api_key_fixture):
|
|
@@ -66,7 +67,7 @@ def test_download_document_with_success(get_api_key_fixture):
|
|
| 66 |
# Check if the retrieved document is of type Document
|
| 67 |
if isinstance(doc, Document):
|
| 68 |
# Download the document content and save it to a file
|
| 69 |
-
with open("
|
| 70 |
file.write(doc.download())
|
| 71 |
# Print the document object for debugging
|
| 72 |
print(doc)
|
|
@@ -144,7 +145,7 @@ def test_parse_and_cancel_document(get_api_key_fixture):
|
|
| 144 |
|
| 145 |
# Define the document name and path
|
| 146 |
name3 = 'westworld.pdf'
|
| 147 |
-
path = '
|
| 148 |
|
| 149 |
# Create a document in the dataset using the file path
|
| 150 |
ds.upload_documents({"name": name3, "blob": open(path, "rb").read()})
|
|
|
|
| 1 |
+
import os
|
| 2 |
from ragflow_sdk import RAGFlow, DataSet, Document, Chunk
|
| 3 |
|
| 4 |
+
HOST_ADDRESS = os.getenv('HOST_ADDRESS', 'http://127.0.0.1:9380')
|
| 5 |
|
| 6 |
|
| 7 |
def test_upload_document_with_success(get_api_key_fixture):
|
|
|
|
| 67 |
# Check if the retrieved document is of type Document
|
| 68 |
if isinstance(doc, Document):
|
| 69 |
# Download the document content and save it to a file
|
| 70 |
+
with open("ragflow.txt", "wb+") as file:
|
| 71 |
file.write(doc.download())
|
| 72 |
# Print the document object for debugging
|
| 73 |
print(doc)
|
|
|
|
| 145 |
|
| 146 |
# Define the document name and path
|
| 147 |
name3 = 'westworld.pdf'
|
| 148 |
+
path = 'test_data/westworld.pdf'
|
| 149 |
|
| 150 |
# Create a document in the dataset using the file path
|
| 151 |
ds.upload_documents({"name": name3, "blob": open(path, "rb").read()})
|
sdk/python/test/t_session.py
CHANGED
|
@@ -1,94 +1,96 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
assistant.
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
assistant.
|
|
|
|
|
|
|
| 94 |
assistant.list_sessions()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from ragflow_sdk import RAGFlow
|
| 3 |
+
|
| 4 |
+
HOST_ADDRESS = os.getenv('HOST_ADDRESS', 'http://127.0.0.1:9380')
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def test_create_session_with_success(get_api_key_fixture):
|
| 8 |
+
API_KEY = get_api_key_fixture
|
| 9 |
+
rag = RAGFlow(API_KEY, HOST_ADDRESS)
|
| 10 |
+
kb = rag.create_dataset(name="test_create_session")
|
| 11 |
+
displayed_name = "ragflow.txt"
|
| 12 |
+
with open("ragflow.txt", "rb") as file:
|
| 13 |
+
blob = file.read()
|
| 14 |
+
document = {"displayed_name":displayed_name,"blob":blob}
|
| 15 |
+
documents = []
|
| 16 |
+
documents.append(document)
|
| 17 |
+
docs= kb.upload_documents(documents)
|
| 18 |
+
for doc in docs:
|
| 19 |
+
doc.add_chunk("This is a test to add chunk")
|
| 20 |
+
assistant=rag.create_chat("test_create_session", dataset_ids=[kb.id])
|
| 21 |
+
assistant.create_session()
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def test_create_conversation_with_success(get_api_key_fixture):
|
| 25 |
+
API_KEY = get_api_key_fixture
|
| 26 |
+
rag = RAGFlow(API_KEY, HOST_ADDRESS)
|
| 27 |
+
kb = rag.create_dataset(name="test_create_conversation")
|
| 28 |
+
displayed_name = "ragflow.txt"
|
| 29 |
+
with open("ragflow.txt", "rb") as file:
|
| 30 |
+
blob = file.read()
|
| 31 |
+
document = {"displayed_name": displayed_name, "blob": blob}
|
| 32 |
+
documents = []
|
| 33 |
+
documents.append(document)
|
| 34 |
+
docs = kb.upload_documents(documents)
|
| 35 |
+
for doc in docs:
|
| 36 |
+
doc.add_chunk("This is a test to add chunk")
|
| 37 |
+
assistant = rag.create_chat("test_create_conversation", dataset_ids=[kb.id])
|
| 38 |
+
session = assistant.create_session()
|
| 39 |
+
question = "What is AI"
|
| 40 |
+
for ans in session.ask(question, stream=True):
|
| 41 |
+
pass
|
| 42 |
+
assert not ans.content.startswith("**ERROR**"), "Please check this error."
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def test_delete_sessions_with_success(get_api_key_fixture):
|
| 46 |
+
API_KEY = get_api_key_fixture
|
| 47 |
+
rag = RAGFlow(API_KEY, HOST_ADDRESS)
|
| 48 |
+
kb = rag.create_dataset(name="test_delete_session")
|
| 49 |
+
displayed_name = "ragflow.txt"
|
| 50 |
+
with open("ragflow.txt", "rb") as file:
|
| 51 |
+
blob = file.read()
|
| 52 |
+
document = {"displayed_name":displayed_name,"blob":blob}
|
| 53 |
+
documents = []
|
| 54 |
+
documents.append(document)
|
| 55 |
+
docs= kb.upload_documents(documents)
|
| 56 |
+
for doc in docs:
|
| 57 |
+
doc.add_chunk("This is a test to add chunk")
|
| 58 |
+
assistant=rag.create_chat("test_delete_session", dataset_ids=[kb.id])
|
| 59 |
+
session = assistant.create_session()
|
| 60 |
+
assistant.delete_sessions(ids=[session.id])
|
| 61 |
+
|
| 62 |
+
def test_update_session_with_name(get_api_key_fixture):
|
| 63 |
+
API_KEY = get_api_key_fixture
|
| 64 |
+
rag = RAGFlow(API_KEY, HOST_ADDRESS)
|
| 65 |
+
kb = rag.create_dataset(name="test_update_session")
|
| 66 |
+
displayed_name = "ragflow.txt"
|
| 67 |
+
with open("ragflow.txt", "rb") as file:
|
| 68 |
+
blob = file.read()
|
| 69 |
+
document = {"displayed_name": displayed_name, "blob": blob}
|
| 70 |
+
documents = []
|
| 71 |
+
documents.append(document)
|
| 72 |
+
docs = kb.upload_documents(documents)
|
| 73 |
+
for doc in docs:
|
| 74 |
+
doc.add_chunk("This is a test to add chunk")
|
| 75 |
+
assistant = rag.create_chat("test_update_session", dataset_ids=[kb.id])
|
| 76 |
+
session = assistant.create_session(name="old session")
|
| 77 |
+
session.update({"name": "new session"})
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def test_list_sessions_with_success(get_api_key_fixture):
|
| 81 |
+
API_KEY = get_api_key_fixture
|
| 82 |
+
rag = RAGFlow(API_KEY, HOST_ADDRESS)
|
| 83 |
+
kb = rag.create_dataset(name="test_list_session")
|
| 84 |
+
displayed_name = "ragflow.txt"
|
| 85 |
+
with open("ragflow.txt", "rb") as file:
|
| 86 |
+
blob = file.read()
|
| 87 |
+
document = {"displayed_name":displayed_name,"blob":blob}
|
| 88 |
+
documents = []
|
| 89 |
+
documents.append(document)
|
| 90 |
+
docs= kb.upload_documents(documents)
|
| 91 |
+
for doc in docs:
|
| 92 |
+
doc.add_chunk("This is a test to add chunk")
|
| 93 |
+
assistant=rag.create_chat("test_list_session", dataset_ids=[kb.id])
|
| 94 |
+
assistant.create_session("test_1")
|
| 95 |
+
assistant.create_session("test_2")
|
| 96 |
assistant.list_sessions()
|