| from gpt_index import SimpleDirectoryReader, GPTListIndex, GPTSimpleVectorIndex, LLMPredictor, PromptHelper |
| from langchain.chat_models import ChatOpenAI |
| import gradio as gr |
| import sys |
| import os |
|
|
| os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY") |
|
|
| def construct_index(directory_path): |
| max_input_size = 4096 |
| num_outputs = 512 |
| max_chunk_overlap = 30 |
| chunk_size_limit = 600 |
|
|
| prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit) |
|
|
| llm_predictor = LLMPredictor(llm=ChatOpenAI(temperature=0.7, model_name="gpt-3.5-turbo", max_tokens=num_outputs)) |
|
|
| documents = SimpleDirectoryReader(directory_path).load_data() |
|
|
| index = GPTSimpleVectorIndex(documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper) |
|
|
| index.save_to_disk('index.json') |
|
|
| return index |
|
|
| def chatbot(input_text): |
| index = GPTSimpleVectorIndex.load_from_disk('index.json') |
| response = index.query(input_text) |
| return response.response |
|
|
| iface = gr.Interface(fn=chatbot, |
| inputs=gr.components.Textbox(lines=5, label="Enter your text", show_copy_button=True), |
| outputs=gr.components.Textbox(lines=5, label="Answer", show_copy_button=True), |
| examples=["What are the different types of 'work product' lifespan? Provide detailed answer", "Хто може бути наставником?", "Question3", "Question4", "Question5"], |
| title="PMO Documents AI Chatbot") |
|
|
| index = construct_index("docs") |
| iface.launch() |