chore: add project config and usage examples
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
parent
5a4e9a6f12
commit
e641fe2e3c
3 changed files with 158 additions and 0 deletions
10
.gitignore
vendored
Normal file
10
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
# Python-generated files
|
||||||
|
__pycache__/
|
||||||
|
*.py[oc]
|
||||||
|
build/
|
||||||
|
dist/
|
||||||
|
wheels/
|
||||||
|
*.egg-info
|
||||||
|
|
||||||
|
# Virtual environments
|
||||||
|
.venv
|
||||||
102
examples.py
Normal file
102
examples.py
Normal file
|
|
@ -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
|
||||||
46
pyproject.toml
Normal file
46
pyproject.toml
Normal file
|
|
@ -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",
|
||||||
|
]
|
||||||
Loading…
Reference in a new issue