ChefProcess
ChefProcess adds AI capabilities to your Capela applications. It provides a way to integrate AI into your app, allowing it to understand and respond to user input.
Components
ChefProcess consists of:
- ChefProcess: Handles AI interactions
- ChefMessage: Messages between your app and the AI
- Image Class: Defines the AI's capabilities and behavior
- Message List: Stores conversation history
Creating an AI-Enabled App
1. Define an Image Class
The Image Class defines the AI's capabilities. Its docstring describes the AI's personality and capabilities, and its methods provide specific functionality.
from builtins import Node, Field
class MyAIImage(Node):
"""
You are a helpful assistant designed to be a personal trainer.
You help users with fitness goals, training schedules, and exercise advice.
"""
def get_exercise_info(self, exercise_id: str) -> str:
"""Get information about an exercise."""
if exercise_id not in ALL_EXERCISES:
raise ValueError(f"Exercise with ID {exercise_id} not found.")
return f"Here's how to do the {exercise_id} exercise."
2. Create a ChefProcess Instance
from builtins import Node, Field, ChefProcess, ChefMessage, MessageHeader, ChefMessageBodyText, List
class MyAIApp(Node):
image: MyAIImage = Field(default_factory=MyAIImage)
process: ChefProcess = Field(default_factory=ChefProcess)
messages: List[ChefMessage] = Field(default_factory=list)
def init_process(self):
this.process = ChefProcess(image=this.image.make_reference())
3. Set Up Message Handling
def send_message(self, message: str) -> None:
msg = ChefMessage(
header=MessageHeader(
source=this.make_reference(),
destination=this.process.make_reference(),
in_reply_to=None,
reply_to=this.make_reference(),
),
body=ChefMessageBodyText(text=message)
)
this.messages.append(msg)
this.process.send_message(msg)
Complete Example
from builtins import Node, Field, ChefProcess, ChefMessage, MessageHeader, ChefMessageBodyText, List
ALL_EXERCISES = ["biceps_curl", "tricep_extension", "shoulder_press"]
class ChefTestImage(Node):
"""
You are a helpful assistant designed to be a personal trainer.
You help users with fitness goals, training schedules, and exercise advice.
"""
def swap_exercise(self, exercise_id: str) -> str:
"""Switch to a different exercise."""
if exercise_id not in ALL_EXERCISES:
raise ValueError(f"Exercise with ID {exercise_id} not found.")
return f"You are now in the exercise with ID {exercise_id}."
def get_exercise_info(self, exercise_id: str) -> str:
"""Get information about an exercise."""
if exercise_id not in ALL_EXERCISES:
raise ValueError(f"Exercise with ID {exercise_id} not found.")
return f"Here's how to do the {exercise_id} exercise."
class ChefChat(Node):
image: ChefTestImage = Field(default_factory=ChefTestImage)
process: ChefProcess = Field(default_factory=ChefProcess)
messages: List[ChefMessage] = Field(default_factory=list)
def reset(self):
this.process = ChefProcess(image=this.image.make_reference())
this.messages = []
def add_message(self, message: str, sender_name: str = "Unknown"):
if sender_name == "Unknown":
sender_name = this.make_reference()
msg = ChefMessage(
header=MessageHeader(
source=sender_name,
destination=this.make_reference(),
in_reply_to=None,
reply_to=this.make_reference(),
),
body=ChefMessageBodyText(text=message)
)
this.messages.append(msg)
this.process.send_message(msg)
return {"status": "success", "message": "Message added successfully"}