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
2. Log in to Relevance AI
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:
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()
Here is an example in the context of FastAPI code.
If you are working off a local computer, use ngrok to get a public url. pip install pyngrok
Add nest_asyncio
if you are in a notebook environment. pip install nest-asyncio
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.
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.