Python scripts to interact with various AI models, supporting both text and image inputs.
- Azure OpenAI: GPT-4 Vision model
- Google Vertex AI: Gemini models with multimodal capabilities
- Send text messages to AI models
- Analyze images with vision-capable models
- Support for multiple image formats (JPG, PNG, GIF, WebP)
- Interactive command-line interface
- Secure environment variable configuration
- Support for multimodal inputs (text + multiple images)
- Python 3.8+
- Access to either:
- Azure OpenAI resource with GPT-4 Vision model deployed
- Google Cloud Project with Vertex AI API enabled
- Required Python packages listed in
requirements.txt
pip install -r requirements.txt
Create a .env
file in the project directory:
# Copy the example file
cp .env.example .env
Edit the .env
file with your actual values for the platform(s) you want to use:
AZURE_OPENAI_ENDPOINT=https://your-resource.openai.azure.com
AZURE_OPENAI_API_KEY=your_api_key_from_azure_portal
AZURE_OPENAI_DEPLOYMENT_NAME=your_gpt4_deployment_name
How to get Azure OpenAI values:
- Go to Azure Portal
- Navigate to your Azure OpenAI resource
- Go to "Keys and Endpoint" section
- Copy the endpoint URL and API key
- Deployment name is the name you gave to your GPT-4 model deployment
GOOGLE_CLOUD_PROJECT=your-project-id
GOOGLE_APPLICATION_CREDENTIALS=path/to/your/service-account-key.json
VERTEX_AI_LOCATION=us-central1
VERTEX_AI_MODEL=gemini-1.5-pro
How to get Vertex AI values:
- Go to Google Cloud Console
- Create or select a project
- Enable the Vertex AI API
- Create a service account with Vertex AI permissions
- Download the service account key JSON file
- Set the path to the JSON file in
GOOGLE_APPLICATION_CREDENTIALS
For GDPR Compliance:
- Use EU regions like
europe-west1
(Belgium),europe-west3
(Germany), oreurope-west4
(Netherlands) - See
vertex-gdpr-setup-guide.md
for detailed GDPR configuration instructions
Run the Azure OpenAI script:
python azureFoundry.py
Choose from the menu:
- Option 1: Send text message to GPT-4
- Option 2: Send image with text prompt to GPT-4 Vision
- Option 3: Exit
Run the Vertex AI script:
python vertex.py
Choose from the menu:
- Option 1: Send text message to Gemini
- Option 2: Send image with text prompt to Gemini Vision
- Option 3: Send multimodal message (text + multiple images)
- Option 4: Exit
You can also import and use the functions directly:
from azureFoundry import send_text_message, send_image_message
# Send text message
response = send_text_message("Explain quantum computing in simple terms")
print(response)
# Analyze image
response = send_image_message("path/to/image.jpg", "What objects do you see in this image?")
print(response)
from vertex import send_text_message, send_image_message, send_multimodal_message
# Send text message
response = send_text_message("Explain quantum computing in simple terms")
print(response)
# Analyze image
response = send_image_message("path/to/image.jpg", "What objects do you see in this image?")
print(response)
# Send text with multiple images
response = send_multimodal_message("Compare these images", ["image1.jpg", "image2.jpg"])
print(response)
- Send a text message to GPT-4
- Returns the assistant's response as a string
send_image_message(image_path, text_prompt="What do you see in this image?", system_prompt="You are a helpful assistant that can analyze images.")
- Send an image with optional text prompt to GPT-4 Vision
- Supports JPG, PNG, GIF, WebP formats
- Returns the assistant's response as a string
- Low-level function to call Azure OpenAI API directly
- Takes OpenAI-compatible message format
- JPEG (.jpg, .jpeg)
- PNG (.png)
- GIF (.gif)
- WebP (.webp)
The script includes comprehensive error handling for:
- Missing environment variables
- File not found errors
- HTTP errors from Azure OpenAI API
- JSON parsing errors
- Network timeouts
- Never commit API keys to version control
- Use environment variables or secure key management systems
- The script uses HTTPS for all API communications
- Image data is base64 encoded for secure transmission
-
"Environment variable not set" error
- Ensure all required environment variables are set correctly
- Check for typos in variable names
-
"HTTP 401 Unauthorized" error
- Verify your API key is correct
- Check that your Azure OpenAI resource is active
-
"HTTP 404 Not Found" error
- Verify your endpoint URL is correct
- Check that your deployment name matches exactly
-
"File not found" error
- Ensure the image path is correct and accessible
- Use absolute paths if having issues with relative paths
Azure OpenAI has rate limits. If you encounter 429 errors, wait and retry. Consider implementing exponential backoff for production use.
This project is provided as-is for educational and development purposes.