|
| 1 | +"""Basic usage example for the Brainus AI Python SDK.""" |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import os |
| 5 | +from brainus_ai import BrainusAI |
| 6 | + |
| 7 | + |
| 8 | +async def main(): |
| 9 | + # Get API key from environment variable |
| 10 | + api_key = os.getenv("BRAINUS_API_KEY") |
| 11 | + if not api_key: |
| 12 | + print("Error: BRAINUS_API_KEY environment variable not set") |
| 13 | + return |
| 14 | + |
| 15 | + # Initialize the client |
| 16 | + async with BrainusAI(api_key=api_key) as client: |
| 17 | + # Example 1: Query with no filters |
| 18 | + print("Example 1: Simple query") |
| 19 | + print("-" * 50) |
| 20 | + response = await client.query( |
| 21 | + query="What is Object-Oriented Programming?", |
| 22 | + # store_id is optional - uses default if not provided |
| 23 | + ) |
| 24 | + print(f"Answer: {response.answer}\n") |
| 25 | + |
| 26 | + if response.has_citations: |
| 27 | + print("Citations:") |
| 28 | + for citation in response.citations: |
| 29 | + print(f" - {citation.document_name} (Pages: {citation.pages})") |
| 30 | + print() |
| 31 | + |
| 32 | + # Example 2: Query with filters |
| 33 | + print("Example 2: Query with filters") |
| 34 | + print("-" * 50) |
| 35 | + from brainus_ai import QueryFilters |
| 36 | + |
| 37 | + response = await client.query( |
| 38 | + query="Explain inheritance in programming", |
| 39 | + store_id="your_store_id", # Optional |
| 40 | + model="gemini-2.5-flash", # Optional |
| 41 | + filters=QueryFilters(subject="ICT", grade="12"), |
| 42 | + ) |
| 43 | + print(f"Answer: {response.answer[:200]}...\n") |
| 44 | + |
| 45 | + # Example 3: Get usage statistics |
| 46 | + print("Example 3: Usage statistics") |
| 47 | + print("-" * 50) |
| 48 | + stats = await client.get_usage() |
| 49 | + print(f"Total requests: {stats.total_requests}") |
| 50 | + print(f"Quota used: {stats.quota_percentage}%") |
| 51 | + if stats.quota_remaining: |
| 52 | + print(f"Quota remaining: {stats.quota_remaining}") |
| 53 | + print() |
| 54 | + |
| 55 | + # Example 4: Get available plans |
| 56 | + print("Example 4: Available plans") |
| 57 | + print("-" * 50) |
| 58 | + plans = await client.get_plans() |
| 59 | + for plan in plans: |
| 60 | + print(f"{plan.name}:") |
| 61 | + print(f" Rate limit: {plan.rate_limit_per_minute} req/min") |
| 62 | + print(f" Monthly quota: {plan.monthly_quota or 'Unlimited'}") |
| 63 | + print(f" Price: LKR {plan.price_lkr or 0}/month") |
| 64 | + print(f" Allowed models: {', '.join(plan.allowed_models)}") |
| 65 | + print() |
| 66 | + |
| 67 | + |
| 68 | +if __name__ == "__main__": |
| 69 | + asyncio.run(main()) |
| 70 | + |
| 71 | + |
| 72 | + |
0 commit comments