Summarize text with AI Inference and cache the summary
Summarize a text with a catalog model on AI Inference from a function, and return the same summary from the Cache API for the same text.
You summarize a text with a catalog model on AI Inference from a function, and store each summary with the Cache API under the SHA-256 of the text, so a repeated text is answered without a model call. To call a chat model without a cache first, refer to Call a model on AI Inference from a function.
Prerequisites
- An application with Application Accelerator turned on, which the Run Function behavior requires. To create one, refer to Applications quickstart.
- The id of the model to call, copied from its page in AI models.
The Cache API is not defined under azion dev, so test the function once it runs on the application.
The examples use /api/summarize for the route, ai-summarize for the function and its instance, and www.example.com for the domain your application answers on. Replace them with your values.
Create the summarization function
The function refuses a missing text with 400 and a text over 20,000 characters with 413 before any model call. It looks the SHA-256 of the text up in the ai-summarize cache, calls the model on a miss, and stores the summary for one day.
Create a function named ai-summarize with this code:
To create the function and its instance, follow Functions quickstart with the name ai-summarize, and name the instance on your application ai-summarize, with no Args.
The application carries an ai-summarize instance that returns a summary for a text, from the cache when the same text was summarized in the last day.
The Add AI features to existing applications use case uses the values of this example.
Run the function on the route
The rule matches /api/summarize exactly, so no other path of your application runs the function, and it runs in the Request Phase, because the function produces the whole response.
Create the rule as Add the rule that runs the function shows, with the name ai - summarize, the criterion ${uri} is equal /api/summarize, and the Run Function behavior selecting the ai-summarize instance.
A POST to /api/summarize on your domain runs the function, and every other path keeps the rules it had. A new rule takes a few minutes to propagate.
Confirm the route returns a summary
To send a text to the route:
The response carries x-summary-cache: miss and a JSON body with summary. Send the same request again: the response carries x-summary-cache: hit and the same summary. Send {"text":""}: the response is 400 with text is required, which the function returns before the model call.
The route returns a summary for a text, and answers the same text from the cache for one day.
These checks confirm the Add AI features to existing applications use case.
In the Add AI features to existing applications use case, expect:
- The route runs only on its path. Any other path of your application answers as it did before the rule existed.