No project description provided
Project description
WorkflowAI Python
A library to use WorkflowAI with Python
Context
WorkflowAI is a platform for building agents.
Installation
workflowai requires a python >= 3.9.
pip install workflowai
Usage
Usage examples are available in the examples directory or end to end test directory.
Getting a workflowai api key
Create an account on workflowai.com, generate an API key and set it as an environment variable.
WORKFLOWAI_API_KEY=...
You can also set the
WORKFLOWAI_API_URLenvironment variable to point to your own WorkflowAI instance.
The current UI does not allow to generate an API key without creating a task. Take the opportunity to play around with the UI. When the task is created, you can generate an API key from the Code section
Set up the workflowai client
If you have defined the api key using an environment variable, the shared workflowai client will be correctly configured.
You can override the shared client by calling the init function.
import workflowai
workflowai.init(
url=..., # defaults to WORKFLOWAI_API_URL env var or https://api.workflowai.com
api_key=..., # defaults to WORKFLOWAI_API_KEY env var
)
Using multiple clients
You might want to avoid using the shared client, for example if you are using multiple API keys or accounts. It is possible to achieve this by manually creating client instances
from workflowai import WorkflowAI
client = WorkflowAI(
url=...,
api_key=...,
)
# Use the client to create and run agents
@client.agent()
def my_agent(task_input: Input) -> Output:
...
Build agents
An agent is in essence an async function with the added constraints that:
- it has a single argument that is a pydantic model
- it has a single return value that is a pydantic model
- it is decorated with the
@client.agent()decorator
Pydantic is a very popular and powerful library for data validation and parsing. It allows us to extract the input and output schema in a simple way
Below is an agent that says hello:
import workflowai
from pydantic import BaseModel
class Input(BaseModel):
name: str
class Output(BaseModel):
greeting: str
@workflowai.agent()
async def say_hello(input: Input) -> Output:
"""Say hello"""
...
When you call that function, the associated agent will be created on workflowai.com if it does not exist yet and a run will be created. By default:
- the docstring will be used as instructions for the agent
- the default model (
workflowai.DEFAULT_MODEL) is used to run the agent - the agent id will be a slugified version of the function name (i-e
say-hello) in this case
What is "..." ?
The
...is the ellipsis value in python. It is usually used as a placeholder. You could use "pass" here as well or anything really, the implementation of the function is handled by the decorator@workflowai.agent()and so the function body is not executed....is usually the right choice because it signals type checkers that they should ignore the function body.
Having the agent id determined at runtime can lead to unexpected changes, since changing the function name will change the agent id. A good practice is to set the agent id explicitly,
@workflowai.agent(id="say-hello").
Using different models
WorkflowAI supports a long list of models. The source of truth for models we support is on workflowai.com. The Model type is a good indication of what models are supported at the time of the sdk release, although it may be missing some models since new ones are added all the time.
You can set the model explicitly in the agent decorator:
@workflowai.agent(model="gpt-4o")
def say_hello(input: Input) -> Output:
...
Models do not become invalid on WorkflowAI. When a model is retired, it will be replaced dynamically by a newer version of the same model with the same or a lower price so calling the api with a retired model will always work.
Version from code or deployments
Setting a docstring or a model in the agent decorator signals the client that the agent parameters are fixed and configured via code.
Handling the agent parameters in code is useful to get started but may be limited in the long run:
- it is somewhat hard to measure the impact of different parameters
- moving to new models or instructions requires a deployment
- iterating on the agent parameters can be very tedious
Deployments allow you to refer to a version of an agent's parameters from your code that's managed from the workflowai.com UI. The following code will use the version of the agent named "production" which is a lot more flexible than changing the function parameters when running in production.
@workflowai.agent(deployment="production") # or simply @workflowai.agent()
def say_hello(input: Input) -> AsyncIterator[Run[Output]]:
...
Streaming and advanced usage
You can configure the agent function to stream or return the full run object, simply by changing the type annotation.
# Return the full run object, useful if you want to extract metadata like cost or duration
@workflowai.agent()
async def say_hello(input: Input) -> Run[Output]:
...
# Stream the output, the output is filled as it is generated
@workflowai.agent()
def say_hello(input: Input) -> AsyncIterator[Output]:
...
# Stream the run object, the output is filled as it is generated
@workflowai.agent()
def say_hello(input: Input) -> AsyncIterator[Run[Output]]:
...
Tools
WorkflowAI has a few tools that can be used to enhance the agent's capabilities:
@browser-textallows fetching the content of a web page@searchallows performing a web search
To use a tool, simply add it's handles to the instructions (the function docstring):
@workflowai.agent()
def say_hello(input: Input) -> Output:
"""
You can use @search and @browser-text to retrieve information about the name.
"""
...
Error handling
Agents can raise errors, for example when the underlying model fails to generate a response or when there are content moderation issues.
All errors are wrapped in a WorkflowAIError that contains details about what happened.
The most interesting fields are:
codeis a string that identifies the type of error, see the errors.py file for more detailsmessageis a human readable message that describes the error
The WorkflowAIError is raised when the agent is called, so you can handle it like any other exception.
try:
await say_hello(Input(name="John"))
except WorkflowAIError as e:
print(e.code)
print(e.message)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file workflowai-0.6.0.dev1.tar.gz.
File metadata
- Download URL: workflowai-0.6.0.dev1.tar.gz
- Upload date:
- Size: 21.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5f68375f3e5b4784598aad0c93bbdb47354900f66182d36c1de78e8a30b6bad6
|
|
| MD5 |
0375b80383c72d42c20b682c7967983f
|
|
| BLAKE2b-256 |
4e6a7d94bd3cce408673c5a1b9b36dd6fb17a830d9b3600749c4f85db60b8d8d
|
Provenance
The following attestation bundles were made for workflowai-0.6.0.dev1.tar.gz:
Publisher:
publish.yml on WorkflowAI/workflowai-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
workflowai-0.6.0.dev1.tar.gz -
Subject digest:
5f68375f3e5b4784598aad0c93bbdb47354900f66182d36c1de78e8a30b6bad6 - Sigstore transparency entry: 165015077
- Sigstore integration time:
-
Permalink:
WorkflowAI/workflowai-py@d4307d45976bd2ef23952b5d844d5d1b7255508b -
Branch / Tag:
refs/heads/main - Owner: https://github.com/WorkflowAI
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@d4307d45976bd2ef23952b5d844d5d1b7255508b -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file workflowai-0.6.0.dev1-py3-none-any.whl.
File metadata
- Download URL: workflowai-0.6.0.dev1-py3-none-any.whl
- Upload date:
- Size: 27.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
684aca8816af261359c50a8f57bccc9635e7fb6518c59ebe6b61266c50998710
|
|
| MD5 |
b7c4f2f8f6610101ef2b65a87c9ef90b
|
|
| BLAKE2b-256 |
6595237a846044de35df9586a3dbef951c0a18954ce570f729a7fd6f5aa97b15
|
Provenance
The following attestation bundles were made for workflowai-0.6.0.dev1-py3-none-any.whl:
Publisher:
publish.yml on WorkflowAI/workflowai-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
workflowai-0.6.0.dev1-py3-none-any.whl -
Subject digest:
684aca8816af261359c50a8f57bccc9635e7fb6518c59ebe6b61266c50998710 - Sigstore transparency entry: 165015078
- Sigstore integration time:
-
Permalink:
WorkflowAI/workflowai-py@d4307d45976bd2ef23952b5d844d5d1b7255508b -
Branch / Tag:
refs/heads/main - Owner: https://github.com/WorkflowAI
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@d4307d45976bd2ef23952b5d844d5d1b7255508b -
Trigger Event:
workflow_dispatch
-
Statement type: