@ -11,12 +11,16 @@ openai_api_key = os.getenv("OPENAI_API_KEY")
# Define prompts for various tasks
# Define prompts for various tasks
MEAL_PLAN_PROMPT = " Based on the following user preferences: dietary restrictions as vegetarian, preferred cuisines as Italian and Indian, a total caloric intake of around 2000 calories per day, and an exclusion of legumes, create a detailed weekly meal plan. Include a variety of meals for breakfast, lunch, dinner, and optional snacks. "
MEAL_PLAN_PROMPT = " Based on the following user preferences: dietary restrictions as vegetarian, preferred cuisines as Italian and Indian, a total caloric intake of around 2000 calories per day, and an exclusion of legumes, create a detailed weekly meal plan. Include a variety of meals for breakfast, lunch, dinner, and optional snacks. "
IMAGE_ANALYSIS_PROMPT = " Identify the items in this fridge, including their quantities and condition. "
IMAGE_ANALYSIS_PROMPT = (
" Identify the items in this fridge, including their quantities and condition. "
)
# Function to encode image to base64
# Function to encode image to base64
def encode_image ( image_path ) :
def encode_image ( image_path ) :
with open ( image_path , " rb " ) as image_file :
with open ( image_path , " rb " ) as image_file :
return base64 . b64encode ( image_file . read ( ) ) . decode ( ' utf-8 ' )
return base64 . b64encode ( image_file . read ( ) ) . decode ( " utf-8 " )
# Initialize Language Model (LLM)
# Initialize Language Model (LLM)
llm = OpenAIChat (
llm = OpenAIChat (
@ -24,12 +28,13 @@ llm = OpenAIChat(
max_tokens = 3000 ,
max_tokens = 3000 ,
)
)
# Function to handle vision tasks
# Function to handle vision tasks
def create_vision_agent ( image_path ) :
def create_vision_agent ( image_path ) :
base64_image = encode_image ( image_path )
base64_image = encode_image ( image_path )
headers = {
headers = {
" Content-Type " : " application/json " ,
" Content-Type " : " application/json " ,
" Authorization " : f " Bearer { openai_api_key } "
" Authorization " : f " Bearer { openai_api_key } " ,
}
}
payload = {
payload = {
" model " : " gpt-4-vision-preview " ,
" model " : " gpt-4-vision-preview " ,
@ -38,28 +43,39 @@ def create_vision_agent(image_path):
" role " : " user " ,
" role " : " user " ,
" content " : [
" content " : [
{ " type " : " text " , " text " : IMAGE_ANALYSIS_PROMPT } ,
{ " type " : " text " , " text " : IMAGE_ANALYSIS_PROMPT } ,
{ " type " : " image_url " , " image_url " : { " url " : f " data:image/jpeg;base64, { base64_image } " } }
{
]
" type " : " image_url " ,
" image_url " : { " url " : f " data:image/jpeg;base64, { base64_image } " } ,
} ,
] ,
}
}
] ,
] ,
" max_tokens " : 300
" max_tokens " : 300 ,
}
}
response = requests . post ( " https://api.openai.com/v1/chat/completions " , headers = headers , json = payload )
response = requests . post (
" https://api.openai.com/v1/chat/completions " , headers = headers , json = payload
)
return response . json ( )
return response . json ( )
# Function to generate an integrated shopping list considering meal plan and fridge contents
# Function to generate an integrated shopping list considering meal plan and fridge contents
def generate_integrated_shopping_list ( meal_plan_output , image_analysis , user_preferences ) :
def generate_integrated_shopping_list (
meal_plan_output , image_analysis , user_preferences
) :
# Prepare the prompt for the LLM
# Prepare the prompt for the LLM
fridge_contents = image_analysis [ ' choices ' ] [ 0 ] [ ' message ' ] [ ' content ' ]
fridge_contents = image_analysis [ " choices " ] [ 0 ] [ " message " ] [ " content " ]
prompt = ( f " Based on this meal plan: { meal_plan_output } , "
prompt = (
f " Based on this meal plan: { meal_plan_output } , "
f " and the following items in the fridge: { fridge_contents } , "
f " and the following items in the fridge: { fridge_contents } , "
f " considering dietary preferences as vegetarian with a preference for Italian and Indian cuisines, "
f " considering dietary preferences as vegetarian with a preference for Italian and Indian cuisines, "
f " generate a comprehensive shopping list that includes only the items needed. " )
f " generate a comprehensive shopping list that includes only the items needed. "
)
# Send the prompt to the LLM and return the response
# Send the prompt to the LLM and return the response
response = llm ( prompt )
response = llm ( prompt )
return response # assuming the response is a string
return response # assuming the response is a string
# Define agent for meal planning
# Define agent for meal planning
meal_plan_agent = Flow (
meal_plan_agent = Flow (
llm = llm ,
llm = llm ,
@ -74,19 +90,19 @@ user_preferences = {
" dietary_restrictions " : " vegetarian " ,
" dietary_restrictions " : " vegetarian " ,
" preferred_cuisines " : [ " Italian " , " Indian " ] ,
" preferred_cuisines " : [ " Italian " , " Indian " ] ,
" caloric_intake " : 2000 ,
" caloric_intake " : 2000 ,
" other notes " : " Doesn ' t eat legumes "
" other notes " : " Doesn ' t eat legumes " ,
}
}
# Generate Meal Plan
# Generate Meal Plan
meal_plan_output = meal_plan_agent . run (
meal_plan_output = meal_plan_agent . run ( f " Generate a meal plan: { user_preferences } " )
f " Generate a meal plan: { user_preferences } "
)
# Vision Agent - Analyze an Image
# Vision Agent - Analyze an Image
image_analysis_output = create_vision_agent ( " full_fridge.jpg " )
image_analysis_output = create_vision_agent ( " full_fridge.jpg " )
# Generate Integrated Shopping List
# Generate Integrated Shopping List
integrated_shopping_list = generate_integrated_shopping_list ( meal_plan_output , image_analysis_output , user_preferences )
integrated_shopping_list = generate_integrated_shopping_list (
meal_plan_output , image_analysis_output , user_preferences
)
# Print and save the outputs
# Print and save the outputs
print ( " Meal Plan: " , meal_plan_output )
print ( " Meal Plan: " , meal_plan_output )