Skip to content

Commit

Permalink
feature: fixed followup questions issues
Browse files Browse the repository at this point in the history
  • Loading branch information
cdxker committed Jan 9, 2025
1 parent 394b940 commit 3722ec1
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 23 deletions.
2 changes: 1 addition & 1 deletion clients/search-component/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"import": "./dist/vanilla/index.js"
}
},
"version": "0.3.6",
"version": "0.3.7",
"license": "MIT",
"homepage": "https://github.com/devflowinc/trieve/tree/main/clients/search-component",
"scripts": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ export const Message = ({
: null}
{props.followupQuestions && messages.length == idx + 1 &&
<FollowupQueries /> }
{isDoneReading && <div className="feedback-wrapper">
{isDoneReading && messages.length == idx + 1 && <div className="feedback-wrapper">
<span className="spacer"></span>
<div className="feedback-icons">
{copied ? (
Expand Down
2 changes: 2 additions & 0 deletions clients/search-component/src/utils/hooks/modal-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export type ModalProps = {
showFloatingSearchIcon?: boolean;
showFloatingInput?: boolean;
inlineHeader?: string;
suggestedQueriesContext: string;
};

const defaultProps = {
Expand Down Expand Up @@ -152,6 +153,7 @@ const defaultProps = {
showFloatingInput: false,
inline: false,
inlineHeader: "AI Assistant by Trieve",
suggestedQueriesContext: "You are an ecommerce sales assistant to help people buy items."
};

const ModalContext = createContext<{
Expand Down
20 changes: 7 additions & 13 deletions clients/search-component/src/utils/hooks/useFollowupQuestions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { useEffect, useState } from "react";
import { SuggestedQueriesResponse } from "trieve-ts-sdk";
import { getSuggestedQuestions } from "../trieve";
import { useModalState } from "./modal-context";
import { useChatState } from "./chat-context";

export const useFollowupQuestions = () => {
const { query, trieveSDK, currentGroup, props } = useModalState();
const { trieveSDK, currentGroup, props } = useModalState();
const { currentQuestion } = useChatState();
const [isLoading, setIsLoading] = useState(false);
const [suggestedQuestions, setSuggestedQuestions] = useState<
SuggestedQueriesResponse["queries"]
Expand All @@ -14,11 +16,12 @@ export const useFollowupQuestions = () => {
setIsLoading(true);
const queries = await getSuggestedQuestions({
trieve: trieveSDK,
query,
query: currentQuestion,
count: props.numberOfSuggestions ?? 3,
context: props.suggestedQueriesContext,
group: currentGroup
});
setSuggestedQuestions(queries.queries.splice(0, 3));
setSuggestedQuestions(queries.queries);
setIsLoading(false);
};

Expand All @@ -31,16 +34,7 @@ export const useFollowupQuestions = () => {
const abortController = new AbortController();

const timeoutId = setTimeout(async () => {
const queries = await getSuggestedQuestions({
trieve: trieveSDK,
abortController,
group: currentGroup,
query,
count: props.numberOfSuggestions ?? 3,
context: "You are an assistant searching through a docs website"
});
setSuggestedQuestions(queries.queries);
setIsLoading(false);
getQuestions();
});

return () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const useSuggestedQueries = () => {
trieve: trieveSDK,
query,
count: props.numberOfSuggestions ?? 3,
context: props.suggestedQueriesContext,
abortController,
});
setSuggestedQueries(queries.queries);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const useSuggestedQuestions = () => {
const queries = await getSuggestedQuestions({
trieve: trieveSDK,
count: props.numberOfSuggestions ?? 3,
context: props.suggestedQueriesContext,
query,
});
setSuggestedQuestions(queries.queries);
Expand All @@ -38,6 +39,7 @@ export const useSuggestedQuestions = () => {
const queries = await getSuggestedQuestions({
trieve: trieveSDK,
count: props.numberOfSuggestions ?? 3,
context: props.suggestedQueriesContext,
abortController,
query,
});
Expand Down
13 changes: 5 additions & 8 deletions clients/search-component/src/utils/trieve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,13 @@ export const getSuggestedQueries = async ({
trieve,
query,
count,
context,
abortController,
}: {
query: string;
trieve: TrieveSDK;
count: number;
context: string;
abortController?: AbortController;
}) => {
return trieve.suggestedQueries(
Expand All @@ -272,7 +274,7 @@ export const getSuggestedQueries = async ({
suggestion_type: "keyword",
suggestions_to_create: count,
search_type: "semantic",
context: "You are a user searching through a docs website",
context: context
},
abortController?.signal
);
Expand All @@ -288,22 +290,17 @@ export const getSuggestedQuestions = async ({
}: {
trieve: TrieveSDK;
abortController?: AbortController;
context?: string;
context: string;
query?: string;
count: number;
group?: ChunkGroup | null;
}) => {
let suggestedQueriesContext =
"You are a user searching through a docs website";
if (context) {
suggestedQueriesContext = context;
}
return trieve.suggestedQueries(
{
...(query && { query }),
suggestion_type: "question",
search_type: "semantic",
context: suggestedQueriesContext,
context: context,
suggestions_to_create: count,
...(group &&
group?.tracking_id && {
Expand Down
4 changes: 4 additions & 0 deletions clients/ts-sdk/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -12718,6 +12718,10 @@
"type": "boolean",
"nullable": true
},
"suggestedQueriesContext": {
"type": "string",
"nullable": true
},
"tabMessages": {
"type": "array",
"items": {
Expand Down
1 change: 1 addition & 0 deletions clients/ts-sdk/src/types.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2119,6 +2119,7 @@ export type PublicPageParameters = {
showFloatingSearchIcon?: (boolean) | null;
singleProductOptions?: ((SingleProductOptions) | null);
suggestedQueries?: (boolean) | null;
suggestedQueriesContext?: (string) | null;
tabMessages?: Array<PublicPageTabMessage> | null;
tags?: Array<PublicPageTag> | null;
theme?: ((PublicPageTheme) | null);
Expand Down
15 changes: 15 additions & 0 deletions frontends/dashboard/src/pages/dataset/PublicPageSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,21 @@ const PublicPageControls = () => {
/>
</div>

<div class="flex gap-4 pb-2 pt-2">
<div class="grow">
<label class="block">Suggested Queries Context</label>
<textarea
cols={2}
placeholder="You are an ecommerce sales assistant to help people buy items."
value={ extraParams.suggestedQueriesContext || ""}
onInput={(e) => {
setExtraParams("suggestedQueriesContext", e.currentTarget.value)
}}
class="block w-full rounded border border-neutral-300 px-3 py-1.5 shadow-sm placeholder:text-neutral-400 focus:outline-magenta-500 sm:text-sm sm:leading-6"
/>
</div>
</div>

<div class="grid grid-cols-2 gap-4">
<div class="flex gap-2">
<div class="flex items-center gap-1">
Expand Down
3 changes: 3 additions & 0 deletions server/src/data/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3297,6 +3297,9 @@ impl DatasetConfigurationDTO {
inline_header: page_parameters_self
.inline_header
.or(page_parameters_curr.inline_header),
suggested_queries_context: page_parameters_self
.suggested_queries_context
.or(page_parameters_curr.suggested_queries_context),
}),
},
DISABLE_ANALYTICS: self
Expand Down
2 changes: 2 additions & 0 deletions server/src/handlers/page_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@ pub struct PublicPageParameters {
pub number_of_suggestions: Option<usize>,
#[serde(skip_serializing_if = "Option::is_none")]
pub inline_header: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub suggested_queries_context: Option<String>,
}

#[utoipa::path(
Expand Down
9 changes: 9 additions & 0 deletions server/src/public/search-component-code.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,18 @@
{% if params.type -%}
type: "{{ params.type }}",
{% endif -%}
{% if params.suggestedQueries -%}
suggestedQueries: {{ params.suggestedQueries }},
{% endif -%}
{% if params.followupQuestions -%}
followupQuestions: {{ params.followupQuestions }},
{% endif -%}
{% if params.inline -%}
inline: {{ params.inline }},
{% endif -%}
{% if params.suggestedQueriesContext -%}
suggestedQueriesContext: "{{ params.suggestedQueriesContext }}",
{% endif -%}
{% if params.analytics -%}
analytics: {{ params.analytics }},
{% endif -%}
Expand Down

0 comments on commit 3722ec1

Please sign in to comment.