exports.handler = async (event) => {
const userMessage = JSON.parse(event.body).message;
const response = await getChatbotResponse(userMessage);
return {
statusCode: 200,
body: JSON.stringify({ response }),
};
};
const getChatbotResponse = async (message) => {
const apiUrl = 'https://api.example.com/chatbot'; // Replace with your AI model endpoint
const apiKey = 'your-api-key'; // Replace with your API key
try {
const response = await axios.post(apiUrl, { message }, {
headers: { 'Authorization': Bearer ${apiKey} }, }); return response.data.reply; } catch (error) { console.error('Error fetching response:', error); return 'Sorry, there was an error processing your request.'; } };
  1. 部署該函數(shù)并記下提供的端點 URL。

設(shè)置 API 網(wǎng)關(guān)

  1. 在 AWS 管理控制臺中導(dǎo)航到API 網(wǎng)關(guān)。
  2. 創(chuàng)建一個新的 API 并將其鏈接到您的 Lambda 函數(shù)。
  3. 部署 API 以使其可通過互聯(lián)網(wǎng)訪問。

構(gòu)建前端界面

創(chuàng)建 HTML 和 JavaScript

創(chuàng)建一個新的 HTML 文件,例如index.html,使用以下代碼:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AI Chatbot</title>
<style>
#chat {
max-width: 600px;
margin: 0 auto;
padding: 20px;
border: 1px solid #ccc;
}
#messages {
height: 300px;
overflow-y: scroll;
border: 1px solid #ccc;
padding: 10px;
}
#input {
width: calc(100% - 22px);
padding: 10px;
margin: 10px 0;
}
</style>
</head>
<body>
<div id="chat">
<div id="messages"></div>
<input type="text" id="input" placeholder="Type a message" />
</div>
<script>
const input = document.getElementById('input');
const messages = document.getElementById('messages');
input.addEventListener('keypress', async (e) => {
if (e.key === 'Enter') {
const message = input.value;
input.value = '';
appendMessage('You', message);
const response = await fetch('https://your-api-endpoint.com', { // Replace with your API Gateway endpoint
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ message }),
});
const data = await response.json();
appendMessage('Chatbot', data.response);

}

});

function appendMessage(sender, text) {

const messageElem = document.createElement('div');

messageElem.textContent = ${sender}: ${text}; messages.appendChild(messageElem); } </script> </body> </html>


測試前端

  1. index.html在 Web 瀏覽器中打開該文件。
  2. 在輸入字段中輸入消息,然后按 Enter 鍵即可查看聊天機器人的響應(yīng)。

4.部署和擴展

托管前端

您可以index.html在任何網(wǎng)絡(luò)托管服務(wù)上托管該文件,甚至可以使用 GitHub Pages 進(jìn)行快速部署。

監(jiān)控和擴展

  1. 通過 AWS CloudWatch監(jiān)控您的 Lambda 函數(shù)和 API 網(wǎng)關(guān)使用情況。
  2. 通過調(diào)整 API 網(wǎng)關(guān)和 Lambda 函數(shù)設(shè)置來根據(jù)需要擴展服務(wù)。

結(jié)論

通過本教程,您已經(jīng)構(gòu)建了一個實時 AI 聊天機器人并使用無服務(wù)器架構(gòu)進(jìn)行了部署。這種方法為部署 AI 驅(qū)動的服務(wù)提供了一種可擴展且經(jīng)濟高效的解決方案,而 AWS Lambda 和簡單前端的組合則展示了如何將現(xiàn)代技術(shù)用于實際應(yīng)用。

通過集成更高級的 AI 模型或增強用戶界面,您可以進(jìn)一步自定義聊天機器人。祝您編碼愉快!

原文鏈接:https://dzone.com/articles/real-time-ai-powered-chatbot-with-serverless-architecture

上一篇:

業(yè)務(wù)風(fēng)險情報的最大化應(yīng)用

下一篇:

百度文心一言API使用指南:非技術(shù)人員入門教程
#你可能也喜歡這些API文章!

我們有何不同?

API服務(wù)商零注冊

多API并行試用

數(shù)據(jù)驅(qū)動選型,提升決策效率

查看全部API→
??

熱門場景實測,選對API

#AI文本生成大模型API

對比大模型API的內(nèi)容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉(zhuǎn)化潛力

25個渠道
一鍵對比試用API 限時免費

#AI深度推理大模型API

對比大模型API的邏輯推理準(zhǔn)確性、分析深度、可視化建議合理性

10個渠道
一鍵對比試用API 限時免費