Spaces:
Sleeping
Sleeping
File size: 1,688 Bytes
377da30 |
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 54 55 |
from llama_index import SimpleDirectoryReader, GPTListIndex, readers, GPTSimpleVectorIndex, LLMPredictor, PromptHelper, ServiceContext
from langchain import OpenAI
from langchain import LLMChain, PromptTemplate
from langchain.memory import ConversationBufferMemory
import sys
import os
import gradio as gr
from IPython.display import Markdown, display
def construct_index(directory_path):
# set maximum input size
max_input_size = 4096
# set number of output tokens
num_outputs = 2000
# set maximum chunk overlap
max_chunk_overlap = 20
# set chunk size limit
chunk_size_limit = 600
# define prompt helper
prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
# define LLM
llm_predictor = LLMPredictor(llm=OpenAI(temperature=0.5, model_name="text-davinci-003", max_tokens=num_outputs))
documents = SimpleDirectoryReader(directory_path).load_data()
service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor, prompt_helper=prompt_helper)
index = GPTSimpleVectorIndex.from_documents(documents, service_context=service_context)
index.save_to_disk('index.json')
return index
def ask_ai(query):
index = GPTSimpleVectorIndex.load_from_disk('index.json')
response = index.query(query)
return f"Response: *{response.response}*"
OPENAI_API_KEY="OPENAI_API_KEY"
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
construct_index("chatbotdata")
iface = gr.Interface(
fn=ask_ai,
inputs="text",
outputs=gr.outputs.Textbox(),
layout="vertical",
title="Ask AI",
description="Ask the AI any question.",
)
iface.launch()
|