Skip to content

Commit

Permalink
Merge pull request #365 from aws-samples/xuhan-aics
Browse files Browse the repository at this point in the history
feat: chatbot management
  • Loading branch information
IcyKallen committed Aug 28, 2024
2 parents 7a25190 + bd4689e commit 42fd6ea
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 50 deletions.
60 changes: 57 additions & 3 deletions source/lambda/etl/chatbot_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def __create_chatbot(event, group_name):
chatbot_table,
group_name,
chatbot_id,
chatbot_description,
index_id,
index_type,
tag,
Expand Down Expand Up @@ -133,7 +134,18 @@ def __list_chatbot(event, group_name):
page_json = []
for item in page_items:
item_json = {}
item_json["chatbotId"] = item.get("chatbotId", {"S": ""})["S"]
chatbot_id = item.get("chatbotId", {"S": ""})["S"]
item_json["ChatbotId"] = chatbot_id
chatbot_model_item = model_table.get_item(
Key={
"groupName": group_name,
"modelId": f"{chatbot_id}-embedding",
}
).get("Item")
item_json["ModelName"] = chatbot_model_item.get("parameter", {}).get(
"ModelEndpoint", ""
)
item_json["LastModifiedTime"] = item.get("updateTime", {"S": ""})["S"]
page_json.append(item_json)
output["Items"] = page_json
if "LastEvaluatedKey" in page:
Expand All @@ -144,12 +156,54 @@ def __list_chatbot(event, group_name):

output["Config"] = config
output["Count"] = len(page_json)
output = {"chatbot_ids": ["Admin"]}
output["chatbot_ids"] = [item["ChatbotId"] for item in page_json]
return output


def merge_index(chatbot_index_ids, key):
return ",".join(list(chatbot_index_ids.get(key, {}).get("value", {}).values()))


def __get_chatbot(event, group_name):
return {"Message": "Not Implemented"}

chatbot_id = get_query_parameter(event, "chatbotId")

if chatbot_id:
chatbot_item = chatbot_table.get_item(
Key={"groupName": group_name, "chatbotId": chatbot_id}
).get("Item")
else:
chatbot_item = None

if chatbot_item:
chatbot_index_ids = chatbot_item.get("indexIds", {})

response = {
"GroupName": group_name,
"ChatbotId": chatbot_id,
"Chatbot": {
"inention": {
"index": merge_index(chatbot_index_ids, "intention"),
},
"qq": {
"index": merge_index(chatbot_index_ids, "qq"),
},
"qd": {
"index": merge_index(chatbot_index_ids, "qd"),
},
},
}
else:
response = {
"GroupName": group_name,
"ChatbotId": chatbot_id,
"Chatbot": {
"inention": {"index": f"{chatbot_id}-intention-default"},
"qq": {"index": f"{chatbot_id}-qq-default"},
"qd": {"index": f"{chatbot_id}-qd-default"},
},
}
return response


def __delete_chatbot(event, group_name):
Expand Down
11 changes: 9 additions & 2 deletions source/lambda/etl/utils/ddb_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,14 @@ def initiate_index(


def initiate_chatbot(
chatbot_table, group_name, chatbot_id, index_id, index_type, tag, create_time=None
chatbot_table,
group_name,
chatbot_id,
chatbot_description,
index_id,
index_type,
tag,
create_time=None,
):
existing_item = item_exist(
chatbot_table, {"groupName": group_name, "chatbotId": chatbot_id}
Expand Down Expand Up @@ -136,7 +143,7 @@ def initiate_chatbot(
{
"groupName": group_name,
"chatbotId": chatbot_id,
"languages": ["zh"],
"chatbotDescription": chatbot_description,
"indexIds": {index_type: {"count": 1, "value": {tag: index_id}}},
"createTime": create_time,
"updateTime": create_time,
Expand Down
3 changes: 2 additions & 1 deletion source/portal/src/locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@
"sessionHistory": "Session History",
"prompt": "Prompt",
"prompts": "Prompts",
"chatbotManagement": "Chat Bot Management",
"chatbot": "Chat Bot",
"chatbots": "Chat Bots",
"chatbotManagement": "Chat Bot Management",
"type": "Type",
"updateBy": "Update by",
"updateTime": "Update time",
Expand Down
3 changes: 2 additions & 1 deletion source/portal/src/locale/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@
"sessionHistory": "历史记录",
"prompt": "提示词",
"prompts": "提示词",
"chatbotManagement": "管理聊天机器人",
"chatbot": "聊天机器人",
"chatbots": "聊天机器人",
"chatbotManagement": "管理聊天机器人",
"type": "类型",
"updateBy": "更新者",
"updateTime": "更新时间",
Expand Down
96 changes: 55 additions & 41 deletions source/portal/src/pages/chatbotManagement/ChatbotManagement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import {
import {
CreateChatbotResponse,
GetChatbotResponse,
ChatbotItem
ChatbotItem,
ChatbotResponse
} from 'src/types';
import useAxiosRequest from 'src/hooks/useAxiosRequest';
import { useTranslation } from 'react-i18next';
Expand All @@ -32,8 +33,8 @@ const ChatbotManagement: React.FC = () => {
const [selectedItems, setSelectedItems] = useState<ChatbotItem[]>([]);
const fetchData = useAxiosRequest();
const { t } = useTranslation();
const [loadingData] = useState(false);
const [allChatbotList] = useState<ChatbotItem[]>([]);
const [loadingData, setLoadingData] = useState(false);
const [allChatbotList, setAllChatbotList] = useState<ChatbotItem[]>([]);
const [tableChatbotList, setTableChatbotList] = useState<ChatbotItem[]>([]);
const [currentPage, setCurrentPage] = useState(1);
const [pageSize, setPageSize] = useState(10);
Expand All @@ -43,7 +44,7 @@ const ChatbotManagement: React.FC = () => {
const [loadingSave, setLoadingSave] = useState(false);
const [currentChatbot, setCurrentChatbot] = useState<GetChatbotResponse>();
const [modelList, setModelList] = useState<SelectProps.Option[]>([]);
const [chatbotList, setChatbotList] = useState<SelectProps.Option[]>([]);
// const [chatbotList, setChatbotList] = useState<SelectProps.Option[]>([]);
const [modelOption, setModelOption] = useState<SelectProps.Option | null>(
null,
);
Expand Down Expand Up @@ -82,40 +83,54 @@ const ChatbotManagement: React.FC = () => {
};

const getChatbotList = async () => {
setLoadingGet(true);
setLoadingData(true);
setSelectedItems([]);
const params = {
max_items: 9999,
page_size: 9999,
};
try {
const data = await fetchData({
url: 'chatbot-management/chatbots',
method: 'get',
params,
});
const items: string[] = data.chatbot_ids;
const getChatbots = items.map((item) => {
const items: ChatbotResponse = data;
const preSortItem = items.Items.map((chatbot) => {
return {
label: item,
value: item,
...chatbot,
uuid: chatbot.ChatbotId,
};
});
setChatbotList(getChatbots);
setChatbotOption(getChatbots[0]);
setAllChatbotList(preSortItem);
setTableChatbotList(preSortItem.slice(0, pageSize));
setLoadingData(false);
} catch (error: unknown) {
setLoadingGet(false);
setLoadingData(false);
}
};

const getChatbotById = async (type: 'create' | 'edit') => {
setLoadingGet(true);
let requestUrl = `chatbot-management/chatbots/${modelOption?.value}/common`;
let requestUrl = `chatbot-management/chatbots/default`;
if (type === 'edit') {
requestUrl = `chatbot-management/chatbots/${selectedItems[0].ModelId}/common`;
requestUrl = `chatbot-management/chatbots/common`;
setChatbotOption({
label: selectedItems[0].ChatbotId,
value: selectedItems[0].ChatbotId,
});
setModelOption({
label: selectedItems[0].ModelId,
value: selectedItems[0].ModelId,
label: selectedItems[0].ModelName,
value: selectedItems[0].ModelName,
});
}
try {
const data: GetChatbotResponse = await fetchData({
url: requestUrl,
method: 'get',
params: {
chatbotId: selectedItems[0].ChatbotId,
},
});
setLoadingGet(false);
setCurrentChatbot(data);
Expand All @@ -132,7 +147,7 @@ const ChatbotManagement: React.FC = () => {
setLoadingSave(true);
try {
await fetchData({
url: `chatbot-management/chatbots/${selectedItems[0].ModelId}/common`,
url: `chatbot-management/chatbots/common`,
method: 'delete',
});
setLoadingSave(false);
Expand Down Expand Up @@ -227,17 +242,18 @@ const ChatbotManagement: React.FC = () => {
} ${t('selected')}`,
}}
columnDefinitions={[
{
id: 'chatbotId',
header: t('chatbotName'),
cell: (item: ChatbotItem) => item.ChatbotId,
isRowHeader: true,
},
{
id: 'modelId',
header: t('modelName'),
cell: (item: ChatbotItem) => item.ModelId,
cell: (item: ChatbotItem) => item.ModelName,
isRowHeader: true,
},
// {
// id: 'updateBy',
// header: t('updateBy'),
// cell: (item: ChatbotItem) => item.LastModifiedBy,
// },
{
id: 'updateTime',
header: t('updateTime'),
Expand Down Expand Up @@ -381,23 +397,21 @@ const ChatbotManagement: React.FC = () => {
header={t('button.createChatbot')}
>
<SpaceBetween direction="vertical" size="xs">
<FormField
label={t('chatbotName')}
stretch={true}
errorText={chatbotError}
>
<Select
disabled={loadingGet || showEdit}
onChange={({ detail }) => {
setChatbotError('');
setChatbotOption(detail.selectedOption);
}}
selectedOption={chatbotOption}
options={chatbotList}
placeholder={t('validation.requireChatbot')}
empty={t('noChatbotFound')}
/>
</FormField>
<FormField
label={t('chatbotName')}
stretch={true}
errorText={chatbotError}
>
<Textarea
rows={1}
value={chatbotOption?.value ?? ''}
placeholder={'admin'}
onChange={({ detail }) => {
setChatbotError('');
setChatbotOption({ value: detail.value, label: detail.value})
}}
/>
</FormField>
<FormField
label={t('modelName')}
stretch={true}
Expand Down Expand Up @@ -496,7 +510,7 @@ const ChatbotManagement: React.FC = () => {
<div className="selected-items-list">
<ul className="gap-5 flex-v">
{selectedItems.map((item) => (
<li key={item.SortKey}>{item.ModelId}</li>
<li key={item.SortKey}>{item.ModelName}</li>
))}
</ul>
</div>
Expand Down
4 changes: 2 additions & 2 deletions source/portal/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,10 @@ export interface CreateChatbotResponse {
}

export type ChatbotItem = {
uuid: string;
ChatbotId: string;
LastModifiedTime: string;
// LastModifiedBy: string;
ModelId: string;
ModelName: string;
SortKey: string;
Scene: string;
};
Expand Down

0 comments on commit 42fd6ea

Please sign in to comment.