Refactoring: modular configuration, separated learning and response logic

This commit is contained in:
Stepan Zhukovsky 2023-04-24 19:41:34 +09:00
parent 0ecbfbadd0
commit 8c76b90e17
4 changed files with 69 additions and 0 deletions

10
src/chadgpt/chatbot.py Normal file
View File

@ -0,0 +1,10 @@
from gpt_index import GPTSimpleVectorIndex
from .config import DB_PATH
def chatbot(input_text):
# TODO: need check if index_file no exist
index_file = DB_PATH + "/index.json"
index = GPTSimpleVectorIndex.load_from_disk(index_file)
response = index.query(input_text, response_mode="compact")
return response.response

6
src/chadgpt/config.py Normal file
View File

@ -0,0 +1,6 @@
import os
from dotenv import load_dotenv
load_dotenv()
DB_PATH = os.environ.get("DB_PATH", "/app/db")

43
src/chadgpt/indexer.py Normal file
View File

@ -0,0 +1,43 @@
from langchain.chat_models import ChatOpenAI
from gpt_index import (
SimpleDirectoryReader,
GPTSimpleVectorIndex,
LLMPredictor,
PromptHelper
)
def construct_index(db_path):
max_input_size = 4096
num_outputs = 512
max_chunk_overlap = 20
chunk_size_limit = 600
prompt_helper = PromptHelper(
max_input_size,
num_outputs,
max_chunk_overlap,
chunk_size_limit=chunk_size_limit
)
llm = ChatOpenAI(
temperature=0.7,
model_name="gpt-3.5-turbo",
max_tokens=num_outputs
)
llm_predictor = LLMPredictor(llm)
# get documents for learn:
documents = SimpleDirectoryReader(db_path).load_data()
index = GPTSimpleVectorIndex(
documents,
llm_predictor=llm_predictor,
prompt_helper=prompt_helper
)
index_file = db_path + "/index.json"
index.save_to_disk(index_file)
return index

10
src/chadgpt/interface.py Normal file
View File

@ -0,0 +1,10 @@
import gradio as gr
from .chatbot import chatbot
iface = gr.Interface(
fn=chatbot,
inputs=gr.components.Textbox(lines=7, label="Enter your text"),
outputs="text",
title="ISPsystem custom-trained AI Chatbot"
)