-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbar.visualizer.ts
More file actions
79 lines (72 loc) · 2.88 KB
/
bar.visualizer.ts
File metadata and controls
79 lines (72 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import {PromptTemplate} from '@langchain/core/prompts';
import {IVisualizer} from '../types';
import {AiIntegrationBindings} from '../../../keys';
import {LLMProvider} from '../../../types';
import {inject} from '@loopback/core';
import {AnyObject} from '@loopback/repository';
import {VisualizationGraphState} from '../state';
import z from 'zod';
import {RunnableSequence} from '@langchain/core/runnables';
import {visualizer} from '../decorators/visualizer.decorator';
@visualizer()
export class BarVisualizer implements IVisualizer {
name = 'bar';
description = `Renders the data in a bar chart format. Best for comparing values across different categories or showing trends over time.`;
renderPrompt = PromptTemplate.fromTemplate(`
<instructions>
You are an expert data visualization assistant. Your task is to create a bar chart config based on the provided SQL query, it's description and user prompt. Follow these steps:
1. Analyze the SQL query results to understand the data structure.
2. Identify the category column (x-axis) and value column (y-axis) for the bar chart.
3. Create a configuration object for the bar chart using the identified columns.
4. Return the bar chart configuration object.
</instructions>
<inputs>
<sql>
{sql}
</sql>
<description>
{description}
</description>
<user-prompt>
{userPrompt}
</user-prompt>
</inputs>`);
context?: string | undefined =
`A bar chart requires data with at exactly two columns: one for the categories (x-axis) and one for the values (y-axis). Ensure that the category column contains discrete values representing different groups or categories, while the value column contains numerical data that can be compared across these categories. Bar charts can be oriented either vertically or horizontally depending on the data representation needs.`;
schema = z.object({
categoryColumn: z
.string()
.describe('Column to be used for categories (x-axis) in the bar chart'),
valueColumn: z
.string()
.describe('Column to be used for values (y-axis) in the bar chart'),
orientation: z
.string()
.default('vertical')
.describe(
'Orientation of the bar chart: `vertical` or `horizontal` without backticks',
),
}) as z.AnyZodObject;
constructor(
@inject(AiIntegrationBindings.CheapLLM)
private readonly llm: LLMProvider,
) {}
async getConfig(state: VisualizationGraphState): Promise<AnyObject> {
if (!state.sql || !state.queryDescription || !state.prompt) {
throw new Error('Invalid State');
}
const llmWithStructuredOutput = this.llm.withStructuredOutput<AnyObject>(
this.schema,
);
const chain = RunnableSequence.from([
this.renderPrompt,
llmWithStructuredOutput,
]);
const settings = await chain.invoke({
sql: state.sql!,
description: state.queryDescription!,
userPrompt: state.prompt!,
});
return settings;
}
}