Quick Start Guide
This guide will help you create your first Legion project in under 5 minutes. We'll build a simple conversational agent that can engage in dialogue and perform tasks.
Project Setup
- Create a new project directory:
mkdir my-legion-project cd my-legion-project
- Create a virtual environment and activate it:
python -m venv venv source venv/bin/activate # On Windows, use: venv\Scripts\activate
- Install Legion:
pip install legion-ai
Creating Your First Agent
Create a new file called main.py
:
from typing import Annotated, List from dotenv import load_dotenv from pydantic import Field from legion.agents import agent from legion.interface.decorators import tool load_dotenv() # Legion will load in API keys from environment variables @tool def add_numbers( numbers: Annotated[List[float], Field(description="List of numbers to add together")] ) -> float: """Add a list of numbers together and return the sum.""" return sum(numbers) @tool def multiply( a: Annotated[float, Field(description="First number to multiply")], b: Annotated[float, Field(description="Second number to multiply")] ) -> float: """Multiply two numbers together.""" return a * b @agent( model="openai:gpt-4o-mini", temperature=0.2, tools=[add_numbers, multiply] # Bind external tools ) class MathHelper: # The agent's system prompt comes from the docstring """You are an agent that helps with basic arithmetic and string operations. You can perform calculations and manipulate text based on your requests. You have both external tools for math operations and internal tools for formatting. """ # Agent-specific tools can be defined within the agent class @tool def format_result( self, number: Annotated[float, Field(description="Number to format")], prefix: Annotated[str, Field(description="Text to add before the number")] = ( "Result: " ) ) -> str: """Format a number with a custom prefix.""" return f"{prefix}{number:.2f}" async def main(): # Create an instance of our agent agent = MathHelper() # Example 1: Using external add_numbers tool with internal format_result response = await agent.aprocess( "I have the numbers 1.5, 2.5, and 3.5. Can you add them together and format " "the result nicely?" ) print("Example 1 Response:") print(response.content) print() # Example 2: Using external multiply tool with internal format_result response = await agent.aprocess( "Can you multiply 4.2 by 2.0 and then format the result with the prefix " "'The product is: '?" ) print("Example 2 Response:") print(response.content) print() # Example 3: Complex operation using both external and internal tools response = await agent.aprocess( "I need to add the numbers 10.5 and 20.5, then multiply the result by 2, " "and format it nicely." ) print("Example 3 Response:") print(response.content) if __name__ == "__main__": import asyncio asyncio.run(main())
Next Steps
Now that you have a basic agent running, explore these topics:
- Agents - Learn about more agent features
- Tools - Learn how to give your agents the ability to interact with their environment
- Blocks - Create pre-defined processing steps with Legion
Troubleshooting
If you encounter issues:
- Check your Python version (
python --version
) - Verify Legion is installed (
pip show legion-ai
) - Ensure your API keys are set correctly
- Check the GitHub issues
Need more help? Join our Discord community or check out the full documentation.