Connect your FastAPI endpoints as Relevance AI tools for agents.

1. Create an account and install Relevance AI

Before you get started, make sure you create an account and install Relevance AI:

  1. Sign up for a free account at https://app.relevanceai.com and then login to your Relevance AI account.
  2. Install the Relevance AI library in a Python 3 environment
pip install relevanceai

2. Log in to Relevance AI

Python
import relevanceai as rai
rai.login()

You can create a key at the bottom of the page here: https://app.relevanceai.com/login/sdk

instead of rai.login() you can also set environmental variables to automatically login:

SET RELEVANCE_API_KEY=RELEVANCE_API_KEY
SET RELEVANCE_REGION=RELEVANCE_REGION
SET RELEVANCE_PROJECT=RELEVANCE_PROJECT

3. Connect FastAPI

3.1 Connect

The main function to connect your fastapi endpoints to Relevance AI is connect_tools:

  • the PUBLIC_URL is the public url to connect to your fastapi.
  • the app.routes is the FastAPI’s app = FastAPI()
Python
from relevanceai.connect.fastapi.tools import connect_tools
connect_tools(app.routes, PUBLIC_URL)

Here is an example in the context of FastAPI code.

Python
PUBLIC_URL = "https://where_my_api_is_hosted.com/"

from fastapi import FastAPI
app = FastAPI()

#YOUR APP CODE#

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

If you are working off a local computer, use ngrok to get a public url. pip install pyngrok

Python
from fastapi import FastAPI
app = FastAPI()

#YOUR APP CODE#

#add this for ngrok
from pyngrok import ngrok
PUBLIC_URL = ngrok.connect(8000).public_url

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

Add nest_asyncio if you are in a notebook environment. pip install nest-asyncio

Python
import nest_asyncio
nest_asyncio.apply()

3.2 Defining the endpoint and tool

  • @app.post(name=..., description=...). The name and description of the endpoint becomes the same name and description of the Tool. This is crucial when a tool is equipped to an agent since its used in the prompt for agents.
  • message : str = Query(..., description="message from user") The typing and description of endpoint parameters is automatically used when setting the user inputs for the Tool.
Python
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"}

#The name and description below is the Tool name and description.
@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.3 Deploy

Once you deploy your FastAPI endpoint. You should be able to see the end point show up as a Tool in your Tools list. It should also have a “SDK” badge on it.

Example of a full code:

Python
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi import APIRouter, Query
from pydantic import BaseModel
from typing import List

#create FastAPI app
app = FastAPI()
app.add_middleware(
    CORSMiddleware,
    allow_origins=['*'],
    allow_credentials=True,
    allow_methods=['*'],
    allow_headers=['*'],
)

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)

#If you are deploying the api from a local computer use ngrok to expose a public url.
from pyngrok import ngrok
PUBLIC_URL = ngrok.connect(8000).public_url

#This will create a Tool in Relevance AI that will call your API endpoint
from relevanceai.connect.fastapi.tools import connect_tools
connect_tools(app.routes, PUBLIC_URL)