Avatar Examples¶
Examples demonstrating the Avatar system for creating custom AI personalities.
List All Avatars¶
from cosmicmind import CosmicMindClient
client = CosmicMindClient(
api_key="sk-your-api-key-here",
base_url="https://cosmicmind.pansynapse.com/api"
)
avatars = client.avatars.list()
print(f"Found {avatars.count} avatars")
for avatar in avatars.avatars:
print(f" - {avatar['name']}")
Create an Avatar¶
Using Pydantic Model (Recommended)¶
from cosmicmind import CosmicMindClient
from cosmicmind.models import AvatarImportRequest
client = CosmicMindClient(
api_key="sk-your-key",
base_url="https://cosmicmind.pansynapse.com/api"
)
# Create avatar with Pydantic model (recommended)
# Note: avatar_id is NOT provided - it's generated and returned in the response
avatar_request = AvatarImportRequest(
name="TechExpert",
personality={
"traits": ["friendly", "patient", "knowledgeable"],
"speaking_style": "Clear and uses examples"
},
knowledge_domains=["python", "apis", "databases"]
)
avatar = client.avatars.create(avatar_request)
# avatar["avatar_id"] contains the generated UUID
print(f"Created avatar with ID: {avatar['avatar_id']}")
Using Dictionary¶
# avatar_id is NOT provided - it's generated and returned
avatar = client.avatars.create({
"name": "TechExpert",
"personality": {
"traits": ["helpful"],
"speaking_style": "Concise"
},
"knowledge_domains": ["python", "apis"]
})
# avatar["avatar_id"] contains the generated UUID
print(f"Created avatar with ID: {avatar['avatar_id']}")
Get Avatar Details¶
# Use the avatar_id returned from creation
avatar_id = avatar['avatar_id'] # From previous creation example
avatar_details = client.avatars.get(avatar_id)
print(f"Avatar: {avatar_details['name']}")
print(f"Personality: {avatar_details.get('personality')}")
Chat with an Avatar¶
from cosmicmind.models import AvatarChatRequest
# First, create or get an avatar to obtain its avatar_id
avatar = client.avatars.create({
"name": "TechExpert",
"personality": {"traits": ["helpful"]},
"knowledge_domains": ["python", "apis"]
})
avatar_id = avatar['avatar_id'] # Use the returned avatar_id
# Using Pydantic model
request = AvatarChatRequest(
avatar_id=avatar_id,
messages=["How do I optimize database queries?"],
user_id="user_123",
preserve_avatar_voice=True
)
response = client.avatars.chat(avatar_id, request)
# Or using legacy parameters
response = client.avatars.chat(
avatar_id=avatar_id,
message="How do I optimize my database queries?",
user_id="user_123"
)
print(response.message)
Update an Avatar¶
# Use the avatar_id returned from creation
avatar_id = avatar['avatar_id'] # From previous creation example
client.avatars.update(avatar_id, {
"personality": {
"traits": ["enthusiastic", "encouraging"],
"speaking_style": "Very casual and fun"
}
})
Delete an Avatar¶
# Use the avatar_id returned from creation
avatar_id = avatar['avatar_id'] # From previous creation example
client.avatars.delete(avatar_id)
Complete Avatar Workflow¶
from cosmicmind import CosmicMindClient
from cosmicmind.models import AvatarImportRequest, AvatarChatRequest
client = CosmicMindClient(
api_key="sk-your-key",
base_url="https://cosmicmind.pansynapse.com/api"
)
# Create avatar - avatar_id is NOT provided, it's returned in the response
avatar_request = AvatarImportRequest(
name="Code Mentor",
personality={
"traits": ["patient", "encouraging", "knowledgeable"],
"speaking_style": "Uses examples and analogies",
"background": "A senior developer who loves teaching"
},
beliefs=[
{"belief": "Everyone can learn to code"},
{"belief": "Mistakes are learning opportunities"}
],
communication_patterns=[
{
"pattern_type": "greeting",
"pattern": "Hello! Ready to level up your coding skills?"
}
],
knowledge_domains=["python", "javascript", "algorithms", "best-practices"]
)
avatar = client.avatars.create(avatar_request)
# avatar["avatar_id"] contains the generated UUID
avatar_id = avatar['avatar_id']
# Chat with avatar using the returned avatar_id
response = client.avatars.chat(
avatar_id=avatar_id,
message="I'm struggling with recursion. Can you help?",
user_id="student_123"
)
print(response.message)
# List all avatars
avatars = client.avatars.list()
for avatar in avatars.avatars:
print(f"{avatar['name']}: {avatar.get('personality', {})}")