In just 10 minutes on Alibaba Cloud, you can add an AI assistant to your website to respond to customer inquiries 24/7, enhance user experience, and strengthen business competitiveness.
Solution overview
Adding an AI assistant to your website requires only 4 steps:
Create a large model Q&A application: First, we will create a large model application through Model Studio and obtain the credentials for calling the large model application API.
Build a sample website: Then we will use Function Compute to quickly build a website that simulates your company website or other sites.
Integrate the AI assistant: Next, we will modify a few lines of code to integrate an AI assistant into the website.
Add private knowledge: Finally, we can prepare some private knowledge to enable the AI assistant to answer questions that it couldn't accurately answer before, helping you better respond to customer inquiries.
1. Create a large model Q&A application
First, we can create a Model Studio application to obtain the large model inference API service for implementing the AI assistant.
The free quota for new users provided by Model Studio can fully cover the resource consumption required for this tutorial. After the quota is consumed, billing is based on tokens, which can significantly reduce initial investment costs compared to deploying large models yourself.
1.1 Create an application
Go to the Application List in the Model Studio console, and click Create Application on the right side of the page. In the dialog box, select Agent Application and click Create Now.
On the Application Configuration page, select Qwen-Plus as the model and keep other parameters as default.
You can also enter some prompts, such as setting up a persona to guide the large model to better respond to customer inquiries.
Your name is Xiao Zhu, and you can help users answer questions about product selection, usage, and other aspects.
You can ask questions on the right side of the page to verify the model's effectiveness. However, you will find that it cannot accurately answer information about your company's products yet. Click Publish in the upper right corner, and we will solve this problem in later steps.
1.2 Obtain the application ID and API-key required for API calls
To call the large model application capabilities through the API in subsequent steps, we need to obtain an API-KEY and application ID from Model Studio:
You can view all Model Studio application IDs in the Application List. Save the application ID locally for later configuration.
On the My API-KEY page, click Create My API-KEY on the right side. Create a new API-KEY in the pop-up window. Save the API-KEY locally for later configuration.
2. Build a sample website
Before making the AI assistant able to answer questions accurately, we can first try to quickly integrate the AI assistant into the website.
You can use our pre-prepared application template to quickly build a blank sample website to simulate your company website or other sites. The detailed steps are as follows:
The free trial quota provided by Function Compute can fully cover the resource consumption required for this tutorial. After the quota is consumed, pay-as-you-go billing applies, and for the web services involved in this tutorial, fees are only incurred when there are visits.
2.1 Create an application
Please click here to open our Function Compute application template.
For first-time users, you need to follow the guide to activate the Function Compute service.
Select Direct Deployment.
In Model Studio Application ID and Model Studio API-KEY, fill in the Model Studio application ID and API-KEY obtained in 1.2 Obtain the application ID and API-Key required for API calls.
Please correctly fill in the Model Studio application ID and API-KEY, because our pre-configured application template includes code for calling large models through Model Studio applications, allowing you to quickly complete the experience in subsequent steps.
Keep other form items as default, click Create and Deploy Default Environment at the bottom left of the page, and wait for the project deployment to complete (estimated to take 1 minute).
If the deployment fails with an error code "UnclassifiedError", it may be because the Model Studio application ID and Model Studio API-KEY you entered are incorrect. Please delete the application and recreate it following the steps above.
If you are using the Alibaba Cloud Function Compute service for the first time, you need to follow the page guide to authorize the "AliyunFCServerlessDevsRole" role with the required permissions.
2.2 Access the website
After the application is deployed, you can find the access domain name of the sample website in the Environment Information of the application details. Click it to view and confirm that the sample website has been successfully deployed.
The website provided in this solution is a sample website. The content details of the website only display styles, as shown in the following figure.
3. Add an AI assistant to the website
Adding an AI assistant to your website is very simple. You only need to insert a few lines of code into the HTML file of your website.
3.1 Add AI assistant related code
The sample project includes commented code for integrating the AI assistant. You need to find and uncomment it. The detailed operation steps are as follows:
Return to the Application Details page, find Function Resources at the bottom of the Environment Details, click the function name to enter the Function Details page.
In the Code view, find the
index.js
file and change the value ofhostname
todashscope-intl.aliyuncs.com
.In the Code view, find the
public/index.html
file, then uncomment the code at position ③.Finally, click Deploy Code and wait for the deployment to complete.
3.2 Verify the AI assistant on the website
Now, you can revisit the sample website page to see the latest effect. At this point, you will find that an AI assistant icon appears in the lower right corner of the website. Click it to bring up the AI assistant.
The website provided in this solution is a sample website. The content details of the website only display styles, as shown in the following figure.
4. Add private knowledge to the AI assistant
Through the previous steps, you already have an AI assistant that can chat with customers. However, if you want the AI assistant to answer product-related questions more accurately and professionally like a company employee, we need to configure a knowledge base for the large model application.
Suppose you work for a company that sells smartphones. Your website will have a lot of information related to smartphones, such as support for Dual SIM Dual Standby, screen, battery capacity, memory, and other information. For detailed configuration lists of different models, refer to: Model Studio Series Smartphone Product Introduction.docx.
4.1 Configure the knowledge base
Next, we can try to make the large model reference this document when facing customer questions to produce a more accurate answer and suggestion.
Upload files: In the Model Studio console, click Unstructured Data tab and click Import Data. Follow the guide to upload our fictional Model Studio series smartphone product introduction:
Depending on the size of the document you upload, Model Studio needs some time to parse it, usually taking 1-6 minutes. Please be patient.
Create a knowledge base: Visit the Knowledge Base page, follow the guide to create a new knowledge base, and select the file you just uploaded. Keep other parameters as default. Subsequently, the large model can retrieve and reference documents in the knowledge base when answering.
When selecting the vector storage type, if you want to centrally store and flexibly manage vector data for multiple applications, you can choose ADB-PG.
Reference knowledge: After creating the knowledge base, you can return to the Application List and enter the application settings interface you created earlier. Turn on the Knowledge Base switch, click +Knowledge Base to select the target knowledge base, test to verify it meets expectations, and then click Publish. A section of information will be automatically added to the Prompt to allow the large model to reference the retrieved information in subsequent answers.
4.2 Verify the effect
With reference knowledge, the AI assistant can now accurately answer questions about your company's products.
Summary
Through the previous learning, you can now build a large model RAG application for free (within the free usage quota) in 10 minutes and add it to your website as an AI assistant to respond to customer inquiries.
Application in production environment
Before formally introducing the AI assistant into your production environment, it is recommended that you understand the following information:
Frontend code
The website AI assistant created earlier is based on NLUX (a frontend library for developing large model chatbots) and has relatively simple functionality.
If you have more customization needs for the AI assistant, such as adjusting styles, supporting historical session management, etc., you can refer to the NLUX open source project for custom development.
The AI assistant component frontend source code for this document is obtained from the web-chatbot-ui repository.
Server-side code
The sample website code created earlier includes an interface POST /chat
that calls the large model to get answers. The specific implementation code is in the file index.js
.
Function Compute applications do not incur any fees when there are no visits. You can completely retain this function application for future use as a forwarding service for calling large models.
If you want to implement the server-side in your own service using Java and Python SDKs, you can refer to the following code:
package com.aliyun.webdemo.controller;
import com.alibaba.dashscope.app.Application;
import com.alibaba.dashscope.app.ApplicationParam;
import com.alibaba.dashscope.app.ApplicationResult;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import io.reactivex.Flowable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter;
import java.util.concurrent.atomic.AtomicInteger;
import com.alibaba.dashscope.utils.Constants;
/**
* pom dependency configuration
* <dependency>
* <groupId>com.alibaba</groupId>
* <artifactId>dashscope-sdk-java</artifactId>
* <!-- Please replace 'the-latest-version' with the latest version number found at: https://mvnrepository.com/artifact/com.alibaba/dashscope-sdk-java -->
* <version>the-latest-version</version>
* </dependency>
*
* Configure API Key to environment variables https://www.alibabacloud.com/help/zh/model-studio/developer-reference/configure-api-key-through-environment-variables
*/
@RestController
@RequestMapping("/chatbot")
public class ChatbotController {
static {
Constants.baseHttpApiUrl="https://dashscope-intl.aliyuncs.com/api/v1";
}
/**
* Implement chat interface, supporting streaming data return
*
* @param query
* @return
*/
@RequestMapping(value = "/chat", method = RequestMethod.POST)
public ResponseBodyEmitter streamData(@RequestBody String query) {
ResponseBodyEmitter emitter = new ResponseBodyEmitter(180000L);
JsonObject jsonObject = new JsonParser().parse(query).getAsJsonObject();
try {
streamCall(emitter, jsonObject.get("prompt").getAsString());
} catch (NoApiKeyException | InputRequiredException e) {
e.printStackTrace();
}
return emitter;
}
/**
* Call Model Studio application, encapsulate streaming return data
* Return data format
* id:1
* event:result
* :HTTP_STATUS/200
* data:{"output":{"session_id":"xxx","finish_reason":"null","text":"Related question"}}
*
* @param emitter
* @param query
* @throws NoApiKeyException
* @throws InputRequiredException
*/
public void streamCall(ResponseBodyEmitter emitter, String query) throws NoApiKeyException, InputRequiredException {
// Fill in Model Studio application ID
ApplicationParam param = ApplicationParam.builder()
.appId("xxx")
.prompt(query)
.incrementalOutput(true)
.build();
Application application = new Application();
Flowable<ApplicationResult> result = application.streamCall(param);
AtomicInteger counter = new AtomicInteger(0);
result.blockingForEach(data -> {
int newValue = counter.incrementAndGet();
String resData = "id:" + newValue + "\nevent:result\n:HTTP_STATUS/200\ndata:" + new Gson().toJson(data) + "\n\n";
emitter.send(resData.getBytes(java.nio.charset.StandardCharsets.UTF_8));
if ("stop".equals(data.getOutput().getFinishReason())) {
emitter.complete();
}
});
}
}
from flask import Flask, request, render_template, Response
from http import HTTPStatus
from dashscope import Application
import json
import dashscope
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
app = Flask(__name__)
@app.route("/")
def home(path=""):
"""
Homepage
"""
return render_template("index.html")
def call_with_stream(query):
"""
Stream call to Model Studio API
Need to configure API Key to environment variables
https://www.alibabacloud.com/help/zh/model-studio/developer-reference/configure-api-key-through-environment-variables
Return data format
id:1
event:result
:HTTP_STATUS/200
data:{"output":{"session_id":"xxx","finish_reason":"null","text":"Related question"}}
"""
# Fill in Model Studio application ID
responses = Application.call(
app_id="xxx",
prompt=query,
stream=True,
incremental_output=True,
)
i = 0
for response in responses:
if response.status_code != HTTPStatus.OK:
print(
"request_id=%s\n output=%s\n usage=%s\n"
% (response.request_id, response.output, response.usage)
)
else:
data = (
"id:"
+ str(i)
+ "\nevent:result\n:HTTP_STATUS/200\ndata:"
+ json.stringfy(response)
+ "\n\n"
)
yield data
@app.route("/chat", methods=["POST"])
def create():
raw_data = request.data
data = json.loads(raw_data.decode("utf-8"))
return Response(
call_with_stream(data["prompt"]), mimetype="application/octet-stream"
)
if __name__ == "__main__":
app.run(debug=True, port=8080)
<?php
// Start a new session or resume an existing one
session_start();
// Exit if not a request
if(!isset($_SERVER['REQUEST_METHOD'])){
exit;
}
// Initialize homepage
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
home();
}
// AI assistant post request
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
chat();
}
/**
* Display homepage
* Please refer to the frontend code above for homepage content
* Note: When testing, please change the endpoint in the frontend code to "/"
*/
function home() {
// HTML file path
$file = 'home.html';
if (file_exists($file)) {
// File exists, display content
header('Content-Type: text/html');
readfile($file);
} else {
// File does not exist notification
echo "404 Not Found. The file does not exist.";
}
}
/**
* AI assistant post request
*/
function chat() {
/**
* Set request URL and Model Studio API-KEY, replace <APP_ID> with Model Studio application ID
*/
$url = 'https://dashscope-intl.aliyuncs.com/api/v1/apps/<APP_ID>/completion'; // Request URL
$apiKey = 'sk-xxx';
/**
* Set request headers
*/
$headers = [ // Request headers array
'Authorization: Bearer ' . $apiKey, // Authentication header
'Content-Type: application/json', // Content type
'X-DashScope-SSE: enable' // SSE enable flag
];
// Initialize prompt as empty string
$prompt = '';
// Get input stream content
$json = file_get_contents('php://input');
// Try to convert JSON to associative array
$data = json_decode($json, true);
// Check if JSON parsing was successful
if (json_last_error() === JSON_ERROR_NONE) {
$prompt = $data['prompt'] ?? null; // Extract prompt field value
}
/**
* Build request body
*/
$requestBody = [ // Request body array
'input' => ['prompt' => $prompt], // Input item
'parameters' => ['incremental_output' => true], // Parameter settings
'debug' => new stdClass(), // Debug option (empty object)
];
// Stream call to Model Studio API
call_with_stream($url, $headers, $requestBody);
}
// Stream call to Model Studio API
function call_with_stream($url, $headers, $requestBody){
/**
* Initialize cURL session and set related properties
*/
$ch = curl_init();
curl_setopt_array($ch, [ // Set cURL options
CURLOPT_URL => $url, // Target URL
CURLOPT_POST => true, // Use POST method
CURLOPT_POSTFIELDS => json_encode($requestBody), // POST data
CURLOPT_HTTPHEADER => $headers, // Request headers
CURLOPT_WRITEFUNCTION => function ($curl, $data) { // Custom write callback
echo $data; // Output data
ob_flush(); // Flush output buffer
flush(); // Flush all output buffers
return strlen($data); // Return data length
},
CURLOPT_BUFFERSIZE => 128, // Buffer size
CURLOPT_TIMEOUT => 300, // Timeout
CURLOPT_RETURNTRANSFER => true, // Whether to return transfer data
CURLOPT_HEADER => false // Do not include HTTP headers
]);
/**
* Execute cURL session and get response
*/
$response = curl_exec($ch);
/**
* Error handling and response output
*/
if (curl_errno($ch)) { // Check if cURL error exists
http_response_code(500); // Set HTTP response status code to 500
echo 'Curl error: ' . curl_error($ch); // Output error information
} else {
echo $response; // Output response result
}
curl_close($ch); // Close cURL session
}
?>
The **.devsapp.net domain name attached to the Function Compute application deployment will be reclaimed within 30 days after issuance and does not support HTTPS access, making it only suitable for testing and verification. If you want to directly call the POST /chat
interface deployed in Function Compute on your website, it is recommended to use the domain name provided in the Function Compute HTTP trigger, such as: https://web-chat****.fcapp.run/chat
. At the same time, it is recommended that you modify the CORS configuration in index.js
to prohibit access to this interface from other sites.
Continuous improvement
Large model courses
There is never an endpoint for improving system experience. You can consider learning and obtaining the Alibaba Cloud Large Model ACA Certification. The free courses accompanying this certification can help you further understand the capabilities and application scenarios of large models, along with how to optimize the effectiveness of large model applications.