File size: 2,982 Bytes
94b1e32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import re
import pandas as pd
from modules.scraper import get_raw_data, get_raw_data_sheets
from modules.embedding_storage import process_safety_with_chroma
from modules.qa_chatbot import create_chatbot, ask_question

def extract_column_name(query_template):
    """
    Extract the column name from the query template enclosed in curly braces.
    """
    match = re.search(r"\{(.*?)\}", query_template)
    if not match:
        raise ValueError("No placeholder found in the query template. Ensure the query contains a placeholder like {column_name}.")
    return match.group(1)

def process_query_and_update_csv(file_path, query_template):
    """
    Processes the queries based on the specified column, updates the CSV file, 
    and adds an 'Answer' column with responses.
    """
    column_name = extract_column_name(query_template)
    df = pd.read_csv(file_path)
    
    if column_name not in df.columns:
        raise ValueError(f"The specified column '{column_name}' is missing in the provided CSV file.")
    
    if "Answer" not in df.columns:
        df["Answer"] = ""

    for index, row in df.iterrows():
        value = row[column_name]
        query = query_template.replace(f"{{{column_name}}}", str(value))
        
        # Process the query using provided functions
        raw_data = get_raw_data(file_path, query)
        vector_store = process_safety_with_chroma(raw_data)
        qa_system = create_chatbot(vector_store)
        prompt = f"Give me the exact answer for this below query '{query}' in a structured format with a link from the content provided only."
        answer = ask_question(qa_system, prompt)
        df.at[index, "Answer"] = answer

    df.to_csv(file_path, index=False)
    return df


def process_query_and_update_sheets(file_path, df, query_template):
    """
    Processes the queries based on the specified column, updates the CSV file, 
    and adds an 'Answer' column with responses.
    """
    column_name = extract_column_name(query_template)
    # df = pd.read_csv(file_path)
    
    if column_name not in df.columns:
        raise ValueError(f"The specified column '{column_name}' is missing in the provided CSV file.")
    
    if "Answer" not in df.columns:
        df["Answer"] = ""

    for index, row in df.iterrows():
        value = row[column_name]
        query = query_template.replace(f"{{{column_name}}}", str(value))
        # print( "Value : ", value, "Query : ", query)
        # Process the query using provided functions
        raw_data = get_raw_data_sheets(query)
        vector_store = process_safety_with_chroma(raw_data)
        qa_system = create_chatbot(vector_store)
        prompt = f"Give me the exact answer for this below query '{query}' in a structured format with a link from the content provided only."
        answer = ask_question(qa_system, prompt)
        df.at[index, "Answer"] = answer
    
    print("ddddddd")
    print(df)
    # df.to_csv(file_path, index=False)
    return df