How to Build AI Agents with Relevance AI

Mar 31, 2024 | Data Science

Welcome to the world of Relevance AI, where you can harness the power of AI agents to enhance your workforce! In this guide, we’ll walk you through the steps to connect your Python APIs, create custom actions for GPTs (Generative Pre-trained Transformers), and share tools as web apps within your team. Ready to dive in?

Getting Started

1. Installation

First things first! You’ll need to get the Relevance AI package. Open your terminal and run the following command:

pip install relevanceai

Next, we’ll install fastapi and uvicorn to set up our web application:

pip install fastapi
pip install uvicorn[standard]

2. Create Your FastAPI App

If you don’t already have a FastAPI app, here’s a quick example to get you started:

from fastapi import FastAPI
from pydantic import BaseModel
from fastapi import Query

app = FastAPI()

class HelloWorldParams(BaseModel):
    message: str = Query(..., title="Message", description="Message from user")

class HelloWorldResponse(BaseModel):
    reply: str

def hello_world(prompt):
    return "reply: hello world"

@app.post("/hello_world", name="Hello World", description="Reply always with hello world", response_model=HelloWorldResponse)
def hello_world_api(commons: HelloWorldParams):
    return hello_world(commons.message)

3. Describe Your Tools

Make sure to provide as much description as possible for your FastAPI endpoints. This will help the AI agent understand your tools better.

4. Connecting to Relevance AI

To connect your app to Relevance AI, simply add the following lines:

from relevanceai.connect.fastapi import connect_fastapi_to_rai
connect_fastapi_to_rai(app.routes, PUBLIC_URL)

Replace PUBLIC_URL with your app’s public URL. If you are developing locally, use ngrok to create a public URL:

from pyngrok import ngrok
PUBLIC_URL = ngrok.connect(8000).public_url

Full Code Copy and Paste

Here’s the complete code block for your FastAPI app:

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi import APIRouter, Query
from pydantic import BaseModel
from typing import List
from pyngrok import ngrok
from relevanceai.connect.fastapi import connect_fastapi_to_rai

# Create FastAPI app
app = FastAPI()

# Add CORS middleware to allow all origins
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

class HelloWorldParams(BaseModel):
    message: str = Query(..., description="Message from user")

class HelloWorldResponse(BaseModel):
    reply: str

def hello_world(prompt):
    return "reply: hello world"

@app.post("/hello_world", name="Hello World", description="Reply always with hello world", response_model=HelloWorldResponse)
def hello_world_api(commons: HelloWorldParams):
    return hello_world(commons.message)

# If you are deploying the API from a local computer use ngrok to expose a public URL.
PUBLIC_URL = ngrok.connect(8000).public_url

# This will create a Tool in Relevance AI that will call your API endpoint
connect_fastapi_to_rai(app.routes, PUBLIC_URL)

Troubleshooting

If you encounter issues while setting up, consider these common troubleshooting tips:

  • Ensure all packages are correctly installed by running the installation commands again.
  • Check your FastAPI app for correct syntax and routing.
  • If ngrok is not working, make sure it is installed and running properly on your selected port.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Final Thoughts

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Now you’re ready to start building your AI workforce! Enjoy the journey with Relevance AI.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox