ChatPandai / app.py
thoristhor's picture
Update app.py
268a2a1
raw
history blame
6.2 kB
import os
from typing import Optional, Tuple
import gradio as gr
from langchain.chains import ConversationChain
from langchain.llms import OpenAI
from threading import Lock
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores.faiss import FAISS
from langchain.docstore.document import Document
from langchain.chains.question_answering import load_qa_chain
def load_chain():
"""Logic for loading the chain you want to use should go here."""
llm = OpenAI(temperature=0)
chain = ConversationChain(llm=llm)
return chain
def set_openai_api_key(api_key):
"""Set the api key and return chain.
If no api_key, then None is returned.
"""
if api_key and api_key.startswith("sk-") and len(api_key) > 50:
os.environ["OPENAI_API_KEY"] = api_key
print("\n\n ++++++++++++++ Setting OpenAI API key ++++++++++++++ \n\n")
print(str(datetime.datetime.now()) + ": Before OpenAI, OPENAI_API_KEY length: " + str(
len(os.environ["OPENAI_API_KEY"])))
llm = OpenAI(temperature=0, max_tokens=MAX_TOKENS)
print(str(datetime.datetime.now()) + ": After OpenAI, OPENAI_API_KEY length: " + str(
len(os.environ["OPENAI_API_KEY"])))
chain, express_chain, memory = load_chain(TOOLS_DEFAULT_LIST, llm)
# Pertains to question answering functionality
embeddings = OpenAIEmbeddings()
qa_chain = load_qa_chain(OpenAI(temperature=0), chain_type="stuff")
print(str(datetime.datetime.now()) + ": After load_chain, OPENAI_API_KEY length: " + str(
len(os.environ["OPENAI_API_KEY"])))
os.environ["OPENAI_API_KEY"] = ""
return chain, express_chain, llm, embeddings, qa_chain, memory
return None, None, None, None, None, None
class ChatWrapper:
def __init__(self):
self.lock = Lock()
def __call__(
self, api_key: str, inp: str, history: Optional[Tuple[str, str]], chain: Optional[ConversationChain], use_embeddings, monologue:bool
):
"""Execute the chat functionality."""
self.lock.acquire()
try:
history = history or []
# If chain is None, that is because no API key was provided.
if chain is None:
history.append((inp, "Please paste your OpenAI key to use"))
return history, history
# Set OpenAI key
import openai
openai.api_key = api_key
if not monologue:
if use_embeddings:
if inp and inp.strip() != "":
if docsearch:
docs = docsearch.similarity_search(inp)
output = str(qa_chain.run(input_documents=docs, question=inp))
else:
output, hidden_text = "Please supply some text in the the Embeddings tab.", None
else:
output, hidden_text = "What's on your mind?", None
else:
output, hidden_text = run_chain(chain, inp, capture_hidden_text=trace_chain)
else:
output, hidden_text = inp, None
# Run chain and append input.
output = chain.run(input=inp)
history.append((inp, output))
except Exception as e:
raise e
finally:
self.lock.release()
return history, history
# Pertains to question answering functionality
def update_embeddings(embeddings_text, embeddings, qa_chain):
if embeddings_text:
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
texts = text_splitter.split_text(embeddings_text)
docsearch = FAISS.from_texts(texts, embeddings)
print("Embeddings updated")
return docsearch
# Pertains to question answering functionality
def update_use_embeddings(widget, state):
if widget:
state = widget
return state
chat = ChatWrapper()
block = gr.Blocks(css=".gradio-container {background-color: lightgray}")
with gt.Tab("Chat"):
with gr.Row():
gr.Markdown("<h3><center>LangChain Demo</center></h3>")
openai_api_key_textbox = gr.Textbox(
placeholder="Paste your OpenAI API key (sk-...)",
show_label=False,
lines=1,
type="password",
)
chatbot = gr.Chatbot()
with gr.Row():
message = gr.Textbox(
label="Treat it like ChatGPT",
placeholder="Buat soalan darjah enam tentang biologi",
lines=1,
)
submit = gr.Button(value="Send", variant="secondary").style(full_width=False)
gr.Examples(
examples=[
"Siapakah PM Malaysia",
"create multiple choice question around chair?",
"Whats 2 + 2?",
],
inputs=message,
)
with gr.Tab("Embeddings"):
embeddings_text_box = gr.Textbox(label="Enter text for embeddings and hit Create:",
lines=20)
with gr.Row():
use_embeddings_cb = gr.Checkbox(label="Use embeddings", value=False)
use_embeddings_cb.change(update_use_embeddings, inputs=[use_embeddings_cb, use_embeddings_state],
outputs=[use_embeddings_state])
embeddings_text_submit = gr.Button(value="Create", variant="secondary").style(full_width=False)
embeddings_text_submit.click(update_embeddings,
inputs=[embeddings_text_box, embeddings_state, qa_chain_state],
outputs=[docsearch_state])
state = gr.State()
agent_state = gr.State()
submit.click(chat, inputs=[openai_api_key_textbox, message, state, agent_state], outputs=[chatbot, state])
message.submit(chat, inputs=[openai_api_key_textbox, message, state, agent_state], outputs=[chatbot, state])
openai_api_key_textbox.change(
set_openai_api_key,
inputs=[openai_api_key_textbox],
outputs=[agent_state],
)
block.launch(debug=True)