## 3 Model Design and Implementation
### Pre-Processing
```
Input: query and schema Output: processed_query and processed_schema
1 function preprocess (query, schema):
2 cleaned_query = clean_and_standardize(query)
3 tokenized_query = tokenize_and_pos_tag(cleaned_query)
4 entities = named_entity_recognition(tokenized_query)
5 syntactic_structure = syntax_and_dependency_analysis(tokenized_query)
6 mapped_schema = map_to_schema(entities, schema)
7 constraints = semantic_analysis(entities, mapped_schema)
8 return tokenized_query, mapped_schema, constraints
```  
**Algorithm 1**Algorithms for Data Preprocessing  
In Algorithm 1, starting from the input natural language query and database schema information, the information is finally converted into a format suitable for the model after a series of processing such as cleaning, word segmentation, entity recognition, syntactic analysis, schema mapping, and semantic analysis. ```
Input: processed_query, processed_schema and constraints Output: prompt
1 function generate_prompt(processed_query, processed_schema, constraints):
2 prompt_template = \"Translate the following question into SQL: {query}\"
3 prompt_with_schema = \"Given the following table schema: {schema}, \" + prompt_template
4 prompt_with_schema = prompt_with_schema.format (query = processed_query, schema = processed_schema)
5 if constraints: \begin{tabular}{l l}  & prompt_with_constraints = prompt_with_schema + \" Apply the \\  & following constraints: {constraints}\" \\  & prompt_with_constraints = prompt_with_constraints.format (constraints = constraints) \\
6 & else: \\
7 & prompt_with_constraints = prompt_with_schema \\
8 & return prompt_with_constraints \\ \end{tabular} 

In Algorithm 2, we combine the preprocessed query and database schema information to construct hints with table structure and query intent to help the model understand the query and generate correct SQL query statements. ### NL2 PY and Iteration

Based on the generated JSON file, we designed a prompt for DeepSeek to generate Python code. When generating code, ensure that Python's Pandas module is imported, as Pandas will be used for data manipulation. Using the problem description, prompt information, and extracted field values from the table, create a prompt that guides the model to generate the Pandas query code for answering the question. The LLM should follow a chain-of-thought prompting method, generating Python code that adheres to a specific style, including both reasoning steps and the final Python code. The generated Python code is then executed to verify and optimize its functionality. During execution, the code undergoes checks by the large model to identify potential syntax errors, confirm that a result is generated, and ensure the result satisfies the question requirements. If any errors occur or the results do not meet expectations, the error message and related context are sent back to the LLM, requesting a corrected version of the Python code. This iterative process can run up to a maximum of N cycles, defaulting to 5 in this case. If the question is successfully answered, the process exits early. In this process, we decompose complex natural language queries into multiple subtasks and generate intermediate code for each subtask. Then, we gradually deduce the logic of each subtask and verify the validity of the code. Finally, we integrate and optimize the various intermediate code fragments into complete Python. This step-by-step reasoning and verification method effectively improves code generation's accuracy, readability, and maintainability. The chain of thought (CoT) reasoning process is shown in Algorithm 3.