File size: 5,798 Bytes
df0d0be d75499c dc3a0d9 de94a5d 10da04c de94a5d d75499c 1910a90 d75499c d3e9458 d75499c 1910a90 d75499c 1910a90 d75499c 1910a90 d75499c 1910a90 d75499c 1910a90 d75499c 1910a90 d75499c 1910a90 d75499c 1910a90 d75499c 59d9224 d3e9458 d75499c 8af400d d75499c | 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | import os
import openai
from src.get_completion_from_messages import get_completion_from_messages
from src.plan.content_moderation import *
from src.plan.get_table_info import *
from constants.constants import constants
# 解包constant中的常量
locals().update(constants)
# 构建database_df题目文件的路径
database_df_path = os.path.join("data", "database_df.xlsx")
#读取database_df
database_df = pd.read_excel(database_df_path)
#定义database_columns&database_datalist
database_columns = list(database_df.columns)
database_datalist = database_df.values.tolist()
# 定义生成候选补全的函数
def get_candidates(
messages,
model = 'gpt-3.5-turbo-16k',
temperature = 0,
max_tokens = 3000,
n = 1,
stop = [';'],
):
prefix = ''
# 使用 OpenAI 完成接口生成响应
response = openai.ChatCompletion.create(
model=model,
messages = messages,
temperature=temperature,
n=n,
stop=stop,
max_tokens=max_tokens,
)
# 将生成的响应与指定的前缀组合
responses = [prefix + choice.message.content for choice in response.choices]
return responses
# 定义一个处理用户消息的函数
def process_user_message(user_input,
all_messages,
model = 'gpt-3.5-turbo-16k',
temperature = 0,
max_tokens = 3000,
hive_select='hive',
is_current_table = True,
data_scope_definition = data_scope_definition,
database_datalist = database_datalist
):
delimiter = "####" # 定义一个分隔符
# 步骤1:检查输入是否涉及有害语句或不属于查询问题
'''check_error = (content_moderation(user_input) == 1
or is_query_question(user_input) == 'N')'''
# 如果输入被标记了
'''if check_error:
all_messages+= [(user_input,"对不起,您的问题不是一个查询问题,请重新输入")]
return "", all_messages # 返回错误消息'''
# 步骤2:从字符串中提取产品列表
database_df = pd.DataFrame(database_datalist,columns=database_columns)
database_and_table_str = query_table_info(
user_message = user_input,
model = model,
temperature = temperature,
max_tokens = max_tokens,
database_datalist = database_datalist
)
database_and_table_list = read_string_to_list(database_and_table_str)
# 步骤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}"
history_prompt = []
for turn in all_messages:
user_message, bot_message = turn
history_prompt += [
{'role': 'user', 'content':user_message},
{'role': 'assistant', 'content': bot_message}
]
# 创建消息列表,包括系统消息、用户消息和助手消息
messages = [
{'role': 'system', 'content': system_message}] \
+ history_prompt + \
[{'role': 'assistant', 'content': database_and_table_info},
{'role': 'user', 'content': nl_query+prefix}
]
# 根据消息生成完成的回复
final_response = get_candidates(
messages = messages,
model = model,
temperature = temperature,
max_tokens = max_tokens
)
all_messages+= [(user_input,final_response[0])]
return "", all_messages # 返回最终回复和所有消息
|