Python SDK
The official Python SDK provides a typed interface for IntelliRepo's API. Requires Python 3.9+.
Installation
pip install intellirepoQuick Start
from intellirepo import IntelliRepo
client = IntelliRepo(api_key="rh_live_xxx")
# Ask a question
response = client.chat.ask(question="What is the refund policy?")
print(response.answer)
print("Sources:", response.sources)Configuration
client = IntelliRepo(
# Required
api_key="rh_live_xxx",
# Optional: Custom API URL
base_url="https://api.intellirepo.ai",
# Optional: Request timeout (seconds)
timeout=30.0
)Context Manager
For automatic cleanup:
with IntelliRepo(api_key="rh_live_xxx") as client:
results = client.search.query(query="test")
# Client is automatically closedCollections
List Collections
result = client.collections.list()
for collection in result.collections:
print(collection.name, collection.document_count)Create Collection
collection = client.collections.create(
name="Product Docs",
description="All product documentation"
)Get Collection
collection = client.collections.get("collection-uuid")Update Collection
updated = client.collections.update("collection-uuid", name="New Name")Delete Collection
client.collections.delete("collection-uuid")Documents
List Documents
result = client.documents.list(collection_id)
for doc in result.documents:
print(doc.name, doc.status)Upload Document (File Path)
result = client.documents.upload(
collection_id,
"path/to/document.pdf"
)Upload Document (File Object)
with open("document.pdf", "rb") as f:
result = client.documents.upload(
collection_id,
f,
filename="document.pdf"
)Update Document
updated = client.documents.update(
collection_id,
document_id,
tags=["important", "2024"]
)Delete Document
client.documents.delete(collection_id, document_id)Chat
Basic Chat
response = client.chat.ask(question="What is the refund policy?")
print(response.answer)Chat in Collection
response = client.chat.ask(
question="What is the refund policy?",
collection_id="collection-uuid"
)With Options
response = client.chat.ask(
question="What is the refund policy?",
collection_id="collection-uuid",
top_k=5, # Number of sources to retrieve
tags=["current"] # Filter by document tags
)Streaming Chat
For real-time responses:
for event in client.chat.stream(question="Explain the process..."):
if event.type == "sources":
print(f"Found {len(event.sources)} sources")
elif event.type == "chunk":
print(event.text, end="", flush=True)
elif event.type == "done":
print(f"\nTokens: {event.usage.input_tokens + event.usage.output_tokens}")
elif event.type == "error":
print(f"Error: {event.message}")Search
Basic Search
results = client.search.query(query="password reset")
for result in results.results:
print(result.document_name, result.similarity)Search in Collection
results = client.search.query(
query="password reset",
collection_id="collection-uuid",
top_k=10
)Error Handling
The SDK provides typed exception classes:
from intellirepo import (
IntelliRepoError,
AuthenticationError,
NotFoundError,
RateLimitError,
ValidationError
)
try:
client.collections.get("invalid-id")
except AuthenticationError:
print("Invalid API key")
except NotFoundError:
print("Collection not found")
except RateLimitError:
print("Rate limit exceeded")
except IntelliRepoError as e:
print(f"API error: {e.message}")| Exception Class | Status Code |
|---|---|
AuthenticationError | 401 |
PermissionError | 403 |
NotFoundError | 404 |
ConflictError | 409 |
ValidationError | 422 |
RateLimitError | 429 |
Async Support
For async applications:
import asyncio
from intellirepo import AsyncIntelliRepo
async def main():
client = AsyncIntelliRepo(api_key="rh_live_xxx")
response = await client.chat.ask(question="What is the refund policy?")
print(response.answer)
await client.close()
asyncio.run(main())Or with context manager:
async with AsyncIntelliRepo(api_key="rh_live_xxx") as client:
response = await client.chat.ask(question="What is the refund policy?")Type Hints
The SDK is fully typed with Pydantic models:
from intellirepo.types import Collection, Document, ChatResponse
def process_response(response: ChatResponse) -> None:
print(response.answer)Flask Example
from flask import Flask, request, jsonify
from intellirepo import IntelliRepo
app = Flask(__name__)
client = IntelliRepo(api_key="rh_live_xxx")
@app.route("/ask", methods=["POST"])
def ask():
question = request.json.get("question")
response = client.chat.ask(question=question)
return jsonify({
"answer": response.answer,
"sources": [s.document_name for s in response.sources]
})Requirements
- Python 3.9+
- httpx
- pydantic
These are installed automatically with pip install intellirepo.
Related Articles
Need Help?
Contact our support team if you need help with the Python SDK.