FastAPI to Tool
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:
- Sign up for a free account at https://app.relevanceai.com and then login to your Relevance AI account.
- Install the Relevance AI library in a Python 3 environment
pip install relevanceai
2. Log in to Relevance AI
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’sapp = FastAPI()
from relevanceai.connect.fastapi.tools import connect_tools
connect_tools(app.routes, PUBLIC_URL)
Here is an example in the context of FastAPI code.
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
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
import nest_asyncio
nest_asyncio.apply()
3.2 Defining the endpoint and tool
@app.post(name=..., description=...)
. Thename
anddescription
of the endpoint becomes the samename
anddescription
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.
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:
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)