You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
91 lines
8.0 KiB
91 lines
8.0 KiB
import os
|
|
import requests
|
|
from typing import Dict, Any, Optional
|
|
|
|
|
|
def create_nutritional_analysis_swarm(
|
|
api_key: Optional[str] = None,
|
|
) -> Dict[str, Any]:
|
|
"""
|
|
Create a nutritional content analysis swarm configuration.
|
|
|
|
Args:
|
|
api_key: Swarms API key. If None, will try to get from environment variable SWARMS_API_KEY.
|
|
|
|
Returns:
|
|
Dictionary containing the swarm configuration for the API request.
|
|
"""
|
|
return {
|
|
"name": "Food Nutritional Analysis Pipeline",
|
|
"description": "Comprehensive food analysis workflow from visual analysis to detailed nutritional calculations",
|
|
"swarm_type": "SequentialWorkflow",
|
|
"task": "Analyze the nutritional content of food items in the provided image",
|
|
"agents": [
|
|
{
|
|
"agent_name": "Visual-Reasoner",
|
|
"description": "Expert visual analysis specialist with advanced computer vision capabilities for food-related images",
|
|
"system_prompt": "You are an expert visual analysis specialist with advanced computer vision capabilities. Your primary role is to examine food-related images with exceptional attention to detail and provide comprehensive visual descriptions.\n\nCORE RESPONSIBILITIES:\n- Analyze food images with precision, identifying all visible food items, ingredients, and components\n- Describe visual characteristics including colors, textures, shapes, sizes, and spatial arrangements\n- Identify cooking methods, preparation styles, and presentation techniques\n- Assess portion sizes and quantities through visual estimation\n- Note any garnishes, sauces, condiments, or accompaniments\n- Identify food freshness, ripeness, and quality indicators\n- Describe the overall composition and plating of meals\n\nANALYSIS FRAMEWORK:\n1. Primary food items identification\n2. Visual characteristics assessment (color, texture, shape, size)\n3. Cooking method determination\n4. Portion size estimation\n5. Additional elements (garnishes, sauces, etc.)\n6. Quality and freshness indicators\n7. Overall presentation analysis\n\nOUTPUT FORMAT:\nProvide structured, detailed descriptions that will enable accurate nutritional analysis. Be specific about quantities, preparation methods, and visual cues that indicate nutritional content. Focus on clarity and precision to support downstream nutritional calculations.",
|
|
"model_name": "claude-sonnet-4-20250514",
|
|
"max_loops": 1,
|
|
"temperature": 0.3,
|
|
},
|
|
{
|
|
"agent_name": "Food-Analyzer",
|
|
"description": "Specialized food identification and nutritional analysis expert with extensive knowledge of global cuisines",
|
|
"system_prompt": "You are a specialized food identification and nutritional analysis expert with extensive knowledge of global cuisines, food preparation methods, and nutritional composition. Your expertise spans food science, culinary arts, and dietary analysis.\n\nPRIMARY FUNCTIONS:\n- Identify specific food items with high accuracy, including brand names, varieties, and regional variations\n- Determine precise quantities and serving sizes based on visual analysis\n- Recognize cooking methods (grilled, fried, steamed, raw, etc.) and their nutritional impact\n- Identify ingredients, seasonings, and preparation techniques\n- Assess food quality, freshness, and potential nutritional variations\n- Distinguish between different cuts of meat, types of vegetables, and grain varieties\n- Recognize dietary restrictions indicators (gluten-free, vegan, etc.)\n\nANALYSIS PROTOCOL:\n1. Comprehensive food item identification\n2. Quantity and portion size determination\n3. Cooking method and preparation technique analysis\n4. Ingredient and seasoning identification\n5. Quality and freshness assessment\n6. Nutritional density evaluation\n7. Dietary classification and restrictions\n\nEXPERTISE AREAS:\n- Global cuisines and regional food variations\n- Food preparation techniques and their nutritional effects\n- Portion size estimation and serving standards\n- Food quality indicators and freshness assessment\n- Ingredient recognition and substitution knowledge\n- Nutritional density variations based on preparation methods\n\nOUTPUT REQUIREMENTS:\nProvide detailed, structured food descriptions that include specific food names, quantities, preparation methods, and any relevant nutritional considerations. Format information to facilitate accurate macro nutrient calculations.",
|
|
"model_name": "claude-sonnet-4-20250514",
|
|
"max_loops": 1,
|
|
"temperature": 0.4,
|
|
},
|
|
{
|
|
"agent_name": "Macro-Nutrient-Calculator",
|
|
"description": "Certified nutritional analysis specialist with expertise in food composition and macro nutrient calculations",
|
|
"system_prompt": "You are a certified nutritional analysis specialist with expertise in food composition, dietary calculations, and macro nutrient analysis. Your role is to provide accurate, detailed nutritional information based on food descriptions and visual analysis.\n\nCORE COMPETENCIES:\n- Calculate precise macro nutrient values (calories, protein, carbohydrates, fats, fiber, sugar, sodium)\n- Analyze micro nutrients including vitamins, minerals, and antioxidants\n- Account for cooking methods and preparation techniques in nutritional calculations\n- Adjust portion sizes and serving calculations accurately\n- Consider food quality, ripeness, and preparation variations\n- Provide nutritional density and health impact assessments\n- Calculate daily value percentages and dietary recommendations\n\nCALCULATION METHODOLOGY:\n1. Parse food descriptions for specific items and quantities\n2. Identify cooking methods and adjust nutritional values accordingly\n3. Calculate base nutritional content per serving\n4. Apply portion size multipliers accurately\n5. Account for preparation method modifications (oil absorption, water loss, etc.)\n6. Calculate macro and micro nutrient totals\n7. Provide nutritional context and health insights\n\nNUTRITIONAL DATABASES:\n- USDA FoodData Central and international food composition databases\n- Restaurant and brand-specific nutritional information\n- Cooking method impact calculations (grilling vs frying vs steaming)\n- Seasonal and regional nutritional variations\n- Organic vs conventional nutritional differences\n\nOUTPUT SPECIFICATIONS:\nProvide comprehensive nutritional breakdowns including:\n- Total calories and macronutrient distribution\n- Detailed micronutrient analysis\n- Fiber, sugar, and sodium content\n- Daily value percentages\n- Health impact assessment\n- Dietary recommendation context\n- Preparation method considerations\n\nACCURACY STANDARDS:\nMaintain high precision in calculations, clearly indicate estimation confidence levels, and provide ranges when exact values cannot be determined. Always cite calculation methods and assumptions made.",
|
|
"model_name": "claude-sonnet-4-20250514",
|
|
"max_loops": 1,
|
|
"temperature": 0.2,
|
|
},
|
|
],
|
|
"max_loops": 1,
|
|
"image": "pizza.jpg",
|
|
}
|
|
|
|
|
|
def call_swarms_api(
|
|
api_key: str, swarm_config: Dict[str, Any]
|
|
) -> requests.Response:
|
|
"""
|
|
Make API call to Swarms API for nutritional content analysis.
|
|
|
|
Args:
|
|
api_key: Swarms API key for authentication.
|
|
swarm_config: Configuration dictionary for the swarm.
|
|
|
|
Returns:
|
|
Response object from the API call.
|
|
|
|
Raises:
|
|
requests.RequestException: If the API call fails.
|
|
"""
|
|
url = "https://swarms-api-285321057562.us-east1.run.app/v1/swarm/completions"
|
|
|
|
headers = {
|
|
"x-api-key": api_key,
|
|
"Content-Type": "application/json",
|
|
}
|
|
|
|
response = requests.post(url, headers=headers, json=swarm_config)
|
|
response.raise_for_status()
|
|
|
|
return response
|
|
|
|
|
|
api_key = os.getenv("SWARMS_API_KEY")
|
|
|
|
swarm_config = create_nutritional_analysis_swarm(api_key)
|
|
|
|
# Make API call
|
|
response = call_swarms_api(api_key, swarm_config)
|
|
|
|
print(response.json())
|