File size: 1,888 Bytes
bac8f2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
import openai
import sys
import os

import csv
import json

import lookup
import gpt

openai.api_key = "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"
if not openai.api_key:
    openai.api_key = input("Please enter your OpenAI API key: ")
    print()

program_name = sys.argv.pop(0)

# CSV processing
csv_file_path = "LIR.csv"  # Update with the correct path
with open(csv_file_path, newline='', encoding='utf-8') as csvfile:
    reader = csv.DictReader(csvfile)
    rows = list(reader)

# Configuration Parameters
chunk_size = 4000
overlap = 1000
limit = 20  # Change to 3 to get the top 3 answers
gpt.model = "gpt-3.5-turbo"

# Chunking CSV text
chunks = [row['texte'][i:i + chunk_size] for row in rows for i in range(0, len(row['texte']), chunk_size)]

print("Chunking CSV...\n")

def ask_question(question):
    keywords = gpt.get_keywords(question)
    matches = lookup.find_matches(chunks, keywords)
    top_matches = list(matches.keys())[:limit]

    responses = []

    for i, chunk_id in enumerate(top_matches):
        chunk = chunks[chunk_id]
        response = gpt.answer_question(chunk, question)

        if response.get("answer_found"):
            matched_row = rows[chunk_id]
            # Extract specific properties from the matched row
            answer = response.get("response")

            # Loop through the columns and add them to the JSON object
            json_object = {"GPT_Response": answer}

            for column_name, column_value in matched_row.items():
                json_object[column_name] = column_value.encode("utf-8").decode("utf-8")


            responses.append(json_object)

    responses.append({"keywords:": keywords})

    if not any(response.get("answer_found") for chunk_id in top_matches):
        responses.append({"GPT_Response": "I'm sorry, but I can't find that information"})

    return responses