| 6 | import type { DataModel, LLMConfig, PythonEngineOptions, QueryDialect, TranslationResult } from '../types'; |
| 7 | |
| 8 | export class PythonDialect implements QueryDialect { |
| 9 | readonly id = 'python'; |
| 10 | |
| 11 | // Bindings and result conversion are supplied by the Python engine. |
| 12 | // https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.reset_index.html |
| 13 | readonly rules = `Use Python 3 and pandas for analysis. pandas is available as pd. Input DataFrames are in tables, keyed by schema table name. |
| 14 | CSV/JSON use tables['data']; Excel exposes every sheet as tables['SheetName']. |
| 15 | For a single table, the DataFrame df is also available. For multiple sheets use tables explicitly. |
| 16 | Assign the final output to result: a DataFrame, Series, list of records, record, or JSON scalar. |
| 17 | DataFrame and Series indexes are not returned; use reset_index() when index values matter. |
| 18 | Each execution starts from fresh input data; variables and mutations do not persist between calls. |
| 19 | Do not read files, access the network, install packages, or print the answer.`; |
| 20 | |
| 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'); |
| 48 | if (code.includes('\0')) throw new Error('Python query contains a NUL character'); |
| 49 | // Compile in the execution environment without running the user's code or loading source data. |
| 50 | // https://docs.python.org/3/library/functions.html#compile |
| 51 | const script = `import json |
| 52 | compile(json.loads(${JSON.stringify(JSON.stringify(code))}), '<ava-query>', 'exec') |
| 53 | print('{"data":[],"schema":[],"rowCount":0}') |
| 54 | `; |
| 55 | if (this.execute) { |
| 56 | await this.execute(script); |
| 57 | } else { |
| 58 | const { createPythonExecutor } = await import('../interpreter/python/util/execute'); |
| 59 | await createPythonExecutor(30_000)(script, 64 * 1024); |
| 60 | } |
| 61 | return code; |
| 62 | } |
| 63 | } |
nothing calls this directly
no outgoing calls
no test coverage detected