From e641fe2e3c0f450661299857d83c5fa7f80cb365 Mon Sep 17 00:00:00 2001 From: mrmamongo Date: Mon, 20 Apr 2026 12:08:31 +0300 Subject: [PATCH] chore: add project config and usage examples Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus --- .gitignore | 10 +++++ examples.py | 102 +++++++++++++++++++++++++++++++++++++++++++++++++ pyproject.toml | 46 ++++++++++++++++++++++ 3 files changed, 158 insertions(+) create mode 100644 .gitignore create mode 100644 examples.py create mode 100644 pyproject.toml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..505a3b1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +# Python-generated files +__pycache__/ +*.py[oc] +build/ +dist/ +wheels/ +*.egg-info + +# Virtual environments +.venv diff --git a/examples.py b/examples.py new file mode 100644 index 0000000..26fefa4 --- /dev/null +++ b/examples.py @@ -0,0 +1,102 @@ +from ergpt.kb import AsyncKnowledgeBaseClient, KnowledgeBaseClient + +API_TOKEN = "your_api_token_here" + + +def sync_example(): + client = KnowledgeBaseClient(api_token=API_TOKEN) + + kb = client.create_knowledge_base( + name="Моя база знаний", + description="Описание базы знаний", + chunk_size=1000, + chunk_overlap=200, + ) + print(f"Создана база знаний: {kb.id}") + + with open("document.pdf", "rb") as f: + doc = client.upload_document(kb_id=kb.id, file=f) + print(f"Загружен документ: {doc.id}, статус: {doc.status}") + + results = client.search( + kb_id=kb.id, + query="Как настроить авторизацию?", + limit=5, + score_threshold=0.2, + ) + for result in results.chunks: + print(f"Документ: {result.filename}") + for chunk in result.chunks: + print(f" [score={chunk.score:.2f}] {chunk.text[:100]}...") + + chunks = client.get_document_chunks(document_id=doc.id, limit=10) + for chunk in chunks.result: + print(f"Чанк {chunk.chunk_id}: {chunk.text[:50]}...") + + client.delete_knowledge_base(kb.id) + print("База знаний удалена") + + client.close() + + +async def async_example(): + async with AsyncKnowledgeBaseClient(api_token=API_TOKEN) as client: + kb = await client.create_knowledge_base( + name="Моя база знаний", + description="Описание базы знаний", + chunk_size=1000, + chunk_overlap=200, + ) + print(f"Создана база знаний: {kb.id}") + + with open("document.pdf", "rb") as f: + doc = await client.upload_document(kb_id=kb.id, file=f) + print(f"Загружен документ: {doc.id}, статус: {doc.status}") + + results = await client.search( + kb_id=kb.id, + query="Как настроить авторизацию?", + limit=5, + score_threshold=0.2, + ) + for result in results.chunks: + print(f"Документ: {result.filename}") + for chunk in result.chunks: + print(f" [score={chunk.score:.2f}] {chunk.text[:100]}...") + + chunks = await client.get_document_chunks(document_id=doc.id, limit=10) + for chunk in chunks.result: + print(f"Чанк {chunk.chunk_id}: {chunk.text[:50]}...") + + await client.delete_knowledge_base(kb.id) + print("База знаний удалена") + + +def list_example(): + with KnowledgeBaseClient(api_token=API_TOKEN) as client: + kbs = client.list_knowledge_bases(search="проект", current=0, page_size=20) + print(f"Всего баз: {kbs.total}") + for kb in kbs.result: + print(f" {kb.name} ({kb.documents_count} документов)") + + docs = client.list_documents( + kb_id="some-kb-id", + statuses=["FAILED"], + current=0, + page_size=10, + ) + for doc in docs.result: + print(f" {doc.filename}: {doc.status}, ошибка: {doc.error}") + + +def bulk_operations_example(): + with KnowledgeBaseClient(api_token=API_TOKEN) as client: + client.delete_documents_bulk(["doc-id-1", "doc-id-2", "doc-id-3"]) + print("Документы удалены") + + client.retry_document("failed-doc-id") + print("Обработка перезапущена") + + +if __name__ == "__main__": + pass diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..5d5cb68 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,46 @@ +[project] +name = "ergpt-kb" +version = "0.1.0" +description = "ErGPT Knowledge Base API SDK" +readme = "README.md" +requires-python = ">=3.12" +dependencies = [ + "httpx>=0.25.0", + "pydantic>=2.0.0", +] + +[project.optional-dependencies] +dev = [ + "pytest>=7.0.0", + "pytest-asyncio>=0.21.0", + "ruff>=0.1.0", +] + +[project.urls] +Homepage = "https://github.com/your-org/ergpt-sdk" +Repository = "https://github.com/your-org/ergpt-sdk" + +[tool.setuptools.packages.find] +where = ["."] +include = ["ergpt*"] + +[tool.ruff] +target-version = "py312" +line-length = 100 + +[tool.ruff.lint] +select = ["E", "F", "W", "I", "N", "UP", "B", "C4", "SIM"] +ignore = ["E501"] + +[tool.ruff.lint.pydocstyle] +convention = "google" + +[tool.ruff.format] +quote-style = "double" +indent-style = "space" + +[dependency-groups] +dev = [ + "build>=1.4.3", + "ruff>=0.15.11", +]