|
| 1 | +import {service} from '@loopback/core'; |
| 2 | +import {graphNode} from '../../../decorators'; |
| 3 | +import {IGraphNode, LLMStreamEventType, RunnableConfig} from '../../../graphs'; |
| 4 | +import {DbQueryGraph, POST_DATASET_TAG} from '../../db-query'; |
| 5 | +import {VisualizationGraphNodes} from '../nodes.enum'; |
| 6 | +import {VisualizationGraphState} from '../state'; |
| 7 | + |
| 8 | +@graphNode(VisualizationGraphNodes.CallQueryGeneration, { |
| 9 | + [POST_DATASET_TAG]: true, |
| 10 | +}) |
| 11 | +export class CallQueryGenerationNode |
| 12 | + implements IGraphNode<VisualizationGraphState> |
| 13 | +{ |
| 14 | + constructor( |
| 15 | + @service(DbQueryGraph) |
| 16 | + private readonly queryPipeline: DbQueryGraph, |
| 17 | + ) {} |
| 18 | + async execute( |
| 19 | + state: VisualizationGraphState, |
| 20 | + config: RunnableConfig, |
| 21 | + ): Promise<VisualizationGraphState> { |
| 22 | + if (state.datasetId) { |
| 23 | + return state; |
| 24 | + } |
| 25 | + |
| 26 | + const queryGraph = await this.queryPipeline.build(); |
| 27 | + |
| 28 | + const result = await queryGraph.invoke( |
| 29 | + { |
| 30 | + datasetId: state.datasetId, |
| 31 | + directCall: true, |
| 32 | + prompt: `Generate a query to fetch data for visualization based on the following user prompt: ${state.prompt}.${state.visualizer?.context ? ` Ensure that the query structure satisfies the following context: ${state.visualizer.context}` : ''}`, |
| 33 | + }, |
| 34 | + config, |
| 35 | + ); |
| 36 | + |
| 37 | + if (!result.datasetId) { |
| 38 | + config.writer?.({ |
| 39 | + type: LLMStreamEventType.Error, |
| 40 | + data: { |
| 41 | + status: `Failed to create dataset for visualization: ${result.replyToUser ?? 'Unknown error'}`, |
| 42 | + }, |
| 43 | + }); |
| 44 | + return { |
| 45 | + ...state, |
| 46 | + error: |
| 47 | + result.replyToUser ?? 'Failed to create dataset for visualization', |
| 48 | + }; |
| 49 | + } |
| 50 | + |
| 51 | + return { |
| 52 | + ...state, |
| 53 | + datasetId: result.datasetId, |
| 54 | + }; |
| 55 | + } |
| 56 | +} |
0 commit comments