Skip to content

Commit 86ef8d7

Browse files
authored
migrated to stream query APIV2 (#53)
* migrated to stream query APIV2 * fixing tests * enabled non streaming response * updated README * changed the max search results to 10 * removed debug logs * minor update * added back FCS calculating spinner * renamed corpusKey to corpusKeys * reformatted with 2 spaces * parsed the facts text using snippets
1 parent f93ef07 commit 86ef8d7

18 files changed

Lines changed: 662 additions & 453 deletions

README.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ import { ReactChatbot } from "@vectara/react-chatbot";
6060

6161
<ReactChatbot
6262
customerId="CUSTOMER_ID"
63-
corpusIds={["CORPUS_ID_1", "CORPUS_ID_2", "CORPUS_ID_N"]}
63+
corpusKeys={["CORPUS_KEY_1", "CORPUS_KEY_2", "CORPUS_KEY_N"]}
6464
apiKey="API_KEY"
6565
title="My Chatbot"
6666
placeholder="Chat with your AI assistant"
@@ -79,9 +79,10 @@ import { ReactChatbot } from "@vectara/react-chatbot";
7979

8080
Every Vectara account is associated with a customer ID. You can find your customer ID by logging into the [Vectara Console](https://console.vectara.com/) and opening your account dropdown in the top-right corner.
8181

82-
##### `corpusIds` (required)
82+
##### `corpuskeys` (required)
8383

84-
After you [create a corpus](https://docs.vectara.com/docs/console-ui/creating-a-corpus), you can find its ID by navigating to the corpus and looking in the top-left corner, next to the corpus name.
84+
After you [create a corpus](https://docs.vectara.com/docs/console-ui/creating-a-corpus), you can find its Key by navigating to the corpus and looking in the top-left corner, next to the corpus name.
85+
To run queries against multiple corpora, use a comma-separated list of corpus keys. For example: "corpus_1,corpus_2".
8586

8687
##### `apiKey` (required)
8788

@@ -132,6 +133,18 @@ When set, this will calculate the [Factual Consistency Score (FCS)](https://docs
132133

133134
Define the [summarizer and prompt](https://docs.vectara.com/docs/learn/grounded-generation/select-a-summarizer) to use to generate the chat response.
134135

136+
##### `numberOfSearchResults` (optional)
137+
138+
Define the number of search results to be used to generate summary. Default is set to 15.
139+
140+
##### `rerankerId` (optional)
141+
142+
Define the reranker Id to be used , Defaults to 272725718.
143+
144+
##### `lambda` (optional)
145+
146+
How much to weigh lexical scores compared to the embedding score. 0 means lexical search is not used at all, and 1 means only lexical search is used.
147+
135148
### Use your own views with the useChat hook
136149

137150
Install React-Chatbot:
@@ -218,7 +231,7 @@ export const App = (props: Props): ReactNode => {
218231
setChatWidget(
219232
<ReactChatbot
220233
customerId="CUSTOMER_ID"
221-
corpusIds={["CORPUS_ID_1", "CORPUS_ID_2", "CORPUS_ID_N"]}
234+
corpusKeys={["CORPUS_KEY_1", "CORPUS_KEY_2", "CORPUS_KEY_N"]}
222235
apiKey="API_KEY"
223236
/>
224237
);

docs/package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/src/components/ConfigurationDrawer.tsx

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import { SUMMARY_LANGUAGES, RerankerId } from "../../../src/types";
2121
type Props = {
2222
isOpen: boolean;
2323
onClose: () => void;
24-
corpusIds: string[];
25-
onUpdateCorpusIds: (event: React.ChangeEvent<HTMLInputElement>) => void;
24+
corpusKeys: string;
25+
onUpdateCorpusKeys: (event: React.ChangeEvent<HTMLInputElement>) => void;
2626
customerId: string;
2727
onUpdateCustomerId: (event: React.ChangeEvent<HTMLInputElement>) => void;
2828
apiKey: string;
@@ -35,8 +35,8 @@ type Props = {
3535
onUpdateInputSize: (inputSize: "large" | "medium") => void;
3636
emptyMessagesContent: string;
3737
onUpdateEmptyMessagesContent: (event: React.ChangeEvent<HTMLTextAreaElement>) => void;
38-
isStreamingEnabled: boolean;
39-
onUpdateIsStreamingEnabled: (isStreamingEnabled: boolean) => void;
38+
numberOfSearchResults: number;
39+
onUpdateNumberOfSearchResults: (isStreamingEnabled: number) => void;
4040
language: SummaryLanguage;
4141
onUpdateLanguage: (language: SummaryLanguage) => void;
4242
exampleQuestions: string;
@@ -49,13 +49,15 @@ type Props = {
4949
onUpdateRerankerId: (rerankerId: RerankerId) => void;
5050
lambda: number;
5151
onUpdateLambda: (lamda: number) => void;
52+
isStreamingEnabled: boolean;
53+
onUpdateIsStreamingEnabled: (isStreamingEnabled: boolean) => void;
5254
};
5355

5456
export const ConfigurationDrawer = ({
5557
isOpen,
5658
onClose,
57-
corpusIds,
58-
onUpdateCorpusIds,
59+
corpusKeys,
60+
onUpdateCorpusKeys,
5961
customerId,
6062
onUpdateCustomerId,
6163
apiKey,
@@ -68,8 +70,8 @@ export const ConfigurationDrawer = ({
6870
onUpdateInputSize,
6971
emptyMessagesContent,
7072
onUpdateEmptyMessagesContent,
71-
isStreamingEnabled,
72-
onUpdateIsStreamingEnabled,
73+
numberOfSearchResults,
74+
onUpdateNumberOfSearchResults,
7375
language,
7476
onUpdateLanguage,
7577
exampleQuestions,
@@ -81,7 +83,9 @@ export const ConfigurationDrawer = ({
8183
rerankerId,
8284
onUpdateRerankerId,
8385
lambda,
84-
onUpdateLambda
86+
onUpdateLambda,
87+
isStreamingEnabled,
88+
onUpdateIsStreamingEnabled,
8589
}: Props) => {
8690
return (
8791
<VuiDrawer
@@ -116,8 +120,8 @@ export const ConfigurationDrawer = ({
116120

117121
<VuiSpacer size="m" />
118122

119-
<VuiFormGroup label="Corpus IDs (comma-separated)" labelFor="corpusId">
120-
<VuiTextInput value={corpusIds.join(",")} onChange={onUpdateCorpusIds} />
123+
<VuiFormGroup label="Corpus Key" labelFor="corpusId">
124+
<VuiTextInput value={corpusKeys} onChange={onUpdateCorpusKeys} />
121125
</VuiFormGroup>
122126

123127
<VuiSpacer size="m" />
@@ -153,18 +157,21 @@ export const ConfigurationDrawer = ({
153157
<VuiSpacer size="m" />
154158

155159
<VuiLabel>Enable streaming</VuiLabel>
156-
157-
<VuiSpacer size="xs" />
158-
159160
<VuiToggle
160-
onChange={(e) => {
161-
onUpdateIsStreamingEnabled(e.target.checked);
162-
}}
163-
checked={isStreamingEnabled}
161+
onChange={(e) => {
162+
onUpdateIsStreamingEnabled(e.target.checked);
163+
}}
164+
checked={isStreamingEnabled}
164165
/>
165166

166167
<VuiSpacer size="m" />
167168

169+
<VuiFormGroup label="Number of search results to summarize" labelFor="numberOfSearchResults">
170+
<VuiNumberInput value={numberOfSearchResults} onChange={(numberOfSearchResults) => onUpdateNumberOfSearchResults(numberOfSearchResults as number)} step={1} />
171+
</VuiFormGroup>
172+
173+
<VuiSpacer size="m" />
174+
168175
<VuiFormGroup label="Response language" labelFor="responseLanguage">
169176
<VuiSelect
170177
value={language}

docs/src/index.tsx

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,22 @@ const formatStringProp = (value?: string) => {
3434

3535
const generateCodeSnippet = (
3636
customerId?: string,
37-
corpusIds?: string[],
37+
corpusKeys?: string,
3838
apiKey?: string,
3939
title?: string,
4040
placeholder?: string,
4141
inputSize?: string,
4242
emptyStateDisplay?: string,
43-
isStreamingEnabled?: boolean,
43+
numberOfSearchResults?: number,
4444
language?: SummaryLanguage,
4545
exampleQuestions?: string,
4646
rerankerId?: RerankerId,
47-
lambda?: number
47+
lambda?: number,
48+
isStreamingEnabled?: boolean,
4849
) => {
4950
const props = [
5051
`customerId="${customerId === "" ? "<Your Vectara customer ID>" : customerId}"`,
51-
`corpusIds=${
52-
corpusIds?.length === 0 ? '"<Your Vectara corpus IDs>"' : `{["${corpusIds?.join('","').replace(/\s/g, "")}"]}`
53-
}`,
52+
`corpusKeys="${corpusKeys === "" ? "<Your Vectara Corpus key>" : corpusKeys}"`,
5453
`apiKey="${apiKey === "" ? "<Your Vectara API key>" : apiKey}"`
5554
];
5655

@@ -80,6 +79,7 @@ const generateCodeSnippet = (
8079
}
8180

8281
props.push(`enableStreaming={${isStreamingEnabled}}`);
82+
props.push(`numberOfSearchResultsForSummary={${numberOfSearchResults}}`);
8383

8484
props.push(`language="${language}"`);
8585
props.push(`rerankerId=${rerankerId}`);
@@ -99,7 +99,7 @@ export const App = () => (
9999
);`;
100100
};
101101

102-
const DEFAULT_CORPUS_IDS = ["1"];
102+
const DEFAULT_CORPUS_KEY = "vectara-docs_1";
103103
const DEFAULT_CUSTOMER_ID = "1366999410";
104104
const DEFAULT_API_KEY = "zqt_UXrBcnI2UXINZkrv4g1tQPhzj02vfdtqYJIDiA";
105105
const DEFAULT_TITLE = "Vectara Docs Chatbot";
@@ -108,13 +108,14 @@ const DEFAULT_PLACEHOLDER = 'Try "What is Vectara?" or "How does RAG work?"';
108108
const App = () => {
109109
const [isConfigurationDrawerOpen, setIsConfigurationDrawerOpen] = useState(false);
110110
const [isChatbotForcedOpen, setIsChatbotForcedOpen] = useState(true);
111-
const [corpusIds, setCorpusIds] = useState<string[]>([]);
111+
const [corpusKeys, setCorpusKeys] = useState<string>("");
112112
const [customerId, setCustomerId] = useState<string>("");
113113
const [apiKey, setApiKey] = useState<string>("");
114114
const [title, setTitle] = useState<string>(DEFAULT_TITLE);
115115
const [placeholder, setPlaceholder] = useState<string>(DEFAULT_PLACEHOLDER);
116116
const [inputSize, setInputSize] = useState<"large" | "medium">("large");
117117
const [isStreamingEnabled, setIsStreamingEnabled] = useState<boolean>(true);
118+
const [numberOfSearchResults, setNumberOfSearchResults] = useState<number>(15);
118119
const [language, setLanguage] = useState<SummaryLanguage>("eng");
119120
const [emptyStateJsx, setEmptyStateJsx] = useState<string>("");
120121
const [exampleQuestions, setExampleQuestions] = useState<string>("What is Vectara?, How does RAG work?");
@@ -123,14 +124,8 @@ const App = () => {
123124
const [rerankerId, setRerankerId] = useState<RerankerId>(DEFAULT_RERANKER_ID);
124125
const [lambda, setLambda] = useState<number>(DEFAULT_LAMBDA_VALUE);
125126

126-
const onUpdateCorpusIds = useCallback((e: ChangeEvent<HTMLInputElement>) => {
127-
const sanitizedValue = e.target.value.trim();
128-
129-
if (sanitizedValue === "") {
130-
setCorpusIds([]);
131-
return;
132-
}
133-
setCorpusIds(sanitizedValue.split(","));
127+
const onUpdateCorpusKeys = useCallback((e: ChangeEvent<HTMLInputElement>) => {
128+
setCorpusKeys(e.target.value);
134129
}, []);
135130

136131
const onUpdateCustomerId = useCallback((e: ChangeEvent<HTMLInputElement>) => {
@@ -217,7 +212,7 @@ const App = () => {
217212
* This ensures that we don't voluntarily display the docs corpus details in the text fields.
218213
*/}
219214
<ReactChatbot
220-
corpusIds={corpusIds.length === 0 ? DEFAULT_CORPUS_IDS : corpusIds}
215+
corpusKey={corpusKeys === "" ? DEFAULT_CORPUS_KEY : corpusKeys}
221216
customerId={customerId === "" ? DEFAULT_CUSTOMER_ID : customerId}
222217
apiKey={apiKey === "" ? DEFAULT_API_KEY : apiKey}
223218
title={title === "" ? undefined : title}
@@ -227,12 +222,13 @@ const App = () => {
227222
emptyStateDisplay={emptyStateJsx === "" ? undefined : <CustomEmptyStateDisplay />}
228223
isInitiallyOpen={isChatbotForcedOpen}
229224
zIndex={9}
230-
enableStreaming={isStreamingEnabled}
225+
numberOfSearchResults={numberOfSearchResults}
231226
language={language}
232227
enableFactualConsistencyScore={enableFactualConsistencyScore}
233228
summaryPromptName={summaryPromptName}
234229
rerankerId={rerankerId}
235230
lambda={lambda}
231+
enableStreaming={isStreamingEnabled}
236232
/>
237233

238234
<VuiSpacer size="m" />
@@ -265,17 +261,18 @@ const App = () => {
265261
<VuiCode language="tsx">
266262
{generateCodeSnippet(
267263
customerId,
268-
corpusIds,
264+
corpusKeys,
269265
apiKey,
270266
title,
271267
placeholder,
272268
inputSize,
273269
emptyStateJsx,
274-
isStreamingEnabled,
270+
numberOfSearchResults,
275271
language,
276272
exampleQuestions,
277273
rerankerId,
278-
lambda
274+
lambda,
275+
isStreamingEnabled
279276
)}
280277
</VuiCode>
281278

@@ -308,9 +305,10 @@ export const App = () => {
308305
startNewConversation
309306
} = useChat({
310307
customerId: DEFAULT_CUSTOMER_ID,
311-
corpusIds: DEFAULT_CORPUS_IDS,
308+
corpusKeys: DEFAULT_CORPUS_KEY,
312309
apiKey: DEFAULT_API_KEY,
313310
enableStreaming: true, // Enable streaming, false otherwise. Defaults to true.
311+
numberOfSearchResults: 15, // Number of search results to use for summary.
314312
language: "fra" // Response language. Defaults to "eng" for English.
315313
});
316314
@@ -350,8 +348,8 @@ export const App = () => {
350348
<ConfigurationDrawer
351349
isOpen={isConfigurationDrawerOpen}
352350
onClose={() => setIsConfigurationDrawerOpen(false)}
353-
corpusIds={corpusIds}
354-
onUpdateCorpusIds={onUpdateCorpusIds}
351+
corpusKeys={corpusKeys}
352+
onUpdateCorpusKeys={onUpdateCorpusKeys}
355353
customerId={customerId}
356354
onUpdateCustomerId={onUpdateCustomerId}
357355
apiKey={apiKey}
@@ -364,8 +362,8 @@ export const App = () => {
364362
onUpdateInputSize={setInputSize}
365363
emptyMessagesContent={emptyStateJsx}
366364
onUpdateEmptyMessagesContent={onUpdateEmptyMessagesContent}
367-
isStreamingEnabled={isStreamingEnabled}
368-
onUpdateIsStreamingEnabled={setIsStreamingEnabled}
365+
numberOfSearchResults={numberOfSearchResults}
366+
onUpdateNumberOfSearchResults={setNumberOfSearchResults}
369367
language={language}
370368
onUpdateLanguage={setLanguage}
371369
exampleQuestions={exampleQuestions}
@@ -378,6 +376,8 @@ export const App = () => {
378376
onUpdateRerankerId={setRerankerId}
379377
lambda={lambda}
380378
onUpdateLambda={setLambda}
379+
isStreamingEnabled={isStreamingEnabled}
380+
onUpdateIsStreamingEnabled={setIsStreamingEnabled}
381381
/>
382382
</div>
383383
</VuiAppContent>

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@vectara/react-chatbot",
3-
"version": "2.0.0",
3+
"version": "3.0.0",
44
"description": "A Vectara-powered Chatbot component",
55
"main": "lib/index.js",
66
"module": "lib/index.js",
@@ -32,7 +32,7 @@
3232
"dependencies": {
3333
"@types/react": "^17.0.0",
3434
"@types/react-dom": "^17.0.0",
35-
"@vectara/stream-query-client": "^2.0.2",
35+
"@vectara/stream-query-client": "^3.2.0",
3636
"classnames": "^2.3.2",
3737
"lodash": "^4.17.21",
3838
"prismjs": "^1.29.0",

0 commit comments

Comments
 (0)