Model Invokers
Prompty supports multiple AI model providers through its invoker system. Each invoker handles the specifics of communicating with different AI services, making it easy to switch between providers or use multiple providers in the same application.
Available Invokers
Section titled “Available Invokers”Prompty comes with built-in support for major AI providers:
- Azure OpenAI - Microsoft’s managed OpenAI service
- OpenAI - Direct integration with OpenAI’s API
- Serverless - GitHub Models and other serverless endpoints
Azure OpenAI Invoker
Section titled “Azure OpenAI Invoker”Installation
Section titled “Installation”pip install "prompty[azure]"Basic Configuration
Section titled “Basic Configuration”import prompty.azure
# In your prompty filemodel: api: chat configuration: type: azure_openai azure_endpoint: ${env:AZURE_OPENAI_ENDPOINT} azure_deployment: gpt-35-turbo api_version: "2024-10-21"Environment Setup
Section titled “Environment Setup”# .env fileAZURE_OPENAI_ENDPOINT=https://your-resource.openai.azure.com/AZURE_OPENAI_API_KEY=your-api-keyAZURE_OPENAI_DEPLOYMENT=gpt-35-turboAuthentication Methods
Section titled “Authentication Methods”API Key Authentication
Section titled “API Key Authentication”model: configuration: type: azure_openai azure_endpoint: ${env:AZURE_OPENAI_ENDPOINT} api_key: ${env:AZURE_OPENAI_API_KEY} azure_deployment: gpt-35-turboAzure AD Authentication
Section titled “Azure AD Authentication”from azure.identity import DefaultAzureCredentialimport prompty.azure
# Configure with Azure ADresponse = prompty.execute( "prompt.prompty", configuration={ "type": "azure_openai", "azure_endpoint": "https://your-resource.openai.azure.com/", "azure_deployment": "gpt-35-turbo", "credential": DefaultAzureCredential() })Available Models
Section titled “Available Models”Azure OpenAI supports various model types:
Chat Models
Section titled “Chat Models”# GPT-3.5 Turbomodel: api: chat configuration: azure_deployment: gpt-35-turbo temperature: 0.7 max_tokens: 1000
# GPT-4model: api: chat configuration: azure_deployment: gpt-4 temperature: 0.7 max_tokens: 2000
# GPT-4 Visionmodel: api: chat configuration: azure_deployment: gpt-4-vision max_tokens: 1000Embedding Models
Section titled “Embedding Models”model: api: embedding configuration: azure_deployment: text-embedding-ada-002Advanced Configuration
Section titled “Advanced Configuration”model: configuration: type: azure_openai azure_endpoint: ${env:AZURE_OPENAI_ENDPOINT} azure_deployment: gpt-4 api_version: "2024-10-21" temperature: 0.7 max_tokens: 2000 top_p: 0.9 frequency_penalty: 0.1 presence_penalty: 0.1 stop: ["###", "END"]OpenAI Invoker
Section titled “OpenAI Invoker”Installation
Section titled “Installation”pip install "prompty[openai]"Basic Configuration
Section titled “Basic Configuration”import prompty.openai
# In your prompty filemodel: api: chat configuration: type: openai model: gpt-3.5-turbo api_key: ${env:OPENAI_API_KEY}Environment Setup
Section titled “Environment Setup”# .env fileOPENAI_API_KEY=sk-your-api-keyOPENAI_ORG_ID=org-your-org-id # OptionalAvailable Models
Section titled “Available Models”Chat Models
Section titled “Chat Models”# GPT-3.5 Turbomodel: api: chat configuration: type: openai model: gpt-3.5-turbo temperature: 0.7
# GPT-4model: api: chat configuration: type: openai model: gpt-4 temperature: 0.7
# GPT-4 Turbomodel: api: chat configuration: type: openai model: gpt-4-turbo-preview temperature: 0.7Embedding Models
Section titled “Embedding Models”model: api: embedding configuration: type: openai model: text-embedding-3-smallAdvanced Configuration
Section titled “Advanced Configuration”model: configuration: type: openai model: gpt-4 temperature: 0.7 max_tokens: 2000 top_p: 0.9 frequency_penalty: 0.0 presence_penalty: 0.0 stream: false logit_bias: {} user: "user-123"Serverless Invoker
Section titled “Serverless Invoker”Installation
Section titled “Installation”pip install "prompty[serverless]"GitHub Models
Section titled “GitHub Models”import prompty.serverless
# Configure for GitHub Modelsmodel: api: chat configuration: type: serverless endpoint: https://models.inference.ai.azure.com model: gpt-4o-mini api_key: ${env:GITHUB_TOKEN}Environment Setup for GitHub Models
Section titled “Environment Setup for GitHub Models”# .env fileGITHUB_TOKEN=ghp_your-personal-access-tokenAvailable GitHub Models
Section titled “Available GitHub Models”# GPT-4o Minimodel: configuration: type: serverless endpoint: https://models.inference.ai.azure.com model: gpt-4o-mini
# Phi-3 Minimodel: configuration: type: serverless endpoint: https://models.inference.ai.azure.com model: Phi-3-mini-4k-instruct
# Llama Modelsmodel: configuration: type: serverless endpoint: https://models.inference.ai.azure.com model: Meta-Llama-3-8B-InstructCustom Serverless Endpoints
Section titled “Custom Serverless Endpoints”model: configuration: type: serverless endpoint: https://your-custom-endpoint.com/v1 model: your-custom-model api_key: ${env:CUSTOM_API_KEY} headers: Custom-Header: valueProgrammatic Usage
Section titled “Programmatic Usage”Switching Between Invokers
Section titled “Switching Between Invokers”import promptyimport prompty.azureimport prompty.openaiimport prompty.serverless
# Use Azure OpenAIresponse1 = prompty.execute( "prompt.prompty", configuration={ "type": "azure_openai", "azure_endpoint": "https://your-resource.openai.azure.com/", "azure_deployment": "gpt-35-turbo" })
# Use OpenAIresponse2 = prompty.execute( "prompt.prompty", configuration={ "type": "openai", "model": "gpt-3.5-turbo" })
# Use GitHub Modelsresponse3 = prompty.execute( "prompt.prompty", configuration={ "type": "serverless", "endpoint": "https://models.inference.ai.azure.com", "model": "gpt-4o-mini" })Multiple Invokers in One Application
Section titled “Multiple Invokers in One Application”# Configure different invokers for different purposeschat_config = { "type": "azure_openai", "azure_deployment": "gpt-4"}
embedding_config = { "type": "openai", "model": "text-embedding-3-small"}
summary_config = { "type": "serverless", "endpoint": "https://models.inference.ai.azure.com", "model": "gpt-4o-mini"}
# Use for different operationschat_response = prompty.execute("chat.prompty", configuration=chat_config)embeddings = prompty.execute("embed.prompty", configuration=embedding_config)summary = prompty.execute("summarize.prompty", configuration=summary_config)Custom Invokers
Section titled “Custom Invokers”Creating a Custom Invoker
Section titled “Creating a Custom Invoker”from prompty.invoker import InvokerFactoryfrom prompty.core import Promptyfrom typing import Any, Dict
class CustomInvoker: def invoke( self, prompt: Prompty, configuration: Dict[str, Any], **kwargs ) -> Any: # Implement your custom logic endpoint = configuration.get("endpoint") model = configuration.get("model") api_key = configuration.get("api_key")
# Make API call to your custom service response = self._call_custom_api( endpoint=endpoint, model=model, api_key=api_key, messages=prompt.content, **configuration )
return response
def _call_custom_api(self, **kwargs): # Implement your API call logic pass
# Register the custom invokerInvokerFactory.add_invoker("custom", CustomInvoker)Using Custom Invokers
Section titled “Using Custom Invokers”# Use your custom invokerresponse = prompty.execute( "prompt.prompty", configuration={ "type": "custom", "endpoint": "https://your-api.com/v1", "model": "your-model", "api_key": "your-key" })Error Handling
Section titled “Error Handling”Common Error Patterns
Section titled “Common Error Patterns”import prompty
try: response = prompty.execute("prompt.prompty")except Exception as e: if "unauthorized" in str(e).lower(): print("Check your API key") elif "not found" in str(e).lower(): print("Check your model/deployment name") elif "quota" in str(e).lower(): print("API quota exceeded") else: print(f"Unexpected error: {e}")Invoker-Specific Error Handling
Section titled “Invoker-Specific Error Handling”from azure.core.exceptions import ClientAuthenticationErrorfrom openai import AuthenticationError
try: response = prompty.execute("prompt.prompty")except ClientAuthenticationError: print("Azure authentication failed")except AuthenticationError: print("OpenAI authentication failed")except Exception as e: print(f"General error: {e}")Performance Considerations
Section titled “Performance Considerations”Connection Pooling
Section titled “Connection Pooling”# Reuse connections for better performanceimport prompty.azure
# Load prompt onceprompt = prompty.load("prompt.prompty")
# Execute multiple times with same connectionfor inputs in input_list: response = prompty.execute(prompt, inputs=inputs)Async Support
Section titled “Async Support”import asyncioimport prompty
async def process_prompts(): tasks = [ prompty.execute_async("prompt1.prompty"), prompty.execute_async("prompt2.prompty"), prompty.execute_async("prompt3.prompty") ]
results = await asyncio.gather(*tasks) return results
# Run asyncresults = asyncio.run(process_prompts())Best Practices
Section titled “Best Practices”Troubleshooting
Section titled “Troubleshooting”Connection Issues
Section titled “Connection Issues”# Test connection with CLIprompty -s test.prompty --verbose -e .envAuthentication Problems
Section titled “Authentication Problems”# Verify credentialsimport osprint(f"Azure Endpoint: {os.getenv('AZURE_OPENAI_ENDPOINT')}")print(f"API Key: {'***' if os.getenv('AZURE_OPENAI_API_KEY') else 'Not set'}")Model Availability
Section titled “Model Availability”# Test model availabilitytry: response = prompty.execute( prompty.headless( api="chat", content="Test", configuration={"type": "azure_openai", "azure_deployment": "gpt-35-turbo"} ) ) print("Model is available")except Exception as e: print(f"Model not available: {e}")Next Steps
Section titled “Next Steps”- Learn about Configuration for advanced setup
- Explore Observability & Tracing for monitoring
- Check out CLI Usage for command-line operations
Want to Contribute To the Project? - Updated Guidance Coming Soon.