Skip to content

Commit

Permalink
feat: limit text to vega input size to 400
Browse files Browse the repository at this point in the history
+ change the agent name of text2vega

Signed-off-by: Yulong Ruan <[email protected]>
  • Loading branch information
ruanyl committed Sep 9, 2024
1 parent d4646cb commit 88707f5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
2 changes: 2 additions & 0 deletions common/constants/llm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ export const NOTEBOOK_API = {
};

export const DEFAULT_USER_NAME = 'User';

export const TEXT2VEGA_INPUT_SIZE_LIMIT = 400;
21 changes: 20 additions & 1 deletion public/components/visualization/text2viz.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import { NLQVisualizationInput } from './embeddable/types';
import { EditorPanel } from './editor_panel';
import { VIS_NLQ_SAVED_OBJECT } from '../../../common/constants/vis_type_nlq';
import { HeaderVariant } from '../../../../../src/core/public';
import { TEXT2VEGA_INPUT_SIZE_LIMIT } from '../../../common/constants/llm';

export const Text2Viz = () => {
const { savedObjectId } = useParams<{ savedObjectId?: string }>();
Expand Down Expand Up @@ -165,6 +166,24 @@ export const Text2Viz = () => {
const onSubmit = useCallback(async () => {
if (status === 'RUNNING' || !selectedSource) return;

const [inputQuestion = '', inputInstruction = ''] = input.split('//');
if (
inputQuestion.trim().length > TEXT2VEGA_INPUT_SIZE_LIMIT ||
inputInstruction.trim().length > TEXT2VEGA_INPUT_SIZE_LIMIT
) {
notifications.toasts.addDanger({
title: i18n.translate('dashboardAssistant.feature.text2viz.invalidInput', {
defaultMessage: `Input size exceed limit: {limit}. Actual size: question({inputQuestionLength}), instruction({inputInstructionLength})`,
values: {
limit: TEXT2VEGA_INPUT_SIZE_LIMIT,
inputQuestionLength: inputQuestion.trim().length,
inputInstructionLength: inputInstruction.trim().length,
},
}),
});
return;
}

setSubmitting(true);

const indexPatterns = getIndexPatterns();
Expand Down Expand Up @@ -332,7 +351,7 @@ export const Text2Viz = () => {
<EuiButtonIcon
aria-label="submit"
onClick={onSubmit}
isDisabled={loading}
isDisabled={loading || input.trim().length === 0}
display="base"
size="s"
iconType="returnKey"
Expand Down
19 changes: 14 additions & 5 deletions server/routes/text2viz_routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,29 @@

import { schema } from '@osd/config-schema';
import { IRouter } from '../../../../src/core/server';
import { TEXT2VIZ_API } from '../../common/constants/llm';
import { TEXT2VEGA_INPUT_SIZE_LIMIT, TEXT2VIZ_API } from '../../common/constants/llm';
import { AssistantServiceSetup } from '../services/assistant_service';

const TEXT2VEGA_AGENT_CONFIG_ID = 'text2vega';
const TEXT2VEGA_AGENT_CONFIG_ID = 'os_text2vega';
const TEXT2PPL_AGENT_CONFIG_ID = 'os_query_assist_ppl';

const inputSchema = schema.string({
maxLength: TEXT2VEGA_INPUT_SIZE_LIMIT,
validate(value) {
if (!value || value.trim().length === 0) {
return "can't be empty or blank.";
}
},
});

export function registerText2VizRoutes(router: IRouter, assistantService: AssistantServiceSetup) {
router.post(
{
path: TEXT2VIZ_API.TEXT2VEGA,
validate: {
body: schema.object({
input_question: schema.string(),
input_instruction: schema.maybe(schema.string()),
input_question: inputSchema,
input_instruction: schema.maybe(inputSchema),
ppl: schema.string(),
dataSchema: schema.string(),
sampleData: schema.string(),
Expand Down Expand Up @@ -65,7 +74,7 @@ export function registerText2VizRoutes(router: IRouter, assistantService: Assist
validate: {
body: schema.object({
index: schema.string(),
question: schema.string(),
question: inputSchema,
}),
query: schema.object({
dataSourceId: schema.maybe(schema.string()),
Expand Down

0 comments on commit 88707f5

Please sign in to comment.