Upload 2 files
Browse files- app.py +189 -0
- requirements.txt +8 -0
app.py
ADDED
@@ -0,0 +1,189 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
|
3 |
+
## Setup
|
4 |
+
# Import the necessary Libraries
|
5 |
+
import gradio as gr
|
6 |
+
import pandas as pd
|
7 |
+
import os
|
8 |
+
import json
|
9 |
+
import uuid
|
10 |
+
import tiktoken
|
11 |
+
import openai
|
12 |
+
from dotenv import load_dotenv
|
13 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
14 |
+
from langchain_core.documents import Document
|
15 |
+
from langchain_community.document_loaders import PyPDFDirectoryLoader
|
16 |
+
from langchain_community.embeddings.sentence_transformer import (
|
17 |
+
SentenceTransformerEmbeddings
|
18 |
+
)
|
19 |
+
from langchain_community.vectorstores import Chroma
|
20 |
+
|
21 |
+
from langchain_community.chat_models import ChatOpenAI
|
22 |
+
|
23 |
+
from huggingface_hub import CommitScheduler
|
24 |
+
from pathlib import Path
|
25 |
+
|
26 |
+
|
27 |
+
|
28 |
+
# Create Client
|
29 |
+
anyscale_api_key = userdata.get('anyscale_apiKey')
|
30 |
+
|
31 |
+
client = OpenAI(base_url="https://api.endpoints.anyscale.com/v1",
|
32 |
+
api_key=anyscale_api_key
|
33 |
+
)
|
34 |
+
|
35 |
+
# Define the embedding model and the vectorstore
|
36 |
+
model_name = "thenlper/gte-large"
|
37 |
+
embedding_model = "thenlper/gte-large"
|
38 |
+
embedding_model = SentenceTransformerEmbeddings(model_name='thenlper/gte-large')
|
39 |
+
|
40 |
+
persisted_vectordb_location = '/content/drive/MyDrive/finsightsdb'
|
41 |
+
collection_name = 'finsights_grey-10k'
|
42 |
+
|
43 |
+
|
44 |
+
# Load the persisted vectorDB
|
45 |
+
vectorstore_persisted = Chroma(
|
46 |
+
collection_name=collection_name,
|
47 |
+
persist_directory=persisted_vectordb_location,
|
48 |
+
embedding_function=embedding_model
|
49 |
+
)
|
50 |
+
|
51 |
+
# Prepare the logging functionality
|
52 |
+
|
53 |
+
log_file = Path("logs/") / f"data_{uuid.uuid4()}.json"
|
54 |
+
log_folder = log_file.parent
|
55 |
+
|
56 |
+
scheduler = CommitScheduler(
|
57 |
+
repo_id="project3-logs",
|
58 |
+
repo_type="dataset",
|
59 |
+
folder_path=log_folder,
|
60 |
+
path_in_repo="data",
|
61 |
+
every=2
|
62 |
+
)
|
63 |
+
|
64 |
+
# Define the Q&A system message
|
65 |
+
qna_system_message = """
|
66 |
+
You are an assistant to a financial services firm. Your task is to determine the most effective platform to support the generation by the firm of advanced analytics and insights for investment management and financial planning.
|
67 |
+
|
68 |
+
User input will include the necessary context for you to answer their questions. This context will begin with the token: ###Context.
|
69 |
+
The context contains references to specific portions of documents relevant to the user's query, along with source links.
|
70 |
+
The source for a context will begin with the token ###Source
|
71 |
+
|
72 |
+
When crafting your response:
|
73 |
+
1. Select only context relevant to answer the question.
|
74 |
+
2. Include the source links in your response.
|
75 |
+
3. User questions will begin with the token: ###Question.
|
76 |
+
4. If the question is irrelevant to the firm's business respond with - "I am an AI assistant for Finsights Grey Inc. I can only help you with questions related to financial analytics."
|
77 |
+
|
78 |
+
Please adhere to the following guidelines:
|
79 |
+
- Your response should only be about the question asked and nothing else.
|
80 |
+
- Answer only using the context provided.
|
81 |
+
- Do not mention anything about the context in your final answer.
|
82 |
+
- If the answer is not found in the context, it is very very important for you to respond with "I don't know. Please check the docs @ 'https://docs.finsights.io/'"
|
83 |
+
- Always quote the source when you use the context. Cite the relevant source at the end of your response under the section - Source:
|
84 |
+
- Do not make up sources. Use the links provided in the sources section of the context and nothing else. You are prohibited from providing other links/sources.
|
85 |
+
|
86 |
+
Here is an example of how to structure your response:
|
87 |
+
|
88 |
+
Answer:
|
89 |
+
[Answer]
|
90 |
+
|
91 |
+
Source:
|
92 |
+
[Source]
|
93 |
+
"""
|
94 |
+
|
95 |
+
|
96 |
+
# Define the user message template
|
97 |
+
qna_user_message_template = """
|
98 |
+
###Context
|
99 |
+
Here are some documents and their source links that are relevant to the question mentioned below.
|
100 |
+
{context}
|
101 |
+
|
102 |
+
###Question
|
103 |
+
{question}
|
104 |
+
"""
|
105 |
+
|
106 |
+
# Define the predict function that runs when 'Submit' is clicked or when a API request is made
|
107 |
+
def predict(user_input,company):
|
108 |
+
|
109 |
+
filter = "/dataset/"+company+"-10-k-2023.pdf"
|
110 |
+
relevant_document_chunks = vectorstore_persisted.similarity_search(user_input, k=5, filter={"source":filter})
|
111 |
+
|
112 |
+
# Create context_for_query
|
113 |
+
context_list = [d.page_content + "\n Page number: " + str(d.metadata['page']) + "\n ###Source: " + d.metadata['source'] + "\n\n " for d in relevant_document_chunks]
|
114 |
+
context_for_query = ". ".join(context_list)
|
115 |
+
|
116 |
+
|
117 |
+
# Create messages
|
118 |
+
prompt = [
|
119 |
+
{'role':'system', 'content': qna_system_message},
|
120 |
+
{'role': 'user', 'content': qna_user_message_template.format(
|
121 |
+
context=context_for_query,
|
122 |
+
question=user_input
|
123 |
+
)
|
124 |
+
}
|
125 |
+
]
|
126 |
+
|
127 |
+
# Get response from the LLM
|
128 |
+
# Handle errors using try-except
|
129 |
+
# print the content of the response
|
130 |
+
try:
|
131 |
+
response = client.chat.completions.create(
|
132 |
+
model=model_name,
|
133 |
+
messages=prompt,
|
134 |
+
temperature=0
|
135 |
+
)
|
136 |
+
|
137 |
+
prediction = response.choices[0].message.content.strip()
|
138 |
+
except Exception as e:
|
139 |
+
prediction = f'Sorry, I encountered the following error: \n {e}'
|
140 |
+
|
141 |
+
print(prediction)
|
142 |
+
|
143 |
+
|
144 |
+
# While the prediction is made, log both the inputs and outputs to a local log file
|
145 |
+
# While writing to the log file, ensure that the commit scheduler is locked to avoid parallel
|
146 |
+
# access
|
147 |
+
|
148 |
+
with scheduler.lock:
|
149 |
+
with log_file.open("a") as f:
|
150 |
+
f.write(json.dumps(
|
151 |
+
{
|
152 |
+
'user_input': user_input,
|
153 |
+
'retrieved_context': context_for_query,
|
154 |
+
'model_response': prediction
|
155 |
+
}
|
156 |
+
))
|
157 |
+
f.write("\n")
|
158 |
+
|
159 |
+
return prediction
|
160 |
+
|
161 |
+
# Set-up the Gradio UI
|
162 |
+
# Add text box and radio button to the interface
|
163 |
+
# The radio button is used to select the company 10k report in which the context needs to be retrieved.
|
164 |
+
# The text box is used to enter the question.
|
165 |
+
# The submit button is used to run the predict function
|
166 |
+
|
167 |
+
|
168 |
+
textbox = gr.Textbox()
|
169 |
+
company = gr.Radio(choices=['aws', 'google', 'meta', 'msft', 'IBM'], label="Select a company:")
|
170 |
+
|
171 |
+
#predict = gr.Button("Submit")
|
172 |
+
predict.click(predict, inputs=[textbox,company], outputs=[predict])
|
173 |
+
|
174 |
+
|
175 |
+
|
176 |
+
|
177 |
+
# Create the interface
|
178 |
+
# For the inputs parameter of Interface provide [textbox,company]
|
179 |
+
# For the outputs parameter of Interface provide [predict]
|
180 |
+
demo = gr.Interface(
|
181 |
+
fn=predict,
|
182 |
+
inputs=[textbox,company],
|
183 |
+
outputs=[predict],
|
184 |
+
title="AI-Powered Question Answering")
|
185 |
+
|
186 |
+
# Run the interface
|
187 |
+
|
188 |
+
demo.queue()
|
189 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
openai==1.23.2
|
2 |
+
tiktoken==0.6.0
|
3 |
+
langchain==0.1.1
|
4 |
+
langchain-community==0.0.13
|
5 |
+
chromadb==0.4.22
|
6 |
+
sentence-transformers==2.3.1
|
7 |
+
datasets
|
8 |
+
pypdf
|