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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
from notebot.app import app
|
| 4 |
|
| 5 |
if __name__ == "__main__":
|
| 6 |
-
app(
|
|
|
|
| 1 |
+
"""Main entry point for NoteBot."""
|
| 2 |
+
|
| 3 |
+
import argparse
|
| 4 |
+
import os
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
from typing import Callable, Final
|
| 7 |
+
|
| 8 |
+
import gradio as gr
|
| 9 |
+
from dotenv import load_dotenv
|
| 10 |
+
from langchain.chains import ConversationalRetrievalChain
|
| 11 |
+
from langchain.chat_models import ChatOpenAI
|
| 12 |
+
from langchain.document_loaders import GitLoader
|
| 13 |
+
from langchain.embeddings import OpenAIEmbeddings
|
| 14 |
+
from langchain.memory import ConversationBufferMemory
|
| 15 |
+
from langchain.text_splitter import MarkdownTextSplitter
|
| 16 |
+
from langchain.vectorstores import FAISS
|
| 17 |
+
|
| 18 |
+
ROOT_PATH: Final = Path(__file__).parent.parent
|
| 19 |
+
DATA_PATH: Final = ROOT_PATH / "data"
|
| 20 |
+
NOTES_PATH: Final = DATA_PATH / "notes"
|
| 21 |
+
DB_PATH: Final = DATA_PATH / "db"
|
| 22 |
+
NOTE_REPO_URL: Final = "https://github.com/slangenbach/notes.git"
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
class NoteBot:
|
| 26 |
+
"""Base class for interacting with NoteBot."""
|
| 27 |
+
|
| 28 |
+
def __init__(self, chain) -> None:
|
| 29 |
+
self.chain = chain
|
| 30 |
+
|
| 31 |
+
def get_response(self, question: str) -> dict:
|
| 32 |
+
return self.chain({"question": question})
|
| 33 |
+
|
| 34 |
+
def chat(self, message: str, history: list) -> str:
|
| 35 |
+
response = self.get_response(question=message)
|
| 36 |
+
|
| 37 |
+
return response["answer"]
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def filter_notes(file_path: str) -> bool:
|
| 41 |
+
"""
|
| 42 |
+
Filter out none markdown notes and READMEs.
|
| 43 |
+
|
| 44 |
+
Args:
|
| 45 |
+
file_path (str): Path to file
|
| 46 |
+
|
| 47 |
+
Returns:
|
| 48 |
+
bool: Whether to filter out a note
|
| 49 |
+
"""
|
| 50 |
+
return file_path.endswith(".md") and not file_path.endswith("README.md")
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def load_and_ingest_notes(note_repo_url: str, embedding) -> None:
|
| 54 |
+
"""
|
| 55 |
+
Load markdown notes from Git repo and ingest them into vector store.
|
| 56 |
+
|
| 57 |
+
Args:
|
| 58 |
+
note_repo_url (str): URL of Git repo to load notes from
|
| 59 |
+
embeddings: Embeddings to use for vector store
|
| 60 |
+
"""
|
| 61 |
+
loader = GitLoader(
|
| 62 |
+
repo_path=str(NOTES_PATH),
|
| 63 |
+
clone_url=note_repo_url,
|
| 64 |
+
file_filter=filter_notes,
|
| 65 |
+
)
|
| 66 |
+
splitter = MarkdownTextSplitter()
|
| 67 |
+
docs = loader.load_and_split(text_splitter=splitter)
|
| 68 |
+
db = FAISS.from_documents(documents=docs, embedding=embedding)
|
| 69 |
+
db.save_local(folder_path=str(DB_PATH))
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def get_chain(db):
|
| 73 |
+
"""
|
| 74 |
+
Get LangChain to interact with notes.
|
| 75 |
+
|
| 76 |
+
Args:
|
| 77 |
+
db: Vector store
|
| 78 |
+
|
| 79 |
+
Returns:
|
| 80 |
+
LangChain chain
|
| 81 |
+
"""
|
| 82 |
+
llm = ChatOpenAI(temperature=0)
|
| 83 |
+
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
| 84 |
+
chain = ConversationalRetrievalChain.from_llm(
|
| 85 |
+
llm=llm, retriever=db.as_retriever(), memory=memory
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
return chain
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def get_ui(chat_function: Callable) -> gr.ChatInterface:
|
| 92 |
+
"""
|
| 93 |
+
Create NoteBot user interface
|
| 94 |
+
|
| 95 |
+
Args:
|
| 96 |
+
chat_function (Callable): Function to chat with NoteBot
|
| 97 |
+
|
| 98 |
+
Returns:
|
| 99 |
+
gr.ChatInterface: User Interface
|
| 100 |
+
"""
|
| 101 |
+
ui = gr.ChatInterface(
|
| 102 |
+
fn=chat_function,
|
| 103 |
+
title="NoteBot",
|
| 104 |
+
examples=[
|
| 105 |
+
"List the title of all notes I can ask you about",
|
| 106 |
+
"Generate a brief summary of my LangChain notes",
|
| 107 |
+
"Do I have some notes related to COBOL?",
|
| 108 |
+
],
|
| 109 |
+
cache_examples=False,
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
return ui
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
def get_parser() -> argparse.ArgumentParser:
|
| 116 |
+
"""
|
| 117 |
+
Get argument parser for CLI options.
|
| 118 |
+
|
| 119 |
+
Returns:
|
| 120 |
+
argparse.ArgumentParser: Parser
|
| 121 |
+
"""
|
| 122 |
+
parser = argparse.ArgumentParser(description="Welcome to NoteBot")
|
| 123 |
+
parser.add_argument(
|
| 124 |
+
"--note-repo-url",
|
| 125 |
+
help="URL of Git repo to load notes from",
|
| 126 |
+
default=NOTE_REPO_URL,
|
| 127 |
+
)
|
| 128 |
+
parser.add_argument(
|
| 129 |
+
"--llm", help="LLM used by NoteBot", choices=["GPT"], default="GPT"
|
| 130 |
+
)
|
| 131 |
+
parser.add_argument(
|
| 132 |
+
"--openai-api-key", help="API key to interact with OpenAI models"
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
return parser
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
def set_openai_api_key(key: str | None) -> None:
|
| 139 |
+
"""
|
| 140 |
+
Set OpenAI API key enviroment variable if it has not been set already.
|
| 141 |
+
|
| 142 |
+
Args:
|
| 143 |
+
key (str | None): OpenAI API key
|
| 144 |
+
"""
|
| 145 |
+
if not os.environ.get("OPENAI_API_KEY"):
|
| 146 |
+
if key:
|
| 147 |
+
os.environ["OPENAI_API_KEY"] = key
|
| 148 |
+
else:
|
| 149 |
+
load_dotenv()
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
def app(run_local: bool = True):
|
| 153 |
+
"""
|
| 154 |
+
Launch NoteBot app.
|
| 155 |
+
|
| 156 |
+
Args:
|
| 157 |
+
run_local (bool, optional): Whether to launch Notebook locally. Defaults to True.
|
| 158 |
+
"""
|
| 159 |
+
if run_local:
|
| 160 |
+
parser = get_parser()
|
| 161 |
+
args = parser.parse_args()
|
| 162 |
+
repo_url = args.note_repo_url
|
| 163 |
+
set_openai_api_key(args.openai_api_key)
|
| 164 |
+
else:
|
| 165 |
+
repo_url = NOTE_REPO_URL
|
| 166 |
+
set_openai_api_key(key=None)
|
| 167 |
+
|
| 168 |
+
embedding = OpenAIEmbeddings() # pyright: ignore[reportGeneralTypeIssues]
|
| 169 |
+
|
| 170 |
+
if not NOTES_PATH.exists() and not DB_PATH.exists():
|
| 171 |
+
load_and_ingest_notes(note_repo_url=repo_url, embedding=embedding)
|
| 172 |
+
|
| 173 |
+
db = FAISS.load_local(folder_path=str(DB_PATH), embeddings=embedding)
|
| 174 |
+
chain = get_chain(db=db)
|
| 175 |
+
notebot = NoteBot(chain=chain)
|
| 176 |
+
ui = get_ui(chat_function=notebot.chat)
|
| 177 |
+
|
| 178 |
+
ui.launch()
|
| 179 |
|
|
|
|
| 180 |
|
| 181 |
if __name__ == "__main__":
|
| 182 |
+
app()
|
notebot-0.1.1-py3-none-any.whl
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:70d4577e2a24cdbac83f5ddb9d9370e9bdebe1109ebdac66089caf1614d53f34
|
| 3 |
-
size 4478
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -1 +1,87 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
aiofiles==23.1.0 ; python_version >= "3.10" and python_version < "4.0"
|
| 2 |
+
aiohttp==3.8.5 ; python_version >= "3.10" and python_version < "4.0"
|
| 3 |
+
aiosignal==1.3.1 ; python_version >= "3.10" and python_version < "4.0"
|
| 4 |
+
altair==5.0.1 ; python_version >= "3.10" and python_version < "4.0"
|
| 5 |
+
anyio==3.7.1 ; python_version >= "3.10" and python_version < "4.0"
|
| 6 |
+
async-timeout==4.0.2 ; python_version >= "3.10" and python_version < "4.0"
|
| 7 |
+
attrs==23.1.0 ; python_version >= "3.10" and python_version < "4.0"
|
| 8 |
+
certifi==2023.7.22 ; python_version >= "3.10" and python_version < "4.0"
|
| 9 |
+
charset-normalizer==3.2.0 ; python_version >= "3.10" and python_version < "4.0"
|
| 10 |
+
click==8.1.6 ; python_version >= "3.10" and python_version < "4.0"
|
| 11 |
+
colorama==0.4.6 ; python_version >= "3.10" and python_version < "4.0" and platform_system == "Windows"
|
| 12 |
+
contourpy==1.1.0 ; python_version >= "3.10" and python_version < "4.0"
|
| 13 |
+
cycler==0.11.0 ; python_version >= "3.10" and python_version < "4.0"
|
| 14 |
+
dataclasses-json==0.5.9 ; python_version >= "3.10" and python_version < "4.0"
|
| 15 |
+
exceptiongroup==1.1.2 ; python_version >= "3.10" and python_version < "3.11"
|
| 16 |
+
faiss-cpu==1.7.4 ; python_version >= "3.10" and python_version < "4.0"
|
| 17 |
+
fastapi==0.100.1 ; python_version >= "3.10" and python_version < "4.0"
|
| 18 |
+
ffmpy==0.3.1 ; python_version >= "3.10" and python_version < "4.0"
|
| 19 |
+
filelock==3.12.2 ; python_version >= "3.10" and python_version < "4.0"
|
| 20 |
+
fonttools==4.41.1 ; python_version >= "3.10" and python_version < "4.0"
|
| 21 |
+
frozenlist==1.4.0 ; python_version >= "3.10" and python_version < "4.0"
|
| 22 |
+
fsspec==2023.6.0 ; python_version >= "3.10" and python_version < "4.0"
|
| 23 |
+
gitdb==4.0.10 ; python_version >= "3.10" and python_version < "4.0"
|
| 24 |
+
gitpython==3.1.32 ; python_version >= "3.10" and python_version < "4.0"
|
| 25 |
+
gradio-client==0.3.0 ; python_version >= "3.10" and python_version < "4.0"
|
| 26 |
+
gradio==3.39.0 ; python_version >= "3.10" and python_version < "4.0"
|
| 27 |
+
greenlet==2.0.2 ; python_version >= "3.10" and python_version < "4.0" and (platform_machine == "win32" or platform_machine == "WIN32" or platform_machine == "AMD64" or platform_machine == "amd64" or platform_machine == "x86_64" or platform_machine == "ppc64le" or platform_machine == "aarch64")
|
| 28 |
+
h11==0.14.0 ; python_version >= "3.10" and python_version < "4.0"
|
| 29 |
+
httpcore==0.17.3 ; python_version >= "3.10" and python_version < "4.0"
|
| 30 |
+
httpx==0.24.1 ; python_version >= "3.10" and python_version < "4.0"
|
| 31 |
+
huggingface-hub==0.16.4 ; python_version >= "3.10" and python_version < "4.0"
|
| 32 |
+
idna==3.4 ; python_version >= "3.10" and python_version < "4.0"
|
| 33 |
+
jinja2==3.1.2 ; python_version >= "3.10" and python_version < "4.0"
|
| 34 |
+
jsonschema-specifications==2023.7.1 ; python_version >= "3.10" and python_version < "4.0"
|
| 35 |
+
jsonschema==4.18.4 ; python_version >= "3.10" and python_version < "4.0"
|
| 36 |
+
kiwisolver==1.4.4 ; python_version >= "3.10" and python_version < "4.0"
|
| 37 |
+
langchain==0.0.249 ; python_version >= "3.10" and python_version < "4.0"
|
| 38 |
+
langsmith==0.0.16 ; python_version >= "3.10" and python_version < "4.0"
|
| 39 |
+
linkify-it-py==2.0.2 ; python_version >= "3.10" and python_version < "4.0"
|
| 40 |
+
markdown-it-py==2.2.0 ; python_version >= "3.10" and python_version < "4.0"
|
| 41 |
+
markdown-it-py[linkify]==2.2.0 ; python_version >= "3.10" and python_version < "4.0"
|
| 42 |
+
markupsafe==2.1.3 ; python_version >= "3.10" and python_version < "4.0"
|
| 43 |
+
marshmallow-enum==1.5.1 ; python_version >= "3.10" and python_version < "4.0"
|
| 44 |
+
marshmallow==3.20.1 ; python_version >= "3.10" and python_version < "4.0"
|
| 45 |
+
matplotlib==3.7.2 ; python_version >= "3.10" and python_version < "4.0"
|
| 46 |
+
mdit-py-plugins==0.3.3 ; python_version >= "3.10" and python_version < "4.0"
|
| 47 |
+
mdurl==0.1.2 ; python_version >= "3.10" and python_version < "4.0"
|
| 48 |
+
multidict==6.0.4 ; python_version >= "3.10" and python_version < "4.0"
|
| 49 |
+
mypy-extensions==1.0.0 ; python_version >= "3.10" and python_version < "4.0"
|
| 50 |
+
numexpr==2.8.4 ; python_version >= "3.10" and python_version < "4.0"
|
| 51 |
+
numpy==1.25.2 ; python_version >= "3.10" and python_version < "4.0"
|
| 52 |
+
openai==0.27.8 ; python_version >= "3.10" and python_version < "4.0"
|
| 53 |
+
openapi-schema-pydantic==1.2.4 ; python_version >= "3.10" and python_version < "4.0"
|
| 54 |
+
orjson==3.9.2 ; python_version >= "3.10" and python_version < "4.0"
|
| 55 |
+
packaging==23.1 ; python_version >= "3.10" and python_version < "4.0"
|
| 56 |
+
pandas==2.0.3 ; python_version >= "3.10" and python_version < "4.0"
|
| 57 |
+
pillow==10.0.0 ; python_version >= "3.10" and python_version < "4.0"
|
| 58 |
+
pydantic==1.10.12 ; python_version >= "3.10" and python_version < "4.0"
|
| 59 |
+
pydub==0.25.1 ; python_version >= "3.10" and python_version < "4.0"
|
| 60 |
+
pyparsing==3.0.9 ; python_version >= "3.10" and python_version < "4.0"
|
| 61 |
+
python-dateutil==2.8.2 ; python_version >= "3.10" and python_version < "4.0"
|
| 62 |
+
python-dotenv==1.0.0 ; python_version >= "3.10" and python_version < "4.0"
|
| 63 |
+
python-multipart==0.0.6 ; python_version >= "3.10" and python_version < "4.0"
|
| 64 |
+
pytz==2023.3 ; python_version >= "3.10" and python_version < "4.0"
|
| 65 |
+
pyyaml==6.0.1 ; python_version >= "3.10" and python_version < "4.0"
|
| 66 |
+
referencing==0.30.0 ; python_version >= "3.10" and python_version < "4.0"
|
| 67 |
+
regex==2023.6.3 ; python_version >= "3.10" and python_version < "4.0"
|
| 68 |
+
requests==2.31.0 ; python_version >= "3.10" and python_version < "4.0"
|
| 69 |
+
rpds-py==0.9.2 ; python_version >= "3.10" and python_version < "4.0"
|
| 70 |
+
semantic-version==2.10.0 ; python_version >= "3.10" and python_version < "4.0"
|
| 71 |
+
six==1.16.0 ; python_version >= "3.10" and python_version < "4.0"
|
| 72 |
+
smmap==5.0.0 ; python_version >= "3.10" and python_version < "4.0"
|
| 73 |
+
sniffio==1.3.0 ; python_version >= "3.10" and python_version < "4.0"
|
| 74 |
+
sqlalchemy==2.0.19 ; python_version >= "3.10" and python_version < "4.0"
|
| 75 |
+
starlette==0.27.0 ; python_version >= "3.10" and python_version < "4.0"
|
| 76 |
+
tenacity==8.2.2 ; python_version >= "3.10" and python_version < "4.0"
|
| 77 |
+
tiktoken==0.4.0 ; python_version >= "3.10" and python_version < "4.0"
|
| 78 |
+
toolz==0.12.0 ; python_version >= "3.10" and python_version < "4.0"
|
| 79 |
+
tqdm==4.65.0 ; python_version >= "3.10" and python_version < "4.0"
|
| 80 |
+
typing-extensions==4.7.1 ; python_version >= "3.10" and python_version < "4.0"
|
| 81 |
+
typing-inspect==0.9.0 ; python_version >= "3.10" and python_version < "4.0"
|
| 82 |
+
tzdata==2023.3 ; python_version >= "3.10" and python_version < "4.0"
|
| 83 |
+
uc-micro-py==1.0.2 ; python_version >= "3.10" and python_version < "4.0"
|
| 84 |
+
urllib3==1.26.16 ; python_version >= "3.10" and python_version < "4.0"
|
| 85 |
+
uvicorn==0.23.2 ; python_version >= "3.10" and python_version < "4.0"
|
| 86 |
+
websockets==11.0.3 ; python_version >= "3.10" and python_version < "4.0"
|
| 87 |
+
yarl==1.9.2 ; python_version >= "3.10" and python_version < "4.0"
|