Skip to content

Commit f25a4fa

Browse files
add agent level knowledge base
1 parent 3a49b99 commit f25a4fa

File tree

3 files changed

+193
-0
lines changed

3 files changed

+193
-0
lines changed

src/lib/helpers/types/agentTypes.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
* @property {boolean} merge_utility - Merge utility
5353
* @property {number?} [max_message_count]
5454
* @property {AgentUtility[]} utilities - The agent utilities.
55+
* @property {AgentKnowledgeBase[]} knowledge_bases - The agent knowledge bases.
5556
* @property {Date} created_datetime
5657
* @property {Date} updated_datetime
5758
* @property {AgentLlmConfig} llm_config - LLM settings.
@@ -126,6 +127,12 @@
126127
* @property {UtilityBase[]} templates
127128
*/
128129

130+
/**
131+
* @typedef {Object} AgentKnowledgeBase
132+
* @property {string} name
133+
* @property {boolean} disabled
134+
*/
135+
129136
/**
130137
* @typedef {Object} UtilityBase
131138
* @property {string} name

src/routes/page/agent/[agentId]/+page.svelte

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import Swal from 'sweetalert2'
2020
import { goto } from '$app/navigation';
2121
import AgentUtility from './agent-utility.svelte';
22+
import AgentKnowledgeBase from './agent-knowledge-base.svelte';
2223
import { AgentExtensions } from '$lib/helpers/utils/agent';
2324
2425
@@ -30,6 +31,8 @@
3031
let agentPromptCmp = null;
3132
/** @type {any} */
3233
let agentUtilityCmp = null;
34+
/** @type {any} */
35+
let agentKnowledgeBaseCmp = null;
3336
3437
/** @type {boolean} */
3538
let isLoading = false;
@@ -70,6 +73,7 @@
7073
fetchJsonContent();
7174
fetchPrompts();
7275
fetchUtilties();
76+
fetchKnowledgeBases();
7377
7478
agent = {
7579
...agent,
@@ -78,6 +82,7 @@
7882
channel_instructions: agent.channel_instructions || [],
7983
profiles: agent.profiles?.filter((x, idx, self) => x?.trim()?.length > 0 && self.indexOf(x) === idx) || [],
8084
utilities: agent.utilities || [],
85+
knowledge_bases: agent.knowledge_bases || [],
8186
max_message_count: Number(agent.max_message_count) > 0 ? Number(agent.max_message_count) : null
8287
};
8388
isLoading = true;
@@ -121,6 +126,11 @@
121126
agent.utilities = list || [];
122127
}
123128
129+
function fetchKnowledgeBases() {
130+
const list = agentKnowledgeBaseCmp?.fetchKnowledgeBases();
131+
agent.knowledge_bases = list || [];
132+
}
133+
124134
function refreshChannelPrompts() {
125135
agentPromptCmp?.refreshChannelPrompts();
126136
}
@@ -167,6 +177,9 @@
167177
<div class="agent-detail-section">
168178
<AgentUtility bind:this={agentUtilityCmp} agent={agent} />
169179
</div>
180+
<div class="agent-detail-section">
181+
<AgentKnowledgeBase bind:this={agentKnowledgeBaseCmp} agent={agent} />
182+
</div>
170183
</Col>
171184
<Col class="section-min-width" style="flex: 65%;">
172185
<div class="agent-detail-section">
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
<script>
2+
import { onMount } from 'svelte';
3+
import { Card, CardBody, Input, Button } from '@sveltestrap/sveltestrap';
4+
import { getVectorKnowledgeCollections } from '$lib/services/knowledge-base-service';
5+
6+
const limit = 5;
7+
8+
/** @type {import('$agentTypes').AgentModel} */
9+
export let agent;
10+
11+
export const fetchKnowledgeBases = () => {
12+
const candidates = innerKnowledgeBases?.filter(x => !!x.name)?.map(x => {
13+
return {
14+
name: x.name,
15+
disabled: x.disabled
16+
};
17+
});
18+
candidates.map(x => {
19+
console.log(x.disabled);
20+
console.log(x.name);
21+
});
22+
refresh(candidates);
23+
return candidates;
24+
}
25+
26+
/** @type {string[]} */
27+
let knowledgeBaseOptions = [];
28+
29+
/** @type {import('$agentTypes').AgentKnowledgeBase[]} */
30+
let innerKnowledgeBases = [];
31+
32+
onMount(async () =>{
33+
const type = 'question-answer';
34+
getVectorKnowledgeCollections(type).then(data => {
35+
const names = data || [];
36+
knowledgeBaseOptions = ["", ...names];
37+
});
38+
init();
39+
});
40+
41+
function init() {
42+
const list = agent.knowledge_bases?.map(x => {
43+
return {
44+
...x,
45+
disabled: false
46+
};
47+
}) || [];
48+
refresh(list);
49+
}
50+
51+
/**
52+
* @param {any} e
53+
* @param {number} idx
54+
*/
55+
function changeKnowledgeBase(e, idx) {
56+
const found = innerKnowledgeBases.find((_, index) => index === idx);
57+
if (!found) return;
58+
59+
const name = e.target.value;
60+
found.name = name;
61+
refresh(innerKnowledgeBases);
62+
}
63+
64+
function addKnowledgeBase() {
65+
innerKnowledgeBases = [
66+
...innerKnowledgeBases,
67+
{
68+
name: '',
69+
disabled: false
70+
}
71+
];
72+
}
73+
74+
/** @param {number} idx */
75+
function deleteKnowledgeBase(idx) {
76+
innerKnowledgeBases = innerKnowledgeBases.filter((_, index) => index !== idx);
77+
}
78+
79+
/**
80+
* @param {any} e
81+
* @param {number} uid
82+
*/
83+
function toggleKnowledgeBase(e, uid) {
84+
const found = innerKnowledgeBases.find((_, index) => index === uid);
85+
if (!found) return;
86+
87+
found.disabled = !e.target.checked;
88+
refresh(innerKnowledgeBases);
89+
}
90+
91+
92+
/** @param {import('$agentTypes').AgentKnowledgeBase[]} list */
93+
function refresh(list) {
94+
innerKnowledgeBases = list?.map(x => {
95+
return {
96+
name: x.name,
97+
disabled: x.disabled
98+
}
99+
}) || [];
100+
}
101+
102+
</script>
103+
104+
<Card>
105+
<CardBody>
106+
<div class="text-center">
107+
<h5 class="mt-1 mb-3">Knowledge Bases</h5>
108+
</div>
109+
110+
<div class="agent-utility-container">
111+
{#each innerKnowledgeBases as knowledge, uid (uid)}
112+
<div class="utility-wrapper">
113+
<div class="utility-row utility-row-primary">
114+
<div class="utility-label fw-bold">
115+
<div class="line-align-center">{`Collection #${uid + 1}`}</div>
116+
<div class="utility-tooltip">
117+
<div class="line-align-center">
118+
<Input
119+
type="checkbox"
120+
checked={!knowledge.disabled}
121+
on:change={e => toggleKnowledgeBase(e, uid)}
122+
/>
123+
</div>
124+
<div
125+
class="line-align-center"
126+
data-bs-toggle="tooltip"
127+
data-bs-placement="top"
128+
title="Uncheck to disable utility"
129+
>
130+
<i class="bx bx-info-circle" />
131+
</div>
132+
</div>
133+
</div>
134+
<div class="utility-value">
135+
<div class="utility-input line-align-center">
136+
<Input
137+
type="select"
138+
value={knowledge.name}
139+
disabled={knowledge.disabled}
140+
on:change={e => changeKnowledgeBase(e, uid)}
141+
>
142+
{#each [...knowledgeBaseOptions, "one.data-assistant"] as option}
143+
<option value={option} selected={option == knowledge.name}>{option}</option>
144+
{/each}
145+
</Input>
146+
</div>
147+
<div class="utility-delete line-align-center">
148+
<i
149+
class="bx bxs-no-entry text-danger clickable"
150+
role="link"
151+
tabindex="0"
152+
on:keydown={() => {}}
153+
on:click={() => deleteKnowledgeBase(uid)}
154+
/>
155+
</div>
156+
</div>
157+
</div>
158+
</div>
159+
{/each}
160+
161+
{#if innerKnowledgeBases.length < limit}
162+
<div class="add-utility">
163+
<Button color="primary" on:click={() => addKnowledgeBase()}>
164+
<span>
165+
<i class="bx bx-plus" />
166+
<span>Add Knowledge Base</span>
167+
</span>
168+
</Button>
169+
</div>
170+
{/if}
171+
</div>
172+
</CardBody>
173+
</Card>

0 commit comments

Comments
 (0)