Update utility.py
Browse files- utility.py +31 -26
utility.py
CHANGED
|
@@ -7,18 +7,19 @@ from dotenv import load_dotenv
|
|
| 7 |
import pandas as pd
|
| 8 |
import ast
|
| 9 |
import json
|
| 10 |
-
from pandasai.responses.response_parser import
|
| 11 |
# Load environment variables and configure Firestore
|
| 12 |
load_dotenv()
|
| 13 |
db = firestore.Client.from_service_account_json("firestore-key.json")
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
max_tokens=1024,
|
| 21 |
-
temperature=0.7,
|
| 22 |
top_k=1,
|
| 23 |
top_p=0.01,
|
| 24 |
)
|
|
@@ -32,12 +33,9 @@ class FlaskResponse(ResponseParser):
|
|
| 32 |
return result['value'].to_html()
|
| 33 |
|
| 34 |
def format_plot(self, result):
|
| 35 |
-
|
| 36 |
try:
|
| 37 |
-
|
| 38 |
img_path = result['value']
|
| 39 |
-
|
| 40 |
-
|
| 41 |
except ValueError:
|
| 42 |
img_path = str(result['value'])
|
| 43 |
print("value error!", img_path)
|
|
@@ -48,7 +46,7 @@ class FlaskResponse(ResponseParser):
|
|
| 48 |
def format_other(self, result):
|
| 49 |
return str(result['value'])
|
| 50 |
|
| 51 |
-
def generateResponse(prompt, model='
|
| 52 |
# Templates for extracting transaction information
|
| 53 |
relevant_info_template = """
|
| 54 |
Intent: The CRUD operation (create, read, update, delete)
|
|
@@ -69,24 +67,31 @@ Transaction 2:
|
|
| 69 |
*Transaction Type*: Sale
|
| 70 |
*Details*: - Item: Chair, - Quantity: 2, etc.
|
| 71 |
"""
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
)
|
|
|
|
|
|
|
|
|
|
| 85 |
try:
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
| 87 |
except Exception as e:
|
| 88 |
print(f'An error occurred: {str(e)}')
|
| 89 |
response_text = None
|
|
|
|
| 90 |
return response_text
|
| 91 |
|
| 92 |
def parse_value(value):
|
|
|
|
| 7 |
import pandas as pd
|
| 8 |
import ast
|
| 9 |
import json
|
| 10 |
+
from pandasai.responses.response_parser import ResponseParser
|
| 11 |
# Load environment variables and configure Firestore
|
| 12 |
load_dotenv()
|
| 13 |
db = firestore.Client.from_service_account_json("firestore-key.json")
|
| 14 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 15 |
+
# Initialize the Gemini LLM client
|
| 16 |
+
import google.generativeai as genai
|
| 17 |
|
| 18 |
+
# Replace SambaNova with Google Gemini
|
| 19 |
+
llm = ChatGoogleGenerativeAI(
|
| 20 |
+
model="gemini-2.0-flash-thinking-exp",
|
| 21 |
+
max_output_tokens=1024,
|
| 22 |
+
temperature=0.01,
|
|
|
|
|
|
|
| 23 |
top_k=1,
|
| 24 |
top_p=0.01,
|
| 25 |
)
|
|
|
|
| 33 |
return result['value'].to_html()
|
| 34 |
|
| 35 |
def format_plot(self, result):
|
| 36 |
+
# Save the plot using savefig
|
| 37 |
try:
|
|
|
|
| 38 |
img_path = result['value']
|
|
|
|
|
|
|
| 39 |
except ValueError:
|
| 40 |
img_path = str(result['value'])
|
| 41 |
print("value error!", img_path)
|
|
|
|
| 46 |
def format_other(self, result):
|
| 47 |
return str(result['value'])
|
| 48 |
|
| 49 |
+
def generateResponse(prompt, model='gemini-2.0-flash-thinking-exp'):
|
| 50 |
# Templates for extracting transaction information
|
| 51 |
relevant_info_template = """
|
| 52 |
Intent: The CRUD operation (create, read, update, delete)
|
|
|
|
| 67 |
*Transaction Type*: Sale
|
| 68 |
*Details*: - Item: Chair, - Quantity: 2, etc.
|
| 69 |
"""
|
| 70 |
+
|
| 71 |
+
# Replace SambaNova with Google Gemini API
|
| 72 |
+
genai.configure(api_key=os.environ.get("GOOGLE_API_KEY"))
|
| 73 |
+
|
| 74 |
+
model_instance = genai.GenerativeModel(
|
| 75 |
+
model_name=model,
|
| 76 |
+
generation_config={
|
| 77 |
+
"temperature": 0.1,
|
| 78 |
+
"top_p": 0.01,
|
| 79 |
+
"top_k": 1,
|
| 80 |
+
"max_output_tokens": 1024,
|
| 81 |
+
}
|
| 82 |
)
|
| 83 |
+
|
| 84 |
+
system_prompt = f"You are a helpful assistant that classifies transactions. Format your output with these guidelines: {relevant_info_template} Sample single transaction: {sample_single_transaction_template} Sample multi-transaction: {sample_multi_transaction_template}"
|
| 85 |
+
|
| 86 |
try:
|
| 87 |
+
response = model_instance.generate_content([
|
| 88 |
+
{"role": "user", "parts": [system_prompt, prompt]}
|
| 89 |
+
])
|
| 90 |
+
response_text = response.text
|
| 91 |
except Exception as e:
|
| 92 |
print(f'An error occurred: {str(e)}')
|
| 93 |
response_text = None
|
| 94 |
+
|
| 95 |
return response_text
|
| 96 |
|
| 97 |
def parse_value(value):
|