Skip to content

Commit

Permalink
v0.2.4, added the CodeToolWorkflow and associated examples/documentat…
Browse files Browse the repository at this point in the history
…ion.
  • Loading branch information
rlesueur committed Jul 13, 2024
1 parent 1abf9be commit 2b1c2da
Show file tree
Hide file tree
Showing 12 changed files with 655 additions and 6 deletions.
151 changes: 151 additions & 0 deletions docs/workflows/code_tool_workflow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
# ReXia.AI CodeToolWorkflow Class

## Overview

The `CodeToolWorkflow` class is a specialized workflow implementation in the ReXia.AI framework. It extends the `BaseWorkflow` class and implements a process for generating and executing custom code tools to solve complex tasks. This workflow is designed to be used within an Agent instance and leverages a large language model (LLM) to dynamically create Python functions that can assist in completing the given task.

## Table of Contents

- [Class Attributes](#class-attributes)
- [Methods](#methods)
- [Usage](#usage)
- [Components](#components)
- [Dependencies](#dependencies)
- [Contributing](#contributing)
- [License](#license)

## Class Attributes

- `llm`: The language model used by the workflow.
- `task`: The task that the workflow is designed to perform.
- `verbose`: A flag used for enabling verbose mode.
- `memory`: The memory component of the workflow.
- `channel`: The collaboration channel for the workflow.
- `max_attempts`: The maximum number of attempts for code generation.

## Methods

### `__init__(self, llm: Any, task: str, memory: BaseMemory, verbose: bool = False, max_attempts: int = 3) -> None`

Initializes a CodeToolWorkflow instance.

**Parameters:**

- `llm`: The language model used by the workflow.
- `task`: The task assigned to the workflow.
- `memory`: The memory instance used by the workflow.
- `verbose`: A flag for enabling verbose mode. Defaults to `False`.
- `max_attempts`: The maximum number of attempts for code generation. Defaults to `3`.

### `_run_task(self) -> None`

Internal method that executes the main CodeToolWorkflow process.

### `run(self) -> str`

Public method to initiate the workflow execution.

## Usage

Here's an example of how to use the `CodeToolWorkflow` class with an Agent in ReXia.AI:

```python
import os
from rexia_ai.llms import RexiaAIOpenAI
from rexia_ai.agents import Agent
from rexia_ai.workflows import CodeToolWorkflow
from rexia_ai.memory import SimpleMemory

# Set up the language model
YI_LARGE_API_KEY = os.getenv("YI_LARGE_API_KEY")
llm = RexiaAIOpenAI(
base_url="https://api.01.ai/v1",
model="yi-large",
temperature=0,
api_key=YI_LARGE_API_KEY,
)

# Define the task
task = "Calculate the sum of squares for the first 100 prime numbers."

# Create an Agent instance with CodeToolWorkflow
agent = Agent(
llm=llm,
task=task,
workflow=CodeToolWorkflow,
verbose=True
)

# Run the agent (which will use the CodeToolWorkflow)
response = agent.invoke()

# Print the response
print("Task:", task)
print("Response:", response)
```

## Components

The `CodeToolWorkflow` primarily utilizes the following components:

1. **Large Language Model (LLM)**: Generates Python code based on the given task.
2. **ContainerisedCodeTester**: Executes the generated code in an isolated Docker container.

### Large Language Model (LLM)

The LLM is responsible for generating Python code that can solve the given task. Key features include:

- Receiving a task description and generating appropriate Python functions
- Handling complex tasks by breaking them down into manageable steps
- Iteratively improving the generated code based on execution results

### ContainerisedCodeTester

The ContainerisedCodeTester class is responsible for executing the generated Python code in isolated Docker containers. Key features include:

- Initialization with a specified Docker image (default: "python:3.12-slim") and execution timeout
- Execution of code in a controlled Docker environment
- Writing code to a temporary directory
- Running the Docker container with the provided code
- Parsing and returning the execution results

Key methods:

- `execute_code(code)`: Executes the provided code in a Docker container
- `_write_files(tmpdir, code)`: Writes the code files to a temporary directory
- `_run_container(tmpdir)`: Runs the Docker container with the provided code
- `_parse_output(output)`: Parses the output from the Docker container

The ContainerisedCodeTester ensures that the generated code runs in a secure, isolated environment with:

- Limited memory (128MB)
- Limited CPU quota
- No network access
- Read-only access to the code files

## Dependencies

- `typing`
- `docker`
- `tempfile`
- `os`
- `json`
- ReXia.AI components (`BaseWorkflow`, `BaseMemory`, `CollaborationChannel`, `TaskStatus`, `Component`)
- ReXia.AI Agent class

Ensure all dependencies are installed and properly imported.

## Contributing

We welcome contributions to improve the ReXia.AI framework. Please follow these steps to contribute:

1. Fork the repository.
2. Create a new branch (`git checkout -b feature-branch`).
3. Make your changes.
4. Commit your changes (`git commit -m 'Add new feature'`).
5. Push to the branch (`git push origin feature-branch`).
6. Create a new Pull Request.

## License

This project is licensed under the Apache License 2.0. See the [LICENSE](../LICENSE) file for details.
Empty file.
32 changes: 32 additions & 0 deletions examples/code_tool_workflow_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""Example agent in ReXia.AI using CodeToolWorkflow for a complex mathematical task."""

import os
from rexia_ai.agents import Agent
from rexia_ai.llms import RexiaAIOpenAI
from rexia_ai.workflows import CodeToolWorkflow

YI_LARGE_API_KEY = os.getenv("YI_LARGE_API_KEY")

# Create an instance of the ReXiaAI LLM
llm = RexiaAIOpenAI(
base_url="https://api.01.ai/v1",
model="yi-large",
temperature=0,
api_key=YI_LARGE_API_KEY,
)

# Define a complex mathematical task
task = "Calculate the first 100 prime numbers and then find the sum of their digits."

# Create an instance of the ReXiaAI Agent with the specified task, LLM and workflow
agent = Agent(
llm=llm,
task=task,
workflow=CodeToolWorkflow,
verbose=True,
)

# Generate the response from the agent
response = agent.invoke()

print("Response:", response)
2 changes: 1 addition & 1 deletion obj/Debug/net8.0/rexia_ai.AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
[assembly: System.Reflection.AssemblyCompanyAttribute("rexia_ai")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+8a4ca504b66337d9f7ed81ca32b5762568effe96")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+1abf9bea62a5b05a8990de615198fc729e8cf50c")]
[assembly: System.Reflection.AssemblyProductAttribute("rexia_ai")]
[assembly: System.Reflection.AssemblyTitleAttribute("rexia_ai")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
Expand Down
2 changes: 1 addition & 1 deletion obj/Debug/net8.0/rexia_ai.AssemblyInfoInputs.cache
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6860acc8e31b2544d30eaef17b437cc430337473f1b17c86cf26ef5c164d9de7
c9b03f83e6ca55a1b9af8c9f5dd15f306e3470a2632dea9dd3178d35347e02c2
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name='rexia_ai',
version='0.2.3',
version='0.2.4',
author='Robyn Le Sueur',
author_email='[email protected]',
description='ReXia.AI: An advanced AI framework for agentic processes',
Expand Down
4 changes: 3 additions & 1 deletion src/rexia_ai/agents/workers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
from .finalise import FinaliseWorker
from .team_work import TeamWorker
from .tdd import TDDWorker
from .llm_tool import LLMTool

__all__ = [
"ToolWorker",
"Worker",
"PlanWorker",
"FinaliseWorker",
"TeamWorker",
"TDDWorker"
"TDDWorker",
"LLMTool"
]
Loading

0 comments on commit 2b1c2da

Please sign in to comment.