Spaces:
Runtime error
Runtime error
Larry Yin
commited on
Commit
•
e7d9fc5
1
Parent(s):
42894a4
Initial commit
Browse files- .env +2 -0
- .gitignore +2 -0
- app.py +82 -0
- requirements.txt +81 -0
.env
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
OPENAI_API_KEY=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
|
2 |
+
HF_API_KEY=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
|
.gitignore
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
.DS_Store
|
2 |
+
|
app.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import openai
|
3 |
+
import sys
|
4 |
+
import io
|
5 |
+
import gradio as gr
|
6 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
7 |
+
from langchain.vectorstores.pgvector import PGVector
|
8 |
+
from langchain.chat_models import ChatOpenAI
|
9 |
+
from langchain.chains import ConversationalRetrievalChain
|
10 |
+
from langchain.memory import ConversationTokenBufferMemory
|
11 |
+
from dotenv import load_dotenv, find_dotenv
|
12 |
+
_ = load_dotenv(find_dotenv()) # read local .env file
|
13 |
+
|
14 |
+
openai.api_key = os.environ['OPENAI_API_KEY']
|
15 |
+
hf_api_key = os.environ['HF_API_KEY']
|
16 |
+
|
17 |
+
embedding = OpenAIEmbeddings()
|
18 |
+
|
19 |
+
host = "experian-ai-instance-1.cyqxijnzhxga.us-west-2.rds.amazonaws.com"
|
20 |
+
port = "5432"
|
21 |
+
database_name = "experian"
|
22 |
+
user = "larryyin"
|
23 |
+
passwd = "experianai"
|
24 |
+
|
25 |
+
CONNECTION_STRING = PGVector.connection_string_from_db_params(
|
26 |
+
driver=os.environ.get("PGVECTOR_DRIVER", "psycopg2"),
|
27 |
+
host=os.environ.get("PGVECTOR_HOST", host),
|
28 |
+
port=int(os.environ.get("PGVECTOR_PORT", port)),
|
29 |
+
database=os.environ.get("PGVECTOR_DATABASE", database_name),
|
30 |
+
user=os.environ.get("PGVECTOR_USER", user),
|
31 |
+
password=os.environ.get("PGVECTOR_PASSWORD", passwd),
|
32 |
+
)
|
33 |
+
|
34 |
+
COLLECTION_NAME = "experian230725"
|
35 |
+
|
36 |
+
vectordb = PGVector(embedding_function=embedding,
|
37 |
+
collection_name=COLLECTION_NAME,
|
38 |
+
connection_string=CONNECTION_STRING,
|
39 |
+
)
|
40 |
+
|
41 |
+
# llm_name = "gpt-3.5-turbo"
|
42 |
+
llm_name = "gpt-3.5-turbo-16k"
|
43 |
+
# llm_name = "gpt-4-32k"
|
44 |
+
llm = ChatOpenAI(model_name=llm_name, temperature=0)
|
45 |
+
|
46 |
+
retriever=vectordb.as_retriever()
|
47 |
+
memory = ConversationTokenBufferMemory(
|
48 |
+
llm = llm,
|
49 |
+
max_token_limit=8000,
|
50 |
+
memory_key="chat_history",
|
51 |
+
return_messages=True
|
52 |
+
)
|
53 |
+
qa = ConversationalRetrievalChain.from_llm(
|
54 |
+
llm,
|
55 |
+
retriever=retriever,
|
56 |
+
memory=memory,
|
57 |
+
verbose=False
|
58 |
+
)
|
59 |
+
|
60 |
+
with gr.Blocks() as demo:
|
61 |
+
gr.Markdown("# Experian Bot V0.2")
|
62 |
+
chatbot = gr.Chatbot()
|
63 |
+
msg = gr.Textbox(label="Type your message (Shift + Enter to submit)", lines=6)
|
64 |
+
submit = gr.Button("Submit")
|
65 |
+
clear = gr.Button("Clear")
|
66 |
+
|
67 |
+
def respond(message, chat_history):
|
68 |
+
result = qa({"question": message})
|
69 |
+
chat_history.append((message, result["answer"]))
|
70 |
+
return ("", chat_history)
|
71 |
+
|
72 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot], queue=False)
|
73 |
+
submit.click(respond, [msg, chatbot], [msg, chatbot], queue=False)
|
74 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
75 |
+
|
76 |
+
gr.close_all()
|
77 |
+
demo.queue()
|
78 |
+
demo.launch(share=True)
|
79 |
+
|
80 |
+
# gr.close_all()
|
81 |
+
# demo.close()
|
82 |
+
# demo.clear()
|
requirements.txt
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
aiofiles==23.1.0
|
2 |
+
aiohttp==3.8.5
|
3 |
+
aiosignal==1.3.1
|
4 |
+
altair==5.0.1
|
5 |
+
anyio==3.7.1
|
6 |
+
async-timeout==4.0.2
|
7 |
+
attrs==23.1.0
|
8 |
+
certifi==2023.7.22
|
9 |
+
charset-normalizer==3.2.0
|
10 |
+
click==8.1.6
|
11 |
+
contourpy==1.1.0
|
12 |
+
cycler==0.11.0
|
13 |
+
dataclasses-json==0.5.14
|
14 |
+
fastapi==0.101.0
|
15 |
+
ffmpy==0.3.1
|
16 |
+
filelock==3.12.2
|
17 |
+
fonttools==4.42.0
|
18 |
+
frozenlist==1.4.0
|
19 |
+
fsspec==2023.6.0
|
20 |
+
gradio==3.39.0
|
21 |
+
gradio_client==0.3.0
|
22 |
+
greenlet==2.0.2
|
23 |
+
h11==0.14.0
|
24 |
+
httpcore==0.17.3
|
25 |
+
httpx==0.24.1
|
26 |
+
huggingface-hub==0.16.4
|
27 |
+
idna==3.4
|
28 |
+
Jinja2==3.1.2
|
29 |
+
jsonschema==4.18.6
|
30 |
+
jsonschema-specifications==2023.7.1
|
31 |
+
kiwisolver==1.4.4
|
32 |
+
langchain==0.0.252
|
33 |
+
langsmith==0.0.19
|
34 |
+
linkify-it-py==2.0.2
|
35 |
+
markdown-it-py==2.2.0
|
36 |
+
MarkupSafe==2.1.3
|
37 |
+
marshmallow==3.20.1
|
38 |
+
matplotlib==3.7.2
|
39 |
+
mdit-py-plugins==0.3.3
|
40 |
+
mdurl==0.1.2
|
41 |
+
multidict==6.0.4
|
42 |
+
mypy-extensions==1.0.0
|
43 |
+
numexpr==2.8.4
|
44 |
+
numpy==1.25.2
|
45 |
+
openai==0.27.8
|
46 |
+
openapi-schema-pydantic==1.2.4
|
47 |
+
orjson==3.9.2
|
48 |
+
packaging==23.1
|
49 |
+
pandas==2.0.3
|
50 |
+
pgvector==0.2.1
|
51 |
+
Pillow==10.0.0
|
52 |
+
psycopg2-binary==2.9.6
|
53 |
+
pydantic==1.10.12
|
54 |
+
pydub==0.25.1
|
55 |
+
pyparsing==3.0.9
|
56 |
+
python-dateutil==2.8.2
|
57 |
+
python-dotenv==1.0.0
|
58 |
+
python-multipart==0.0.6
|
59 |
+
pytz==2023.3
|
60 |
+
PyYAML==6.0.1
|
61 |
+
referencing==0.30.2
|
62 |
+
regex==2023.6.3
|
63 |
+
requests==2.31.0
|
64 |
+
rpds-py==0.9.2
|
65 |
+
semantic-version==2.10.0
|
66 |
+
six==1.16.0
|
67 |
+
sniffio==1.3.0
|
68 |
+
SQLAlchemy==2.0.19
|
69 |
+
starlette==0.27.0
|
70 |
+
tenacity==8.2.2
|
71 |
+
tiktoken==0.4.0
|
72 |
+
toolz==0.12.0
|
73 |
+
tqdm==4.65.0
|
74 |
+
typing-inspect==0.9.0
|
75 |
+
typing_extensions==4.7.1
|
76 |
+
tzdata==2023.3
|
77 |
+
uc-micro-py==1.0.2
|
78 |
+
urllib3==2.0.4
|
79 |
+
uvicorn==0.23.2
|
80 |
+
websockets==11.0.3
|
81 |
+
yarl==1.9.2
|