-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from sbslee/0.2.0-dev
0.2.0 dev
- Loading branch information
Showing
13 changed files
with
283 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
# CHANGELOG | ||
|
||
## 0.2.0 (2023-03-27) | ||
* Enable users to initiate a new chat session. | ||
* Enable users to choose their preferred GPT model. | ||
* Enable users to chat with their documents. | ||
|
||
## 0.1.0 (2023-03-24) | ||
Initial release. | ||
* Initial release. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,26 @@ | ||
# kanu | ||
# README | ||
|
||
KANU is a minimalistic Python-based GUI for various chatbots. | ||
|
||
## Installation | ||
|
||
``` | ||
$ pip install kanu | ||
``` | ||
|
||
## Screenshots | ||
|
||
### Homepage | ||
|
||
![Alt Text](./images/home.png) | ||
|
||
### ChatGPT | ||
|
||
![Alt Text](./images/chatgpt-1.png) | ||
![Alt Text](./images/chatgpt-2.png) | ||
|
||
### DocGPT | ||
|
||
![Alt Text](./images/docgpt-1.png) | ||
![Alt Text](./images/docgpt-2.png) | ||
![Alt Text](./images/docgpt-3.png) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import tkinter as tk | ||
|
||
import openai | ||
|
||
class ChatGPT: | ||
def __init__(self, kanu, openai_key): | ||
self.kanu = kanu | ||
self.kanu.container.pack_forget() | ||
self.kanu.container = tk.Frame(self.kanu.root) | ||
self.kanu.container.pack() | ||
openai.api_key = openai_key | ||
|
||
def run(self): | ||
title_label = tk.Label(self.kanu.container, text="ChatGPT") | ||
title_label.grid(row=0, column=0, columnspan=3) | ||
self.model = tk.StringVar(self.kanu.container, value="gpt-3.5-turbo") | ||
model_label = tk.Label(self.kanu.container, text="Model:") | ||
model_label.grid(row=1, column=0) | ||
model1_button = tk.Radiobutton(self.kanu.container, variable=self.model, text="gpt-3.5-turbo", value="gpt-3.5-turbo") | ||
model2_button = tk.Radiobutton(self.kanu.container, variable=self.model, text="gpt-4", value="gpt-4") | ||
model1_button.grid(row=1, column=1) | ||
model2_button.grid(row=1, column=2) | ||
session_label = tk.Label(self.kanu.container, text="Chat session") | ||
session_label.grid(row=2, column=0, columnspan=3) | ||
self.session = tk.Text(self.kanu.container, width=70, height=20) | ||
self.session.grid(row=3, column=0, columnspan=3) | ||
entry = tk.Entry(self.kanu.container, width=54) | ||
entry.grid(row=4, column=0, columnspan=3) | ||
self.messages = [] | ||
send_button = tk.Button(self.kanu.container, text="Send", command=lambda: self._send_message(entry)) | ||
send_button.grid(row=5, column=0) | ||
clear_butoon = tk.Button(self.kanu.container, text="Clear", command=lambda: self._clear_session()) | ||
clear_butoon.grid(row=5, column=1) | ||
back_button = tk.Button(self.kanu.container, text="Go back", command=lambda: self.kanu.chatgpt_config()) | ||
back_button.grid(row=5, column=2) | ||
|
||
def _send_message(self, entry): | ||
if not self.messages: | ||
self.messages.append({"role": "system", "content": "You are a helpful assistant."}) | ||
self.messages += [{"role": "user", "content": entry.get()}] | ||
bot_response = openai.ChatCompletion.create( | ||
model=self.model.get(), | ||
messages=self.messages, | ||
) | ||
response = bot_response["choices"][0]["message"]["content"] | ||
self.messages += [{"role": "assistant", "content": response}] | ||
self.session.insert(tk.END, "You: " + entry.get() + "\n") | ||
self.session.insert(tk.END, f"Bot ({self.model.get()}): " + response + "\n") | ||
entry.delete(0, tk.END) | ||
|
||
def _clear_session(self): | ||
self.session.delete(1.0, tk.END) | ||
self.messages.clear() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import os | ||
import tkinter as tk | ||
from tkinter import filedialog | ||
|
||
from langchain.embeddings.openai import OpenAIEmbeddings | ||
from langchain.text_splitter import CharacterTextSplitter | ||
from langchain.vectorstores import Chroma | ||
from langchain.llms import OpenAI | ||
from langchain.document_loaders import TextLoader | ||
from langchain.chains import RetrievalQA | ||
|
||
class DocGPT: | ||
def __init__(self, kanu, openai_key): | ||
self.kanu = kanu | ||
os.environ["OPENAI_API_KEY"] = openai_key | ||
|
||
def run(self): | ||
self.kanu.container.pack_forget() | ||
self.kanu.container = tk.Frame(self.kanu.root) | ||
self.kanu.container.pack() | ||
label = tk.Label(self.kanu.container, text="DocGPT") | ||
label.grid(row=0, column=0, columnspan=3) | ||
back_button = tk.Button(self.kanu.container, text="Go back", command=lambda: self.kanu.docgpt_config()) | ||
back_button.grid(row=1, column=0) | ||
back_button = tk.Button(self.kanu.container, text="Reload", command=lambda: self.run()) | ||
back_button.grid(row=1, column=2) | ||
label = tk.Message(self.kanu.container, width=300, text="Option 1. Create a new database") | ||
label.grid(row=2, column=0, columnspan=3) | ||
f = tk.Label(self.kanu.container, text="Document:") | ||
f.grid(row=3, column=0) | ||
self.document_label = tk.Label(self.kanu.container, text="No file selected", fg="red") | ||
self.document_label.grid(row=3, column=1) | ||
b = tk.Button(self.kanu.container, text="Browse", command=self.specify_document_file) | ||
b.grid(row=3, column=2) | ||
l = tk.Label(self.kanu.container, text="Database:") | ||
l.grid(row=4, column=0) | ||
self.new_database_label = tk.Label(self.kanu.container, text="No directory selected", fg="red") | ||
self.new_database_label.grid(row=4, column=1) | ||
b = tk.Button(self.kanu.container, text="Browse", command=self.specify_new_database_directory) | ||
b.grid(row=4, column=2) | ||
self.option1_button = tk.Button(self.kanu.container, text="Go with Option 1", command=self.go_with_option1) | ||
self.option1_button.grid(row=5, column=0, columnspan=3) | ||
self.option1_button["state"] = tk.DISABLED | ||
label = tk.Message(self.kanu.container, width=300, text="Option 2. Use an existing database") | ||
label.grid(row=6, column=0, columnspan=3) | ||
f = tk.Label(self.kanu.container, text="Database:") | ||
f.grid(row=7, column=0) | ||
self.old_database_label = tk.Label(self.kanu.container, text="No directory selected", fg="red") | ||
self.old_database_label.grid(row=7, column=1) | ||
open_button = tk.Button(self.kanu.container, text="Browse", command=self.specify_old_database_directory) | ||
open_button.grid(row=7, column=2) | ||
self.option2_button = tk.Button(self.kanu.container, text="Go with Option 2", command=self.go_with_option2) | ||
self.option2_button.grid(row=8, column=0, columnspan=3) | ||
self.option2_button["state"] = tk.DISABLED | ||
|
||
def query(self): | ||
self.db = Chroma(persist_directory=self.database_directory, embedding_function=OpenAIEmbeddings()) | ||
self.qa = RetrievalQA.from_chain_type(llm=OpenAI(), chain_type="stuff", retriever=self.db.as_retriever()) | ||
self.kanu.container.pack_forget() | ||
self.kanu.container = tk.Frame(self.kanu.root) | ||
self.kanu.container.pack() | ||
title_label = tk.Label(self.kanu.container, text="DocGPT") | ||
title_label.grid(row=0, column=0, columnspan=3) | ||
session_label = tk.Label(self.kanu.container, text="Chat session") | ||
session_label.grid(row=1, column=0, columnspan=3) | ||
self.session = tk.Text(self.kanu.container, width=70, height=20) | ||
self.session.grid(row=2, column=0, columnspan=3) | ||
entry = tk.Entry(self.kanu.container, width=54) | ||
entry.grid(row=3, column=0, columnspan=3) | ||
send_button = tk.Button(self.kanu.container, text="Send", command=lambda: self._send_message(entry)) | ||
send_button.grid(row=4, column=0) | ||
clear_butoon = tk.Button(self.kanu.container, text="Clear", command=lambda: self.clear_session()) | ||
clear_butoon.grid(row=4, column=1) | ||
back_button = tk.Button(self.kanu.container, text="Go back", command=lambda: self.run()) | ||
back_button.grid(row=4, column=2) | ||
|
||
def _send_message(self, entry): | ||
self.session.insert(tk.END, "You: " + entry.get() + "\n") | ||
response = self.qa(entry.get())["result"] | ||
self.session.insert(tk.END, "Bot:" + response + "\n") | ||
entry.delete(0, tk.END) | ||
|
||
def go_with_option1(self): | ||
loader = TextLoader(self.document_file) | ||
documents = loader.load() | ||
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) | ||
docs = text_splitter.split_documents(documents) | ||
embeddings = OpenAIEmbeddings() | ||
db = Chroma.from_documents(docs, embeddings, persist_directory=self.database_directory) | ||
db.persist() | ||
db = None | ||
self.query() | ||
|
||
def go_with_option2(self): | ||
self.query() | ||
|
||
def specify_document_file(self): | ||
file_path = filedialog.askopenfilename() | ||
if not file_path: | ||
return | ||
self.document_file = file_path | ||
self.document_label.configure(text=os.path.basename(file_path), fg="lime green") | ||
if self.new_database_label["text"] != "No directory selected": | ||
self.option1_button["state"] = tk.NORMAL | ||
|
||
def specify_new_database_directory(self): | ||
directory_path = filedialog.askdirectory() | ||
if not directory_path: | ||
return | ||
self.database_directory = directory_path | ||
self.new_database_label.configure(text=os.path.basename(directory_path), fg="lime green") | ||
if self.document_label["text"] != "No file selected": | ||
self.option1_button["state"] = tk.NORMAL | ||
|
||
def specify_old_database_directory(self): | ||
directory_path = filedialog.askdirectory() | ||
if not directory_path: | ||
return | ||
self.database_directory = directory_path | ||
self.old_database_label.configure(text=os.path.basename(directory_path), fg="lime green") | ||
self.option2_button["state"] = tk.NORMAL | ||
|
||
def clear_session(self): | ||
self.session.delete(1.0, tk.END) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,12 +7,11 @@ | |
version=__version__, | ||
author='Seung-been "Steven" Lee', | ||
author_email="[email protected]", | ||
description="KANU", | ||
description="A minimalistic Python-based GUI for various chatbots", | ||
url="https://github.com/sbslee/kanu", | ||
packages=find_packages(), | ||
install_requires=["openai"], | ||
license="MIT", | ||
entry_points={"console_scripts": ["kanu=kanu.__main__:main"]}, | ||
long_description="This is a detailed description of the package.", | ||
long_description="A minimalistic Python-based GUI for various chatbots", | ||
long_description_content_type="text/plain" | ||
) |