Skip to content

Commit

Permalink
Merge pull request #313 from ruanyl/use-two-agent-for-t2viz
Browse files Browse the repository at this point in the history
feat: use two agents for text2viz
(cherry picked from commit 4a3fc51)
Signed-off-by: Yulong Ruan <[email protected]>
  • Loading branch information
xluo-aws authored and ruanyl committed Sep 20, 2024
1 parent b90978c commit f4d1df3
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 13 deletions.
3 changes: 2 additions & 1 deletion common/constants/llm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ export const DEFAULT_USER_NAME = 'User';

export const TEXT2VEGA_INPUT_SIZE_LIMIT = 400;

export const TEXT2VEGA_AGENT_CONFIG_ID = 'os_text2vega';
export const TEXT2VEGA_RULE_BASED_AGENT_CONFIG_ID = 'os_text2vega';
export const TEXT2VEGA_WITH_INSTRUCTIONS_AGENT_CONFIG_ID = 'os_text2vega_with_instructions';
export const TEXT2PPL_AGENT_CONFIG_ID = 'os_query_assist_ppl';
21 changes: 16 additions & 5 deletions public/components/visualization/source_selector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import {
DataSourceOption,
} from '../../../../../src/plugins/data/public';
import { StartServices } from '../../types';
import { TEXT2VEGA_AGENT_CONFIG_ID } from '../../../common/constants/llm';
import {
TEXT2PPL_AGENT_CONFIG_ID,
TEXT2VEGA_RULE_BASED_AGENT_CONFIG_ID,
TEXT2VEGA_WITH_INSTRUCTIONS_AGENT_CONFIG_ID,
} from '../../../common/constants/llm';
import { getAssistantService } from '../../services';

const DEFAULT_DATA_SOURCE_TYPE = 'DEFAULT_INDEX_PATTERNS';
Expand Down Expand Up @@ -110,13 +114,20 @@ export const SourceSelector = ({

const assistantService = getAssistantService();
/**
* Check each data source to see if text to vega agent is configured or not
* Check each data source to see if text to vega agents are configured or not
* If not configured, disable the corresponding index pattern from the selection list
*/
Object.keys(dataSourceIdToIndexPatternIds).forEach(async (key) => {
const res = await assistantService.client.agentConfigExists(TEXT2VEGA_AGENT_CONFIG_ID, {
dataSourceId: key !== 'DEFAULT' ? key : undefined,
});
const res = await assistantService.client.agentConfigExists(
[
TEXT2VEGA_RULE_BASED_AGENT_CONFIG_ID,
TEXT2VEGA_WITH_INSTRUCTIONS_AGENT_CONFIG_ID,
TEXT2PPL_AGENT_CONFIG_ID,
],
{
dataSourceId: key !== 'DEFAULT' ? key : undefined,
}
);
if (!res.exists) {
dataSourceIdToIndexPatternIds[key].forEach((indexPatternId) => {
indexPatternOptions.options.forEach((option) => {
Expand Down
5 changes: 3 additions & 2 deletions public/services/assistant_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ export class AssistantClient {
};

/**
* Return if the given agent config name has agent id configured
* Check if the given agent config name has agent id configured
* Return false if any of the given config name has no agent id configured
*/
agentConfigExists = (agentConfigName: string, options?: Options) => {
agentConfigExists = (agentConfigName: string | string[], options?: Options) => {
return this.http.fetch<{ exists: boolean }>({
method: 'GET',
path: AGENT_API.CONFIG_EXISTS,
Expand Down
10 changes: 7 additions & 3 deletions server/routes/agent_routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,20 @@ export function registerAgentRoutes(router: IRouter, assistantService: Assistant
query: schema.oneOf([
schema.object({
dataSourceId: schema.maybe(schema.string()),
agentConfigName: schema.string(),
agentConfigName: schema.oneOf([schema.string(), schema.arrayOf(schema.string())]),
}),
]),
},
},
router.handleLegacyErrors(async (context, req, res) => {
try {
const assistantClient = assistantService.getScopedClient(req, context);
const agentId = await assistantClient.getAgentIdByConfigName(req.query.agentConfigName);
return res.ok({ body: { exists: Boolean(agentId) } });
const promises = Array<string>()
.concat(req.query.agentConfigName)
.map((configName) => assistantClient.getAgentIdByConfigName(configName));
const results = await Promise.all(promises);
const exists = results.every((r) => Boolean(r));
return res.ok({ body: { exists } });
} catch (e) {
return res.ok({ body: { exists: false } });
}
Expand Down
8 changes: 6 additions & 2 deletions server/routes/text2viz_routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import { schema } from '@osd/config-schema';
import { IRouter } from '../../../../src/core/server';
import {
TEXT2PPL_AGENT_CONFIG_ID,
TEXT2VEGA_AGENT_CONFIG_ID,
TEXT2VEGA_RULE_BASED_AGENT_CONFIG_ID,
TEXT2VEGA_INPUT_SIZE_LIMIT,
TEXT2VEGA_WITH_INSTRUCTIONS_AGENT_CONFIG_ID,
TEXT2VIZ_API,
} from '../../common/constants/llm';
import { AssistantServiceSetup } from '../services/assistant_service';
Expand Down Expand Up @@ -42,7 +43,10 @@ export function registerText2VizRoutes(router: IRouter, assistantService: Assist
router.handleLegacyErrors(async (context, req, res) => {
const assistantClient = assistantService.getScopedClient(req, context);
try {
const response = await assistantClient.executeAgentByConfigName(TEXT2VEGA_AGENT_CONFIG_ID, {
const agentConfigName = req.body.input_instruction
? TEXT2VEGA_WITH_INSTRUCTIONS_AGENT_CONFIG_ID
: TEXT2VEGA_RULE_BASED_AGENT_CONFIG_ID;
const response = await assistantClient.executeAgentByConfigName(agentConfigName, {
input_question: req.body.input_question,
input_instruction: req.body.input_instruction,
ppl: req.body.ppl,
Expand Down

0 comments on commit f4d1df3

Please sign in to comment.