|
|
|
|
|
|
|
from llama_index import GPTVectorStoreIndex, SimpleDirectoryReader, LLMPredictor, ServiceContext, StorageContext, load_index_from_storage |
|
from langchain import OpenAI |
|
from langchain.prompts import ChatMessagePromptTemplate |
|
import gradio |
|
import os |
|
|
|
os.environ["OPENAI_API_KEY"] = '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' |
|
|
|
def construct_index(directory_path): |
|
|
|
num_outputs = 256 |
|
|
|
_llm_predictor = LLMPredictor(llm=OpenAI(temperature=0.5, model_name="gpt-3.5-turbo", max_tokens=num_outputs)) |
|
|
|
service_context = ServiceContext.from_defaults(llm_predictor=_llm_predictor) |
|
|
|
docs = SimpleDirectoryReader(directory_path).load_data() |
|
|
|
index = GPTVectorStoreIndex.from_documents(docs, service_context=service_context) |
|
|
|
|
|
index.storage_context.persist(persist_dir="indexes") |
|
|
|
return index |
|
|
|
def chatbot(input_text): |
|
|
|
|
|
storage_context = StorageContext.from_defaults(persist_dir="indexes") |
|
|
|
|
|
query_engne = load_index_from_storage(storage_context).as_query_engine() |
|
|
|
|
|
response = query_engne.query(input_text) |
|
|
|
|
|
return response.response |
|
|
|
|
|
iface = gradio.Interface(fn=chatbot, |
|
inputs=gradio.inputs.Textbox(lines=5, label="Skriv din fråga"), |
|
outputs="text", |
|
title="NK-bot") |
|
|
|
|
|
|
|
|
|
|
|
|
|
iface.launch(share=True) |