rairo commited on
Commit
d96fd41
·
verified ·
1 Parent(s): 23ffce4

Update utility.py

Browse files
Files changed (1) hide show
  1. utility.py +56 -0
utility.py CHANGED
@@ -4,6 +4,13 @@ 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()
@@ -19,6 +26,36 @@ client = openai.OpenAI(
19
  base_url="https://api.sambanova.ai/v1",
20
  )
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  # Generate AI response from user input
23
  def generateResponse(prompt,model='Meta-Llama-3.1-70B-Instruct'):
24
  #----- Call API to classify and extract relevant transaction information
@@ -218,6 +255,23 @@ def parse_multiple_transactions(response_text):
218
 
219
  return transactions
220
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221
  def create_inventory(user_phone, transaction_data):
222
  for transaction in transaction_data:
223
  item_name = transaction['details']['item'] # assumes unique item name
@@ -289,6 +343,8 @@ def delete_transaction(user_phone, transaction_id):
289
  print("Transaction deleted successfully!")
290
 
291
 
 
 
292
  # Example usage
293
  # response_text = """
294
  # The information provided indicates that you want to **create/record** a new transaction.
 
4
  import openai
5
  from google.cloud import firestore
6
  from dotenv import load_dotenv
7
+ from pandasai import SmartDatalake
8
+ from pandasai import Agent
9
+ from pandasai.responses.response_parser import ResponseParser
10
+ from langchain_experimental.agents import create_pandas_dataframe_agent
11
+ import pandas as pd
12
+ from pandasai.llm import OpenAI
13
+
14
 
15
  # Make API connection
16
  load_dotenv()
 
26
  base_url="https://api.sambanova.ai/v1",
27
  )
28
 
29
+ llm = OpenAI(
30
+ api_key=os.environ.get("sambanova_api_key"),
31
+ base_url="https://api.sambanova.ai/v1",
32
+ )
33
+
34
+
35
+ class FlaskResponse(ResponseParser):
36
+ def __init__(self, context) -> None:
37
+ super().__init__(context)
38
+
39
+ def format_dataframe(self, result):
40
+ return result['value'].to_html()
41
+
42
+ def format_plot(self, result):
43
+ # Save the plot using savefig
44
+ try:
45
+
46
+ img_path = result['value']
47
+
48
+
49
+ except ValueError:
50
+ img_path = str(result['value'])
51
+ print("value error!", img_path)
52
+
53
+ print("response_class_path:", img_path)
54
+ return img_path
55
+
56
+ def format_other(self, result):
57
+ return str(result['value'])
58
+
59
  # Generate AI response from user input
60
  def generateResponse(prompt,model='Meta-Llama-3.1-70B-Instruct'):
61
  #----- Call API to classify and extract relevant transaction information
 
255
 
256
  return transactions
257
 
258
+ def read_datalake(user_phone, user_question):
259
+ inventory_ref = db.collection("users").document(user_phone).collection("inventory")
260
+
261
+ sales_ref = db.collection("users").document(user_phone).collection('sales')
262
+
263
+ inventory_list = [doc.to_dict() for doc in inventory_ref.stream()]
264
+ sales_list = [doc.to_dict() for doc in sales_ref.stream()]
265
+
266
+ inventory_df = pd.DataFrame(inventory_list)
267
+ sales_df = pd.DataFrame(sales_list)
268
+
269
+
270
+ lake = SmartDatalake([inventory_df, sales_df], config={"llm": llm, "response_parser": FlaskResponse, "enable_cache": False, "save_logs": False})
271
+ response = lake.chat(user_question)
272
+
273
+ return response
274
+
275
  def create_inventory(user_phone, transaction_data):
276
  for transaction in transaction_data:
277
  item_name = transaction['details']['item'] # assumes unique item name
 
343
  print("Transaction deleted successfully!")
344
 
345
 
346
+
347
+
348
  # Example usage
349
  # response_text = """
350
  # The information provided indicates that you want to **create/record** a new transaction.