File size: 2,391 Bytes
8dc7e9f
5669f53
a5d9ed3
4cca4c9
 
5669f53
 
 
 
d1a3943
5669f53
 
c00bb17
03b394c
 
6b7eaf7
5669f53
c00bb17
5669f53
c00bb17
880e5a0
c00bb17
5669f53
c00bb17
c2ea10b
c00bb17
c2ea10b
5669f53
 
 
c00bb17
e282c2e
080fddd
4cca4c9
 
 
 
 
 
78252ea
4cca4c9
78252ea
4cca4c9
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
from llama_index import Prompt, SimpleDirectoryReader, LLMPredictor, PromptHelper, StorageContext, ServiceContext, GPTVectorStoreIndex, load_index_from_storage
from langchain.chat_models import ChatOpenAI
from langchain.prompts import ChatPromptTemplate
from langchain.prompts import HumanMessagePromptTemplate
from langchain.schema.messages import SystemMessage
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-4", 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):
    # load the index from disk
    query_engine = index.as_query_engine()
    # define custom Prompt
    chat_template = ChatPromptTemplate.from_messages(
    [
        SystemMessage(
            content=(
                "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. Dado esto, por favor responde a todas las preguntas."
            )
        ),
        HumanMessagePromptTemplate.from_template("{inpu_text}"),
    ])
    response = query_engine.query(chat_template)
    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)