File size: 13,093 Bytes
2a26d3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
 # 1. git clone -b v0.5.5-tablegpt-merged https://github.com/zTaoplus/vllm.git
# install tablegpt vllm


##  apply diff file (recommended in case of use only)
# 1. pip install vllm==0.5.5
# 2. cd vllm
# 3. git diff 09c7792610ada9f88bbf87d32b472dd44bf23cc2 HEAD -- vllm | patch -p1 -d "$(pip show vllm | grep Location | awk '{print $2}')"

## build from source (dev recommended)
## Note: Building from source may take 10-30 minutes and requires access to GitHub or other repositories. Make sure to configure an HTTP/HTTPS proxy.
## cd vllm && pip install -e . [-v]. The -v flag is optional and can be used to display verbose logs.


# see https://github.com/zTaoplus/TableGPT-hf to view the model-related configs.

import os
import json
from tqdm import tqdm
from vllm import LLM
from vllm.sampling_params import SamplingParams
from transformers import AutoTokenizer
from text2sql.src.gpt_request import (
    generate_schema_prompt, 
    generate_comment_prompt, 
    cot_wizard, 
    decouple_question_schema,
    generate_sql_file,
    parser_sql
    ) #  text2sql.src.
import warnings
import random
# 忽略所有警告
warnings.filterwarnings("ignore")

# import pandas as pd

# from typing import Literal, List, Optional

# from io import StringIO
import sqlite3
# DEFAULT_SYS_MSG = "You are a helpful assistant."
# ENCODER_TYPE = "contrastive"


def get_table_info(db_path, enum_num=None):
    # extract create ddls
    '''
    :param root_place:
    :param db_name:
    :return:
    '''
    full_schema_prompt_list = []
    conn = sqlite3.connect(db_path)
    # Create a cursor object
    cursor = conn.cursor()
    cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
    tables = cursor.fetchall()


    all_tables_info = []

    # 表截断
    # if len(tables) > 16:
    #     tables = random.sample(tables, 16)

    for table in tables:
        if table == 'sqlite_sequence':
            continue

        # 前几行枚举值
        cur_table = "`{}`".format(table[0])
        cursor.execute("SELECT * FROM {} LIMIT {}".format(cur_table, enum_num))
        row_ls = cursor.fetchall()

        cursor.execute(f"PRAGMA table_info({cur_table});")
        column_name_tp_ls = cursor.fetchall()

        all_columns_info = []
        for column_name_tp in column_name_tp_ls:
            pos_id = column_name_tp[0]  # 字段位置
            col_name = column_name_tp[1]  # 字段名
            col_type = column_name_tp[2]  # 字段类型

            # 字段枚举值
            contains_nan = False
            enum_values = []
            for row in row_ls:
                value = row[pos_id]
                if value is None:
                    contains_nan = True
                
                enum_values.append(str(value))
                
            if len(enum_values) == 0:
                enum_values = ["None"]

            single_columns_info = {
                "name": col_name,
                "dtype": col_type,
                "values": enum_values,
                "contains_nan": contains_nan,
                "is_unique": False
                }
            all_columns_info.append(single_columns_info)
        
        # 列截断
        # if len(all_columns_info) > 32:
        #     all_columns_info = random.sample(all_columns_info, 32)
        single_table_info = {"columns": all_columns_info}
        all_tables_info.append(single_table_info)

    return all_tables_info


def generate_combined_prompts_one_encoder(db_path, question, knowledge=None):
    schema_prompt = generate_schema_prompt(db_path, num_rows=None) # This is the entry to collect values
    comment_prompt = generate_comment_prompt(question, knowledge)
    # encoder_prompt = get_encoder_prompt(table_info)

    combined_prompts = schema_prompt + '\n\n' + comment_prompt + cot_wizard() + '\nSELECT '

    return combined_prompts

def get_encoder_prompt(table_info):
    encode_prompt = "".join(
                        f"table_{i} as follow:\n<TABLE_CONTENT>\n" 
                        for i in range(len(table_info)))
    return encode_prompt


def get_messages_one(db_path, question, knowledge=None):

    table_info = get_table_info(db_path, enum_num=3)  # 采用几行枚举值

    prompt = generate_combined_prompts_one_encoder(db_path, question, knowledge=knowledge)

    messages = [
        {
			"role": "system",
			"content": "You are a helpul assistant."
		}
    ]

    content = []
    for i in range(len(table_info)):
        if i == len(table_info) -1:
            # 最后一个
            content.extend([
                    {
                        "type": "text",
                        "text": f"table_{i} as follow: \n",
                    },
                    {"type": "table", "table": table_info[i]},
                    {
                        "type": "text",
                        "text": prompt,
                    }
                ]
                )
        else:
            content.extend([
                    {
                        "type": "text",
                        "text": f"table_{i} as follow: \n",
                    },
                    {"type": "table", "table": table_info[i]},
                ]
                )

    messages.append(
        {
            "role": "user",
            "content": content
        }
    )
    # print("*"*100)
    # print(json.dumps(messages, ensure_ascii=False))
    # print("*"*100)
    # exit()
    return messages

def calculate_table_num():
    import os
    db_dir = "/home/jyuan/LLM/evaluation/table_related_benchmarks/evalset/bird_data/dev_databases"
    table_nums = []
    cols_nums = []
    for file in os.listdir(db_dir):
        if "." in file:
            continue
        db_path = os.path.join(db_dir, file, f"{file}.sqlite")
        # print(db_path)
        conn = sqlite3.connect(db_path)
        # Create a cursor object
        cursor = conn.cursor()
        cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
        tables = cursor.fetchall()
        table_num = len(tables)
        table_nums.append(table_num)

        for table in tables:
            if table == 'sqlite_sequence':
                continue
            # cursor.execute("SELECT sql FROM sqlite_master WHERE type='table' AND name='{}';".format(table[0]))
            # create_prompt = cursor.fetchone()[0]
            # schemas[table[0]] = create_prompt


            # 前几行枚举值
            cur_table = "`{}`".format(table[0])
            cursor.execute(f"PRAGMA table_info({cur_table});")
            column_name_tp_ls = cursor.fetchall()
            if len(column_name_tp_ls) == 115:
                print(db_path)
            cols_nums.append(len(column_name_tp_ls))
    cols_nums  = sorted(cols_nums, reverse=True)
    print(f"max table: {max(table_nums)}, max columns: {max(cols_nums)}")
    print(cols_nums[:10])


def llm_generate_result_encoder(model_name_or_path, gpus_num, messages_ls):
    # 批量推理
    print("model", model_name_or_path)
    tokenizer = AutoTokenizer.from_pretrained(model_name_or_path)
    print(
        "load tokenizer {} from {} over.".format(
            tokenizer.__class__, model_name_or_path
        )
    )
    model = LLM(
        model=model_name_or_path,
        max_model_len=12000,
        max_num_seqs=1,
        dtype="bfloat16",
        limit_mm_per_prompt={"table": 16},
        gpu_memory_utilization=0.9,
        tensor_parallel_size=gpus_num,

    )

    p = SamplingParams(temperature=0, max_tokens=1024)
    outputs = model.chat(messages=messages_ls, sampling_params=p)
    
    generated_res = []
    for i, output in enumerate(tqdm(outputs)):
        text = output.outputs[0].text
        sql = parser_sql(text)
        generated_res.append(sql)

    return generated_res


def col_nums_max(message):
    content = message[1]["content"]
    table_nums = 0
    col_nums_ls = []
    for dic in content:
        if dic["type"] == "table":
            table_nums += 1
            col_num = len(dic["table"]["columns"])
            col_nums_ls.append(col_num)
    return int(max(col_nums_ls) + 1), table_nums

def llm_generate_result_encoder_one(model_name_or_path, gpus_num, messages_ls):
    # 单条推理
    print("model", model_name_or_path)
    tokenizer = AutoTokenizer.from_pretrained(model_name_or_path)
    print(
        "load tokenizer {} from {} over.".format(
            tokenizer.__class__, model_name_or_path
        )
    )
    model = LLM(
        model=model_name_or_path,
        max_model_len=12000,
        max_num_seqs=1,
        dtype="bfloat16",
        limit_mm_per_prompt={"table": 20},
        gpu_memory_utilization=0.9

    )

    p = SamplingParams(temperature=0, max_tokens=1024)
    error_ls = []
    generated_res = []
    for i, messages in enumerate(messages_ls):
        # if i != 89:
        #     continue
        try:
            # max_col, table_nums = col_nums_max(messages)
            # # print("="*100, max_col, table_nums)
            # model.llm_engine.model_config.hf_config.encoder_config.max_cols = max_col
            # model.llm_engine.model_config.multimodal_config.limit_per_prompt = {"table": table_nums}

            outputs = model.chat(messages=messages, sampling_params=p)

            text = outputs[0].outputs[0].text
            sql = parser_sql(text)

        except Exception as e:

            error_ls.append(i)
            sql = ""
        generated_res.append(sql)
    if len(error_ls) != 0:
        json.dump({"error_id": error_ls}, open("table_related_benchmarks/text2sql/output/error_ls.json", 'w'), indent=4)
    return generated_res


def collect_response_from_gpt_encoder(model_path, gpus_num, db_path_list, question_list, knowledge_list=None):
    '''
    :param db_path: str
    :param question_list: []
    :return: dict of responses collected from llm
    '''
    responses_dict = {}
    response_list = []

    messages_ls = []
    for i in tqdm(range(len(question_list)), desc="get prompt"):
        # print('--------------------- processing {}th question ---------------------'.format(i))
        # print('the question is: {}'.format(question))
        question = question_list[i]
        db_path = db_path_list[i]
        
        if knowledge_list:
            messages = get_messages_one(db_path, question, knowledge=knowledge_list[i])
        else:
            messages = get_messages_one(db_path, question)
        messages_ls.append(messages)
        
    outputs_sql = llm_generate_result_encoder(model_path, gpus_num, messages_ls)
    for i in tqdm(range(len(outputs_sql)), desc="postprocess result"):
        question = question_list[i]
        sql = outputs_sql[i]
        
        db_id = db_path_list[i].split('/')[-1].split('.sqlite')[0]
        sql = sql + '\t----- bird -----\t' + db_id # to avoid unpredicted \t appearing in codex results
        response_list.append(sql)

    return response_list

def generate_main_encoder(eval_data, args):

    question_list, db_path_list, knowledge_list = decouple_question_schema(
        datasets=eval_data, db_root_path=args.db_root_path)
    assert len(question_list) == len(db_path_list) == len(knowledge_list)

    
    if args.use_knowledge == 'True':
        responses = collect_response_from_gpt_encoder(model_path=args.model_path, gpus_num=args.gpus_num, db_path_list=db_path_list, question_list=question_list, knowledge_list=knowledge_list)
    else:
        responses = collect_response_from_gpt_encoder(model_path=args.model_path, gpus_num=args.gpus_num, db_path_list=db_path_list, question_list=question_list, knowledge_list=None)
    
    if args.chain_of_thought == 'True':
        output_name = os.path.join(args.data_output_path, f'predict_{args.mode}_cot.json')
    else:
        output_name =  os.path.join(args.data_output_path, f'predict_{args.mode}.json')
    # pdb.set_trace()
    generate_sql_file(sql_lst=responses, output_path=output_name)

    print('successfully collect results from {} for {} evaluation; Use knowledge: {}; Use COT: {}; Use encoder: {}'.format(args.model_path, args.mode, args.use_knowledge, args.chain_of_thought, args.use_encoder))
    print(f'output: {output_name}')
    # 返回推理数据保存路径
    return output_name

def test_single():
    db_path = "/home/jyuan/LLM/evaluation/table_related_benchmarks/evalset/spider_data/test_database/aan_1/aan_1.sqlite"
    question = "How many authors do we have?"

    messages = get_messages_one(db_path, question, knowledge=None)
    # print("*"*100)
    # print(messages)
    # print("*"*100)

    model_name_or_path = "/data4/workspace/yss/models/longlin_encoder_model/contrastive"

    model = LLM(
        model=model_name_or_path,
        max_model_len=8192,
        max_num_seqs=16,
        dtype="bfloat16",
        limit_mm_per_prompt={"table": 10},
        gpu_memory_utilization=0.9,
        tensor_parallel_size=1,
    )
    p = SamplingParams(temperature=0, max_tokens=1024)
    res = model.chat(messages=messages, sampling_params=p)
    print(res[0].outputs[0].text)

if __name__ == "__main__":
    # test_single()
    calculate_table_num()