Skip to content

Basic Agent Example

This example demonstrates the simplest use case of YosrAI - creating a single agent for basic interactions. It's a great starting point for understanding the core concepts of the framework.

Overview

The Basic Agent example shows how to: - Create a simple action - Configure a basic agent with instructions and prompt templates - Set up a basic workflow - Handle context and message management

Implementation

Here's the complete implementation:

from yosrai import Agent, Action, ContextFunctions

# Create the action
action = Action(action_name='Basic Action')

# Create the basic agent
basic_agent = Agent(
    agent_code='basic_agent', 
    agent_name='Basic Agent',
    instructions_template='You are an AI assistant, your name is YosrAI',
    prompt_template='{{ query }}',
    outputs={
        'messages': ContextFunctions.APPEND_MESSAGES, 
        'output': ContextFunctions.DEFAULT
    }
)

# Set up context and workflow
action.Context(query='hi, what is your name?')
action.add_agent(basic_agent)
action.add_link(basic_agent, "END") 
result = await action.run()

Workflow Visualization

The workflow is straightforward - a single agent that processes input and produces output:

---
title: Single Agent Action
---
graph LR
START((Start)) --> single_agent[Single Agent]
single_agent(Single Agent) --> END((End))

Key Components

Action Setup

The action is created with a descriptive name that represents its purpose:

action = Action(action_name='Basic Action')

Agent Configuration

The basic agent is configured with: - A unique code and name for identification - Instructions template defining its role - Prompt template for processing input - Output configuration for handling responses

basic_agent = Agent(
    agent_code='basic_agent', 
    agent_name='Basic Agent',
    instructions_template='You are an AI assistant, your name is YosrAI',
    prompt_template='{{ query }}',
    outputs={
        'messages': ContextFunctions.APPEND_MESSAGES, 
        'output': ContextFunctions.DEFAULT
    }
)

Context Management

The context is initialized with a simple query:

action.Context(query='hi, what is your name?')

Workflow Definition

The workflow is defined by: 1. Adding the agent to the action 2. Creating a link from the agent to the END state

action.add_agent(basic_agent)
action.add_link(basic_agent, "END")

Example Interaction

When you run this example, you'll get an interaction like this:

Input: "hi, what is your name?"

Output: "My name is YosrAI. I'm here to help you with any questions or information you need. How can I assist you today?"

Best Practices

When using the Basic Agent pattern:

  1. Clear Instructions: Keep the instructions template clear and specific
  2. Appropriate Outputs: Configure outputs based on what you need to capture
  3. Context Management: Initialize context with all necessary parameters
  4. Error Handling: Implement proper error handling for the run operation

Use Cases

This pattern is ideal for: - Simple chatbot implementations - Quick prototypes - Learning the YosrAI framework - Single-purpose agents - Direct question-answer scenarios

Next Steps

After mastering the Basic Agent pattern, you can: 1. Add more complex instructions 2. Implement multiple output types 3. Add error handling 4. Explore more complex agent patterns