Chat / app.py
4PHealth's picture
Update app.py
e048b6e
raw
history blame
1.52 kB
from llama_index import SimpleDirectoryReader, GPTListIndex, GPTVectorStoreIndex, StorageContext, LLMPredictor, PromptHelper, load_index_from_storage
from langchain.chat_models import ChatOpenAI
import gradio as gr
import sys
import os
os.environ["OPENAI_API_KEY"] = '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'
def construct_index(directory_path):
max_input_size = 1000000
num_outputs = 256
max_chunk_overlap = 20
chunk_size_limit = 2048
prompt_helper = PromptHelper(max_input_size, num_outputs, chunk_overlap_ratio= 0.1, chunk_size_limit=chunk_size_limit)
llm_predictor = LLMPredictor(llm=ChatOpenAI(temperature=1, model_name="gpt-4", max_tokens=num_outputs))
documents = SimpleDirectoryReader(directory_path).load_data()
index = GPTVectorStoreIndex(documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper,chunk_size_limit = 2048)
index.storage_context.persist(persist_dir="index.json")
return index
def chatbot(input_text):
storage_context = StorageContext.from_defaults(persist_dir="index.json")
index = load_index_from_storage(storage_context)
query_engine = index.as_query_engine()
response = query_engine.query(input_text)
return response.response
iface = gr.Interface(fn=chatbot,
inputs=gr.components.Textbox(lines=7, label="Enter your text"),
outputs="text",
title="Custom-trained AI Chatbot")
index = construct_index("docs")
iface.launch()