File size: 12,020 Bytes
a52c7ce c62e5cd 969f5dc c62e5cd 969f5dc c62e5cd a835cf0 c62e5cd 969f5dc cd62201 a835cf0 bc3a2c2 a835cf0 bc3a2c2 a835cf0 1433ce9 5c15af4 1433ce9 969f5dc 1433ce9 cd62201 1433ce9 cd62201 55da24c 969f5dc a835cf0 55da24c 969f5dc 55da24c 969f5dc a835cf0 969f5dc a835cf0 969f5dc a835cf0 c62e5cd 1433ce9 c62e5cd 969f5dc 55da24c c62e5cd 969f5dc c62e5cd a835cf0 1433ce9 a835cf0 35725ab a835cf0 55da24c 0f43d97 a835cf0 55da24c a835cf0 c62e5cd a835cf0 b10b3aa a835cf0 b10b3aa 0f43d97 55da24c a835cf0 1433ce9 a835cf0 c62e5cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# chat-pykg/app.py
import datetime
import os
import gradio as gr
import chromadb
from chromadb.config import Settings
# logging.basicConfig(stream=sys.stdout, level=logging.INFO)
# logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
from langchain.vectorstores import Chroma
from langchain.docstore.document import Document
import shutil
import random, string
from chain import get_new_chain1
from ingest import ingest_docs
def randomword(length):
letters = string.ascii_lowercase
return ''.join(random.choice(letters) for i in range(length))
def change_tab():
return gr.Tabs.update(selected=0)
def merge_collections(collection_load_names, vs_state):
merged_documents = []
merged_embeddings = []
client = chromadb.Client(Settings(
chroma_db_impl="duckdb+parquet",
persist_directory=".persisted_data" # Optional, defaults to .chromadb/ in the current directory
))
for collection_name in collection_load_names:
collection_name = collection_name
if collection_name == '':
continue
collection = client.get_collection(collection_name)
collection = collection.get(include=["metadatas", "documents", "embeddings"])
for i in range(len(collection['documents'])):
merged_documents.append(Document(page_content=collection['documents'][i], metadata = collection['metadatas'][i]))
merged_embeddings.append(collection['embeddings'][i])
merged_collection_name = "merged_collection"
merged_vectorstore = Chroma.from_documents(documents=merged_documents, embeddings=merged_embeddings, collection_name=merged_collection_name)
return merged_vectorstore
def set_chain_up(openai_api_key, model_selector, k_textbox, max_tokens_textbox, vectorstore, agent):
if not agent or type(agent) == str:
if vectorstore != None:
if model_selector in ["gpt-3.5-turbo", "gpt-4"]:
if openai_api_key:
os.environ["OPENAI_API_KEY"] = openai_api_key
qa_chain = get_new_chain1(vectorstore, model_selector, k_textbox, max_tokens_textbox)
os.environ["OPENAI_API_KEY"] = ""
return qa_chain
else:
return 'no_open_aikey'
else:
qa_chain = get_new_chain1(vectorstore, model_selector, k_textbox, max_tokens_textbox)
return qa_chain
else:
return 'no_vectorstore'
else:
return agent
def delete_collection(all_collections_state, collections_viewer):
client = chromadb.Client(Settings(
chroma_db_impl="duckdb+parquet",
persist_directory=".persisted_data" # Optional, defaults to .chromadb/ in the current directory
))
for collection in collections_viewer:
client.delete_collection(collection)
all_collections_state.remove(collection)
collections_viewer.remove(collection)
return all_collections_state, collections_viewer
def delete_all_collections(all_collections_state):
shutil.rmtree(".persisted_data")
return []
def list_collections(all_collections_state):
client = chromadb.Client(Settings(
chroma_db_impl="duckdb+parquet",
persist_directory=".persisted_data" # Optional, defaults to .chromadb/ in the current directory
))
collection_names = [[c.name][0] for c in client.list_collections()]
return collection_names
def update_checkboxgroup(all_collections_state):
new_options = [i for i in all_collections_state]
return gr.CheckboxGroup.update(choices=new_options)
def destroy_agent(agent):
agent = None
return agent
def chat(inp, history, agent):
history = history or []
if type(agent) == str:
if agent == 'no_open_aikey':
history.append((inp, "Please paste your OpenAI key to use"))
return history, history
if agent == 'no_vectorstore':
history.append((inp, "Please ingest some package docs to use"))
return history, history
if agent == 'all_collections' and inp != []:
history.append(("", f"Current vectorstores: {inp}"))
return history, history
if agent == 'all_vs_deleted':
history.append((inp, "All vectorstores deleted"))
return history, history
else:
print("\n==== date/time: " + str(datetime.datetime.now()) + " ====")
print("inp: " + inp)
history = history or []
output = agent({"question": inp, "chat_history": history})
answer = output["answer"]
history.append((inp, answer))
print(history)
return history, history
block = gr.Blocks(css=".gradio-container {background-color: system;}")
with block:
gr.Markdown("<h3><center>chat-pykg</center></h3>")
with gr.Tabs() as tabs:
with gr.TabItem("Chat", id=0):
with gr.Row():
openai_api_key_textbox = gr.Textbox(
placeholder="Paste your OpenAI API key (sk-...)",
show_label=False,
lines=1,
type="password",
)
model_selector = gr.Dropdown(["gpt-3.5-turbo", "gpt-4", "other"], label="Model", show_label=True)
model_selector.value = "gpt-3.5-turbo"
k_textbox = gr.Textbox(
placeholder="k: Number of search results to consider",
label="Search Results k:",
show_label=True,
lines=1,
)
k_textbox.value = "20"
max_tokens_textbox = gr.Textbox(
placeholder="max_tokens: Maximum number of tokens to generate",
label="max_tokens",
show_label=True,
lines=1,
)
max_tokens_textbox.value="2000"
chatbot = gr.Chatbot()
with gr.Row():
message = gr.Textbox(
label="What's your question?",
placeholder="What is this code?",
lines=1,
)
submit = gr.Button(value="Send", variant="secondary").style(full_width=False)
gr.Examples(
examples=[
"What does this code do?",
"Where is this specific method in the source code and why is it broken?"
],
inputs=message,
)
gr.HTML(
"""
This simple application is an implementation of ChatGPT but over an external dataset.
The source code is split/broken down into many document objects using langchain's pythoncodetextsplitter, which apparently tries to keep whole functions etc. together. This means that each file in the source code is split into many smaller documents, and the k value is the number of documents to consider when searching for the most similar documents to the question. With gpt-3.5-turbo, k=10 seems to work well, but with gpt-4, k=20 seems to work better.
The model's memory is set to 5 messages, but I haven't tested with gpt-3.5-turbo yet to see if it works well. It seems to work well with gpt-4."""
)
with gr.TabItem("Collections manager", id=1):
with gr.Row():
with gr.Column(scale=2):
all_collections_to_get = gr.List(headers=['New Collections to make'],row_count=3, label='Collections_to_get', show_label=True, interactive=True, max_cols=1, max_rows=3)
make_collections_button = gr.Button(value="Make new collection(s)", variant="secondary").style(full_width=False)
with gr.Row():
chunk_size_textbox = gr.Textbox(
placeholder="Chunk size",
label="Chunk size",
show_label=True,
lines=1,
)
chunk_overlap_textbox = gr.Textbox(
placeholder="Chunk overlap",
label="Chunk overlap",
show_label=True,
lines=1,
)
chunk_size_textbox.value = "1000"
chunk_overlap_textbox.value = "1000"
with gr.Row():
gr.HTML('<center>See the <a href=https://python.langchain.com/en/latest/reference/modules/text_splitter.html>Langchain textsplitter docs</a></center>')
with gr.Column(scale=2):
collections_viewer = gr.CheckboxGroup(choices=[], label='Collections_viewer', show_label=True)
with gr.Column(scale=1):
load_collections_button = gr.Button(value="Load collection(s) to chat!", variant="secondary").style(full_width=False)
get_all_collection_names_button = gr.Button(value="List all saved collections", variant="secondary").style(full_width=False)
delete_collections_button = gr.Button(value="Delete selected saved collections", variant="secondary").style(full_width=False)
delete_all_collections_button = gr.Button(value="Delete all saved collections", variant="secondary").style(full_width=False)
gr.HTML(
"<center>Powered by <a href='https://github.com/hwchase17/langchain'>LangChain 🦜️🔗</a></center>"
)
history_state = gr.State()
agent_state = gr.State()
vs_state = gr.State()
all_collections_state = gr.State()
chat_state = gr.State()
submit.click(set_chain_up, inputs=[openai_api_key_textbox, model_selector, k_textbox, max_tokens_textbox, vs_state, agent_state], outputs=[agent_state]).then(chat, inputs=[message, history_state, agent_state], outputs=[chatbot, history_state])
message.submit(chat, inputs=[message, history_state, agent_state], outputs=[chatbot, history_state])
load_collections_button.click(merge_collections, inputs=[collections_viewer, vs_state], outputs=[vs_state])#.then(change_tab, None, tabs) #.then(set_chain_up, inputs=[openai_api_key_textbox, model_selector, k_textbox, max_tokens_textbox, vs_state, agent_state], outputs=[agent_state])
make_collections_button.click(ingest_docs, inputs=[all_collections_state, all_collections_to_get, chunk_size_textbox, chunk_overlap_textbox], outputs=[all_collections_state], show_progress=True).then(update_checkboxgroup, inputs = [all_collections_state], outputs = [collections_viewer])
delete_collections_button.click(delete_collection, inputs=[all_collections_state, collections_viewer], outputs=[all_collections_state, collections_viewer]).then(update_checkboxgroup, inputs = [all_collections_state], outputs = [collections_viewer])
delete_all_collections_button.click(delete_all_collections, inputs=[all_collections_state], outputs=[all_collections_state]).then(update_checkboxgroup, inputs = [all_collections_state], outputs = [collections_viewer])
get_all_collection_names_button.click(list_collections, inputs=[all_collections_state], outputs=[all_collections_state]).then(update_checkboxgroup, inputs = [all_collections_state], outputs = [collections_viewer])
# Whenever chain parameters change, destroy the agent.
input_list = [openai_api_key_textbox, model_selector, k_textbox, max_tokens_textbox]
output_list = [agent_state]
for input_item in input_list:
input_item.change(
destroy_agent,
inputs=output_list,
outputs=output_list,
)
all_collections_state.value = list_collections(all_collections_state)
block.load(update_checkboxgroup, inputs = all_collections_state, outputs = collections_viewer)
block.launch(debug=True)
|