Init: poetry project and mvp chadGPT app

This commit is contained in:
Stepan Zhukovsky 2023-04-24 13:54:59 +09:00
commit 51c27cabf2
7 changed files with 2876 additions and 0 deletions

135
.gitignore vendored Normal file
View File

@ -0,0 +1,135 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
.python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# GPT artifacts
*.pdf
db/
articles/
index.json

0
README.md Normal file
View File

2639
poetry.lock generated Normal file

File diff suppressed because it is too large Load Diff

23
pyproject.toml Normal file
View File

@ -0,0 +1,23 @@
[tool.poetry]
name = "chadgpt"
version = "0.1.0"
description = "chatGPT client for knowledge base"
authors = ["MOIS3Y <stepan@zhukovsky.me>"]
readme = "README.md"
packages = [{include = "chadgpt", from = "src"}]
[tool.poetry.dependencies]
python = "^3.11"
openai = "^0.27.4"
gpt-index = "0.4.24"
pypdf2 = "^3.0.1"
pycryptodome = "^3.17"
gradio = "^3.27.0"
[tool.poetry.group.dev.dependencies]
flake8 = "^6.0.0"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

0
src/chadgpt/__init__.py Normal file
View File

79
src/chadgpt/main.py Normal file
View File

@ -0,0 +1,79 @@
#!/usr/bin/env python3
import os
import sys
import gradio
from dotenv import load_dotenv, find_dotenv
from langchain.chat_models import ChatOpenAI
from gpt_index import (
SimpleDirectoryReader,
GPTListIndex,
GPTSimpleVectorIndex,
LLMPredictor,
PromptHelper
)
def get_env():
if not os.environ.get("OPENAI_API_KEY"):
load_dotenv(find_dotenv())
# parse hidden api key:
def construct_index(directory_path):
# promt params:
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(directory_path).load_data()
index = GPTSimpleVectorIndex(
documents,
llm_predictor=llm_predictor,
prompt_helper=prompt_helper
)
index.save_to_disk('index.json')
return index
def chatbot(input_text):
index = GPTSimpleVectorIndex.load_from_disk('index.json')
response = index.query(input_text, response_mode="compact")
return response.response
iface = gradio.Interface(
fn=chatbot,
inputs=gradio.components.Textbox(lines=7, label="Enter your text"),
outputs="text",
title="ISPsystem custom-trained AI Chatbot"
)
index = construct_index("../docs")
def main():
get_env()
iface.launch(share=False)
if __name__ == "__main__":
main()

0
tests/__init__.py Normal file
View File