williamagyapong commited on
Commit
d813e31
·
verified ·
1 Parent(s): 82f5b03

Create utility.py

Browse files
Files changed (1) hide show
  1. utility.py +220 -0
utility.py ADDED
@@ -0,0 +1,220 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ import os
3
+ from datetime import datetime
4
+ import openai
5
+ from google.cloud import firestore
6
+ from dotenv import load_dotenv
7
+
8
+ # Make API connection
9
+ load_dotenv()
10
+ # gemini_api_key = os.environ['Gemini']
11
+ o_api_key = os.getenv("openai_api_key")
12
+ openai.api_key = o_api_key
13
+
14
+ # Authenticate to Firesotre with the JSON account key
15
+ db = firestore.Client.from_service_account_json("firestore-key.json")
16
+
17
+ # Generate AI response from user input
18
+ def generateResponse(prompt):
19
+ #----- Call API to classify and extract relevant transaction information
20
+ # These templates help provide a unified response format for use as context clues when
21
+ # parsing the AI generated response into a structured data format
22
+ relevant_info_template = """
23
+ Intent: The CRUD operation
24
+ Transaction Type: The type of transaction
25
+ Details: as a sublist of the key details like name of item, amount, description, among other details you are able to extract.
26
+ """
27
+ sample_response_template = """
28
+ The information provided indicates that you want to **create/record** a new transaction.
29
+
30
+ **Extracted Information**:
31
+
32
+ **Intent**: Create
33
+
34
+ Transaction 1:
35
+
36
+ **Transaction Type**: Purchase
37
+ **Details**:
38
+ - Item: Car
39
+ - Purpose: Business
40
+ - Amount: 1000
41
+ - Tax: 200
42
+ - Note: A new car for business
43
+
44
+ Transaction 2:
45
+
46
+ **Transaction Type**: Expense
47
+ **Details**:
48
+ - Item: Office Chair
49
+ - Amount: 300 USD
50
+ - Category: Furniture
51
+ """
52
+ response = openai.chat.completions.create(
53
+ model="gpt-4o",
54
+ messages=[
55
+ {"role": "system", "content": f"You are a helpful assistant that classifies transactions written in natural language into CRUD operations (Create, Read, Update, and Delete) and extracts relevant information. Format the relevant information extracted from the transaction text in this format: {relevant_info_template}. You can use markdown syntax to present a nicely formated and readable response to the user, but make sure the user does not see the markdown keyword. Keywords and field names must be in bold face. A sample response could look like this: {sample_response_template}. Delineate multiple transactions with the label 'Transaction 1' before the start of the relevant information for each transaction. There should be only one intent even in the case of multiple transactions."},
56
+ {"role": "user", "content": prompt}
57
+ ]
58
+ )
59
+ #----- Process response
60
+ try:
61
+ response = response.choices[0].message.content
62
+ except Exception as e:
63
+ print(f'An error occurred: {str(e)}')
64
+ response = None
65
+
66
+ return response
67
+
68
+ # TODO Handle multiple multiple transactions from one user input.
69
+ def parse_ai_response(response_text):
70
+ # Initialize the structured data dictionary
71
+ data = {
72
+ "intent": None,
73
+ "transaction_type": None,
74
+ "details": {},
75
+ "created_at": datetime.now().isoformat() # Add current date and time
76
+ }
77
+
78
+ # Extract the intent
79
+ intent_match = re.search(r"\*\*Intent\*\*:\s*(\w+)", response_text)
80
+ if intent_match:
81
+ data["intent"] = intent_match.group(1)
82
+
83
+ # Extract the transaction type
84
+ transaction_type_match = re.search(r"\*\*Transaction Type\*\*:\s*(\w+)", response_text)
85
+ if transaction_type_match:
86
+ data["transaction_type"] = transaction_type_match.group(1)
87
+
88
+ # Extract details
89
+ details = {}
90
+ detail_matches = re.findall(r"-\s*([\w\s]+):\s*([\d\w\s,.]+(?:\sUSD)?)", response_text)
91
+ for field, value in detail_matches:
92
+ # Clean up the field name and value
93
+ field = field.strip().lower().replace(" ", "_") # Convert to snake_case
94
+ value = value.strip()
95
+
96
+ # Convert numeric values where possible
97
+ if "USD" in value:
98
+ value = float(value.replace(" USD", "").replace(",", ""))
99
+ elif value.isdigit():
100
+ value = int(value)
101
+
102
+ details[field] = value
103
+
104
+ # Store details in the structured data
105
+ data["details"] = details
106
+
107
+ return data
108
+
109
+ def parse_multiple_transactions(response_text):
110
+ transactions = []
111
+ # Split the response into transaction sections based on keyword 'Transaction #'
112
+ transaction_sections = re.split(r"Transaction \d+:", response_text, flags=re.IGNORECASE)
113
+ # Adjust regex to handle variations in "Transaction X:"
114
+ # transaction_sections = re.split(r"(?i)(?<=\n)transaction\s+\d+:", response_text)
115
+ transaction_sections = [section.strip() for section in transaction_sections if section.strip()]
116
+ # Remove the first section if it's not a valid transaction
117
+ if not re.search(r"\*\*Transaction Type\*\*", transaction_sections[0], re.IGNORECASE):
118
+ transaction_sections.pop(0)
119
+ print(len(transaction_sections))
120
+ print(transaction_sections)
121
+ # Extract intent: with support for a single intent per user prompt
122
+ intent_match = re.search(r"\*\*Intent\*\*:\s*(\w+)", response_text)
123
+ if intent_match:
124
+ intent = intent_match.group(1)
125
+
126
+ for section in transaction_sections:
127
+ # Initialize transaction data
128
+ transaction_data = {
129
+ "intent": intent, # global intent
130
+ "transaction_type": None,
131
+ "details": {},
132
+ "created_at": datetime.now().isoformat()
133
+ }
134
+ # transaction_data["intent"] = intent
135
+
136
+ # Extract transaction type
137
+ transaction_type_match = re.search(r"\*\*Transaction Type\*\*:\s*(\w+)", section)
138
+ if transaction_type_match:
139
+ transaction_data["transaction_type"] = transaction_type_match.group(1)
140
+
141
+ # Extract details
142
+ details = {}
143
+ detail_matches = re.findall(r"-\s*([\w\s]+):\s*([\d\w\s,.]+(?:\sUSD)?)", section)
144
+ for field, value in detail_matches:
145
+ field = field.strip().lower().replace(" ", "_") # Convert to snake_case
146
+ value = value.strip()
147
+ # Convert numeric values where possible
148
+ if "USD" in value:
149
+ value = float(value.replace(" USD", "").replace(",", ""))
150
+ elif value.isdigit():
151
+ value = int(value)
152
+ details[field] = value
153
+
154
+ transaction_data["details"] = details
155
+ transactions.append(transaction_data)
156
+
157
+ return transactions
158
+
159
+ def create_transaction(user_phone, transaction_data):
160
+ for transaction in transaction_data:
161
+ doc_ref = db.collection("users").document(user_phone).collection("transactions").document()
162
+ # transaction_data['transaction_id'] # let's default to random generated document ids
163
+ doc_ref.set(transaction)
164
+ # print("Transaction created successfully!")
165
+ return True
166
+
167
+ # Update logic:
168
+ # -
169
+ def update_transaction(user_phone, transaction_id, update_data):
170
+ doc_ref = db.collection("users").document(user_phone).collection("transactions").document(transaction_id)
171
+ doc_ref.update(update_data)
172
+ # print("Transaction updated successfully!")
173
+ return True
174
+
175
+ def fetch_transaction(user_phone, transaction_id=None):
176
+ if transaction_id:
177
+ doc_ref = db.collection("users").document(user_phone).collection("transactions").document(transaction_id)
178
+ transaction = doc_ref.get()
179
+ if transaction.exists:
180
+ return transaction.to_dict()
181
+ else:
182
+ # print("Transaction not found.")
183
+ return None
184
+ else:
185
+ collection_ref = db.collection("users").document(user_phone).collection("transactions")
186
+ transactions = [doc.to_dict() for doc in collection_ref.stream()]
187
+ return transactions
188
+
189
+ # Delete fields or an entire transaction document.
190
+ # Delete specific fields
191
+ def delete_transaction_fields(user_phone, transaction_id, fields_to_delete):
192
+ doc_ref = db.collection("users").document(user_phone).collection("transactions").document(transaction_id)
193
+ updates = {field: firestore.DELETE_FIELD for field in fields_to_delete}
194
+ doc_ref.update(updates)
195
+ print("Fields deleted successfully!")
196
+
197
+ # Delete an entire transaction
198
+ def delete_transaction(user_phone, transaction_id):
199
+ doc_ref = db.collection("users").document(user_phone).collection("transactions").document(transaction_id)
200
+ doc_ref.delete()
201
+ print("Transaction deleted successfully!")
202
+
203
+
204
+ # Example usage
205
+ # response_text = """
206
+ # The information provided indicates that you want to **create/record** a new transaction.
207
+
208
+ # **Extracted Information**:
209
+
210
+ # **Intent**: Create
211
+ # **Transaction Type**: Purchase
212
+ # **Details**:
213
+ # - Item: Car
214
+ # - Purpose: Business
215
+ # - Amount: 100000 USD
216
+ # - Tax: 1000 USD
217
+ # """
218
+
219
+ # parsed_data = parse_ai_response(response_text)
220
+ # print(parsed_data)