Skip to content
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
6 changes: 4 additions & 2 deletions docs/package-lock.json

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

34 changes: 33 additions & 1 deletion docs/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { HeaderLogo } from "./components/HeaderLogo";
import { ConfigurationDrawer } from "components/ConfigurationDrawer";
import "./ui/_index.scss";
import "./index.scss";
import { RerankerId } from "../../src/types";
import { AgenticResponse, RerankerId } from "../../src/types";

const formatStringProp = (value?: string) => {
if (!value) {
Expand Down Expand Up @@ -235,6 +235,38 @@ const App = () => {
rerankerId={rerankerId}
lambda={lambda}
enableStreaming={isStreamingEnabled}
agenticConfiguration={{
url: "https://vectara-com-chatbot-agent-server.onrender.com/verify-prospect",
onAgenticResponse: (response: AgenticResponse) => {
if (response.event === "prompt_schedule_sales") {
return {
message: response.message,
userActionOptions: [
{
label: "Schedule a demo",
onSelect: () =>
console.log("In a live context, this would connect you to the Vectara Sales team.")
}
]
};
}

if (response.event === "handle_prospect_decline") {
return {
message: response.message
};
}

if (response.event === "schedule_sales") {
return {
message: {
content: "In a live context, this would connect you to the Vectara Sales team."
}
};
}
}
}}
requestSource="react-chatbot-docs"
/>

<VuiSpacer size="m" />
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"dependencies": {
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0",
"@vectara/stream-query-client": "^3.2.0",
"@vectara/stream-query-client": "^5.1.0",
"classnames": "^2.3.2",
"lodash": "^4.17.21",
"prismjs": "^1.29.0",
Expand Down
10 changes: 5 additions & 5 deletions src/components/ChatItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,12 @@ export const ChatItem = ({ question, answer, searchResults, factualConsistencySc
</span>
)}
</VuiText>

{factualConsistencyScore && (
<>
<VuiSpacer size="xs" />
{factualConsistencyScore}
</>
)}

{reorderedSearchResults && reorderedSearchResults.length > 0 && (
<>
<VuiSpacer size="s" />
Expand All @@ -140,9 +138,11 @@ export const ChatItem = ({ question, answer, searchResults, factualConsistencySc

return (
<>
<div className="vrcbChatMessageContainer vrcbChatMessageContainer--question">
<div className="vrcbChatMessage">{question}</div>
</div>
{question && (
<div className="vrcbChatMessageContainer vrcbChatMessageContainer--question">
<div className="vrcbChatMessage">{question}</div>
</div>
)}

<VuiSpacer size="xs" />

Expand Down
8 changes: 4 additions & 4 deletions src/components/ChatReferences.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {VuiFlexContainer, VuiFlexItem, VuiText, VuiAccordion, VuiSpacer} from "../vui";
import { VuiFlexContainer, VuiFlexItem, VuiText, VuiAccordion, VuiSpacer } from "../vui";
import { SearchResultWithSnippet } from "../types";
import {parseSnippet} from "../utils/parseSnippet";
import { parseSnippet } from "../utils/parseSnippet";

type Props = {
searchResults: SearchResultWithSnippet[];
Expand Down Expand Up @@ -32,7 +32,7 @@ export const ChatReferences = ({ searchResults, isOpen = false, setIsOpen = () =

const ChatReference = ({ result, position }: { result: SearchResultWithSnippet; position: number }) => {
const url = result.document_metadata.url as string;
const { text } = parseSnippet(result?.snippet?.text)
const { text } = parseSnippet(result?.snippet?.text);
return (
<>
<VuiFlexContainer alignItems="start" spacing="s">
Expand All @@ -48,7 +48,7 @@ const ChatReference = ({ result, position }: { result: SearchResultWithSnippet;
{text}
</a>
) : (
text
text
)}
</p>
</VuiText>
Expand Down
96 changes: 71 additions & 25 deletions src/components/ChatView.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { Fragment, ReactNode, useEffect, useMemo, useRef, useState } from "react";
import { VuiButtonSecondary, VuiContextProvider, VuiFlexContainer, VuiFlexItem, VuiSpacer } from "../vui";
import {
VuiButtonSecondary,
VuiContextProvider,
VuiFlexContainer,
VuiFlexItem,
VuiSpacer,
VuiTopicButton
} from "../vui";
import { QueryInput } from "./QueryInput";
import { ChatItem } from "./ChatItem";
import { useChat } from "../useChat";
import { Loader } from "./Loader";
import { MinimizeIcon } from "./Icons";
import { FactualConsistencyBadge } from "./FactualConsistencyBadge";
import { ExampleQuestions } from "./exampleQuestions/ExampleQuestions";
import {RerankerId, SummaryLanguage} from "types";
import { AgenticConfiguration, ChatActionOption, RerankerId, SummaryLanguage } from "types";

const inputSizeToQueryInputSize = {
large: "l",
Expand Down Expand Up @@ -66,6 +73,13 @@ export interface Props {

// Enables streaming responses from the API. Defaults to true.
enableStreaming?: boolean;

// Enables the chatbot to modify its behavior by sending requests to an agentic service.
agenticConfiguration?: AgenticConfiguration;

// A string that allows the Vectara platform to track where chat requests are coming from.
// This could be an application name, for example.
requestSource?: string;
}

/**
Expand All @@ -91,6 +105,8 @@ export const ChatView = ({
rerankerId,
lambda,
enableStreaming = true,
agenticConfiguration,
requestSource
}: Props) => {
const [isOpen, setIsOpen] = useState<boolean>(isInitiallyOpen ?? false);
const [query, setQuery] = useState<string>("");
Expand All @@ -105,7 +121,9 @@ export const ChatView = ({
summaryPromptName,
rerankerId,
lambda,
enableStreaming
enableStreaming,
agenticConfiguration,
requestSource
});

const appLayoutRef = useRef<HTMLDivElement>(null);
Expand Down Expand Up @@ -150,27 +168,53 @@ export const ChatView = ({

const historyItems = useMemo(
() =>
messageHistory.map((turn, index) => {
const { question, answer, results, factualConsistencyScore } = turn;
const onRetry =
hasError && index === messageHistory.length - 1
? () => sendMessage({ query: question, isRetry: true })
: undefined;

return (
<Fragment key={index}>
<ChatItem
question={question}
answer={answer}
searchResults={results}
factualConsistencyScore={
enableFactualConsistencyScore && <FactualConsistencyBadge score={factualConsistencyScore} />
}
onRetry={onRetry}
/>
{index < messageHistory.length - 1 && <VuiSpacer size="m" />}
</Fragment>
);
messageHistory.map((messageHistoryItem, index) => {
if (messageHistoryItem.type === "action") {
const { options } = messageHistoryItem;

return (
<Fragment key={index}>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason for using Fragment here, since there's only 1 child?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't recall, TBH. Could have been just to cleanly wrap it and have the key there instead of mixed in with the div that has a bunch of classes.

<div className="vrcbChatMessageContainer vrcbChatMessageContainer--actionResponse">
<VuiFlexContainer spacing="m">
{options?.map((option: ChatActionOption, optionIndex: number) => (
<VuiTopicButton
key={`messageHistoryItem-${index}-actionOption-${optionIndex}`}
href={option.url}
title={option.label}
onClick={() => {
if (option.message) {
sendMessage({ query: option.message });
}
option.onSelect?.();
}}
/>
))}
</VuiFlexContainer>
</div>
</Fragment>
);
} else {
const { question, answer, results, factualConsistencyScore } = messageHistoryItem;
const onRetry =
hasError && index === messageHistory.length - 1
? () => sendMessage({ query: question, isRetry: true })
: undefined;

return (
<Fragment key={index}>
<ChatItem
question={question}
answer={answer}
searchResults={results}
factualConsistencyScore={
enableFactualConsistencyScore && <FactualConsistencyBadge score={factualConsistencyScore} />
}
onRetry={onRetry}
/>
{index < messageHistory.length - 1 && <VuiSpacer size="m" />}
</Fragment>
);
}
}),
[messageHistory]
);
Expand All @@ -180,8 +224,10 @@ export const ChatView = ({

const onSendQuery = (queryOverride?: string) => {
if (isRequestDisabled && !queryOverride) return;
sendMessage({ query: queryOverride ?? query });

setQuery("");

sendMessage({ query: queryOverride ?? query });
};

const spacer = historyItems.length === 0 ? null : <VuiSpacer size={activeMessage ? "m" : "l"} />;
Expand Down
12 changes: 12 additions & 0 deletions src/components/chatView.scss
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ $chatbotPosition: $sizeS;
}
}

.vrcbChatMessageContainer--actionResponse {
justify-content: flex-start;
padding: $sizeM $sizeXxl $sizeM $sizeS;

.vrcbChatMessage {
color: $colorText;
font-weight: $fontWeightBold;
font-size: $fontSizeStandard;
padding-left: 0;
}
}

.vrcbChatMessageContainer--thinking,
.vrcbChatMessageContainer--answer {
padding: 0 $sizeXxl;
Expand Down
Loading