Spaces:
Runtime error
Runtime error
Commit
·
562552a
1
Parent(s):
da67f32
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,60 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import openai
|
| 3 |
import random
|
| 4 |
import time
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
openai
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
with gr.Blocks() as demo:
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
def ask_gpt(message, messages_history):
|
| 30 |
-
messages_history += [{"role": "user", "content": message}]
|
| 31 |
-
response = openai.ChatCompletion.create(
|
| 32 |
-
model="gpt-3.5-turbo",
|
| 33 |
-
messages=messages_history
|
| 34 |
-
)
|
| 35 |
-
return response['choices'][0]['message']['content'], messages_history
|
| 36 |
-
|
| 37 |
-
def init_history(messages_history):
|
| 38 |
-
messages_history = []
|
| 39 |
-
messages_history += [system_message]
|
| 40 |
-
return messages_history
|
| 41 |
-
|
| 42 |
-
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 43 |
-
bot, [chatbot, state], [chatbot, state]
|
| 44 |
-
)
|
| 45 |
-
|
| 46 |
-
clear.click(lambda: None, None, chatbot, queue=False).success(init_history, [state], [state])
|
| 47 |
-
|
| 48 |
demo.launch()
|
|
|
|
|
|
|
| 1 |
import openai
|
| 2 |
import random
|
| 3 |
import time
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import os
|
| 6 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 7 |
+
from langchain.vectorstores import DeepLake
|
| 8 |
+
from langchain.chat_models import ChatOpenAI
|
| 9 |
+
from langchain.chains import ConversationalRetrievalChain
|
| 10 |
+
|
| 11 |
+
def set_api_key(key):
|
| 12 |
+
os.environ["OPENAI_API_KEY"] = key
|
| 13 |
+
return f"Key Successfully Set to: {key}"
|
| 14 |
+
|
| 15 |
+
def get_api_key():
|
| 16 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
| 17 |
+
return api_key
|
| 18 |
+
|
| 19 |
+
def respond(message, chat_history):
|
| 20 |
+
|
| 21 |
+
# Get embeddings
|
| 22 |
+
embeddings = OpenAIEmbeddings()
|
| 23 |
+
|
| 24 |
+
#Connect to existing vectorstore
|
| 25 |
+
db = DeepLake(dataset_path="./documentation_db", embedding_function=embeddings, read_only=True)
|
| 26 |
+
#Set retriever settings
|
| 27 |
+
retriever = db.as_retriever(search_kwargs={"distance_metric":'cos',
|
| 28 |
+
"fetch_k":20,
|
| 29 |
+
"maximal_marginal_relevance":True,
|
| 30 |
+
"k":20})
|
| 31 |
+
|
| 32 |
+
# Create ChatOpenAI and ConversationalRetrievalChain
|
| 33 |
+
model = ChatOpenAI(model='gpt-3.5-turbo')
|
| 34 |
+
qa = ConversationalRetrievalChain.from_llm(model, retriever)
|
| 35 |
+
|
| 36 |
+
chat_history=[]
|
| 37 |
+
bot_message = qa({"question": message, "chat_history": chat_history})
|
| 38 |
+
chat_history.append((message, bot_message['answer']))
|
| 39 |
+
time.sleep(1)
|
| 40 |
+
return "", chat_history
|
| 41 |
|
| 42 |
with gr.Blocks() as demo:
|
| 43 |
+
with gr.Tab("OpenAI API Key Submission"):
|
| 44 |
+
api_input = gr.Textbox(label = "API Key", placeholder = "Please provide your OpenAI API key here")
|
| 45 |
+
api_submission_conf = gr.Textbox(label = "Submission Confirmation")
|
| 46 |
+
api_submit_button = gr.Button("Submit")
|
| 47 |
+
|
| 48 |
+
with gr.Tab("Coding Assistant"):
|
| 49 |
+
api_check_button = gr.Button("Get API Key")
|
| 50 |
+
api_print = gr.Textbox(label = "OpenAI API Key - Please ensure the API Key is set correctly")
|
| 51 |
+
chatbot = gr.Chatbot(label="ChatGPT Powered Coding Assistant")
|
| 52 |
+
msg = gr.Textbox(label="User Prompt", placeholder="Your Query Here")
|
| 53 |
+
clear = gr.Button("Clear")
|
| 54 |
+
|
| 55 |
+
api_submit_button.click(set_api_key, inputs=api_input, outputs=api_submission_conf)
|
| 56 |
+
api_check_button.click(get_api_key, outputs=api_print)
|
| 57 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 58 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 59 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
demo.launch()
|