Commit ·
1910a90
1
Parent(s): 9e90ae8
Update src/plan/text_to_sql.py
Browse files6 unmodified lines7891011125 unmodified lines1819202122232423 unmodified lines484950515253545556575859609 unmodified lines70717273747576777879808182838485868788896 unmodified lines# 解包constant中的常量locals().update(constants)# 定义生成候选补全的函数def get_candidates(5 unmodified linesstop = [';'],):prefix = 'SELECT '# 使用 OpenAI 完成接口生成响应response = openai.ChatCompletion.create(23 unmodified linesdelimiter = "####" # 定义一个分隔符# 步骤1:检查输入是否涉及有害语句或不属于查询问题check_error = (content_moderation(user_input) == 1or is_query_question(user_input) == 'N')# 如果输入被标记了if check_error:all_messages+= [(user_input,"对不起,您的问题不是一个查询问题,请重新输入")]return "", all_messages # 返回错误消息# 步骤2:从字符串中提取产品列表9 unmodified lines# 步骤3:如果找到了库表信息,查找表字段信息database_and_table_info = generate_table_info(database_and_table_list,database_datalist)if database_and_table_info == "" and is_current_table:all_messages+= [(user_input,"对不起,未查到相关表信息")]return "", all_messages# 步骤4:回答用户的问题# 定义系统消息system_message = f"""给定以下SQL表格信息,你的工作是根据用户的请求编写{hive_select} SQL查询,相关的口径定义为:{data_scope_definition}注意你编写的SQL代码需要符合{hive_select} SQL函数的语法要求"""prefix = 'SELECT 'nl_query = f"{delimiter}{user_input}{delimiter}"6 unmodified lines7891011121314151617181920215 unmodified lines2728293031323323 unmodified lines575859606162636465666768699 unmodified lines7980818283848586878889909192939495969798991001011021031041051061076 unmodified lines# 解包constant中的常量locals().update(constants)# 构建database_df题目文件的路径database_df_path = os.path.join("data", "database_df.xlsx")#读取database_dfdatabase_df = pd.read_excel(database_df_path)#定义database_columns&database_datalistdatabase_columns = list(database_df.columns)database_datalist = database_df.values.tolist()# 定义生成候选补全的函数def get_candidates(5 unmodified linesstop = [';'],):prefix = ''# 使用 OpenAI 完成接口生成响应response = openai.ChatCompletion.create(23 unmodified linesdelimiter = "####" # 定义一个分隔符# 步骤1:检查输入是否涉及有害语句或不属于查询问题'''check_error = (content_moderation(user_input) == 1or is_query_question(user_input) == 'N')'''# 如果输入被标记了'''if check_error:all_messages+= [(user_input,"对不起,您的问题不是一个查询问题,请重新输入")]return "", all_messages # 返回错误消息'''# 步骤2:从字符串中提取产品列表9 unmodified lines# 步骤3:如果找到了库表信息,查找表字段信息database_and_table_info = generate_table_info(database_and_table_list,database_datalist)'''if database_and_table_info == "" and is_current_table:all_messages+= [(user_input,"对不起,未查到相关表信息")]return "", all_messages'''# 步骤4:回答用户的问题# 定义系统消息system_message = f"""You are a helpful assistant capable of aiding users in converting natural language into SQL statements. Here's how you can assist users in generating {hive_select} SQL statements and provide help:1. Based on the provided SQL table information, list the fields related to the content the user wishes to query.2. Ask the user if these are the fields they need, encouraging them to provide additional information such as field definitions and caliber definitions.3. Once the user confirms the information they wish to query, provide the corresponding {hive_select} SQL statement, ensuring it adheres to {hive_select} SQL syntax standards.4. If the user wishes to understand SQL syntax, inquire about their level of understanding of {hive_select} SQL statements: beginner, novice, intermediate, or advanced.-- If the user is a beginner, shift the conversation towards a basic explanation of SQL syntax.-- If the user is a novice, guide them to ask more {hive_select} SQL-related questions and provide clear and patient answers.-- If the user is at an intermediate or advanced level, engage in a Socratic dialogue to help them clarify their difficulties in understanding {hive_select} SQL.Always remember, you are an assistant for generating SQL statements, and there's no need to answer other unrelated questions.related caliber definitions:{data_scope_definition}Be concise and constructive with feedback.注意,对于用户的输入,始终使用中文回复"""prefix = ''nl_query = f"{delimiter}{user_input}{delimiter}"
src/plan/text_to_sql.py+30-12