Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into integrations/uni-v3
Browse files Browse the repository at this point in the history
  • Loading branch information
jwarmuz99 committed May 28, 2024
2 parents f5c027e + 31d91f6 commit 7d5f882
Show file tree
Hide file tree
Showing 34 changed files with 1,764 additions and 3,542 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/onpush.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ jobs:
poetry install --all-extras
- name: Lint with ruff
run: |
poetry run ruff giza_actions
poetry run ruff giza
- name: Pre-commit check
run: |
poetry run pre-commit run --all-files
- name: Testing
run: |
poetry run pytest --cov=giza_actions --cov-report term-missing
poetry run pytest --cov=giza.agents --cov-report term-missing
2 changes: 1 addition & 1 deletion .github/workflows/onrelease.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
poetry install
- name: Lint with ruff
run: |
poetry run ruff giza_actions
poetry run ruff giza
- name: Build dist
run: poetry build
- name: Publish a Python distribution to PyPI
Expand Down
79 changes: 10 additions & 69 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
![image](https://github.com/gizatechxyz/giza-agents/assets/18899187/eb01e7f2-c0ec-4467-ba29-09e96042dae4)

# Actions SDK
# AI Agents

The Actions SDK is a Python library designed to facilitate the development of ZKML applications on the Giza platform. It provides a set of decorators and classes to define tasks, actions, and models, and to handle data inputs.
Giza Agents is a framework for trust-minimized integration of machine learning into on-chain strategy and action, featuring mechanisms for agentic memory and reflection that improve performance over their lifecycle.

The extensible nature of Giza Agents allows developers to enshrine custom strategies using ML and other algorithms, develop novel agent functionalities and manage continuous iteration processes.

## Where to start?

Check out our extensive [documentation](https://actions.gizatech.xyz/welcome/giza-actions-sdk) to understand the concepts and follow how-to-guides.
Check out our extensive [documentation]([docs.gizatech.xyz/products/ai-agents) to understand the concepts and follow how-to-guides.

## Installation

Expand All @@ -21,10 +24,10 @@ $ source .env/bin/activate
$ .env/Scripts/activate
```

Now you’re ready to install ⚡Actions with the following command:
Now you’re ready to install ⚡Agents with the following command:

```bash
$ pip install giza-actions
$ pip install giza-agents
```

## Setup
Expand All @@ -47,70 +50,8 @@ Optional: you can create an API Key for your user in order to not regenerate you
$ giza users create-api-key
```

To create Actions Runs you will need a Giza Workspace, create it by executing the following command in your terminal:

```bash
$ giza workspaces create
```

## Usage

### Defining Tasks

A task is a function that represents a distinct segment of work in a Giza Actions workflow. Tasks provide a way to encapsulate parts of your workflow logic in traceable, reusable units across actions.

Tasks are defined using the `@task` decorator. Here's an example:

```python
from giza.task import task

@task
def preprocess():
print(f"Preprocessing...")
```

### Defining Actions

An action serves as a framework for coding ML inferencing workflow logic, enabling users to tailor the behaviour of their workflows.

Actions are defined using the `@action` decorator. Here's an example:

```python
from giza.action import action

@action(name="My Action")
def inference():
print(f"Running inference...")
```

### Deploy Actions

Deployments are server-side representations of actions. They keep essential metadata required for remote orchestration, including when, where, and how a workflow should run. Deployments transform workflows from functions that need to be manually activated to API-managed entities capable of being triggered remotely.

We can easily create a deployment by creating the Action object and then calling the serve function in the entrypoint script:

```python
from giza_actions.action import Action, action
from giza_actions.task import task

@task
def print_hello():
print(f"Hello Action!")

@action
def hello_world():
print_hello()

if __name__ == '__main__':
action_deploy = Action(entrypoint=hello_world, name="hello-world-action")
action_deploy.serve(name="hello-world-action-deployment")
```

Running this script will do two things:

- Create a deployment called "hello-world-action" for your action in the Giza Platform.
- Stay running to listen for action runs for this deployment; when a run is found, it will be asynchronously executed within a subprocess locally.

### Creating Agents

Agents are the entities that interact with the Giza Platform to handle verification of predictions and interactions with Smart Contracts. They are responsible for wating until the proof of the prediction is available and verified, and then handling the interaction with the contract.
Expand Down Expand Up @@ -166,7 +107,7 @@ $ export <ACCOUNT NAME>_PASSPHRASE=<passphrase>
```
```python
from giza_actions.agent import Agent
from giza.agents import Agent
# Here we check for the passphrase in the environment
agent = Agent.from_id(id=1, contracts={"my_contract": "0x1234567890"})
Expand All @@ -185,7 +126,7 @@ with agent.execute() as contracts:
## Examples
Examples of how to use the Actions SDK can be found in the `examples` directory. Each example includes a README or a Notebook with detailed instructions on how to run the example.
Examples of how to use the Agents can be found in the `examples` directory. Each example includes a README or a Notebook with detailed instructions on how to run the example.
## Contributing
Expand Down
13 changes: 4 additions & 9 deletions examples/agents/action_agent.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import logging
import pprint
import numpy as np
from PIL import Image
from giza_actions.action import action
from giza_actions.agent import GizaAgent
from giza_actions.task import task
from giza.agents import GizaAgent

from prefect import get_run_logger

# Process the image
@task
def process_image(img):
img = np.resize(img, (28,28))
img = img.reshape(1,1,28,28)
Expand All @@ -19,17 +16,15 @@ def process_image(img):
return tensor

# Get the image
@task
def get_image(path):
with Image.open(path) as img:
img = img.convert('L')
img = np.array(img)
return img

# Create the Action
@action(log_prints=True)
# Create the execution function
def transmission():
logger = get_run_logger()
logger = logging.getLogger(__name__)
img_path = 'seven.png'
img = get_image(img_path)
img = process_image(img)
Expand Down
24 changes: 8 additions & 16 deletions examples/agents/mnist_agent_tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
"\n",
"In this notebook, we will create an AI Agent to mint an MNIST NFT. Using an existing MNIST endpoint, we will perform a prediction on the MNIST dataset and mint an NFT based on the prediction.\n",
"\n",
"This tutorial can be thought of as the continuation of the previous tutorial [Build a Verifiable Neural Network with Giza Actions](https://actions.gizatech.xyz/tutorials/build-a-verifiable-neural-network-with-giza-actions) where we created a verifiable MNIST model ([notebook](https://github.com/gizatechxyz/actions-sdk/blob/main/examples/verifiable_mnist/verifiable_mnist.ipynb)).\n",
"This tutorial can be thought of as the continuation of the previous tutorial [Verifiable MNIST Neural Network](https://docs.gizatech.xyz/tutorials/zkml/verifiable-mnist-neural-network) where we created a verifiable MNIST model ([notebook](https://github.com/gizatechxyz/giza-agents/blob/main/examples/verifiable_mnist/verifiable_mnist.ipynb)).\n",
"\n",
"## Before you begin\n",
"\n",
"1. Python 3.11 or later must be installed on your machine\n",
"2. Giza CLI must be installed on your machine. You can install it by running `pip install giza-cli`\n",
"3. Actions-SDK should be installed with the extra `agents`. You can install it by running `pip install giza-actions[agents]`\n",
"3. giza-agents should be installed. You can install it by running `pip install giza-agents`\n",
"4. You must have an active Giza account. If you don't have one, you can create one [here](https://cli.gizatech.xyz/examples/basic).\n",
"5. You must have an MNIST model deployed on Giza. You can follow the tutorial [Build a Verifiable Neural Network with Giza Actions](https://actions.gizatech.xyz/tutorials/build-a-verifiable-neural-network-with-giza-actions) to deploy an MNIST model on Giza.\n",
"5. You must have an MNIST model deployed on Giza. You can follow the tutorial [Build a Verifiable Neural Network](https://docs.gizatech.xyz/tutorials/zkml/verifiable-mnist-neural-network) to deploy an MNIST model on Giza.\n",
"6. During the tutorial, you will need to interact with the Giza CLI, and Ape's framework, and provide multiple inputs, like `model-id`, `version-id`, `account name`, etc.\n",
"7. You must be logged in to the Giza CLI or have an API KEY.\n",
"\n",
Expand All @@ -29,10 +29,10 @@
"Let's start by installing the required libraries.\n",
"\n",
"```bash\n",
"pip install giza-actions[agents]\n",
"pip install giza-agents\n",
"```\n",
"\n",
"This will install the `giza-actions` library along with the `agents` extra, which contains the necessary tools to create an AI Agent.\n",
"This will install the `giza-agents` library, which contains the necessary tools to create an AI Agent.\n",
"\n",
"## Creating an account\n",
"\n",
Expand Down Expand Up @@ -108,7 +108,7 @@
"\n",
"## How to use the AI Agent\n",
"\n",
"Now that the agent is created we can start using it through the `giza-actions` library. As we will be using the agent to mint an MNIST NFT, we will need to provide the MNIST image to the agent and preprocess it before sending it to the model."
"Now that the agent is created we can start using it through the `giza-agents` library. As we will be using the agent to mint an MNIST NFT, we will need to provide the MNIST image to the agent and preprocess it before sending it to the model."
]
},
{
Expand All @@ -121,9 +121,7 @@
"import numpy as np\n",
"from PIL import Image\n",
"\n",
"from giza_actions.agent import GizaAgent, AgentResult\n",
"from giza_actions.action import action\n",
"from giza_actions.task import task\n",
"from giza.agents import GizaAgent, AgentResult\n",
"\n",
"# Make sure to fill these in\n",
"MODEL_ID = ...\n",
Expand All @@ -146,7 +144,7 @@
"* Access the prediction result\n",
"* Mint an NFT based on the prediction result\n",
"\n",
"To load and preprocess the image, we can use the function that we already created in the previous tutorial [Build a Verifiable Neural Network with Giza Actions](https://actions.gizatech.xyz/tutorials/build-a-verifiable-neural-network-with-giza-actions)."
"To load and preprocess the image, we can use the function that we already created in the previous tutorial [Build a Verifiable Neural Network](https://docs.gizatech.xyz/tutorials/zkml/verifiable-mnist-neural-network)."
]
},
{
Expand All @@ -156,7 +154,6 @@
"outputs": [],
"source": [
"# This function is for the previous MNIST tutorial\n",
"@task(name=\"Preprocess an image for the MNIST model.\")\n",
"def preprocess_image(image_path: str):\n",
" \"\"\"\n",
" Preprocess an image for the MNIST model.\n",
Expand Down Expand Up @@ -197,7 +194,6 @@
"metadata": {},
"outputs": [],
"source": [
"@task(name=\"Create a Giza agent for the MNIST model\")\n",
"def create_agent(model_id: int, version_id: int, chain: str, contract: str):\n",
" \"\"\"\n",
" Create a Giza agent for the MNIST model with MNIST \n",
Expand Down Expand Up @@ -225,7 +221,6 @@
"metadata": {},
"outputs": [],
"source": [
"@task(name=\"Predict the digit in an image.\")\n",
"def predict(agent: GizaAgent, image: np.ndarray):\n",
" \"\"\"\n",
" Predict the digit in an image.\n",
Expand Down Expand Up @@ -259,7 +254,6 @@
"metadata": {},
"outputs": [],
"source": [
"@task(name=\"Get the digit from the prediction.\")\n",
"def get_digit(prediction: AgentResult):\n",
" \"\"\"\n",
" Get the digit from the prediction.\n",
Expand Down Expand Up @@ -300,7 +294,6 @@
"metadata": {},
"outputs": [],
"source": [
"@task(name=\"Execute the MNIST to mint a new NFT.\")\n",
"def execute_contract(agent: GizaAgent, digit: int):\n",
" \"\"\"\n",
" Execute the MNIST contract with the predicted digit to mint a new NFT.\n",
Expand Down Expand Up @@ -330,7 +323,6 @@
"metadata": {},
"outputs": [],
"source": [
"@action(name=\"Mint an NFT with a prediction.\", log_prints=True)\n",
"def mint_nft_with_prediction():\n",
" # Preprocess the image\n",
" image = preprocess_image(\"seven.png\")\n",
Expand Down
2 changes: 1 addition & 1 deletion examples/agents/read_contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
In this example, we call the `name` method of the contracts, which is a method that returns the name of the contract, and then we print the result. But we could execute any function of a contract in the same way.
"""

from giza_actions.agent import GizaAgent
from giza.agents import GizaAgent

MODEL_ID = ...
VERSION_ID = ...
Expand Down
16 changes: 8 additions & 8 deletions examples/agents/using_arbitrum.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@
"\n",
"1. Python 3.11 or later must be installed on your machine\n",
"2. Giza CLI must be installed on your machine. You can install it by running `pip install giza-cli`\n",
"3. Actions-SDK should be installed with the extra `agents`. You can install it by running `pip install giza-actions[agents]`\n",
"3. giza-agents should be installed. You can install it by running `pip install giza-agents`\n",
"4. You must have an active Giza account. If you don't have one, you can create one [here](https://cli.gizatech.xyz/examples/basic).\n",
"5. You must have a model deployed on Giza. You can follow the tutorial [Build a Verifiable Neural Network with Giza Actions](https://actions.gizatech.xyz/tutorials/build-a-verifiable-neural-network-with-giza-actions) to deploy an MNIST model on Giza.\n",
"6. During the tutorial, you will need to interact with the Giza CLI, Ape's framework, and provide multiple inputs, like `model-id`, `version-id`, `account name`, etc.\n",
"5. You must have an MNIST model deployed on Giza. You can follow the tutorial [Build a Verifiable Neural Network](https://docs.gizatech.xyz/tutorials/zkml/verifiable-mnist-neural-network) to deploy an MNIST model on Giza.\n",
"6. During the tutorial, you will need to interact with the Giza CLI, and Ape's framework, and provide multiple inputs, like `model-id`, `version-id`, `account name`, etc.\n",
"7. You must be logged in to the Giza CLI or have an API KEY.\n",
"\n",
"## Installing the required libraries\n",
"\n",
"Let's start by installing the required libraries.\n",
"\n",
"```bash\n",
"pip install giza-actions[agents]\n",
"pip install giza-agents\n",
"```\n",
"\n",
"This will install the `giza-actions` library along with the `agents` extra, which contains the necessary tools to create an AI Agent.\n",
"This will install the `giza-agents` library along with the `agents` extra, which contains the necessary tools to create an AI Agent.\n",
"\n",
"## Creating an account\n",
"\n",
Expand Down Expand Up @@ -174,13 +174,13 @@
"export <Account>_PASSPHRASE=your-passphrase\n",
"```\n",
"\n",
"If you are using it from a notebook, you will need to launch the notebook instance from an environment with the passphrase variable or set it in the code before importing `giza_actions`:\n",
"If you are using it from a notebook, you will need to launch the notebook instance from an environment with the passphrase variable or set it in the code before importing `giza.agents`:\n",
"\n",
"```python\n",
"import os\n",
"os.environ[\"<Account>_PASSPHRASE\"] = \"your-passphrase\"\n",
"\n",
"from giza_actions.agent import GizaAgent\n",
"from giza.agents import GizaAgent\n",
"...\n",
"```\n",
"\n",
Expand All @@ -197,7 +197,7 @@
"os.environ[\"<Account>_PASSPHRASE\"] = ...\n",
"\n",
"\n",
"from giza_actions.agent import GizaAgent\n",
"from giza.agents import GizaAgent\n",
"\n",
"MODEL_ID = ...\n",
"VERSION_ID = ...\n",
Expand Down
Loading

0 comments on commit 7d5f882

Please sign in to comment.