(query: string, dataModel: DataModel)
| 21 | constructor(private readonly llmConfig: LLMConfig, private readonly execute?: PythonEngineOptions['execute']) {} |
| 22 | |
| 23 | async getDSL(query: string, dataModel: DataModel): Promise<TranslationResult> { |
| 24 | const { text, usage, finishReason } = await generateText({ |
| 25 | model: languageModel(this.llmConfig), |
| 26 | maxRetries: this.llmConfig.maxRetries ?? 3, |
| 27 | prompt: `You are a data analysis expert. Generate Python code to answer the question. |
| 28 | |
| 29 | Dataset Context: |
| 30 | ${stringifyDataModel(dataModel)} |
| 31 | |
| 32 | User Query: ${query} |
| 33 | |
| 34 | ${this.rules} |
| 35 | |
| 36 | Return only executable Python code, without explanation or Markdown.`, |
| 37 | }); |
| 38 | if (finishReason !== 'stop' && finishReason !== 'other') { |
| 39 | throw new Error(`Python generation did not complete (${finishReason})`); |
| 40 | } |
| 41 | const dsl = text.trim().match(/^```(?:python|py)?[ \t]*\r?\n([\s\S]*?)\r?\n```$/i)?.[1] ?? text; |
| 42 | if (!dsl.trim()) throw new Error('Python generation returned empty code'); |
| 43 | return { dsl, usage }; |
| 44 | } |
| 45 | |
| 46 | async validateDSL(code: string): Promise<string> { |
| 47 | if (!code.trim()) throw new Error('Python query must contain code'); |
nothing calls this directly
no test coverage detected