Skip to content

Commit d24a604

Browse files
author
Bogdan Tsechoev
committed
Merge branch 'kb_stats' into 'master'
Display knowledge base stats in AI Assistant See merge request postgres-ai/database-lab!941
2 parents 98347d3 + 314fcdc commit d24a604

File tree

6 files changed

+129
-3
lines changed

6 files changed

+129
-3
lines changed

ui/cspell.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@
200200
"TSQL",
201201
"sparql",
202202
"SPARQL",
203-
"subtransactions"
203+
"subtransactions",
204+
"mbox"
204205
]
205206
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import React, { useMemo } from "react";
2+
import { useKBStats } from "./hooks";
3+
import Box from "@mui/material/Box/Box";
4+
import { makeStyles, Typography } from "@material-ui/core";
5+
import { Link } from "@postgres.ai/shared/components/Link2";
6+
7+
const useStyles = makeStyles((theme) => ({
8+
container: {
9+
marginTop: 42,
10+
'& p': {
11+
margin: 0,
12+
lineHeight: 1.5,
13+
},
14+
[theme.breakpoints.down(480)]: {
15+
marginTop: 24
16+
},
17+
[theme.breakpoints.down(360)]: {
18+
marginTop: 0
19+
}
20+
},
21+
headingLink: {
22+
fontSize: 16,
23+
[theme.breakpoints.down(330)]: {
24+
fontSize: 14
25+
}
26+
},
27+
}))
28+
29+
export const KBStats = () => {
30+
const { data, loading, error } = useKBStats();
31+
const classes = useStyles()
32+
33+
const { totalSum, lastUpdate } = useMemo(() => {
34+
if (!data?.length) {
35+
return { totalSum: 0, lastUpdate: '' };
36+
}
37+
38+
const categoryTotals = new Map<string, number>();
39+
let latestDate = data[0].last_document_date;
40+
41+
data.forEach(({ category, total_count, last_document_date }) => {
42+
categoryTotals.set(category, total_count);
43+
if (new Date(last_document_date) > new Date(latestDate)) {
44+
latestDate = last_document_date;
45+
}
46+
});
47+
48+
latestDate = new Date(latestDate).toISOString().replace('T', ' ').split('.')[0]
49+
50+
const totalSum = Array.from(categoryTotals.values()).reduce((sum, count) => sum + count, 0);
51+
return { totalSum, lastUpdate: latestDate };
52+
}, [data]);
53+
54+
if (error || loading || !data?.length) {
55+
return <div className={classes.container} style={{ height: 58.5 }}></div>;
56+
}
57+
58+
return (
59+
<Box className={classes.container}>
60+
<p>Knowledge base contains {totalSum.toLocaleString(navigator.language)} documents.</p>
61+
<p>Last updated: {lastUpdate}.</p>
62+
<Link
63+
external
64+
to={`https://postgres.ai/docs/reference-guides/postgres-ai-bot-reference#tool-rag_search`}
65+
target="_blank"
66+
title="Show full information"
67+
>
68+
Details
69+
</Link>
70+
</Box>
71+
);
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { useEffect, useState } from 'react'
2+
import { request } from "../../helpers/request";
3+
4+
export type KBStats = {
5+
category: 'articles' | 'docs' | 'src' | 'mbox',
6+
domain: string,
7+
total_count: number,
8+
count: number,
9+
last_document_date: string
10+
}
11+
12+
type UseKBStats = {
13+
data: KBStats[] | null,
14+
error: string | null,
15+
loading: boolean
16+
}
17+
18+
export const useKBStats = (): UseKBStats => {
19+
const [data, setData] = useState<KBStats[] | null>(null);
20+
const [loading, setLoading] = useState(true);
21+
const [error, setError] = useState<string | null>(null);
22+
const apiServer = process.env.REACT_APP_API_URL_PREFIX || '';
23+
useEffect(() => {
24+
const fetchData = async () => {
25+
setLoading(true)
26+
try {
27+
const response = await request("/kb_category_domain_counts", {}, apiServer)
28+
const result: KBStats[] = await response.json();
29+
setData(result);
30+
} catch (err) {
31+
setError(err instanceof Error ? err.message : 'Unknown error');
32+
} finally {
33+
setLoading(false);
34+
}
35+
};
36+
37+
fetchData().catch(console.error);
38+
}, []);
39+
40+
return { data, loading, error };
41+
};

ui/packages/platform/src/pages/Bot/HintCards/HintCard/HintCard.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ const useStyles = makeStyles((theme) => ({
4545
fontSize: '0.813rem',
4646
height: 'auto',
4747
},
48+
[theme.breakpoints.down(330)]: {
49+
fontSize: '.75rem'
50+
}
4851
},
4952
}));
5053

ui/packages/platform/src/pages/Bot/HintCards/HintCards.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ const useStyles = makeStyles((theme) => ({
1414
justifyContent: 'center',
1515
},
1616
[theme.breakpoints.down(480)]: {
17-
marginBottom: '1.5rem',
17+
marginBottom: '1rem',
18+
},
19+
[theme.breakpoints.down(380)]: {
20+
marginTop: '1rem',
21+
marginBottom: '.5rem',
1822
},
1923
[theme.breakpoints.down(760)]: {
2024
'& > *:nth-child(n+3)': {

ui/packages/platform/src/pages/Bot/Messages/Messages.tsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { Message } from "./Message/Message";
1919
import { useAiBot } from "../hooks";
2020
import { HintCards } from "../HintCards/HintCards";
2121
import { ErrorMessage } from "./ErrorMessage/ErrorMessage";
22+
import { KBStats } from "../../../components/KBStats/KBStats";
2223

2324
const useStyles = makeStyles(
2425
(theme) => ({
@@ -36,7 +37,10 @@ const useStyles = makeStyles(
3637
},
3738
emptyChatMessage: {
3839
maxWidth: '80%',
39-
fontSize: '0.875rem'
40+
fontSize: 14,
41+
[theme.breakpoints.down(330)]: {
42+
fontSize: 12
43+
}
4044
},
4145
messages: {
4246
overflowY: 'auto',
@@ -229,6 +233,7 @@ export const Messages = React.memo(({orgId}: {orgId: number}) => {
229233
Depending on settings, LLM service provider such as GCP or OpenAI is used.
230234
</Typography>
231235
<HintCards orgId={orgId} />
236+
<KBStats />
232237
</div>
233238
)
234239
}

0 commit comments

Comments
 (0)