API Reference¶
Complete API reference for the CosmicMind Python SDK.
CosmicMindClient¶
Main client class for interacting with CosmicMind.
from cosmicmind import CosmicMindClient
client = CosmicMindClient(
api_key: str, # Your API key (required)
base_url: str = "http://localhost:8000", # API base URL
api_version: str = "v1" # API version (defaults to v1) - sent in X-API-Version header
)
Note: API versioning is handled via the X-API-Version header, not in request payloads. The api_version parameter sets this header for all requests made by the client.
Methods¶
health()¶
Check API health status.
Chat API¶
Access via client.chat.
send()¶
Send a chat message and get AI response.
Parameters:
The send() method accepts parameters in three different styles:
Style 1: Pydantic Model (Recommended)
from cosmicmind.models import ChatRequest
request = ChatRequest(
messages=["Hello!"], # Required: List of messages
user_id="alice", # Default: "default_user"
llm="cerebras", # Default: "cerebras"
llm_model="llama-3.3-70b", # Default: "llama-3.3-70b"
llm_api_key=None, # Optional: Organization LLM API key
max_tokens=None # Optional: Max tokens in response
)
response = client.chat.send(request)
Style 2: Dictionary
response = client.chat.send({
"messages": ["Hello!"],
"user_id": "alice",
"llm": "cerebras",
"llm_model": "llama-3.3-70b"
})
Style 3: Legacy Parameters (Backward Compatible)
response = client.chat.send(
message="Hello!", # Single message string
user_id="alice", # Default: "default_user"
llm="cerebras", # Default: "cerebras"
model="llama-3.3-70b", # Default: "llama-3.3-70b"
llm_api_key=None, # Optional
max_tokens=None # Optional
)
Parameter Details:
- request - ChatRequest model or dict (new style, recommended)
- message - Single message string (legacy style, use with other kwargs)
- user_id - User identifier for context tracking (default: "default_user")
- llm - LLM provider: "cerebras", "openai", "anthropic", "google", "perplexity" (default: "cerebras")
- llm_api_key - Optional organization-owned API key for the LLM provider
- model - Model name (default: "llama-3.3-70b")
- max_tokens - Maximum tokens in response (optional, uses provider default if not set)
Returns: ChatResponse with AI message and metadata
Avatar API¶
Access via client.avatars.
list()¶
List all available avatars.
Returns: AvatarListResponse
Example:
get(avatar_id: str)¶
Get details about a specific avatar.
Parameters:
- avatar_id (str): Avatar identifier
Returns: Dict[str, Any]
Example:
create(avatar_data: Union[AvatarImportRequest, Dict[str, Any]])¶
Create a new avatar.
Parameters:
- avatar_data: AvatarImportRequest model or dict with avatar configuration
Returns: Dict[str, Any]
Example:
# avatar_id is NOT provided - it's generated and returned
avatar = client.avatars.create({
"name": "TechExpert",
"personality": {"traits": ["helpful"]}
})
# avatar["avatar_id"] contains the generated UUID
print(f"Created avatar with ID: {avatar['avatar_id']}")
update(avatar_id: str, avatar_data: Dict[str, Any])¶
Update an existing avatar.
Parameters:
- avatar_id (str): Avatar identifier
- avatar_data (Dict[str, Any]): Updated configuration
Returns: Dict[str, Any]
Example:
delete(avatar_id: str)¶
Delete an avatar.
Parameters:
- avatar_id (str): Avatar identifier
Returns: Dict[str, Any]
Example:
chat(avatar_id: str, request: Optional[Union[AvatarChatRequest, Dict[str, Any]]] = None, **kwargs)¶
Chat with a specific avatar.
Parameters:
Style 1: Pydantic Model (Recommended)
from cosmicmind.models import AvatarChatRequest
request = AvatarChatRequest(
avatar_id="tech_expert", # Required: Avatar identifier
messages=["Hello!"], # Required: List of messages
user_id="alice", # Default: "default_user"
llm="cerebras", # Default: "cerebras"
avatar_context_only=False, # Default: False
preserve_avatar_voice=True # Default: True
)
response = client.avatars.chat("tech_expert", request)
Style 2: Dictionary
Style 3: Legacy Parameters (Backward Compatible)
response = client.avatars.chat(
"tech_expert", # Required: Avatar identifier
message="Hello!", # Message string
user_id="alice" # Default: "default_user"
)
Parameter Details:
- avatar_id - Avatar identifier (required, first positional argument)
- request - AvatarChatRequest model or dict (optional, new style)
- message - Single message string (legacy style, use with other kwargs)
- user_id - User identifier for context tracking (default: "default_user")
- Additional parameters: Same as ChatRequest (llm, llm_model, max_tokens, etc.)
Returns: ChatResponse with AI message and metadata
Models¶
ChatRequest¶
Request model for chat interactions.
from cosmicmind.models import ChatRequest
request = ChatRequest(
messages: List[str], # Required
user_id: str = "default_user",
llm: str = "cerebras",
llm_model: str = "llama-3.3-70b",
llm_api_key: Optional[str] = None,
db_id: str = "default",
timestamp: str = ..., # Auto-generated
overwrite: bool = False,
max_tokens: Optional[int] = None
)
ChatResponse¶
Response model for chat interactions.
from cosmicmind.models import ChatResponse
response: ChatResponse = client.chat.send(request)
# response.message: str
# response.success: bool
# response.request_id: str
# response.token_usage: Optional[Dict[str, Any]]
# response.model_used: Optional[str]
# response.provider: Optional[str]
AvatarImportRequest¶
Request model for importing/creating avatars.
from cosmicmind.models import AvatarImportRequest
request = AvatarImportRequest(
name: str, # Required
version: str = "1.0", # Avatar version (stored in DynamoDB), NOT the API version
personality: Dict[str, Any], # Required
beliefs: List[Dict[str, str]] = [],
communication_patterns: List[Dict[str, str]] = [],
knowledge_domains: List[str] = []
)
Note: The version field in AvatarImportRequest is the avatar's version (e.g., "1.0", "2.0") stored in DynamoDB, not the API version. API versioning is handled via the X-API-Version header set when initializing the client.
AvatarChatRequest¶
Extended chat request for avatar interactions.
from cosmicmind.models import AvatarChatRequest
request = AvatarChatRequest(
avatar_id: str, # Required
messages: List[str], # Required
user_id: str = "default_user",
avatar_context_only: bool = False,
preserve_avatar_voice: bool = True,
# ... all ChatRequest fields ...
)
AvatarListResponse¶
Response model for listing avatars.
from cosmicmind.models import AvatarListResponse
avatars: AvatarListResponse = client.avatars.list()
# avatars.avatars: List[Dict[str, Any]]
# avatars.count: int
Exceptions¶
CosmicMindError: Base exception for all errorsAuthenticationError: Invalid or missing API keyRateLimitError: Rate limit exceededServiceNotAvailableError: Service not included in your licenseValidationError: Invalid request dataServerError: Server-side error (500+)
Code Documentation¶
For detailed code documentation, see the auto-generated API docs below:
CosmicMindClient ¶
CosmicMind API Client
Main client for interacting with CosmicMind services.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
api_key
|
str
|
Your CosmicMind API key |
required |
base_url
|
str
|
API base URL (default: http://localhost:8000 for local dev) Production: https://cosmicmind.pansynapse.com/api (must include /api) |
'http://localhost:8000'
|
api_version
|
str
|
API version to use (default: v1) |
'v1'
|
Example
from cosmicmind import CosmicMindClient
Production¶
client = CosmicMindClient( ... api_key="sk-your-key", ... base_url="https://cosmicmind.pansynapse.com/api" ... )
Local development¶
client = CosmicMindClient(api_key="sk-your-key") response = client.chat.send("Hello!")
Source code in python/cosmicmind/client.py
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 | |
ChatAPI ¶
Chat API client
Handles all chat-related operations.
Source code in python/cosmicmind/client.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | |
Functions¶
send ¶
send(request: Optional[Union[ChatRequest, Dict[str, Any]]] = None, message: Optional[str] = None, user_id: str = 'default_user', llm: str = 'cerebras', llm_api_key: Optional[str] = None, model: str = 'llama-3.3-70b', max_tokens: Optional[int] = None, **kwargs) -> ChatResponse
Send a chat message and get AI response
Supports multiple input styles for backward compatibility: 1. Pydantic model (recommended) 2. Dictionary 3. Legacy parameters
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Optional[Union[ChatRequest, Dict[str, Any]]]
|
ChatRequest model or dict (new style) |
None
|
message
|
Optional[str]
|
Single message string (legacy style) |
None
|
user_id
|
str
|
User identifier for context tracking |
'default_user'
|
llm
|
str
|
LLM provider (cerebras, openai, anthropic, google) |
'cerebras'
|
llm_api_key
|
Optional[str]
|
Organization owned API key for the LLM provider |
None
|
model
|
str
|
Model name |
'llama-3.3-70b'
|
max_tokens
|
Optional[int]
|
Maximum tokens in response |
None
|
**kwargs
|
Additional parameters |
{}
|
Returns:
| Type | Description |
|---|---|
ChatResponse
|
ChatResponse with AI message and metadata |
Examples:
Style 1: Pydantic model (recommended)¶
>>> from cosmicmind.models import ChatRequest
>>> request = ChatRequest(
... messages=["Hello!"],
... user_id="alice",
... llm="cerebras"
... )
>>> response = client.chat.send(request)
Style 2: Dict¶
Style 3: Legacy parameters (backward compatible)¶
Source code in python/cosmicmind/client.py
AvatarAPI ¶
Avatar API client
Handles all avatar-related operations.
Source code in python/cosmicmind/client.py
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | |
Functions¶
chat ¶
chat(avatar_id: str, request: Optional[Union[AvatarChatRequest, Dict[str, Any]]] = None, message: Optional[str] = None, user_id: str = 'default_user', **kwargs) -> ChatResponse
Chat with a specific avatar
Supports multiple input styles for backward compatibility: 1. Pydantic model (recommended) 2. Dictionary 3. Legacy parameters
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
avatar_id
|
str
|
Avatar identifier (required, first positional argument) |
required |
request
|
Optional[Union[AvatarChatRequest, Dict[str, Any]]]
|
AvatarChatRequest model or dict (new style, recommended) |
None
|
message
|
Optional[str]
|
Single message string (legacy style, use with other kwargs) |
None
|
user_id
|
str
|
User identifier for context tracking (default: "default_user") |
'default_user'
|
**kwargs
|
Additional parameters (llm, llm_model, max_tokens, etc.) |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
ChatResponse |
ChatResponse
|
Response with avatar's reply and metadata |
Examples:
Style 1: Pydantic model (recommended)¶
>>> from cosmicmind.models import AvatarChatRequest
>>> request = AvatarChatRequest(
... avatar_id="tech_expert",
... messages=["How do I optimize queries?"],
... user_id="alice"
... )
>>> response = client.avatars.chat("tech_expert", request)
Style 2: Dict¶
>>> response = client.avatars.chat("tech_expert", {
... "messages": ["Help me with Python"],
... "user_id": "alice"
... })
Style 3: Legacy parameters (backward compatible)¶
>>> response = client.avatars.chat(
... "tech_expert",
... message="Hello!",
... user_id="alice"
... )
Source code in python/cosmicmind/client.py
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | |
create ¶
Create a new avatar
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
avatar_data
|
Union[AvatarImportRequest, Dict[str, Any]]
|
AvatarImportRequest model or dict with avatar configuration |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Created avatar with ID |
Examples:
Style 1: Pydantic model (recommended)¶
>>> from cosmicmind.models import AvatarImportRequest
>>> request = AvatarImportRequest(
... name="TechExpert",
... personality={"traits": ["helpful"]}
... )
>>> avatar = client.avatars.create(request)
>>> print(avatar["avatar_id"]) # Server-generated UUID
Style 2: Dict (validates internally)¶
>>> avatar = client.avatars.create({
... "name": "TechExpert",
... "personality": {"traits": ["helpful"]}
... })
>>> print(avatar["avatar_id"]) # Server-generated UUID
Source code in python/cosmicmind/client.py
delete ¶
Delete an avatar
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
avatar_id
|
str
|
Avatar identifier |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Success message |
Example
client.avatars.delete("avatar_123")
Source code in python/cosmicmind/client.py
get ¶
Get details about a specific avatar
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
avatar_id
|
str
|
Avatar identifier |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Avatar details dictionary |
Example
avatar = client.avatars.get("avatar_123") print(avatar['name'])
Source code in python/cosmicmind/client.py
list ¶
List all available avatars
Returns:
| Type | Description |
|---|---|
AvatarListResponse
|
AvatarListResponse with list of avatars and count |
Example
avatars = client.avatars.list() print(f"Found {avatars.count} avatars") for avatar in avatars.avatars: ... print(avatar['name'])
Source code in python/cosmicmind/client.py
update ¶
Update an existing avatar
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
avatar_id
|
str
|
Avatar identifier |
required |
avatar_data
|
Dict[str, Any]
|
Updated configuration |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Success message |
Example
client.avatars.update("avatar_123", { ... "personality": "more cheerful" ... })
Source code in python/cosmicmind/client.py
ChatRequest ¶
Bases: BaseModel
Request model for chat interactions
Matches cosmicmind-server's CosmicMindRequest schema. Provides client-side validation before sending to API.
Attributes:
| Name | Type | Description |
|---|---|---|
messages |
List[str]
|
List of user messages to process |
user_id |
str
|
Unique identifier for context tracking |
llm |
str
|
LLM provider (cerebras, openai, anthropic, google, perplexity) |
llm_model |
str
|
Specific model name (e.g., llama-3.3-70b, gpt-4) |
llm_api_key |
Optional[str]
|
Optional organization-owned LLM API key |
db_id |
str
|
Database identifier for multi-tenancy |
timestamp |
str
|
ISO format timestamp (auto-generated if not provided) |
overwrite |
bool
|
Whether to overwrite existing context |
max_tokens |
Optional[int]
|
Maximum tokens for response (None = provider default) |
Example
from cosmicmind.models import ChatRequest request = ChatRequest( ... messages=["Hello, who am I?"], ... user_id="alice_123", ... llm="cerebras", ... llm_model="llama-3.3-70b" ... ) response = client.chat.send(request)
Source code in python/cosmicmind/models.py
ChatResponse ¶
Bases: BaseModel
Response model for chat interactions
Matches cosmicmind-server's CosmicMindResponse schema.
Attributes:
| Name | Type | Description |
|---|---|---|
message |
str
|
AI-generated response text |
success |
bool
|
Whether the request was processed successfully |
code |
str
|
Error code if success is False |
request_id |
str
|
Unique identifier for this request |
debug_context |
Optional[Dict[str, Any]]
|
Debug information (only in debug mode) |
token_usage |
Optional[Dict[str, Any]]
|
Token usage statistics with costs |
model_used |
Optional[str]
|
The specific model that generated the response |
provider |
Optional[str]
|
The LLM provider that was used |
Example
response = client.chat.send(request) print(response.message) print(f"Tokens used: {response.token_usage['total_tokens']}")
Source code in python/cosmicmind/models.py
AvatarImportRequest ¶
Bases: BaseModel
Request model for importing/creating avatars
Matches cosmicmind-server's AvatarImportRequest schema.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Display name of the avatar (required) |
personality |
Dict[str, Any]
|
Core personality configuration dict (required) |
avatar_id |
Optional[str]
|
Optional - if not provided, server generates a UUID |
version |
str
|
Avatar version (for future updates) |
beliefs |
List[Dict[str, str]]
|
List of avatar beliefs |
communication_patterns |
List[Dict[str, str]]
|
Communication style patterns |
knowledge_domains |
List[str]
|
Areas of expertise |
Example
from cosmicmind.models import AvatarImportRequest request = AvatarImportRequest( ... name="TechExpert", ... personality={ ... "traits": ["helpful", "patient", "knowledgeable"], ... "speaking_style": "Clear and concise" ... }, ... knowledge_domains=["python", "apis", "databases"] ... ) avatar = client.avatars.create(request) print(avatar["avatar_id"]) # Server-generated UUID
Source code in python/cosmicmind/models.py
AvatarChatRequest ¶
Bases: ChatRequest
Extended chat request for avatar interactions
Inherits from ChatRequest and adds avatar-specific fields. Matches cosmicmind-server's AvatarChatRequest schema.
Attributes:
| Name | Type | Description |
|---|---|---|
avatar_id |
str
|
Avatar to chat with |
avatar_context_only |
bool
|
Use only avatar context, no user history |
preserve_avatar_voice |
bool
|
Maintain avatar's speaking style |
Example
from cosmicmind.models import AvatarChatRequest request = AvatarChatRequest( ... avatar_id="tech_expert", ... messages=["How do I optimize database queries?"], ... user_id="alice_123", ... preserve_avatar_voice=True ... ) response = client.avatars.chat(request)
Source code in python/cosmicmind/models.py
AvatarListResponse ¶
Bases: BaseModel
Response model for listing avatars
Attributes:
| Name | Type | Description |
|---|---|---|
avatars |
List[Dict[str, Any]]
|
List of available avatars with their details |
count |
int
|
Total number of avatars |
Example
avatars = client.avatars.list() print(f"Found {avatars.count} avatars") for avatar in avatars.avatars: ... print(avatar['name'])
Source code in python/cosmicmind/models.py
exceptions ¶
CosmicMind SDK Exceptions
Custom exceptions for the CosmicMind Python SDK.
Classes¶
AuthenticationError ¶
Bases: CosmicMindError
Raised when API key is invalid or missing
CosmicMindError ¶
RateLimitError ¶
Bases: CosmicMindError
Raised when rate limit is exceeded
ServerError ¶
Bases: CosmicMindError
Raised when server encounters an error
ServiceNotAvailableError ¶
Bases: CosmicMindError
Raised when trying to access a service not included in your license
ValidationError ¶
Bases: CosmicMindError
Raised when request data is invalid