Python SDK
Official Python SDK for integrating Bleu.js into your Python applications
Installation
pip install bleujs
The Python SDK is available on PyPI and can be installed using pip. Requires Python 3.8 or higher.
Quick Start
from bleujs import BleuClient
client = BleuClient(api_key="your-api-key-here")
# Generate text
response = client.generate(
prompt="Hello, world!",
max_tokens=100
)
print(response.text)
Configuration
API Key
Your API key is required for authentication. You can get one from your dashboard.
Environment Variables
You can also set your API key as an environment variable: BLEUJS_API_KEY
Base URL
By default, the SDK uses the production API endpoint. You can override this for testing or custom deployments.
Core Methods
generate()
Generate text completions using the Bleu.js AI model.
client.generate(
prompt: str,
max_tokens: Optional[int] = None,
temperature: Optional[float] = None,
top_p: Optional[float] = None
)
embed()
Generate embeddings for text input.
client.embed(
input: Union[str, List[str]]
)
chat()
Have a conversation with the AI model.
client.chat(
messages: List[Dict[str, str]],
max_tokens: Optional[int] = None,
temperature: Optional[float] = None
)
Error Handling
from bleujs import BleuError
try:
response = client.generate(prompt="Hello, world!")
except BleuError as e:
if e.code == "RATE_LIMIT_EXCEEDED":
# Handle rate limiting
pass
elif e.code == "INVALID_API_KEY":
# Handle authentication errors
pass
Examples
Text Generation
response = client.generate(
prompt="Write a short story about a robot learning to paint.",
max_tokens=200,
temperature=0.7
)
print(response.text)
Chat Conversation
messages = [
{"role": "user", "content": "What is the capital of France?"}
]
response = client.chat(
messages=messages,
max_tokens=100
)
print(response.message.content)
Text Embeddings
embeddings = client.embed(
input=["Hello world", "Goodbye world"]
)
print(embeddings.data[0].embedding)
Async Usage
import asyncio
from bleujs import AsyncBleuClient
async def main():
client = AsyncBleuClient(api_key="your-api-key")
response = await client.generate(prompt="Hello, world!")
print(response.text)
asyncio.run(main())
Advanced Features
Streaming Responses
for chunk in client.generate_stream(
prompt="Write a long story...",
max_tokens=500
):
print(chunk.text, end="", flush=True)
Custom Headers
client = BleuClient(
api_key="your-api-key",
headers={"User-Agent": "MyApp/1.0"}
)
Retry Logic
from tenacity import retry, stop_after_attempt
@retry(stop=stop_after_attempt(3))
def generate_with_retry(client, prompt):
return client.generate(prompt=prompt)
SDK Versions
Current1.1.6
LTS1.1.0
Beta1.2.0-beta
Requirements
Python≥ 3.8
requests≥ 2.25.0
pydantic≥ 1.8.0