File size: 1,235 Bytes
33fbb25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os, sys
from os.path import dirname as up

sys.path.append(os.path.abspath(os.path.join(up(__file__), os.pardir)))

from langchain.document_loaders import CSVLoader
from langchain.indexes import VectorstoreIndexCreator
from langchain.chains import RetrievalQA
from langchain.llms import OpenAI
import os

import gradio as gr
import pandas as pd

from utils.constants import *

os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY

# Load the documents
loader = CSVLoader(file_path=CSV_FILE_PATH)

# Create an index using the loaded documents
index_creator = VectorstoreIndexCreator()
docsearch = index_creator.from_loaders([loader])

# Create a question-answering chain using the index
chain = RetrievalQA.from_chain_type(
    llm=OpenAI(),
    chain_type="stuff",
    retriever=docsearch.vectorstore.as_retriever(),
    input_key="question",
)

def return_response_chain(query: str):
    response = chain({"question": query})
    return response['result']

def clear_fields(query: str, output: str):
    query = ""
    output = ""

# if __name__ == "__main__":
#     # Pass a query to the chain
#     query = "How does UAE compare with USA in terms of gdp?"
#     response = chain({"question": query})
#     print(response['result'])