Build a streaming chat with AI Inference
Build a chat page whose answers stream from a model on AI Inference, through a Function, an Application, and a Workload.
In this tutorial, you will build a chat page whose answers stream from a model on AI Inference. You will write a Function that calls the model, publish it through an Application and a Workload, and send it a question.
Every resource the tutorial creates links to one created before it:
- The Function holds the code: a chat page, and an endpoint that sends each question to the model.
- The Application has Functions turned on, a function instance that attaches the Function, and a request rule that runs it on every path.
- The Workload holds the domain that receives requests, and its deployment sets the Application that answers them.
The resources are the same in Azion Console, the Azion CLI, and the API. Select the interface you use, and every stage shows its steps.
Prerequisites
Console
- An Azion account with access to AI Inference. On the Hobby and Pro plans, access is available upon request, subject to approval. For more information, refer to Access by plan.
- Access to Azion Console. To sign in, refer to Access Azion Console.
curl, to send a question to the chat endpoint.
1. Write the Function
Create a directory for the tutorial, go to it, and create an index.js file with this code:
The Function answers two routes:
GET /returns the chat page. The page sends the question to/api/chatand writes the answer as it arrives.POST /api/chatcalls the model withstream: true.Azion.AI.runthen resolves to an async iterator of chunks, and the Function writes the text of each chunk,choices[0].delta.content, to the response. For every field of the request and the chunks, refer to Model invocation.
When Azion.AI.run raises an error, the Function answers 502 with the error message, so the reason reaches the browser. A request with no question, or with a body that is not JSON, answers 400 and runs no model.
2. Create the Function
Console
To create the Function in Azion Console:
- Access Azion Console > Functions.
- Select + Function.
- In Name, enter
ai-chat. - Keep Application selected in Execution Environment.
- Select the Code tab.
- Replace the sample code in the editor with the content of
index.js. - Select Save.
The Console shows the message Your function has been created, and ai-chat appears in the list of Functions.
The account holds the Function. It runs only after an Application attaches it.
3. Create the Application
The Application needs three parts: the Application itself with Functions turned on, a function instance that attaches the Function, and a rule that runs the instance.
Console
To create the Application:
- Access Azion Console > Applications.
- Select + Application.
- In Name, enter
ai-chat. - In Modules, turn on Functions.
- Select Create.
The Console opens the new Application on its Main Settings tab.
To attach the Function:
- Select the Functions Instances tab.
- Select + Function.
- In Name, enter
ai-chat. - In Function, select
ai-chat. - Select Save.
The instance appears in the Functions Instances tab.
To run the Function on every request:
- Select the Rules Engine tab.
- Select + Rule.
- In Name, enter
Run ai-chat. - Keep Request Phase selected.
- In Criteria, keep
${uri}and starts with, and enter/as the argument. - In Behaviors, replace Deliver with Run Function.
- In the function instance field that Run Function adds, select the
ai-chatinstance. - Select Save.
The rule appears in the request phase of the Rules Engine tab.
The Application runs the Function on every request it receives.
4. Create the Workload
The Workload holds the domain, and its deployment sets the Application that answers its requests.
Console
To create the Workload and its deployment:
- Access Azion Console > Workloads.
- Select + Workload.
- In Name, enter
ai-chat. - In Deployment Settings, select
ai-chatin Application. - Select Save.
The Console shows the message Your Workload has been created. The list of Workloads shows the domain in the Workload Domain column, in the format xxxxxxxxxxx.map.azionedge.net.
Your domain has the same format as the examples, with different letters.
5. Chat with the model
A new Workload can take about ten minutes to propagate across the Azion network. Until then, the domain answers 404 with an HTML error page, and it can alternate between 404 and the Function’s response for a few minutes.
Open https://<your-domain>/ in a browser, where <your-domain> is the domain of your Workload. When the page shows the Ask a question field, the Function serves your domain.
Send a question to the chat endpoint. If you used the Console, set DOMAIN to your domain first, with DOMAIN=<your-domain>:
The response is the model’s answer as plain text, in three sentences or fewer. --no-buffer prints each part of the text as it reaches the terminal. In the browser, enter the same question in Ask a question and select Send: the page writes the answer below the form.
When the account has no access to AI Inference, the endpoint answers 502 with the error that Azion.AI.run raised:
To request access, refer to Access by plan. The same message also appears when the model ID is wrong; for the causes, refer to AI Inference troubleshooting.
6. (Optional) Delete the resources
The chat keeps running on your domain after the tutorial, and it answers anyone who reaches the domain. Every call is billed in Compute Time and Requests. To keep the chat, add a check before the model call, as described in Check each request in the Function before it calls a model.
If you want to remove the resources, delete them in the reverse order of their creation. Deleting the Workload also deletes its deployment, and deleting the Application also deletes its instance and its rule.
Console
To delete the Workload:
- Access Azion Console > Workloads.
- In the
ai-chatrow, open the actions menu and select Delete. - In the Delete Workload dialog, enter
ai-chat. - Select Delete.
Repeat the steps in Applications for the ai-chat Application, then in Functions for the ai-chat Function. Each dialog asks for the name of the resource.
The three resources no longer appear in their lists.
The account no longer holds the Function, the Application, or the Workload. The removal also propagates, so the domain can keep answering for a few minutes.