|
|
| from llama_index import LLMPredictor, PromptHelper, StorageContext, ServiceContext, load_index_from_storage, SimpleDirectoryReader, GPTVectorStoreIndex |
| from langchain.chat_models import ChatOpenAI |
| import gradio as gr |
| import sys |
| import os |
| import openai |
| from ratelimit import limits, sleep_and_retry |
| from langchain import HuggingFaceHub |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| os.environ["OPENAI_API_KEY"] = os.environ.get("openai_key") |
| openai.api_key = os.environ["OPENAI_API_KEY"] |
|
|
| |
| RATE_LIMIT = 3 |
|
|
| |
| @sleep_and_retry |
| @limits(calls=RATE_LIMIT, period=1) |
| def create_service_context(): |
|
|
| |
| |
| |
| |
| |
| |
|
|
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| max_input_size = 4096 |
| num_outputs = 3300 |
| |
| max_chunk_overlap = 15 |
| chunk_size_limit = 600 |
|
|
| |
| 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=0.5, model_name="gpt-3.5-turbo", max_tokens=num_outputs)) |
| |
| |
| service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor, prompt_helper=prompt_helper) |
| return service_context |
|
|
|
|
| |
| @sleep_and_retry |
| @limits(calls=RATE_LIMIT, period=1) |
| def data_ingestion_indexing(directory_path): |
|
|
| |
| documents = SimpleDirectoryReader(directory_path).load_data() |
|
|
| |
| index = GPTVectorStoreIndex.from_documents( |
| documents, service_context=create_service_context() |
| ) |
|
|
| |
| index.storage_context.persist() |
|
|
| return index |
|
|
| def data_querying(input_text): |
|
|
| |
| storage_context = StorageContext.from_defaults(persist_dir="./storage") |
|
|
| |
| index = load_index_from_storage(storage_context, service_context=create_service_context()) |
|
|
| |
| response = index.as_query_engine().query(input_text) |
|
|
| return response.response |
|
|
| iface = gr.Interface(fn=data_querying, |
| inputs=gr.components.Textbox(lines=20, label="Enter your question"), |
| outputs=gr.components.Textbox(lines=25, label="Response", style="height: 400px; overflow-y: scroll;"), |
| title="Philosophy GPT - Aristotle Complete Works: https://www.rauterberg.employee.id.tue.nl/lecturenotes/DDM110%20CAS/Aristotle-320BC%20The%20Complete%20Works.pdf") |
|
|
| |
| index = data_ingestion_indexing("books-philosophy") |
| iface.launch(inline=True) |
|
|