Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added reranker id in the config of chat bot #52

Merged
merged 3 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
/docs/public/script.js
/docs/public/script.js.map
/coverage
.DS_Store
.DS_Store
.idea/
2 changes: 1 addition & 1 deletion docs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 23 additions & 3 deletions docs/src/components/ConfigurationDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import {
VuiTextArea,
VuiRadioButton,
VuiLabel,
VuiSelect
VuiSelect, VuiNumberInput
} from "../ui";

import { SummaryLanguage } from "@vectara/react-chatbot";

import { SUMMARY_LANGUAGES } from "../../../src/types";
import { SUMMARY_LANGUAGES, RerankerId } from "../../../src/types";

type Props = {
isOpen: boolean;
Expand Down Expand Up @@ -45,6 +45,10 @@ type Props = {
onUpdateEnableFactualConsistencyScore: (enableFactualConsistencyScore: boolean) => void;
summaryPromptName: string;
onUpdateSummaryPromptName: (summaryPromptName: string) => void;
rerankerId: RerankerId;
onUpdateRerankerId: (rerankerId: RerankerId) => void;
lambda: number;
onUpdateLambda: (lamda: number) => void;
};

export const ConfigurationDrawer = ({
Expand Down Expand Up @@ -73,7 +77,11 @@ export const ConfigurationDrawer = ({
enableFactualConsistencyScore,
onUpdateEnableFactualConsistencyScore,
summaryPromptName,
onUpdateSummaryPromptName
onUpdateSummaryPromptName,
rerankerId,
onUpdateRerankerId,
lambda,
onUpdateLambda
}: Props) => {
return (
<VuiDrawer
Expand Down Expand Up @@ -217,6 +225,18 @@ export const ConfigurationDrawer = ({
<VuiTextInput value={summaryPromptName} onChange={(e) => onUpdateSummaryPromptName(e.target.value)} fullWidth />
</VuiFormGroup>

<VuiSpacer size="m" />

<VuiFormGroup label="Reranker ID" labelFor="rerankerId">
<VuiNumberInput value={rerankerId} onChange={(rerankerId) => onUpdateRerankerId(rerankerId as RerankerId)} />
</VuiFormGroup>

<VuiSpacer size="m" />

<VuiFormGroup label="Lambda" labelFor="lambda">
<VuiNumberInput value={lambda} onChange={(lambda) => onUpdateLambda(lambda as number)} max={1} min={0} step={0.001} />
</VuiFormGroup>

<VuiSpacer size="l" />

<VuiButtonPrimary color="primary" onClick={onClose}>
Expand Down
21 changes: 18 additions & 3 deletions docs/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ChangeEvent, useCallback, useState } from "react";
import ReactDOM from "react-dom";
import { BiLogoGithub } from "react-icons/bi";
import JsxParser from "react-jsx-parser";
import { ReactChatbot, SummaryLanguage, DEFAULT_SUMMARIZER } from "@vectara/react-chatbot";
import { ReactChatbot, SummaryLanguage, DEFAULT_SUMMARIZER, DEFAULT_RERANKER_ID, DEFAULT_LAMBDA_VALUE } from "@vectara/react-chatbot";
import {
VuiAppContent,
VuiAppHeader,
Expand All @@ -22,6 +22,7 @@ import { HeaderLogo } from "./components/HeaderLogo";
import { ConfigurationDrawer } from "components/ConfigurationDrawer";
import "./ui/_index.scss";
import "./index.scss";
import {RerankerId} from "../../src/types";

const formatStringProp = (value?: string) => {
if (!value) {
Expand All @@ -41,7 +42,9 @@ const generateCodeSnippet = (
emptyStateDisplay?: string,
isStreamingEnabled?: boolean,
language?: SummaryLanguage,
exampleQuestions?: string
exampleQuestions?: string,
rerankerId?: RerankerId,
lambda?: number
) => {
const props = [
`customerId="${customerId === "" ? "<Your Vectara customer ID>" : customerId}"`,
Expand Down Expand Up @@ -79,6 +82,8 @@ const generateCodeSnippet = (
props.push(`enableStreaming={${isStreamingEnabled}}`);

props.push(`language="${language}"`);
props.push(`rerankerId=${rerankerId}`);
props.push(`lambda=${lambda}`);

props.push(`isInitiallyOpen={ /* (optional) true, if the component should be initially opened */ }`);
props.push(`zIndex={ /* (optional) number representing the z-index the component should have */ }`);
Expand Down Expand Up @@ -115,6 +120,8 @@ const App = () => {
const [exampleQuestions, setExampleQuestions] = useState<string>("What is Vectara?, How does RAG work?");
const [enableFactualConsistencyScore, setEnableFactualConsistencyScore] = useState<boolean>(false);
const [summaryPromptName, setSummaryPromptName] = useState<string>(DEFAULT_SUMMARIZER);
const [rerankerId, setRerankerId] = useState<RerankerId>(DEFAULT_RERANKER_ID);
const [lambda, setLambda] = useState<number>(DEFAULT_LAMBDA_VALUE);

const onUpdateCorpusIds = useCallback((e: ChangeEvent<HTMLInputElement>) => {
const sanitizedValue = e.target.value.trim();
Expand Down Expand Up @@ -224,6 +231,8 @@ const App = () => {
language={language}
enableFactualConsistencyScore={enableFactualConsistencyScore}
summaryPromptName={summaryPromptName}
rerankerId={rerankerId}
lambda={lambda}
/>

<VuiSpacer size="m" />
Expand Down Expand Up @@ -264,7 +273,9 @@ const App = () => {
emptyStateJsx,
isStreamingEnabled,
language,
exampleQuestions
exampleQuestions,
rerankerId,
lambda
)}
</VuiCode>

Expand Down Expand Up @@ -363,6 +374,10 @@ export const App = () => {
onUpdateEnableFactualConsistencyScore={setEnableFactualConsistencyScore}
summaryPromptName={summaryPromptName}
onUpdateSummaryPromptName={setSummaryPromptName}
rerankerId={rerankerId}
onUpdateRerankerId={setRerankerId}
lambda={lambda}
onUpdateLambda={setLambda}
/>
</div>
</VuiAppContent>
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 12 additions & 3 deletions src/components/ChatView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Loader } from "./Loader";
import { MinimizeIcon } from "./Icons";
import { FactualConsistencyBadge } from "./FactualConsistencyBadge";
import { ExampleQuestions } from "./exampleQuestions/ExampleQuestions";
import { SummaryLanguage } from "types";
import {RerankerId, SummaryLanguage} from "types";

const inputSizeToQueryInputSize = {
large: "l",
Expand Down Expand Up @@ -56,6 +56,11 @@ export interface Props {

// Defines the name of the summary prompt. Defaults to "vectara-summary-ext-v1.2.0".
summaryPromptName?: string;

// Define the reranker Id to be used , Defaults to "272725718"
rerankerId?: RerankerId;

lambda?: number
}

/**
Expand All @@ -77,7 +82,9 @@ export const ChatView = ({
enableStreaming = true,
language = "eng",
enableFactualConsistencyScore,
summaryPromptName
summaryPromptName,
rerankerId,
lambda
}: Props) => {
const [isOpen, setIsOpen] = useState<boolean>(isInitiallyOpen ?? false);
const [query, setQuery] = useState<string>("");
Expand All @@ -89,7 +96,9 @@ export const ChatView = ({
enableStreaming,
language,
enableFactualConsistencyScore,
summaryPromptName
summaryPromptName,
rerankerId,
lambda
});

const appLayoutRef = useRef<HTMLDivElement>(null);
Expand Down
12 changes: 9 additions & 3 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ReactNode, useEffect, useRef } from "react";
import * as ReactDOM from "react-dom";
import { Props, ChatView } from "./components/ChatView";
import type { SummaryLanguage } from "./types";
import type {RerankerId, SummaryLanguage} from "./types";
export type { Props } from "components/ChatView";
export { DEFAULT_SUMMARIZER } from "./useChat";
export { DEFAULT_SUMMARIZER, DEFAULT_RERANKER_ID, DEFAULT_LAMBDA_VALUE } from "./useChat";

// @ts-ignore
import cssText from "index.scss";
Expand Down Expand Up @@ -31,7 +31,9 @@ class ReactChatbotWebComponent extends HTMLElement {
"enablestreaming",
"language",
"enablefactualconsistencyscore",
"summarypromptname"
"summarypromptname",
"rerankerId",
"lambda"
];
}

Expand Down Expand Up @@ -81,6 +83,8 @@ class ReactChatbotWebComponent extends HTMLElement {
const language = (this.getAttribute("language") as SummaryLanguage) ?? undefined;
const enableFactualConsistencyScore = this.getAttribute("enableFactualConsistencyScore") === "true";
const summaryPromptName = this.getAttribute("summaryPromptName") ?? undefined;
const rerankerId = this.getAttribute("rerankerId") !== null ? parseInt(this.getAttribute("rerankerId")!, 10) : undefined;
const lambda = this.getAttribute("lambda") !== null ? parseFloat(this.getAttribute("lambda")!) : undefined;

ReactDOM.render(
<div>
Expand All @@ -99,6 +103,8 @@ class ReactChatbotWebComponent extends HTMLElement {
language={language}
enableFactualConsistencyScore={enableFactualConsistencyScore}
summaryPromptName={summaryPromptName}
rerankerId={rerankerId as RerankerId}
lambda={lambda}
/>
</div>,
this.mountPoint
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,5 @@ export type ChatTurn = {
results: DeserializedSearchResult[];
factualConsistencyScore?: number;
};

export type RerankerId = 272725717 | 272725719 | 272725718
16 changes: 13 additions & 3 deletions src/useChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import { deserializeSearchResponse } from "utils/deserializeSearchResponse";
*/

export const DEFAULT_SUMMARIZER = "vectara-summary-ext-v1.2.0";
export const DEFAULT_RERANKER_ID = 272725718

export const DEFAULT_LAMBDA_VALUE = 0.025

type UseChatConfig = {
customerId: string;
Expand All @@ -25,6 +28,8 @@ type UseChatConfig = {
language?: SummaryLanguage;
enableFactualConsistencyScore?: boolean;
summaryPromptName?: string;
rerankerId?: number;
lambda?: number
};

export const useChat = ({
Expand All @@ -34,7 +39,9 @@ export const useChat = ({
enableStreaming = true,
language = "eng",
enableFactualConsistencyScore,
summaryPromptName = DEFAULT_SUMMARIZER
summaryPromptName = DEFAULT_SUMMARIZER,
rerankerId = DEFAULT_RERANKER_ID,
lambda = DEFAULT_LAMBDA_VALUE
}: UseChatConfig) => {
const [messageHistory, setMessageHistory] = useState<ChatTurn[]>([]);
const recentQuestion = useRef<string>("");
Expand Down Expand Up @@ -68,7 +75,7 @@ export const useChat = ({
queryValue: query,
rerank: true,
rerankNumResults: 7,
rerankerId: 272725718,
rerankerId,
rerankDiversityBias: 0.3,
customerId: customerId,
corpusId: corpusIds.join(","),
Expand All @@ -88,8 +95,10 @@ export const useChat = ({
summaryNumSentences: 3,
summaryPromptName,
language,
rerankerId,
enableFactualConsistencyScore,
chat: { store: true, conversationId: conversationId ?? undefined }
chat: { store: true, conversationId: conversationId ?? undefined },
lambda: lambda
},
(update) => onStreamUpdate(update)
);
Expand All @@ -109,6 +118,7 @@ export const useChat = ({
summaryMode: true,
summaryNumResults: 7,
summaryNumSentences: 3,
rerankerId,
summaryPromptName,
language,
enableFactualConsistencyScore,
Expand Down
Loading