williamagyapong rairo commited on
Commit
630a5df
·
verified ·
1 Parent(s): 4571e0f

Update utility.py (#3)

Browse files

- Update utility.py (f02a873dd316beda860aaedc9efaf823adaebf19)


Co-authored-by: Raphael M <[email protected]>

Files changed (1) hide show
  1. utility.py +71 -0
utility.py CHANGED
@@ -4,6 +4,15 @@ 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 +28,49 @@ 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 +270,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 +358,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
+ import pandas as pd
11
+ from pandasai.llm import OpenAI
12
+ #from langchain.llms.sambanova import Sambaverse
13
+ #from langchain_community.llms.sambanova import Sambaverse
14
+ from langchain_community.chat_models.sambanova import ChatSambaNovaCloud
15
+
16
 
17
  # Make API connection
18
  load_dotenv()
 
28
  base_url="https://api.sambanova.ai/v1",
29
  )
30
 
31
+
32
+
33
+
34
+ sambaverse_api_key = os.environ.get("SAMBAVERSE_API_KEY")
35
+
36
+
37
+ llm = ChatSambaNovaCloud(
38
+ model="Meta-Llama-3.1-70B-Instruct",
39
+ max_tokens=1024,
40
+ temperature=0.7,
41
+ top_k=1,
42
+ top_p=0.01,
43
+ )
44
+
45
+
46
+ print(llm.invoke("Why should I use open source models?"))
47
+
48
+
49
+
50
+ class FlaskResponse(ResponseParser):
51
+ def __init__(self, context) -> None:
52
+ super().__init__(context)
53
+
54
+ def format_dataframe(self, result):
55
+ return result['value'].to_html()
56
+
57
+ def format_plot(self, result):
58
+ # Save the plot using savefig
59
+ try:
60
+
61
+ img_path = result['value']
62
+
63
+
64
+ except ValueError:
65
+ img_path = str(result['value'])
66
+ print("value error!", img_path)
67
+
68
+ print("response_class_path:", img_path)
69
+ return img_path
70
+
71
+ def format_other(self, result):
72
+ return str(result['value'])
73
+
74
  # Generate AI response from user input
75
  def generateResponse(prompt,model='Meta-Llama-3.1-70B-Instruct'):
76
  #----- Call API to classify and extract relevant transaction information
 
270
 
271
  return transactions
272
 
273
+ def read_datalake(user_phone, user_question):
274
+ inventory_ref = db.collection("users").document(user_phone).collection("inventory")
275
+
276
+ sales_ref = db.collection("users").document(user_phone).collection('sales')
277
+
278
+ inventory_list = [doc.to_dict() for doc in inventory_ref.stream()]
279
+ sales_list = [doc.to_dict() for doc in sales_ref.stream()]
280
+
281
+ inventory_df = pd.DataFrame(inventory_list)
282
+ sales_df = pd.DataFrame(sales_list)
283
+
284
+
285
+ lake = SmartDatalake([inventory_df, sales_df], config={"llm": llm, "response_parser": FlaskResponse, "enable_cache": False, "save_logs": False})
286
+ response = lake.chat(user_question)
287
+
288
+ return response
289
+
290
  def create_inventory(user_phone, transaction_data):
291
  for transaction in transaction_data:
292
  item_name = transaction['details']['item'] # assumes unique item name
 
358
  print("Transaction deleted successfully!")
359
 
360
 
361
+
362
+
363
  # Example usage
364
  # response_text = """
365
  # The information provided indicates that you want to **create/record** a new transaction.