Build with AWP
Implement AWP events directly in your agent framework
In this tutorial, you’ll learn how to build an HTTP endpoint that is compatible with the AWP protocol.
Prerequisites
Make sure to have Python and Poetry installed.
Setup a New Project with Poetry
First, let’s create a new project and set up Poetry for dependency management:
Install Dependencies
Now, let’s install the necessary packages:
Create a Basic Endpoint with FastAPI
Create a new file called my_endpoint/main.py
with the following code:
Run and Test Your Endpoint
Start the server with:
In another terminal, test your endpoint is running using curl:
You should see the following response:
Parsing AWP Input
Next let’s update our endpoint to properly parse the incoming AWP request using
the RunAgentInput
Pydantic model:
FastAPI automatically validates the incoming request against the RunAgentInput schema. If the request doesn’t match the expected format, it will return a 422 Validation Error with details about what went wrong.
Add Event Streaming
AWP supports streaming events using Server-Sent Events (SSE). Let’s modify our
/awp
endpoint to stream events back to the client:
Awesome! We are already sending RunStartedEvent
and RunFinishedEvent
events,
which gives us a basic AWP compliant endpoint. Now let’s make it do something
useful.
Implementing Basic Chat
Let’s enhance our endpoint to call OpenAI’s API and stream the responses back as AWP events:
You’ll need to set your OpenAI API key as an environment variable and then restart the server:
This implementation creates a fully functional AWP endpoint that processes messages and streams back the responses in real-time.
Prerequisites
Make sure to have Python and Poetry installed.
Setup a New Project with Poetry
First, let’s create a new project and set up Poetry for dependency management:
Install Dependencies
Now, let’s install the necessary packages:
Create a Basic Endpoint with FastAPI
Create a new file called my_endpoint/main.py
with the following code:
Run and Test Your Endpoint
Start the server with:
In another terminal, test your endpoint is running using curl:
You should see the following response:
Parsing AWP Input
Next let’s update our endpoint to properly parse the incoming AWP request using
the RunAgentInput
Pydantic model:
FastAPI automatically validates the incoming request against the RunAgentInput schema. If the request doesn’t match the expected format, it will return a 422 Validation Error with details about what went wrong.
Add Event Streaming
AWP supports streaming events using Server-Sent Events (SSE). Let’s modify our
/awp
endpoint to stream events back to the client:
Awesome! We are already sending RunStartedEvent
and RunFinishedEvent
events,
which gives us a basic AWP compliant endpoint. Now let’s make it do something
useful.
Implementing Basic Chat
Let’s enhance our endpoint to call OpenAI’s API and stream the responses back as AWP events:
You’ll need to set your OpenAI API key as an environment variable and then restart the server:
This implementation creates a fully functional AWP endpoint that processes messages and streams back the responses in real-time.
Prerequisites
Make sure to have Node.js (v16 or later) and npm or yarn installed.
Setup a New Project
First, let’s create a new project and set up npm with TypeScript:
Install Dependencies
Install the necessary packages:
Create a Basic Endpoint with Express
Create a new file called src/server.ts
with the following code:
Run and Test Your Endpoint
Start the server with:
In another terminal, test your endpoint is running using curl:
You should see the following response:
Parsing AWP Input
Next let’s update our endpoint to properly parse the incoming AWP request using
the RunAgentInput
schema:
Express with zod validation ensures the incoming request conforms to the AWP protocol format. If the request doesn’t match the expected format, it will return a 422 Validation Error with details about what went wrong.
Add Event Streaming
AWP supports streaming events using Server-Sent Events (SSE). Let’s modify our
/awp
endpoint to stream events back to the client:
Implementing Basic Chat
Let’s enhance our endpoint to call OpenAI’s API and stream the responses back as AWP events:
You’ll need to set your OpenAI API key as an environment variable and then start the server:
This implementation creates a fully functional AWP endpoint that processes messages and streams back the responses in real-time.