Skip to content

Finetune llm

FinetuneLLM

Fine tune the LLM with the train QA pairs

We can not provide a fair game between this and RAG, and text2sql. They all can be information retrieval task, which will be more explainable.

And if the LLM do not have the knowledge, it will not make sense even you fine tune it. So what we propose should be, we ingest the information to the LLM, and ask it from other perspective.

Comparison of experiments:

  • Fine tuned QA with paraphrased questions
  • Fine tuned QA, answer as question
  • Fine tuned with simple QA, and ask the relevant medium question
Source code in TimelineKGQA/rag/finetune_llm.py
 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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
class FinetuneLLM:
    """
    Fine tune the LLM with the train QA pairs

    We can not provide a fair game between this and RAG, and text2sql.
    They all can be information retrieval task, which will be more explainable.

    And if the LLM do not have the knowledge, it will not make sense even you fine tune it.
    So what we propose should be, we ingest the information to the LLM, and ask it from other perspective.

    Comparison of experiments:

    - Fine tuned QA with paraphrased questions
    - Fine tuned QA, answer as question
    - Fine tuned with simple QA, and ask the relevant medium question
    """

    def __init__(
        self,
        table_name: str,
        host: str,
        port: int,
        user: str,
        password: str,
        db_name: str,
        fine_tune_model: str = "gpt-3.5-turbo-1106",
        paraphrased_model: str = "gpt-3.5-turbo-1106",
    ):
        """
        Args:
            table_name (str): The table name
            host (str): The host
            port (int): The port
            user (str): The user
            password (str): The password
            db_name (str): The db name
            fine_tune_model (str): The fine tune model
            paraphrased_model (str): The paraphrased model


        """
        self.table_name = table_name
        self.host = host
        self.port = port
        self.user = user
        self.password = password
        self.db_name = db_name

        self.engine = create_engine(
            f"postgresql://{self.user}:{self.password}@{self.host}:{self.port}/{self.db_name}"
        )

        self.fine_tune_model = fine_tune_model
        self.paraphrased_model = paraphrased_model

    def generate_finetune_data_paraphrased_questions(
        self, number_of_questions: int = 10, identifier_file_name: str = "paraphrased"
    ):
        """
        Args:
            number_of_questions (int): The number of questions
            identifier_file_name (str): The identifier file name

        """
        paraphrased_finetune_query = f"""
        SELECT * FROM unified_kg_icews_actor_questions
        WHERE question_type = 'timeline_recovery'
              or question_type = 'temporal_constrainted_retrieval'
        ORDER BY events
        LIMIT {number_of_questions} * 3;
        """

        questions_df = pd.read_sql(paraphrased_finetune_query, self.engine)
        fine_tune_data = []
        evaluation_data = []

        for index, row in tqdm(
            questions_df.iterrows(),
            total=questions_df.shape[0],
            desc="Generating finetune data",
        ):
            question = row["question"]
            answer = row["answer"]

            fine_tune_data.append(
                {
                    "messages": [
                        {
                            "role": "system",
                            "content": "You are a QA robot expert in temporal related questions.",
                        },
                        {"role": "user", "content": question},
                        {"role": "assistant", "content": answer},
                    ]
                }
            )

            # paraphrased question
            paraphrased_question = self.paraphrased_question(question)
            if paraphrased_question:
                evaluation_data.append(
                    {
                        "question": question,
                        "paraphrased_question": paraphrased_question,
                        "answer": answer,
                    }
                )

        # dump it into a jsonl file
        with open(
            FINE_TUNE_LOGS_DIR / f"finetune_data_{identifier_file_name}.jsonl", "w"
        ) as f:
            for line in fine_tune_data:
                f.write(json.dumps(line) + "\n")

        with open(
            FINE_TUNE_LOGS_DIR / f"evaluation_data_{identifier_file_name}.jsonl", "w"
        ) as f:
            for line in evaluation_data:
                f.write(json.dumps(line) + "\n")

        self.create_fine_tune_task(
            train_filename=(
                FINE_TUNE_LOGS_DIR / f"finetune_data_{identifier_file_name}.jsonl"
            ).as_posix(),
            eval_filename=(
                FINE_TUNE_LOGS_DIR / f"evaluation_data_{identifier_file_name}.jsonl"
            ).as_posix(),
            identifier_file_name=identifier_file_name,
        )

    def generate_finetune_data_answer_as_question(
        self, number_of_questions: int = 10, identifier_file_name: str = "a_as_q"
    ):
        """
        Generate the finetune data

        Args:
            number_of_questions (int): The number of questions
            identifier_file_name (str): The identifier file name

        """
        simple_finetune_query = f"""
        SELECT * FROM unified_kg_icews_actor_questions
        WHERE question_type = 'timeline_recovery'
           or question_type = 'temporal_constrainted_retrieval'
        ORDER BY events
        LIMIT {number_of_questions} * 3;
        """

        questions_df = pd.read_sql(simple_finetune_query, self.engine)
        fine_tune_data = []
        evaluation_data = []

        for index, row in tqdm(
            questions_df.iterrows(),
            total=questions_df.shape[0],
            desc="Generating finetune data",
        ):
            question = row["question"]
            answer = row["answer"]
            if row["question_type"] == "timeline_recovery":
                evaluation_data.append(
                    {
                        "question": question,
                        "answer": answer,
                    }
                )
                continue

            fine_tune_data.append(
                {
                    "messages": [
                        {
                            "role": "system",
                            "content": "You are a QA robot expert in temporal related questions.",
                        },
                        {"role": "user", "content": question},
                        {"role": "assistant", "content": answer},
                    ]
                }
            )
        # dump it into a jsonl file
        with open(
            FINE_TUNE_LOGS_DIR / f"finetune_data_{identifier_file_name}.jsonl", "w"
        ) as f:
            for line in fine_tune_data:
                f.write(json.dumps(line) + "\n")

        with open(
            FINE_TUNE_LOGS_DIR / f"evaluation_data_{identifier_file_name}.jsonl", "w"
        ) as f:
            for line in evaluation_data:
                f.write(json.dumps(line) + "\n")

        self.create_fine_tune_task(
            train_filename=(
                FINE_TUNE_LOGS_DIR / f"finetune_data_{identifier_file_name}.jsonl"
            ).as_posix(),
            eval_filename=(
                FINE_TUNE_LOGS_DIR / f"evaluation_data_{identifier_file_name}.jsonl"
            ).as_posix(),
            identifier_file_name=identifier_file_name,
        )

    def generate_finetune_data_simple_vs_medium(
        self,
        number_of_questions: int = 30,
        identifier_file_name: str = "simple_vs_medium",
    ):
        """
        Args:
            number_of_questions (int): The number of questions
            identifier_file_name (str): The identifier file name

        """
        simple_vs_medium_query = f"""
        SELECT * FROM unified_kg_icews_actor_questions
        WHERE question_level = 'medium'
        ORDER BY events
        LIMIT {number_of_questions} * 3;
        """
        questions_df = pd.read_sql(
            simple_vs_medium_query,
            self.engine,
        )
        fine_tune_data = []
        evaluation_data = []
        for index, row in tqdm(
            questions_df.iterrows(),
            total=questions_df.shape[0],
            desc="Generating finetune data",
        ):
            question = row["question"]
            answer = row["answer"]

            evaluation_data.append(
                {
                    "question": question,
                    "answer": answer,
                }
            )

            events = row["events"]
            for event in events:
                logger.info(event)
                # query the db for simple question with this event
                simple_query = (
                    """
                    SELECT * FROM unified_kg_icews_actor_questions
                    WHERE events = '{"""
                    + event.replace("'", "''")
                    + """}'
                AND question_level = 'simple'
                LIMIT 3;
                """
                )
                logger.info(simple_query)
                simple_question_df = pd.read_sql(
                    simple_query,
                    self.engine,
                )
                logger.info(simple_question_df)
                if simple_question_df.empty:
                    continue

                for _, simple_row in simple_question_df.iterrows():
                    simple_question = simple_row["question"]
                    simple_answer = simple_row["answer"]
                    fine_tune_data.append(
                        {
                            "messages": [
                                {
                                    "role": "system",
                                    "content": "You are a QA robot expert in temporal related questions.",
                                },
                                {"role": "user", "content": simple_question},
                                {"role": "assistant", "content": simple_answer},
                            ]
                        }
                    )

        # dump it into a jsonl file
        with open(
            FINE_TUNE_LOGS_DIR / f"finetune_data_{identifier_file_name}.jsonl", "w"
        ) as f:
            for line in fine_tune_data:
                f.write(json.dumps(line) + "\n")

        with open(
            FINE_TUNE_LOGS_DIR / f"evaluation_data_{identifier_file_name}.jsonl", "w"
        ) as f:
            for line in evaluation_data:
                f.write(json.dumps(line) + "\n")

        self.create_fine_tune_task(
            train_filename=(
                FINE_TUNE_LOGS_DIR / f"finetune_data_{identifier_file_name}.jsonl"
            ).as_posix(),
            eval_filename=(
                FINE_TUNE_LOGS_DIR / f"evaluation_data_{identifier_file_name}.jsonl"
            ).as_posix(),
            identifier_file_name=identifier_file_name,
        )

    def create_fine_tune_task(
        self, train_filename: str, eval_filename: str, identifier_file_name: str
    ):
        """
        Fine tune the LLM

        Args:
            train_filename (str): The filename
            eval_filename (str): The evaluation filename
            identifier_file_name (str): The identifier, output csv file identifier
        """
        # Upload the training file
        response = client.files.create(
            file=open(train_filename, "rb"), purpose="fine-tune"
        )
        logger.info(response.id)
        # logger.info(f"Uploaded training file: {response['id']}")
        fine_tune_res = client.fine_tuning.jobs.create(
            training_file=response.id, model=self.fine_tune_model
        )
        logger.info(fine_tune_res.id)

        status = "pending"
        while status not in ["succeeded", "failed"]:
            response = client.fine_tuning.jobs.retrieve(
                fine_tuning_job_id=fine_tune_res.id
            )
            status = response.status
            logger.info(f"Fine-tune job status: {status}")
            time.sleep(30)  # Wait for 30 seconds before checking the status again

        # when success, then call the model to do the QA, for further evaluation
        if status == "succeeded":
            logger.info("Fine-tuning succeeded.")
            fine_tuned_model = response.fine_tuned_model
            logger.info(f"Fine-tuned model: {fine_tuned_model}")
        else:
            logger.info("Fine-tuning failed.")
            return

        self.evaluation_fine_tune_task(
            eval_filename,
            fine_tuned_model,
            output_filename=f"evaluation_results_{identifier_file_name}.csv",
        )

    @staticmethod
    def evaluation_fine_tune_task(
        eval_filename: str, fine_tuned_model: str, output_filename: str
    ):
        # Evaluation
        evl_df = pd.read_json(eval_filename, lines=True)
        for index, row in evl_df.iterrows():
            question = row["question"]

            response = client.chat.completions.create(
                model=fine_tuned_model,
                messages=[
                    {"role": "user", "content": question},
                ],
                temperature=0.0,
            )
            logger.info(question)
            logger.info(response.choices[0].message.content)
            fine_tune_ans = response.choices[0].message.content
            evl_df.loc[index, "fine_tune_ans"] = fine_tune_ans
        evl_df.to_csv(FINE_TUNE_LOGS_DIR / output_filename, index=False)

    def paraphrased_question(self, question: str):
        """
        Args:
            question (str): The question

        Returns:
            str: The paraphrased question

        """

        try:
            prompt = f"""
            Please paraphrase the following question with the same meaning but another way to ask:
            {question}
            Return it in json format with the key "paraphrased_question"
            """
            response = client.chat.completions.create(
                model=self.paraphrased_model,
                messages=[
                    {"role": "user", "content": prompt},
                ],
                temperature=0.0,
                response_format={"type": "json_object"},
            )
            logger.info(response.choices[0].message.content)
            paraphrased_question_str = response.choices[0].message.content
            paraphrased_question = json.loads(paraphrased_question_str).get(
                "paraphrased_question", ""
            )
            return paraphrased_question
        except Exception as e:
            logger.exception(e)
            return ""

__init__(table_name, host, port, user, password, db_name, fine_tune_model='gpt-3.5-turbo-1106', paraphrased_model='gpt-3.5-turbo-1106')

Parameters:

Name Type Description Default
table_name str

The table name

required
host str

The host

required
port int

The port

required
user str

The user

required
password str

The password

required
db_name str

The db name

required
fine_tune_model str

The fine tune model

'gpt-3.5-turbo-1106'
paraphrased_model str

The paraphrased model

'gpt-3.5-turbo-1106'
Source code in TimelineKGQA/rag/finetune_llm.py
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
def __init__(
    self,
    table_name: str,
    host: str,
    port: int,
    user: str,
    password: str,
    db_name: str,
    fine_tune_model: str = "gpt-3.5-turbo-1106",
    paraphrased_model: str = "gpt-3.5-turbo-1106",
):
    """
    Args:
        table_name (str): The table name
        host (str): The host
        port (int): The port
        user (str): The user
        password (str): The password
        db_name (str): The db name
        fine_tune_model (str): The fine tune model
        paraphrased_model (str): The paraphrased model


    """
    self.table_name = table_name
    self.host = host
    self.port = port
    self.user = user
    self.password = password
    self.db_name = db_name

    self.engine = create_engine(
        f"postgresql://{self.user}:{self.password}@{self.host}:{self.port}/{self.db_name}"
    )

    self.fine_tune_model = fine_tune_model
    self.paraphrased_model = paraphrased_model

create_fine_tune_task(train_filename, eval_filename, identifier_file_name)

Fine tune the LLM

Parameters:

Name Type Description Default
train_filename str

The filename

required
eval_filename str

The evaluation filename

required
identifier_file_name str

The identifier, output csv file identifier

required
Source code in TimelineKGQA/rag/finetune_llm.py
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
def create_fine_tune_task(
    self, train_filename: str, eval_filename: str, identifier_file_name: str
):
    """
    Fine tune the LLM

    Args:
        train_filename (str): The filename
        eval_filename (str): The evaluation filename
        identifier_file_name (str): The identifier, output csv file identifier
    """
    # Upload the training file
    response = client.files.create(
        file=open(train_filename, "rb"), purpose="fine-tune"
    )
    logger.info(response.id)
    # logger.info(f"Uploaded training file: {response['id']}")
    fine_tune_res = client.fine_tuning.jobs.create(
        training_file=response.id, model=self.fine_tune_model
    )
    logger.info(fine_tune_res.id)

    status = "pending"
    while status not in ["succeeded", "failed"]:
        response = client.fine_tuning.jobs.retrieve(
            fine_tuning_job_id=fine_tune_res.id
        )
        status = response.status
        logger.info(f"Fine-tune job status: {status}")
        time.sleep(30)  # Wait for 30 seconds before checking the status again

    # when success, then call the model to do the QA, for further evaluation
    if status == "succeeded":
        logger.info("Fine-tuning succeeded.")
        fine_tuned_model = response.fine_tuned_model
        logger.info(f"Fine-tuned model: {fine_tuned_model}")
    else:
        logger.info("Fine-tuning failed.")
        return

    self.evaluation_fine_tune_task(
        eval_filename,
        fine_tuned_model,
        output_filename=f"evaluation_results_{identifier_file_name}.csv",
    )

generate_finetune_data_answer_as_question(number_of_questions=10, identifier_file_name='a_as_q')

Generate the finetune data

Parameters:

Name Type Description Default
number_of_questions int

The number of questions

10
identifier_file_name str

The identifier file name

'a_as_q'
Source code in TimelineKGQA/rag/finetune_llm.py
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
def generate_finetune_data_answer_as_question(
    self, number_of_questions: int = 10, identifier_file_name: str = "a_as_q"
):
    """
    Generate the finetune data

    Args:
        number_of_questions (int): The number of questions
        identifier_file_name (str): The identifier file name

    """
    simple_finetune_query = f"""
    SELECT * FROM unified_kg_icews_actor_questions
    WHERE question_type = 'timeline_recovery'
       or question_type = 'temporal_constrainted_retrieval'
    ORDER BY events
    LIMIT {number_of_questions} * 3;
    """

    questions_df = pd.read_sql(simple_finetune_query, self.engine)
    fine_tune_data = []
    evaluation_data = []

    for index, row in tqdm(
        questions_df.iterrows(),
        total=questions_df.shape[0],
        desc="Generating finetune data",
    ):
        question = row["question"]
        answer = row["answer"]
        if row["question_type"] == "timeline_recovery":
            evaluation_data.append(
                {
                    "question": question,
                    "answer": answer,
                }
            )
            continue

        fine_tune_data.append(
            {
                "messages": [
                    {
                        "role": "system",
                        "content": "You are a QA robot expert in temporal related questions.",
                    },
                    {"role": "user", "content": question},
                    {"role": "assistant", "content": answer},
                ]
            }
        )
    # dump it into a jsonl file
    with open(
        FINE_TUNE_LOGS_DIR / f"finetune_data_{identifier_file_name}.jsonl", "w"
    ) as f:
        for line in fine_tune_data:
            f.write(json.dumps(line) + "\n")

    with open(
        FINE_TUNE_LOGS_DIR / f"evaluation_data_{identifier_file_name}.jsonl", "w"
    ) as f:
        for line in evaluation_data:
            f.write(json.dumps(line) + "\n")

    self.create_fine_tune_task(
        train_filename=(
            FINE_TUNE_LOGS_DIR / f"finetune_data_{identifier_file_name}.jsonl"
        ).as_posix(),
        eval_filename=(
            FINE_TUNE_LOGS_DIR / f"evaluation_data_{identifier_file_name}.jsonl"
        ).as_posix(),
        identifier_file_name=identifier_file_name,
    )

generate_finetune_data_paraphrased_questions(number_of_questions=10, identifier_file_name='paraphrased')

Parameters:

Name Type Description Default
number_of_questions int

The number of questions

10
identifier_file_name str

The identifier file name

'paraphrased'
Source code in TimelineKGQA/rag/finetune_llm.py
 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
def generate_finetune_data_paraphrased_questions(
    self, number_of_questions: int = 10, identifier_file_name: str = "paraphrased"
):
    """
    Args:
        number_of_questions (int): The number of questions
        identifier_file_name (str): The identifier file name

    """
    paraphrased_finetune_query = f"""
    SELECT * FROM unified_kg_icews_actor_questions
    WHERE question_type = 'timeline_recovery'
          or question_type = 'temporal_constrainted_retrieval'
    ORDER BY events
    LIMIT {number_of_questions} * 3;
    """

    questions_df = pd.read_sql(paraphrased_finetune_query, self.engine)
    fine_tune_data = []
    evaluation_data = []

    for index, row in tqdm(
        questions_df.iterrows(),
        total=questions_df.shape[0],
        desc="Generating finetune data",
    ):
        question = row["question"]
        answer = row["answer"]

        fine_tune_data.append(
            {
                "messages": [
                    {
                        "role": "system",
                        "content": "You are a QA robot expert in temporal related questions.",
                    },
                    {"role": "user", "content": question},
                    {"role": "assistant", "content": answer},
                ]
            }
        )

        # paraphrased question
        paraphrased_question = self.paraphrased_question(question)
        if paraphrased_question:
            evaluation_data.append(
                {
                    "question": question,
                    "paraphrased_question": paraphrased_question,
                    "answer": answer,
                }
            )

    # dump it into a jsonl file
    with open(
        FINE_TUNE_LOGS_DIR / f"finetune_data_{identifier_file_name}.jsonl", "w"
    ) as f:
        for line in fine_tune_data:
            f.write(json.dumps(line) + "\n")

    with open(
        FINE_TUNE_LOGS_DIR / f"evaluation_data_{identifier_file_name}.jsonl", "w"
    ) as f:
        for line in evaluation_data:
            f.write(json.dumps(line) + "\n")

    self.create_fine_tune_task(
        train_filename=(
            FINE_TUNE_LOGS_DIR / f"finetune_data_{identifier_file_name}.jsonl"
        ).as_posix(),
        eval_filename=(
            FINE_TUNE_LOGS_DIR / f"evaluation_data_{identifier_file_name}.jsonl"
        ).as_posix(),
        identifier_file_name=identifier_file_name,
    )

generate_finetune_data_simple_vs_medium(number_of_questions=30, identifier_file_name='simple_vs_medium')

Parameters:

Name Type Description Default
number_of_questions int

The number of questions

30
identifier_file_name str

The identifier file name

'simple_vs_medium'
Source code in TimelineKGQA/rag/finetune_llm.py
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
def generate_finetune_data_simple_vs_medium(
    self,
    number_of_questions: int = 30,
    identifier_file_name: str = "simple_vs_medium",
):
    """
    Args:
        number_of_questions (int): The number of questions
        identifier_file_name (str): The identifier file name

    """
    simple_vs_medium_query = f"""
    SELECT * FROM unified_kg_icews_actor_questions
    WHERE question_level = 'medium'
    ORDER BY events
    LIMIT {number_of_questions} * 3;
    """
    questions_df = pd.read_sql(
        simple_vs_medium_query,
        self.engine,
    )
    fine_tune_data = []
    evaluation_data = []
    for index, row in tqdm(
        questions_df.iterrows(),
        total=questions_df.shape[0],
        desc="Generating finetune data",
    ):
        question = row["question"]
        answer = row["answer"]

        evaluation_data.append(
            {
                "question": question,
                "answer": answer,
            }
        )

        events = row["events"]
        for event in events:
            logger.info(event)
            # query the db for simple question with this event
            simple_query = (
                """
                SELECT * FROM unified_kg_icews_actor_questions
                WHERE events = '{"""
                + event.replace("'", "''")
                + """}'
            AND question_level = 'simple'
            LIMIT 3;
            """
            )
            logger.info(simple_query)
            simple_question_df = pd.read_sql(
                simple_query,
                self.engine,
            )
            logger.info(simple_question_df)
            if simple_question_df.empty:
                continue

            for _, simple_row in simple_question_df.iterrows():
                simple_question = simple_row["question"]
                simple_answer = simple_row["answer"]
                fine_tune_data.append(
                    {
                        "messages": [
                            {
                                "role": "system",
                                "content": "You are a QA robot expert in temporal related questions.",
                            },
                            {"role": "user", "content": simple_question},
                            {"role": "assistant", "content": simple_answer},
                        ]
                    }
                )

    # dump it into a jsonl file
    with open(
        FINE_TUNE_LOGS_DIR / f"finetune_data_{identifier_file_name}.jsonl", "w"
    ) as f:
        for line in fine_tune_data:
            f.write(json.dumps(line) + "\n")

    with open(
        FINE_TUNE_LOGS_DIR / f"evaluation_data_{identifier_file_name}.jsonl", "w"
    ) as f:
        for line in evaluation_data:
            f.write(json.dumps(line) + "\n")

    self.create_fine_tune_task(
        train_filename=(
            FINE_TUNE_LOGS_DIR / f"finetune_data_{identifier_file_name}.jsonl"
        ).as_posix(),
        eval_filename=(
            FINE_TUNE_LOGS_DIR / f"evaluation_data_{identifier_file_name}.jsonl"
        ).as_posix(),
        identifier_file_name=identifier_file_name,
    )

paraphrased_question(question)

Parameters:

Name Type Description Default
question str

The question

required

Returns:

Name Type Description
str

The paraphrased question

Source code in TimelineKGQA/rag/finetune_llm.py
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
def paraphrased_question(self, question: str):
    """
    Args:
        question (str): The question

    Returns:
        str: The paraphrased question

    """

    try:
        prompt = f"""
        Please paraphrase the following question with the same meaning but another way to ask:
        {question}
        Return it in json format with the key "paraphrased_question"
        """
        response = client.chat.completions.create(
            model=self.paraphrased_model,
            messages=[
                {"role": "user", "content": prompt},
            ],
            temperature=0.0,
            response_format={"type": "json_object"},
        )
        logger.info(response.choices[0].message.content)
        paraphrased_question_str = response.choices[0].message.content
        paraphrased_question = json.loads(paraphrased_question_str).get(
            "paraphrased_question", ""
        )
        return paraphrased_question
    except Exception as e:
        logger.exception(e)
        return ""