forked from dkedar7/embedchain-fastdash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
94 lines (73 loc) · 2.67 KB
/
app.py
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
import os
from fast_dash import FastDash, dcc, dmc, Chat
from flask import session
from embedchain_utils import generate_response
# Define components
openai_api_key_component = dmc.PasswordInput(
placeholder="API Key",
description="Get yours at https://platform.openai.com/account/api-keys",
required=True,
)
web_page_urls_component = dmc.MultiSelect(
description="Include all the reference web URLs",
placeholder="Enter URLs separated by commas",
searchable=True,
creatable=True,
)
text_component = dmc.Textarea(
placeholder="Write your query here",
autosize=True,
minRows=4,
description="Any additional information that could be useful",
)
query_component = dmc.Textarea(
placeholder="Write your query here",
autosize=True,
minRows=4,
required=True,
description="Write your query here",
)
answer_component = dcc.Markdown(
style={"text-align": "left", "padding": "1%"}, link_target="_blank"
)
def explore_your_knowledge_base(
openai_api_key: openai_api_key_component,
web_page_urls: web_page_urls_component,
youtube_urls: web_page_urls_component,
pdf_urls: web_page_urls_component,
text: text_component,
query: text_component,
) -> Chat:
"""
Input your sources and let GPT4 find answers. Built with Fast Dash.
This app uses embedchain.ai, which abstracts the entire process of loading and chunking datasets, creating embeddings, and storing them in a vector database.
Embedchain itself uses Langchain and OpenAI's ChatGPT API.
"""
answer_suffix = ""
if not openai_api_key:
answer = "Did you forget adding your OpenAI API key? If you don't have one, you can get it [here](https://platform.openai.com/account/api-keys)."
elif not query:
answer = "Did you forget writing your query in the query box?"
else:
os.environ["OPENAI_API_KEY"] = openai_api_key
# Get chat history from Flask session
chat_history = session.get("chat_history", [])
# Generate a response
answer = generate_response(web_page_urls, youtube_urls, pdf_urls, text, query, chat_history)
# Save chat history back to the session cache
chat_history.append([query, answer])
session["chat_history"] = chat_history
answer = f"""{answer}
{answer_suffix}
"""
chat = dict(query=query, response=answer)
return chat
# Build app (this is all it takes!). Fast Dash understands what it needs to do.
app = FastDash(
explore_your_knowledge_base,
github_url="https://github.com/dkedar7/embedchain-fastdash",
)
server = app.server
server.config["SECRET_KEY"] = "Some key"
if __name__ == "__main__":
app.run()