File size: 2,348 Bytes
8dc7e9f
080fddd
5669f53
 
 
 
 
d1a3943
5669f53
 
c00bb17
03b394c
 
6b7eaf7
5669f53
c00bb17
5669f53
c00bb17
 
 
5669f53
c00bb17
c2ea10b
c00bb17
c2ea10b
5669f53
 
e282c2e
 
 
 
 
5669f53
c00bb17
e282c2e
080fddd
 
 
 
c00bb17
080fddd
 
c00bb17
e282c2e
5669f53
 
 
c2ea10b
5669f53
441fdf4
5669f53
c00bb17
c2ea10b
5669f53
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
from llama_index import Prompt, SimpleDirectoryReader, LLMPredictor, PromptHelper, StorageContext, ServiceContext, GPTVectorStoreIndex, load_index_from_storage
from llama_index.prompts import PromptTemplate
from langchain.chat_models import ChatOpenAI
import gradio as gr
import sys
import os

os.environ["OPENAI_API_KEY"]

def construct_index(directory_path):
    # setup parameters
    max_input_size = 4096
    num_outputs = 512
    max_chunk_overlap = 0.2
    chunk_size_limit = 600
    # create a prompt helper
    prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
    # initialize the predictor with a fine-tuned model
    llm_predictor = LLMPredictor(llm=ChatOpenAI(temperature=0.9, model_name="gpt-3.5-turbo", max_tokens=num_outputs))
    # load documents from the specified directory
    documents = SimpleDirectoryReader(directory_path).load_data()
    # construct the index
    index = GPTVectorStoreIndex(documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper)
    # save the index to disk
    index.storage_context.persist(persist_dir="index.json")
    return index


def chatbot(input_text):
    response = query_engine.query(input_text)
    return response.response

def chatbot(input_text):
    # load the index from disk
    query_engine = index.as_query_engine()
    # define custom Prompt
    TEMPLATE_STR = (
        "Quiero que actues como un asistente personal de un cliente del Banco Galicia. Tu nombre es Gala. Me brindas información sobre mi resument de tarjeta de credito VISA. Si la respuesta no esta en el documento, respondeme de forma creativa que no lo sabes, pero que podes ayudarme con otra pregunta. Nunca te enojes y no contestes preguntas politicas o religiosas. \n"
        "---------------------\n"
        "Dado esto, por favor responde a la pregunta: {input_text}\n"
    )
    QA_TEMPLATE = PromptTemplate(TEMPLATE_STR)
    # query the index and get the response
    response = query_engine.query(input_text)
    return response.response

iface = gr.Interface(fn=chatbot,
                     inputs=gr.components.Textbox(lines=7, label="Ingresa tu pregunta"),
                     outputs="text",
                     title="Demo Galicia")

# construct the index
index = construct_index("docs")
iface.launch(share=True, debug=True)