From 9fa87dbf169468011ad778cd010ffb4cffca957b Mon Sep 17 00:00:00 2001 From: MOIS3Y Date: Mon, 24 Apr 2023 17:04:18 +0900 Subject: [PATCH] Create: mvp --- poetry.lock | 17 ++++++++++++++++- pyproject.toml | 6 ++++++ src/chadgpt/main.py | 26 +++++++++++++------------- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/poetry.lock b/poetry.lock index af0d151..d9ab356 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1978,6 +1978,21 @@ files = [ [package.dependencies] six = ">=1.5" +[[package]] +name = "python-dotenv" +version = "1.0.0" +description = "Read key-value pairs from a .env file and set them as environment variables" +category = "main" +optional = false +python-versions = ">=3.8" +files = [ + {file = "python-dotenv-1.0.0.tar.gz", hash = "sha256:a8df96034aae6d2d50a4ebe8216326c61c3eb64836776504fcca410e5937a3ba"}, + {file = "python_dotenv-1.0.0-py3-none-any.whl", hash = "sha256:f5971a9226b701070a4bf2c38c89e5a3f0d64de8debda981d1db98583009122a"}, +] + +[package.extras] +cli = ["click (>=5.0)"] + [[package]] name = "python-multipart" version = "0.0.6" @@ -2636,4 +2651,4 @@ multidict = ">=4.0" [metadata] lock-version = "2.0" python-versions = "^3.11" -content-hash = "190c777d4ffddd08f97be99418024b9eef296fe835bce89f16db821354f749c0" +content-hash = "45093343787af14605e9eaa3b71391b90bc039795a061b5d9e99e19f53620af7" diff --git a/pyproject.toml b/pyproject.toml index 4959a11..4df96b5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,11 +13,17 @@ gpt-index = "0.4.24" pypdf2 = "^3.0.1" pycryptodome = "^3.17" gradio = "^3.27.0" +python-dotenv = "^1.0.0" [tool.poetry.group.dev.dependencies] flake8 = "^6.0.0" + +[tool.poetry.scripts] +chadgpt = "chadgpt.main:main" + + [build-system] requires = ["poetry-core"] build-backend = "poetry.core.masonry.api" diff --git a/src/chadgpt/main.py b/src/chadgpt/main.py index 64cd1be..ccb4895 100644 --- a/src/chadgpt/main.py +++ b/src/chadgpt/main.py @@ -1,12 +1,10 @@ #!/usr/bin/env python3 import os -import sys -import gradio -from dotenv import load_dotenv, find_dotenv +import gradio as gr +from dotenv import load_dotenv from langchain.chat_models import ChatOpenAI from gpt_index import ( SimpleDirectoryReader, - GPTListIndex, GPTSimpleVectorIndex, LLMPredictor, PromptHelper @@ -15,8 +13,8 @@ from gpt_index import ( def get_env(): if not os.environ.get("OPENAI_API_KEY"): - load_dotenv(find_dotenv()) - + load_dotenv() + # parse hidden api key: def construct_index(directory_path): @@ -33,7 +31,7 @@ def construct_index(directory_path): chunk_size_limit=chunk_size_limit ) - llm=ChatOpenAI( + llm = ChatOpenAI( temperature=0.7, model_name="gpt-3.5-turbo", max_tokens=num_outputs @@ -50,28 +48,30 @@ def construct_index(directory_path): prompt_helper=prompt_helper ) - index.save_to_disk('index.json') + index_file = os.environ.get('DB_PATH') + "/index.json" + index.save_to_disk(index_file) return index + def chatbot(input_text): - index = GPTSimpleVectorIndex.load_from_disk('index.json') + index_file = os.environ.get("DB_PATH") + "/index.json" + index = GPTSimpleVectorIndex.load_from_disk(index_file) response = index.query(input_text, response_mode="compact") return response.response -iface = gradio.Interface( +iface = gr.Interface( fn=chatbot, - inputs=gradio.components.Textbox(lines=7, label="Enter your text"), + inputs=gr.components.Textbox(lines=7, label="Enter your text"), outputs="text", title="ISPsystem custom-trained AI Chatbot" ) -index = construct_index("../docs") - def main(): get_env() + construct_index(os.environ.get("DB_PATH")) iface.launch(share=False)