Spaces:
Runtime error
Runtime error
Commit ·
ceca01b
1
Parent(s): a37b230
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
import csv
|
| 3 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 4 |
+
from langchain.vectorstores import FAISS
|
| 5 |
+
from langchain.llms import OpenAI
|
| 6 |
+
from langchain.chat_models import ChatOpenAI
|
| 7 |
+
from langchain.vectorstores import Pinecone
|
| 8 |
+
from langchain.chains import RetrievalQA
|
| 9 |
+
import os
|
| 10 |
+
import gradio as gr
|
| 11 |
+
import time
|
| 12 |
+
from fuzzywuzzy import fuzz
|
| 13 |
+
import pinecone
|
| 14 |
+
from getpass import getpass
|
| 15 |
+
|
| 16 |
+
os.environ["OPENAI_API_KEY"] = "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"
|
| 17 |
+
YOUR_API_KEY = "7fd42be9-4380-44f0-8f9e-c6f6a9c2f088"
|
| 18 |
+
YOUR_ENV = "northamerica-northeast1-gcp"
|
| 19 |
+
|
| 20 |
+
index_name = 'knowledgebase'
|
| 21 |
+
pinecone.init(
|
| 22 |
+
api_key=YOUR_API_KEY,
|
| 23 |
+
environment=YOUR_ENV
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
faiss_index_folder_path = "/Users/axelwindbrake/Desktop/AI Automation folders/Chatbot Ver 3/Memory/faiss_index"
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
model_name = 'text-embedding-ada-002'
|
| 31 |
+
|
| 32 |
+
embed = OpenAIEmbeddings(
|
| 33 |
+
model=model_name,
|
| 34 |
+
openai_api_key=os.environ["OPENAI_API_KEY"]
|
| 35 |
+
)
|
| 36 |
+
text_field = "text"
|
| 37 |
+
res = embed.embed_documents(text_field)
|
| 38 |
+
|
| 39 |
+
# switch back to normal index for langchain
|
| 40 |
+
index = pinecone.Index(index_name)
|
| 41 |
+
|
| 42 |
+
vectorstore = Pinecone(index, embed.embed_query, text_field)
|
| 43 |
+
|
| 44 |
+
llm = ChatOpenAI(
|
| 45 |
+
openai_api_key=os.environ["OPENAI_API_KEY"],
|
| 46 |
+
model_name='gpt-4',
|
| 47 |
+
temperature=0.5 ,
|
| 48 |
+
max_tokens=450
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
qa = RetrievalQA.from_chain_type(
|
| 52 |
+
llm=llm,
|
| 53 |
+
chain_type="stuff",
|
| 54 |
+
retriever=vectorstore.as_retriever()
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
inputs = gr.inputs.Textbox(lines=7, label="Frage:")
|
| 58 |
+
outputs = gr.outputs.Textbox(label="Antwort")
|
| 59 |
+
|
| 60 |
+
query = inputs
|
| 61 |
+
|
| 62 |
+
def answer_question(query):
|
| 63 |
+
result = vectorstore.similarity_search(query, k=3)
|
| 64 |
+
llm = ChatOpenAI(
|
| 65 |
+
openai_api_key=os.environ["OPENAI_API_KEY"],
|
| 66 |
+
model_name='gpt-4',
|
| 67 |
+
temperature=0.5 ,
|
| 68 |
+
max_tokens=850
|
| 69 |
+
)
|
| 70 |
+
qa = RetrievalQA.from_chain_type(
|
| 71 |
+
llm=llm,
|
| 72 |
+
chain_type="stuff",
|
| 73 |
+
retriever=vectorstore.as_retriever()
|
| 74 |
+
)
|
| 75 |
+
answer = qa.run(query)
|
| 76 |
+
return {"answer": answer} # Return the result as a dictionary with an "answer" key
|
| 77 |
+
|
| 78 |
+
with gr.Blocks() as demo:
|
| 79 |
+
chatbot = gr.Chatbot()
|
| 80 |
+
msg = gr.Textbox()
|
| 81 |
+
clear = gr.Button("Clear")
|
| 82 |
+
|
| 83 |
+
def respond(message, chat_history):
|
| 84 |
+
# If the message starts with "??", skip the CSV lookup
|
| 85 |
+
if not message.startswith("??"):
|
| 86 |
+
# Check if the question already has an answer in the CSV file
|
| 87 |
+
with open("q&a.csv", "r", newline='') as f:
|
| 88 |
+
reader = csv.reader(f)
|
| 89 |
+
for row in reader:
|
| 90 |
+
if fuzz.token_set_ratio(row[0], message) > 80: # Adjust the threshold as needed
|
| 91 |
+
bot_message = row[1]
|
| 92 |
+
break
|
| 93 |
+
else:
|
| 94 |
+
# If the question doesn't have an answer in the CSV file, proceed with the existing process
|
| 95 |
+
bot_response = answer_question(message)
|
| 96 |
+
if isinstance(bot_response, dict) and "answer" in bot_response:
|
| 97 |
+
bot_message = bot_response["answer"]
|
| 98 |
+
else:
|
| 99 |
+
bot_message = "Sorry, I couldn't generate a response."
|
| 100 |
+
else:
|
| 101 |
+
# If the message starts with "??", remove the "??" and proceed with the existing process
|
| 102 |
+
message = message[2:]
|
| 103 |
+
bot_response = answer_question(message)
|
| 104 |
+
if isinstance(bot_response, dict) and "answer" in bot_response:
|
| 105 |
+
bot_message = bot_response["answer"]
|
| 106 |
+
else:
|
| 107 |
+
bot_message = "Sorry, I couldn't generate a response."
|
| 108 |
+
|
| 109 |
+
# Save to CSV file
|
| 110 |
+
with open("q&a.csv", "a", newline='') as f:
|
| 111 |
+
writer = csv.writer(f)
|
| 112 |
+
writer.writerow([message, bot_message])
|
| 113 |
+
|
| 114 |
+
chat_history.append((message, bot_message))
|
| 115 |
+
time.sleep(1)
|
| 116 |
+
return "", chat_history
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 122 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 123 |
+
|
| 124 |
+
with gr.Blocks() as demo:
|
| 125 |
+
instructions = gr.Markdown("## Willkommen zur Vertriebs Knowledgebase\n\n\nBitte stelle deine Frage in der Textbox unten. \n\n\nAntworten werden zunächst in den gespeicherten Chatverläufen gesucht - wenn du das nicht möchtest, beginne deine Frage mit '??'.")
|
| 126 |
+
chatbot = gr.Chatbot()
|
| 127 |
+
msg = gr.Textbox()
|
| 128 |
+
clear = gr.Button("Clear")
|
| 129 |
+
|
| 130 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 131 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
if __name__ == "__main__":
|
| 135 |
+
demo.launch(share=True, inbrowser=True)
|
| 136 |
+
|