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

[8.14] [Search] [Playground] present message when no documents are returned (#183219) #183251

Merged
merged 1 commit into from
May 13, 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
19 changes: 18 additions & 1 deletion docs/playground/playground-query.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,21 @@ Refer to <<playground-context, Optimize context>> for more information.
Retrievers make it easier to compose and test different retrieval strategies in your search pipelines.
====
// TODO: uncomment and add to note once following page is live
//Refer to {ref}/retrievers-overview.html[documentation] for a high level overview of retrievers.
Refer to {ref}/retrievers-overview.html[documentation] for a high level overview of retrievers.
[float]
[[playground-hidden-fields]]
=== Hidden fields

The query editor shows fields which make sense for the user to search on, but not all fields in your documents are visible from the editor.

Available field types:

- Semantic fields like `sparse_vector` or `dense_vector` fields where the embeddings have been created from a inference processor
- `text` fields

Hidden Field Types:

- non `text` fields like `keyword` fields
- fields that are not indexed
- semantic fields where the embeddings have not been created from a inference processor
- nested fields
4 changes: 4 additions & 0 deletions packages/kbn-doc-links/src/get_doc_links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,10 @@ export const getDocLinks = ({ kibanaBranch, buildFlavor }: GetDocLinkOptions): D
},
playground: {
chatPlayground: `${KIBANA_DOCS}playground.html`,
retrievalOptimize: `${KIBANA_DOCS}playground-query.html#playground-query-relevance`,
retrieval: `${KIBANA_DOCS}playground-query.html`,
context: `${KIBANA_DOCS}playground-context.html`,
hiddenFields: `${KIBANA_DOCS}playground-query.html#playground-hidden-fields`,
},
});
};
4 changes: 4 additions & 0 deletions packages/kbn-doc-links/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,10 @@ export interface DocLinks {
};
readonly playground: {
readonly chatPlayground: string;
readonly retrievalOptimize: string;
readonly retrieval: string;
readonly context: string;
readonly hiddenFields: string;
};
}

Expand Down
8 changes: 8 additions & 0 deletions x-pack/plugins/search_playground/common/doc_links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@ import { DocLinks } from '@kbn/doc-links';

class PlaygroundDocLinks {
public chatPlayground: string = '';
public retrievalOptimize: string = '';
public retrieval: string = '';
public context: string = '';
public hiddenFields: string = '';

constructor() {}

setDocLinks(newDocLinks: DocLinks) {
this.chatPlayground = newDocLinks.playground.chatPlayground;
this.retrievalOptimize = newDocLinks.playground.retrievalOptimize;
this.retrieval = newDocLinks.playground.retrieval;
this.context = newDocLinks.playground.context;
this.hiddenFields = newDocLinks.playground.hiddenFields;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export const EditContextFlyout: React.FC<EditContextFlyoutProps> = ({ onClose })
defaultMessage="Context is the information you provide to the LLM, by selecting fields from your Elasticsearch documents. Optimize context for better results."
/>
<EuiLink
href={docLinks.chatPlayground}
href={docLinks.context}
target="_blank"
data-test-subj="context-optimization-documentation-link"
>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import React from 'react';
import { render, screen } from '@testing-library/react';
import { AssistantMessage } from './assistant_message';
import { FormProvider, useForm } from 'react-hook-form';
import { __IntlProvider as IntlProvider } from '@kbn/i18n-react';

// for tooltip
jest.mock('../../hooks/use_llms_models', () => ({
useLLMsModels: () => [
{ value: 'model1', promptTokenLimit: 100 },
{ value: 'model2', promptTokenLimit: 200 },
],
}));

const MockFormProvider = ({ children }: { children: React.ReactElement }) => {
const methods = useForm({
values: {
indices: ['index1', 'index2'],
},
});
return (
<IntlProvider locale="en">
<FormProvider {...methods}>{children}</FormProvider>
</IntlProvider>
);
};

describe('AssistantMessage component', () => {
const mockMessage = {
content: 'Test content',
createdAt: new Date(),
citations: [],
retrievalDocs: [{ content: '', metadata: { _id: '1', _index: 'index', _score: 1 } }],
inputTokens: { context: 20, total: 10 },
};

it('renders message content correctly', () => {
const { getByText } = render(
<MockFormProvider>
<AssistantMessage message={mockMessage} />
</MockFormProvider>
);
expect(getByText('Test content')).toBeInTheDocument();
expect(screen.getByTestId('retrieval-docs-comment')).toBeInTheDocument();
expect(screen.getByTestId('retrieval-docs-button')).toHaveTextContent('1 document sources');
expect(screen.getByTestId('token-tooltip-button')).toHaveTextContent('10 tokens sent');
});

it('renders message content correctly with no retrieval docs', () => {
const { getByText } = render(
<MockFormProvider>
<AssistantMessage
message={{
...mockMessage,
retrievalDocs: [],
}}
/>
</MockFormProvider>
);
expect(getByText('Test content')).toBeInTheDocument();
expect(screen.queryByTestId('retrieval-docs-comment')).not.toBeInTheDocument();
expect(screen.getByTestId('retrieval-docs-comment-no-docs')).toBeInTheDocument();
});

it('renders message content correctly with citations', () => {
render(
<MockFormProvider>
<AssistantMessage
message={{
...mockMessage,
citations: [
{ content: 'Citation content', metadata: { _id: '1', _index: 'index', _score: 1 } },
],
}}
/>
</MockFormProvider>
);

expect(screen.getByTestId('assistant-message-citations')).toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
EuiButtonEmpty,
EuiComment,
EuiFlexGroup,
EuiLink,
EuiSpacer,
EuiText,
EuiTitle,
Expand All @@ -21,6 +22,7 @@ import { i18n } from '@kbn/i18n';

import { FormattedMessage } from '@kbn/i18n-react';
import { css } from '@emotion/react';
import { docLinks } from '../../../common/doc_links';
import { RetrievalDocsFlyout } from './retrieval_docs_flyout';
import type { AIMessage as AIMessageType } from '../../types';

Expand Down Expand Up @@ -52,6 +54,7 @@ export const AssistantMessage: React.FC<AssistantMessageProps> = ({ message }) =
<EuiComment
username={username}
timelineAvatar="dot"
data-test-subj="retrieval-docs-comment"
event={
<>
<EuiText size="s">
Expand All @@ -68,6 +71,7 @@ export const AssistantMessage: React.FC<AssistantMessageProps> = ({ message }) =
css={{ blockSize: 'auto' }}
size="s"
flush="left"
data-test-subj="retrieval-docs-button"
onClick={() => setIsDocsFlyoutOpen(true)}
>
<FormattedMessage
Expand All @@ -87,11 +91,50 @@ export const AssistantMessage: React.FC<AssistantMessageProps> = ({ message }) =
}
/>
)}
{retrievalDocs?.length === 0 && (
<EuiComment
username={username}
timelineAvatar="dot"
data-test-subj="retrieval-docs-comment-no-docs"
event={
<>
<EuiText size="s">
<p>
<FormattedMessage
id="xpack.searchPlayground.chat.message.assistant.noRetrievalDocs"
defaultMessage="Unable to retrieve documents based on the provided question and query."
/>
{` `}
</p>
</EuiText>

<EuiLink
href={docLinks.retrievalOptimize}
target="_blank"
data-test-subj="retrieval-optimization-documentation-link"
>
<FormattedMessage
id="xpack.searchPlayground.chat.message.assistant.noRetrievalDocs.learnMore"
defaultMessage=" Learn more."
/>
</EuiLink>

{isDocsFlyoutOpen && (
<RetrievalDocsFlyout
onClose={() => setIsDocsFlyoutOpen(false)}
retrievalDocs={retrievalDocs}
/>
)}
</>
}
/>
)}
<EuiComment
username={username}
event={i18n.translate('xpack.searchPlayground.chat.message.assistant.event.responded', {
defaultMessage: 'responded',
})}
data-test-subj="assistant-message"
timestamp={
createdAt &&
i18n.translate('xpack.searchPlayground.chat.message.assistant.createdAt', {
Expand Down Expand Up @@ -137,7 +180,7 @@ export const AssistantMessage: React.FC<AssistantMessageProps> = ({ message }) =
{!!citations?.length && (
<>
<EuiSpacer size="l" />
<EuiTitle size="xs">
<EuiTitle size="xs" data-test-subj="assistant-message-citations">
<p>
<FormattedMessage
id="xpack.searchPlayground.chat.message.assistant.citations.title"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ describe('SourcesPanelForStartChat component', () => {
noFieldsIndicesWarning: 'index1',
});

render(<SourcesPanelForStartChat />);
render(<SourcesPanelForStartChat />, { wrapper: Wrapper });
expect(screen.getByTestId('NoIndicesFieldsMessage')).toBeInTheDocument();
expect(screen.getByTestId('NoIndicesFieldsMessage')).toHaveTextContent('index1');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,17 @@ export const ViewQueryFlyout: React.FC<ViewQueryFlyoutProps> = ({ onClose }) =>
defaultMessage="This query will be used to search your indices. Customize by choosing which
fields in your Elasticsearch documents to search."
/>
{` `}
<EuiLink
href={docLinks.retrievalOptimize}
target="_blank"
data-test-subj="query-optimize-documentation-link"
>
<FormattedMessage
id="xpack.searchPlayground.viewQuery.flyout.learnMoreQueryOptimizeLink"
defaultMessage="Learn more."
/>
</EuiLink>
</p>
</EuiText>
</EuiFlyoutHeader>
Expand Down Expand Up @@ -236,9 +247,9 @@ export const ViewQueryFlyout: React.FC<ViewQueryFlyoutProps> = ({ onClose }) =>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiLink
href={docLinks.chatPlayground}
href={docLinks.hiddenFields}
target="_blank"
data-test-subj="context-optimization-documentation-link"
data-test-subj="hidden-fields-documentation-link"
>
<FormattedMessage
id="xpack.searchPlayground.viewQuery.flyout.learnMoreLink"
Expand Down
Loading