Created
May 28, 2023 05:34
-
-
Save Rizr09/30dae1c60b94de517ca917a4f0ae512d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from llama_index import SimpleDirectoryReader, GPTListIndex, LLMPredictor, PromptHelper, ServiceContext, GPTSimpleVectorIndex | |
from langchain.chat_models import ChatOpenAI | |
from langchain.llms import OpenAI | |
import gradio as gr | |
import sys | |
import os | |
""" | |
Requirement: | |
langchain==0.0.153 | |
llama-index==0.5.4 | |
PyPDF2 | |
gradio | |
buat pdf disimpan di folder ./docs | |
recommended pakai venv | |
python main.py, nanti tinggal buka di local URL | |
""" | |
os.environ["OPENAI_API_KEY"] = 'openai_api_key' | |
def construct_index(directory_path): | |
max_input_size = 4096 | |
num_outputs = 1024 | |
max_chunk_overlap = 40 | |
chunk_size_limit = 1200 | |
prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit) | |
# gpt-3.5-turbo paling bagus | |
llm_predictor = LLMPredictor(llm=OpenAI(temperature=0.2, model_name="gpt-3.5-turbo", max_tokens=num_outputs)) | |
documents = SimpleDirectoryReader(directory_path).load_data() | |
service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor) | |
index = GPTSimpleVectorIndex.from_documents(documents, service_context=service_context) | |
index.save_to_disk('index.json') | |
return index | |
def chatbot(input_text): | |
index = GPTSimpleVectorIndex.load_from_disk('index.json') | |
response = index.query(input_text, response_mode="compact") | |
return response.response | |
iface = gr.Interface(fn=chatbot, | |
inputs=gr.inputs.Textbox(lines=7, label="Enter your text"), | |
outputs="text", | |
title="test") | |
index = construct_index("docs") | |
iface.launch(share=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment