Skip to content

Commit c0ce93a

Browse files
0xneobyteclaude
andcommitted
fix: update examples to use automatic env var support
Replace BrainusAI(api_key=os.getenv("BRAINUS_API_KEY")) with BrainusAI() across async_patterns.py, batch_processing.py, error_handling.py, fastapi_example.py, and flask_example.py. Remove unused import os. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent eb881f1 commit c0ce93a

5 files changed

Lines changed: 12 additions & 17 deletions

File tree

examples/async_patterns.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
import asyncio
1515
from brainus_ai import BrainusAI
16-
import os
1716
from typing import List, Dict
1817

1918

@@ -28,7 +27,7 @@ async def query_single(query: str, store_id: str = "default") -> Dict:
2827
Returns:
2928
Dict with query and result
3029
"""
31-
async with BrainusAI(api_key=os.getenv("BRAINUS_API_KEY")) as client:
30+
async with BrainusAI() as client:
3231
result = await client.query(query=query, store_id=store_id)
3332
return {
3433
'query': query,
@@ -51,7 +50,7 @@ async def query_multiple(queries: List[str], store_id: str = "default") -> List[
5150
Returns:
5251
List of results
5352
"""
54-
async with BrainusAI(api_key=os.getenv("BRAINUS_API_KEY")) as client:
53+
async with BrainusAI() as client:
5554
tasks = [
5655
client.query(query=q, store_id=store_id)
5756
for q in queries
@@ -103,7 +102,7 @@ async def query_with_fallback(query: str, fallback_stores: List[str]) -> Dict:
103102
Returns:
104103
Result from first successful store
105104
"""
106-
async with BrainusAI(api_key=os.getenv("BRAINUS_API_KEY")) as client:
105+
async with BrainusAI() as client:
107106
for store_id in fallback_stores:
108107
try:
109108
result = await client.query(query=query, store_id=store_id)

examples/batch_processing.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import pandas as pd
1717
from typing import List, Dict
1818
import asyncio
19-
import os
2019
from datetime import datetime
2120

2221

@@ -41,7 +40,7 @@ async def process_batch(
4140
results = []
4241
total_batches = (len(queries) + batch_size - 1) // batch_size
4342

44-
async with BrainusAI(api_key=os.getenv("BRAINUS_API_KEY")) as client:
43+
async with BrainusAI() as client:
4544
for batch_num, i in enumerate(range(0, len(queries), batch_size), 1):
4645
batch = queries[i:i + batch_size]
4746

@@ -150,7 +149,7 @@ async def process_with_progress(queries: List[str]) -> List[Dict]:
150149
"""
151150
results = []
152151

153-
async with BrainusAI(api_key=os.getenv("BRAINUS_API_KEY")) as client:
152+
async with BrainusAI() as client:
154153
for i, query in enumerate(queries, 1):
155154
try:
156155
print(f"[{i}/{len(queries)}] Processing: {query[:50]}...")

examples/error_handling.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
APIError
2121
)
2222
import asyncio
23-
import os
2423
from typing import Optional
2524

2625

@@ -35,7 +34,7 @@ async def basic_error_handling(query: str) -> Optional[dict]:
3534
Result dict or None on error
3635
"""
3736
try:
38-
async with BrainusAI(api_key=os.getenv("BRAINUS_API_KEY")) as client:
37+
async with BrainusAI() as client:
3938
result = await client.query(query=query, store_id="default")
4039
return {
4140
'answer': result.answer,
@@ -57,7 +56,7 @@ async def robust_query(query: str, max_retries: int = 3) -> Optional[dict]:
5756
Returns:
5857
Result dict or None on permanent failure
5958
"""
60-
async with BrainusAI(api_key=os.getenv("BRAINUS_API_KEY")) as client:
59+
async with BrainusAI() as client:
6160
for attempt in range(max_retries):
6261
try:
6362
result = await client.query(query=query, store_id="default")
@@ -138,7 +137,7 @@ async def query_with_fallback(query: str, fallback_answer: str = "Unable to proc
138137
Result dict with answer (from API or fallback)
139138
"""
140139
try:
141-
async with BrainusAI(api_key=os.getenv("BRAINUS_API_KEY")) as client:
140+
async with BrainusAI() as client:
142141
result = await client.query(query=query, store_id="default")
143142
return {
144143
'answer': result.answer,
@@ -184,7 +183,7 @@ async def validate_and_query(query: str) -> Optional[dict]:
184183

185184
# Query with error handling
186185
try:
187-
async with BrainusAI(api_key=os.getenv("BRAINUS_API_KEY")) as client:
186+
async with BrainusAI() as client:
188187
result = await client.query(query=query, store_id="default")
189188
return {
190189
'answer': result.answer,
@@ -210,7 +209,7 @@ async def batch_with_error_handling(queries: list[str]) -> list[dict]:
210209
"""
211210
results = []
212211

213-
async with BrainusAI(api_key=os.getenv("BRAINUS_API_KEY")) as client:
212+
async with BrainusAI() as client:
214213
for i, query in enumerate(queries, 1):
215214
print(f"\n[{i}/{len(queries)}] Processing: {query[:50]}...")
216215

examples/fastapi_example.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from pydantic import BaseModel
2121
from typing import Optional, Dict, List
2222
from brainus_ai import BrainusAI, BrainusError
23-
import os
2423
from datetime import datetime
2524

2625
app = FastAPI(
@@ -89,7 +88,7 @@ async def query_endpoint(
8988
HTTPException: On API errors
9089
"""
9190
try:
92-
async with BrainusAI(api_key=os.getenv("BRAINUS_API_KEY")) as client:
91+
async with BrainusAI() as client:
9392
result = await client.query(
9493
query=request.query,
9594
store_id=request.store_id,

examples/flask_example.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
from flask import Flask, request, jsonify
1616
from brainus_ai import BrainusAI, RateLimitError
17-
import os
1817
import asyncio
1918

2019
app = Flask(__name__)
@@ -35,7 +34,7 @@ async def query_with_retry(query, store_id="default", max_retries=3):
3534
Raises:
3635
RateLimitError: If max retries exceeded
3736
"""
38-
async with BrainusAI(api_key=os.getenv("BRAINUS_API_KEY")) as client:
37+
async with BrainusAI() as client:
3938
for attempt in range(max_retries):
4039
try:
4140
return await client.query(query=query, store_id=store_id)

0 commit comments

Comments
 (0)