Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Get your server authorized to contact nutrition advisor AI
Your api key should never be visible to customers. Your backend service which you are going to enhance with the advisor should be the only server ever requesting a token.
It is your responsibility to keep your API key safe.
If you make this request on your client side, they will be able to see your API key in the headers, and abuse it.
In order to obtain your token for the API, you will need to send a request with your API key to our token service.
For example:
https://api.passiolife.com/v2/token-cache/nutrition-advisor/oauth/token/1i5eXnFpiRfiBGgLibonnBg10Ct14nALTAK5Jb6B4V4o
The key values to note in the response are the access_token
, customer_id
, and the expires_in
fields.
The token you receive from our service will have a time-stamp of its expiry. This is the seconds until this token expires from the time you obtained it.
Tokens can refresh before the expiration for various reasons, so token-reloads on 403 responses should be handled in your API if necessary.
Recommended Implementation:
Put your license key into your applications env using injection, and ideally a secret manager
Create a wrapper, or preflight check when sending requests to Nutrition Advisor such that:
If there is no current token, obtain one as documented below
Anytime you collect a token, note the time it was gathered. The sum of the timestamp
of receiving the response, and the expires_in
duration is when your token will expire.
Send new requests to refresh token as needed
apply the required headers as noted below
All incoming requests require that you have your Passio identifier in the request headers. Your customer id is returned with the token request once your API key has been validated, and can be pulled from the response to apply to your headers.
This is used to track usage and identify leaks. Any requests coming in with no identifier, or an invalid identifier will be rejected.
Authorization Header: you must provide your access_token
in an authorization header with the prefix Bearer
for example:
{"Authorization": "Bearer <access_token here>"}
Passio Identification Header: you must provide your passio customer_id
(either as returned with the token or as seen on your product dashboard) in a Passio-ID
header
Continue to API Request Setup to see code examples on making requests to Nutrition Advisor
Code examples on how to set up your requests after retrieving a token
These examples assume you have followed the steps from #requesting-the-token and have received a response from that route.
Welcome to the documentation for interacting with Passio's Nutrition Advisor AI.
To begin, you will need your API key. Once you have subscribed for api access, your product dashboard in our accounts portal (https://accounts.passiolife.com) will contain your license key, as well as your customer ID.
The customer ID is provided in the portal for convenience, though it is also returned with your token when requesting access. You can choose to hardcode it somewhere in your app, or just pull it out from the token response.
A brief overview of the conversational flow
This guide provides a concise overview of how to engage in a conversation with the Nutrition Advisor using the API. It includes code examples in Python to help you get started quickly.
This guide assumes you have read and executed the Authorization Process and Request Setup. All requests in code examples pass an undefined headers
which you can quickly build following the steps in the above guides.
All responses from the Nutrition Advisor will be returned in the AdvisorResponse
object format:
threadId
string
The unique identifier of the conversation thread.
messageId
string
The unique identifier of the advisor's response message.
content
string
The main content of the advisor's response. Empty if message has a dataRequest
or actionResponse
. Should be JSON decoded from string when received.
contentToolHints
[]string
An array of tool names (or empty) that the advisor has sensed could be useful to run on this response.
dataRequest
AdvisorDataRequest
If the advisor requires additional information, this field contains the details of the data request.
actionResponse
AdvisorActionResponse
If the advisor performs an action based on the message, this field contains the details of the action response.
usage
AdvisorResponseUsage
Information about the token usage for the response.
To list the available tools, make a GET request to /v2/products/nutrition-advisor/tools
. The response will include an array of tool objects with the following fields:
name
string
The unique name of the tool.
displayName
string
The display name of the tool.
description
string
A brief description of what the tool does.
type
enum
The type of the tool: target
, inputsense
, or vision
.
To start a new conversation, make a POST request to /v2/products/nutrition-advisor/threads
. You can optionally include the ?plainText=true
query parameter to receive plain text responses without markdown formatting.
When you receive an AdvisorResponse
back from the Nutrition Advisor, follow these steps:
content
is non-empty (Lines 2-17)If so, parse contentToolHints
.
If any tools are found, match them to the name
field of a cached available tool.
Use a switch statement based on the tool name to handle specific cases.
For example, if the tool name is "SearchIngredientMatches", make a call to the target
tool type using the message.
dataRequest
is not null (Lines 19-40)If not null, parse the name
field and use a switch statement to handle specific cases.
For example, if the tool name is "DetectMealLogsRequired" and the parameters
field contains daysBack: 1
, respond to the data request with the appropriate meal log data.
actionResponse
is not null (Lines 41-48)If not null, parse the name
field and use a switch statement to handle specific cases.
For example, if the tool name is "SearchIngredientMatches", parse the data
field as JSON to obtain an array of ingredient data.
An overview of interacting with the Advisor
After following authorization steps to receive your token, you can begin to interact with the advisor.
Once you have your API client authorized, you will need to begin a conversation.
By default, responses will come with markdown style formatting. You can pass the query param
?plainText=true
to this route in order to receive plain text, with no markdown formatting.
The React Demo Project for this uses ReactMarkdown package to display the messages with markdown.
Now you can begin the conversation. The content of message
can be any serializable type.
The advisor has 2 types of input sensing intelligence. These are designed to help you reduce token usage, by only suggesting actions or requesting data added to the conversation when it is deemed necessary.
The AdvisorResponse
model contains a field called contentToolHints
.
Whenever you receive a message (ie, non-tool related) AdvisorResponse
the content
field will contain the message. the contentToolHints
will provide an array of tool names that could be useful to run on this message, at your discretion.
For Example
User sends: generate me a recipe using salmon and asparagus for dinner
The advisor will respond with a recipe for the user. Logically, this recipe is going to contain references to some fruits, veggies, meats, herbs, etc. Because there is reference to food items the response will contain: contentToolHints: ['SearchIngredientMatches']
This informs you that this particular tool may be worth running on the message.
In this case, it lets you know there are food items in the message that could be linked to Passio's Nutrition Database. In the Nutrition Mobile SDK Integration, this hint might prompt the UI displaying an action to "Log this meal", which would then run the extraction, link ingredients, and create a meal log for the user.
You can pass the name of Tools of type inputsense
(currently limit of 1 per message) to run certain actions if they are detected as necessary. For instance, if DetectMealLogsRequired
is given as an inputSensors
and the users message was something like:
check my last 3 days of eating and tell me what I can improve
In this case you would receive a response containing a DataRequest
with some info you should provide. In this case it would tell you it needs 3 days of the users meal log data.
This design prevents having to include all the users meal logs at all times in the conversation history, to save you token usage costs (as every token in a given conversation contributes to the cost of all future messages in that conversation).
The AdvisorResponse
object represents the response received from the Nutrition Advisor API. It contains information about the conversation thread, the advisor's message, and any additional data or actions related to the response.
threadId
(string): The unique identifier of the conversation thread.
messageId
(string): The unique identifier of the advisor's response message.
content
(string): The main content of the advisor's response. Empty if message has a dataRequest
or actionResponse
. Should be JSON Decoded from string when received.
contentToolHints
([]string): An array of tool names (or empty) that the advisor has sensed could be useful to run on this response. See Content Tool Hints
dataRequest
(AdvisorDataRequest, optional): If the advisor requires additional information, this field contains the details of the data request.
actionResponse
(AdvisorActionResponse, optional): If the advisor performs an action based on the message, this field contains the details of the action response.
usage
(AdvisorResponseUsage, optional): Information about the token usage for the response.
The AdvisorDataRequest
object represents a data request from the advisor when additional information is needed to provide a complete response. This can be triggered on Sending Messages, if related parameters are enabled.
To respond to a DataRequest
RespondParameters
will return a JSON encoded string of a key/value object containing information on the data being requested. For instance, when detecting meal logs you might receive:
{"daysBack": 3}
To give you some idea of how many logs to supply.
The AdvisorActionResponse
object represents an action performed by the advisor based on the message. This will be received after submitting a Tool Run.
The data
field will contain a JSON encoded string of the objects returned by the tool.
The AdvisorResponseUsage
object provides information about the token usage for the advisor's response.
Note: The InputTokens
and OutputTokens
fields are not included in the JSON representation of the AdvisorResponseUsage
object.
This route will list the tools available for you to run, and differentiate which tools can be enabled for messages going to the advisor (resulting in possible Data Requests) or if they can be executed on messages from the advisor (Tool Executions).
Using the tool name
and type
from the Get Tools route, you can execute a tool on a message.
The different tool type
s have different methods of execution described below.
Tools of type inputsense
can be used with outgoing user messages. Pass the tool name in the inputSenseors
field when sending a user message to enabled this tool. See Sending Messages
Tools of type target
are run on specific messages in the thread history.
These are often used in a Context Menu sense, for instance after asking the Advisor to generate a recipe, once you are happy with the recipe you get, you can set up a right-click action that runs the SearchIngredientMatches
tool on the messageId to link all of the ingredients specified to passios food database.
Tools of type vision
are actions that can be run on images. The image must be sent with the vision tool request, either by URL or a base64-encoded string in the requests image
field.
the currently available tools that can be executed on messages
Our Tools allow integrated actions between our AI Nutritionist and our database behind our and our services.
Target tools can execute some action on an existing message in the conversation history, and return some data related to its content.
See overview of Executing Target Tools
When executed on a message, this tool will find the names of all individual ingredients mentioned within the message, and infer amounts in grams for each.
Each of these food items are then linked to an item from our Nutrition Database and returned in same format as results from a text search with our , with an additional few fields added by a wrapper to link back to your message content.
After execution, the return value can be fetched via the actionResponse
object, in the data
field.
The value will be a JSON encoded string.
You can use the ingredientName
to relate to your original message, as this will be the string from the message that was used to locate the ingredient information.
InputSense tools can be enabled when sending user messages. These help reduce cost by preventing you from needing to provide all data at the start of a conversation.
These tools automatically sense if they will need any additional data based on the content of the users message. If they are necessary, your response will contain a dataRequest
object. You then Respond To The DataRequest, supplying the data requested, and the following response will answer the users question, with the added context of the data supplied.
See Sending User Messages to see how to enable one in a request
If enabled, when a user sends a message, Nutrition Advisor will read their message, and determine if the user has sad anything like review my last 2 days of eating
or what can i improve in my diet
.
These questions imply that the Advisor should have some history on the users eating habits. When detected, rather than receiving an answer immediately, the advisor will respond with a DataRequest
Note if the tool is not enabled, or it didn't think more data was needed, it will respond as usual, and leave dataRequest
null.
If necessary, the dataRequest.respondParameters
field will be a JSON encoded string containing
The provided daysBack
float value gives you an idea of how much data the user was asking to be reviewed, so you can send the appropriate amount.
You can respond with data of any format.
You could send back a json dump, a csv, some plain text saying "i ate an apple", and Advisor will sort out the rest!
The POST body should contain a JSON encoded string of your data (even if the original content is just a string itself).
This tool can be enabled via a query parameter on the route when sending a user message by passing its tool nmae DetectMealLogsRequired
in the inputSensors
of your message request.
Vision tools require an image to be sent with the tool request; either by URL or base64 encoded string.
Depending on the tool run, the Advisor will view your image, infer the requested information, and provide an actionResponse
object containing the resulting data.
See Vision Tools Overview for more information
Nutrition Advisor will look at your image, find all food items/ingredients/drinks etc it can locate, and link them to our nutritional database, returning summary info for each food-item in the image.
Advisor will also supply its best-guess of amount (weight in grams) of each item.
The advisorResponse.data
field will contain a JSON encoded string of an array like the following:
More detailed information on the food item can be retrieved using
Further information, nutrients, etc can be collected for this item via
Using your license key, retreive a token you can use from your backend to call the nutrition advisor API. You must include the customerId returned here in your header as per the auth documentation
the api or license key you were provided in your product dashboard
OK
Show the tool actions available to use. See tool documentation at: https://passio.gitbook.io/nutrition-advisor-api/interaction-guide/overview
OK
Execute an advisor interraction tool on a message. See available tools and documentation at: https://passio.gitbook.io/nutrition-advisor-api/interaction-guide/advisor-tools
Thread ID
Tool name to execute (only tools of type 'target' are valid)
details of the target request
"the id of the message to execute the tool on"
OK
"json-encoded string of the data result of the tool execution. see tool definitions for details https://passio.gitbook.io/nutrition-advisor-api/interaction-guide/advisor-tools"
"the id of the message to execute the tool on"
"the tool name/key that was run"
"json-encoded string of response content. Will be empty if dataReqeust or actionResponse is not null."
"the name (tool key for the route) of the tool or function used. ex: SearchIngredientMatches"
"json-encoded string of the data to respond with"
"the run id of the execution (required for responding)"
"the id of this tool in the execution (required for responding)"
"the base model used under passios tools (for billing clarity)"
Fulfill a data request
Thread ID
Message ID
The data response to fulfill the request
"the run id of the execution (required for responding)"
"the id of this tool in the execution (required for responding)"
OK
"json-encoded string of the data result of the tool execution. see tool definitions for details https://passio.gitbook.io/nutrition-advisor-api/interaction-guide/advisor-tools"
"the id of the message to execute the tool on"
"the tool name/key that was run"
"json-encoded string of response content. Will be empty if dataReqeust or actionResponse is not null."
"the name (tool key for the route) of the tool or function used. ex: SearchIngredientMatches"
"json-encoded string of the data to respond with"
"the run id of the execution (required for responding)"
"the id of this tool in the execution (required for responding)"
"the base model used under passios tools (for billing clarity)"
Add a message to the Nutrition Advisor thread
The ID of the thread thread
The message to add to the thread
OK
"json-encoded string of the data result of the tool execution. see tool definitions for details https://passio.gitbook.io/nutrition-advisor-api/interaction-guide/advisor-tools"
"the id of the message to execute the tool on"
"the tool name/key that was run"
"json-encoded string of response content. Will be empty if dataReqeust or actionResponse is not null."
"the name (tool key for the route) of the tool or function used. ex: SearchIngredientMatches"
"json-encoded string of the data to respond with"
"the run id of the execution (required for responding)"
"the id of this tool in the execution (required for responding)"
"the base model used under passios tools (for billing clarity)"
Create a new thread with the Nutrition Advisor
OK
"json-encoded string of the data result of the tool execution. see tool definitions for details https://passio.gitbook.io/nutrition-advisor-api/interaction-guide/advisor-tools"
"the id of the message to execute the tool on"
"the tool name/key that was run"
"json-encoded string of response content. Will be empty if dataReqeust or actionResponse is not null."
"the name (tool key for the route) of the tool or function used. ex: SearchIngredientMatches"
"json-encoded string of the data to respond with"
"the run id of the execution (required for responding)"
"the id of this tool in the execution (required for responding)"
"the base model used under passios tools (for billing clarity)"
Execute a 'vision' type tool on an image. Inputs and outputs are not stored in conversation history to avoid excessive token usage
Thread ID
Name of the tool to execute (as seen in name field from GET tools)
details of the vision request
"url or base64 encoded image. https://example.com/image.jpg || data:image/jpeg;base64,<Base64ImageData>"
OK
"json-encoded string of the data result of the tool execution. see tool definitions for details https://passio.gitbook.io/nutrition-advisor-api/interaction-guide/advisor-tools"
"the id of the message to execute the tool on"
"the tool name/key that was run"
"json-encoded string of response content. Will be empty if dataReqeust or actionResponse is not null."
"the name (tool key for the route) of the tool or function used. ex: SearchIngredientMatches"
"json-encoded string of the data to respond with"
"the run id of the execution (required for responding)"
"the id of this tool in the execution (required for responding)"
"the base model used under passios tools (for billing clarity)"