-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
182 lines (140 loc) · 5.1 KB
/
main.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import json
import torch
import faiss
import numpy as np
import openai
import os
from dotenv import load_dotenv
import streamlit as st
from transformers import AutoTokenizer, AutoModel
#### OPENAI API ####
# Load environment variables from .env file
load_dotenv()
# Get the OpenAI API key
openai_api_key = os.getenv("OPENAI_API_KEY")
# Set the OpenAI API key
openai.api_key = openai_api_key
#### DATA ####
# Read data from jsonl file
file_path_texts = "data/statements_id_statement2paragraphs_sub.jsonl"
file_path_titles = "data/statements_id_title_sub.jsonl"
texts = []
titles = []
ids = []
with open(file_path_titles, "r") as file:
for line in file:
data = json.loads(line)
titles.append(data["title"])
ids.append(data["id"])
with open(file_path_texts, "r") as file:
for line in file:
data = json.loads(line)
texts.append(data["statement2paragraphs"])
print(f"Loaded {len(titles)} titles.")
#### VECTORIZE ####
# # Function to encode text to a vector using OpenAI's embedding API
# def encode_text_to_vector(text):
# response = openai.embeddings.create(input=text, model="text-embedding-ada-002")
# return response.data[0].embedding
# Load the tokenizer and model from Hugging Face
model_name = "sentence-transformers/all-MiniLM-L6-v2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModel.from_pretrained(model_name)
# Function to encode text to a vector using the chosen model
def encode_text_to_vector(text):
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
with torch.no_grad():
outputs = model(**inputs)
embeddings = outputs.last_hidden_state.mean(dim=1).squeeze().numpy()
return embeddings
# Vectorize documents
vectors = [encode_text_to_vector(title) for title in titles]
print(f"Embeddings created for {len(titles)} titles.")
#### FAISS INDEX ####
# Dimension of vectors
dim = len(vectors[0])
# Creating the FAISS index
index = faiss.IndexFlatL2(dim)
index.add(np.array(vectors).astype("float32")) # FAISS expects float32
print(f"FAISS index contains {index.ntotal} vectors.")
#### RETRIEVE ####
# Define a function to retrieve documents
def retrieve_documents(query, index, documents, top_k=1):
# Vectorize the query using the same approach as for the documents
query_vector = encode_text_to_vector(query)
# Search the index for the most similar document vectors
D, I = index.search(np.array([query_vector]).astype("float32"), top_k)
# Retrieve the corresponding documents
retrieved_docs = [documents[i] for i in I[0]]
return retrieved_docs
#### PROMPT ####
# Define a query
query_question = "What was the revenue for UP Fintech in the second quater of 2023?"
retrieved_title = retrieve_documents(query_question, index, titles)
print(retrieved_title)
# Find statement based on title
# Get the index of the retrieved title
retrieved_title_index = titles.index(retrieved_title[0])
retrieved_text = texts[retrieved_title_index]
# Define the prompt
system_prompt = [
{
"role": "system",
"content": "Hello, I am a financial analyst. My expertise is answering questions about financial statements.",
}
]
user_prompt = [
{
"role": "user",
"content": "Based on the financial statement below, please answer the following question: "
+ query_question
+ "\n\n"
+ retrieved_text,
}
]
conversation = system_prompt + user_prompt
# Call the OpenAI API
response = openai.chat.completions.create(
model="gpt-4-1106-preview",
messages=conversation,
)
# Extract and return the model's reply
reply = response.choices[0].message.content
print(reply)
# query_question = st.text_input("Enter your query about financial statements:")
# # Button to trigger analysis
# if st.button('Analyze'):
# if query_question:
# retrieved_title = retrieve_documents(query_question, index, titles)
# # Find statement based on title
# # Get the index of the retrieved title
# retrieved_title_index = titles.index(retrieved_title[0])
# retrieved_text = texts[retrieved_title_index]
# # Define the prompt
# system_prompt = [
# {
# "role": "system",
# "content": "Hello, I am a financial analyst. My expertise is answering questions about financial statements.",
# }
# ]
# user_prompt = [
# {
# "role": "user",
# "content": "Based on the financial statement below, please answer the following question: "
# + query_question
# + "\n\n"
# + retrieved_text,
# }
# ]
# conversation = system_prompt + user_prompt
# print(conversation)
# # Call the OpenAI API
# response = openai.chat.completions.create(
# model="gpt-4-1106-preview",
# messages=conversation,
# )
# # Extract and return the model's reply
# reply = response.choices[0].message.content
# st.write('Response:', reply)
# else:
# st.write('Please enter a query.')