Comprehend-it
Zero-shot and few-shot text classification with confidence scores using the Knowledgator Python SDK.
Example Usage
from knowledgator import Knowledgator
kg = Knowledgator(api_key="YOUR_API_KEY")
# 1. Using the default inference model with Unified Inference tasks
result = kg.comprehend.it(
text="I love this product! It exceeded my expectations.",
tasks={
"sentiment": {
"type": "classify",
"labels": ["positive", "negative", "neutral"],
"threshold": 0.5,
}
},
)
print(result.outputs["sentiment"].scores)
# 2. Using a fine-tuned LoRA adapter
result = kg.comprehend.it(
text="I love this product! It exceeded my expectations.",
tasks={
"sentiment": {
"type": "classify",
"labels": ["positive", "negative", "neutral"],
"threshold": 0.5,
}
},
adapter_id="your-fine-tuned-adapter-id", # Optional LoRA adapter
)
print(result.outputs["sentiment"].scores)
LoRA Adapters vs. Open Models
adapter_id is for fine-tuned LoRA adapters attached to the base Comprehend-it service. If you trained or deployed a standalone Open Model (e.g. GLiClass or GLiNER checkpoint), query it via kg.model(model_id).predict(...) instead.
Async Example
import asyncio
from knowledgator import Knowledgator
async def main():
kg = Knowledgator(api_key="YOUR_API_KEY")
result = await kg.comprehend.it_async(
text="Kyiv is an ancient capital",
tasks={
"topic": {
"type": "classify",
"labels": ["history", "geography"],
}
},
)
print(result.outputs["topic"].scores)
asyncio.run(main())
Expected Output
{
"positive": 0.942,
"neutral": 0.045,
"negative": 0.013
}
Result Object Structure
result.outputs contains task outputs indexed by your defined task names:
# Accessing scores programmatically
sentiment_task = result.outputs["sentiment"]
for label, score in sentiment_task.scores.items():
print(f"{label}: {score:.3f}")