October 10, 2023

GET API tool

We’ve added support for a tool that can make GET calls to an external API.

This can be used to dynamically retrieve context for your prompts. For example, you may wish to get additional information about a user from your system based on their ID, or look up additional information based on a query from a user.

To set up the tool you need to provide the following details for your API:

Tool parameterDescriptionExample
NameA unique tool name to reference as a call signature in your promptsget_api_tool
URLThe URL for your API endpointhttps://your-api.your-domain.com
API Key HeaderThe authentication header required by your endpoint.X-API-KEY
API KeyThe API key value to use in the authentication header.sk_1234567891011121314
Query parametersA comma delimited list of the query parameters to set when making requests.user_query, client_id

Define your API

First you will need to define your API. For demo purposes, we will create a mock endpoint in postman. Our mock endpoint simply returns details about a mock user given their user_id.

A call to our Mock API in Python is as follows; note the query parameter user_id

1import requests
2
3url = "https://01a02b84-08c5-4e53-b283-a8c2beef331c.mock.pstmn.io/users?user_id=01234567891011"
4headers = {
5 'X-API-KEY': '<API KEY VALUE>'
6}
7response = requests.request("GET", url, headers=headers)
8print(response.text)

And returns the response:

1{
2 "user_id", "012345678910",
3 "name": "Albert",
4 "company": "Humanloop",
5 "role": "Engineer"
6}

We can now use this tool to inject information for a given user into our prompts.

Set up the tool

Navigate to the tools tab in your organisation and select the Get API Call tool card:

Configure the tool with your API details:

Use the tool

Now your API tool is set up, you can use it to populate input variables in your prompt templates. Double curly bracket syntax is used to call a tool in the template. The call signature is the unique tool name with arguments for the query parameters defined when the tool was set up.

In our mock example, the signature will be: get_user_api(user_id).

An example prompt template using this tool is:

$You are a helpful assistant. Please draft an example job role summary for the following user:
>
>User details: {{ get_user_api(user_id) }}
>Keep it short and concise.

The tool requires an input value to be provided for user_id. In our playground environment the result of the tool will be shown populated top right above the chat:

What’s next

Explore more complex examples of context stuffing such as defining your own custom RAG service.