Skip to content

Commit aa426f3

Browse files
authored
Merge pull request #2 from Ontos-AI/chore/wangbinqi/update-readme
chore: update readme
2 parents 239a68c + 9f900a0 commit aa426f3

2 files changed

Lines changed: 47 additions & 133 deletions

File tree

README.md

Lines changed: 46 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,40 @@
11
# Knowhere Python SDK
22

3+
[![PyPI version](https://img.shields.io/pypi/v/knowhere-python-sdk.svg)](https://pypi.org/project/knowhere-python-sdk/)
4+
35
Official Python SDK for the [Knowhere](https://knowhereto.ai) document parsing API.
46

57
## Installation
68

7-
```bash
9+
```sh
810
pip install knowhere-python-sdk
911
```
1012

1113
Or with [uv](https://docs.astral.sh/uv/):
1214

13-
```bash
15+
```sh
1416
uv add knowhere-python-sdk
1517
```
1618

17-
## Quick Start
19+
## Usage
1820

1921
```python
2022
import knowhere
2123

2224
client = knowhere.Knowhere(api_key="sk_...")
2325

24-
# Parse a document from URL
2526
result = client.parse(url="https://example.com/report.pdf")
2627

27-
print(result.statistics.total_chunks) # 152
28-
print(result.full_markdown[:200]) # First 200 chars of full markdown
28+
print(result.statistics.total_chunks)
29+
print(result.full_markdown[:200])
2930

3031
for chunk in result.text_chunks:
3132
print(chunk.content[:80])
3233
```
3334

34-
### Parse a Local File
35+
While you can provide an `api_key` keyword argument, we recommend using [python-dotenv](https://pypi.org/project/python-dotenv/) to add `KNOWHERE_API_KEY="sk_..."` to your `.env` file so that your API key is not stored in source control.
36+
37+
### Parse a local file
3538

3639
```python
3740
from pathlib import Path
@@ -45,7 +48,7 @@ print(result.manifest.source_file_name) # "report.pdf"
4548
print(len(result.chunks)) # 152
4649
```
4750

48-
### Access Different Chunk Types
51+
### Access different chunk types
4952

5053
```python
5154
result = client.parse(url="https://example.com/report.pdf")
@@ -67,14 +70,14 @@ for chunk in result.table_chunks:
6770
print(chunk.html[:100])
6871
```
6972

70-
### Save All Results to Disk
73+
### Save all results to disk
7174

7275
```python
7376
result = client.parse(file=Path("report.pdf"))
7477
result.save("./output/report/")
7578
```
7679

77-
## Async Usage
80+
## Async usage
7881

7982
```python
8083
import asyncio
@@ -91,7 +94,7 @@ async def main():
9194
asyncio.run(main())
9295
```
9396

94-
## Step-by-Step Control
97+
## Step-by-step control
9598

9699
For granular control over the parsing workflow, use the `jobs` resource directly:
97100

@@ -116,6 +119,22 @@ result = client.jobs.load(job_result)
116119
print(result.statistics)
117120
```
118121

122+
## Handling errors
123+
124+
All errors inherit from `knowhere.KnowhereError`.
125+
126+
127+
```python
128+
import knowhere
129+
130+
try:
131+
result = client.parse(url="https://example.com/report.pdf")
132+
except knowhere.AuthenticationError:
133+
print("Invalid API key")
134+
except knowhere.APIStatusError as e:
135+
print(f"{e.status_code}: {e.message}")
136+
```
137+
119138
## Configuration
120139

121140
The SDK reads configuration from constructor arguments, environment variables, or defaults (in that priority order):
@@ -140,143 +159,37 @@ client = knowhere.Knowhere(
140159
)
141160
```
142161

143-
### Context Manager
162+
### Retries
144163

145-
```python
146-
# Sync — ensures httpx.Client is properly closed
147-
with knowhere.Knowhere(api_key="sk_...") as client:
148-
result = client.parse(url="https://example.com/report.pdf")
164+
Connection errors, 429 Rate Limit, and >=500 Internal errors are automatically retried with exponential backoff.
149165

150-
# Async — ensures httpx.AsyncClient is properly closed
151-
async with knowhere.AsyncKnowhere(api_key="sk_...") as client:
152-
result = await client.parse(url="https://example.com/report.pdf")
166+
```python
167+
client = knowhere.Knowhere(
168+
api_key="sk_...",
169+
max_retries=3, # default is 5
170+
)
153171
```
154172

155-
## Error Handling
173+
### Determining the installed version
156174

157175
```python
158-
from knowhere import (
159-
Knowhere,
160-
AuthenticationError,
161-
NotFoundError,
162-
RateLimitError,
163-
BadRequestError,
164-
APIStatusError,
165-
PollingTimeoutError,
166-
)
167-
168-
try:
169-
result = client.parse(url="https://example.com/report.pdf")
170-
except BadRequestError as e:
171-
print(e.status_code) # 400
172-
print(e.code) # "INVALID_ARGUMENT"
173-
print(e.message) # "Unsupported file format"
174-
print(e.request_id) # "req_abc123"
175-
except NotFoundError as e:
176-
print(e.message) # "Job not found"
177-
except RateLimitError as e:
178-
print(e.retry_after) # seconds to wait
179-
except AuthenticationError:
180-
print("Invalid API key")
181-
except PollingTimeoutError:
182-
print("Job did not complete within timeout")
183-
except APIStatusError as e:
184-
print(f"API error {e.status_code}: {e.message}")
176+
import knowhere
177+
print(knowhere.__version__)
185178
```
186179

180+
## Versioning
181+
182+
This package follows [Semantic Versioning](https://semver.org/).
183+
184+
We publish stable releases to [PyPI](https://pypi.org/project/knowhere-python-sdk/). To install the latest unreleased changes directly from the repository: https://github.com/Ontos-AI/knowhere-python-sdk
185+
187186
## Requirements
188187

189188
- Python 3.9+
190189
- [httpx](https://www.python-httpx.org/) `>=0.25.0,<1.0`
191190
- [pydantic](https://docs.pydantic.dev/) `>=2.0.0,<3.0`
192191
- [typing-extensions](https://pypi.org/project/typing-extensions/) `>=4.7.0`
193192

194-
## Building from Source
195-
196-
### Prerequisites
197-
198-
- Python 3.9 or later
199-
- [uv](https://docs.astral.sh/uv/) (recommended) or pip
200-
201-
### Build
202-
203-
```bash
204-
git clone https://github.com/Ontos-AI/knowhere-python-sdk.git
205-
cd knowhere-python-sdk
206-
207-
# Install uv if you don't have it
208-
curl -LsSf https://astral.sh/uv/install.sh | sh
209-
210-
# Build sdist + wheel
211-
uv build
212-
213-
# Install the built wheel
214-
pip install dist/knowhere_python_sdk-*.whl
215-
```
216-
217-
## Development
218-
219-
### Setup
220-
221-
```bash
222-
git clone https://github.com/Ontos-AI/knowhere-python-sdk.git
223-
cd knowhere-python-sdk
224-
225-
# Create venv and install all dependencies (including dev)
226-
uv sync --all-extras
227-
```
228-
229-
### Running Tests
230-
231-
```bash
232-
# Run all unit tests
233-
uv run pytest tests/ -v
234-
235-
# Run with coverage
236-
uv run coverage run -m pytest tests/ -v
237-
uv run coverage report -m
238-
```
239-
240-
### Linting and Type Checking
241-
242-
```bash
243-
# Lint
244-
uv run ruff check src/
245-
246-
# Type check
247-
uv run mypy src/knowhere/
248-
```
249-
250-
### Project Structure
251-
252-
```
253-
knowhere-python-sdk/
254-
├── src/knowhere/
255-
│ ├── __init__.py # Public API surface
256-
│ ├── _client.py # Knowhere + AsyncKnowhere clients
257-
│ ├── _base_client.py # HTTP logic, retry, error parsing
258-
│ ├── _exceptions.py # Exception hierarchy
259-
│ ├── _constants.py # Default URLs, timeouts, env var names
260-
│ ├── _types.py # Sentinel types, callback type aliases
261-
│ ├── _logging.py # Logger setup, header redaction
262-
│ ├── _response.py # APIResponse wrapper
263-
│ ├── _version.py # __version__
264-
│ ├── py.typed # PEP 561 marker
265-
│ ├── types/
266-
│ │ ├── job.py # Job, JobResult, JobError
267-
│ │ ├── result.py # ParseResult, Manifest, Chunk types
268-
│ │ └── params.py # ParsingParams, WebhookConfig
269-
│ ├── resources/
270-
│ │ └── jobs.py # Jobs + AsyncJobs resource
271-
│ └── lib/
272-
│ ├── polling.py # Adaptive polling loop
273-
│ ├── upload.py # Streaming file upload
274-
│ └── result_parser.py # ZIP parsing, checksum verification
275-
├── tests/ # Unit tests (respx-mocked HTTP)
276-
├── examples/ # Usage examples
277-
└── pyproject.toml
278-
```
279-
280193
## License
281194

282195
MIT

release-please-config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"bump-minor-pre-major": true,
77
"bump-patch-for-minor-pre-major": false,
88
"pull-request-title-pattern": "release: ${version}",
9+
"pull-request-header": ":rocket: Release `${version}` is ready for review.\n\nMerge this PR to publish to PyPI.",
910
"packages": {
1011
".": {}
1112
},

0 commit comments

Comments
 (0)