Just a note I am just playing I want to do a video on functions, but I also have a project im working on that I would really like to use them for but it will require that i figure out how to get them to reliably pick up.
Let me know what you think maybe i’m doing something wrong. Could we maybe get a description on the properties so that we can describe what they are so the ai could pick it up if a user uses different terms. Or should we maybe have more then one proptery so if the user says
product, item, shoe
My code
App.py
from dotenv import load_dotenv
import google.generativeai as genai
import google.ai.generativelanguage as glm
import os
from data import get_products_data
load_dotenv()
# name of the AI model used in this call.
CHAT_MODEL_NAME = os.getenv("CHAT_MODEL_NAME")
def get_prompt(message: str, file: str) -> str:
# Open the file in read mode
with open(file, 'r') as file:
# Read the entire contents of the file
file_contents = file.read()
return file_contents.replace("{User's Question}", message)
class GeminiService:
generation_config: str = {
'temperature': 0.9,
'top_p': 1,
'top_k': 40,
'max_output_tokens': 2048,
'stop_sequences': [],
}
safety_settings: list[str] = [{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"},
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"}]
def __init__(self):
genai.configure(api_key=os.getenv("API_KEY"))
check_inventory = glm.Tool(
function_declarations=[
glm.FunctionDeclaration(
name='check_inventory',
description="Function for checking inventory",
parameters=glm.Schema(
type=glm.Type.OBJECT,
properties={
'product': glm.Schema(type=glm.Type.STRING),
'query': glm.Schema(type=glm.Type.STRING)
},
required=['product', 'query']
)
)
]
)
get_info = glm.Tool(
function_declarations=[
glm.FunctionDeclaration(
name='get_product_price_description_and_information',
description="A tool used to get information about an items, description or price based upon its name",
parameters=glm.Schema(
type=glm.Type.OBJECT,
properties={
'product': glm.Schema(type=glm.Type.STRING),
'query': glm.Schema(type=glm.Type.STRING)
},
required=['product', 'query']
)
)
]
)
self.model = genai.GenerativeModel(model_name=CHAT_MODEL_NAME,
tools=[get_info, check_inventory],
generation_config=self.generation_config,
safety_settings=self.safety_settings
)
self.model2 = genai.GenerativeModel(model_name=CHAT_MODEL_NAME,
generation_config=self.generation_config,
safety_settings=self.safety_settings)
def single_completion(self,
message: str) -> str:
response = self.model.generate_content(message)
# if the response is a detected function call.
if response.candidates[0].content.parts[0].function_call:
response_function_call_content = response.candidates[0].content
fc = response.candidates[0].content.parts[0].function_call
if fc.name == 'check_inventory':
query = response.candidates[0].content.parts[0].function_call.args["query"]
product_name = response.candidates[0].content.parts[0].function_call.args["product"]
product_id = get_products_data("name", product_name, "supplier_id")
product_description = get_products_data("name", product_name, "description")
product_price = get_products_data("name", product_name, "price")
prompt = get_prompt(query, "prompt_formula.txt")
formula = self.model2.generate_content(prompt)
return formula.text
elif fc.name == 'get_product_price_description_and_information':
print("get_product_price_description_and_information")
query = response.candidates[0].content.parts[0].function_call.args["query"]
product_name = response.candidates[0].content.parts[0].function_call.args["product"]
product_id = get_products_data("name", product_name, "supplier_id")
product_description = get_products_data("name", product_name, "description")
product_price = get_products_data("name", product_name, "price")
prompt = get_prompt(query, "prompt_formula.txt")
formula = self.model2.generate_content(prompt)
return formula.text
elif fc.name == 'fallback':
response = self.model2.generate_content(message)
return response.text
else:
return f"Unsupported function: {fc.name}"
# probably a text response.
return response.text
class GeminiUI:
def __init__(
self
) -> None:
self.service = GeminiService()
def single_answer(self, message: str) -> str:
prompt = get_prompt(message, "prompt.txt")
return self.service.single_completion(message=prompt)
gemini = GeminiUI()
answer = gemini.single_answer("Who is James T. Kirk")
print(f"answer: {answer}")
Data.py
suppliers_data = [
{"name": "Samsung Electronics", "address": "Seoul, South Korea", "contact": "800-726-7864"},
{"name": "Apple Inc.", "address": "Cupertino, California, USA", "contact": "800–692–7753"},
{"name": "OnePlus Technology", "address": "Shenzhen, Guangdong, China", "contact": "400-888-1111"},
{"name": "Google LLC", "address": "Mountain View, California, USA", "contact": "855-836-3987"},
{"name": "Xiaomi Corporation", "address": "Beijing, China", "contact": "1800-103-6286"},
]
products_data = [
{"name": "Samsung Galaxy S21", "description": "Samsung flagship smartphone", "price": 799.99, "supplier_id": 1},
{"name": "Samsung Galaxy Note 20", "description": "Samsung premium smartphone with stylus", "price": 999.99,
"supplier_id": 1},
{"name": "iPhone 13 Pro", "description": "Apple flagship smartphone", "price": 999.99, "supplier_id": 2},
{"name": "iPhone SE", "description": "Apple budget smartphone", "price": 399.99, "supplier_id": 2},
{"name": "OnePlus 9", "description": "High performance smartphone", "price": 729.00, "supplier_id": 3},
{"name": "OnePlus Nord", "description": "Mid-range smartphone", "price": 499.00, "supplier_id": 3},
{"name": "Google Pixel 6", "description": "Google's latest smartphone", "price": 599.00, "supplier_id": 4},
{"name": "Google Pixel 5a", "description": "Affordable Google smartphone", "price": 449.00, "supplier_id": 4},
{"name": "Xiaomi Mi 11", "description": "Xiaomi high-end smartphone", "price": 749.99, "supplier_id": 5},
{"name": "Xiaomi Redmi Note 10", "description": "Xiaomi budget smartphone", "price": 199.99, "supplier_id": 5},
]
def get_products_data(name: str, equals: str, respond: str):
for product in products_data:
if product[name] == equals:
return product[respond]
return None # Return None if the product name is not found
inventory_data = [
{"product_id": 1, "quantity": 150, "min_required": 30},
{"product_id": 2, "quantity": 100, "min_required": 20},
{"product_id": 3, "quantity": 120, "min_required": 30},
{"product_id": 4, "quantity": 80, "min_required": 15},
{"product_id": 5, "quantity": 200, "min_required": 40},
{"product_id": 6, "quantity": 150, "min_required": 25},
{"product_id": 7, "quantity": 100, "min_required": 20},
{"product_id": 8, "quantity": 90, "min_required": 18},
{"product_id": 9, "quantity": 170, "min_required": 35},
{"product_id": 10, "quantity": 220, "min_required": 45}]
.env
API_KEY=[redacted]
CHAT_MODEL_NAME=gemini-pro