diff --git a/api-reference/inference-api/authentication.mdx b/api-reference/inference-api/authentication.mdx index 6c8c55af..0e2470dc 100644 --- a/api-reference/inference-api/authentication.mdx +++ b/api-reference/inference-api/authentication.mdx @@ -46,7 +46,7 @@ from portkey_ai import Portkey client = Portkey( api_key="PORTKEY_API_KEY", # Replace with your actual API key - virtual_key="VIRTUAL_KEY" # Optional: Use if virtual keys are set up + provider="@VIRTUAL_KEY" # Optional: Use if virtual keys are set up ) chat_completion = client.chat.completions.create( diff --git a/api-reference/inference-api/gateway-for-other-apis.mdx b/api-reference/inference-api/gateway-for-other-apis.mdx index bb020bc4..6c55d7c5 100644 --- a/api-reference/inference-api/gateway-for-other-apis.mdx +++ b/api-reference/inference-api/gateway-for-other-apis.mdx @@ -48,7 +48,7 @@ Portkey integrates with 40+ LLM providers. Add your provider credentials (such a curl https://api.portkey.ai/v1/rerank \ -H "Content-Type: application/json" \ -H "x-portkey-api-key: $PORTKEY_API_KEY" \ - -H "x-portkey-virtual-key: $PORTKEY_PROVIDER_VIRTUAL_KEY" \ + -H "x-portkey-virtual-key: $PORTKEY_PROVIDER" \ ``` ```py Python @@ -56,7 +56,7 @@ from portkey_ai import Portkey portkey = Portkey( api_key = "PORTKEY_API_KEY", - virtual_key = "PROVIDER_VIRTUAL_KEY" + virtual_key = "PROVIDER" ) ``` @@ -65,7 +65,7 @@ import Portkey from 'portkey-ai'; const portkey = new Portkey({ apiKey: 'PORTKEY_API_KEY', - virtualKey: 'PROVIDER_VIRTUAL_KEY' + virtualKey: 'PROVIDER' }); ``` @@ -181,7 +181,7 @@ curl --request GET \ --url https://api.portkey.ai/v1/collections \ --header 'Content-Type: application/json' \ --header 'x-portkey-api-key: $PORTKEY_API_KEY' \ - --header 'x-portkey-virtual-key: $PROVIDER_VIRTUAL_KEY' + --header 'x-portkey-virtual-key: $PROVIDER' ``` ```bash PUT @@ -189,7 +189,7 @@ curl --request PUT \ --url https://api.portkey.ai/v1/collections/my-collection \ --header 'Content-Type: application/json' \ --header 'x-portkey-api-key: $PORTKEY_API_KEY' \ - --header 'x-portkey-virtual-key: $PROVIDER_VIRTUAL_KEY' \ + --header 'x-portkey-virtual-key: $PROVIDER' \ --data '{ "metadata": { "description": "Updated collection description" @@ -202,7 +202,7 @@ curl --request DELETE \ --url https://api.portkey.ai/v1/collections/my-collection \ --header 'Content-Type: application/json' \ --header 'x-portkey-api-key: $PORTKEY_API_KEY' \ - --header 'x-portkey-virtual-key: $PROVIDER_VIRTUAL_KEY' + --header 'x-portkey-virtual-key: $PROVIDER' ``` @@ -216,7 +216,7 @@ from portkey_ai import Portkey portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="PROVIDER_VIRTUAL_KEY" + provider="@PROVIDER" ) response = portkey.post( @@ -235,7 +235,7 @@ from portkey_ai import Portkey portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="PROVIDER_VIRTUAL_KEY" + provider="@PROVIDER" ) response = portkey.get('/collections') @@ -246,7 +246,7 @@ from portkey_ai import Portkey portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="PROVIDER_VIRTUAL_KEY" + provider="@PROVIDER" ) response = portkey.put( @@ -262,7 +262,7 @@ from portkey_ai import Portkey portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="PROVIDER_VIRTUAL_KEY" + provider="@PROVIDER" ) response = portkey.delete('/collections/my-collection') @@ -279,7 +279,7 @@ import Portkey from 'portkey-ai'; const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", - virtualKey: "PROVIDER_VIRTUAL_KEY" + virtualKey: "PROVIDER" }); const response = await portkey.post('/rerank', { @@ -297,7 +297,7 @@ import Portkey from 'portkey-ai'; const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", - virtualKey: "PROVIDER_VIRTUAL_KEY" + virtualKey: "PROVIDER" }); const response = await portkey.get('/collections'); @@ -308,7 +308,7 @@ import Portkey from 'portkey-ai'; const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", - virtualKey: "PROVIDER_VIRTUAL_KEY" + virtualKey: "PROVIDER" }); const response = await portkey.put('/collections/my-collection', { @@ -323,7 +323,7 @@ import Portkey from 'portkey-ai'; const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", - virtualKey: "PROVIDER_VIRTUAL_KEY" + virtualKey: "PROVIDER" }); const response = await portkey.delete('/collections/my-collection'); @@ -343,7 +343,7 @@ from portkey_ai import Portkey portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="COHERE_VIRTUAL_KEY" + provider="@COHERE_VIRTUAL_KEY" ) response = portkey.post( diff --git a/api-reference/inference-api/headers.mdx b/api-reference/inference-api/headers.mdx index c4936cf5..308b3b96 100644 --- a/api-reference/inference-api/headers.mdx +++ b/api-reference/inference-api/headers.mdx @@ -569,7 +569,7 @@ from portkey_ai import Portkey portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="VIRTUAL_KEY", + provider="@VIRTUAL_KEY", config="CONFIG_ID" ) diff --git a/api-reference/sdk/python.mdx b/api-reference/sdk/python.mdx index 05a64801..6437e51f 100644 --- a/api-reference/sdk/python.mdx +++ b/api-reference/sdk/python.mdx @@ -45,7 +45,7 @@ from portkey_ai import Portkey client = Portkey( api_key="your_api_key_here", # Or use the env var PORTKEY_API_KEY - virtual_key="your_virtual_key_here" # Or use config="cf-***" + provider="@your_virtual_key_here" # Or use config="cf-***" ) response = client.chat.completions.create( @@ -67,7 +67,7 @@ The SDK requires: ```python # With Virtual Key -portkey = Portkey(api_key="...", virtual_key="...") +portkey = Portkey(api_key="...", provider="@...") # With Config portkey = Portkey(api_key="...", config="cf-***") @@ -87,7 +87,7 @@ from portkey_ai import AsyncPortkey portkey = AsyncPortkey( api_key="PORTKEY_API_KEY", - virtual_key="VIRTUAL_KEY" + provider="@VIRTUAL_KEY" ) async def main(): @@ -117,7 +117,7 @@ custom_client = httpx.Client(verify=False) portkey = Portkey( api_key="your_api_key_here", - virtual_key="your_virtual_key_here", + provider="@your_virtual_key_here", http_client=custom_client ) @@ -157,7 +157,7 @@ Here's how you can use these headers with the Python SDK: ```python portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="VIRTUAL_KEY", + provider="@VIRTUAL_KEY", # Add any other headers from the reference ) diff --git a/docs.json b/docs.json index f38212da..61a8ca0a 100644 --- a/docs.json +++ b/docs.json @@ -12,7 +12,7 @@ "navigation": { "versions": [ { - "version": "Using Model Catalog", + "version": "Latest", "tabs": [ { "tab": "Documentation", @@ -954,7 +954,7 @@ ] }, { - "version": "Using Virtual Key", + "version": "Virtual Keys (Deprecated)", "tabs": [ { "tab": "Documentation", @@ -962,9 +962,9 @@ { "group": "Introduction", "pages": [ - "introduction/what-is-portkey", - "introduction/make-your-first-request", - "introduction/feature-overview" + "virtual_key_old/introduction/what-is-portkey", + "virtual_key_old/introduction/make-your-first-request", + "virtual_key_old/introduction/feature-overview" ] }, { @@ -973,38 +973,47 @@ { "group": "Observability", "pages": [ - "product/observability", - "product/observability/logs", - "product/observability/traces", - "product/observability/analytics", - "product/observability/feedback", - "product/observability/metadata", - "product/observability/filters", - "product/observability/logs-export", - "product/observability/budget-limits", - "product/observability/auto-instrumentation" + "virtual_key_old/product/observability", + "virtual_key_old/product/observability/logs", + "virtual_key_old/product/observability/traces", + "virtual_key_old/product/observability/analytics", + "virtual_key_old/product/observability/feedback", + "virtual_key_old/product/observability/metadata", + "virtual_key_old/product/observability/filters", + "virtual_key_old/product/observability/logs-export", + "virtual_key_old/product/observability/budget-limits", + "virtual_key_old/product/observability/auto-instrumentation" ] }, { "group": "AI Gateway", "pages": [ - "product/ai-gateway", - "product/ai-gateway/universal-api", - "product/ai-gateway/configs", - "product/ai-gateway/remote-mcp", - "product/ai-gateway/conditional-routing", + "virtual_key_old/product/ai-gateway", + "virtual_key_old/product/ai-gateway/universal-api", + "virtual_key_old/product/ai-gateway/configs", + "virtual_key_old/product/ai-gateway/remote-mcp", + "virtual_key_old/product/ai-gateway/conditional-routing", { "group": "Multimodal Capabilities", "pages": [ - "product/ai-gateway/multimodal-capabilities", - "product/ai-gateway/multimodal-capabilities/image-generation", - "product/ai-gateway/multimodal-capabilities/function-calling", - "product/ai-gateway/multimodal-capabilities/vision", - "product/ai-gateway/multimodal-capabilities/text-to-speech", - "product/ai-gateway/multimodal-capabilities/speech-to-text", - "/product/ai-gateway/multimodal-capabilities/thinking-mode" + "virtual_key_old/product/ai-gateway/multimodal-capabilities", + "virtual_key_old/product/ai-gateway/multimodal-capabilities/image-generation", + "virtual_key_old/product/ai-gateway/multimodal-capabilities/function-calling", + "virtual_key_old/product/ai-gateway/multimodal-capabilities/vision", + "virtual_key_old/product/ai-gateway/multimodal-capabilities/text-to-speech", + "virtual_key_old/product/ai-gateway/multimodal-capabilities/speech-to-text", + "virtual_key_old/product/ai-gateway/multimodal-capabilities/thinking-mode" ] }, +<<<<<<< model-catalog-v4 + "virtual_key_old/product/ai-gateway/cache-simple-and-semantic", + "virtual_key_old/product/ai-gateway/fallbacks", + "virtual_key_old/product/ai-gateway/automatic-retries", + "virtual_key_old/product/ai-gateway/realtime-api", + "virtual_key_old/product/ai-gateway/load-balancing", + "virtual_key_old/product/ai-gateway/canary-testing", + "virtual_key_old/product/ai-gateway/strict-open-ai-compliance", +======= "product/ai-gateway/cache-simple-and-semantic", "product/ai-gateway/fallbacks", "product/ai-gateway/automatic-retries", @@ -1013,105 +1022,106 @@ "product/ai-gateway/load-balancing", "product/ai-gateway/canary-testing", "product/ai-gateway/strict-open-ai-compliance", +>>>>>>> main { "group": "Virtual Keys", "pages": [ - "product/ai-gateway/virtual-keys", - "product/ai-gateway/virtual-keys/budget-limits", - "product/ai-gateway/virtual-keys/rate-limits", - "product/ai-gateway/virtual-keys/bedrock-amazon-assumed-role" + "virtual_key_old/product/ai-gateway/virtual-keys", + "virtual_key_old/product/ai-gateway/virtual-keys/budget-limits", + "virtual_key_old/product/ai-gateway/virtual-keys/rate-limits", + "virtual_key_old/product/ai-gateway/virtual-keys/bedrock-amazon-assumed-role" ] }, - "product/ai-gateway/request-timeouts", - "product/ai-gateway/files", - "product/ai-gateway/batches", - "product/ai-gateway/fine-tuning" + "virtual_key_old/product/ai-gateway/request-timeouts", + "virtual_key_old/product/ai-gateway/files", + "virtual_key_old/product/ai-gateway/batches", + "virtual_key_old/product/ai-gateway/fine-tuning" ] }, { "group": "Guardrails", "pages": [ - "product/guardrails", - "product/guardrails/list-of-guardrail-checks", - "product/guardrails/embedding-guardrails", - "product/guardrails/creating-raw-guardrails-in-json", - "product/guardrails/pii-redaction", - "integrations/guardrails/bring-your-own-guardrails" + "virtual_key_old/product/guardrails", + "virtual_key_old/product/guardrails/list-of-guardrail-checks", + "virtual_key_old/product/guardrails/embedding-guardrails", + "virtual_key_old/product/guardrails/creating-raw-guardrails-in-json", + "virtual_key_old/product/guardrails/pii-redaction", + "virtual_key_old/integrations/guardrails/bring-your-own-guardrails" ] }, - "product/mcp", + "virtual_key_old/product/mcp", { "group": "Administration", "pages": [ - "product/administration/enforcing-request-metadata", - "product/administration/enforce-orgnization-level-guardrails", - "product/administration/enforce-workspace-level-guardials", - "product/administration/enforce-default-config", - "product/administration/enforce-budget-and-rate-limit", - "product/administration/configure-logs-access-permissions-in-workspace", - "product/administration/configure-virtual-key-access-permissions", - "product/administration/configure-api-key-access-permissions" + "virtual_key_old/product/administration/enforcing-request-metadata", + "virtual_key_old/product/administration/enforce-orgnization-level-guardrails", + "virtual_key_old/product/administration/enforce-workspace-level-guardials", + "virtual_key_old/product/administration/enforce-default-config", + "virtual_key_old/product/administration/enforce-budget-and-rate-limit", + "virtual_key_old/product/administration/configure-logs-access-permissions-in-workspace", + "virtual_key_old/product/administration/configure-virtual-key-access-permissions", + "virtual_key_old/product/administration/configure-api-key-access-permissions" ] }, { "group": "Security", - "pages": ["product/security/pii"] + "pages": ["virtual_key_old/product/security/pii"] }, - "product/autonomous-fine-tuning", + "virtual_key_old/product/autonomous-fine-tuning", { "group": "Enterprise Offering", "pages": [ - "product/enterprise-offering", + "virtual_key_old/product/enterprise-offering", { "group": "Org Management", "pages": [ - "product/enterprise-offering/org-management", - "product/enterprise-offering/org-management/organizations", - "product/enterprise-offering/org-management/workspaces", - "product/enterprise-offering/org-management/user-roles-and-permissions", - "product/enterprise-offering/org-management/api-keys-authn-and-authz", - "product/enterprise-offering/org-management/jwt", + "virtual_key_old/product/enterprise-offering/org-management", + "virtual_key_old/product/enterprise-offering/org-management/organizations", + "virtual_key_old/product/enterprise-offering/org-management/workspaces", + "virtual_key_old/product/enterprise-offering/org-management/user-roles-and-permissions", + "virtual_key_old/product/enterprise-offering/org-management/api-keys-authn-and-authz", + "virtual_key_old/product/enterprise-offering/org-management/jwt", { "group": "SCIM", "pages": [ - "product/enterprise-offering/org-management/scim/scim", - "product/enterprise-offering/org-management/scim/azure-ad", - "product/enterprise-offering/org-management/scim/okta" + "virtual_key_old/product/enterprise-offering/org-management/scim/scim", + "virtual_key_old/product/enterprise-offering/org-management/scim/azure-ad", + "virtual_key_old/product/enterprise-offering/org-management/scim/okta" ] }, - "product/enterprise-offering/org-management/sso" + "virtual_key_old/product/enterprise-offering/org-management/sso" ] }, - "product/enterprise-offering/access-control-management", - "product/enterprise-offering/budget-limits", - "product/enterprise-offering/security-portkey", - "product/enterprise-offering/analytics-logs-export", - "product/enterprise-offering/logs-export", - "/product/enterprise-offering/audit-logs", + "virtual_key_old/product/enterprise-offering/access-control-management", + "virtual_key_old/product/enterprise-offering/budget-limits", + "virtual_key_old/product/enterprise-offering/security-portkey", + "virtual_key_old/product/enterprise-offering/analytics-logs-export", + "virtual_key_old/product/enterprise-offering/logs-export", + "virtual_key_old/product/enterprise-offering/audit-logs", { "group": "Private Cloud Deployments", "pages": [ - "product/enterprise-offering/private-cloud-deployments", - "product/enterprise-offering/private-cloud-deployments/architecture", - "product/enterprise-offering/private-cloud-deployments/aws", - "product/enterprise-offering/private-cloud-deployments/gcp", - "product/enterprise-offering/private-cloud-deployments/azure", - "product/enterprise-offering/private-cloud-deployments/cloudflare-workers", - "product/enterprise-offering/private-cloud-deployments/f5-app-stack" + "virtual_key_old/product/enterprise-offering/private-cloud-deployments", + "virtual_key_old/product/enterprise-offering/private-cloud-deployments/architecture", + "virtual_key_old/product/enterprise-offering/private-cloud-deployments/aws", + "virtual_key_old/product/enterprise-offering/private-cloud-deployments/gcp", + "virtual_key_old/product/enterprise-offering/private-cloud-deployments/azure", + "virtual_key_old/product/enterprise-offering/private-cloud-deployments/cloudflare-workers", + "virtual_key_old/product/enterprise-offering/private-cloud-deployments/f5-app-stack" ] }, - "product/enterprise-offering/components", - "product/enterprise-offering/kms", + "virtual_key_old/product/enterprise-offering/components", + "virtual_key_old/product/enterprise-offering/kms", { "group": "Cloud Marketplace", "pages": [ - "product/enterprise-offering/cloud-marketplace/aws" + "virtual_key_old/product/enterprise-offering/cloud-marketplace/aws" ] } ] }, - "product/open-source", - "product/product-feature-comparison" + "virtual_key_old/product/open-source", + "virtual_key_old/product/product-feature-comparison" ] }, { @@ -1132,231 +1142,231 @@ "groups": [ { "group": "Ecosystem", - "pages": ["integrations/ecosystem", "integrations/partner"] + "pages": ["virtual_key_old/integrations/ecosystem", "virtual_key_old/integrations/partner"] }, { "group": "LLM Integrations", "pages": [ - "integrations/llms", + "virtual_key_old/integrations/llms", { "group": "OpenAI", "pages": [ - "integrations/llms/openai", - "integrations/llms/openai/structured-outputs", - "integrations/llms/openai/prompt-caching-openai", - "integrations/llms/openai/files", - "integrations/llms/openai/batches", - "integrations/llms/openai/fine-tuning" + "virtual_key_old/integrations/llms/openai", + "virtual_key_old/integrations/llms/openai/structured-outputs", + "virtual_key_old/integrations/llms/openai/prompt-caching-openai", + "virtual_key_old/integrations/llms/openai/files", + "virtual_key_old/integrations/llms/openai/batches", + "virtual_key_old/integrations/llms/openai/fine-tuning" ] }, { "group": "Anthropic", "pages": [ - "integrations/llms/anthropic", - "integrations/llms/anthropic/prompt-caching", - "integrations/llms/anthropic/computer-use" + "virtual_key_old/integrations/llms/anthropic", + "virtual_key_old/integrations/llms/anthropic/prompt-caching", + "virtual_key_old/integrations/llms/anthropic/computer-use" ] }, - "integrations/llms/gemini", + "virtual_key_old/integrations/llms/gemini", { "group": "Google Vertex AI", "pages": [ - "integrations/llms/vertex-ai", - "integrations/llms/vertex-ai/files", - "integrations/llms/vertex-ai/batches", - "integrations/llms/vertex-ai/fine-tuning", - "integrations/llms/vertex-ai/controlled-generations", - "integrations/llms/vertex-ai/embeddings" + "virtual_key_old/integrations/llms/vertex-ai", + "virtual_key_old/integrations/llms/vertex-ai/files", + "virtual_key_old/integrations/llms/vertex-ai/batches", + "virtual_key_old/integrations/llms/vertex-ai/fine-tuning", + "virtual_key_old/integrations/llms/vertex-ai/controlled-generations", + "virtual_key_old/integrations/llms/vertex-ai/embeddings" ] }, { "group": "Azure", "pages": [ - "integrations/llms/azure-foundry", - "integrations/llms/azure-openai/azure-openai", - "integrations/llms/azure-openai/fine-tuning", - "integrations/llms/azure-openai/files", - "integrations/llms/azure-openai/batches" + "virtual_key_old/integrations/llms/azure-foundry", + "virtual_key_old/integrations/llms/azure-openai/azure-openai", + "virtual_key_old/integrations/llms/azure-openai/fine-tuning", + "virtual_key_old/integrations/llms/azure-openai/files", + "virtual_key_old/integrations/llms/azure-openai/batches" ] }, { "group": "Bedrock", "pages": [ - "integrations/llms/bedrock/aws-bedrock", - "integrations/llms/bedrock/files", - "integrations/llms/bedrock/batches", - "integrations/llms/bedrock/fine-tuning", - "integrations/llms/bedrock/prompt-caching", - "integrations/llms/bedrock/embeddings" + "virtual_key_old/integrations/llms/bedrock/aws-bedrock", + "virtual_key_old/integrations/llms/bedrock/files", + "virtual_key_old/integrations/llms/bedrock/batches", + "virtual_key_old/integrations/llms/bedrock/fine-tuning", + "virtual_key_old/integrations/llms/bedrock/prompt-caching", + "virtual_key_old/integrations/llms/bedrock/embeddings" ] }, - "integrations/llms/aws-sagemaker", - "integrations/llms/ollama", + "virtual_key_old/integrations/llms/aws-sagemaker", + "virtual_key_old/integrations/llms/ollama", { "group": "More", "pages": [ - "integrations/llms/local-ai", - "integrations/llms/vllm", - "integrations/llms/triton", - "integrations/llms/ai21", - "integrations/llms/anyscale-llama2-mistral-zephyr", - "integrations/llms/cerebras", - "integrations/llms/cohere", + "virtual_key_old/integrations/llms/local-ai", + "virtual_key_old/integrations/llms/vllm", + "virtual_key_old/integrations/llms/triton", + "virtual_key_old/integrations/llms/ai21", + "virtual_key_old/integrations/llms/anyscale-llama2-mistral-zephyr", + "virtual_key_old/integrations/llms/cerebras", + "virtual_key_old/integrations/llms/cohere", { "group": "Fireworks", "pages": [ - "integrations/llms/fireworks", - "integrations/llms/fireworks/files", - "integrations/llms/fireworks/fine-tuning" + "virtual_key_old/integrations/llms/fireworks", + "virtual_key_old/integrations/llms/fireworks/files", + "virtual_key_old/integrations/llms/fireworks/fine-tuning" ] }, - "integrations/llms/dashscope", - "integrations/llms/deepinfra", - "integrations/llms/deepbricks", - "integrations/llms/deepgram", - "integrations/llms/deepseek", - "integrations/llms/github", - "integrations/llms/groq", - "integrations/llms/huggingface", - "integrations/llms/inference.net", - "integrations/llms/jina-ai", - "integrations/llms/lambda", - "integrations/llms/lemon-fox", - "integrations/llms/lepton", - "integrations/llms/lingyi-01.ai", - "integrations/llms/mistral-ai", - "integrations/llms/monster-api", - "integrations/llms/moonshot", - "integrations/llms/ncompass", - "integrations/llms/nomic", - "integrations/llms/nscale", - "integrations/llms/novita-ai", - "integrations/llms/nebius", - "integrations/llms/openrouter", - "integrations/llms/perplexity-ai", - "integrations/llms/predibase", - "integrations/llms/reka-ai", - "integrations/llms/recraft-ai", - "integrations/llms/sambanova", - "integrations/llms/segmind", - "integrations/llms/snowflake-cortex", - "integrations/llms/stability-ai", - "integrations/llms/siliconflow", - "integrations/llms/together-ai", - "integrations/llms/upstage", - "integrations/llms/voyage-ai", - "integrations/llms/workers-ai", - "integrations/llms/x-ai", - "integrations/llms/zhipu", - "integrations/llms/replicate", - "integrations/llms/suggest-a-new-integration" + "virtual_key_old/integrations/llms/dashscope", + "virtual_key_old/integrations/llms/deepinfra", + "virtual_key_old/integrations/llms/deepbricks", + "virtual_key_old/integrations/llms/deepgram", + "virtual_key_old/integrations/llms/deepseek", + "virtual_key_old/integrations/llms/github", + "virtual_key_old/integrations/llms/groq", + "virtual_key_old/integrations/llms/huggingface", + "virtual_key_old/integrations/llms/inference.net", + "virtual_key_old/integrations/llms/jina-ai", + "virtual_key_old/integrations/llms/lambda", + "virtual_key_old/integrations/llms/lemon-fox", + "virtual_key_old/integrations/llms/lepton", + "virtual_key_old/integrations/llms/lingyi-01.ai", + "virtual_key_old/integrations/llms/mistral-ai", + "virtual_key_old/integrations/llms/monster-api", + "virtual_key_old/integrations/llms/moonshot", + "virtual_key_old/integrations/llms/ncompass", + "virtual_key_old/integrations/llms/nomic", + "virtual_key_old/integrations/llms/nscale", + "virtual_key_old/integrations/llms/novita-ai", + "virtual_key_old/integrations/llms/nebius", + "virtual_key_old/integrations/llms/openrouter", + "virtual_key_old/integrations/llms/perplexity-ai", + "virtual_key_old/integrations/llms/predibase", + "virtual_key_old/integrations/llms/reka-ai", + "virtual_key_old/integrations/llms/recraft-ai", + "virtual_key_old/integrations/llms/sambanova", + "virtual_key_old/integrations/llms/segmind", + "virtual_key_old/integrations/llms/snowflake-cortex", + "virtual_key_old/integrations/llms/stability-ai", + "virtual_key_old/integrations/llms/siliconflow", + "virtual_key_old/integrations/llms/together-ai", + "virtual_key_old/integrations/llms/upstage", + "virtual_key_old/integrations/llms/voyage-ai", + "virtual_key_old/integrations/llms/workers-ai", + "virtual_key_old/integrations/llms/x-ai", + "virtual_key_old/integrations/llms/zhipu", + "virtual_key_old/integrations/llms/replicate", + "virtual_key_old/integrations/llms/suggest-a-new-integration" ] }, - "integrations/llms/byollm" + "virtual_key_old/integrations/llms/byollm" ] }, { "group": "Cloud Platforms", - "pages": ["integrations/cloud/azure"] + "pages": ["virtual_key_old/integrations/cloud/azure"] }, { "group": "Guardrails", "pages": [ - "integrations/guardrails/aporia", - "integrations/guardrails/acuvity", - "integrations/guardrails/azure-guardrails", - "integrations/guardrails/bedrock-guardrails", - "integrations/guardrails/lasso", - "integrations/guardrails/mistral", - "integrations/guardrails/pangea", - "integrations/guardrails/patronus-ai", - "integrations/guardrails/pillar", - "integrations/guardrails/prompt-security", - "integrations/guardrails/bring-your-own-guardrails" + "virtual_key_old/integrations/guardrails/aporia", + "virtual_key_old/integrations/guardrails/acuvity", + "virtual_key_old/integrations/guardrails/azure-guardrails", + "virtual_key_old/integrations/guardrails/bedrock-guardrails", + "virtual_key_old/integrations/guardrails/lasso", + "virtual_key_old/integrations/guardrails/mistral", + "virtual_key_old/integrations/guardrails/pangea", + "virtual_key_old/integrations/guardrails/patronus-ai", + "virtual_key_old/integrations/guardrails/pillar", + "virtual_key_old/integrations/guardrails/prompt-security", + "virtual_key_old/integrations/guardrails/bring-your-own-guardrails" ] }, { "group": "Plugins", - "pages": ["integrations/plugins/exa"] + "pages": ["virtual_key_old/integrations/plugins/exa"] }, { "group": "Vector Databases", "pages": [ - "integrations/vector-databases/milvus", - "integrations/vector-databases/qdrant" + "virtual_key_old/integrations/vector-databases/milvus", + "virtual_key_old/integrations/vector-databases/qdrant" ] }, { "group": "Agents", "pages": [ - "integrations/agents", - "integrations/agents/openai-agents", - "integrations/agents/openai-agents-ts", - "integrations/agents/autogen", - "integrations/agents/control-flow", - "integrations/agents/crewai", - "integrations/agents/langchain-agents", - "integrations/agents/langroid", - "integrations/agents/langgraph", - "integrations/agents/livekit", - "integrations/agents/llama-agents", - "integrations/agents/openai-swarm", - "integrations/agents/pydantic-ai", - "integrations/agents/phidata", - "integrations/agents/strands", - "integrations/agents/bring-your-own-agents" + "virtual_key_old/integrations/agents", + "virtual_key_old/integrations/agents/openai-agents", + "virtual_key_old/integrations/agents/openai-agents-ts", + "virtual_key_old/integrations/agents/autogen", + "virtual_key_old/integrations/agents/control-flow", + "virtual_key_old/integrations/agents/crewai", + "virtual_key_old/integrations/agents/langchain-agents", + "virtual_key_old/integrations/agents/langroid", + "virtual_key_old/integrations/agents/langgraph", + "virtual_key_old/integrations/agents/livekit", + "virtual_key_old/integrations/agents/llama-agents", + "virtual_key_old/integrations/agents/openai-swarm", + "virtual_key_old/integrations/agents/pydantic-ai", + "virtual_key_old/integrations/agents/phidata", + "virtual_key_old/integrations/agents/strands", + "virtual_key_old/integrations/agents/bring-your-own-agents" ] }, { "group": "AI Apps", "pages": [ - "integrations/ai-apps", - "integrations/libraries/anythingllm", - "integrations/libraries/claude-code", - "integrations/libraries/anthropic-computer-use", - "integrations/libraries/cline", - "integrations/libraries/goose", - "integrations/libraries/janhq", + "virtual_key_old/integrations/ai-apps", + "virtual_key_old/integrations/libraries/anythingllm", + "virtual_key_old/integrations/libraries/claude-code", + "virtual_key_old/integrations/libraries/anthropic-computer-use", + "virtual_key_old/integrations/libraries/cline", + "virtual_key_old/integrations/libraries/goose", + "virtual_key_old/integrations/libraries/janhq", { "group": "Librechat", "pages": [ - "integrations/libraries/librechat", + "virtual_key_old/integrations/libraries/librechat", "guides/use-cases/librechat-web-search" ] }, - "integrations/libraries/n8n", - "integrations/libraries/codex", - "integrations/libraries/openwebui", - "integrations/libraries/roo-code", - "integrations/libraries/zed" + "virtual_key_old/integrations/libraries/n8n", + "virtual_key_old/integrations/libraries/codex", + "virtual_key_old/integrations/libraries/openwebui", + "virtual_key_old/integrations/libraries/roo-code", + "virtual_key_old/integrations/libraries/zed" ] }, { "group": "Libraries", "pages": [ - "integrations/libraries", - "integrations/libraries/autogen", - "integrations/libraries/dspy", - "integrations/libraries/instructor", - "integrations/libraries/langchain-python", - "integrations/libraries/langchain-js", - "integrations/libraries/llama-index-python", - "integrations/libraries/promptfoo", - "integrations/libraries/vercel", - "integrations/libraries/mindsdb", - "integrations/libraries/tooljet", - "integrations/libraries/mongodb", - "integrations/libraries/supabase", - "integrations/libraries/zed", - "integrations/libraries/openai-compatible", - "integrations/libraries/microsoft-semantic-kernel" + "virtual_key_old/integrations/libraries", + "virtual_key_old/integrations/libraries/autogen", + "virtual_key_old/integrations/libraries/dspy", + "virtual_key_old/integrations/libraries/instructor", + "virtual_key_old/integrations/libraries/langchain-python", + "virtual_key_old/integrations/libraries/langchain-js", + "virtual_key_old/integrations/libraries/llama-index-python", + "virtual_key_old/integrations/libraries/promptfoo", + "virtual_key_old/integrations/libraries/vercel", + "virtual_key_old/integrations/libraries/mindsdb", + "virtual_key_old/integrations/libraries/tooljet", + "virtual_key_old/integrations/libraries/mongodb", + "virtual_key_old/integrations/libraries/supabase", + "virtual_key_old/integrations/libraries/zed", + "virtual_key_old/integrations/libraries/openai-compatible", + "virtual_key_old/integrations/libraries/microsoft-semantic-kernel" ] }, { "group": "Tracing Providers", "pages": [ - "integrations/tracing-providers/arize", - "integrations/tracing-providers/logfire" + "virtual_key_old/integrations/tracing-providers/arize", + "virtual_key_old/integrations/tracing-providers/logfire" ] } ] diff --git a/guides/getting-started/getting-started-with-ai-gateway.mdx b/guides/getting-started/getting-started-with-ai-gateway.mdx index bf7ee0c0..b1d66673 100644 --- a/guides/getting-started/getting-started-with-ai-gateway.mdx +++ b/guides/getting-started/getting-started-with-ai-gateway.mdx @@ -553,7 +553,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key + provider="@VIRTUAL_KEY" # Add your provider's virtual key ) tools = [ @@ -749,7 +749,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key + provider="@VIRTUAL_KEY" # Add your provider's virtual key ) response = portkey.chat.completions.create( @@ -890,7 +890,7 @@ from IPython.display import display, Image # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key + provider="@VIRTUAL_KEY" # Add your provider's virtual key ) image = portkey.images.generate( @@ -969,7 +969,7 @@ client = OpenAI( base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( api_key="PORTKEY_API_KEY", - virtual_key="OPENAI_VIRTUAL_KEY" + provider="@OPENAI_VIRTUAL_KEY" ) ) @@ -1057,7 +1057,7 @@ client = OpenAI( base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( api_key="PORTKEY_API_KEY", - virtual_key="PROVIDER_VIRTUAL_KEY" + provider="@PROVIDER_VIRTUAL_KEY" ) ) @@ -1160,7 +1160,7 @@ client = OpenAI( base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( api_key="PORTKEY_API_KEY", - virtual_key="PROVIDER_VIRTUAL_KEY" + provider="@PROVIDER_VIRTUAL_KEY" ) ) diff --git a/guides/getting-started/llama-3-on-groq.mdx b/guides/getting-started/llama-3-on-groq.mdx index 1d91027e..e5ac1bd5 100644 --- a/guides/getting-started/llama-3-on-groq.mdx +++ b/guides/getting-started/llama-3-on-groq.mdx @@ -112,7 +112,7 @@ from portkey_ai import Portkey portkey = Portkey( api_key = userdata.get('PORTKEY_API_KEY'), # replace with your Portkey API key - virtual_key= "groq-431005", # replace with your virtual key for Groq AI + provider="@groq-431005", # replace with your virtual key for Groq AI ) ``` ```py diff --git a/guides/integrations/groq.mdx b/guides/integrations/groq.mdx index cb6d37b8..3a23aaa0 100644 --- a/guides/integrations/groq.mdx +++ b/guides/integrations/groq.mdx @@ -135,7 +135,7 @@ portkey = Portkey( api_key = userdata.get('PORTKEY_API_KEY'), # replace with your Portkey API key - virtual_key= "groq-431005", # replace with your virtual key for Groq AI + provider="@groq-431005", # replace with your virtual key for Groq AI ) ``` diff --git a/guides/integrations/llama-3-on-portkey-+-together-ai.mdx b/guides/integrations/llama-3-on-portkey-+-together-ai.mdx index f6b33083..7bc71744 100644 --- a/guides/integrations/llama-3-on-portkey-+-together-ai.mdx +++ b/guides/integrations/llama-3-on-portkey-+-together-ai.mdx @@ -66,7 +66,7 @@ portkey = Portkey( api_key = 'PORTKEY_API_KEY', ## Grab from https://app.portkey.ai/ - virtual_key= "together-virtual-key" ## Grab from https://api.together.xyz/ and add to Portkey Virtual Keys + provider="@together-virtual-key" ## Grab from https://api.together.xyz/ and add to Portkey Virtual Keys ) diff --git a/guides/integrations/mixtral-8x22b.mdx b/guides/integrations/mixtral-8x22b.mdx index 17d3b190..fc9cc72c 100644 --- a/guides/integrations/mixtral-8x22b.mdx +++ b/guides/integrations/mixtral-8x22b.mdx @@ -94,7 +94,7 @@ portkey = Portkey( api_key = userdata.get('PORTKEY_API_KEY'), # replace with your Portkey API key - virtual_key= "together-1c20e9", # replace with your virtual key for Together AI + provider="@together-1c20e9", # replace with your virtual key for Together AI ) ``` diff --git a/guides/integrations/segmind.mdx b/guides/integrations/segmind.mdx index b6626e56..4574e40e 100644 --- a/guides/integrations/segmind.mdx +++ b/guides/integrations/segmind.mdx @@ -138,7 +138,7 @@ portkey = Portkey( api_key= userdata.get('PORTKEY_API_KEY'), - virtual_key= "segmind-e63290" + provider="@segmind-e63290" ) ``` @@ -180,7 +180,7 @@ portkey = Portkey( config="pc-segmin-ab3d5d", # Config key, Generated when you create a config - virtual_key= "test-segmind-643f94" + provider="@test-segmind-643f94" ) ``` diff --git a/guides/use-cases/deepseek-r1.mdx b/guides/use-cases/deepseek-r1.mdx index 5bd88925..8030e783 100644 --- a/guides/use-cases/deepseek-r1.mdx +++ b/guides/use-cases/deepseek-r1.mdx @@ -29,7 +29,7 @@ from portkey_ai import Portkey # Initialize Portkey client client = Portkey( api_key="your-portkey-api-key", - virtual_key="provider-virtual-key" # Just change this to switch providers + provider="@provider-virtual-key" # Just change this to switch providers ) # Make completion call - same code for all providers @@ -84,7 +84,7 @@ Here's how you can use Portkey's unified API ```python client = Portkey( api_key="your-portkey-api-key", - virtual_key="your-virtual-key--for-chosen-provider" + provider="@your-virtual-key--for-chosen-provider" ) response = client.chat.completions.create( diff --git a/guides/use-cases/enforcing-json-schema-with-anyscale-and-together.mdx b/guides/use-cases/enforcing-json-schema-with-anyscale-and-together.mdx index 8cc535c0..58efecb3 100644 --- a/guides/use-cases/enforcing-json-schema-with-anyscale-and-together.mdx +++ b/guides/use-cases/enforcing-json-schema-with-anyscale-and-together.mdx @@ -55,7 +55,7 @@ from pydantic import BaseModel, Field portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="ANYSCALE_VIRTUAL_KEY" # OR #TOGETHER_VIRTUAL_KEY + provider="@ANYSCALE_VIRTUAL_KEY" # OR #TOGETHER_VIRTUAL_KEY ) class Recipe(BaseModel): diff --git a/guides/use-cases/openai-computer-use.mdx b/guides/use-cases/openai-computer-use.mdx index 3049975f..1a8ec284 100644 --- a/guides/use-cases/openai-computer-use.mdx +++ b/guides/use-cases/openai-computer-use.mdx @@ -63,7 +63,7 @@ Replace the standard OpenAI client with Portkey's client, configuring it with yo ```python client = Portkey( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_OPENAI_VIRTUAL_KEY", # Virtual key containing your OpenAI credentials + provider="@YOUR_OPENAI_VIRTUAL_KEY", # Virtual key containing your OpenAI credentials ) ``` @@ -248,7 +248,7 @@ from playwright.sync_api import sync_playwright # Initialize Portkey client client = Portkey( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + provider="@YOUR_OPENAI_VIRTUAL_KEY", ) def handle_model_action(page, action): diff --git a/guides/use-cases/setup-openai-greater-than-azure-openai-fallback.mdx b/guides/use-cases/setup-openai-greater-than-azure-openai-fallback.mdx index bc390929..1e62a694 100644 --- a/guides/use-cases/setup-openai-greater-than-azure-openai-fallback.mdx +++ b/guides/use-cases/setup-openai-greater-than-azure-openai-fallback.mdx @@ -110,31 +110,19 @@ config_data = { }, 'targets': [ - { - 'virtual_key': OPENAI_VIRTUAL_KEY, - }, - { - 'virtual_key': AZURE_OPENAI_VIRTUAL_KEY, - - }, - - ], - + } + ] } portkey = Portkey( - api_key=PORTKEYAI_API_KEY, - virtual_key=OPENAI_VIRTUAL_KEY, - config=json.dumps(config_data) - ) ``` @@ -148,33 +136,19 @@ Fallback Configs without virtual keys ```js { - "strategy": { - "mode": "fallback" - }, - "targets": [ - { - "provider": "openai", - "api_key": "sk-xxxxxxxxpRT4xxxx5" - }, - { - "provider": "azure-openai", - "api_key": "*******" - } - ] - } ``` @@ -182,27 +156,17 @@ Fallback Configs without virtual keys All the requests will hit OpenAI since Portkey proxies all those requests to the target(s) we already specified. Notice that the changes to the requests do not demand any code changes in the business logic implementation. Smooth! - - ```js messages = [ - { - "role": "user", - "content": "What are the 7 wonders of the world?" - } - ] response = portkey.chat.completions.create( - messages = messages, - model = 'gpt-3.5-turbo' - ) print(response.choices[0].message.content) # Here is the plan @@ -215,14 +179,10 @@ When OpenAI returns any 4xx or 5xx errors, Portkey will automatically switch to Since all the requests go through Portkey, Portkey can log them for better observability of your app. You can find the specific requests by passing an _trace ID_. It can be any desired string name. In this case, `my-trace-id` - ```js response = portkey.with_options(trace_id="").chat.completions.create( - messages = messages, - model = 'gpt-4' - ) print(response.choices[0].message.content) @@ -240,15 +200,11 @@ Portkey provides finer control over the when it should apply fallback strategy t ```js from portkey_ai import Portkey - from google.colab import userdata - import json PORTKEYAI_API_KEY=userdata.get('PORTKEY_API_KEY') - OPENAI_VIRTUAL_KEY=userdata.get('OPENAI_VIRTUAL_KEY') - AZURE_OPENAI_VIRTUAL_KEY=userdata.get('AZURE_OPENAI_VIRTUAL_KEY') config_data = { diff --git a/guides/use-cases/track-costs-using-metadata.mdx b/guides/use-cases/track-costs-using-metadata.mdx index 29a8b0e7..380f6c5c 100644 --- a/guides/use-cases/track-costs-using-metadata.mdx +++ b/guides/use-cases/track-costs-using-metadata.mdx @@ -37,7 +37,7 @@ from portkey import Portkey portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="OPENAI_VIRTUAL_KEY" + provider="@OPENAI_VIRTUAL_KEY" ) diff --git a/integrations/agents/autogen.mdx b/integrations/agents/autogen.mdx index 5e6fb1b0..ae297ed1 100644 --- a/integrations/agents/autogen.mdx +++ b/integrations/agents/autogen.mdx @@ -66,7 +66,7 @@ config = [ } ] ``` -To switch to Azure as your provider, add your Azure details to Portley vault ([here's how](/integrations/llms/azure-openai)) and use Azure OpenAI using virtual keys +To switch to Azure as your provider, just create a new Azure integration ([here's how](/integrations/llms/azure-openai)) and use the new Azure OpenAI provider. ```py config = [ @@ -77,8 +77,7 @@ config = [ "api_type": "openai", # Portkey conforms to the openai api_type "default_headers": createHeaders( api_key ="PORTKEY_API_KEY", #Replace with Your Portkey API key - provider = "azure-openai", - virtual_key="AZURE_VIRTUAL_KEY" # Replace with Azure Virtual Key + provider="@azure-openai" ) } ] @@ -89,7 +88,7 @@ If you are using Anthropic with CrewAI, your code would look like this: ```py config = [ { - "api_key": "ANTHROPIC_VIRTUAL_KEY", + "api_key": "ANTHROPIC_PROVIDER", "model": "gpt-3.5-turbo", "api_type": "openai", # Portkey conforms to the openai api_type "base_url": PORTKEY_GATEWAY_URL, @@ -100,19 +99,18 @@ config = [ } ] ``` -To switch to AWS Bedrock as your provider, add your AWS Bedrock details to Portley vault ([here's how](/integrations/llms/aws-bedrock)) and use AWS Bedrock using virtual keys, +To switch to AWS Bedrock as your provider, create a new AWS Bedrock integration ([here's how](/integrations/llms/aws-bedrock)) and use the new AWS Bedrock provider. ```py config = [ { - "api_key": "api-key", #We are using Virtual Key + "api_key": "api-key", "model": "gpt-3.5-turbo", "api_type": "openai", # Portkey conforms to the openai api_type "base_url": PORTKEY_GATEWAY_URL, "default_headers": createHeaders( api_key ="PORTKEY_API_KEY", #Replace with Your Portkey API key - provider = "bedrock", - virtual_key="AWS_VIRTUAL_API_KEY" # Replace with Virtual Key + provider="@AWS_VIRTUAL_API_KEY" ) } ] diff --git a/integrations/agents/bring-your-own-agents.mdx b/integrations/agents/bring-your-own-agents.mdx index 0611978b..c563fea0 100644 --- a/integrations/agents/bring-your-own-agents.mdx +++ b/integrations/agents/bring-your-own-agents.mdx @@ -21,7 +21,7 @@ client = OpenAI( default_headers=createHeaders( provider="openai", api_key="PORTKEY_API_KEY", - virtual_key="openai-latest-a4a53d" + provider="@openai-latest-a4a53d" ) ) ``` @@ -57,18 +57,16 @@ client = OpenAI( ) ``` -To switch to Azure as your provider, add your Azure details to Portley vault ([here's how](/integrations/llms/azure-openai)) and use Azure OpenAI using virtual keys +To switch to Azure as your provider, just create a new Azure integration ([here's how](/integrations/llms/azure-openai)) and use the Azure OpenAI provider. ```py client = OpenAI( - api_key="API_KEY", #We will use Virtual Key in this + api_key="PORTKEY_API_KEY", base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - provider="azure-openai", - api_key="PORTKEY_API_KEY", - virtual_key="AZURE_VIRTUAL_KEY" #Azure Virtual key + provider="@AZURE_PROVIDER" ) ) ``` @@ -87,18 +85,16 @@ client = OpenAI( ) ``` -To switch to AWS Bedrock as your provider, add your AWS Bedrock details to Portley vault ([here's how](/integrations/llms/aws-bedrock)) and use AWS Bedrock using virtual keys, +To switch to AWS Bedrock as your provider, just create a new AWS Bedrock integration ([here's how](/integrations/llms/aws-bedrock)) and use the AWS Bedrock provider. ```py client = OpenAI( - api_key="api_key", #We will use Virtual Key in this + api_key="PORTKEY_API_KEY", base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - provider="bedrock", - api_key="PORTKEY_API_KEY", - virtual_key="AWS_VIRTUAL_KEY" #Bedrock Virtual Key + provider="@AWS_PROVIDER" ) ) ``` @@ -140,12 +136,11 @@ Portkey automatically logs comprehensive metrics for your AI agents, including * ```py llm2 = ChatOpenAI( - api_key="Anthropic_API_Key", + api_key="PORTKEY_API_KEY", base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - api_key="PORTKEY_API_KEY", - provider="anthropic", - trace_id="research_agent1" #Add individual trace-id for your agent analytics + provider="@anthropic", + trace_id="research_agent1" ) ) ``` diff --git a/integrations/agents/control-flow.mdx b/integrations/agents/control-flow.mdx index 56748ab9..554cf4d2 100644 --- a/integrations/agents/control-flow.mdx +++ b/integrations/agents/control-flow.mdx @@ -60,18 +60,16 @@ llm = ChatOpenAI( ) ``` -To switch to Azure as your provider, add your Azure details to Portley vault ([here's how](/integrations/llms/azure-openai)) and use Azure OpenAI using virtual keys +To switch to Azure as your provider, just create a new Azure integration ([here's how](/integrations/llms/azure-openai)) and use the Azure OpenAI provider. ```py llm = ChatOpenAI( - api_key="api-key", + api_key="PORTKEY_API_KEY", base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - provider="azure-openai", #choose your provider - api_key="PORTKEY_API_KEY", - virtual_key="AZURE_VIRTUAL_KEY" # Replace with your virtual key for Azure + provider="@AZURE_PROVIDER" ) ) ``` @@ -83,27 +81,24 @@ If you are using Anthropic with CrewAI, your code would look like this: ```py llm = ChatOpenAI( - api_key="ANTHROPIC_API_KEY", + api_key="PORTKEY_API_KEY", base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - provider="anthropic", #choose your provider - api_key="PORTKEY_API_KEY" + provider="@anthropic" ) ) ``` -To switch to AWS Bedrock as your provider, add your AWS Bedrock details to Portley vault ([here's how](/integrations/llms/aws-bedrock)) and use AWS Bedrock using virtual keys, +To switch to AWS Bedrock as your provider, just create a new AWS Bedrock integration ([here's how](/integrations/llms/aws-bedrock)) and use the AWS Bedrock provider. ```py llm = ChatOpenAI( - api_key="api-key", + api_key="PORTKEY_API_KEY", base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - provider="bedrock", #choose your provider - api_key="PORTKEY_API_KEY", - virtual_key="AWS_Bedrock_VIRTUAL_KEY" # Replace with your virtual key for Bedrock + provider="@AWS_Bedrock_PROVIDER" ) ) ``` @@ -150,12 +145,11 @@ Portkey automatically logs comprehensive metrics for your AI agents, including * ```py llm2 = ChatOpenAI( - api_key="Anthropic_API_Key", + api_key="PORTKEY_API_KEY", base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - api_key="PORTKEY_API_KEY", - provider="anthropic", - trace_id="research_agent1" #Add individual trace-id for your agent analytics + provider="@anthropic", + trace_id="research_agent1" ) ) diff --git a/integrations/agents/crewai.mdx b/integrations/agents/crewai.mdx index 263a86d9..f380e37a 100644 --- a/integrations/agents/crewai.mdx +++ b/integrations/agents/crewai.mdx @@ -44,10 +44,10 @@ from portkey_ai import createHeaders, PORTKEY_GATEWAY_URL gpt_llm = LLM( model="gpt-4o", base_url=PORTKEY_GATEWAY_URL, - api_key="dummy", # We are using a Virtual key, so this is a placeholder + api_key="dummy", extra_headers=createHeaders( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_LLM_VIRTUAL_KEY", + provider="@YOUR_LLM_PROVIDER", trace_id="unique-trace-id", # Optional, for request tracing ) ) @@ -64,10 +64,6 @@ gpt_llm = LLM( ) ``` - - -**What are Virtual Keys?** Virtual keys in Portkey securely store your LLM provider API keys (OpenAI, Anthropic, etc.) in an encrypted vault. They allow for easier key rotation and budget management. [Learn more about virtual keys here](/product/ai-gateway/virtual-keys). - @@ -93,7 +89,7 @@ portkey_llm = LLM( api_key="dummy", extra_headers=createHeaders( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + provider="@YOUR_OPENAI_PROVIDER", trace_id="unique-session-id" # Add unique trace ID ) ) @@ -144,7 +140,7 @@ portkey_llm = LLM( api_key="dummy", extra_headers=createHeaders( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + provider="@YOUR_OPENAI_PROVIDER", metadata={ "crew_type": "research_crew", "environment": "production", @@ -268,7 +264,7 @@ portkey_llm = LLM( api_key="dummy", extra_headers=createHeaders( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_OPENAI_VIRTUAL_KEY" + provider="@YOUR_OPENAI_PROVIDER" ) ) @@ -362,7 +358,7 @@ portkey_llm = LLM( api_key="dummy", extra_headers=createHeaders( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + provider="@YOUR_OPENAI_PROVIDER", config={ "input_guardrails": ["guardrails-id-xxx", "guardrails-id-yyy"], "output_guardrails": ["guardrails-id-zzz"] @@ -410,7 +406,7 @@ portkey_llm = LLM( api_key="dummy", extra_headers=createHeaders( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + provider="@YOUR_OPENAI_PROVIDER", metadata={ "_user": "user_123", # Special _user field for user analytics "user_tier": "premium", @@ -465,7 +461,7 @@ portkey_llm = LLM( api_key="dummy", extra_headers=createHeaders( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + provider="@YOUR_OPENAI_PROVIDER", config={ "cache": { "mode": "simple" @@ -499,7 +495,7 @@ portkey_llm = LLM( api_key="dummy", extra_headers=createHeaders( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + provider="@YOUR_OPENAI_PROVIDER", config={ "cache": { "mode": "semantic" @@ -537,7 +533,7 @@ openai_llm = LLM( api_key="dummy", extra_headers=createHeaders( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_OPENAI_VIRTUAL_KEY" + provider="@YOUR_OPENAI_PROVIDER" ) ) @@ -548,7 +544,7 @@ anthropic_llm = LLM( api_key="dummy", extra_headers=createHeaders( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_ANTHROPIC_VIRTUAL_KEY" + provider="@YOUR_ANTHROPIC_PROVIDER" ) ) @@ -589,22 +585,8 @@ If you are using CrewAI inside your organization, you need to consider several g Portkey adds a comprehensive governance layer to address these enterprise needs. Let's implement these controls step by step. - -Virtual Keys are Portkey's secure way to manage your LLM provider API keys. They provide essential controls like: -- Budget limits for API usage -- Rate limiting capabilities -- Secure API key storage - -To create a virtual key: -Go to [Virtual Keys](https://app.portkey.ai/virtual-keys) in the Portkey App. Save and copy the virtual key ID - - - - - - -Save your virtual key ID - you'll need it for the next step. - + +Go to [Integrations](https://app.portkey.ai/integrations) in the Portkey App, choose your LLM and connect to it. Save and copy the generated provider ID. @@ -615,7 +597,7 @@ To create your config: 2. Create new config with: ```json { - "virtual_key": "YOUR_VIRTUAL_KEY_FROM_STEP1", + "provider":"@YOUR_PROVIDER_FROM_STEP1", "override_params": { "model": "gpt-4o" // Your preferred model name } @@ -670,19 +652,16 @@ researcher = Agent( ### Step 1: Implement Budget Controls & Rate Limits -Virtual Keys enable granular control over LLM access at the team/department level. This helps you: +Enable granular control over LLM access at the team/department level. This helps you: - Set up [budget limits](/product/ai-gateway/virtual-keys/budget-limits) - Prevent unexpected usage spikes using Rate limits - Track departmental spending #### Setting Up Department-Specific Controls: -1. Navigate to [Virtual Keys](https://app.portkey.ai/virtual-keys) in Portkey dashboard -2. Create new Virtual Key for each department with budget limits and rate limits +1. Navigate to [Integrations](https://app.portkey.ai/integrations) in Portkey dashboard and create a new LLM integration +2. Provision this integration for each department with budget limits and rate limits 3. Configure department-specific limits - - - @@ -705,7 +684,7 @@ Here's a basic configuration to route requests to OpenAI, specifically using GPT }, "targets": [ { - "virtual_key": "YOUR_OPENAI_VIRTUAL_KEY", + "provider":"@YOUR_OPENAI_PROVIDER", "override_params": { "model": "gpt-4o" } @@ -725,7 +704,7 @@ Here's a basic configuration to route requests to OpenAI, specifically using GPT ### Step 3: Implement Access Controls Create User-specific API keys that automatically: - - Track usage per user/team with the help of virtual keys + - Track usage per user/team with metadata - Apply appropriate configs to route requests - Collect relevant metadata to filter logs - Enforce access permissions @@ -805,7 +784,7 @@ Here's a basic configuration to route requests to OpenAI, specifically using GPT - Yes! Portkey uses your own API keys for the various LLM providers. It securely stores them as virtual keys, allowing you to easily manage and rotate keys without changing your code. + Yes! Portkey uses your own API keys for the various LLM providers. It securely stores them, allowing you to easily manage and rotate keys without changing your code. diff --git a/integrations/agents/langchain-agents.mdx b/integrations/agents/langchain-agents.mdx index fed5c50c..7d39efc1 100644 --- a/integrations/agents/langchain-agents.mdx +++ b/integrations/agents/langchain-agents.mdx @@ -59,17 +59,15 @@ llm = ChatOpenAI( ) ``` -To switch to Azure as your provider, add your Azure details to Portley vault ([here's how](/integrations/llms/azure-openai)) and use Azure OpenAI using virtual keys +To switch to Azure as your provider, add your Azure details to Portley and create an integration ([here's how](/integrations/llms/azure-openai)). ```py llm = ChatOpenAI( - api_key="api-key", + api_key="PORTKEY_API_KEY", base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - provider="azure-openai", #choose your provider - api_key="PORTKEY_API_KEY", - virtual_key="AZURE_VIRTUAL_KEY" # Replace with your virtual key for Azure + provider="@AZURE_PROVIDER" ) ) ``` @@ -79,23 +77,20 @@ If you are using Anthropic with CrewAI, your code would look like this: ```py llm = ChatOpenAI( - api_key="ANTHROPIC_API_KEY", + api_key="PORTKEY_API_KEY", base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - provider="anthropic", #choose your provider - api_key="PORTKEY_API_KEY" + provider="@anthropic" ) ) ``` -To switch to AWS Bedrock as your provider, add your AWS Bedrock details to Portley vault ([here's how](/integrations/llms/aws-bedrock)) and use AWS Bedrock using virtual keys, +To switch to AWS Bedrock as your provider, add your AWS Bedrock details to Portley and create an integration ([here's how](/integrations/llms/aws-bedrock)). ```py llm = ChatOpenAI( - api_key="api-key", + api_key="PORTKEY_API_KEY", base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - provider="bedrock", #choose your provider - api_key="PORTKEY_API_KEY", - virtual_key="AWS_Bedrock_VIRTUAL_KEY" # Replace with your virtual key for Bedrock + provider="@AWS_Bedrock_PROVIDER" ) ) ``` @@ -139,11 +134,10 @@ Portkey automatically logs comprehensive metrics for your AI agents, including * ```py llm2 = ChatOpenAI( - api_key="Anthropic_API_Key", + api_key="PORTKEY_API_KEY", base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - api_key="PORTKEY_API_KEY", - provider="anthropic", + provider="@anthropic", trace_id="research_agent1" #Add individual trace-id for your agent analytics ) ) diff --git a/integrations/agents/langgraph.mdx b/integrations/agents/langgraph.mdx index b7f32c65..8d47cc9e 100644 --- a/integrations/agents/langgraph.mdx +++ b/integrations/agents/langgraph.mdx @@ -50,7 +50,7 @@ llm = ChatOpenAI( base_url="https://api.portkey.ai/v1", default_headers=createHeaders( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_PROVIDER_VIRTUAL_KEY", + provider="@YOUR_PROVIDER", trace_id="unique-trace-id", # Optional, for request tracing metadata={ # Optional, for request segmentation "app_env": "production", @@ -59,10 +59,6 @@ llm = ChatOpenAI( ) ) ``` - - -**What are Virtual Keys?** Virtual keys in Portkey securely store your LLM provider API keys (OpenAI, Anthropic, etc.) in an encrypted vault. They allow for easier key rotation and budget management. [Learn more about virtual keys here](/product/ai-gateway/virtual-keys). - @@ -91,7 +87,7 @@ llm = ChatOpenAI( base_url="https://api.portkey.ai/v1", default_headers=createHeaders( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_PROVIDER_VIRTUAL_KEY", + provider="@YOUR_PROVIDER", trace_id="chat-session-123" # Optional ) ) @@ -163,7 +159,7 @@ llm = ChatOpenAI( base_url="https://api.portkey.ai/v1", default_headers=createHeaders( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_PROVIDER_VIRTUAL_KEY", + provider="@YOUR_PROVIDER", trace_id="search-agent-session" # Optional ) ) @@ -233,7 +229,7 @@ llm = ChatOpenAI( base_url="https://api.portkey.ai/v1", default_headers=createHeaders( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_PROVIDER_VIRTUAL_KEY", + provider="@YOUR_PROVIDER", ) ) llm_with_tools = llm.bind_tools(tools) @@ -287,7 +283,7 @@ llm = ChatOpenAI( base_url="https://api.portkey.ai/v1", default_headers=createHeaders( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_PROVIDER_VIRTUAL_KEY", + provider="@YOUR_PROVIDER", trace_id="memory-agent-session" # Optional ) ) @@ -352,7 +348,7 @@ llm = ChatOpenAI( base_url="https://api.portkey.ai/v1", default_headers=createHeaders( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_LLM_PROVIDER_VIRTUAL_KEY", + provider="@YOUR_LLM_PROVIDER", trace_id="unique-session-id", # Add unique trace ID metadata={"request_type": "user_query"} ) @@ -405,7 +401,7 @@ llm = ChatOpenAI( base_url="https://api.portkey.ai/v1", default_headers=createHeaders( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_LLM_PROVIDER_VIRTUAL_KEY", + provider="@YOUR_LLM_PROVIDER", metadata={ "agent_type": "search_agent", "environment": "production", @@ -441,11 +437,11 @@ llm = ChatOpenAI( }, "targets": [ { - "virtual_key": "YOUR_OPENAI_VIRTUAL_API_KEY", + "provider":"@YOUR_OPENAI_VIRTUAL_API_KEY", "override_params": {"model": "gpt-4o"} }, { - "virtual_key": "YOUR_ANTHROPIC_VIRTUAL_API_KEY", + "provider":"@YOUR_ANTHROPIC_VIRTUAL_API_KEY", "override_params": {"model": "claude-3-opus-20240229"} } ] @@ -519,7 +515,7 @@ llm = ChatOpenAI( base_url="https://api.portkey.ai/v1", default_headers=createHeaders( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + provider="@YOUR_OPENAI_PROVIDER", ) ) @@ -610,7 +606,7 @@ llm = ChatOpenAI( base_url="https://api.portkey.ai/v1", default_headers=createHeaders( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + provider="@YOUR_OPENAI_PROVIDER", config={ "input_guardrails": ["guardrails-id-xxx", "guardrails-id-yyy"], "output_guardrails": ["guardrails-id-zzz"] @@ -648,7 +644,7 @@ llm = ChatOpenAI( base_url="https://api.portkey.ai/v1", default_headers=createHeaders( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + provider="@YOUR_OPENAI_PROVIDER", metadata={ "_user": "user_123", # Special _user field for user analytics "user_tier": "premium", @@ -694,7 +690,7 @@ llm = ChatOpenAI( base_url="https://api.portkey.ai/v1", default_headers=createHeaders( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + provider="@YOUR_OPENAI_PROVIDER", config={ "cache": { "mode": "simple" @@ -718,7 +714,7 @@ llm = ChatOpenAI( base_url="https://api.portkey.ai/v1", default_headers=createHeaders( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + provider="@YOUR_OPENAI_PROVIDER", config={ "cache": { "mode": "semantic" @@ -746,7 +742,7 @@ openai_llm = ChatOpenAI( base_url="https://api.portkey.ai/v1", default_headers=createHeaders( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_OPENAI_VIRTUAL_KEY" + provider="@YOUR_OPENAI_PROVIDER" ) ) @@ -756,7 +752,7 @@ anthropic_llm = ChatOpenAI( base_url="https://api.portkey.ai/v1", default_headers=createHeaders( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_ANTHROPIC_VIRTUAL_KEY" + provider="@YOUR_ANTHROPIC_PROVIDER" ) ) @@ -795,25 +791,15 @@ If you are using LangGraph inside your organization, you need to consider severa Portkey adds a comprehensive governance layer to address these enterprise needs. Let's implement these controls step by step. - -Virtual Keys are Portkey's secure way to manage your LLM provider API keys. They provide essential controls like: -- Budget limits for API usage -- Rate limiting capabilities -- Secure API key storage + +To create a new LLM integration: +Go to [Integrations](https://app.portkey.ai/integrations) in the Portkey App. Set budget / rate limits, model access if required and save the integration. -To create a virtual key: -Go to [Virtual Keys](https://app.portkey.ai/virtual-keys) in the Portkey App. Save and copy the virtual key ID - - - - +This creates a "Portkey Provider" that you can then use in any of your Portkey requests without having to send auth details for that LLM provider again. - -Save your virtual key ID - you'll need it for the next step. - - + Configs in Portkey define how your requests are routed, with features like advanced routing, fallbacks, and retries. To create your config: @@ -821,7 +807,7 @@ To create your config: 2. Create new config with: ```json { - "virtual_key": "YOUR_VIRTUAL_KEY_FROM_STEP1", + "provider":"@YOUR_PROVIDER_FROM_STEP1", "override_params": { "model": "gpt-4o" // Your preferred model name } @@ -869,19 +855,15 @@ llm = ChatOpenAI( ### Step 1: Implement Budget Controls & Rate Limits -Virtual Keys enable granular control over LLM access at the team/department level. This helps you: +Integrations enable granular control over LLM access at the team/department level. This helps you: - Set up [budget limits](/product/ai-gateway/virtual-keys/budget-limits) - Prevent unexpected usage spikes using Rate limits - Track departmental spending #### Setting Up Department-Specific Controls: -1. Navigate to [Virtual Keys](https://app.portkey.ai/virtual-keys) in Portkey dashboard -2. Create new Virtual Key for each department with budget limits and rate limits -3. Configure department-specific limits - - - - +1. Navigate to [Integrations](https://app.portkey.ai/integrations) in Portkey dashboard and create a new Integration. +2. Provision this Integration for each department with their budget limits and rate limits +3. Configure model access if required. @@ -904,7 +886,7 @@ Here's a basic configuration to route requests to OpenAI, specifically using GPT }, "targets": [ { - "virtual_key": "YOUR_OPENAI_VIRTUAL_KEY", + "provider":"@YOUR_OPENAI_PROVIDER", "override_params": { "model": "gpt-4o" } @@ -924,7 +906,7 @@ Here's a basic configuration to route requests to OpenAI, specifically using GPT ### Step 3: Implement Access Controls Create User-specific API keys that automatically: - - Track usage per user/team with the help of virtual keys + - Track usage per user/team with the help of metadata - Apply appropriate configs to route requests - Collect relevant metadata to filter logs - Enforce access permissions diff --git a/integrations/agents/livekit.mdx b/integrations/agents/livekit.mdx index e7a1b131..2bbd13fb 100644 --- a/integrations/agents/livekit.mdx +++ b/integrations/agents/livekit.mdx @@ -24,22 +24,14 @@ This guide will walk you through integrating Portkey with LiveKit's STT-LLM-TTS Portkey allows you to use 250+ LLMs with your LiveKit agents, with minimal configuration required. Let's set up the core components in Portkey that you'll need for integration. - -Virtual Keys are Portkey's secure way to manage your LLM provider API keys. For LiveKit integration, you'll need to create a virtual key for OpenAI (or any other LLM provider you prefer). + +To create a new integration: +1. Go to [Integrations](https://app.portkey.ai/integrations) in the Portkey App +2. Click "Add Integration" and select OpenAI +3. Provision workspaces and budget/rate limits if required -To create a virtual key: -1. Go to [Virtual Keys](https://app.portkey.ai/virtual-keys) in the Portkey App -2. Click "Add Virtual Key" and select OpenAI as the provider -3. Add your OpenAI API key -4. Save and copy the virtual key ID +When you create a new integration, Portkey creates a unique provider slug for the integration that you can then use in your requests. - - - - - -Save your virtual key ID - you'll need it for the next step. - @@ -51,7 +43,7 @@ To create your config: 2. Create new config with: ```json { - "virtual_key": "YOUR_VIRTUAL_KEY_FROM_STEP1" + "provider":"@YOUR_PROVIDER_FROM_STEP1" } ``` 3. Save and note the Config ID for the next step @@ -61,7 +53,7 @@ To create your config: -This basic config connects to your virtual key. You can add advanced features like caching, fallbacks, and guardrails later. +This basic config connects to your integration. You can add advanced features like caching, fallbacks, and guardrails later. @@ -107,7 +99,7 @@ llm=openai.LLM(model="gpt-4o", # your preferred model ``` -Make sure your Portkey virtual key has sufficient budget and rate limits for your expected usage. +Make sure your Portkey integration has sufficient budget and rate limits for your expected usage. @@ -221,20 +213,15 @@ Portkey adds a comprehensive governance layer to address these enterprise needs. ### Step 1: Implement Budget Controls & Rate Limits -Virtual Keys enable granular control over LLM access at the team/department level. This helps you: +Integrations enable granular control over LLM access at the team/department level. This helps you: - Set up [budget limits](/product/ai-gateway/virtual-keys/budget-limits) - Prevent unexpected usage spikes using Rate limits - Track departmental spending #### Setting Up Department-Specific Controls: -1. Navigate to [Virtual Keys](https://app.portkey.ai/virtual-keys) in Portkey dashboard -2. Create new Virtual Key for each department with budget limits and rate limits -3. Configure department-specific limits - - - - - +1. Navigate to [Integrations](https://app.portkey.ai/integrations) in Portkey dashboard and create a new Integration +2. Provision the integration to relevant workspaces with their own budgets + @@ -257,7 +244,7 @@ Here's a basic configuration to route requests to OpenAI, specifically using GPT }, "targets": [ { - "virtual_key": "YOUR_OPENAI_VIRTUAL_KEY", + "provider":"@YOUR_OPENAI_PROVIDER", "override_params": { "model": "gpt-4o" } @@ -278,7 +265,7 @@ Configs can be updated anytime to adjust controls without affecting running appl ### Step 3: Implement Access Controls Create User-specific API keys that automatically: -- Track usage per user/team with the help of virtual keys +- Track usage per user/team with the help of metadata - Apply appropriate configs to route requests - Collect relevant metadata to filter logs - Enforce access permissions @@ -366,7 +353,7 @@ Portkey's logging dashboard provides detailed logs for every request made to you ### 3. Unified Access to 1600+ LLMs -You can easily switch between 1600+ LLMs. Call various LLMs such as Anthropic, Gemini, Mistral, Azure OpenAI, Google Vertex AI, AWS Bedrock, and many more by simply changing the `virtual key` in your default `config` object. +You can easily switch between 1600+ LLMs. Call various LLMs such as Anthropic, Gemini, Mistral, Azure OpenAI, Google Vertex AI, AWS Bedrock, and many more by simply changing the `provider` in your default `config` object. ### 4. Advanced Metadata Tracking @@ -440,14 +427,14 @@ Implement real-time protection for your LLM interactions with automatic detectio # FAQs - Yes! Portkey supports 250+ LLMs. Simply create a virtual key for your preferred provider (Anthropic, Google, Cohere, etc.) and update your config accordingly. The LiveKit OpenAI client will work seamlessly with any provider through Portkey. + Yes! Portkey supports 250+ LLMs. Simply integrate your preferred provider (Anthropic, Google, Cohere, etc.) and update your config accordingly. The LiveKit OpenAI client will work seamlessly with any provider through Portkey. Use metadata tags when creating Portkey headers to segment costs: - Add `agent_type`, `department`, or `customer_id` tags - View costs filtered by these tags in the Portkey dashboard - - Set up separate virtual keys with budget limits for each use case + - Set up separate providers with budget limits for each use case @@ -464,7 +451,7 @@ Implement real-time protection for your LLM interactions with automatic detectio Migration is simple: - 1. Create virtual keys and configs in Portkey + 1. Create providers and configs in Portkey 2. Update the OpenAI client initialization to use Portkey's base URL 3. Add Portkey headers with your API key and config 4. No other code changes needed! diff --git a/integrations/agents/llama-agents.mdx b/integrations/agents/llama-agents.mdx index 7df70852..110f3639 100644 --- a/integrations/agents/llama-agents.mdx +++ b/integrations/agents/llama-agents.mdx @@ -68,7 +68,7 @@ llm = OpenAI( ) ) ``` -To switch to Azure as your provider, add your Azure details to Portley vault ([here's how](/integrations/llms/azure-openai)) and use Azure OpenAI using virtual keys +To switch to Azure as your provider, add your Azure details to Portley and create an integration ([here's how](/integrations/llms/azure-openai)). ```py @@ -104,7 +104,7 @@ llm = OpenAI( ) ) ``` -To switch to AWS Bedrock as your provider, add your AWS Bedrock details to Portley vault ([here's how](/integrations/llms/aws-bedrock)) and use AWS Bedrock using virtual keys, +To switch to AWS Bedrock as your provider, add your AWS Bedrock details to Portley and create an integration ([here's how](/integrations/llms/aws-bedrock)). ```py llm_config = { diff --git a/integrations/agents/openai-agents-ts.mdx b/integrations/agents/openai-agents-ts.mdx index dba87cb7..dc94e6e2 100644 --- a/integrations/agents/openai-agents-ts.mdx +++ b/integrations/agents/openai-agents-ts.mdx @@ -54,7 +54,7 @@ const portkey = new OpenAI({ baseURL: PORTKEY_GATEWAY_URL, apiKey: process.env.PORTKEY_API_KEY!, defaultHeaders: createHeaders({ - virtualKey: "YOUR_OPENAI_VIRTUAL_KEY" + provider:"@YOUR_OPENAI_PROVIDER" }) }); @@ -63,10 +63,6 @@ setDefaultOpenAIClient(portkey); setOpenAIAPI('chat_completions'); // Responses API โ†’ Chat setTracingDisabled(true); // Optional: disable OpenAI's tracing ``` - - -**What are Virtual Keys?** Virtual keys in Portkey securely store your LLM provider API keys (OpenAI, Anthropic, etc.) in an encrypted vault. They allow for easier key rotation and budget management. [Learn more about virtual keys here](/product/ai-gateway/virtual-keys). - @@ -85,7 +81,7 @@ const portkey = new OpenAI({ baseURL: PORTKEY_GATEWAY_URL, apiKey: process.env.PORTKEY_API_KEY!, defaultHeaders: createHeaders({ - virtualKey: "YOUR_OPENAI_VIRTUAL_KEY" + provider:"@YOUR_OPENAI_PROVIDER" }) }); @@ -130,7 +126,7 @@ const portkey = new OpenAI({ apiKey: process.env.PORTKEY_API_KEY!, baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ - virtualKey: "YOUR_OPENAI_VIRTUAL_KEY" + provider:"@YOUR_OPENAI_PROVIDER" }) }); setDefaultOpenAIClient(portkey); @@ -201,7 +197,7 @@ const portkey = new OpenAI({ apiKey: process.env.PORTKEY_API_KEY!, defaultHeaders: createHeaders({ traceId: "unique_execution_trace_id", // Add unique trace ID - virtualKey: "YOUR_OPENAI_VIRTUAL_KEY" + provider:"@YOUR_OPENAI_PROVIDER" }) }); setDefaultOpenAIClient(portkey); @@ -253,7 +249,7 @@ const portkey = new OpenAI({ apiKey: process.env.PORTKEY_API_KEY!, defaultHeaders: createHeaders({ metadata: {"agent_type": "research_agent"}, // Add custom metadata - virtualKey: "YOUR_OPENAI_VIRTUAL_KEY" + provider:"@YOUR_OPENAI_PROVIDER" }) }); setDefaultOpenAIClient(portkey); @@ -371,7 +367,7 @@ const openaiClient = new OpenAI({ baseURL: PORTKEY_GATEWAY_URL, apiKey: process.env.PORTKEY_API_KEY!, defaultHeaders: createHeaders({ - virtualKey: "YOUR_OPENAI_VIRTUAL_KEY" + provider:"@YOUR_OPENAI_PROVIDER" }) }); setDefaultOpenAIClient(openaiClient); @@ -462,7 +458,7 @@ import { setDefaultOpenAIClient } from '@openai/agents'; // Create a config with input and output guardrails, It's recommended you create Config in Portkey App and pass the config ID in the client const config = { - "virtual_key": "openai-xxx", + "provider":"@openai-xxx", "input_guardrails": ["guardrails-id-xxx", "guardrails-id-yyy"], "output_guardrails": ["guardrails-id-xxx"] }; @@ -473,7 +469,7 @@ const portkey = new OpenAI({ apiKey: process.env.PORTKEY_API_KEY!, defaultHeaders: createHeaders({ config: config, - virtualKey: "YOUR_OPENAI_VIRTUAL_KEY" + provider:"@YOUR_OPENAI_PROVIDER" }) }); setDefaultOpenAIClient(portkey); @@ -508,7 +504,7 @@ const portkey = new OpenAI({ baseURL: PORTKEY_GATEWAY_URL, apiKey: process.env.PORTKEY_API_KEY!, defaultHeaders: createHeaders({ - virtualKey: "YOUR_LLM_PROVIDER_VIRTUAL_KEY", + provider:"@YOUR_LLM_PROVIDER", metadata: { "_user": "user_123", // Special _user field for user analytics "user_name": "John Doe", @@ -549,7 +545,7 @@ const portkeyConfig = { "cache": { "mode": "simple" }, - "virtual_key": "YOUR_LLM_PROVIDER_VIRTUAL_KEY" + "provider":"@YOUR_LLM_PROVIDER" }; // Configure OpenAI client with chosen provider @@ -572,7 +568,7 @@ const portkeyConfig = { "cache": { "mode": "semantic" }, - "virtual_key": "YOUR_LLM_PROVIDER_VIRTUAL_KEY" + "provider":"@YOUR_LLM_PROVIDER" }; // Configure OpenAI client with chosen provider @@ -676,7 +672,7 @@ Portkey adds a comprehensive governance layer to address these enterprise needs. Portkey allows you to use 1600+ LLMs with your OpenAI Agents setup, with minimal configuration required. Let's set up the core components in Portkey that you'll need for integration. - + Virtual Keys are Portkey's secure way to manage your LLM provider API keys. Think of them like disposable credit cards for your LLM API keys, providing essential controls like: - Budget limits for API usage - Rate limiting capabilities @@ -705,7 +701,7 @@ To create your config: 2. Create new config with: ```json { - "virtual_key": "YOUR_VIRTUAL_KEY_FROM_STEP1", + "provider":"@YOUR_PROVIDER_FROM_STEP1", "override_params": { "model": "gpt-4o" // Your preferred model name } @@ -798,7 +794,7 @@ Here's a basic configuration to route requests to OpenAI, specifically using GPT }, "targets": [ { - "virtual_key": "YOUR_OPENAI_VIRTUAL_KEY", + "provider":"@YOUR_OPENAI_PROVIDER", "override_params": { "model": "gpt-4o" } @@ -818,7 +814,7 @@ Configs can be updated anytime to adjust controls without affecting running appl ### Step 3: Implement Access Controls Create User-specific API keys that automatically: -- Track usage per user/team with the help of virtual keys +- Track usage per user/team with the help of metadata - Apply appropriate configs to route requests - Collect relevant metadata to filter logs - Enforce access permissions @@ -902,7 +898,7 @@ Monitor usage in Portkey dashboard: - Yes! Portkey uses your own API keys for the various LLM providers. It securely stores them as virtual keys, allowing you to easily manage and rotate keys without changing your code. + Yes! Portkey uses your own API keys for the various LLM providers. It securely stores them, allowing you to easily manage and rotate keys without changing your code. diff --git a/integrations/agents/openai-agents.mdx b/integrations/agents/openai-agents.mdx index 1288ca06..6184af47 100644 --- a/integrations/agents/openai-agents.mdx +++ b/integrations/agents/openai-agents.mdx @@ -58,7 +58,7 @@ portkey = AsyncOpenAI( base_url=PORTKEY_GATEWAY_URL, api_key=os.environ["PORTKEY_API_KEY"], default_headers=createHeaders( - virtual_key="YOUR_OPENAI_VIRTUAL_KEY" + provider="@YOUR_OPENAI_PROVIDER" ) ) @@ -66,10 +66,6 @@ portkey = AsyncOpenAI( set_default_openai_client(portkey, use_for_tracing=False) set_default_openai_api("chat_completions") # Responses API โ†’ Chat ``` - - -**What are Virtual Keys?** Virtual keys in Portkey securely store your LLM provider API keys (OpenAI, Anthropic, etc.) in an encrypted vault. They allow for easier key rotation and budget management. [Learn more about virtual keys here](/product/ai-gateway/virtual-keys). - @@ -92,7 +88,7 @@ portkey = AsyncOpenAI( base_url=PORTKEY_GATEWAY_URL, api_key=os.environ["PORTKEY_API_KEY"], default_headers=createHeaders( - virtual_key="YOUR_OPENAI_VIRTUAL_KEY" + provider="@YOUR_OPENAI_PROVIDER" ) ) @@ -144,7 +140,7 @@ portkey = AsyncOpenAI( base_url=PORTKEY_GATEWAY_URL, api_key=os.environ["PORTKEY_API_KEY"], default_headers=createHeaders( - virtual_key="YOUR_OPENAI_VIRTUAL_KEY" + provider="@YOUR_OPENAI_PROVIDER" ) ) @@ -187,7 +183,7 @@ client = AsyncOpenAI( base_url=PORTKEY_GATEWAY_URL, api_key=os.environ["PORTKEY_API_KEY"], default_headers=createHeaders( - virtual_key="YOUR_OPENAI_VIRTUAL_KEY" + provider="@YOUR_OPENAI_PROVIDER" ) ) @@ -228,7 +224,7 @@ portkey_client = AsyncOpenAI( base_url=PORTKEY_GATEWAY_URL, api_key=os.environ["PORTKEY_API_KEY"], default_headers=createHeaders( - virtual_key="YOUR_OPENAI_VIRTUAL_KEY" + provider="@YOUR_OPENAI_PROVIDER" ) ) @@ -272,7 +268,7 @@ portkey = AsyncOpenAI( api_key=os.environ.get("PORTKEY_API_KEY"), base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - virtual_key="YOUR_OPENAI_VIRTUAL_KEY" + provider="@YOUR_OPENAI_PROVIDER" ) ) set_default_openai_client(portkey) @@ -352,7 +348,7 @@ Portkey provides comprehensive observability for your OpenAI Agents, helping you api_key=os.environ["PORTKEY_API_KEY"], default_headers=createHeaders( trace_id="unique_execution_trace_id", # Add unique trace ID - virtual_key="YOUR_OPENAI_VIRTUAL_KEY" + provider="@YOUR_OPENAI_PROVIDER" ) ) set_default_openai_client(portkey) @@ -406,7 +402,7 @@ Portkey provides comprehensive observability for your OpenAI Agents, helping you api_key=os.environ["PORTKEY_API_KEY"], default_headers=createHeaders( metadata={"agent_type": "research_agent"}, # Add custom metadata - virtual_key="YOUR_OPENAI_VIRTUAL_KEY" + provider="@YOUR_OPENAI_PROVIDER" ) ) set_default_openai_client(portkey) @@ -524,7 +520,7 @@ openai_client = AsyncOpenAI( base_url=PORTKEY_GATEWAY_URL, api_key="YOUR_PORTKEY_API_KEY", default_headers=createHeaders( - virtual_key="YOUR_OPENAI_VIRTUAL_KEY" + provider="@YOUR_OPENAI_PROVIDER" ) ) set_default_openai_client(openai_client) @@ -619,7 +615,7 @@ from agents import set_default_openai_client # Create a config with input and output guardrails, It's recommended you create Config in Portkey App and pass the config ID in the client config = { - "virtual_key": "openai-xxx", + "provider":"@openai-xxx", "input_guardrails": ["guardrails-id-xxx", "guardrails-id-yyy"], "output_guardrails": ["guardrails-id-xxx"] } @@ -630,7 +626,7 @@ portkey = AsyncOpenAI( api_key=os.environ["PORTKEY_API_KEY"], default_headers=createHeaders( config=config, - virtual_key="YOUR_OPENAI_VIRTUAL_KEY" + provider="@YOUR_OPENAI_PROVIDER" ) ) set_default_openai_client(portkey) @@ -665,7 +661,7 @@ portkey = AsyncOpenAI( base_url=PORTKEY_GATEWAY_URL, api_key=os.environ["PORTKEY_API_KEY"], default_headers=createHeaders( - virtual_key="YOUR_LLM_PROVIDER_VIRTUAL_KEY", + provider="@YOUR_LLM_PROVIDER", metadata={ "_user": "user_123", # Special _user field for user analytics "user_name": "John Doe", @@ -713,7 +709,7 @@ Implement caching to make your OpenAI Agents agents more efficient and cost-effe "cache": { "mode": "simple" }, - "virtual_key": "YOUR_LLM_PROVIDER_VIRTUAL_KEY" + "provider":"@YOUR_LLM_PROVIDER" } # Configure OpenAI client with chosen provider @@ -734,7 +730,7 @@ Implement caching to make your OpenAI Agents agents more efficient and cost-effe "cache": { "mode": "semantic" }, - "virtual_key": "YOUR_LLM_PROVIDER_VIRTUAL_KEY" + "provider":"@YOUR_LLM_PROVIDER" } # Configure OpenAI client with chosen provider @@ -994,7 +990,7 @@ To create your config: 2. Create new config with: ```json { - "virtual_key": "YOUR_VIRTUAL_KEY_FROM_STEP1", + "provider":"@YOUR_PROVIDER_FROM_STEP1", "override_params": { "model": "gpt-4o" // Your preferred model name } @@ -1090,7 +1086,7 @@ Here's a basic configuration to route requests to OpenAI, specifically using GPT }, "targets": [ { - "virtual_key": "YOUR_OPENAI_VIRTUAL_KEY", + "provider":"@YOUR_OPENAI_PROVIDER", "override_params": { "model": "gpt-4o" } @@ -1111,7 +1107,7 @@ Configs can be updated anytime to adjust controls without affecting running appl ### Step 3: Implement Access Controls Create User-specific API keys that automatically: -- Track usage per user/team with the help of virtual keys +- Track usage per user/team with the help of metadata - Apply appropriate configs to route requests - Collect relevant metadata to filter logs - Enforce access permissions @@ -1197,7 +1193,7 @@ Monitor usage in Portkey dashboard: - Yes! Portkey uses your own API keys for the various LLM providers. It securely stores them as virtual keys, allowing you to easily manage and rotate keys without changing your code. + Yes! Portkey uses your own API keys for the various LLM providers. It securely stores them, allowing you to easily manage and rotate keys without changing your code. diff --git a/integrations/agents/openai-swarm.mdx b/integrations/agents/openai-swarm.mdx index 687b1ad7..9af3eb34 100644 --- a/integrations/agents/openai-swarm.mdx +++ b/integrations/agents/openai-swarm.mdx @@ -34,7 +34,7 @@ from portkey_ai import Portkey portkey = Portkey( api_key="YOUR_PORTKEY_API_KEY", # defaults to os.environ.get("PORTKEY_API_KEY") - virtual_key="YOUR_VIRTUAL_KEY" + provider="@YOUR_PROVIDER" ) client = Swarm(client=portkey) @@ -73,7 +73,7 @@ from portkey_ai import Portkey portkey = Portkey( api_key="YOUR_PORTKEY_API_KEY", # defaults to os.environ.get("PORTKEY_API_KEY") - virtual_key="YOUR_VIRTUAL_KEY" + provider="@YOUR_PROVIDER" ) client = Swarm(client=portkey) @@ -138,7 +138,7 @@ Instead of managing multiple API keys and provider-specific configurations, Port ```python portkey = Portkey( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="ANTHROPIC_VIRTUAL_KEY" #Just change the virtual key to your preferred LLM provider + provider="@ANTHROPIC_PROVIDER" #Just change the virtual key to your preferred LLM provider ) client = Swarm(client=portkey) @@ -148,7 +148,7 @@ client = Swarm(client=portkey) ```python portkey = Portkey( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="AZURE_OPENAI_VIRTUAL_KEY" #Just change the virtual key to your preferred LLM provider + provider="@AZURE_OPENAI_PROVIDER" #Just change the virtual key to your preferred LLM provider ) client = Swarm(client=portkey) @@ -176,7 +176,7 @@ config = { portkey = Portkey( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_VIRTUAL_KEY", + provider="@YOUR_PROVIDER", config=config ) ``` @@ -232,7 +232,7 @@ Add trace IDs to track specific workflows: ```python portkey = Portkey( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_VIRTUAL_KEY", + provider="@YOUR_PROVIDER", trace_id="weather_workflow_123", metadata={ "agent": "weather_agent", @@ -294,7 +294,7 @@ from portkey_ai import Portkey portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="YOUR_OPENAI_VIRTUAL_KEY" + provider="@YOUR_OPENAI_PROVIDER" ) feedback = portkey.feedback.create( diff --git a/integrations/agents/phidata.mdx b/integrations/agents/phidata.mdx index d8d83937..a0e60887 100644 --- a/integrations/agents/phidata.mdx +++ b/integrations/agents/phidata.mdx @@ -56,7 +56,7 @@ llm = OpenAIChat( ) ) ``` -To switch to Azure as your provider, add your Azure details to Portley vault ([here's how](/integrations/llms/azure-openai)) and use Azure OpenAI using virtual keys +To switch to Azure as your provider, add your Azure details to Portley and create an integration ([here's how](/integrations/llms/azure-openai)). ```py @@ -66,7 +66,7 @@ llm = OpenAIChat( default_headers=createHeaders( provider="azure-openai", api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="AZURE_OPENAI_KEY" + provider="@AZURE_OPENAI_KEY" ) ) ``` @@ -83,7 +83,7 @@ llm = OpenAIChat( ) ) ``` -To switch to AWS Bedrock as your provider, add your AWS Bedrock details to Portley vault ([here's how](/integrations/llms/aws-bedrock)) and use AWS Bedrock using virtual keys, +To switch to AWS Bedrock as your provider, add your AWS Bedrock details to Portley and create an integration ([here's how](/integrations/llms/aws-bedrock)). ```py llm = OpenAIChat( @@ -92,7 +92,7 @@ llm = OpenAIChat( default_headers=createHeaders( provider="bedrock", api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="BEDROCK_OPENAI_KEY" #Bedrock Virtual Key + provider="@BEDROCK_OPENAI_KEY" #Bedrock Virtual Key ) ) ``` diff --git a/integrations/agents/pydantic-ai.mdx b/integrations/agents/pydantic-ai.mdx index a1aeb76d..8020969e 100644 --- a/integrations/agents/pydantic-ai.mdx +++ b/integrations/agents/pydantic-ai.mdx @@ -42,7 +42,7 @@ from portkey_ai import AsyncPortkey # Set up Portkey client with appropriate metadata for tracking portkey_client = AsyncPortkey( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_PROVIDER_VIRTUAL_KEY", # Optional, if using virtual keys + provider="@YOUR_PROVIDER", trace_id="unique-trace-id", # Optional, for request tracing metadata={ # Optional, for request segmentation "app_env": "production", @@ -50,10 +50,6 @@ portkey_client = AsyncPortkey( } ) ``` - - -**What are Virtual Keys?** Virtual keys in Portkey securely store your LLM provider API keys (OpenAI, Anthropic, etc.) in an encrypted vault. They allow for easier key rotation and budget management. [Learn more about virtual keys here](/product/ai-gateway/virtual-keys). - @@ -90,7 +86,7 @@ from pydantic_ai.providers.openai import OpenAIProvider # Set up Portkey client with tracing and metadata portkey_client = AsyncPortkey( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + provider="@YOUR_OPENAI_PROVIDER", trace_id="f1-data-request", metadata={"app_env": "production", "_user": "user_123"} ) @@ -153,7 +149,7 @@ from pydantic_ai.providers.openai import OpenAIProvider # Set up Portkey client portkey_client = AsyncPortkey( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + provider="@YOUR_OPENAI_PROVIDER", trace_id="vision-request", metadata={"request_type": "image_analysis"} ) @@ -191,7 +187,7 @@ from pydantic_ai.providers.openai import OpenAIProvider # Set up Portkey client portkey_client = AsyncPortkey( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + provider="@YOUR_OPENAI_PROVIDER", trace_id="dice-game-session", metadata={"game_type": "dice"} ) @@ -281,7 +277,7 @@ from portkey_ai import AsyncPortkey # Set up Portkey clients with shared trace ID for connected tracing portkey_client = AsyncPortkey( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + provider="@YOUR_OPENAI_PROVIDER", trace_id="flight-booking-session", metadata={"app_type": "flight_booking"} ) @@ -487,7 +483,7 @@ Traces provide a hierarchical view of your agent's execution, showing the sequen # Add trace_id to enable hierarchical tracing in Portkey portkey_client = AsyncPortkey( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_LLM_PROVIDER_VIRTUAL_KEY", + provider="@YOUR_LLM_PROVIDER", trace_id="unique-session-id", # Add unique trace ID metadata={"request_type": "user_query"} ) @@ -534,7 +530,7 @@ Add custom metadata to your PydanticAI agent calls to enable powerful filtering ```python portkey_client = AsyncPortkey( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_LLM_PROVIDER_VIRTUAL_KEY", + provider="@YOUR_LLM_PROVIDER", metadata={ "agent_type": "weather_agent", "environment": "production", @@ -631,7 +627,7 @@ from pydantic_ai.providers.openai import OpenAIProvider # Initialize Portkey clients portkey_admin = Portkey(api_key="YOUR_PORTKEY_API_KEY") -portkey_client = AsyncPortkey(api_key="YOUR_PORTKEY_API_KEY", virtual_key="YOUR_OPENAI_VIRTUAL_KEY") +portkey_client = AsyncPortkey(api_key="YOUR_PORTKEY_API_KEY", provider="@YOUR_OPENAI_PROVIDER") # Retrieve prompt using the render API prompt_data = portkey_admin.prompts.render( @@ -729,7 +725,7 @@ from pydantic_ai.providers.openai import OpenAIProvider # Create Portkey client with guardrails portkey_client = AsyncPortkey( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + provider="@YOUR_OPENAI_PROVIDER", config={ "input_guardrails": ["guardrails-id-xxx", "guardrails-id-yyy"], "output_guardrails": ["guardrails-id-zzz"] @@ -774,7 +770,7 @@ from pydantic_ai.providers.openai import OpenAIProvider # Configure client with user tracking portkey_client = AsyncPortkey( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + provider="@YOUR_OPENAI_PROVIDER", metadata={ "_user": "user_123", # Special _user field for user analytics "user_tier": "premium", @@ -826,7 +822,7 @@ from pydantic_ai.providers.openai import OpenAIProvider # Configure Portkey client with simple caching portkey_client = AsyncPortkey( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + provider="@YOUR_OPENAI_PROVIDER", config={ "cache": { "mode": "simple" @@ -857,7 +853,7 @@ from pydantic_ai.providers.openai import OpenAIProvider # Configure Portkey client with semantic caching portkey_client = AsyncPortkey( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + provider="@YOUR_OPENAI_PROVIDER", config={ "cache": { "mode": "semantic" @@ -892,13 +888,13 @@ from pydantic_ai.providers.openai import OpenAIProvider # OpenAI with Portkey portkey_openai = AsyncPortkey( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_OPENAI_VIRTUAL_KEY" + provider="@YOUR_OPENAI_PROVIDER" ) # Anthropic with Portkey portkey_anthropic = AsyncPortkey( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_ANTHROPIC_VIRTUAL_KEY" + provider="@YOUR_ANTHROPIC_PROVIDER" ) # Create agents with different models @@ -978,7 +974,7 @@ To create your config: 2. Create new config with: ```json { - "virtual_key": "YOUR_VIRTUAL_KEY_FROM_STEP1", + "provider":"@YOUR_PROVIDER_FROM_STEP1", "override_params": { "model": "gpt-4o" // Your preferred model name } @@ -1068,7 +1064,7 @@ Here's a basic configuration to route requests to OpenAI, specifically using GPT }, "targets": [ { - "virtual_key": "YOUR_OPENAI_VIRTUAL_KEY", + "provider":"@YOUR_OPENAI_PROVIDER", "override_params": { "model": "gpt-4o" } @@ -1088,7 +1084,7 @@ Here's a basic configuration to route requests to OpenAI, specifically using GPT ### Step 3: Implement Access Controls Create User-specific API keys that automatically: - - Track usage per user/team with the help of virtual keys + - Track usage per user/team with the help of metadata - Apply appropriate configs to route requests - Collect relevant metadata to filter logs - Enforce access permissions @@ -1168,7 +1164,7 @@ Here's a basic configuration to route requests to OpenAI, specifically using GPT - Yes! Portkey uses your own API keys for the various LLM providers. It securely stores them as virtual keys, allowing you to easily manage and rotate keys without changing your code. + Yes! Portkey uses your own API keys for the various LLM providers. It securely stores them, allowing you to easily manage and rotate keys without changing your code. diff --git a/integrations/agents/strands-backup.mdx b/integrations/agents/strands-backup.mdx index 6fb6d8ee..83022ffc 100644 --- a/integrations/agents/strands-backup.mdx +++ b/integrations/agents/strands-backup.mdx @@ -273,8 +273,8 @@ model = OpenAIModel( "on_status_codes": [429] }, "targets": [ - { "virtual_key": "azure-81fddb" }, - { "virtual_key": "open-ai-key-66a67d" } + { "provider":"@azure-81fddb" }, + { "provider":"@open-ai-key-66a67d" } ] } ) @@ -499,7 +499,7 @@ for attempt in range(3): - Yes! Portkey uses your own API keys for the various LLM providers. It securely stores them as virtual keys, allowing you to easily manage and rotate keys without changing your code. + Yes! Portkey uses your own API keys for the various LLM providers. It securely stores them, allowing you to easily manage and rotate keys without changing your code. diff --git a/integrations/agents/strands.mdx b/integrations/agents/strands.mdx index fb41312e..ae51345d 100644 --- a/integrations/agents/strands.mdx +++ b/integrations/agents/strands.mdx @@ -95,7 +95,7 @@ Go to [Configs](https://app.portkey.ai/configs) to define how requests should be ```json { - "virtual_key": "openai-key-abc123" + "provider":"@openai-key-abc123" } ``` @@ -250,8 +250,8 @@ model = OpenAIModel( "on_status_codes": [429, 503, 502] # Rate limits and server errors }, "targets": [ - { "virtual_key": "openai-key-primary" }, # Try OpenAI first - { "virtual_key": "anthropic-key-backup" } # Fall back to Claude + { "provider":"@openai-key-primary" }, # Try OpenAI first + { "provider":"@anthropic-key-backup" } # Fall back to Claude ] } ) @@ -279,8 +279,8 @@ model = OpenAIModel( config={ "strategy": {"mode": "loadbalance"}, "targets": [ - { "virtual_key": "openai-key-1", "weight": 70 }, - { "virtual_key": "openai-key-2", "weight": 30 } + { "provider":"@openai-key-1", "weight": 70 }, + { "provider":"@openai-key-2", "weight": 30 } ] } ) @@ -317,8 +317,7 @@ model = OpenAIModel( "api_key": "YOUR_PORTKEY_API_KEY", "base_url": PORTKEY_GATEWAY_URL, "default_headers": createHeaders( - provider="anthropic", - api_key="YOUR_ANTHROPIC_KEY" # Can also use virtual keys + provider="@anthropic", # Your Portkey provider ) }, model_id="claude-3-7-sonnet-latest", # Claude model ID @@ -338,7 +337,7 @@ reasoning_model = OpenAIModel( client_args={ "api_key": "YOUR_PORTKEY_API_KEY", "base_url": PORTKEY_GATEWAY_URL, - "default_headers": createHeaders(virtual_key="openai-key") + "default_headers": createHeaders(provider="@openai-key") }, model_id="gpt-4o", params={"temperature": 0.1} # Low temperature for reasoning @@ -348,7 +347,7 @@ creative_model = OpenAIModel( client_args={ "api_key": "YOUR_PORTKEY_API_KEY", "base_url": PORTKEY_GATEWAY_URL, - "default_headers": createHeaders(virtual_key="gemini-key") + "default_headers": createHeaders(provider="@gemini-key") }, model_id="gemini-2.0-flash-exp", params={"temperature": 0.8} # Higher temperature for creativity @@ -413,7 +412,7 @@ def create_model(environment="production"): if environment == "development": # Use faster, cheaper models for development headers = createHeaders( - config={"targets": [{"virtual_key": "openai-dev-key"}]}, + config={"targets": [{"provider":"@openai-dev-key"}]}, metadata={"environment": "dev"} ) model_id = "gpt-4o-mini" @@ -425,8 +424,8 @@ def create_model(environment="production"): config={ "strategy": {"mode": "fallback"}, "targets": [ - {"virtual_key": "openai-prod-key"}, - {"virtual_key": "anthropic-prod-key"} + {"provider":"@openai-prod-key"}, + {"provider":"@anthropic-prod-key"} ] }, metadata={"environment": "prod"} @@ -476,7 +475,7 @@ response1 = agent("What's 2+2?") response2 = agent( "Critical calculation: What's the compound interest?", headers=createHeaders( - config={"targets": [{"virtual_key": "premium-openai-key"}]}, + config={"targets": [{"provider":"@premium-openai-key"}]}, metadata={"priority": "high"} ) ) @@ -577,7 +576,7 @@ print(f"Portkey: {portkey_ai.__version__}") **Problem**: `AuthenticationError` when making requests -**Solution**: Verify your Portkey API key and provider setup: Verify your Portkey API key and provider setup. Test your Portkey API key directly and check for common issues such as wrong API key format, misconfigured provider virtual keys, and missing config attachments. +**Solution**: Verify your Portkey API key and provider setup: Verify your Portkey API key and provider setup. Test your Portkey API key directly and check for common issues such as wrong API key format, misconfigured provider, and missing config attachments. ```python # Test your Portkey API key directly from portkey_ai import Portkey @@ -605,8 +604,8 @@ headers = createHeaders( "on_status_codes": [429, 503, 502, 504] }, "targets": [ - {"virtual_key": "primary-key"}, - {"virtual_key": "backup-key"} + {"provider":"@primary-key"}, + {"provider":"@backup-key"} ] } ) @@ -677,7 +676,7 @@ Also check the Logs section in your Portkey dashboard and filter by your metadat - Yes! Portkey uses your own API keys for the various LLM providers. It securely stores them as virtual keys, allowing you to easily manage and rotate keys without changing your code. + Yes! Portkey uses your own API keys for the various LLM providers. It securely stores them, allowing you to easily manage and rotate keys without changing your code. diff --git a/integrations/cloud/azure.mdx b/integrations/cloud/azure.mdx index b5ab704f..42276c72 100644 --- a/integrations/cloud/azure.mdx +++ b/integrations/cloud/azure.mdx @@ -46,7 +46,7 @@ Expose Portkey configurations as APIM endpoints, ensuring consistent governance 1. **Deploy from the Azure Marketplace**: Launch Portkey directly within your Azure subscription. 2. **Connect Microsoft Entra ID for SSO & SCIM**: Follow our [SSO guide](/product/enterprise-offering/org-management/sso) and [Azure SCIM setup](/product/enterprise-offering/org-management/scim/azure-ad). -3. **Create Virtual Keys in Portkey**: Link your Azure OpenAI resources or Azure AI Foundry model deployments. +3. **Integrate your LLM**: Link your Azure OpenAI resources or Azure AI Foundry model deployments. 4. **Enable Azure Content Safety Guardrails**: Configure content filters within your Portkey Configs. 5. **Instrument Your Applications**: Use your preferred SDK (like the C# example below) to route requests through Portkey. @@ -105,11 +105,11 @@ public class ExampleAzureIntegration // 2. Define Portkey headers // Get your Portkey API Key from https://app.portkey.ai/settings - // Create a virtual key for your Azure OpenAI setup in Portkey: https://app.portkey.ai/virtual-keys + // Integrate your Azure OpenAI setup in Portkey: https://app.portkey.ai/integrations var portkeyHeaders = new Dictionary { { "x-portkey-api-key", "YOUR_PORTKEY_API_KEY" }, - { "x-portkey-virtual-key", "YOUR_AZURE_OPENAI_VIRTUAL_KEY" } // Connects to your Azure setup + { "x-portkey-provider", "YOUR_AZURE_OPENAI_PROVIDER" } // Connects to your Azure setup // Optional: { "x-portkey-trace-id", "my-azure-app-trace" }, // Optional: { "x-portkey-metadata", "{\"userId\": \"user-123\"}" } }; diff --git a/integrations/libraries/anthropic-computer-use.mdx b/integrations/libraries/anthropic-computer-use.mdx index f21b0c02..2bb16472 100644 --- a/integrations/libraries/anthropic-computer-use.mdx +++ b/integrations/libraries/anthropic-computer-use.mdx @@ -14,7 +14,7 @@ For more information on the computer use tool, please refer to the [Anthropic do # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY", + provider="@PROVIDER", strict_open_ai_compliance="false" @@ -65,7 +65,7 @@ For more information on the computer use tool, please refer to the [Anthropic do // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY", // Add your anthropic's virtual key + provider:"@PROVIDER", strictOpenAiCompliance: false }); @@ -255,7 +255,6 @@ Implement real-time protection for your development environment with automatic d Portkey provides several ways to track developer costs: -- Create separate Virtual Keys for each developer - Use metadata tags to identify developers - Set up developer-specific API keys - View detailed analytics in the dashboard diff --git a/integrations/libraries/anythingllm.mdx b/integrations/libraries/anythingllm.mdx index a7a3b85a..7947483e 100644 --- a/integrations/libraries/anythingllm.mdx +++ b/integrations/libraries/anythingllm.mdx @@ -20,36 +20,27 @@ This guide will walk you through integrating Portkey with Anything LLM and setti Portkey allows you to use 1600+ LLMs with your Anything LLM setup, with minimal configuration required. Let's set up the core components in Portkey that you'll need for integration. - -Virtual Keys are Portkey's secure way to manage your LLM provider API keys. Think of them like disposable credit cards for your LLM API keys, providing essential controls like: -- Budget limits for API usage -- Rate limiting capabilities -- Secure API key storage + +Integrate your LLM provider API keys in Portkey. You can: +- Set Budget limits for API usage +- Set Rate limits on requests or tokens +- Provision the integration to specific teams -To create a virtual key: -Go to [Virtual Keys](https://app.portkey.ai/virtual-keys) in the Portkey App. Save and copy the virtual key ID - - - - - - -Save your virtual key ID - you'll need it for the next step. - +When you create an integration, you will get a provider name that you can use in your Anything LLM setup. Configs in Portkey are JSON objects that define how your requests are routed. They help with implementing features like advanced routing, fallbacks, and retries. -We need to create a default config to route our requests to the virtual key created in Step 1. +We need to create a default config to route our requests to the provider created in Step 1. To create your config: 1. Go to [Configs](https://app.portkey.ai/configs) in Portkey dashboard 2. Create new config with: ```json { - "virtual_key": "YOUR_VIRTUAL_KEY_FROM_STEP1", + "provider":"@YOUR_PROVIDER_FROM_STEP1", } ``` 3. Save and note the Config name for the next step @@ -59,7 +50,7 @@ To create your config: -This basic config connects to your virtual key. You can add more advanced portkey features later. +This basic config connects to your provider. You can add more advanced portkey features later. @@ -103,7 +94,7 @@ You need your Portkey API Key from [Step 1](#Getting-started-with-portkey) befor You can monitor your requests and usage in the [Portkey Dashboard](https://app.portkey.ai/dashboard). -Make sure your virtual key has sufficient budget and rate limits for your expected usage. Also use the complete model name given by the provider. +Make sure your provider has sufficient budget and rate limits for your expected usage. Also use the complete model name given by the provider. @@ -124,21 +115,9 @@ Portkey adds a comprehensive governance layer to address these enterprise needs. -### Step 1: Implement Budget Controls & Rate Limits - -Virtual Keys enable granular control over LLM access at the team/department level. This helps you: -- Set up [budget limits](/product/ai-gateway/virtual-keys/budget-limits) -- Prevent unexpected usage spikes using Rate limits -- Track departmental spending - -#### Setting Up Department-Specific Controls: -1. Navigate to [Virtual Keys](https://app.portkey.ai/virtual-keys) in Portkey dashboard -2. Create new Virtual Key for each department with budget limits and rate limits -3. Configure department-specific limits - - - - +### Step 1: Setting Up Department-Specific Controls +1. Navigate to [Integrations](https://app.portkey.ai/integrations) in Portkey dashboard and create one. +2. Provision the integration to relevant Workspaces and add budget/rate limits per workspace as required. @@ -162,7 +141,7 @@ Here's a basic configuration to route requests to OpenAI, specifically using GPT }, "targets": [ { - "virtual_key": "YOUR_OPENAI_VIRTUAL_KEY", + "provider":"@YOUR_OPENAI_PROVIDER", "override_params": { "model": "gpt-4o" } @@ -183,7 +162,7 @@ Configs can be updated anytime to adjust controls without affecting running appl ### Step 3: Implement Access Controls Create User-specific API keys that automatically: -- Track usage per user/team with the help of virtual keys +- Track usage per user/team with the help of metadata - Apply appropriate configs to route requests - Collect relevant metadata to filter logs - Enforce access permissions diff --git a/integrations/libraries/claude-code.mdx b/integrations/libraries/claude-code.mdx index ab1ed1f5..24d25b16 100644 --- a/integrations/libraries/claude-code.mdx +++ b/integrations/libraries/claude-code.mdx @@ -48,7 +48,7 @@ To create a config: 2. Create new config: ```json { - "virtual_key": "YOUR_VIRTUAL_KEY_ID", + "provider":"@YOUR_PROVIDER_ID", "override_params": { "model": "us.anthropic.claude-sonnet-4-20250514-v1:0" } @@ -84,7 +84,7 @@ Edit your Claude Code settings file (`~/.claude/settings.json` for global or `.c { "env": { "ANTHROPIC_BEDROCK_BASE_URL": "https://api.portkey.ai/v1", - "ANTHROPIC_CUSTOM_HEADERS": "x-portkey-api-key: YOUR_PORTKEY_API_KEY\nx-portkey-provider: bedrock\nx-portkey-virtual-key: YOUR_VIRTUAL_KEY", + "ANTHROPIC_CUSTOM_HEADERS": "x-portkey-api-key: YOUR_PORTKEY_API_KEY\nx-portkey-provider: bedrock\nx-portkey-provider: YOUR_PROVIDER", "ANTHROPIC_MODEL": "us.anthropic.claude-sonnet-4-20250514-v1:0", "CLAUDE_CODE_SKIP_BEDROCK_AUTH": 1, "CLAUDE_CODE_USE_BEDROCK": 1 @@ -98,7 +98,7 @@ The `CLAUDE_CODE_SKIP_BEDROCK_AUTH` and `CLAUDE_CODE_USE_BEDROCK` must be set as Replace: - `YOUR_PORTKEY_API_KEY` with your Portkey API key -- `YOUR_VIRTUAL_KEY` with the virtual key ID from Step 1 +- `YOUR_PROVIDER` with the virtual key ID from Step 1 - Update the model name if using a different Claude model @@ -109,7 +109,7 @@ For Vertex AI integration: { "env": { "ANTHROPIC_VERTEX_BASE_URL": "https://api.portkey.ai/v1", - "ANTHROPIC_CUSTOM_HEADERS": "x-portkey-api-key: YOUR_PORTKEY_API_KEY\nx-portkey-provider: vertex-ai\nx-portkey-virtual-key: YOUR_VIRTUAL_KEY", + "ANTHROPIC_CUSTOM_HEADERS": "x-portkey-api-key: YOUR_PORTKEY_API_KEY\nx-portkey-provider: vertex-ai\nx-portkey-provider: YOUR_PROVIDER", "ANTHROPIC_MODEL": "claude-3-7-sonnet@20250219", "CLAUDE_CODE_SKIP_VERTEX_AUTH": 1, "CLAUDE_CODE_USE_VERTEX": 1 @@ -129,7 +129,7 @@ For direct Anthropic API usage: { "env": { "ANTHROPIC_BASE_URL": "https://api.portkey.ai/v1", - "ANTHROPIC_CUSTOM_HEADERS": "x-portkey-api-key: YOUR_PORTKEY_API_KEY\nx-portkey-provider: anthropic\nx-portkey-virtual-key: YOUR_VIRTUAL_KEY", + "ANTHROPIC_CUSTOM_HEADERS": "x-portkey-api-key: YOUR_PORTKEY_API_KEY\nx-portkey-provider: anthropic\nx-portkey-provider: YOUR_PROVIDER", "ANTHROPIC_MODEL": "claude-sonnet-4-20250514" } } @@ -175,7 +175,7 @@ Here's a full `settings.json` example with permissions and Bedrock configuration }, "env": { "ANTHROPIC_BEDROCK_BASE_URL": "https://api.portkey.ai/v1", - "ANTHROPIC_CUSTOM_HEADERS": "x-portkey-api-key: YOUR_PORTKEY_API_KEY\nx-portkey-provider: bedrock\nx-portkey-virtual-key: YOUR_VIRTUAL_KEY", + "ANTHROPIC_CUSTOM_HEADERS": "x-portkey-api-key: YOUR_PORTKEY_API_KEY\nx-portkey-provider: bedrock\nx-portkey-provider: YOUR_PROVIDER", "ANTHROPIC_MODEL": "us.anthropic.claude-sonnet-4-20250514-v1:0", "CLAUDE_CODE_SKIP_BEDROCK_AUTH": 1, "CLAUDE_CODE_USE_BEDROCK": 1 @@ -221,7 +221,7 @@ When deploying Claude Code across your organization, implement these governance -Create separate virtual keys for each team with specific limits: +Create separate providers for each team with specific limits: 1. **Engineering Team** - Budget: $500/month @@ -239,7 +239,7 @@ Deploy team-specific `settings.json` through your configuration management: { "env": { "ANTHROPIC_BEDROCK_BASE_URL": "https://api.portkey.ai/v1", - "ANTHROPIC_CUSTOM_HEADERS": "x-portkey-api-key: TEAM_PORTKEY_API_KEY\nx-portkey-virtual-key: TEAM_VIRTUAL_KEY", + "ANTHROPIC_CUSTOM_HEADERS": "x-portkey-api-key: TEAM_PORTKEY_API_KEY\nx-portkey-provider: TEAM_PROVIDER", "ANTHROPIC_MODEL": "us.anthropic.claude-sonnet-4-20250514-v1:0", "CLAUDE_CODE_SKIP_BEDROCK_AUTH": 1, "CLAUDE_CODE_USE_BEDROCK": 1 @@ -301,7 +301,7 @@ Enable caching in your Portkey config to speed up repeated queries: ```json { - "virtual_key": "YOUR_VIRTUAL_KEY", + "provider":"@YOUR_PROVIDER", "cache": { "mode": "simple", } diff --git a/integrations/libraries/cline.mdx b/integrations/libraries/cline.mdx index 493c6361..cb40ce54 100644 --- a/integrations/libraries/cline.mdx +++ b/integrations/libraries/cline.mdx @@ -49,7 +49,7 @@ To create your config: 2. Create new config with: ```json { - "virtual_key": "YOUR_VIRTUAL_KEY_FROM_STEP1", + "provider":"@YOUR_PROVIDER_FROM_STEP1", "override_params": { "model": "gpt-4o" // Your preferred model name } @@ -126,7 +126,7 @@ If you prefer more direct control or need to use multiple providers dynamically, 2. Add custom headers by clicking "Add Header" and include: ``` - x-portkey-virtual-key: YOUR_VIRTUAL_KEY + x-portkey-provider: YOUR_PROVIDER ``` Optional headers: @@ -193,13 +193,13 @@ As your development team scales, controlling which developers can access specifi }, "targets": [ { - "virtual_key": "YOUR_OPENAI_VIRTUAL_KEY", + "provider":"@YOUR_OPENAI_PROVIDER", "override_params": { "model": "gpt-4o" } }, { - "virtual_key": "YOUR_ANTHROPIC_VIRTUAL_KEY", + "provider":"@YOUR_ANTHROPIC_PROVIDER", "override_params": { "model": "claude-3-sonnet-20240229" } @@ -219,7 +219,7 @@ Configs can be updated anytime to adjust controls without developers needing to ### Step 3: Implement Developer Access Controls Create Developer-specific API keys that automatically: -- Track usage per developer with virtual keys +- Track usage per developer with metadata - Apply appropriate configs to route requests - Collect metadata about coding sessions - Enforce access permissions diff --git a/integrations/libraries/codex.mdx b/integrations/libraries/codex.mdx index 788d089e..372a89ec 100644 --- a/integrations/libraries/codex.mdx +++ b/integrations/libraries/codex.mdx @@ -49,7 +49,7 @@ To create your config: 2. Create new config with: ```json { - "virtual_key": "YOUR_VIRTUAL_KEY_FROM_STEP1", + "provider":"@YOUR_PROVIDER_FROM_STEP1", "override_params": { "model": "gpt-4o" // Your preferred model name } @@ -181,7 +181,7 @@ Here's a basic configuration to route requests to OpenAI, specifically using GPT }, "targets": [ { - "virtual_key": "YOUR_OPENAI_VIRTUAL_KEY", + "provider":"@YOUR_OPENAI_PROVIDER", "override_params": { "model": "gpt-4o" } @@ -202,7 +202,7 @@ Configs can be updated anytime to adjust controls without affecting running appl ### Step 3: Implement Access Controls Create User-specific API keys that automatically: -- Track usage per user/team with the help of virtual keys +- Track usage per user/team with the help of metadata - Apply appropriate configs to route requests - Collect relevant metadata to filter logs - Enforce access permissions diff --git a/integrations/libraries/dspy.mdx b/integrations/libraries/dspy.mdx index 2e4e4f8d..c0347c64 100644 --- a/integrations/libraries/dspy.mdx +++ b/integrations/libraries/dspy.mdx @@ -209,7 +209,7 @@ turbo = dspy.OpenAI( model_type="chat", default_headers=createHeaders( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="MY_OPENAI_VIRTUAL_KEY" + provider="@MY_OPENAI_PROVIDER" ) ) dspy.settings.configure(lm=turbo) diff --git a/integrations/libraries/goose.mdx b/integrations/libraries/goose.mdx index 66134c3c..11bf0541 100644 --- a/integrations/libraries/goose.mdx +++ b/integrations/libraries/goose.mdx @@ -49,7 +49,7 @@ To create your config: 2. Create new config with: ```json { - "virtual_key": "YOUR_VIRTUAL_KEY_FROM_STEP1", + "provider":"@YOUR_PROVIDER_FROM_STEP1", "override_params": { "model": "gpt-4o" // Your preferred model name } @@ -176,7 +176,7 @@ Here's a basic configuration to route requests to OpenAI, specifically using GPT }, "targets": [ { - "virtual_key": "YOUR_OPENAI_VIRTUAL_KEY", + "provider":"@YOUR_OPENAI_PROVIDER", "override_params": { "model": "gpt-4o" } @@ -197,7 +197,7 @@ Configs can be updated anytime to adjust controls without affecting running appl ### Step 3: Implement Access Controls Create User-specific API keys that automatically: -- Track usage per developer/team with the help of virtual keys +- Track usage per developer/team with the help of metadata - Apply appropriate configs to route requests - Collect relevant metadata to filter logs - Enforce access permissions diff --git a/integrations/libraries/instructor.mdx b/integrations/libraries/instructor.mdx index 75cfea6e..0d731535 100644 --- a/integrations/libraries/instructor.mdx +++ b/integrations/libraries/instructor.mdx @@ -18,7 +18,7 @@ from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders portkey = OpenAI( base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - virtual_key="OPENAI_VIRTUAL_KEY", + provider="@OPENAI_PROVIDER", api_key="PORTKEY_API_KEY" ) ) @@ -53,7 +53,7 @@ const portkey = new OpenAI({ baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ apiKey: "PORTKEY_API_KEY", - virtualKey: "OPENAI_API_KEY", + provider:"@OPENAI_API_KEY", }), }); @@ -115,7 +115,7 @@ cache_config = { portkey = OpenAI( base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - virtual_key="OPENAI_VIRTUAL_KEY", + provider="@OPENAI_PROVIDER", api_key="PORTKEY_API_KEY", config=cache_config # Or pass your Config ID saved from Portkey app ) @@ -156,7 +156,7 @@ const portkey = new OpenAI({ baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ apiKey: "PORTKEY_API_KEY", - virtualKey: "OPENAI_API_KEY", + provider:"@OPENAI_API_KEY", config: cache_config // Or pass your Config ID saved from Portkey app }), }); diff --git a/integrations/libraries/janhq.mdx b/integrations/libraries/janhq.mdx index c69a8bc0..25055c1f 100644 --- a/integrations/libraries/janhq.mdx +++ b/integrations/libraries/janhq.mdx @@ -48,7 +48,7 @@ To create your config: 2. Create new config with: ```json { - "virtual_key": "YOUR_VIRTUAL_KEY_FROM_STEP1", + "provider":"@YOUR_PROVIDER_FROM_STEP1", "override_params": { "model": "gpt-4o" // Your preferred model name } @@ -160,7 +160,7 @@ Here's a basic configuration to route requests to OpenAI, specifically using GPT }, "targets": [ { - "virtual_key": "YOUR_OPENAI_VIRTUAL_KEY", + "provider":"@YOUR_OPENAI_PROVIDER", "override_params": { "model": "gpt-4o" } @@ -181,7 +181,7 @@ Configs can be updated anytime to adjust controls without affecting running appl ### Step 3: Implement Access Controls Create User-specific API keys that automatically: -- Track usage per user/team with the help of virtual keys +- Track usage per user/team with the help of metadata - Apply appropriate configs to route requests - Collect relevant metadata to filter logs - Enforce access permissions diff --git a/integrations/libraries/langchain-python.mdx b/integrations/libraries/langchain-python.mdx index 1e5e8e85..7b05dacf 100644 --- a/integrations/libraries/langchain-python.mdx +++ b/integrations/libraries/langchain-python.mdx @@ -114,11 +114,11 @@ from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders import os PORTKEY_API_KEY = os.environ.get("PORTKEY_API_KEY") -ANTHROPIC_VIRTUAL_KEY = os.environ.get("ANTHROPIC_VIRTUAL_KEY") +ANTHROPIC_PROVIDER = os.environ.get("ANTHROPIC_PROVIDER") portkey_anthropic_headers = createHeaders( api_key=PORTKEY_API_KEY, - virtual_key=ANTHROPIC_VIRTUAL_KEY + provider="@anthropic" ) llm_claude = ChatOpenAI( @@ -144,11 +144,11 @@ from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders import os PORTKEY_API_KEY = os.environ.get("PORTKEY_API_KEY") -GOOGLE_VIRTUAL_KEY = os.environ.get("GOOGLE_VIRTUAL_KEY") +GOOGLE_PROVIDER = os.environ.get("GOOGLE_PROVIDER") portkey_gemini_headers = createHeaders( api_key=PORTKEY_API_KEY, - virtual_key=GOOGLE_VIRTUAL_KEY + provider="@google" ) llm_gemini = ChatOpenAI( @@ -180,7 +180,7 @@ A Portkey Config can specify `mode` (`simple`, `semantic`, `hybrid`) and `max_ag // Example Portkey Config JSON for semantic cache { "cache": { "mode": "semantic", "max_age": "2h" }, - "virtual_key": "your_openai_virtual_key_id", + "provider":"@your_openai_virtual_key_id", "override_params": { "model": "gpt-4o" } } ``` @@ -198,7 +198,7 @@ PORTKEY_CONFIG_ID = "cfg_semantic_cache_123" portkey_cached_headers = createHeaders( api_key=PORTKEY_API_KEY, - config=PORTKEY_CONFIG_ID + provider="@openai" ) llm_cached = ChatOpenAI( @@ -233,8 +233,8 @@ Portkey improves Langchain app resilience via Configs: { "strategy": { "mode": "fallback" }, "targets": [ - { "override_params": {"model": "gpt-4o"}, "virtual_key": "vk_openai", "retry": {"count": 2} }, - { "override_params": {"model": "claude-3-sonnet-20240229"}, "virtual_key": "vk_anthropic" } + { "override_params": {"model": "gpt-4o"}, "provider":"@vk_openai", "retry": {"count": 2} }, + { "override_params": {"model": "claude-3-sonnet-20240229"}, "provider":"@vk_anthropic" } ] } ``` @@ -252,7 +252,7 @@ PORTKEY_RELIABLE_CONFIG_ID = "cfg_reliable_123" portkey_reliable_headers = createHeaders( api_key=PORTKEY_API_KEY, - config=PORTKEY_RELIABLE_CONFIG_ID + provider="@openai" ) llm_reliable = ChatOpenAI( @@ -322,8 +322,7 @@ if rendered_prompt and rendered_prompt.prompt: if msg.get("role") == "user": langchain_messages.append(HumanMessage(content=msg.get("content"))) elif msg.get("role") == "system": langchain_messages.append(SystemMessage(content=msg.get("content"))) -OPENAI_VIRTUAL_KEY = os.environ.get("OPENAI_VIRTUAL_KEY") -portkey_headers = createHeaders(api_key=PORTKEY_API_KEY, virtual_key=OPENAI_VIRTUAL_KEY) +portkey_headers = createHeaders(api_key=PORTKEY_API_KEY, provider="@openai") llm_portkey_prompt = ChatOpenAI( api_key="placeholder_key", @@ -363,9 +362,8 @@ from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders import os PORTKEY_API_KEY = os.environ.get("PORTKEY_API_KEY") -OPENAI_EMBEDDINGS_VIRTUAL_KEY = os.environ.get("OPENAI_EMBEDDINGS_VIRTUAL_KEY") -portkey_headers = createHeaders(api_key=PORTKEY_API_KEY, virtual_key=OPENAI_EMBEDDINGS_VIRTUAL_KEY) +portkey_headers = createHeaders(api_key=PORTKEY_API_KEY, provider="@openai") embeddings_model = OpenAIEmbeddings( api_key="placeholder_key", @@ -394,9 +392,8 @@ from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders import os PORTKEY_API_KEY = os.environ.get("PORTKEY_API_KEY") -OPENAI_VIRTUAL_KEY = os.environ.get("OPENAI_VIRTUAL_KEY") -portkey_headers = createHeaders(api_key=PORTKEY_API_KEY, virtual_key=OPENAI_VIRTUAL_KEY) +portkey_headers = createHeaders(api_key=PORTKEY_API_KEY, provider="@openai") chat_llm = ChatOpenAI( api_key="placeholder_key", diff --git a/integrations/libraries/librechat.mdx b/integrations/libraries/librechat.mdx index ddb5e766..750295df 100644 --- a/integrations/libraries/librechat.mdx +++ b/integrations/libraries/librechat.mdx @@ -49,7 +49,7 @@ To create your config: 2. Create new config with: ```json { - "virtual_key": "YOUR_VIRTUAL_KEY_FROM_STEP1", + "provider":"@YOUR_PROVIDER_FROM_STEP1", "override_params": { "model": "gpt-4o" // Your preferred model name } @@ -154,7 +154,7 @@ endpoints: baseURL: ${PORTKEY_GATEWAY_URL} headers: x-portkey-api-key: "${PORTKEY_API_KEY}" - x-portkey-virtual-key: "PORTKEY_OPENAI_VIRTUAL_KEY" + x-portkey-provider: "PORTKEY_OPENAI_PROVIDER" models: default: ["gpt-4o-mini"] fetch: true @@ -226,7 +226,7 @@ Here's a basic configuration to route requests to OpenAI, specifically using GPT }, "targets": [ { - "virtual_key": "YOUR_OPENAI_VIRTUAL_KEY", + "provider":"@YOUR_OPENAI_PROVIDER", "override_params": { "model": "gpt-4o" } @@ -247,7 +247,7 @@ Configs can be updated anytime to adjust controls without affecting running appl ### Step 3: Implement Access Controls Create User-specific API keys that automatically: -- Track usage per user/team with the help of virtual keys +- Track usage per user/team with the help of metadata - Apply appropriate configs to route requests - Collect relevant metadata to filter logs - Enforce access permissions diff --git a/integrations/libraries/llama-index-python.mdx b/integrations/libraries/llama-index-python.mdx index 20875365..46b18892 100644 --- a/integrations/libraries/llama-index-python.mdx +++ b/integrations/libraries/llama-index-python.mdx @@ -428,7 +428,7 @@ from llama_index.core.llms import ChatMessage from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders config = { - "virtual_key":"AZURE_OPENAI_PORTKEY_VIRTUAL_KEY" + "provider:"@AZURE_OPENAI_PORTKEY_PROVIDER" } #### You can also reference a saved Config instead #### @@ -461,7 +461,7 @@ from llama_index.core.llms import ChatMessage from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders config = { - "virtual_key":"AWS_BEDROCK_PORTKEY_VIRTUAL_KEY" + "provider:"@AWS_BEDROCK_PORTKEY_PROVIDER" } #### You can also reference a saved Config instead #### @@ -497,7 +497,7 @@ from llama_index.core.llms import ChatMessage from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders config = { - "virtual_key":"VERTEX_AI_PORTKEY_VIRTUAL_KEY" + "provider:"@VERTEX_AI_PORTKEY_PROVIDER" } #### You can also reference a saved Config instead #### @@ -630,13 +630,13 @@ config = { }, "targets": [ { - "virtual_key": "openai-virtual-key", + "provider":"@openai-virtual-key", "override_params": { "model": "gpt-4o" } }, { - "virtual_key": "anthropic-virtual-key", + "provider":"@anthropic-virtual-key", "override_params": { "model": "claude-3-opus-20240229", "max_tokens":64 @@ -655,11 +655,11 @@ config = { }, "targets": [ { - "virtual_key": "openai-virtual-key-1", + "provider":"@openai-virtual-key-1", "weight":1 }, { - "virtual_key": "openai-virtual-key-2", + "provider":"@openai-virtual-key-2", "weight":1 } ] @@ -673,7 +673,7 @@ config = { "retry": { "attempts": 5 }, - "virtual_key": "virtual-key-xxx" + "provider":"@virtual-key-xxx" } ``` @@ -684,8 +684,8 @@ config = { "strategy": { "mode": "fallback" }, "request_timeout": 10000, "targets": [ - { "virtual_key": "open-ai-xxx" }, - { "virtual_key": "azure-open-ai-xxx" } + { "provider":"@open-ai-xxx" }, + { "provider":"@azure-open-ai-xxx" } ] } ``` @@ -812,7 +812,7 @@ prompt_template = client.prompts.render( ).data.dict() config = { - "virtual_key":"GROQ_VIRTUAL_KEY", # You need to send the virtual key separately + "provider:"@GROQ_PROVIDER", # You need to send the virtual key separately "override_params":{ "model":prompt_template["model"], # Set the model name based on the value in the prompt template "temperature":prompt_template["temperature"] # Similarly, you can also set other model params diff --git a/integrations/libraries/mindsdb.mdx b/integrations/libraries/mindsdb.mdx index 6ed0a397..26223c5f 100644 --- a/integrations/libraries/mindsdb.mdx +++ b/integrations/libraries/mindsdb.mdx @@ -35,7 +35,7 @@ CREATE ML_ENGINE portkey_engine FROM portkey USING portkey_api_key = '{PORTKEY_API_KEY}', - virtual_key = '{YOUR_PROVIDER_VIRTUAL_KEY}', -- choose from 200+ provider + virtual_key = '{YOUR_PROVIDER}', -- choose from 200+ provider provider = '{PROVIDER_NAME}'; --- ex- openai, anthropic, bedrock, etc. ``` diff --git a/integrations/libraries/n8n.mdx b/integrations/libraries/n8n.mdx index a515bac2..5b132bf9 100644 --- a/integrations/libraries/n8n.mdx +++ b/integrations/libraries/n8n.mdx @@ -48,7 +48,7 @@ To create your config: 2. Create new config with: ```json { - "virtual_key": "YOUR_VIRTUAL_KEY_FROM_STEP1", + "provider":"@YOUR_PROVIDER_FROM_STEP1", "override_params": { "model": "gpt-4o" // Your preferred model name } @@ -178,7 +178,7 @@ Here's a basic configuration to route requests to OpenAI, specifically using GPT }, "targets": [ { - "virtual_key": "YOUR_OPENAI_VIRTUAL_KEY", + "provider":"@YOUR_OPENAI_PROVIDER", "override_params": { "model": "gpt-4o" } @@ -199,7 +199,7 @@ Configs can be updated anytime to adjust controls without affecting running appl ### Step 3: Implement Access Controls Create User-specific API keys that automatically: -- Track usage per user/team with the help of virtual keys +- Track usage per user/team with the help of metadata - Apply appropriate configs to route requests - Collect relevant metadata to filter logs - Enforce access permissions diff --git a/integrations/libraries/openai-compatible.mdx b/integrations/libraries/openai-compatible.mdx index e4f25c06..cbd32156 100644 --- a/integrations/libraries/openai-compatible.mdx +++ b/integrations/libraries/openai-compatible.mdx @@ -48,7 +48,7 @@ To create your config: 2. Create new config with: ```json { - "virtual_key": "YOUR_VIRTUAL_KEY_FROM_STEP1", + "provider":"@YOUR_PROVIDER_FROM_STEP1", } ``` 3. Save and note the Config name for the next step @@ -102,7 +102,7 @@ Navigate to your project's LLM settings page and find the OpenAI configuration s ```json { - "virtual_key": "", + "provider":"@", "override_params": { "model": "gpt-4o" // Specify your desired model } @@ -163,7 +163,7 @@ Here's a basic configuration to route requests to OpenAI, specifically using GPT }, "targets": [ { - "virtual_key": "YOUR_OPENAI_VIRTUAL_KEY", + "provider":"@YOUR_OPENAI_PROVIDER", "override_params": { "model": "gpt-4o" } @@ -184,7 +184,7 @@ Configs can be updated anytime to adjust controls without affecting running appl ### Step 3: Implement Access Controls Create User-specific API keys that automatically: -- Track usage per user/team with the help of virtual keys +- Track usage per user/team with the help of metadata - Apply appropriate configs to route requests - Collect relevant metadata to filter logs - Enforce access permissions diff --git a/integrations/libraries/openwebui.mdx b/integrations/libraries/openwebui.mdx index 61006053..e34f7577 100644 --- a/integrations/libraries/openwebui.mdx +++ b/integrations/libraries/openwebui.mdx @@ -57,7 +57,7 @@ Configs in Portkey enhance your implementation with features like advanced routi "retry": { "attempts": 5 }, - "virtual_key": "virtual-key-xxx" + "provider":"@virtual-key-xxx" } ``` You can create and store these configs in Portkey's config library. This can later be accessed on using the Config Slug in Open WebUI. @@ -87,8 +87,8 @@ The pipeline setup involves configuring both credentials and model access in Ope 3. Update it with your virtual keys: ```json "virtual_keys": { - "openai": "YOUR_OPENAI_VIRTUAL_KEY", - "anthropic": "YOUR_ANTHROPIC_VIRTUAL_KEY" + "openai": "YOUR_OPENAI_PROVIDER", + "anthropic": "YOUR_ANTHROPIC_PROVIDER" } ``` 4. Configure model names in the pipe function in this format: @@ -166,7 +166,7 @@ Here's a basic configuration to route requests to OpenAI, specifically using GPT }, "targets": [ { - "virtual_key": "YOUR_OPENAI_VIRTUAL_KEY", + "provider":"@YOUR_OPENAI_PROVIDER", "override_params": { "model": "gpt-4o" } @@ -187,7 +187,7 @@ Configs can be updated anytime to adjust controls without affecting running appl ### Step 3: Implement Access Controls Create User-specific API keys that automatically: -- Track usage per user/team with the help of virtual keys +- Track usage per user/team with the help of metadata - Apply appropriate configs to route requests - Collect relevant metadata to filter logs - Enforce access permissions diff --git a/integrations/libraries/promptfoo.mdx b/integrations/libraries/promptfoo.mdx index 724feadc..498adf67 100644 --- a/integrations/libraries/promptfoo.mdx +++ b/integrations/libraries/promptfoo.mdx @@ -68,7 +68,7 @@ providers: config: portkeyProvider: anthropic # You can also add your Anthropic API key to Portkey and pass the virtual key here - portkeyVirtualKey: ANTHROPIC_VIRTUAL_KEY + portkeyVirtualKey: ANTHROPIC_PROVIDER ``` @@ -80,7 +80,7 @@ providers: config: portkeyProvider: google # You can also add your Gemini API key to Portkey and pass the virtual key here - portkeyVirtualKey: GEMINI_VIRTUAL_KEY + portkeyVirtualKey: GEMINI_PROVIDER ``` @@ -91,7 +91,7 @@ providers: config: portkeyProvider: groq # You can also add your Groq API key to Portkey and pass the virtual key here - portkeyVirtualKey: GROQ_VIRTUAL_KEY + portkeyVirtualKey: GROQ_PROVIDER ``` @@ -116,7 +116,7 @@ providers: providers: id: portkey:xxx config: - portkeyVirtualKey: YOUR_PORTKEY_AZURE_OPENAI_VIRTUAL_KEY + portkeyVirtualKey: YOUR_PORTKEY_AZURE_OPENAI_PROVIDER ``` #### Without Using Virtual Keys @@ -154,7 +154,7 @@ providers: providers: id: portkey:anthropic.claude-3-sonnet-20240229-v1:0 config: - portkeyVirtualKey: YOUR_PORTKEY_AWS_BEDROCK_VIRTUAL_KEY + portkeyVirtualKey: YOUR_PORTKEY_AWS_BEDROCK_PROVIDER # If you're using AWS Security Token Service, you can set it here awsSessionToken: "AWS_SESSION_TOKEN" ``` @@ -185,7 +185,7 @@ Set your Vertex AI access token with the `VERTEX_API_KEY` env var, and pass the providers: id: portkey:gemini-1.5-flash-latest config: - portkeyVirtualKey: YOUR_PORTKEY_GOOGLE_VERTEX_AI_VIRTUAL_KEY + portkeyVirtualKey: YOUR_PORTKEY_GOOGLE_VERTEX_AI_PROVIDER ``` #### Without Using Virtual Keys @@ -216,7 +216,7 @@ Using Portkey, you can also send custom metadata with each of your requests to f providers: id: portkey:claude-3-opus20240229 config: - portkeyVirtualKey: ANTHROPIC_VIRTUAL_KEY + portkeyVirtualKey: ANTHROPIC_PROVIDER portkeyMetadata: team: alpha9 prompt: classification @@ -233,7 +233,7 @@ You can filter or group data by these metadata keys on Portkey dashboards. ## 4\. Avoid Promptfoo Rate Limits & Leverage Cache -Since promptfoo can make a lot of calls very quickly, you can use a loadbalanced config in Portkey with cache enabled. You can pass the config header similar to virtual keys in promptfoo. +Since promptfoo can make a lot of calls very quickly, you can use a loadbalanced config in Portkey with cache enabled. You can pass the config header in the same YAML. Here's a sample Config that you can save in the Portkey UI and get a respective config slug: @@ -242,9 +242,9 @@ Here's a sample Config that you can save in the Portkey UI and get a respective "cache": { "mode": "simple" }, "strategy": { "mode": "loadbalance" }, "targets": [ - { "virtual_key": "ACCOUNT_ONE" }, - { "virtual_key": "ACCOUNT_TWO" }, - { "virtual_key": "ACCOUNT_THREE" } + { "provider":"@ACCOUNT_ONE" }, + { "provider":"@ACCOUNT_TWO" }, + { "provider":"@ACCOUNT_THREE" } ] } ``` @@ -255,7 +255,6 @@ And then we can just add the saved Config's slug in the YAML: providers: id: portkey:claude-3-opus20240229 config: - portkeyVirtualKey: ANTHROPIC_VIRTUAL_KEY portkeyConfig: PORTKEY_CONFIG_SLUG ``` diff --git a/integrations/libraries/roo-code.mdx b/integrations/libraries/roo-code.mdx index 0cb07a6a..c2edb91a 100644 --- a/integrations/libraries/roo-code.mdx +++ b/integrations/libraries/roo-code.mdx @@ -49,7 +49,7 @@ To create your config: 2. Create new config with: ```json { - "virtual_key": "YOUR_VIRTUAL_KEY_FROM_STEP1", + "provider":"@YOUR_PROVIDER_FROM_STEP1", "override_params": { "model": "gpt-4o" // Your preferred model name } @@ -125,7 +125,7 @@ If you prefer more direct control or need to use multiple providers dynamically, 2. Add custom headers by clicking the `+` button in the Custom Headers section: ``` - x-portkey-virtual-key: YOUR_VIRTUAL_KEY + x-portkey-provider: YOUR_PROVIDER ``` Optional headers: @@ -192,13 +192,13 @@ As your development team scales, controlling which developers can access specifi }, "targets": [ { - "virtual_key": "YOUR_OPENAI_VIRTUAL_KEY", + "provider":"@YOUR_OPENAI_PROVIDER", "override_params": { "model": "gpt-4o" } }, { - "virtual_key": "YOUR_ANTHROPIC_VIRTUAL_KEY", + "provider":"@YOUR_ANTHROPIC_PROVIDER", "override_params": { "model": "claude-3-sonnet-20240229" } @@ -218,7 +218,7 @@ Configs can be updated anytime to adjust controls without developers needing to ### Step 3: Implement Developer Access Controls Create Developer-specific API keys that automatically: -- Track usage per developer with virtual keys +- Track usage per developer with metadata - Apply appropriate configs to route requests - Collect metadata about coding sessions - Enforce access permissions diff --git a/integrations/libraries/supabase.mdx b/integrations/libraries/supabase.mdx index f7ab5e21..ff437ac6 100644 --- a/integrations/libraries/supabase.mdx +++ b/integrations/libraries/supabase.mdx @@ -59,7 +59,7 @@ supabase: Client = create_client(supabase_url, supabase_key) portkey_client = Portkey( api_key="YOUR_PORTKEY_API_KEY", provider="openai", - virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + provider="@YOUR_OPENAI_PROVIDER", ) ``` @@ -97,7 +97,7 @@ Here's an example on how to use `Cohere` with Portkey client = Portkey( api_key="YOUR_PORTKEY_API_KEY", # defaults to os.environ.get("PORTKEY_API_KEY") provider="cohere", - virtual_key="YOUR_COHERE_VIRTUAL_KEY", + provider="@YOUR_COHERE_PROVIDER", ) embeddings = client.embeddings.create( diff --git a/integrations/libraries/zed.mdx b/integrations/libraries/zed.mdx index 6b02fafd..b7bc7632 100644 --- a/integrations/libraries/zed.mdx +++ b/integrations/libraries/zed.mdx @@ -49,7 +49,7 @@ To create your config: 2. Create new config with: ```json { - "virtual_key": "YOUR_VIRTUAL_KEY_FROM_STEP1", + "provider":"@YOUR_PROVIDER_FROM_STEP1", } ``` 3. Save and note the Config name for the next step @@ -172,7 +172,7 @@ Here's a basic configuration to route requests to OpenAI, specifically using GPT }, "targets": [ { - "virtual_key": "YOUR_OPENAI_VIRTUAL_KEY", + "provider":"@YOUR_OPENAI_PROVIDER", "override_params": { "model": "gpt-4o" } @@ -193,7 +193,7 @@ Configs can be updated anytime to adjust controls without affecting running appl ### Step 3: Implement Access Controls Create User-specific API keys that automatically: -- Track usage per user/team with the help of virtual keys +- Track usage per user/team with the help of metadata - Apply appropriate configs to route requests - Collect relevant metadata to filter logs - Enforce access permissions diff --git a/integrations/llms/ai21.mdx b/integrations/llms/ai21.mdx index d471f671..851dad81 100644 --- a/integrations/llms/ai21.mdx +++ b/integrations/llms/ai21.mdx @@ -44,7 +44,7 @@ To use AI21 with Portkey, [get your API key from here](https://studio.ai21.com/a const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your AI21 Virtual Key + provider:"@PROVIDER" // Your AI21 Virtual Key }) ``` @@ -54,7 +54,7 @@ To use AI21 with Portkey, [get your API key from here](https://studio.ai21.com/a portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Groq + provider="@PROVIDER" # Replace with your virtual key for Groq ) ``` diff --git a/integrations/llms/anthropic.mdx b/integrations/llms/anthropic.mdx index a292b619..0774101f 100644 --- a/integrations/llms/anthropic.mdx +++ b/integrations/llms/anthropic.mdx @@ -44,7 +44,7 @@ import Portkey from 'portkey-ai' const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your Anthropic Virtual Key + provider:"@PROVIDER" // Your Anthropic Virtual Key }) ``` @@ -56,7 +56,7 @@ from portkey_ai import Portkey portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Anthropic + provider="@PROVIDER" # Replace with your virtual key for Anthropic ) ``` @@ -238,7 +238,7 @@ Here's an example using Anthropic `claude-3.5-sonnet` model # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key + provider="@PROVIDER" ) # Create the request @@ -273,7 +273,7 @@ Here's an example using Anthropic `claude-3.5-sonnet` model // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY" // Add your anthropic's virtual key + provider:"@PROVIDER" // Add your anthropic's virtual key }); // Generate a chat completion @@ -462,7 +462,7 @@ Currently, Portkey supports PDF processing using base64-encoded PDF documents, f # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Anthropic + provider="@PROVIDER" # Replace with your virtual key for Anthropic ) # Fetch and encode the PDF @@ -521,7 +521,7 @@ Currently, Portkey supports PDF processing using base64-encoded PDF documents, f // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY" // Replace with your virtual key for Anthropic + provider:"@PROVIDER" // Replace with your virtual key for Anthropic }); async function processPdf() { @@ -682,7 +682,7 @@ Note that you will have to set [`strict_open_ai_compliance=False`](/product/ai-g # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + provider="@PROVIDER", strict_open_ai_compliance=False ) @@ -727,7 +727,7 @@ Note that you will have to set [`strict_open_ai_compliance=False`](/product/ai-g // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY", // Add your anthropic's virtual key + provider:"@PROVIDER", // Add your anthropic's virtual key strictOpenAiCompliance: false }); @@ -898,7 +898,7 @@ Note that you will have to set [`strict_open_ai_compliance=False`](/product/ai-g # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + provider="@PROVIDER", strict_open_ai_compliance=False ) @@ -945,7 +945,7 @@ Note that you will have to set [`strict_open_ai_compliance=False`](/product/ai-g // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY", // Add your anthropic's virtual key + provider:"@PROVIDER", // Add your anthropic's virtual key strictOpenAiCompliance: false }); @@ -1162,7 +1162,7 @@ from portkey_ai import Portkey client = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="VIRTUAL_KEY", + provider="@PROVIDER", anthropic_beta="token-efficient-tools-2025-02-19", strict_open_ai_compliance=False ) @@ -1173,7 +1173,7 @@ import Portkey from 'portkey-ai'; const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", - virtualKey: "VIRTUAL_KEY", + provider:"@PROVIDER", anthropicBeta: "token-efficient-tools-2025-02-19", strictOpenAiCompliance: false }); @@ -1185,7 +1185,7 @@ from portkey_ai import createHeaders headers = createHeaders( provider="anthropic", api_key="PORTKEY_API_KEY", - virtual_key="VIRTUAL_KEY", + provider="@PROVIDER", strict_open_ai_compliance=False, anthropic_beta="token-efficient-tools-2025-02-19" ) @@ -1197,7 +1197,7 @@ import { createHeaders } from 'portkey-ai' const headers = createHeaders({ provider: "anthropic", apiKey: "PORTKEY_API_KEY", - virtual_key="VIRTUAL_KEY", + provider="@PROVIDER", strictOpenAiCompliance: false, anthropicBeta: "token-efficient-tools-2025-02-19" }) diff --git a/integrations/llms/anthropic/prompt-caching.mdx b/integrations/llms/anthropic/prompt-caching.mdx index 82bf8c26..e437bd93 100644 --- a/integrations/llms/anthropic/prompt-caching.mdx +++ b/integrations/llms/anthropic/prompt-caching.mdx @@ -17,7 +17,7 @@ import Portkey from 'portkey-ai' const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your Anthropic Virtual Key + provider:"@PROVIDER" }) const chatCompletion = await portkey.chat.completions.create({ @@ -45,7 +45,7 @@ from portkey_ai import Portkey portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="ANTHROPIC_VIRTUAL_KEY", + provider="@ANTHROPIC_PROVIDER", ) chat_completion = portkey.chat.completions.create( diff --git a/integrations/llms/anyscale-llama2-mistral-zephyr.mdx b/integrations/llms/anyscale-llama2-mistral-zephyr.mdx index 743bec60..2cca2f96 100644 --- a/integrations/llms/anyscale-llama2-mistral-zephyr.mdx +++ b/integrations/llms/anyscale-llama2-mistral-zephyr.mdx @@ -37,7 +37,7 @@ import Portkey from 'portkey-ai' const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "ANYSCALE_VIRTUAL_KEY" // Your Anyscale Virtual Key + provider:"@ANYSCALE_PROVIDER" // Your Anyscale Virtual Key }) ``` @@ -47,7 +47,7 @@ const portkey = new Portkey({ portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="ANYSCALE_VIRTUAL_KEY" # Replace with your virtual key for Anyscale + provider="@ANYSCALE_PROVIDER" # Replace with your virtual key for Anyscale ) ``` @@ -91,7 +91,7 @@ Alternatively, you can also directly call Anyscale models through Portkey's REST 1. You send your requests to Portkey's complete Gateway URL `https://api.portkey.ai/v1/chat/completions` 2. You have to add Portkey specific headers. 1. `x-portkey-api-key` for sending your Portkey API Key - 2. `x-portkey-virtual-key` for sending your provider's virtual key (Alternatively, if you are not using Virtual keys, you can send your Auth header for your provider, and pass the `x-portkey-provider` header along with it) + 2. `x-portkey-provider` for sending your provider's virtual key (Alternatively, if you are not using Virtual keys, you can send your Auth header for your provider, and pass the `x-portkey-provider` header along with it) diff --git a/integrations/llms/aws-sagemaker.mdx b/integrations/llms/aws-sagemaker.mdx index bec08e0b..e31ab64a 100644 --- a/integrations/llms/aws-sagemaker.mdx +++ b/integrations/llms/aws-sagemaker.mdx @@ -33,12 +33,12 @@ npm install --save portkey-ai -### 2\. Initialize Portkey with a Virtual Key +### 2\. Initialize Portkey with a Integration There are multiple ways to integrate Sagemaker with Portkey. You can use your AWS credentials, or use an assumed role. -In this example we will create a virtual key and use it to interact with Sagemaker. +In this example we will create a integration and use it to interact with Sagemaker. This helps you restrict access (specific models, few endpoints, etc). @@ -58,10 +58,10 @@ Here's how to find your AWS credentials: -Create a virtual key in the Portkey dashboard in the virtual keys section. +Create a new integration in the Portkey dashboard. You can select sagemaker as the provider, and fill in deployment details. -Initialize the Portkey SDK with the virtual key. (If you are using the REST API, skip to next step) +Initialize the Portkey SDK with the integration. (If you are using the REST API, skip to next step) ```js @@ -69,7 +69,7 @@ Initialize the Portkey SDK with the virtual key. (If you are using the REST API, const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY" // Replace with your Sagemaker Virtual Key + provider:"@sagemaker" // Replace with your Sagemaker provider slug }) ``` @@ -79,7 +79,7 @@ Initialize the Portkey SDK with the virtual key. (If you are using the REST API, portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your Sagemaker Virtual Key + provider="@sagemaker" # Replace with your Sagemaker provider slug ) ``` @@ -117,7 +117,7 @@ Initialize the Portkey SDK with the virtual key. (If you are using the REST API, ```cURL curl --location 'https://api.portkey.ai/v1/endpoints/{endpoint_name}/invocations' \ - --header 'x-portkey-virtual-key: {VIRTUAL_KEY}' \ + --header 'x-portkey-provider: @sagemaker' \ --header 'x-portkey-api-key: {PORTKEY_API_KEY}' \ --header 'Content-Type: application/json' \ --data '{ diff --git a/integrations/llms/azure-foundry.mdx b/integrations/llms/azure-foundry.mdx index 358a61da..3ded1272 100644 --- a/integrations/llms/azure-foundry.mdx +++ b/integrations/llms/azure-foundry.mdx @@ -120,7 +120,7 @@ import Portkey from 'portkey-ai'; const client = new Portkey({ apiKey: 'PORTKEY_API_KEY', - virtualKey: 'AZURE_FOUNDRY_VIRTUAL_KEY' + provider:'@AZURE_FOUNDRY_PROVIDER' }); async function main() { @@ -145,7 +145,7 @@ from portkey_ai import Portkey client = Portkey( api_key = "PORTKEY_API_KEY", - virtual_key = "AZURE_FOUNDRY_VIRTUAL_KEY" + virtual_key = "AZURE_FOUNDRY_PROVIDER" ) response = client.chat.completions.create( @@ -164,7 +164,7 @@ print(response.choices[0].message.content) curl https://api.portkey.ai/v1/chat/completions \ -H "Content-Type: application/json" \ -H "x-portkey-api-key: $PORTKEY_API_KEY" \ - -H "x-portkey-virtual-key: $AZURE_FOUNDRY_VIRTUAL_KEY" \ + -H "x-portkey-provider: $AZURE_FOUNDRY_PROVIDER" \ -d '{ "model": "DeepSeek-V3-0324", "messages": [ @@ -356,13 +356,13 @@ Create fallback configurations to ensure reliability when working with Azure AI }, "targets": [ { - "virtual_key": "azure-foundry-virtual-key", + "provider":"@azure-foundry-virtual-key", "override_params": { "model": "DeepSeek-V3-0324" } }, { - "virtual_key": "openai-virtual-key", + "provider":"@openai-virtual-key", "override_params": { "model": "gpt-4o" } @@ -382,14 +382,14 @@ Distribute requests across multiple models for optimal performance: }, "targets": [ { - "virtual_key": "azure-foundry-virtual-key-1", + "provider":"@azure-foundry-virtual-key-1", "override_params": { "model": "DeepSeek-V3-0324" }, "weight": 0.7 }, { - "virtual_key": "azure-foundry-virtual-key-2", + "provider":"@azure-foundry-virtual-key-2", "override_params": { "model": "cohere-command-a" }, @@ -422,21 +422,21 @@ Route requests based on specific conditions like user type or content requiremen "targets": [ { "name": "high-performance-model", - "virtual_key": "azure-foundry-virtual-key-1", + "provider":"@azure-foundry-virtual-key-1", "override_params": { "model": "Llama-4-Scout-17B-16E" } }, { "name": "code-specialized-model", - "virtual_key": "azure-foundry-virtual-key-2", + "provider":"@azure-foundry-virtual-key-2", "override_params": { "model": "DeepSeek-V3-0324" } }, { "name": "standard-model", - "virtual_key": "azure-foundry-virtual-key-3", + "provider":"@azure-foundry-virtual-key-3", "override_params": { "model": "cohere-command-a" } diff --git a/integrations/llms/azure-openai/azure-openai.mdx b/integrations/llms/azure-openai/azure-openai.mdx index 34340216..25e483c5 100644 --- a/integrations/llms/azure-openai/azure-openai.mdx +++ b/integrations/llms/azure-openai/azure-openai.mdx @@ -65,7 +65,7 @@ Set up Portkey with your virtual key as part of the initialization configuration const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "AZURE_VIRTUAL_KEY" // Your Azure Virtual Key + provider:"@AZURE_PROVIDER" // Your Azure Virtual Key }) ``` @@ -75,7 +75,7 @@ from portkey_ai import Portkey portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="AZURE_VIRTUAL_KEY" # Replace with your virtual key for Azure + provider="@AZURE_PROVIDER" # Replace with your virtual key for Azure ) ``` @@ -129,7 +129,7 @@ import Portkey from 'portkey-ai' const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", - virtualKey: "DALL-E_VIRTUAL_KEY" // Referencing a Dall-E Azure deployment with Virtual Key + provider:"@DALL-E_PROVIDER" // Referencing a Dall-E Azure deployment with Virtual Key }) const image = await portkey.images.generate({ @@ -144,7 +144,7 @@ from portkey_ai import Portkey portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="DALL-E_VIRTUAL_KEY" # Referencing a Dall-E Azure deployment with Virtual Key + provider="@DALL-E_PROVIDER" # Referencing a Dall-E Azure deployment with Virtual Key ) image = portkey.images.generate( @@ -170,7 +170,7 @@ More information on image generation is available in the [API Reference](https:/ ## Making Requests Without Virtual Keys -Here's how you can pass your Azure OpenAI details & secrets directly without using the Virutal Keys feature. +Here's how you can pass your Azure OpenAI details & secrets directly without using the Virtual Keys feature. ### Key Mapping diff --git a/integrations/llms/azure-openai/batches.mdx b/integrations/llms/azure-openai/batches.mdx index 6b1073ed..2d5d5003 100644 --- a/integrations/llms/azure-openai/batches.mdx +++ b/integrations/llms/azure-openai/batches.mdx @@ -19,7 +19,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key + provider="@PROVIDER" ) start_batch_response = portkey.batches.create( @@ -39,7 +39,7 @@ import { Portkey } from 'portkey-ai'; // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key + provider:"@PROVIDER" }); const startBatch = async () => { @@ -61,7 +61,7 @@ await startBatch(); ```sh curl --location 'https://api.portkey.ai/v1/batches' \ --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-provider: @provider' \ --header 'Content-Type: application/json' \ --data '{ "input_file_id": "", @@ -135,7 +135,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key + provider="@PROVIDER" ) batches = portkey.batches.list() @@ -150,7 +150,7 @@ import { Portkey } from 'portkey-ai'; // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key + provider:"@PROVIDER" }); const listBatches = async () => { @@ -167,7 +167,7 @@ await listBatches(); ```sh curl --location 'https://api.portkey.ai/v1/batches' \ --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' +--header 'x-portkey-provider: @provider' ``` @@ -225,7 +225,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key + provider="@PROVIDER" ) batch = portkey.batches.retrieve(batch_id="batch_id") @@ -240,7 +240,7 @@ import { Portkey } from 'portkey-ai'; // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key + provider:"@PROVIDER" }); const getBatch = async () => { @@ -257,7 +257,7 @@ await getBatch(); ```sh curl --location 'https://api.portkey.ai/v1/batches/' \ --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' +--header 'x-portkey-provider: @provider' ``` @@ -312,7 +312,7 @@ print(batch) ```sh curl --location 'https://api.portkey.ai/v1/batches//output' \ --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' +--header 'x-portkey-provider: @provider' ``` @@ -327,7 +327,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key + provider="@PROVIDER" ) batches = portkey.batches.list() @@ -342,7 +342,7 @@ import { Portkey } from 'portkey-ai'; // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key + provider:"@PROVIDER" }); const listBatchingJobs = async () => { @@ -359,7 +359,7 @@ await listBatchingJobs(); ```sh curl --location 'https://api.portkey.ai/v1/batches' \ --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' +--header 'x-portkey-provider: @provider' ``` @@ -416,7 +416,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key + provider="@PROVIDER" ) cancel_batch_response = portkey.batches.cancel(batch_id="batch_id") @@ -431,7 +431,7 @@ import { Portkey } from 'portkey-ai'; // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key + provider:"@PROVIDER" }); const cancelBatch = async () => { @@ -448,7 +448,7 @@ await cancelBatch(); ```sh curl --request POST --location 'https://api.portkey.ai/v1/batches//cancel' \ --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' +--header 'x-portkey-provider: @provider' ``` diff --git a/integrations/llms/azure-openai/files.mdx b/integrations/llms/azure-openai/files.mdx index 82ee50a0..aa4161e6 100644 --- a/integrations/llms/azure-openai/files.mdx +++ b/integrations/llms/azure-openai/files.mdx @@ -12,7 +12,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key + provider="@PROVIDER" ) upload_file_response = portkey.files.create( @@ -30,7 +30,7 @@ import { Portkey } from 'portkey-ai'; // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key + provider:"@PROVIDER" }); const uploadFile = async () => { @@ -50,7 +50,7 @@ await uploadFile(); ```sh curl --location --request POST 'https://api.portkey.ai/v1/files' \ --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-provider: @provider' \ --form 'purpose=""' \ --form 'file=@""' ``` @@ -115,7 +115,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key + provider="@PROVIDER" ) files = portkey.files.list() @@ -130,7 +130,7 @@ import { Portkey } from 'portkey-ai'; // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key + provider:"@PROVIDER" }); const listFiles = async () => { @@ -147,7 +147,7 @@ await listFiles(); ```sh curl --location 'https://api.portkey.ai/v1/files' \ --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' +--header 'x-portkey-provider: @provider' ``` @@ -205,7 +205,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key + provider="@PROVIDER" ) file = portkey.files.retrieve(file_id="file_id") @@ -220,7 +220,7 @@ import { Portkey } from 'portkey-ai'; // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key + provider:"@PROVIDER" }); const getFile = async () => { @@ -237,7 +237,7 @@ await getFile(); ```sh curl --location 'https://api.portkey.ai/v1/files/' \ --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' +--header 'x-portkey-provider: @provider' ``` @@ -295,7 +295,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key + provider="@PROVIDER" ) file_content = portkey.files.content(file_id="file_id") @@ -310,7 +310,7 @@ import { Portkey } from 'portkey-ai'; // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key + provider:"@PROVIDER" }); const getFileContent = async () => { @@ -327,7 +327,7 @@ await getFileContent(); ```sh curl --location 'https://api.portkey.ai/v1/files//content' \ --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' +--header 'x-portkey-provider: @provider' ``` @@ -385,7 +385,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key + provider="@PROVIDER" ) delete_file_response = portkey.files.delete(file_id="file_id") @@ -400,7 +400,7 @@ import { Portkey } from 'portkey-ai'; // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key + provider:"@PROVIDER" }); const deleteFile = async () => { @@ -417,7 +417,7 @@ await deleteFile(); ```sh curl --location --request DELETE 'https://api.portkey.ai/v1/files/' \ --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' +--header 'x-portkey-provider: @provider' ``` diff --git a/integrations/llms/azure-openai/fine-tuning.mdx b/integrations/llms/azure-openai/fine-tuning.mdx index d04a5644..7b94a59f 100644 --- a/integrations/llms/azure-openai/fine-tuning.mdx +++ b/integrations/llms/azure-openai/fine-tuning.mdx @@ -15,7 +15,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key for Azure OpenAI + provider="@PROVIDER" ) # Upload a file for fine-tuning @@ -35,7 +35,7 @@ import * as fs from 'fs'; // Initialize the Portkey client const portkey = Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key for Azure OpenAI + provider:"@PROVIDER" for Azure OpenAI }); (async () => { @@ -59,7 +59,7 @@ client = AzureOpenAI( api_version="2023-05-15", azure_endpoint=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - virtual_key="VIRTUAL_KEY", + provider="@PROVIDER", api_key="PORTKEY_API_KEY" ) ) @@ -76,7 +76,7 @@ print(file) ```sh curl -X POST --header 'x-portkey-api-key: ' \ - --header 'x-portkey-virtual-key: ' \ + --header 'x-portkey-provider: @provider' \ --form 'file=@dataset.jsonl' \ --form 'purpose=fine-tune' \ 'https://api.portkey.ai/v1/files' @@ -94,7 +94,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key for Azure OpenAI + provider="@PROVIDER" ) # Create a fine-tuning job @@ -118,7 +118,7 @@ import { Portkey } from "portkey-ai"; // Initialize the Portkey client const portkey = Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key for Azure OpenAI + provider:"@PROVIDER" for Azure OpenAI }); (async () => { @@ -147,7 +147,7 @@ client = AzureOpenAI( api_version="2023-05-15", azure_endpoint=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - virtual_key="VIRTUAL_KEY", + provider="@PROVIDER", api_key="PORTKEY_API_KEY" ) ) @@ -170,7 +170,7 @@ print(fine_tune_job) ```sh curl -X POST --header 'Content-Type: application/json' \ --header 'x-portkey-api-key: ' \ - --header 'x-portkey-virtual-key: ' \ + --header 'x-portkey-provider: @provider' \ --data \ $'{"model": "", "suffix": "", "training_file": "", "validation_file": "", "hyperparameters": {"n_epochs": 1}}\n' \ 'https://api.portkey.ai/v1/fine_tuning/jobs' diff --git a/integrations/llms/bedrock/aws-bedrock.mdx b/integrations/llms/bedrock/aws-bedrock.mdx index 1029581f..c5b6c7f3 100644 --- a/integrations/llms/bedrock/aws-bedrock.mdx +++ b/integrations/llms/bedrock/aws-bedrock.mdx @@ -60,7 +60,7 @@ There are two ways to integrate AWS Bedrock with Portkey: const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your Bedrock Virtual Key + provider:"@PROVIDER" // Your Bedrock Virtual Key }) ``` @@ -70,7 +70,7 @@ There are two ways to integrate AWS Bedrock with Portkey: portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Bedrock + provider="@PROVIDER" # Replace with your virtual key for Bedrock ) ``` @@ -88,7 +88,7 @@ If you're using [AWS Security Token Service](https://docs.aws.amazon.com/STS/lat const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your Bedrock Virtual Key, + provider:"@PROVIDER" // Your Bedrock Virtual Key, aws_session_token: "" }) ``` @@ -99,7 +99,7 @@ If you're using [AWS Security Token Service](https://docs.aws.amazon.com/STS/lat portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Bedrock, + provider="@PROVIDER" # Replace with your virtual key for Bedrock, aws_session_token="" ) ``` @@ -173,7 +173,7 @@ Note that you will have to set [`strict_open_ai_compliance=False`](/product/ai-g # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + provider="@PROVIDER", strict_openai_compliance=False ) @@ -216,7 +216,7 @@ Note that you will have to set [`strict_open_ai_compliance=False`](/product/ai-g // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY", // Add your bedrock's virtual key + provider:"@PROVIDER", // Add your bedrock's virtual key strictOpenAICompliance: false }); @@ -387,7 +387,7 @@ Note that you will have to set [`strict_open_ai_compliance=False`](/product/ai-g # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + provider="@PROVIDER", strict_openai_compliance=False ) @@ -434,7 +434,7 @@ Note that you will have to set [`strict_open_ai_compliance=False`](/product/ai-g // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY", // Add your bedrock's virtual key + provider:"@PROVIDER", // Add your bedrock's virtual key strictOpenAICompliance: false }); diff --git a/integrations/llms/bedrock/batches.mdx b/integrations/llms/bedrock/batches.mdx index e2b6b3f1..3d1945de 100644 --- a/integrations/llms/bedrock/batches.mdx +++ b/integrations/llms/bedrock/batches.mdx @@ -243,7 +243,7 @@ await listBatches(); ```sh curl --location 'https://api.portkey.ai/v1/batches' \ --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-provider: @provider' \ ``` @@ -307,7 +307,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + provider="@PROVIDER", ) batch = portkey.batches.retrieve(batch_id="batch_id") @@ -322,7 +322,7 @@ import { Portkey } from 'portkey-ai'; // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY", // Add your provider's virtual key + provider:"@PROVIDER", }); const getBatch = async () => { @@ -339,7 +339,7 @@ await getBatch(); ```sh curl --location 'https://api.portkey.ai/v1/batches/' \ --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-provider: @provider' \ ``` @@ -353,7 +353,7 @@ const openai = new OpenAI({ defaultHeaders: createHeaders({ provider: "openai", apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "BEDROCK_VIRTUAL_KEY", + provider:"@BEDROCK_PROVIDER", }) }); @@ -377,7 +377,7 @@ openai = OpenAI( default_headers=createHeaders( provider="openai", api_key="PORTKEY_API_KEY", - virtualKey: "BEDROCK_VIRTUAL_KEY", + provider:"@BEDROCK_PROVIDER", ) ) @@ -396,7 +396,7 @@ print(batch) ```sh curl --location 'https://api.portkey.ai/v1/batches//output' \ --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-provider: @provider' \ ``` @@ -411,7 +411,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + provider="@PROVIDER", ) batches = portkey.batches.list() @@ -426,7 +426,7 @@ import { Portkey } from 'portkey-ai'; // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY", // Add your provider's virtual key + provider:"@PROVIDER", }); const listBatchingJobs = async () => { @@ -443,7 +443,7 @@ await listBatchingJobs(); ```sh curl --location 'https://api.portkey.ai/v1/batches' \ --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-provider: @provider' \ ``` @@ -457,7 +457,7 @@ const openai = new OpenAI({ defaultHeaders: createHeaders({ provider: "openai", apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "BEDROCK_VIRTUAL_KEY", + provider:"@BEDROCK_PROVIDER", }) }); @@ -481,7 +481,7 @@ openai = OpenAI( default_headers=createHeaders( provider="openai", api_key="PORTKEY_API_KEY", - virtualKey: "BEDROCK_VIRTUAL_KEY", + provider:"@BEDROCK_PROVIDER", ) ) @@ -502,7 +502,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + provider="@PROVIDER", ) cancel_batch_response = portkey.batches.cancel(batch_id="batch_id") @@ -517,7 +517,7 @@ import { Portkey } from 'portkey-ai'; // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY", // Add your provider's virtual key + provider:"@PROVIDER", }); const cancelBatch = async () => { @@ -534,7 +534,7 @@ await cancelBatch(); ```sh curl --request POST --location 'https://api.portkey.ai/v1/batches//cancel' \ --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-provider: @provider' \ ``` @@ -548,7 +548,7 @@ const openai = new OpenAI({ defaultHeaders: createHeaders({ provider: "openai", apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "BEDROCK_VIRTUAL_KEY", + provider:"@BEDROCK_PROVIDER", }) }); @@ -572,7 +572,7 @@ openai = OpenAI( default_headers=createHeaders( provider="openai", api_key="PORTKEY_API_KEY", - virtualKey: "BEDROCK_VIRTUAL_KEY", + provider:"@BEDROCK_PROVIDER", ) ) diff --git a/integrations/llms/bedrock/embeddings.mdx b/integrations/llms/bedrock/embeddings.mdx index e00f7cfe..43d6b33b 100644 --- a/integrations/llms/bedrock/embeddings.mdx +++ b/integrations/llms/bedrock/embeddings.mdx @@ -15,7 +15,7 @@ Portkey provides a standardized interface for embedding multiple modalities. client = Portkey( api_key="YOUR_PORTKEY_API_KEY", # defaults to os.environ.get("PORTKEY_API_KEY") - virtual_key="VIRTUAL_KEY", + provider="@PROVIDER", ) embeddings = client.embeddings.create( @@ -32,7 +32,7 @@ Portkey provides a standardized interface for embedding multiple modalities. const portkey = new Portkey({ apiKey: "YOUR_API_KEY", - virtualKey: "YOUR_VIRTUAL_KEY" + provider:"@YOUR_PROVIDER" }); const embedding = await portkey.embeddings.create({ @@ -50,7 +50,7 @@ Portkey provides a standardized interface for embedding multiple modalities. curl --location 'https://api.portkey.ai/v1/embeddings' \ --header 'Content-Type: application/json' \ --header 'x-portkey-api-key: PORTKEY_API_KEY' \ - --header 'x-portkey-virtual-key: PORTKEY_VIRTUAL_KEY' \ + --header 'x-portkey-provider: PORTKEY_PROVIDER' \ --data-raw '{ "model": "amazon.titan-embed-text-v2:0", "input": "Hello this is a test", @@ -92,7 +92,7 @@ Portkey provides a standardized interface for embedding multiple modalities. defaultHeaders: createHeaders({ provider: "vertex-ai", apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "PORTKEY_VIRTUAL_KEY" + provider:"@PORTKEY_PROVIDER" }) }); @@ -117,7 +117,7 @@ Portkey provides a standardized interface for embedding multiple modalities. client = Portkey( api_key="YOUR_PORTKEY_API_KEY", # defaults to os.environ.get("PORTKEY_API_KEY") - virtual_key="VIRTUAL_KEY", + provider="@PROVIDER", ) embeddings = client.embeddings.create( @@ -139,7 +139,7 @@ Portkey provides a standardized interface for embedding multiple modalities. const portkey = new Portkey({ apiKey: "YOUR_API_KEY", - virtualKey: "YOUR_VIRTUAL_KEY" + provider:"@YOUR_PROVIDER" }); const embedding = await portkey.embeddings.create({ @@ -162,7 +162,7 @@ Portkey provides a standardized interface for embedding multiple modalities. curl --location 'https://api.portkey.ai/v1/embeddings' \ --header 'Content-Type: application/json' \ --header 'x-portkey-api-key: PORTKEY_API_KEY' \ - --header 'x-portkey-virtual-key: PORTKEY_VIRTUAL_KEY' \ + --header 'x-portkey-provider: PORTKEY_PROVIDER' \ --data-raw '{ "model": "amazon.titan-embed-image-v1", "dimensions": 256, @@ -213,7 +213,7 @@ Portkey provides a standardized interface for embedding multiple modalities. baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "PORTKEY_VIRTUAL_KEY" + provider:"@PORTKEY_PROVIDER" }) }); @@ -244,7 +244,7 @@ Portkey provides a standardized interface for embedding multiple modalities. client = Portkey( api_key="YOUR_PORTKEY_API_KEY", # defaults to os.environ.get("PORTKEY_API_KEY") - virtual_key="VIRTUAL_KEY", + provider="@PROVIDER", ) embeddings = client.embeddings.create( @@ -259,7 +259,7 @@ Portkey provides a standardized interface for embedding multiple modalities. const portkey = new Portkey({ apiKey: "YOUR_API_KEY", - virtualKey: "YOUR_VIRTUAL_KEY" + provider:"@YOUR_PROVIDER" }); const embedding = await portkey.embeddings.create({ @@ -275,7 +275,7 @@ Portkey provides a standardized interface for embedding multiple modalities. curl --location 'https://api.portkey.ai/v1/embeddings' \ --header 'Content-Type: application/json' \ --header 'x-portkey-api-key: PORTKEY_API_KEY' \ - --header 'x-portkey-virtual-key: PORTKEY_VIRTUAL_KEY' \ + --header 'x-portkey-provider: PORTKEY_PROVIDER' \ --data-raw '{ "model": "cohere.embed-english-v3", "input": ["Hello this is a test", "skibidi"], @@ -313,7 +313,7 @@ Portkey provides a standardized interface for embedding multiple modalities. defaultHeaders: createHeaders({ provider: "vertex-ai", apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "PORTKEY_VIRTUAL_KEY" + provider:"@PORTKEY_PROVIDER" }) }); @@ -336,7 +336,7 @@ Portkey provides a standardized interface for embedding multiple modalities. client = Portkey( api_key="YOUR_PORTKEY_API_KEY", # defaults to os.environ.get("PORTKEY_API_KEY") - virtual_key="VIRTUAL_KEY", + provider="@PROVIDER", ) embeddings = client.embeddings.create( @@ -358,7 +358,7 @@ Portkey provides a standardized interface for embedding multiple modalities. const portkey = new Portkey({ apiKey: "YOUR_API_KEY", - virtualKey: "YOUR_VIRTUAL_KEY" + provider:"@YOUR_PROVIDER" }); const embedding = await portkey.embeddings.create({ @@ -381,7 +381,7 @@ Portkey provides a standardized interface for embedding multiple modalities. curl --location 'https://api.portkey.ai/v1/embeddings' \ --header 'Content-Type: application/json' \ --header 'x-portkey-api-key: PORTKEY_API_KEY' \ - --header 'x-portkey-virtual-key: PORTKEY_VIRTUAL_KEY' \ + --header 'x-portkey-provider: PORTKEY_PROVIDER' \ --data-raw '{ "model": "cohere.embed-english-v3", "input_type": "image", @@ -432,7 +432,7 @@ Portkey provides a standardized interface for embedding multiple modalities. baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "PORTKEY_VIRTUAL_KEY" + provider:"@PORTKEY_PROVIDER" }) }); diff --git a/integrations/llms/bedrock/files.mdx b/integrations/llms/bedrock/files.mdx index 8f5fa583..622a803a 100644 --- a/integrations/llms/bedrock/files.mdx +++ b/integrations/llms/bedrock/files.mdx @@ -22,7 +22,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + provider="@PROVIDER", provider="bedrock", aws_region="YOUR_AWS_REGION", aws_s3_bucket="YOUR_AWS_S3_BUCKET", @@ -47,7 +47,7 @@ import { Portkey } from 'portkey-ai'; // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY", // Add your provider's virtual key + provider:"@PROVIDER", provider: "bedrock", awsRegion: "YOUR_AWS_REGION", awsS3Bucket: "YOUR_AWS_S3_BUCKET", @@ -162,7 +162,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + provider="@PROVIDER", aws_region="YOUR_AWS_REGION", ) @@ -178,7 +178,7 @@ import { Portkey } from 'portkey-ai'; // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY", // Add your provider's virtual key + provider:"@PROVIDER", awsRegion="YOUR_AWS_REGION", }); @@ -196,7 +196,7 @@ await getFile(); ```sh curl --location 'https://api.portkey.ai/v1/files/' \ --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-provider: @provider' \ --header 'x-portkey-aws-region: ' ``` @@ -257,7 +257,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + provider="@PROVIDER", aws_region="YOUR_AWS_REGION", ) @@ -273,7 +273,7 @@ import { Portkey } from 'portkey-ai'; // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY", // Add your provider's virtual key + provider:"@PROVIDER", awsRegion="YOUR_AWS_REGION", }); @@ -291,7 +291,7 @@ await getFileContent(); ```sh curl --location 'https://api.portkey.ai/v1/files//content' \ --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-provider: @provider' \ --header 'x-portkey-aws-region: ' ``` diff --git a/integrations/llms/bedrock/fine-tuning.mdx b/integrations/llms/bedrock/fine-tuning.mdx index 5a33db16..5b8d67f4 100644 --- a/integrations/llms/bedrock/fine-tuning.mdx +++ b/integrations/llms/bedrock/fine-tuning.mdx @@ -18,7 +18,7 @@ from portkey_ai import Portkey portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key -virtual_key="VIRTUAL_KEY" # Add your provider's virtual key +provider="@PROVIDER" ) fine_tune_job = portkey.fine_tuning.jobs.create( @@ -44,7 +44,7 @@ import { Portkey } from "portkey-ai"; # Initialize the Portkey client const portkey = Portkey( apiKey="PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey="VIRTUAL_KEY" // Add your provider's virtual key + provider="@PROVIDER" ) (async () => { @@ -75,7 +75,7 @@ openai = OpenAI( api_key='OPENAI_API_KEY', base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - virtual_key="VIRTUAL_KEY", + provider="@PROVIDER", api_key="PORTKEY_API_KEY" ) ) @@ -105,7 +105,7 @@ const openai = new OpenAI({ apiKey: 'OPENAI_API_KEY', // defaults to process.env["OPENAI_API_KEY"], baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ - virtualKey: "VIRTUAL_KEY", + provider:"@PROVIDER", apiKey: "PORTKEY_API_KEY" // defaults to process.env["PORTKEY_API_KEY"] }) }); @@ -134,7 +134,7 @@ const openai = new OpenAI({ curl \ --header 'Content-Type: application/json' \ --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-provider: @provider' \ --header 'x-portkey-aws-s3-bucket: ' \ --data '{ "model": "", @@ -173,7 +173,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key + provider="@PROVIDER" ) # List all fine-tuning jobs @@ -191,7 +191,7 @@ import { Portkey } from "portkey-ai"; // Initialize the Portkey client const portkey = Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key + provider:"@PROVIDER" }); (async () => { @@ -213,7 +213,7 @@ openai = OpenAI( api_key='OPENAI_API_KEY', base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - virtual_key="VIRTUAL_KEY", + provider="@PROVIDER", api_key="PORTKEY_API_KEY" ) ) @@ -235,7 +235,7 @@ const openai = new OpenAI({ apiKey: 'OPENAI_API_KEY', baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ - virtualKey: "VIRTUAL_KEY", + provider:"@PROVIDER", apiKey: "PORTKEY_API_KEY" }) }); @@ -255,7 +255,7 @@ const openai = new OpenAI({ curl \ --header 'Content-Type: application/json' \ --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-provider: @provider' \ 'https://api.portkey.ai/v1/fine_tuning/jobs?limit=10' ``` @@ -271,7 +271,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key + provider="@PROVIDER" ) # Retrieve a specific fine-tuning job @@ -289,7 +289,7 @@ import { Portkey } from "portkey-ai"; // Initialize the Portkey client const portkey = Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key + provider:"@PROVIDER" }); (async () => { @@ -311,7 +311,7 @@ openai = OpenAI( api_key='OPENAI_API_KEY', base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - virtual_key="VIRTUAL_KEY", + provider="@PROVIDER", api_key="PORTKEY_API_KEY" ) ) @@ -333,7 +333,7 @@ const openai = new OpenAI({ apiKey: 'OPENAI_API_KEY', baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ - virtualKey: "VIRTUAL_KEY", + provider:"@PROVIDER", apiKey: "PORTKEY_API_KEY" }) }); @@ -353,7 +353,7 @@ const openai = new OpenAI({ curl \ --header 'Content-Type: application/json' \ --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-provider: @provider' \ 'https://api.portkey.ai/v1/fine_tuning/jobs/' ``` @@ -369,7 +369,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key + provider="@PROVIDER" ) # Cancel a fine-tuning job @@ -387,7 +387,7 @@ import { Portkey } from "portkey-ai"; // Initialize the Portkey client const portkey = Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key + provider:"@PROVIDER" }); (async () => { @@ -409,7 +409,7 @@ openai = OpenAI( api_key='OPENAI_API_KEY', base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - virtual_key="VIRTUAL_KEY", + provider="@PROVIDER", api_key="PORTKEY_API_KEY" ) ) @@ -431,7 +431,7 @@ const openai = new OpenAI({ apiKey: 'OPENAI_API_KEY', baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ - virtualKey: "VIRTUAL_KEY", + provider:"@PROVIDER", apiKey: "PORTKEY_API_KEY" }) }); @@ -452,7 +452,7 @@ curl \ --request POST \ --header 'Content-Type: application/json' \ --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-provider: @provider' \ 'https://api.portkey.ai/v1/fine_tuning/jobs//cancel' ``` diff --git a/integrations/llms/bedrock/prompt-caching.mdx b/integrations/llms/bedrock/prompt-caching.mdx index f67a0a3f..0405ba4b 100644 --- a/integrations/llms/bedrock/prompt-caching.mdx +++ b/integrations/llms/bedrock/prompt-caching.mdx @@ -42,7 +42,7 @@ import Portkey from 'portkey-ai' const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your Bedrock Virtual Key + provider:"@PROVIDER" // Your Bedrock Virtual Key }) const chatCompletion = await portkey.chat.completions.create({ @@ -69,7 +69,7 @@ from portkey_ai import Portkey portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="BEDROCK_VIRTUAL_KEY", + provider="@BEDROCK_PROVIDER", ) chat_completion = portkey.chat.completions.create( @@ -158,7 +158,7 @@ print(chat_completion.choices[0].message.content) ```sh REST API curl https://api.portkey.ai/v1/chat/completions \ -H "Content-Type: application/json" \ - -H "x-portkey-virtual-key: $BEDROCK_VIRTUAL_KEY" \ + -H "x-portkey-provider: $BEDROCK_PROVIDER" \ -H "x-portkey-api-key: $PORTKEY_API_KEY" \ -d '{ "model": "anthropic.claude-3-7-sonnet-20250219-v1:0", diff --git a/integrations/llms/byollm.mdx b/integrations/llms/byollm.mdx index 095efb4b..8fac3ede 100644 --- a/integrations/llms/byollm.mdx +++ b/integrations/llms/byollm.mdx @@ -53,7 +53,7 @@ import Portkey from 'portkey-ai' const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", - virtualKey: "YOUR_PRIVATE_LLM_VIRTUAL_KEY" + provider:"@YOUR_PRIVATE_LLM_PROVIDER" }) async function main() { @@ -73,7 +73,7 @@ from portkey_ai import Portkey portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="YOUR_PRIVATE_LLM_VIRTUAL_KEY" + provider="@YOUR_PRIVATE_LLM_PROVIDER" ) response = portkey.chat.completions.create( @@ -90,7 +90,7 @@ print(response.choices[0].message.content) curl https://api.portkey.ai/v1/chat/completions \ -H "Content-Type: application/json" \ -H "x-portkey-api-key: $PORTKEY_API_KEY" \ - -H "x-portkey-virtual-key: $YOUR_PRIVATE_LLM_VIRTUAL_KEY" \ + -H "x-portkey-provider: $YOUR_PRIVATE_LLM_PROVIDER" \ -d '{ "model": "YOUR_MODEL_NAME", "messages": [ @@ -275,7 +275,7 @@ Private LLMs work seamlessly with all Portkey Gateway features. Some common use "forward_headers": ["Authorization"] }, { - "virtual_key": "openai-key" + "provider":"@openai-key" } ] } @@ -315,7 +315,7 @@ Portkey provides comprehensive observability for your private LLM deployments, j You have two options: - 1. Create separate virtual keys for each endpoint + 1. Create separate integration for each endpoint 2. Use Gateway Configs with load balancing to distribute traffic across multiple endpoints diff --git a/integrations/llms/cerebras.mdx b/integrations/llms/cerebras.mdx index 73092951..23c4bf29 100644 --- a/integrations/llms/cerebras.mdx +++ b/integrations/llms/cerebras.mdx @@ -39,7 +39,7 @@ To use Cerebras with Portkey, get your API key from [here](https://cerebras.ai/i const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "CEREBRAS_VIRTUAL_KEY" // Your Cerebras Inference virtual key + provider:"@CEREBRAS_PROVIDER" // Your Cerebras Inference virtual key }) ``` @@ -49,7 +49,7 @@ To use Cerebras with Portkey, get your API key from [here](https://cerebras.ai/i portkey = Portkey( api_key ="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="CEREBRAS_VIRTUAL_KEY" # Your Cerebras Inference virtual key + provider="@CEREBRAS_PROVIDER" # Your Cerebras Inference virtual key ) ``` diff --git a/integrations/llms/cohere.mdx b/integrations/llms/cohere.mdx index 1c7d0c7e..c3dabd05 100644 --- a/integrations/llms/cohere.mdx +++ b/integrations/llms/cohere.mdx @@ -43,7 +43,7 @@ To use Cohere with Portkey, [get your API key from here](https://dashboard.coher const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your Cohere Virtual Key + provider:"@PROVIDER" // Your Cohere Virtual Key }) ``` @@ -53,7 +53,7 @@ To use Cohere with Portkey, [get your API key from here](https://dashboard.coher portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Cohere + provider="@PROVIDER" # Replace with your virtual key for Cohere ) ``` diff --git a/integrations/llms/dashscope.mdx b/integrations/llms/dashscope.mdx index 6d78f570..41e2ecc7 100644 --- a/integrations/llms/dashscope.mdx +++ b/integrations/llms/dashscope.mdx @@ -42,7 +42,7 @@ Portkey offers native integrations with [dashscope](https://dashscope.aliyun.com portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Dashscope + provider="@PROVIDER" # Replace with your virtual key for Dashscope ) ``` @@ -51,7 +51,7 @@ Portkey offers native integrations with [dashscope](https://dashscope.aliyun.com const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your Dashscope Virtual Key + provider:"@PROVIDER" // Your Dashscope Virtual Key }) ``` @@ -256,11 +256,11 @@ Here's a simplified version of how to use Portkey's Gateway Configuration: "targets": [ { "name": "qwen-turbo", - "virtual_key": "xx" + "provider":"@xx" }, { "name": "gpt-3.5", - "virtual_key": "yy" + "provider":"@yy" } ] } @@ -281,7 +281,7 @@ Here's a simplified version of how to use Portkey's Gateway Configuration: portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="VIRTUAL_KEY", + provider="@PROVIDER", config=portkey_config ) ``` @@ -291,7 +291,7 @@ Here's a simplified version of how to use Portkey's Gateway Configuration: const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", - virtualKey: "VIRTUAL_KEY", + provider:"@PROVIDER", config: portkeyConfig }) ``` @@ -322,7 +322,7 @@ Portkey's AI gateway enables you to enforce input/output checks on requests by a ```json { - "virtual_key":"dashscope-xxx", + "provider:"@dashscope-xxx", "before_request_hooks": [{ "id": "input-guardrail-id-xx" }], diff --git a/integrations/llms/deepbricks.mdx b/integrations/llms/deepbricks.mdx index 19918067..f49c5761 100644 --- a/integrations/llms/deepbricks.mdx +++ b/integrations/llms/deepbricks.mdx @@ -43,7 +43,7 @@ To use Deepbricks with Portkey, [get your API key from here](https://deepbricks. const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your Deepbricks + provider:"@PROVIDER" // Your Deepbricks }) ``` @@ -52,7 +52,7 @@ To use Deepbricks with Portkey, [get your API key from here](https://deepbricks. from portkey_ai import Portkey portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Deepbricks + provider="@PROVIDER" # Replace with your virtual key for Deepbricks ) ``` diff --git a/integrations/llms/deepinfra.mdx b/integrations/llms/deepinfra.mdx index f0f286dc..2ed0d3ac 100644 --- a/integrations/llms/deepinfra.mdx +++ b/integrations/llms/deepinfra.mdx @@ -41,7 +41,7 @@ To use Deepinfra with Virtual Key, [get your API key from here](https://deepinfr const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your Deepinfra Virtual Key + provider:"@PROVIDER" // Your Deepinfra Virtual Key }) ``` @@ -51,7 +51,7 @@ To use Deepinfra with Virtual Key, [get your API key from here](https://deepinfr portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="DEEPINFRA_VIRTUAL_KEY" + provider="@DEEPINFRA_PROVIDER" ) ``` diff --git a/integrations/llms/deepseek.mdx b/integrations/llms/deepseek.mdx index 4d162b9f..88f0d7b1 100644 --- a/integrations/llms/deepseek.mdx +++ b/integrations/llms/deepseek.mdx @@ -43,7 +43,7 @@ To use DeepSeek with Portkey, [get your API key from here](https://platform.deep const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your DeepSeek Virtual Key + provider:"@PROVIDER" // Your DeepSeek Virtual Key }) ``` @@ -53,7 +53,7 @@ To use DeepSeek with Portkey, [get your API key from here](https://platform.deep portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for DeepSeek + provider="@PROVIDER" # Replace with your virtual key for DeepSeek ) ``` @@ -93,7 +93,7 @@ Use the Portkey instance to send requests to DeepSeek. You can also override the ```javascript const client = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your DeepSeek Virtual Key + provider:"@PROVIDER" // Your DeepSeek Virtual Key }) // Function to send chat messages and get a response @@ -134,7 +134,7 @@ Use the Portkey instance to send requests to DeepSeek. You can also override the ```python client = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for DeepSeek + provider="@PROVIDER" # Replace with your virtual key for DeepSeek ) # Round 1 @@ -167,7 +167,7 @@ Use the Portkey instance to send requests to DeepSeek. You can also override the ```javascript const client = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your DeepSeek Virtual Key + provider:"@PROVIDER" // Your DeepSeek Virtual Key }) const systemPrompt = ` @@ -209,7 +209,7 @@ Use the Portkey instance to send requests to DeepSeek. You can also override the client = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for DeepSeek + provider="@PROVIDER" # Replace with your virtual key for DeepSeek ) system_prompt = """ diff --git a/integrations/llms/fireworks.mdx b/integrations/llms/fireworks.mdx index c6b8d81f..7d7bec12 100644 --- a/integrations/llms/fireworks.mdx +++ b/integrations/llms/fireworks.mdx @@ -40,7 +40,7 @@ To use Fireworks with Portkey, [get your API key from here](https://fireworks.ai const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "FIREWORKS_VIRTUAL_KEY" // Your Virtual Key + provider:"@FIREWORKS_PROVIDER" // Your Virtual Key }) ``` @@ -50,7 +50,7 @@ To use Fireworks with Portkey, [get your API key from here](https://fireworks.ai portkey = Portkey( api_key="PORTKEY_API_KEY", # Defaults to os.env("PORTKEY_API_KEY") - virtual_key="FIREWORKS_VIRTUAL_KEY" # Your Virtual Key + provider="@FIREWORKS_PROVIDER" # Your Virtual Key ) ``` @@ -174,7 +174,7 @@ Portkey also supports calling [image generation models hosted on Fireworks](http const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", - virtualKey: "FIREWORKS_VIRTUAL_KEY" + provider:"@FIREWORKS_PROVIDER" }); async function main(){ @@ -200,7 +200,7 @@ Portkey also supports calling [image generation models hosted on Fireworks](http portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="FIREWORKS_VIRTUAL_KEY" + provider="@FIREWORKS_PROVIDER" ) image = portkey.images.generate( @@ -231,7 +231,7 @@ Let's say you want to classify patient requests into 3 pre-defined classes: portkey = Portkey( api_key="PORTKEY_API_KEY", # Defaults to os.env("PORTKEY_API_KEY") - virtual_key="FIREWORKS_VIRTUAL_KEY" # Your Virtual Key + provider="@FIREWORKS_PROVIDER" # Your Virtual Key ) patient_classification = """ @@ -254,7 +254,7 @@ Let's say you want to classify patient requests into 3 pre-defined classes: const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "FIREWORKS_VIRTUAL_KEY" // Your Virtual Key + provider:"@FIREWORKS_PROVIDER" // Your Virtual Key }) const patient_classification = ` @@ -290,7 +290,7 @@ You can force the model to return (1) **An arbitrary JSON**, or (2) **JSON with portkey = Portkey( api_key="PORTKEY_API_KEY", # Defaults to os.env("PORTKEY_API_KEY") - virtual_key="FIREWORKS_VIRTUAL_KEY" # Your Virtual Key + provider="@FIREWORKS_PROVIDER" # Your Virtual Key ) class Recipe(BaseModel): @@ -317,7 +317,7 @@ You can force the model to return (1) **An arbitrary JSON**, or (2) **JSON with const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "FIREWORKS_VIRTUAL_KEY" // Your Virtual Key + provider:"@FIREWORKS_PROVIDER" // Your Virtual Key }) asyn function main(){ diff --git a/integrations/llms/fireworks/files.mdx b/integrations/llms/fireworks/files.mdx index bc0555ec..0f3d3d39 100644 --- a/integrations/llms/fireworks/files.mdx +++ b/integrations/llms/fireworks/files.mdx @@ -13,7 +13,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + provider="@PROVIDER", provider="fireworks-ai", fireworks_account_id="FIREWORKS_ACCOUNT_ID" ) @@ -33,7 +33,7 @@ import { Portkey } from 'portkey-ai'; // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY", // Add your provider's virtual key + provider:"@PROVIDER", provider: "fireworks-ai", fireworksAccountId: "FIREWORKS_ACCOUNT_ID" }); @@ -126,7 +126,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + provider="@PROVIDER", fireworks_account_id="FIREWORKS_ACCOUNT_ID" ) @@ -142,7 +142,7 @@ import { Portkey } from 'portkey-ai'; // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY", // Add your provider's virtual key + provider:"@PROVIDER", fireworksAccountId="FIREWORKS_ACCOUNT_ID", }); @@ -160,7 +160,7 @@ await getFile(); ```sh curl --location 'https://api.portkey.ai/v1/files/' \ --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-provider: @provider' \ --header 'x-portkey-fireworks-account-id: {YOUR_FIREWORKS_ACCOUNT_ID}' ``` @@ -221,7 +221,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + provider="@PROVIDER", fireworks_account_id="FIREWORKS_ACCOUNT_ID", ) @@ -237,7 +237,7 @@ import { Portkey } from 'portkey-ai'; // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY", // Add your provider's virtual key + provider:"@PROVIDER", fireworksAccountId="FIREWORKS_ACCOUNT_ID", }); @@ -255,7 +255,7 @@ await getFileContent(); ```sh curl --location 'https://api.portkey.ai/v1/files//content' \ --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-provider: @provider' \ --header 'x-portkey-fireworks-account-id: {YOUR_FIREWORKS_ACCOUNT_ID}' ``` diff --git a/integrations/llms/fireworks/fine-tuning.mdx b/integrations/llms/fireworks/fine-tuning.mdx index c92cd7da..58c9f348 100644 --- a/integrations/llms/fireworks/fine-tuning.mdx +++ b/integrations/llms/fireworks/fine-tuning.mdx @@ -18,7 +18,7 @@ from portkey_ai import Portkey portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + provider="@PROVIDER", fireworks_account_id="FIREWORKS_ACCOUNT_ID" # Add your fireworks's account id ) @@ -42,7 +42,7 @@ import { Portkey } from "portkey-ai"; // Initialize the Portkey client const portkey = Portkey( apiKey="PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey="VIRTUAL_KEY", // Add your provider's virtual key + provider="@PROVIDER", fireworksAccountId="FIREWORKS_ACCOUNT_ID" // Add your fireworks's account id ) @@ -71,7 +71,7 @@ openai = OpenAI( api_key='OPENAI_API_KEY', base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - virtual_key="VIRTUAL_KEY", + provider="@PROVIDER", api_key="PORTKEY_API_KEY", fireworks_account_id="FIREWORKS_ACCOUNT_ID" ) @@ -99,7 +99,7 @@ const openai = new OpenAI({ apiKey: 'OPENAI_API_KEY', // defaults to process.env["OPENAI_API_KEY"], baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ - virtualKey: "VIRTUAL_KEY", + provider:"@PROVIDER", apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] fireworksAccountId: "FIREWORKS_ACCOUNT_ID" }) @@ -126,7 +126,7 @@ const openai = new OpenAI({ curl \ --header 'Content-Type: application/json' \ --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-provider: @provider' \ --header 'x-portkey-fireworks-account-id: ' \ --data '{ "model": "", @@ -152,7 +152,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + provider="@PROVIDER", fireworks_account_id="FIREWORKS_ACCOUNT_ID" # Add your fireworks's account id ) @@ -171,7 +171,7 @@ import { Portkey } from "portkey-ai"; // Initialize the Portkey client const portkey = Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY", // Add your provider's virtual key + provider:"@PROVIDER", fireworksAccountId: "FIREWORKS_ACCOUNT_ID" // Add your fireworks's account id }); @@ -194,7 +194,7 @@ openai = OpenAI( api_key='OPENAI_API_KEY', base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - virtual_key="VIRTUAL_KEY", + provider="@PROVIDER", api_key="PORTKEY_API_KEY", fireworks_account_id="FIREWORKS_ACCOUNT_ID" ) @@ -217,7 +217,7 @@ const openai = new OpenAI({ apiKey: 'OPENAI_API_KEY', baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ - virtualKey: "VIRTUAL_KEY", + provider:"@PROVIDER", apiKey: "PORTKEY_API_KEY", fireworksAccountId: "FIREWORKS_ACCOUNT_ID" }) @@ -238,7 +238,7 @@ const openai = new OpenAI({ curl \ --header 'Content-Type: application/json' \ --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-provider: @provider' \ --header 'x-portkey-fireworks-account-id: ' \ 'https://api.portkey.ai/v1/fine_tuning/jobs?limit=10' ``` @@ -255,7 +255,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + provider="@PROVIDER", fireworks_account_id="FIREWORKS_ACCOUNT_ID" # Add your fireworks's account id ) @@ -274,7 +274,7 @@ import { Portkey } from "portkey-ai"; // Initialize the Portkey client const portkey = Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY", // Add your provider's virtual key + provider:"@PROVIDER", fireworksAccountId: "FIREWORKS_ACCOUNT_ID" // Add your fireworks's account id }); @@ -297,7 +297,7 @@ openai = OpenAI( api_key='OPENAI_API_KEY', base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - virtual_key="VIRTUAL_KEY", + provider="@PROVIDER", api_key="PORTKEY_API_KEY", fireworks_account_id="FIREWORKS_ACCOUNT_ID" ) @@ -320,7 +320,7 @@ const openai = new OpenAI({ apiKey: 'OPENAI_API_KEY', baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ - virtualKey: "VIRTUAL_KEY", + provider:"@PROVIDER", apiKey: "PORTKEY_API_KEY", fireworksAccountId: "FIREWORKS_ACCOUNT_ID" }) @@ -341,7 +341,7 @@ const openai = new OpenAI({ curl \ --header 'Content-Type: application/json' \ --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-provider: @provider' \ --header 'x-portkey-fireworks-account-id: ' \ 'https://api.portkey.ai/v1/fine_tuning/jobs/' ``` @@ -358,7 +358,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + provider="@PROVIDER", fireworks_account_id="FIREWORKS_ACCOUNT_ID" # Add your fireworks's account id ) @@ -377,7 +377,7 @@ import { Portkey } from "portkey-ai"; // Initialize the Portkey client const portkey = Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY", // Add your provider's virtual key + provider:"@PROVIDER", fireworksAccountId: "FIREWORKS_ACCOUNT_ID" // Add your fireworks's account id }); @@ -400,7 +400,7 @@ openai = OpenAI( api_key='OPENAI_API_KEY', base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - virtual_key="VIRTUAL_KEY", + provider="@PROVIDER", api_key="PORTKEY_API_KEY", fireworks_account_id="FIREWORKS_ACCOUNT_ID" ) @@ -423,7 +423,7 @@ const openai = new OpenAI({ apiKey: 'OPENAI_API_KEY', baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ - virtualKey: "VIRTUAL_KEY", + provider:"@PROVIDER", apiKey: "PORTKEY_API_KEY", fireworksAccountId: "FIREWORKS_ACCOUNT_ID" }) @@ -445,7 +445,7 @@ curl \ --request POST \ --header 'Content-Type: application/json' \ --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-provider: @provider' \ --header 'x-portkey-fireworks-account-id: ' \ 'https://api.portkey.ai/v1/fine_tuning/jobs//cancel' ``` diff --git a/integrations/llms/gemini.mdx b/integrations/llms/gemini.mdx index ca5c17d0..14b94b17 100644 --- a/integrations/llms/gemini.mdx +++ b/integrations/llms/gemini.mdx @@ -40,7 +40,7 @@ import Portkey from 'portkey-ai' const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your Google Virtual Key + provider:"@PROVIDER" // Your Google Virtual Key }) ``` @@ -50,7 +50,7 @@ const portkey = new Portkey({ portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Google + provider="@PROVIDER" # Replace with your virtual key for Google ) ``` @@ -258,7 +258,7 @@ Note that you will have to set [`strict_open_ai_compliance=False`](/product/ai-g # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + provider="@PROVIDER", strict_open_ai_compliance=False ) @@ -301,7 +301,7 @@ Note that you will have to set [`strict_open_ai_compliance=False`](/product/ai-g // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY", // your vertex-ai virtual key + provider:"@PROVIDER", // your vertex-ai virtual key strictOpenAiCompliance: false }); diff --git a/integrations/llms/github.mdx b/integrations/llms/github.mdx index 666ca35f..3b9efb39 100644 --- a/integrations/llms/github.mdx +++ b/integrations/llms/github.mdx @@ -39,7 +39,7 @@ To use Github with Portkey, get your API key [from here](https://github.com/sett const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "GITHUB_VIRTUAL_KEY" // Your Github Models virtual key + provider:"@GITHUB_PROVIDER" // Your Github Models virtual key }) ``` @@ -49,7 +49,7 @@ To use Github with Portkey, get your API key [from here](https://github.com/sett portkey = Portkey( api_key ="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="GITHUB_VIRTUAL_KEY" # Your Github Models virtual key + provider="@GITHUB_PROVIDER" # Your Github Models virtual key ) ``` diff --git a/integrations/llms/google-palm.mdx b/integrations/llms/google-palm.mdx index 5f3cff17..5ceaf96c 100644 --- a/integrations/llms/google-palm.mdx +++ b/integrations/llms/google-palm.mdx @@ -45,7 +45,7 @@ Set up Portkey with your virtual key as part of the initialization configuration apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your Google Palm Virtual Key + provider:"@PROVIDER" // Your Google Palm Virtual Key }) ``` @@ -58,7 +58,7 @@ Set up Portkey with your virtual key as part of the initialization configuration api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Google Palm + provider="@PROVIDER" # Replace with your virtual key for Google Palm ) ``` diff --git a/integrations/llms/groq.mdx b/integrations/llms/groq.mdx index 3ff1d459..a6728562 100644 --- a/integrations/llms/groq.mdx +++ b/integrations/llms/groq.mdx @@ -45,7 +45,7 @@ import Portkey from 'portkey-ai' const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your Groq Virtual Key + provider:"@PROVIDER" // Your Groq Virtual Key }) ``` @@ -55,7 +55,7 @@ const portkey = new Portkey({ portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Groq + provider="@PROVIDER" # Replace with your virtual key for Groq ) ``` diff --git a/integrations/llms/huggingface.mdx b/integrations/llms/huggingface.mdx index 797aa952..d21d98ae 100644 --- a/integrations/llms/huggingface.mdx +++ b/integrations/llms/huggingface.mdx @@ -46,7 +46,7 @@ import Portkey from 'portkey-ai' const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY", // Your Huggingface Virtual Key + provider:"@PROVIDER", // Your Huggingface Virtual Key huggingfaceBaseUrl: "HUGGINGFACE_DEDICATED_URL" // Optional: Use this if you have a dedicated server hosted on Huggingface }) ``` @@ -58,7 +58,7 @@ from portkey_ai import Portkey portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY", # Replace with your virtual key for Huggingface + provider="@PROVIDER", # Replace with your virtual key for Huggingface huggingface_base_url="HUGGINGFACE_DEDICATED_URL" # Optional: Use this if you have a dedicated server hosted on Huggingface ) ``` @@ -191,7 +191,7 @@ You can now use this virtual key in your requests: ```js const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", - virtualKey: "YOUR_SELF_HOSTED_LLM_VIRTUAL_KEY" + provider:"@YOUR_SELF_HOSTED_LLM_PROVIDER" async function main() { const response = await client.chat.completions.create({ @@ -207,7 +207,7 @@ You can now use this virtual key in your requests: ```python portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="YOUR_SELF_HOSTED_LLM_VIRTUAL_KEY" + provider="@YOUR_SELF_HOSTED_LLM_PROVIDER" ) response = portkey.chat.completions.create( diff --git a/integrations/llms/jina-ai.mdx b/integrations/llms/jina-ai.mdx index b5d1a84d..952fd32e 100644 --- a/integrations/llms/jina-ai.mdx +++ b/integrations/llms/jina-ai.mdx @@ -44,7 +44,7 @@ To use JinaAI with Portkey, [get your API key from here](https://jina.ai/), then const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "JINA_AI_VIRTUAL_KEY" // Your Jina AI Virtual Key + provider:"@JINA_AI_PROVIDER" // Your Jina AI Virtual Key }) ``` @@ -54,7 +54,7 @@ from portkey_ai import Portkey portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="JINA_AI_VIRTUAL_KEY" # Replace with your virtual key for Jina AI + provider="@JINA_AI_PROVIDER" # Replace with your virtual key for Jina AI ) ``` diff --git a/integrations/llms/lambda.mdx b/integrations/llms/lambda.mdx index ead52503..9d614725 100644 --- a/integrations/llms/lambda.mdx +++ b/integrations/llms/lambda.mdx @@ -42,7 +42,7 @@ Portkey offers native integrations with [Lambda](https://lambdalabs.com/) for No portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Lambda + provider="@PROVIDER" # Replace with your virtual key for Lambda ) ``` @@ -51,7 +51,7 @@ Portkey offers native integrations with [Lambda](https://lambdalabs.com/) for No const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your Lambda Virtual Key + provider:"@PROVIDER" // Your Lambda Virtual Key }) ``` @@ -365,11 +365,11 @@ Here's a simplified version of how to use Portkey's Gateway Configuration: "targets": [ { "name": "llama3.1", - "virtual_key": "xx" + "provider":"@xx" }, { "name": "gpt-3.5", - "virtual_key": "yy" + "provider":"@yy" } ] } @@ -390,7 +390,7 @@ Here's a simplified version of how to use Portkey's Gateway Configuration: portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="VIRTUAL_KEY", + provider="@PROVIDER", config=portkey_config ) ``` @@ -400,7 +400,7 @@ Here's a simplified version of how to use Portkey's Gateway Configuration: const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", - virtualKey: "VIRTUAL_KEY", + provider:"@PROVIDER", config: portkeyConfig }) ``` @@ -431,7 +431,7 @@ Portkey's AI gateway enables you to enforce input/output checks on requests by a ```json { - "virtual_key":"lambda-xxx", + "provider:"@lambda-xxx", "before_request_hooks": [{ "id": "input-guardrail-id-xx" }], diff --git a/integrations/llms/lemon-fox.mdx b/integrations/llms/lemon-fox.mdx index ccda305b..1a467ffb 100644 --- a/integrations/llms/lemon-fox.mdx +++ b/integrations/llms/lemon-fox.mdx @@ -42,7 +42,7 @@ Portkey offers native integrations with [LemonFox-AI](https://www.lemonfox.ai/) portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for LemonFox AI + provider="@PROVIDER" # Replace with your virtual key for LemonFox AI ) ``` @@ -51,7 +51,7 @@ Portkey offers native integrations with [LemonFox-AI](https://www.lemonfox.ai/) const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your LemonFox AI Virtual Key + provider:"@PROVIDER" // Your LemonFox AI Virtual Key }) ``` @@ -201,7 +201,7 @@ from portkey_ai import Portkey client = Portkey( api_key = "PORTKEY_API_KEY", - virtual_key = "PROVIDER_VIRTUAL_KEY" + virtual_key = "PROVIDER" ) client.images.generate( @@ -217,7 +217,7 @@ import Portkey from 'portkey-ai'; const client = new Portkey({ apiKey: 'PORTKEY_API_KEY', - virtualKey: 'PROVIDER_VIRTUAL_KEY' + provider:'@PROVIDER' }); async function main() { @@ -233,7 +233,7 @@ main(); curl https://api.portkey.ai/v1/images/generations \ -H "Content-Type: application/json" \ -H "x-portkey-api-key: $PORTKEY_API_KEY" \ - -H "x-portkey-virtual-key: $PORTKEY_PROVIDER_VIRTUAL_KEY" \ + -H "x-portkey-provider: $PORTKEY_PROVIDER" \ -d '{ "prompt": "A cute baby sea otter", "n": 1, @@ -363,11 +363,11 @@ Here's a simplified version of how to use Portkey's Gateway Configuration: "targets": [ { "name": "llama-8b-chat", - "virtual_key": "xx" + "provider":"@xx" }, { "name": "gpt-3.5", - "virtual_key": "yy" + "provider":"@yy" } ] } @@ -388,7 +388,7 @@ Here's a simplified version of how to use Portkey's Gateway Configuration: portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="VIRTUAL_KEY", + provider="@PROVIDER", config=portkey_config ) ``` @@ -398,7 +398,7 @@ Here's a simplified version of how to use Portkey's Gateway Configuration: const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", - virtualKey: "VIRTUAL_KEY", + provider:"@PROVIDER", config: portkeyConfig }) ``` @@ -429,7 +429,7 @@ Portkey's AI gateway enables you to enforce input/output checks on requests by a ```json { - "virtual_key":"lemonfox-ai-xxx", + "provider:"@lemonfox-ai-xxx", "before_request_hooks": [{ "id": "input-guardrail-id-xx" }], diff --git a/integrations/llms/lepton.mdx b/integrations/llms/lepton.mdx index 67f69344..1871eabf 100644 --- a/integrations/llms/lepton.mdx +++ b/integrations/llms/lepton.mdx @@ -41,7 +41,7 @@ To use Lepton AI with Portkey, [get your API key from Lepton AI](https://console const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your Lepton AI Virtual Key + provider:"@PROVIDER" // Your Lepton AI Virtual Key }) ``` @@ -51,7 +51,7 @@ To use Lepton AI with Portkey, [get your API key from Lepton AI](https://console portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Lepton + provider="@PROVIDER" # Replace with your virtual key for Lepton ) ``` @@ -87,7 +87,7 @@ Use the Portkey instance to send requests to Lepton AI. You can also override th curl --location 'https://api.portkey.ai/v1/chat/completions' \ -H 'Content-Type: application/json' \ -H 'x-portkey-api-key: PORTKEY_API_KEY' \ - -H 'x-portkey-virtual-key: VIRTUAL_KEY' \ + -H 'x-portkey-provider: PROVIDER' \ --data '{ "model": "llama-3-8b-sft-v1", "messages": [ diff --git a/integrations/llms/lingyi-01.ai.mdx b/integrations/llms/lingyi-01.ai.mdx index 800dcb6c..9f1112ae 100644 --- a/integrations/llms/lingyi-01.ai.mdx +++ b/integrations/llms/lingyi-01.ai.mdx @@ -41,7 +41,7 @@ To use Lingyi with Portkey, [get your API key from here](https://platform.lingyi const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your Lingyi Virtual Key + provider:"@PROVIDER" // Your Lingyi Virtual Key }) ``` @@ -51,7 +51,7 @@ To use Lingyi with Portkey, [get your API key from here](https://platform.lingyi portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Groq + provider="@PROVIDER" # Replace with your virtual key for Groq ) ``` diff --git a/integrations/llms/local-ai.mdx b/integrations/llms/local-ai.mdx index 745c5add..8b0f1048 100644 --- a/integrations/llms/local-ai.mdx +++ b/integrations/llms/local-ai.mdx @@ -113,7 +113,7 @@ You can now use this virtual key in your requests: ```js const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", - virtualKey: "YOUR_SELF_HOSTED_LLM_VIRTUAL_KEY" + provider:"@YOUR_SELF_HOSTED_LLM_PROVIDER" async function main() { const response = await client.chat.completions.create({ @@ -129,7 +129,7 @@ You can now use this virtual key in your requests: ```python portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="YOUR_SELF_HOSTED_LLM_VIRTUAL_KEY" + provider="@YOUR_SELF_HOSTED_LLM_PROVIDER" ) response = portkey.chat.completions.create( diff --git a/integrations/llms/mistral-ai.mdx b/integrations/llms/mistral-ai.mdx index bbf445b4..cffb7473 100644 --- a/integrations/llms/mistral-ai.mdx +++ b/integrations/llms/mistral-ai.mdx @@ -43,7 +43,7 @@ To use Mistral AI with Portkey, [get your API key from here](https://console.mis const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your Mistral AI Virtual Key + provider:"@PROVIDER" // Your Mistral AI Virtual Key }) ``` @@ -53,7 +53,7 @@ To use Mistral AI with Portkey, [get your API key from here](https://console.mis portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Mistral AI + provider="@PROVIDER" # Replace with your virtual key for Mistral AI ) ``` @@ -104,7 +104,7 @@ Using Portkey, you can also call Mistral API's new Codestral endpoint. Just pass const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", - virtualKey: "MISTRAL_VIRTUAL_KEY", + provider:"@MISTRAL_PROVIDER", customHost: "https://codestral.mistral.ai/v1" }) @@ -122,7 +122,7 @@ Using Portkey, you can also call Mistral API's new Codestral endpoint. Just pass portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="MISTRAL_VIRTUAL_KEY", + provider="@MISTRAL_PROVIDER", custom_host="https://codestral.mistral.ai/v1" ) diff --git a/integrations/llms/monster-api.mdx b/integrations/llms/monster-api.mdx index cf03283d..6354eb8e 100644 --- a/integrations/llms/monster-api.mdx +++ b/integrations/llms/monster-api.mdx @@ -43,7 +43,7 @@ To use Monster API with Portkey, [get your API key from here,](https://monsterap const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your MonsterAPI Virtual Key + provider:"@PROVIDER" // Your MonsterAPI Virtual Key }) ``` diff --git a/integrations/llms/moonshot.mdx b/integrations/llms/moonshot.mdx index 6e87d00d..3dd06908 100644 --- a/integrations/llms/moonshot.mdx +++ b/integrations/llms/moonshot.mdx @@ -44,7 +44,7 @@ To use Moonshot with Portkey, [get your API key from here,](https://moonshot.cn) const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your Moonshot Virtual Key + provider:"@PROVIDER" // Your Moonshot Virtual Key }) ``` @@ -54,7 +54,7 @@ To use Moonshot with Portkey, [get your API key from here,](https://moonshot.cn) portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Groq + provider="@PROVIDER" # Replace with your virtual key for Groq ) ``` diff --git a/integrations/llms/ncompass.mdx b/integrations/llms/ncompass.mdx index b572d889..5dbc5800 100644 --- a/integrations/llms/ncompass.mdx +++ b/integrations/llms/ncompass.mdx @@ -45,7 +45,7 @@ import Portkey from 'portkey-ai' const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your nCompass Virtual Key + provider:"@PROVIDER" // Your nCompass Virtual Key }) ``` @@ -55,7 +55,7 @@ const portkey = new Portkey({ portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for nCompass + provider="@PROVIDER" # Replace with your virtual key for nCompass ) ``` diff --git a/integrations/llms/nebius.mdx b/integrations/llms/nebius.mdx index 302ca0b8..5be64d84 100644 --- a/integrations/llms/nebius.mdx +++ b/integrations/llms/nebius.mdx @@ -42,7 +42,7 @@ To use Nebius AI with Portkey, [get your API key from here](https://studio.nebiu const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your Nebius Virtual Key + provider:"@PROVIDER" // Your Nebius Virtual Key }) ``` @@ -52,7 +52,7 @@ To use Nebius AI with Portkey, [get your API key from here](https://studio.nebiu portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Nebius + provider="@PROVIDER" # Replace with your virtual key for Nebius ) ``` diff --git a/integrations/llms/nomic.mdx b/integrations/llms/nomic.mdx index 1e1463ca..0dd49cd5 100644 --- a/integrations/llms/nomic.mdx +++ b/integrations/llms/nomic.mdx @@ -14,15 +14,15 @@ Provider Slug. `nomic` Portkey provides a consistent API to interact with embedding models from various providers. To integrate Nomic with Portkey: -### 1\. Create a Virtual Key for Nomic in your Portkey account +### 1\. Integrate Nomic in your Portkey account -You can head over to the virtual keys tab and create one for Nomic. This will be then used to make API requests to Nomic without needing the protected API key. [Grab your Nomic API key from here](https://atlas.nomic.ai/data/randomesid/org/keys). - +You can head over to the Integrations tab and connect Nomic with API key. This will be then used to make API requests to Nomic without needing the protected API key. [Grab your Nomic API key from here](https://atlas.nomic.ai/data/randomesid/org/keys). + ![Logo](/images/llms/nomic.png) -### 2\. Install the Portkey SDK and Initialize with this Virtual Key +### 2\. Install the Portkey SDK and Initialize with Nomic Add the Portkey SDK to your application to interact with Nomic's API through Portkey's gateway. @@ -32,7 +32,7 @@ Add the Portkey SDK to your application to interact with Nomic's API through Por const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your Nomic Virtual Key + provider:"@nomic" // Your Nomic provider slug from Portkey }) ``` @@ -42,7 +42,7 @@ Add the Portkey SDK to your application to interact with Nomic's API through Por portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Nomic + provider="@nomic" # Your Nomic provider slug from Portkey ) ``` diff --git a/integrations/llms/novita-ai.mdx b/integrations/llms/novita-ai.mdx index d1087887..53c960c4 100644 --- a/integrations/llms/novita-ai.mdx +++ b/integrations/llms/novita-ai.mdx @@ -43,7 +43,7 @@ To use Novita AI with Portkey, [get your API key from here](https://novita.ai/se const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your Novita Virtual Key + provider:"@PROVIDER" // Your Novita Virtual Key }) ``` @@ -53,7 +53,7 @@ To use Novita AI with Portkey, [get your API key from here](https://novita.ai/se portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Groq + provider="@PROVIDER" # Replace with your virtual key for Groq ) ``` diff --git a/integrations/llms/nscale.mdx b/integrations/llms/nscale.mdx index 99b6608f..a4dcfb40 100644 --- a/integrations/llms/nscale.mdx +++ b/integrations/llms/nscale.mdx @@ -35,7 +35,7 @@ To use Nscale with Virtual Key, [get your API key from here](https://console.nsc const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your Nscale Virtual Key + provider:"@PROVIDER" // Your Nscale Virtual Key }) ``` @@ -45,7 +45,7 @@ To use Nscale with Virtual Key, [get your API key from here](https://console.nsc portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="NSCALE_VIRTUAL_KEY" + provider="@NSCALE_PROVIDER" ) ``` diff --git a/integrations/llms/ollama.mdx b/integrations/llms/ollama.mdx index 5b5a16fe..354dfb1b 100644 --- a/integrations/llms/ollama.mdx +++ b/integrations/llms/ollama.mdx @@ -173,7 +173,7 @@ You can now use this virtual key in your requests: ```js const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", - virtualKey: "YOUR_SELF_HOSTED_LLM_VIRTUAL_KEY" + provider:"@YOUR_SELF_HOSTED_LLM_PROVIDER" async function main() { const response = await client.chat.completions.create({ @@ -189,7 +189,7 @@ You can now use this virtual key in your requests: ```python portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="YOUR_SELF_HOSTED_LLM_VIRTUAL_KEY" + provider="@YOUR_SELF_HOSTED_LLM_PROVIDER" ) response = portkey.chat.completions.create( @@ -243,7 +243,7 @@ import Portkey from 'portkey-ai'; const client = new Portkey({ baseUrl: 'http://localhost:8080/v1', apiKey: 'PORTKEY_API_KEY', - virtualKey: 'PROVIDER_VIRTUAL_KEY', + provider:'@PROVIDER', customHost: "http://host.docker.internal:11434" // Your Ollama Docker URL }); @@ -264,7 +264,7 @@ from portkey_ai import Portkey client = Portkey( base_url = 'http://localhost:8080/v1', api_key = "PORTKEY_API_KEY", - virtual_key = "PROVIDER_VIRTUAL_KEY", + virtual_key = "PROVIDER", custom_host="http://localhost:11434" # Your Ollama URL ) @@ -283,7 +283,7 @@ curl http://localhost:8080/v1/chat/completions \ -H "Content-Type: application/json" \ -H "x-portkey-api-key: $PORTKEY_API_KEY" \ -H "x-portkey-custom-host: http://localhost:11434" \ - -H "x-portkey-virtual-key: $PORTKEY_PROVIDER_VIRTUAL_KEY" \ + -H "x-portkey-provider: $PORTKEY_PROVIDER" \ -d '{ "model": "gpt-4o", "messages": [ @@ -300,7 +300,7 @@ client = OpenAI( base_url="https://localhost:8080/v1", default_headers=createHeaders( api_key="PORTKEY_API_KEY", - virtual_key="OPENAI_VIRTUAL_KEY", + provider="@OPENAI_PROVIDER", custom_host="http://localhost:11434" ) ) @@ -324,7 +324,7 @@ const openai = new OpenAI({ baseURL: 'https://localhost:8080/v1', defaultHeaders: createHeaders({ apiKey: "PORTKEY_API_KEY", - virtualKey: "OPENAI_VIRTUAL_KEY", + provider:"@OPENAI_PROVIDER", customHost: "http://localhost:11434" }) }); diff --git a/integrations/llms/openai.mdx b/integrations/llms/openai.mdx index ff7bd8f8..ca5ad196 100644 --- a/integrations/llms/openai.mdx +++ b/integrations/llms/openai.mdx @@ -29,7 +29,7 @@ import Portkey from 'portkey-ai'; const client = new Portkey({ apiKey: 'PORTKEY_API_KEY', - virtualKey: 'PROVIDER_VIRTUAL_KEY' + provider:'@PROVIDER' }); async function main() { @@ -57,7 +57,7 @@ from portkey_ai import Portkey client = Portkey( api_key = "PORTKEY_API_KEY", - virtual_key = "PROVIDER_VIRTUAL_KEY" + virtual_key = "PROVIDER" ) response = client.chat.completions.create( @@ -79,7 +79,7 @@ print(response.choices[0].message) curl https://api.portkey.ai/v1/chat/completions \ -H "Content-Type: application/json" \ -H "x-portkey-api-key: $PORTKEY_API_KEY" \ - -H "x-portkey-virtual-key: $PORTKEY_PROVIDER_VIRTUAL_KEY" \ + -H "x-portkey-provider: $PORTKEY_PROVIDER" \ -d '{ "model": "gpt-4o", "messages": [ @@ -105,7 +105,7 @@ client = OpenAI( base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( api_key="PORTKEY_API_KEY", - virtual_key="OPENAI_VIRTUAL_KEY" + provider="@OPENAI_PROVIDER" ) ) @@ -136,7 +136,7 @@ const openai = new OpenAI({ baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ apiKey: "PORTKEY_API_KEY", - virtualKey: "OPENAI_VIRTUAL_KEY" + provider:"@OPENAI_PROVIDER" }) }); @@ -175,7 +175,7 @@ OpenAI has released a new Responses API that combines the best of both Chat Comp portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="OPENAI_VIRTUAL_KEY" + provider="@OPENAI_PROVIDER" ) response = portkey.responses.create( @@ -192,7 +192,7 @@ OpenAI has released a new Responses API that combines the best of both Chat Comp const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", - virtualKey: "OPENAI_VIRTUAL_KEY" + provider:"@OPENAI_PROVIDER" }); async function main() { @@ -218,7 +218,7 @@ OpenAI has released a new Responses API that combines the best of both Chat Comp default_headers=createHeaders( provider="openai", api_key="PORTKEY_API_KEY", - virtual_key="OPENAI_VIRTUAL_KEY" + provider="@OPENAI_PROVIDER" ) ) @@ -240,7 +240,7 @@ OpenAI has released a new Responses API that combines the best of both Chat Comp defaultHeaders: createHeaders({ provider: "openai", apiKey: "PORTKEY_API_KEY", - virtualKey: "OPENAI_VIRTUAL_KEY" + provider:"@OPENAI_PROVIDER" }) }); diff --git a/integrations/llms/openai/batches.mdx b/integrations/llms/openai/batches.mdx index cdef8db4..d5900272 100644 --- a/integrations/llms/openai/batches.mdx +++ b/integrations/llms/openai/batches.mdx @@ -19,7 +19,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key + provider="@PROVIDER" ) start_batch_response = portkey.batches.create( @@ -39,7 +39,7 @@ import { Portkey } from 'portkey-ai'; // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key + provider:"@PROVIDER" }); const startBatch = async () => { @@ -61,7 +61,7 @@ await startBatch(); ```sh curl --location 'https://api.portkey.ai/v1/batches' \ --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-provider: @provider' \ --header 'Content-Type: application/json' \ --data '{ "input_file_id": "", @@ -135,7 +135,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key + provider="@PROVIDER" ) batches = portkey.batches.list() @@ -150,7 +150,7 @@ import { Portkey } from 'portkey-ai'; // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key + provider:"@PROVIDER" }); const listBatches = async () => { @@ -167,7 +167,7 @@ await listBatches(); ```sh curl --location 'https://api.portkey.ai/v1/batches' \ --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' +--header 'x-portkey-provider: @provider' ``` @@ -225,7 +225,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key + provider="@PROVIDER" ) batch = portkey.batches.retrieve(batch_id="batch_id") @@ -240,7 +240,7 @@ import { Portkey } from 'portkey-ai'; // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key + provider:"@PROVIDER" }); const getBatch = async () => { @@ -257,7 +257,7 @@ await getBatch(); ```sh curl --location 'https://api.portkey.ai/v1/batches/' \ --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' +--header 'x-portkey-provider: @provider' ``` @@ -312,7 +312,7 @@ print(batch) ```sh curl --location 'https://api.portkey.ai/v1/batches//output' \ --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' +--header 'x-portkey-provider: @provider' ``` @@ -327,7 +327,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key + provider="@PROVIDER" ) batches = portkey.batches.list() @@ -342,7 +342,7 @@ import { Portkey } from 'portkey-ai'; // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key + provider:"@PROVIDER" }); const listBatchingJobs = async () => { @@ -359,7 +359,7 @@ await listBatchingJobs(); ```sh curl --location 'https://api.portkey.ai/v1/batches' \ --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' +--header 'x-portkey-provider: @provider' ``` @@ -416,7 +416,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key + provider="@PROVIDER" ) cancel_batch_response = portkey.batches.cancel(batch_id="batch_id") @@ -431,7 +431,7 @@ import { Portkey } from 'portkey-ai'; // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key + provider:"@PROVIDER" }); const cancelBatch = async () => { @@ -448,7 +448,7 @@ await cancelBatch(); ```sh curl --request POST --location 'https://api.portkey.ai/v1/batches//cancel' \ --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' +--header 'x-portkey-provider: @provider' ``` diff --git a/integrations/llms/openai/files.mdx b/integrations/llms/openai/files.mdx index 874a1f8b..3eb102a5 100644 --- a/integrations/llms/openai/files.mdx +++ b/integrations/llms/openai/files.mdx @@ -12,7 +12,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key + provider="@PROVIDER" ) upload_file_response = portkey.files.create( @@ -30,7 +30,7 @@ import { Portkey } from 'portkey-ai'; // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key + provider:"@PROVIDER" }); const uploadFile = async () => { @@ -50,7 +50,7 @@ await uploadFile(); ```sh curl --location --request POST 'https://api.portkey.ai/v1/files' \ --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-provider: @provider' \ --form 'purpose=""' \ --form 'file=@""' ``` @@ -115,7 +115,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key + provider="@PROVIDER" ) files = portkey.files.list() @@ -130,7 +130,7 @@ import { Portkey } from 'portkey-ai'; // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key + provider:"@PROVIDER" }); const listFiles = async () => { @@ -147,7 +147,7 @@ await listFiles(); ```sh curl --location 'https://api.portkey.ai/v1/files' \ --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' +--header 'x-portkey-provider: @provider' ``` @@ -205,7 +205,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key + provider="@PROVIDER" ) file = portkey.files.retrieve(file_id="file_id") @@ -220,7 +220,7 @@ import { Portkey } from 'portkey-ai'; // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key + provider:"@PROVIDER" }); const getFile = async () => { @@ -237,7 +237,7 @@ await getFile(); ```sh curl --location 'https://api.portkey.ai/v1/files/' \ --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' +--header 'x-portkey-provider: @provider' ``` @@ -295,7 +295,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key + provider="@PROVIDER" ) file_content = portkey.files.content(file_id="file_id") @@ -310,7 +310,7 @@ import { Portkey } from 'portkey-ai'; // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key + provider:"@PROVIDER" }); const getFileContent = async () => { @@ -327,7 +327,7 @@ await getFileContent(); ```sh curl --location 'https://api.portkey.ai/v1/files//content' \ --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' +--header 'x-portkey-provider: @provider' ``` @@ -385,7 +385,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key + provider="@PROVIDER" ) delete_file_response = portkey.files.delete(file_id="file_id") @@ -400,7 +400,7 @@ import { Portkey } from 'portkey-ai'; // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key + provider:"@PROVIDER" }); const deleteFile = async () => { @@ -417,7 +417,7 @@ await deleteFile(); ```sh curl --location --request DELETE 'https://api.portkey.ai/v1/files/' \ --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' +--header 'x-portkey-provider: @provider' ``` diff --git a/integrations/llms/openai/fine-tuning.mdx b/integrations/llms/openai/fine-tuning.mdx index 6347142c..c3c60421 100644 --- a/integrations/llms/openai/fine-tuning.mdx +++ b/integrations/llms/openai/fine-tuning.mdx @@ -13,7 +13,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key + provider="@PROVIDER" ) # Upload a file for fine-tuning @@ -33,7 +33,7 @@ import * as fs from 'fs'; // Initialize the Portkey client const portkey = Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key + provider:"@PROVIDER" }); (async () => { @@ -56,7 +56,7 @@ openai = OpenAI( api_key='OPENAI_API_KEY', base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - virtual_key="VIRTUAL_KEY", + provider="@PROVIDER", api_key="PORTKEY_API_KEY" ) ) @@ -80,7 +80,7 @@ const openai = new OpenAI({ apiKey: 'OPENAI_API_KEY', baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ - virtualKey: "VIRTUAL_KEY", + provider:"@PROVIDER", apiKey: "PORTKEY_API_KEY" }) }); @@ -99,7 +99,7 @@ const openai = new OpenAI({ ```sh curl -X POST --header 'x-portkey-api-key: ' \ - --header 'x-portkey-virtual-key: ' \ + --header 'x-portkey-provider: @provider' \ --form 'file=@dataset.jsonl' \ --form 'purpose=fine-tune' \ 'https://api.portkey.ai/v1/files' @@ -117,7 +117,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key + provider="@PROVIDER" ) # Create a fine-tuning job @@ -141,7 +141,7 @@ import { Portkey } from "portkey-ai"; // Initialize the Portkey client const portkey = Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key + provider:"@PROVIDER" }); (async () => { @@ -169,7 +169,7 @@ openai = OpenAI( api_key='OPENAI_API_KEY', base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - virtual_key="VIRTUAL_KEY", + provider="@PROVIDER", api_key="PORTKEY_API_KEY" ) ) @@ -197,7 +197,7 @@ const openai = new OpenAI({ apiKey: 'OPENAI_API_KEY', baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ - virtualKey: "VIRTUAL_KEY", + provider:"@PROVIDER", apiKey: "PORTKEY_API_KEY" }) }); @@ -222,7 +222,7 @@ const openai = new OpenAI({ ```sh curl -X POST --header 'Content-Type: application/json' \ --header 'x-portkey-api-key: ' \ - --header 'x-portkey-virtual-key: ' \ + --header 'x-portkey-provider: @provider' \ --data \ $'{"model": "", "suffix": "", "training_file": "", "validation_file": "", "method": {"type": "supervised", "supervised": {"hyperparameters": {"n_epochs": 1}}}}\n' \ 'https://api.portkey.ai/v1/fine_tuning/jobs' @@ -240,7 +240,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key + provider="@PROVIDER" ) # List all fine-tuning jobs @@ -258,7 +258,7 @@ import { Portkey } from "portkey-ai"; // Initialize the Portkey client const portkey = Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key + provider:"@PROVIDER" }); (async () => { @@ -280,7 +280,7 @@ openai = OpenAI( api_key='OPENAI_API_KEY', base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - virtual_key="VIRTUAL_KEY", + provider="@PROVIDER", api_key="PORTKEY_API_KEY" ) ) @@ -302,7 +302,7 @@ const openai = new OpenAI({ apiKey: 'OPENAI_API_KEY', baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ - virtualKey: "VIRTUAL_KEY", + provider:"@PROVIDER", apiKey: "PORTKEY_API_KEY" }) }); @@ -320,7 +320,7 @@ const openai = new OpenAI({ ```sh curl -X GET --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-provider: @provider' \ 'https://api.portkey.ai/v1/fine_tuning/jobs' ``` @@ -336,7 +336,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key + provider="@PROVIDER" ) # Retrieve a specific fine-tuning job @@ -354,7 +354,7 @@ import { Portkey } from "portkey-ai"; // Initialize the Portkey client const portkey = Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key + provider:"@PROVIDER" }); (async () => { @@ -376,7 +376,7 @@ openai = OpenAI( api_key='OPENAI_API_KEY', base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - virtual_key="VIRTUAL_KEY", + provider="@PROVIDER", api_key="PORTKEY_API_KEY" ) ) @@ -398,7 +398,7 @@ const openai = new OpenAI({ apiKey: 'OPENAI_API_KEY', baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ - virtualKey: "VIRTUAL_KEY", + provider:"@PROVIDER", apiKey: "PORTKEY_API_KEY" }) }); @@ -416,7 +416,7 @@ const openai = new OpenAI({ ```sh curl -X GET --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-provider: @provider' \ 'https://api.portkey.ai/v1/fine_tuning/jobs/' ``` @@ -432,7 +432,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key + provider="@PROVIDER" ) # Cancel a fine-tuning job @@ -450,7 +450,7 @@ import { Portkey } from "portkey-ai"; // Initialize the Portkey client const portkey = Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key + provider:"@PROVIDER" }); (async () => { @@ -472,7 +472,7 @@ openai = OpenAI( api_key='OPENAI_API_KEY', base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - virtual_key="VIRTUAL_KEY", + provider="@PROVIDER", api_key="PORTKEY_API_KEY" ) ) @@ -494,7 +494,7 @@ const openai = new OpenAI({ apiKey: 'OPENAI_API_KEY', baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ - virtualKey: "VIRTUAL_KEY", + provider:"@PROVIDER", apiKey: "PORTKEY_API_KEY" }) }); @@ -512,7 +512,7 @@ const openai = new OpenAI({ ```sh curl -X POST --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-provider: @provider' \ 'https://api.portkey.ai/v1/fine_tuning/jobs//cancel' ``` diff --git a/integrations/llms/openai/prompt-caching-openai.mdx b/integrations/llms/openai/prompt-caching-openai.mdx index c6b8fad9..73277479 100644 --- a/integrations/llms/openai/prompt-caching-openai.mdx +++ b/integrations/llms/openai/prompt-caching-openai.mdx @@ -20,7 +20,7 @@ Portkey supports OpenAI's prompt caching feature out of the box. Here is an exam portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="OPENAI_VIRTUAL_KEY", + provider="@OPENAI_PROVIDER", ) # Define tools (for function calling example) @@ -65,7 +65,7 @@ Portkey supports OpenAI's prompt caching feature out of the box. Here is an exam const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your OpenAI Virtual Key + provider:"@PROVIDER" // Your OpenAI Virtual Key }) // Define tools (for function calling example) diff --git a/integrations/llms/openai/structured-outputs.mdx b/integrations/llms/openai/structured-outputs.mdx index ed2c65e4..796d9cb7 100644 --- a/integrations/llms/openai/structured-outputs.mdx +++ b/integrations/llms/openai/structured-outputs.mdx @@ -27,7 +27,7 @@ Portkey SDKs for [Python](https://github.com/openai/openai-python/blob/main/help portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="OPENAI_VIRTUAL_KEY" + provider="@OPENAI_PROVIDER" ) completion = portkey.beta.chat.completions.parse( @@ -56,7 +56,7 @@ Portkey SDKs for [Python](https://github.com/openai/openai-python/blob/main/help const portkey = new Portkey({ apiKey: "YOUR_API_KEY", - virtualKey: "YOUR_VIRTUAL_KEY" + provider:"@YOUR_PROVIDER" }); async function runMathTutor() { @@ -90,7 +90,7 @@ The second approach, shown in the subsequent examples, uses a JSON schema direct const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", - virtualKey: "OPENAI_VIRTUAL_KEY", + provider:"@OPENAI_PROVIDER", }); async function main() { @@ -144,7 +144,7 @@ The second approach, shown in the subsequent examples, uses a JSON schema direct portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="OPENAI_VIRTUAL_KEY" + provider="@OPENAI_PROVIDER" ) completion = portkey.chat.completions.create( @@ -189,7 +189,7 @@ The second approach, shown in the subsequent examples, uses a JSON schema direct ```sh curl https://api.portkey.ai/v1/chat/completions \ -H "x-portkey-api-key: $PORTKEY_API_KEY" \ - -H "x-portkey-virtual-key: $OPENAI_VIRTUAL_KEY" \ + -H "x-portkey-provider: $OPENAI_PROVIDER" \ -H "Content-Type: application/json" \ -d '{ "model": "gpt-4o-2024-08-06", diff --git a/integrations/llms/openai2.mdx b/integrations/llms/openai2.mdx index 145f340a..733bf9b5 100644 --- a/integrations/llms/openai2.mdx +++ b/integrations/llms/openai2.mdx @@ -36,7 +36,7 @@ Portkey integrates with [OpenAI](https://platform.openai.com/docs/api-reference/ portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for OpenAI + provider="@PROVIDER" # Replace with your virtual key for OpenAI ) ``` @@ -45,7 +45,7 @@ Portkey integrates with [OpenAI](https://platform.openai.com/docs/api-reference/ const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your OpenAI Virtual Key + provider:"@PROVIDER" // Your OpenAI Virtual Key }) ``` @@ -596,11 +596,11 @@ Here's a simplified version of how to use Portkey's Gateway Configuration: "targets": [ { "name": "gpt4o", - "virtual_key": "xx" + "provider":"@xx" }, { "name": "gpt-3.5", - "virtual_key": "yy" + "provider":"@yy" } ] } @@ -621,7 +621,7 @@ Here's a simplified version of how to use Portkey's Gateway Configuration: portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="VIRTUAL_KEY", + provider="@PROVIDER", config=portkey_config ) ``` @@ -631,7 +631,7 @@ Here's a simplified version of how to use Portkey's Gateway Configuration: const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", - virtualKey: "VIRTUAL_KEY", + provider:"@PROVIDER", config: portkeyConfig }) ``` @@ -662,7 +662,7 @@ Portkey's AI gateway enables you to enforce input/output checks on requests by a ```json { - "virtual_key":"openai-xxx", + "provider:"@openai-xxx", "before_request_hooks": [{ "id": "input-guardrail-id-xx" }], diff --git a/integrations/llms/openai3.mdx b/integrations/llms/openai3.mdx index cdb894b3..d2a82b93 100644 --- a/integrations/llms/openai3.mdx +++ b/integrations/llms/openai3.mdx @@ -45,7 +45,7 @@ import Portkey from 'portkey-ai'; const client = new Portkey({ apiKey: 'PORTKEY_API_KEY', - virtualKey: 'PROVIDER_VIRTUAL_KEY' + provider:'@PROVIDER' }); async function main() { @@ -76,7 +76,7 @@ from portkey_ai import Portkey client = Portkey( api_key = "PORTKEY_API_KEY", - virtual_key = "PROVIDER_VIRTUAL_KEY" + virtual_key = "PROVIDER" ) response = client.chat.completions.create( @@ -101,7 +101,7 @@ print(response.choices[0].message) curl https://api.portkey.ai/v1/chat/completions \ -H "Content-Type: application/json" \ -H "x-portkey-api-key: $PORTKEY_API_KEY" \ - -H "x-portkey-virtual-key: $PORTKEY_PROVIDER_VIRTUAL_KEY" \ + -H "x-portkey-provider: $PORTKEY_PROVIDER" \ -d '{ "model": "gpt-4o", "messages": [ @@ -130,7 +130,7 @@ client = OpenAI( base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( api_key="PORTKEY_API_KEY", - virtual_key="OPENAI_VIRTUAL_KEY" + provider="@OPENAI_PROVIDER" ) ) @@ -165,7 +165,7 @@ const openai = new OpenAI({ baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ apiKey: "PORTKEY_API_KEY", - virtualKey: "OPENAI_VIRTUAL_KEY" + provider:"@OPENAI_PROVIDER" }) }); @@ -228,7 +228,7 @@ import Portkey from 'portkey-ai'; const client = new Portkey({ baseUrl: 'http://localhost:8080/v1', apiKey: 'PORTKEY_API_KEY', - virtualKey: 'PROVIDER_VIRTUAL_KEY' + provider:'@PROVIDER' }); async function main() { @@ -248,7 +248,7 @@ from portkey_ai import Portkey client = Portkey( base_url = 'http://localhost:8080/v1', api_key = "PORTKEY_API_KEY", - virtual_key = "PROVIDER_VIRTUAL_KEY" + virtual_key = "PROVIDER" ) response = client.chat.completions.create( @@ -265,7 +265,7 @@ print(response.choices[0].message) curl http://localhost:8080/v1/chat/completions \ -H "Content-Type: application/json" \ -H "x-portkey-api-key: $PORTKEY_API_KEY" \ - -H "x-portkey-virtual-key: $PORTKEY_PROVIDER_VIRTUAL_KEY" \ + -H "x-portkey-provider: $PORTKEY_PROVIDER" \ -d '{ "model": "gpt-4o", "messages": [ @@ -282,7 +282,7 @@ client = OpenAI( base_url="https://localhost:8080/v1", default_headers=createHeaders( api_key="PORTKEY_API_KEY", - virtual_key="OPENAI_VIRTUAL_KEY" + provider="@OPENAI_PROVIDER" ) ) @@ -305,7 +305,7 @@ const openai = new OpenAI({ baseURL: 'https://localhost:8080/v1', defaultHeaders: createHeaders({ apiKey: "PORTKEY_API_KEY", - virtualKey: "OPENAI_VIRTUAL_KEY" + provider:"@OPENAI_PROVIDER" }) }); @@ -636,7 +636,7 @@ The API offers three core capabilities: const client = new Portkey({ apiKey: 'PORTKEY_API_KEY', - virtualKey: 'PROVIDER_VIRTUAL_KEY' + provider:'@PROVIDER' }); async function main() { @@ -654,7 +654,7 @@ The API offers three core capabilities: client = Portkey( api_key = "PORTKEY_API_KEY", - virtual_key = "PROVIDER_VIRTUAL_KEY" + virtual_key = "PROVIDER" ) client.images.generate( @@ -668,7 +668,7 @@ The API offers three core capabilities: curl https://api.portkey.ai/v1/images/generations \ -H "Content-Type: application/json" \ -H "x-portkey-api-key: $PORTKEY_API_KEY" \ - -H "x-portkey-virtual-key: $PORTKEY_PROVIDER_VIRTUAL_KEY" \ + -H "x-portkey-provider: $PORTKEY_PROVIDER" \ -d '{ "model": "dall-e-3", "prompt": "A cute baby sea otter", @@ -938,11 +938,11 @@ Here's a simplified version of how to use Portkey's Gateway Configuration: "targets": [ { "name": "gpt4o", - "virtual_key": "xx" + "provider":"@xx" }, { "name": "gpt-3.5", - "virtual_key": "yy" + "provider":"@yy" } ] } @@ -963,7 +963,7 @@ Here's a simplified version of how to use Portkey's Gateway Configuration: portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="VIRTUAL_KEY", + provider="@PROVIDER", config=portkey_config ) ``` @@ -973,7 +973,7 @@ Here's a simplified version of how to use Portkey's Gateway Configuration: const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", - virtualKey: "VIRTUAL_KEY", + provider:"@PROVIDER", config: portkeyConfig }) ``` @@ -1006,7 +1006,7 @@ Portkey's AI gateway enables you to enforce input/output checks on requests by a ```json { - "virtual_key":"openai-xxx", + "provider:"@openai-xxx", "before_request_hooks": [{ "id": "input-guardrail-id-xx" }], diff --git a/integrations/llms/openrouter.mdx b/integrations/llms/openrouter.mdx index 77631dc8..fe73d14e 100644 --- a/integrations/llms/openrouter.mdx +++ b/integrations/llms/openrouter.mdx @@ -42,7 +42,7 @@ To use OpenRouter with Portkey, [get your API key from here](https://openrouter. const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your OpenRouter Virtual Key + provider:"@PROVIDER" // Your OpenRouter Virtual Key }) ``` @@ -52,7 +52,7 @@ To use OpenRouter with Portkey, [get your API key from here](https://openrouter. portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Open Router + provider="@PROVIDER" # Replace with your virtual key for Open Router ) ``` diff --git a/integrations/llms/perplexity-ai.mdx b/integrations/llms/perplexity-ai.mdx index 3085c0c3..177dfac7 100644 --- a/integrations/llms/perplexity-ai.mdx +++ b/integrations/llms/perplexity-ai.mdx @@ -41,7 +41,7 @@ To use Perplexity AI with Portkey, [get your API key from here,](https://www.per const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your Perplexity AI Virtual Key + provider:"@PROVIDER" // Your Perplexity AI Virtual Key }) ``` @@ -51,7 +51,7 @@ To use Perplexity AI with Portkey, [get your API key from here,](https://www.per portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Perplexity AI + provider="@PROVIDER" # Replace with your virtual key for Perplexity AI ) ``` diff --git a/integrations/llms/predibase.mdx b/integrations/llms/predibase.mdx index 1ca586d1..f2cfa7fe 100644 --- a/integrations/llms/predibase.mdx +++ b/integrations/llms/predibase.mdx @@ -40,7 +40,7 @@ To use Predibase with Portkey, [get your API key from here](https://app.predibas const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your Predibase Virtual Key + provider:"@PROVIDER" // Your Predibase Virtual Key }) ``` @@ -50,7 +50,7 @@ from portkey_ai import Portkey portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Predibase + provider="@PROVIDER" # Replace with your virtual key for Predibase ) ``` @@ -64,7 +64,7 @@ const portkey = new OpenAI({ baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ apiKey: "PORTKEY_API_KEY", - virtualKey: "PREDIBASE_VIRTUAL_KEY", + provider:"@PREDIBASE_PROVIDER", }), }); ``` @@ -78,7 +78,7 @@ portkey = OpenAI( base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( api_key="PORTKEY_API_KEY", - virtual_key="PREDIBASE_VIRTUAL_KEY" + provider="@PREDIBASE_PROVIDER" ) ) ``` @@ -125,7 +125,7 @@ print(completion) curl https://api.portkey.ai/v1/chat/completions \ -H "Content-Type: application/json" \ -H "x-portkey-api-key: $PORTKEY_API_KEY" \ - -H "x-portkey-virtual-key: $PREDIBASE_VIRTUAL_KEY" \ + -H "x-portkey-provider: $PREDIBASE_PROVIDER" \ -d '{ "messages": [{"role": "user","content": "Hello!"}], "model": "llama-3-8b", @@ -177,7 +177,7 @@ print(completion) curl https://api.portkey.ai/v1/chat/completions \ -H "Content-Type: application/json" \ -H "x-portkey-api-key: $PORTKEY_API_KEY" \ - -H "x-portkey-virtual-key: $PREDIBASE_VIRTUAL_KEY" \ + -H "x-portkey-provider: $PREDIBASE_PROVIDER" \ -d '{ "messages": [{"role": "user","content": "Hello!"}], "model": "llama-3-8b:sentiment-analysis/1", @@ -254,7 +254,7 @@ print(completion) curl https://api.portkey.ai/v1/chat/completions \ -H "Content-Type: application/json" \ -H "x-portkey-api-key: $PORTKEY_API_KEY" \ - -H "x-portkey-virtual-key: $PREDIBASE_VIRTUAL_KEY" \ + -H "x-portkey-provider: $PREDIBASE_PROVIDER" \ -d '{ "messages": [{"role": "user","content": "Hello!"}], "model": "llama-3-8b", diff --git a/integrations/llms/reka-ai.mdx b/integrations/llms/reka-ai.mdx index 322440e0..1c315f59 100644 --- a/integrations/llms/reka-ai.mdx +++ b/integrations/llms/reka-ai.mdx @@ -42,7 +42,7 @@ To use Reka AI with Portkey, [get your API key from here,](https://platform.reka const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your Reka AI Virtual Key + provider:"@PROVIDER" // Your Reka AI Virtual Key }) ``` @@ -52,7 +52,7 @@ To use Reka AI with Portkey, [get your API key from here,](https://platform.reka portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Groq + provider="@PROVIDER" # Replace with your virtual key for Groq ) ``` diff --git a/integrations/llms/replicate.mdx b/integrations/llms/replicate.mdx index 20300f7f..8144b2f9 100644 --- a/integrations/llms/replicate.mdx +++ b/integrations/llms/replicate.mdx @@ -5,7 +5,7 @@ title: "Replicate" [Replicate](https://replicate.com/) is a platform for building and running machine learning models. Replicate does not have a standarized JSON body format for their inference API, hence it is not possible to use unified API to interact with Replicate. -Portkey instead provides a proxy to Replicate, allowing you to use virtual keys and observability features. +Portkey instead provides a proxy to Replicate, allowing you to manage auth on Portkey and log all Replicate requests. ## Portkey SDK Integration with Replicate @@ -28,7 +28,7 @@ Add the Portkey SDK to your application to interact with Replicate through Portk -### 2\. Initialize Portkey with a Virtual Key +### 2\. Create a New Integration To use Replicate with Portkey, get your Replicate API key from [here](https://replicate.com/account/api-tokens), then add it to Portkey to create your [Replicate virtual key](/product/ai-gateway/virtual-keys#using-virtual-keys). @@ -40,7 +40,7 @@ import Portkey from 'portkey-ai' const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your Replicate Virtual Key + provider:"@PROVIDER" // Your Replicate Virtual Key }) ``` @@ -52,7 +52,7 @@ from portkey_ai import Portkey portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Replicate + provider="@PROVIDER" # Replace with your virtual key for Replicate ) ``` @@ -102,7 +102,7 @@ from portkey_ai import Portkey portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="REPLICATE_VIRTUAL_KEY", + provider="@REPLICATE_PROVIDER", ) response = portkey.post( @@ -119,7 +119,7 @@ print(response) // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "REPLICATE_VIRTUAL_KEY", // Add your Replicate's virtual key + provider:"@REPLICATE_PROVIDER", // Add your Replicate's virtual key }); response = portkey.post( @@ -138,7 +138,7 @@ print(response) apiKey: 'REPLICATE_API_KEY', // defaults to process.env["OPENAI_API_KEY"], baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ - virtualKey: "REPLICATE_VIRTUAL_KEY", + provider:"@REPLICATE_PROVIDER", apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] }) }); @@ -174,7 +174,7 @@ print(response) ```sh curl --location --request POST 'https://api.portkey.ai/v1/predictions' \ ---header 'x-portkey-virtual-key: REPLICATE_VIRTUAL_KEY' \ +--header 'x-portkey-provider: REPLICATE_PROVIDER' \ --header 'x-portkey-api-key: PORTKEY_API_KEY' ``` diff --git a/integrations/llms/sambanova.mdx b/integrations/llms/sambanova.mdx index 2a3ed47c..7d0dd908 100644 --- a/integrations/llms/sambanova.mdx +++ b/integrations/llms/sambanova.mdx @@ -38,7 +38,7 @@ Add the Portkey SDK to your application to interact with SambaNova's API through const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your SambaNova Virtual Key + provider:"@PROVIDER" // Your SambaNova Virtual Key }) ``` @@ -47,7 +47,7 @@ Add the Portkey SDK to your application to interact with SambaNova's API through from portkey_ai import Portkey portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for SambaNova AI + provider="@PROVIDER" # Replace with your virtual key for SambaNova AI ) ``` diff --git a/integrations/llms/segmind.mdx b/integrations/llms/segmind.mdx index 0f3570e2..56fbecb6 100644 --- a/integrations/llms/segmind.mdx +++ b/integrations/llms/segmind.mdx @@ -42,7 +42,7 @@ To use Segmind with Portkey, [get your API key from here](https://cloud.segmind. const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your Segmind Virtual Key + provider:"@PROVIDER" // Your Segmind Virtual Key }) ``` @@ -52,7 +52,7 @@ from portkey_ai import Portkey portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Segmind + provider="@PROVIDER" # Replace with your virtual key for Segmind ) ``` diff --git a/integrations/llms/siliconflow.mdx b/integrations/llms/siliconflow.mdx index cc12e228..ea265769 100644 --- a/integrations/llms/siliconflow.mdx +++ b/integrations/llms/siliconflow.mdx @@ -42,7 +42,7 @@ To use SiliconFlow with Portkey, [get your API key from here](https://siliconflo const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your Silicon Flow + provider:"@PROVIDER" // Your Silicon Flow }) ``` @@ -52,7 +52,7 @@ To use SiliconFlow with Portkey, [get your API key from here](https://siliconflo portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for SiliconFlow + provider="@PROVIDER" # Replace with your virtual key for SiliconFlow ) ``` diff --git a/integrations/llms/snowflake-cortex.mdx b/integrations/llms/snowflake-cortex.mdx index c99813b5..4a0e6955 100644 --- a/integrations/llms/snowflake-cortex.mdx +++ b/integrations/llms/snowflake-cortex.mdx @@ -45,7 +45,7 @@ import Portkey from 'portkey-ai' const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your Snowflake Cortex Virtual Key + provider:"@PROVIDER" // Your Snowflake Cortex Virtual Key }) ``` @@ -55,7 +55,7 @@ const portkey = new Portkey({ portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Snowflake Cortex + provider="@PROVIDER" # Replace with your virtual key for Snowflake Cortex ) ``` diff --git a/integrations/llms/stability-ai.mdx b/integrations/llms/stability-ai.mdx index 4e46130a..cc64a8b3 100644 --- a/integrations/llms/stability-ai.mdx +++ b/integrations/llms/stability-ai.mdx @@ -39,7 +39,7 @@ To use Stability AI with Portkey, [get your API key from here](https://platform. const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your Stability AI Virtual Key + provider:"@PROVIDER" // Your Stability AI Virtual Key }) ``` @@ -49,7 +49,7 @@ from portkey_ai import Portkey portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Stability AI + provider="@PROVIDER" # Replace with your virtual key for Stability AI ) ``` diff --git a/integrations/llms/together-ai.mdx b/integrations/llms/together-ai.mdx index e4fd4aa2..847d9127 100644 --- a/integrations/llms/together-ai.mdx +++ b/integrations/llms/together-ai.mdx @@ -43,7 +43,7 @@ To use Together AI with Portkey, [get your API key from here](https://api.togeth const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your Together AI Virtual Key + provider:"@PROVIDER" // Your Together AI Virtual Key }) ``` @@ -53,7 +53,7 @@ To use Together AI with Portkey, [get your API key from here](https://api.togeth portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Together AI + provider="@PROVIDER" # Replace with your virtual key for Together AI ) ``` diff --git a/integrations/llms/upstage.mdx b/integrations/llms/upstage.mdx index 07ce2e45..a142ca10 100644 --- a/integrations/llms/upstage.mdx +++ b/integrations/llms/upstage.mdx @@ -42,7 +42,7 @@ Portkey offers native integrations with [Upstage](https://www.upstage.ai/) for N portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Upstage + provider="@PROVIDER" # Replace with your virtual key for Upstage ) ``` @@ -51,7 +51,7 @@ Portkey offers native integrations with [Upstage](https://www.upstage.ai/) for N const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your Upstage Virtual Key + provider:"@PROVIDER" // Your Upstage Virtual Key }) ``` @@ -391,11 +391,11 @@ Here's a simplified version of how to use Portkey's Gateway Configuration: "targets": [ { "name": "solar-pro", - "virtual_key": "xx" + "provider":"@xx" }, { "name": "gpt-3.5", - "virtual_key": "yy" + "provider":"@yy" } ] } @@ -416,7 +416,7 @@ Here's a simplified version of how to use Portkey's Gateway Configuration: portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="VIRTUAL_KEY", + provider="@PROVIDER", config=portkey_config ) ``` @@ -426,7 +426,7 @@ Here's a simplified version of how to use Portkey's Gateway Configuration: const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", - virtualKey: "VIRTUAL_KEY", + provider:"@PROVIDER", config: portkeyConfig }) ``` @@ -457,7 +457,7 @@ Portkey's AI gateway enables you to enforce input/output checks on requests by a ```json { - "virtual_key":"upstage-xxx", + "provider:"@upstage-xxx", "before_request_hooks": [{ "id": "input-guardrail-id-xx" }], diff --git a/integrations/llms/vertex-ai.mdx b/integrations/llms/vertex-ai.mdx index b9052bf8..739d23a9 100644 --- a/integrations/llms/vertex-ai.mdx +++ b/integrations/llms/vertex-ai.mdx @@ -49,7 +49,7 @@ If you are integrating through Service Account File, [refer to this guide](/inte const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VERTEX_VIRTUAL_KEY", // Your Vertex AI Virtual Key + provider:"@VERTEX_PROVIDER", // Your Vertex AI Virtual Key }) ``` @@ -59,7 +59,7 @@ If you are integrating through Service Account File, [refer to this guide](/inte portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VERTEX_VIRTUAL_KEY" # Replace with your virtual key for Google + provider="@VERTEX_PROVIDER" # Replace with your virtual key for Google ) ``` @@ -281,7 +281,7 @@ Note that you will have to set [`strict_open_ai_compliance=False`](/product/ai-g # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + provider="@PROVIDER", strict_open_ai_compliance=False ) @@ -324,7 +324,7 @@ Note that you will have to set [`strict_open_ai_compliance=False`](/product/ai-g // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY", // your vertex-ai virtual key + provider:"@PROVIDER", // your vertex-ai virtual key strictOpenAiCompliance: false }); @@ -505,7 +505,7 @@ Note that you will have to set [`strict_open_ai_compliance=False`](/product/ai-g # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + provider="@PROVIDER", strict_open_ai_compliance=False ) @@ -552,7 +552,7 @@ Note that you will have to set [`strict_open_ai_compliance=False`](/product/ai-g // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY", // your vertex-ai virtual key + provider:"@PROVIDER", // your vertex-ai virtual key strictOpenAiCompliance: false }); @@ -776,7 +776,7 @@ import Portkey from 'portkey-ai'; const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", - virtualKey: "VERTEX_VIRTUAL_KEY" + provider:"@VERTEX_PROVIDER" }); // Generate embeddings @@ -800,7 +800,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VERTEX_VIRTUAL_KEY" + provider="@VERTEX_PROVIDER" ) # Generate embeddings @@ -822,7 +822,7 @@ get_embeddings() -H 'x-portkey-api-key: PORTKEY_API_KEY' \ -H 'x-portkey-provider: vertex-ai' \ -H 'Authorization: Bearer VERTEX_AI_ACCESS_TOKEN' \ - -H 'x-portkey-virtual-key: $VERTEX_VIRTUAL_KEY' \ + -H 'x-portkey-provider: $VERTEX_PROVIDER' \ --data-raw '{ "model": "textembedding-004", "input": "A HTTP 246 code is used to signify an AI response containing hallucinations or other inaccuracies", @@ -855,7 +855,7 @@ Portkey supports the `Imagen API` on Vertex AI for image generations, letting yo curl https://api.portkey.ai/v1/images/generations \ -H "Content-Type: application/json" \ -H "x-portkey-api-key: $PORTKEY_API_KEY" \ - -H "x-portkey-virtual-key: $PORTKEY_PROVIDER_VIRTUAL_KEY" \ + -H "x-portkey-provider: $PORTKEY_PROVIDER" \ -d '{ "prompt": "Cat flying to mars from moon", "model":"imagen-3.0-generate-001" @@ -866,7 +866,7 @@ from portkey_ai import Portkey client = Portkey( api_key = "PORTKEY_API_KEY", - virtual_key = "PROVIDER_VIRTUAL_KEY" + virtual_key = "PROVIDER" ) client.images.generate( @@ -879,7 +879,7 @@ import Portkey from 'portkey-ai'; const client = new Portkey({ apiKey: 'PORTKEY_API_KEY', - virtualKey: 'PROVIDER_VIRTUAL_KEY' + provider:'@PROVIDER' }); async function main() { diff --git a/integrations/llms/vertex-ai/batches.mdx b/integrations/llms/vertex-ai/batches.mdx index 0521a0bf..fdebcfe8 100644 --- a/integrations/llms/vertex-ai/batches.mdx +++ b/integrations/llms/vertex-ai/batches.mdx @@ -18,7 +18,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VERTEX_VIRTUAL_KEY", # Add your Vertex virtual key + provider="@VERTEX_PROVIDER", vertex_storage_bucket_name="your_bucket_name", # Specify the GCS bucket name provider_file_name="your_file_name.jsonl", # Specify the file name in GCS provider_model="gemini-1.5-flash-001" # Specify the model to use @@ -41,7 +41,7 @@ import * as fs from 'fs'; // Initialize the Portkey client const portkey = Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VERTEX_VIRTUAL_KEY", // Add your Vertex virtual key + provider:"@VERTEX_PROVIDER", vertexStorageBucketName: "your_bucket_name", // Specify the GCS bucket name providerFileName: "your_file_name.jsonl", // Specify the file name in GCS providerModel: "gemini-1.5-flash-001" // Specify the model to use @@ -67,7 +67,7 @@ openai = OpenAI( api_key='OPENAI_API_KEY', base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - virtual_key="VERTEX_VIRTUAL_KEY", + provider="@VERTEX_PROVIDER", api_key="PORTKEY_API_KEY", vertex_storage_bucket_name="your_bucket_name", provider_file_name="your_file_name.jsonl", @@ -94,7 +94,7 @@ const openai = new OpenAI({ apiKey: 'OPENAI_API_KEY', baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ - virtualKey: "VERTEX_VIRTUAL_KEY", + provider:"@VERTEX_PROVIDER", apiKey: "PORTKEY_API_KEY", vertexStorageBucketName: "your_bucket_name", providerFileName: "your_file_name.jsonl", @@ -116,7 +116,7 @@ const openai = new OpenAI({ ```sh curl -X POST --header 'x-portkey-api-key: ' \ - --header 'x-portkey-virtual-key: ' \ + --header 'x-portkey-provider: ' \ --header 'x-portkey-vertex-storage-bucket-name: ' \ --header 'x-portkey-provider-file-name: .jsonl' \ --header 'x-portkey-provider-model: ' \ @@ -137,7 +137,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VERTEX_VIRTUAL_KEY" # Add your Vertex virtual key + provider="@VERTEX_PROVIDER" ) # Create a batch inference job @@ -158,7 +158,7 @@ import { Portkey } from "portkey-ai"; // Initialize the Portkey client const portkey = Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VERTEX_VIRTUAL_KEY" // Add your Vertex virtual key + provider:"@VERTEX_PROVIDER" }); (async () => { @@ -183,7 +183,7 @@ openai = OpenAI( api_key='OPENAI_API_KEY', base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - virtual_key="VERTEX_VIRTUAL_KEY", + provider="@VERTEX_PROVIDER", api_key="PORTKEY_API_KEY" ) ) @@ -208,7 +208,7 @@ const openai = new OpenAI({ apiKey: 'OPENAI_API_KEY', baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ - virtualKey: "VERTEX_VIRTUAL_KEY", + provider:"@VERTEX_PROVIDER", apiKey: "PORTKEY_API_KEY" }) }); @@ -230,7 +230,7 @@ const openai = new OpenAI({ ```sh curl -X POST --header 'Content-Type: application/json' \ --header 'x-portkey-api-key: ' \ - --header 'x-portkey-virtual-key: ' \ + --header 'x-portkey-provider: ' \ --data \ $'{"input_file_id": "", "endpoint": "/v1/chat/completions", "completion_window": "24h", "model":"gemini-1.5-flash-001"}' \ 'https://api.portkey.ai/v1/batches' @@ -248,7 +248,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VERTEX_VIRTUAL_KEY" # Add your Vertex virtual key + provider="@VERTEX_PROVIDER" ) # List all batch jobs @@ -266,7 +266,7 @@ import { Portkey } from "portkey-ai"; // Initialize the Portkey client const portkey = Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VERTEX_VIRTUAL_KEY" // Add your Vertex virtual key + provider:"@VERTEX_PROVIDER" }); (async () => { @@ -288,7 +288,7 @@ openai = OpenAI( api_key='OPENAI_API_KEY', base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - virtual_key="VERTEX_VIRTUAL_KEY", + provider="@VERTEX_PROVIDER", api_key="PORTKEY_API_KEY" ) ) @@ -311,7 +311,7 @@ const openai = new OpenAI({ apiKey: 'OPENAI_API_KEY', baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ - virtualKey: "VERTEX_VIRTUAL_KEY", + provider:"@VERTEX_PROVIDER", apiKey: "PORTKEY_API_KEY" }) }); @@ -330,7 +330,7 @@ const openai = new OpenAI({ ```sh curl -X GET --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-provider: ' \ 'https://api.portkey.ai/v1/batches' ``` @@ -346,7 +346,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VERTEX_VIRTUAL_KEY" # Add your Vertex virtual key + provider="@VERTEX_PROVIDER" ) # Retrieve a specific batch job @@ -364,7 +364,7 @@ import { Portkey } from "portkey-ai"; // Initialize the Portkey client const portkey = Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VERTEX_VIRTUAL_KEY" // Add your Vertex virtual key + provider:"@VERTEX_PROVIDER" }); (async () => { @@ -386,7 +386,7 @@ openai = OpenAI( api_key='OPENAI_API_KEY', base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - virtual_key="VERTEX_VIRTUAL_KEY", + provider="@VERTEX_PROVIDER", api_key="PORTKEY_API_KEY" ) ) @@ -408,7 +408,7 @@ const openai = new OpenAI({ apiKey: 'OPENAI_API_KEY', baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ - virtualKey: "VERTEX_VIRTUAL_KEY", + provider:"@VERTEX_PROVIDER", apiKey: "PORTKEY_API_KEY" }) }); @@ -426,7 +426,7 @@ const openai = new OpenAI({ ```sh curl -X GET --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-provider: ' \ 'https://api.portkey.ai/v1/batches/' ``` @@ -437,7 +437,7 @@ curl -X GET --header 'x-portkey-api-key: ' \ ```sh curl -X GET --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-provider: ' \ 'https://api.portkey.ai/v1/batches//output' ``` diff --git a/integrations/llms/vertex-ai/controlled-generations.mdx b/integrations/llms/vertex-ai/controlled-generations.mdx index 4099d8e4..3ab6b019 100644 --- a/integrations/llms/vertex-ai/controlled-generations.mdx +++ b/integrations/llms/vertex-ai/controlled-generations.mdx @@ -31,7 +31,7 @@ class MathReasoning(BaseModel): portkey = Portkey( apiKey= "PORTKEY_API_KEY", - virtual_key="VERTEX_VIRTUAL_KEY" + provider="@VERTEX_PROVIDER" ) completion = portkey.beta.chat.completions.parse( @@ -63,7 +63,7 @@ To use Zod with VerteX AI you will also need to import `{ zodResponseFormat }` f const portkey = new Portkey({ apiKey: "YOUR_API_KEY", - virtualKey: "YOUR_GEMINI_VIRTUAL_KEY" + provider:"@YOUR_GEMINI_PROVIDER" }); async function runMathTutor() { @@ -107,7 +107,7 @@ You can also use enums to constrain the model's output to a predefined set of va # Initialize Portkey with API details portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="VERTEX_VIRTUAL_KEY" + provider="@VERTEX_PROVIDER" ) # Simple enum classification @@ -142,7 +142,7 @@ This method is more portable across different languages and doesn't require addi const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", - virtualKey: "VERTEX_VIRTUAL_KEY", + provider:"@VERTEX_PROVIDER", }); async function main() { @@ -196,7 +196,7 @@ This method is more portable across different languages and doesn't require addi portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="VERTEX_VIRTUAL_KEY" + provider="@VERTEX_PROVIDER" ) completion = portkey.chat.completions.create( @@ -241,7 +241,7 @@ This method is more portable across different languages and doesn't require addi ```sh curl https://api.portkey.ai/v1/chat/completions \ -H "x-portkey-api-key: $PORTKEY_API_KEY" \ - -H "x-portkey-virtual-key: $VERTEX_VIRTUAL_KEY" \ + -H "x-portkey-provider: $VERTEX_PROVIDER" \ -H "Content-Type: application/json" \ -d '{ "model": "gemini-1.5-pro-002", diff --git a/integrations/llms/vertex-ai/embeddings.mdx b/integrations/llms/vertex-ai/embeddings.mdx index 2c654a2f..69cd12ab 100644 --- a/integrations/llms/vertex-ai/embeddings.mdx +++ b/integrations/llms/vertex-ai/embeddings.mdx @@ -13,7 +13,7 @@ Portkey provides a standardized interface for embedding multiple modalities. client = Portkey( api_key="YOUR_PORTKEY_API_KEY", # defaults to os.environ.get("PORTKEY_API_KEY") - virtual_key="VIRTUAL_KEY", + provider="@PROVIDER", ) embeddings = client.embeddings.create( @@ -29,7 +29,7 @@ Portkey provides a standardized interface for embedding multiple modalities. const portkey = new Portkey({ apiKey: "YOUR_API_KEY", - virtualKey: "YOUR_VIRTUAL_KEY" + provider:"@YOUR_PROVIDER" }); const embedding = await portkey.embeddings.create({ @@ -45,7 +45,7 @@ Portkey provides a standardized interface for embedding multiple modalities. curl --location 'https://api.portkey.ai/v1/embeddings' \ --header 'Content-Type: application/json' \ --header 'x-portkey-api-key: PORTKEY_API_KEY' \ - --header 'x-portkey-virtual-key: PORTKEY_VIRTUAL_KEY' \ + --header 'x-portkey-provider: PORTKEY_PROVIDER' \ --data-raw '{ "model": "textembedding-gecko@003", "input": [ @@ -88,7 +88,7 @@ Portkey provides a standardized interface for embedding multiple modalities. defaultHeaders: createHeaders({ provider: "vertex-ai", apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "PORTKEY_VIRTUAL_KEY" + provider:"@PORTKEY_PROVIDER" }) }); @@ -111,7 +111,7 @@ Portkey provides a standardized interface for embedding multiple modalities. client = Portkey( api_key="YOUR_PORTKEY_API_KEY", # defaults to os.environ.get("PORTKEY_API_KEY") - virtual_key="VIRTUAL_KEY", + provider="@PROVIDER", ) embeddings = client.embeddings.create( @@ -133,7 +133,7 @@ Portkey provides a standardized interface for embedding multiple modalities. const portkey = new Portkey({ apiKey: "YOUR_API_KEY", - virtualKey: "YOUR_VIRTUAL_KEY" + provider:"@YOUR_PROVIDER" }); const embedding = await portkey.embeddings.create({ @@ -156,7 +156,7 @@ Portkey provides a standardized interface for embedding multiple modalities. curl --location 'https://api.portkey.ai/v1/embeddings' \ --header 'Content-Type: application/json' \ --header 'x-portkey-api-key: PORTKEY_API_KEY' \ - --header 'x-portkey-virtual-key: PORTKEY_VIRTUAL_KEY' \ + --header 'x-portkey-provider: PORTKEY_PROVIDER' \ --data-raw '{ "model": "multimodalembedding@001", "input": [ @@ -207,7 +207,7 @@ Portkey provides a standardized interface for embedding multiple modalities. baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "PORTKEY_VIRTUAL_KEY" + provider:"@PORTKEY_PROVIDER" }) }); @@ -238,7 +238,7 @@ Portkey provides a standardized interface for embedding multiple modalities. client = Portkey( api_key="YOUR_PORTKEY_API_KEY", # defaults to os.environ.get("PORTKEY_API_KEY") - virtual_key="VIRTUAL_KEY", + provider="@PROVIDER", ) embeddings = client.embeddings.create( @@ -262,7 +262,7 @@ Portkey provides a standardized interface for embedding multiple modalities. const portkey = new Portkey({ apiKey: "YOUR_API_KEY", - virtualKey: "YOUR_VIRTUAL_KEY" + provider:"@YOUR_PROVIDER" }); const embedding = await portkey.embeddings.create({ @@ -288,7 +288,7 @@ Portkey provides a standardized interface for embedding multiple modalities. curl --location 'https://api.portkey.ai/v1/embeddings' \ --header 'Content-Type: application/json' \ --header 'x-portkey-api-key: PORTKEY_API_KEY' \ - --header 'x-portkey-virtual-key: PORTKEY_VIRTUAL_KEY' \ + --header 'x-portkey-provider: PORTKEY_PROVIDER' \ --data-raw '{ "model": "multimodalembedding@001", "input": [ @@ -345,7 +345,7 @@ Portkey provides a standardized interface for embedding multiple modalities. baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "PORTKEY_VIRTUAL_KEY" + provider:"@PORTKEY_PROVIDER" }) }); diff --git a/integrations/llms/vertex-ai/files.mdx b/integrations/llms/vertex-ai/files.mdx index d2e18b87..cf7ebf7a 100644 --- a/integrations/llms/vertex-ai/files.mdx +++ b/integrations/llms/vertex-ai/files.mdx @@ -15,7 +15,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VERTEX_VIRTUAL_KEY", # Add your Vertex virtual key + provider="@VERTEX_PROVIDER", vertex_storage_bucket_name="your_bucket_name", # Specify the GCS bucket name provider_file_name="your_file_name.jsonl", # Specify the file name in GCS provider_model="gemini-1.5-flash-001" # Specify the model to use @@ -37,7 +37,7 @@ import * as fs from 'fs'; // Initialize the Portkey client const portkey = Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VERTEX_VIRTUAL_KEY", // Add your Vertex virtual key + provider:"@VERTEX_PROVIDER", vertexStorageBucketName: "your_bucket_name", // Specify the GCS bucket name providerFileName: "your_file_name.jsonl", // Specify the file name in GCS providerModel: "gemini-1.5-flash-001" // Specify the model to use @@ -58,7 +58,7 @@ uploadFile(); ```sh curl -X POST --header 'x-portkey-api-key: ' \ - --header 'x-portkey-virtual-key: ' \ + --header 'x-portkey-provider: ' \ --header 'x-portkey-vertex-storage-bucket-name: ' \ --header 'x-portkey-provider-file-name: .jsonl' \ --header 'x-portkey-provider-model: ' \ @@ -77,7 +77,7 @@ const openai = new OpenAI({ apiKey: 'OPENAI_API_KEY', baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ - virtualKey: "VERTEX_VIRTUAL_KEY", + provider:"@VERTEX_PROVIDER", apiKey: "PORTKEY_API_KEY", vertexStorageBucketName: "your_bucket_name", providerFileName: "your_file_name.jsonl", @@ -106,7 +106,7 @@ openai = OpenAI( api_key='OPENAI_API_KEY', base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - virtual_key="VERTEX_VIRTUAL_KEY", + provider="@VERTEX_PROVIDER", api_key="PORTKEY_API_KEY", vertex_storage_bucket_name="your_bucket_name", provider_file_name="your_file_name.jsonl", @@ -135,7 +135,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VERTEX_VIRTUAL_KEY" # Add your Vertex virtual key + provider="@VERTEX_PROVIDER" ) file = portkey.files.retrieve(file_id="file_id") @@ -150,7 +150,7 @@ import { Portkey } from 'portkey-ai'; // Initialize the Portkey client const portkey = Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VERTEX_VIRTUAL_KEY" // Add your Vertex virtual key + provider:"@VERTEX_PROVIDER" }); const getFile = async () => { @@ -165,7 +165,7 @@ getFile(); ```sh curl -X GET --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-provider: ' \ 'https://api.portkey.ai/v1/files/' ``` @@ -178,7 +178,7 @@ const openai = new OpenAI({ apiKey: 'OPENAI_API_KEY', baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ - virtualKey: "VERTEX_VIRTUAL_KEY", + provider:"@VERTEX_PROVIDER", apiKey: "PORTKEY_API_KEY" }) }); @@ -201,7 +201,7 @@ openai = OpenAI( api_key='OPENAI_API_KEY', base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - virtual_key="VERTEX_VIRTUAL_KEY", + provider="@VERTEX_PROVIDER", api_key="PORTKEY_API_KEY" ) ) @@ -224,7 +224,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VERTEX_VIRTUAL_KEY" # Add your Vertex virtual key + provider="@VERTEX_PROVIDER" ) file_content = portkey.files.content(file_id="file_id") @@ -239,7 +239,7 @@ import { Portkey } from 'portkey-ai'; // Initialize the Portkey client const portkey = Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VERTEX_VIRTUAL_KEY" // Add your Vertex virtual key + provider:"@VERTEX_PROVIDER" }); const getFileContent = async () => { @@ -254,7 +254,7 @@ getFileContent(); ```sh curl -X GET --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-provider: ' \ 'https://api.portkey.ai/v1/files//content' ``` @@ -267,7 +267,7 @@ const openai = new OpenAI({ apiKey: 'OPENAI_API_KEY', baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ - virtualKey: "VERTEX_VIRTUAL_KEY", + provider:"@VERTEX_PROVIDER", apiKey: "PORTKEY_API_KEY" }) }); @@ -290,7 +290,7 @@ openai = OpenAI( api_key='OPENAI_API_KEY', base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - virtual_key="VERTEX_VIRTUAL_KEY", + provider="@VERTEX_PROVIDER", api_key="PORTKEY_API_KEY" ) ) diff --git a/integrations/llms/vertex-ai/fine-tuning.mdx b/integrations/llms/vertex-ai/fine-tuning.mdx index 2e952f52..494e10d5 100644 --- a/integrations/llms/vertex-ai/fine-tuning.mdx +++ b/integrations/llms/vertex-ai/fine-tuning.mdx @@ -13,7 +13,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VERTEX_VIRTUAL_KEY", # Add your Vertex virtual key + provider="@VERTEX_PROVIDER", vertex_storage_bucket_name="your_bucket_name", # Specify the GCS bucket name provider_file_name="your_file_name.jsonl", # Specify the file name in GCS provider_model="gemini-1.5-flash-001" # Specify the model to fine-tune @@ -36,7 +36,7 @@ import * as fs from 'fs'; // Initialize the Portkey client const portkey = Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VERTEX_VIRTUAL_KEY", // Add your Vertex virtual key + provider:"@VERTEX_PROVIDER", vertexStorageBucketName: "your_bucket_name", // Specify the GCS bucket name providerFileName: "your_file_name.jsonl", // Specify the file name in GCS providerModel: "gemini-1.5-flash-001" // Specify the model to fine-tune @@ -62,7 +62,7 @@ openai = OpenAI( api_key='OPENAI_API_KEY', base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - virtual_key="VERTEX_VIRTUAL_KEY", + provider="@VERTEX_PROVIDER", api_key="PORTKEY_API_KEY", vertex_storage_bucket_name="your_bucket_name", provider_file_name="your_file_name.jsonl", @@ -89,7 +89,7 @@ const openai = new OpenAI({ apiKey: 'OPENAI_API_KEY', baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ - virtualKey: "VERTEX_VIRTUAL_KEY", + provider:"@VERTEX_PROVIDER", apiKey: "PORTKEY_API_KEY", vertexStorageBucketName: "your_bucket_name", providerFileName: "your_file_name.jsonl", @@ -111,7 +111,7 @@ const openai = new OpenAI({ ```sh curl -X POST --header 'x-portkey-api-key: ' \ - --header 'x-portkey-virtual-key: ' \ + --header 'x-portkey-provider: ' \ --header 'x-portkey-vertex-storage-bucket-name: ' \ --header 'x-portkey-provider-file-name: .jsonl' \ --header 'x-portkey-provider-model: ' \ @@ -132,7 +132,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VERTEX_VIRTUAL_KEY" # Add your Vertex virtual key + provider="@VERTEX_PROVIDER" ) # Create a fine-tuning job @@ -155,7 +155,7 @@ import { Portkey } from "portkey-ai"; // Initialize the Portkey client const portkey = Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VERTEX_VIRTUAL_KEY" // Add your Vertex virtual key + provider:"@VERTEX_PROVIDER" }); (async () => { @@ -182,7 +182,7 @@ openai = OpenAI( api_key='OPENAI_API_KEY', base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - virtual_key="VERTEX_VIRTUAL_KEY", + provider="@VERTEX_PROVIDER", api_key="PORTKEY_API_KEY" ) ) @@ -209,7 +209,7 @@ const openai = new OpenAI({ apiKey: 'OPENAI_API_KEY', baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ - virtualKey: "VERTEX_VIRTUAL_KEY", + provider:"@VERTEX_PROVIDER", apiKey: "PORTKEY_API_KEY" }) }); @@ -233,7 +233,7 @@ const openai = new OpenAI({ ```sh curl -X POST --header 'Content-Type: application/json' \ --header 'x-portkey-api-key: ' \ - --header 'x-portkey-virtual-key: ' \ + --header 'x-portkey-provider: ' \ --data \ $'{"model": "", "suffix": "", "training_file": "gs:///.jsonl", "hyperparameters": {"n_epochs": 2}}\n' \ 'https://api.portkey.ai/v1/fine_tuning/jobs' @@ -251,7 +251,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VERTEX_VIRTUAL_KEY" # Add your Vertex virtual key + provider="@VERTEX_PROVIDER" ) # List all fine-tuning jobs @@ -269,7 +269,7 @@ import { Portkey } from "portkey-ai"; // Initialize the Portkey client const portkey = Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VERTEX_VIRTUAL_KEY" // Add your Vertex virtual key + provider:"@VERTEX_PROVIDER" }); (async () => { @@ -291,7 +291,7 @@ openai = OpenAI( api_key='OPENAI_API_KEY', base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - virtual_key="VERTEX_VIRTUAL_KEY", + provider="@VERTEX_PROVIDER", api_key="PORTKEY_API_KEY" ) ) @@ -313,7 +313,7 @@ const openai = new OpenAI({ apiKey: 'OPENAI_API_KEY', baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ - virtualKey: "VERTEX_VIRTUAL_KEY", + provider:"@VERTEX_PROVIDER", apiKey: "PORTKEY_API_KEY" }) }); @@ -331,7 +331,7 @@ const openai = new OpenAI({ ```sh curl -X GET --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-provider: ' \ 'https://api.portkey.ai/v1/fine_tuning/jobs' ``` @@ -347,7 +347,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VERTEX_VIRTUAL_KEY" # Add your Vertex virtual key + provider="@VERTEX_PROVIDER" ) # Retrieve a specific fine-tuning job @@ -365,7 +365,7 @@ import { Portkey } from "portkey-ai"; // Initialize the Portkey client const portkey = Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VERTEX_VIRTUAL_KEY" // Add your Vertex virtual key + provider:"@VERTEX_PROVIDER" }); (async () => { @@ -387,7 +387,7 @@ openai = OpenAI( api_key='OPENAI_API_KEY', base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - virtual_key="VERTEX_VIRTUAL_KEY", + provider="@VERTEX_PROVIDER", api_key="PORTKEY_API_KEY" ) ) @@ -409,7 +409,7 @@ const openai = new OpenAI({ apiKey: 'OPENAI_API_KEY', baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ - virtualKey: "VERTEX_VIRTUAL_KEY", + provider:"@VERTEX_PROVIDER", apiKey: "PORTKEY_API_KEY" }) }); @@ -427,7 +427,7 @@ const openai = new OpenAI({ ```sh curl -X GET --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-provider: ' \ 'https://api.portkey.ai/v1/fine_tuning/jobs/' ``` @@ -443,7 +443,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VERTEX_VIRTUAL_KEY" # Add your Vertex virtual key + provider="@VERTEX_PROVIDER" ) # Cancel a fine-tuning job @@ -461,7 +461,7 @@ import { Portkey } from "portkey-ai"; // Initialize the Portkey client const portkey = Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VERTEX_VIRTUAL_KEY" // Add your Vertex virtual key + provider:"@VERTEX_PROVIDER" }); (async () => { @@ -483,7 +483,7 @@ openai = OpenAI( api_key='OPENAI_API_KEY', base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - virtual_key="VERTEX_VIRTUAL_KEY", + provider="@VERTEX_PROVIDER", api_key="PORTKEY_API_KEY" ) ) @@ -505,7 +505,7 @@ const openai = new OpenAI({ apiKey: 'OPENAI_API_KEY', baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ - virtualKey: "VERTEX_VIRTUAL_KEY", + provider:"@VERTEX_PROVIDER", apiKey: "PORTKEY_API_KEY" }) }); @@ -523,7 +523,7 @@ const openai = new OpenAI({ ```sh curl -X POST --header 'x-portkey-api-key: ' \ ---header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-provider: ' \ 'https://api.portkey.ai/v1/fine_tuning/jobs//cancel' ``` diff --git a/integrations/llms/vllm.mdx b/integrations/llms/vllm.mdx index 0481191c..395794af 100644 --- a/integrations/llms/vllm.mdx +++ b/integrations/llms/vllm.mdx @@ -120,7 +120,7 @@ You can now use this virtual key in your requests: ```js const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", - virtualKey: "YOUR_SELF_HOSTED_LLM_VIRTUAL_KEY" + provider:"@YOUR_SELF_HOSTED_LLM_PROVIDER" async function main() { const response = await client.chat.completions.create({ @@ -136,7 +136,7 @@ You can now use this virtual key in your requests: ```python portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="YOUR_SELF_HOSTED_LLM_VIRTUAL_KEY" + provider="@YOUR_SELF_HOSTED_LLM_PROVIDER" ) response = portkey.chat.completions.create( diff --git a/integrations/llms/workers-ai.mdx b/integrations/llms/workers-ai.mdx index 83a04228..c2eebb65 100644 --- a/integrations/llms/workers-ai.mdx +++ b/integrations/llms/workers-ai.mdx @@ -41,7 +41,7 @@ To use Workers AI with Portkey, [get your API key from here](https://console.gro const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your Workers AI Virtual Key + provider:"@PROVIDER" // Your Workers AI Virtual Key }) ``` @@ -51,7 +51,7 @@ To use Workers AI with Portkey, [get your API key from here](https://console.gro portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Groq + provider="@PROVIDER" # Replace with your virtual key for Groq ) ``` @@ -105,7 +105,7 @@ import Portkey from 'portkey-ai'; // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key + provider:"@PROVIDER" }); async function main() { @@ -129,7 +129,7 @@ from IPython.display import display, Image # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key + provider="@PROVIDER" ) image = portkey.images.generate( @@ -201,7 +201,7 @@ display(Image(url=image.data[0].url)) curl "https://api.portkey.ai/v1/images/generations" \ -H "Content-Type: application/json" \ -H "x-portkey-api-key: $PORTKEY_API_KEY" \ - -H "x-portkey-virtual-key: $WORKERS_AI_VIRTUAL_KEY" \ + -H "x-portkey-provider: $WORKERS_AI_PROVIDER" \ -d '{ "model": "image_model_name", "prompt": "Lucy in the sky with diamonds" diff --git a/integrations/llms/x-ai.mdx b/integrations/llms/x-ai.mdx index 307762b7..5db451b5 100644 --- a/integrations/llms/x-ai.mdx +++ b/integrations/llms/x-ai.mdx @@ -23,7 +23,7 @@ import Portkey from 'portkey-ai'; const client = new Portkey({ apiKey: 'PORTKEY_API_KEY', - virtualKey: 'PROVIDER_VIRTUAL_KEY' + provider:'@PROVIDER' }); async function main() { @@ -42,7 +42,7 @@ from portkey_ai import Portkey client = Portkey( api_key = "PORTKEY_API_KEY", - virtual_key = "PROVIDER_VIRTUAL_KEY" + virtual_key = "PROVIDER" ) response = client.chat.completions.create( @@ -56,7 +56,7 @@ print(response.choices[0].message) curl https://api.portkey.ai/v1/chat/completions \ -H "Content-Type: application/json" \ -H "x-portkey-api-key: $PORTKEY_API_KEY" \ - -H "x-portkey-virtual-key: $PORTKEY_PROVIDER_VIRTUAL_KEY" \ + -H "x-portkey-provider: $PORTKEY_PROVIDER" \ -d '{ "model": "grok-beta", "messages": [ @@ -73,7 +73,7 @@ client = OpenAI( base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( api_key="PORTKEY_API_KEY", - virtual_key="OPENAI_VIRTUAL_KEY" + provider="@OPENAI_PROVIDER" ) ) @@ -95,7 +95,7 @@ const openai = new OpenAI({ baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ apiKey: "PORTKEY_API_KEY", - virtualKey: "OPENAI_VIRTUAL_KEY" + provider:"@OPENAI_PROVIDER" }) }); @@ -137,7 +137,7 @@ import Portkey from 'portkey-ai'; const client = new Portkey({ baseUrl: 'http://localhost:8080/v1', apiKey: 'PORTKEY_API_KEY', - virtualKey: 'PROVIDER_VIRTUAL_KEY' + provider:'@PROVIDER' }); async function main() { @@ -157,7 +157,7 @@ from portkey_ai import Portkey client = Portkey( base_url = 'http://localhost:8080/v1', api_key = "PORTKEY_API_KEY", - virtual_key = "PROVIDER_VIRTUAL_KEY" + virtual_key = "PROVIDER" ) response = client.chat.completions.create( @@ -173,7 +173,7 @@ print(response.choices[0].message) curl http://localhost:8080/v1/chat/completions \ -H "Content-Type: application/json" \ -H "x-portkey-api-key: $PORTKEY_API_KEY" \ - -H "x-portkey-virtual-key: $PORTKEY_PROVIDER_VIRTUAL_KEY" \ + -H "x-portkey-provider: $PORTKEY_PROVIDER" \ -d '{ "model": "grok-beta", "messages": [ @@ -190,7 +190,7 @@ client = OpenAI( base_url="https://localhost:8080/v1", default_headers=createHeaders( api_key="PORTKEY_API_KEY", - virtual_key="OPENAI_VIRTUAL_KEY" + provider="@OPENAI_PROVIDER" ) ) @@ -212,7 +212,7 @@ const openai = new OpenAI({ baseURL: 'https://localhost:8080/v1', defaultHeaders: createHeaders({ apiKey: "PORTKEY_API_KEY", - virtualKey: "OPENAI_VIRTUAL_KEY" + provider:"@OPENAI_PROVIDER" }) }); @@ -449,11 +449,11 @@ Here's a simplified version of how to use Portkey's Gateway Configuration: "targets": [ { "name": "grok-beta", - "virtual_key": "xx" + "provider":"@xx" }, { "name": "grok-2-1212", - "virtual_key": "yy" + "provider":"@yy" } ] } @@ -474,7 +474,7 @@ Here's a simplified version of how to use Portkey's Gateway Configuration: portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="VIRTUAL_KEY", + provider="@PROVIDER", config=portkey_config ) ``` @@ -484,7 +484,7 @@ Here's a simplified version of how to use Portkey's Gateway Configuration: const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", - virtualKey: "VIRTUAL_KEY", + provider:"@PROVIDER", config: portkeyConfig }) ``` @@ -517,7 +517,7 @@ Portkey's AI gateway enables you to enforce input/output checks on requests by a ```json { - "virtual_key":"xai-xxx", + "provider:"@xai-xxx", "before_request_hooks": [{ "id": "input-guardrail-id-xx" }], diff --git a/integrations/llms/zhipu.mdx b/integrations/llms/zhipu.mdx index 794e3505..2b6b3774 100644 --- a/integrations/llms/zhipu.mdx +++ b/integrations/llms/zhipu.mdx @@ -40,7 +40,7 @@ To use ZhipuAI / ChatGLM / BigModel with Portkey, [get your API key from here](h const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your ZhipuAI Virtual Key + provider:"@PROVIDER" // Your ZhipuAI Virtual Key }) ``` @@ -50,7 +50,7 @@ from portkey_ai import Portkey portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for ZhipuAI + provider="@PROVIDER" # Replace with your virtual key for ZhipuAI ) ``` @@ -64,7 +64,7 @@ const portkey = new OpenAI({ baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ apiKey: "PORTKEY_API_KEY", - virtualKey: "ZHIPUAI_VIRTUAL_KEY", + provider:"@ZHIPUAI_PROVIDER", }), }); ``` @@ -79,7 +79,7 @@ portkey = OpenAI( base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( api_key="PORTKEY_API_KEY", - virtual_key="ZHIPUAI_VIRTUAL_KEY" + provider="@ZHIPUAI_PROVIDER" ) ) ``` @@ -121,7 +121,7 @@ print(completion) curl https://api.portkey.ai/v1/chat/completions \ -H "Content-Type: application/json" \ -H "x-portkey-api-key: $PORTKEY_API_KEY" \ - -H "x-portkey-virtual-key: $ZHIPUAI_VIRTUAL_KEY" \ + -H "x-portkey-provider: $ZHIPUAI_PROVIDER" \ -d '{ "messages": [{"role": "user","content": "Hello!"}], "model": "glm-4", diff --git a/integrations/tracing-providers/arize.mdx b/integrations/tracing-providers/arize.mdx index 61c6d7e2..67ff92f0 100644 --- a/integrations/tracing-providers/arize.mdx +++ b/integrations/tracing-providers/arize.mdx @@ -104,8 +104,8 @@ from portkey_ai import Portkey # Initialize Portkey client portkey = Portkey( - api_key="your-portkey-api-key", # Optional for self-hosted - virtual_key="your-openai-virtual-key" # Or use provider-specific virtual keys + api_key="your-portkey-api-key", + provider="@your-openai-portkey-provider" ) response = portkey.chat.completions.create( @@ -149,12 +149,12 @@ advanced_config = { }, "targets": [ { - "virtual_key": "openai-vk", + "provider":"@openai-vk", "weight": 0.7, "override_params": {"model": "gpt-4o"} }, { - "virtual_key": "anthropic-vk", + "provider":"@anthropic-vk", "weight": 0.3, "override_params": {"model": "claude-3-opus-20240229"} } @@ -172,10 +172,9 @@ advanced_config = { # Initialize Portkey-powered client client = OpenAI( - api_key="not-needed", # Virtual keys handle auth + api_key="PORTKEY_API_KEY", base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - api_key=os.environ.get("PORTKEY_API_KEY"), config=advanced_config, metadata={ "user_id": "user-123", @@ -213,7 +212,7 @@ Access OpenAI, Anthropic, Google, Cohere, Mistral, Llama, and 1600+ models throu Use the same code to call any LLM provider. Switch between models and providers without changing your application code. - + Secure vault for API keys with budget limits, rate limiting, and access controls. Never expose raw API keys in your code. @@ -346,7 +345,7 @@ Deploy Portkey in your own AWS, Azure, or GCP environment with full control. Discover all AI Gateway capabilities beyond observability - + Secure your API keys and set budgets diff --git a/integrations/tracing-providers/langfuse.mdx b/integrations/tracing-providers/langfuse.mdx index cadf471e..19ca4b76 100644 --- a/integrations/tracing-providers/langfuse.mdx +++ b/integrations/tracing-providers/langfuse.mdx @@ -40,7 +40,7 @@ client = OpenAI( base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_VIRTUAL_KEY", # Optional: Use virtual keys + provider="@YOUR_PROVIDER", # config="YOUR_CONFIG_ID", # Optional: Use saved configs # trace_id="YOUR_TRACE_ID", # Optional: Custom trace ID ) @@ -61,25 +61,21 @@ This integration automatically logs requests to both Langfuse and Portkey, givin ## Using Portkey Features with Langfuse -### 1. Virtual Keys +### 1. LLM Integrations -Virtual Keys in Portkey allow you to securely manage API keys and set usage limits. Use them with Langfuse for better security: +LLM Integrations in Portkey allow you to securely manage API keys and set usage limits. Use them with Langfuse for better security: ```python from langfuse.openai import OpenAI from portkey_ai import createHeaders, PORTKEY_GATEWAY_URL client = OpenAI( - api_key="dummy_key", # Not used when virtual key is provided - base_url=PORTKEY_GATEWAY_URL, - default_headers=createHeaders( - api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_VIRTUAL_KEY" - ) + api_key="PORTKEY_API_KEY", + base_url=PORTKEY_GATEWAY_URL ) response = client.chat.completions.create( - model="gpt-4o", + model="@openai/gpt-4o", messages=[{"role": "user", "content": "Explain quantum computing"}] ) ``` @@ -93,36 +89,23 @@ Switch between 250+ LLM providers while maintaining Langfuse observability: ```python client = OpenAI( api_key="YOUR_OPENAI_KEY", - base_url=PORTKEY_GATEWAY_URL, - default_headers=createHeaders( - api_key="YOUR_PORTKEY_API_KEY", - provider="openai" - ) + base_url=PORTKEY_GATEWAY_URL ) ``` ```python client = OpenAI( - api_key="YOUR_ANTHROPIC_KEY", - base_url=PORTKEY_GATEWAY_URL, - default_headers=createHeaders( - api_key="YOUR_PORTKEY_API_KEY", - provider="anthropic", - metadata={"model_override": "claude-3-opus-20240229"} - ) + api_key="PORTKEY_API_KEY", + base_url=PORTKEY_GATEWAY_URL ) ``` ```python client = OpenAI( - api_key="dummy_key", - base_url=PORTKEY_GATEWAY_URL, - default_headers=createHeaders( - api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_AZURE_VIRTUAL_KEY" - ) + api_key="PORTKEY_API_KEY", + base_url=PORTKEY_GATEWAY_URL ) ``` @@ -135,12 +118,8 @@ Use Portkey's config system for advanced features while tracking in Langfuse: ```python # Create a config in Portkey dashboard first, then reference it client = OpenAI( - api_key="dummy_key", - base_url=PORTKEY_GATEWAY_URL, - default_headers=createHeaders( - api_key="YOUR_PORTKEY_API_KEY", - config="pc-langfuse-prod" # Your saved config ID - ) + api_key="PORTKEY_API_KEY", + base_url=PORTKEY_GATEWAY_URL ) ``` @@ -152,11 +131,11 @@ Example config for fallback between providers: }, "targets": [ { - "virtual_key": "openai-key", + "provider":"@openai-key", "override_params": {"model": "gpt-4o"} }, { - "virtual_key": "anthropic-key", + "provider":"@anthropic-key", "override_params": {"model": "claude-3-opus-20240229"} } ] @@ -173,14 +152,13 @@ config = { "mode": "semantic", "max_age": 3600 }, - "virtual_key": "YOUR_VIRTUAL_KEY" + "provider":"@YOUR_PROVIDER" } client = OpenAI( - api_key="dummy_key", + api_key="PORTKEY_API_KEY", base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - api_key="YOUR_PORTKEY_API_KEY", config=config ) ) @@ -192,11 +170,9 @@ Add custom metadata visible in both Langfuse and Portkey: ```python client = OpenAI( - api_key="YOUR_API_KEY", + api_key="PORTKEY_API_KEY", base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - api_key="YOUR_PORTKEY_API_KEY", - provider="openai", metadata={ "user_id": "user_123", "session_id": "session_456", @@ -286,7 +262,7 @@ client = OpenAI( ## Next Steps -- [Create Virtual Keys](/product/ai-gateway/virtual-keys) for secure API key management +- [Create LLM Integrations](/product/integrations) for secure API key management - [Build Configs](/product/ai-gateway/configs) for advanced routing - [Set up Guardrails](/product/guardrails) for content filtering - [Implement Caching](/product/ai-gateway/cache-simple-and-semantic) for cost optimization diff --git a/integrations/vector-databases/milvus.mdx b/integrations/vector-databases/milvus.mdx index 36f03bf0..d0643f44 100644 --- a/integrations/vector-databases/milvus.mdx +++ b/integrations/vector-databases/milvus.mdx @@ -5,7 +5,7 @@ title: "Milvus" [Milvus](https://milvus.io/) is an open-source vector database built for GenAI applications. It is built to be performant and scale to tens of billions of vectors with minimal performance loss. -Portkey provides a proxy to Milvus, allowing you to use virtual keys and observability features. +Portkey provides a proxy to Milvus - you can log your Milvus requests and manage auth for Qdrant clusters on Portkey. ## Portkey SDK Integration with Milvus @@ -28,9 +28,9 @@ Add the Portkey SDK to your application to interact with Milvus through Portkey' -### 2\. Initialize Portkey with a Virtual Key +### 2\. Create a New Integration -To use Milvus with Portkey, get your Milvus API key from here, then add it to Portkey to create your [Milvus virtual key](/product/ai-gateway/virtual-keys#using-virtual-keys). +To use Milvus with Portkey, get your Milvus API key and create a new Milvus integration on Portkey. @@ -40,7 +40,7 @@ import Portkey from 'portkey-ai' const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your Milvus Virtual Key + provider:"@PROVIDER" // Your Milvus provider slug }) ``` @@ -52,7 +52,7 @@ from portkey_ai import Portkey portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Milvus + provider="@PROVIDER" # Replace with your Milvus provider slug ) ``` @@ -104,7 +104,7 @@ from portkey_ai import Portkey portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="MILVUS_VIRTUAL_KEY", + provider="@MILVUS_PROVIDER", custom_host="MILVUS_HOST" # Replace with your Milvus host example: https://in03-34d7b37f7ee12c7.serverless.gcp-us-west1.cloud.zilliz.com ) @@ -122,7 +122,7 @@ print(response) // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "MILVUS_VIRTUAL_KEY", // Add your Milvus's virtual key + provider:"@MILVUS_PROVIDER", // Add your Milvus provider slug customHost="MILVUS_HOST" // Replace with your Milvus host example: https://in03-34d7b37f7ee12c7.serverless.gcp-us-west1.cloud.zilliz.com }); @@ -142,7 +142,7 @@ print(response) apiKey: 'MILVUS_API_KEY', // defaults to process.env["OPENAI_API_KEY"], baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ - virtualKey: "MILVUS_VIRTUAL_KEY", + provider:"@MILVUS_PROVIDER", apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] customHost: "MILVUS_HOST" // Replace with your Milvus host example: https://in03-34d7b37f7ee12c7.serverless.gcp-us-west1.cloud.zilliz.com }) @@ -181,7 +181,7 @@ print(response) ```sh curl --location --request POST 'https://api.portkey.ai/v1/v2/vectordb/collections/list' \ --header 'x-portkey-custom-host: https://in03-34d7b37f7de12c7.serverless.gcp-us-west1.cloud.zilliz.com' \ ---header 'x-portkey-virtual-key: MILVUS_VIRTUAL_KEY' \ +--header 'x-portkey-provider: MILVUS_PROVIDER' \ --header 'x-portkey-api-key: PORTKEY_API_KEY' ``` diff --git a/integrations/vector-databases/qdrant.mdx b/integrations/vector-databases/qdrant.mdx index bd9f0d6e..23454d4c 100644 --- a/integrations/vector-databases/qdrant.mdx +++ b/integrations/vector-databases/qdrant.mdx @@ -5,7 +5,7 @@ title: "Qdrant" [Qdrant](https://qdrant.tech/) is an open-source vector similarity search engine built for production-ready vector search applications. It provides a convenient API to store, search, and manage vectors with additional payload data. -Portkey provides a proxy to Qdrant, allowing you to use virtual keys and observability features. +Portkey provides a proxy to Qdrant - you can log your Qdrant requests and manage auth for Qdrant clusters on Portkey. ## Portkey SDK Integration with Qdrant @@ -28,9 +28,9 @@ Add the Portkey SDK to your application to interact with Qdrant through Portkey' -### 2\. Initialize Portkey with a Virtual Key +### 2\. Create a New Integration -To use Qdrant with Portkey, get your API key from [Qdrant App](https://cloud.qdrant.io/), then add it to Portkey to create your [Qdrant virtual key](/product/ai-gateway/virtual-keys#using-virtual-keys). +To use Qdrant with Portkey, get your API key from [Qdrant App](https://cloud.qdrant.io/), then add it to Portkey to create Qdrant integration. You will also need your Portkey API Key from [Portkey's Dashboard](https://app.portkey.ai). @@ -42,7 +42,7 @@ import Portkey from 'portkey-ai' const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Your Qdrant Virtual Key + provider:"@PROVIDER" // Your Qdrant provider slug }) ``` @@ -52,7 +52,7 @@ from portkey_ai import Portkey portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Qdrant + provider="@PROVIDER" # Replace with your provider slug for Qdrant ) ``` @@ -102,7 +102,7 @@ from portkey_ai import Portkey portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="QDRANT_VIRTUAL_KEY", + provider="@QDRANT_PROVIDER", custom_host="QDRANT_HOST" # Replace with your Qdrant host ) @@ -120,7 +120,7 @@ import Portkey from 'portkey-ai'; const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "QDRANT_VIRTUAL_KEY", // Add your Qdrant's virtual key + provider:"@QDRANT_PROVIDER", // Add your Qdrant provider slug customHost: "QDRANT_HOST" // Replace with your Qdrant host }); @@ -145,7 +145,7 @@ const portkey = new OpenAI({ apiKey: 'QDRANT_API_KEY', baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ - virtualKey: "QDRANT_VIRTUAL_KEY", + provider:"@QDRANT_PROVIDER", apiKey: "PORTKEY_API_KEY", customHost: "QDRANT_HOST" // Replace with your Qdrant host }) @@ -190,7 +190,7 @@ print(response) ```sh curl --location --request POST 'https://xxxx-xxx-xxx-xx-xxxxxx.us-west-2-0.aws.cloud.qdrant.io' \ --header 'x-portkey-custom-host: QDRANT_HOST' \ ---header 'x-portkey-virtual-key: QDRANT_VIRTUAL_KEY' \ +--header 'x-portkey-provider: QDRANT_PROVIDER' \ --header 'x-portkey-api-key: PORTKEY_API_KEY' ``` diff --git a/introduction/feature-overview.mdx b/introduction/feature-overview.mdx index 7006706f..8c475561 100644 --- a/introduction/feature-overview.mdx +++ b/introduction/feature-overview.mdx @@ -29,8 +29,8 @@ Connect to 250+ AI models using a single consistent API. Set up load balancers, Distribute workload efficiently across multiple models - - Manage access with virtual API keys + + Manage access to LLMs Set and manage request timeouts diff --git a/introduction/what-is-portkey.mdx b/introduction/what-is-portkey.mdx index af8c8276..ef7364a8 100644 --- a/introduction/what-is-portkey.mdx +++ b/introduction/what-is-portkey.mdx @@ -19,7 +19,7 @@ from portkey_ai import Portkey portkey = Portkey( api_key="YOUR_PORTKEY_API_KEY", - virtual_key="YOUR_VIRTUAL_KEY" + provider="@YOUR_PROVIDER" ) chat_complete = portkey.chat.completions.create( @@ -37,7 +37,7 @@ import Portkey from 'portkey-ai'; const portkey = new Portkey({ apiKey: "YOUR_PORTKEY_API_KEY", - virtualKey: "YOUR_VIRTUAL_KEY" + provider:"@YOUR_PROVIDER" }); async function createChatCompletion() { @@ -58,7 +58,7 @@ createChatCompletion(); curl https://api.portkey.ai/v1/chat/completions \ -H "Content-Type: application/json" \ -H "x-portkey-api-key: YOUR_PORTKEY_API_KEY" \ - -H "x-portkey-virtual-key: YOUR_VIRTUAL_KEY" \ + -H "x-portkey-provider: YOUR_PROVIDER" \ -d '{ "model": "gpt-3.5-turbo", "messages": [ diff --git a/product/ai-gateway.mdx b/product/ai-gateway.mdx index b94ac143..915efa20 100644 --- a/product/ai-gateway.mdx +++ b/product/ai-gateway.mdx @@ -47,10 +47,6 @@ description: The world's fastest AI Gateway with advanced routing & integrated G Canary test new models in production - - Manage AI provider keys and auth in a secure vault - - Easily handle unresponsive LLM requests diff --git a/product/ai-gateway/automatic-retries.mdx b/product/ai-gateway/automatic-retries.mdx index d16d29da..e6e1a044 100644 --- a/product/ai-gateway/automatic-retries.mdx +++ b/product/ai-gateway/automatic-retries.mdx @@ -23,7 +23,7 @@ To enable retry, just add the `retry` param to your [config object](/api-referen "retry": { "attempts": 5 }, - "virtual_key": "virtual-key-xxx" + "provider":"@virtual-key-xxx" } ``` @@ -40,7 +40,7 @@ You can change this behaviour by setting the optional `on_status_codes` param in "attempts": 3, "on_status_codes": [ 408, 429, 401 ] }, - "virtual_key": "virtual-key-xxx" + "provider":"@virtual-key-xxx" } ``` @@ -61,7 +61,7 @@ To enable this feature, add the `use_retry_after_headers` parameter to your retr "on_status_codes": [ 429 ], "use_retry_after_headers": true }, - "virtual_key": "virtual-key-xxx" + "provider":"@virtual-key-xxx" } ``` @@ -109,7 +109,7 @@ To enable retry, just add the `retry` param to your [config object](/api-referen "retry": { "attempts": 5 }, - "virtual_key": "virtual-key-xxx" + "provider":"@virtual-key-xxx" } ``` @@ -126,7 +126,7 @@ You can change this behaviour by setting the optional `on_status_codes` param in "attempts": 3, "on_status_codes": [ 408, 429, 401 ] }, - "virtual_key": "virtual-key-xxx" + "provider":"@virtual-key-xxx" } ``` diff --git a/product/ai-gateway/batches.mdx b/product/ai-gateway/batches.mdx index ba602eaa..6c126eb6 100644 --- a/product/ai-gateway/batches.mdx +++ b/product/ai-gateway/batches.mdx @@ -35,7 +35,7 @@ This is controlled by `completion_window` parameter in the request. Along with this, you have to set `portkey_options` which helps Portkey to batch requests to Provider's batch API or Gateway. - This is achieved by using provider specific headers in the portkey_options - - For example, if you want to use OpenAI's batch API, you can set `portkey_options` to `{"x-portkey-virtual-key": "openai-virtual_key"}` + - For example, if you want to use OpenAI's batch API, you can set `portkey_options` to `{"x-portkey-provider": "openai-virtual_key"}` ### 2.1 Batching requests with Provider's batch API using Portkey's file diff --git a/product/ai-gateway/cache-simple-and-semantic.mdx b/product/ai-gateway/cache-simple-and-semantic.mdx index 5d46c697..df8e85b8 100644 --- a/product/ai-gateway/cache-simple-and-semantic.mdx +++ b/product/ai-gateway/cache-simple-and-semantic.mdx @@ -141,7 +141,7 @@ You can enable cache force refresh with this header: curl https://api.portkey.ai/v1/chat/completions \ -H "Content-Type: application/json" \ -H "x-portkey-api-key: $PORTKEY_API_KEY" \ - -H "x-portkey-virtual-key: open-ai-xxx" \ + -H "x-portkey-provider: open-ai-xxx" \ -H "x-portkey-config: cache-config-xxx" \ -H "x-portkey-cache-force-refresh: true" \ -d '{ @@ -156,7 +156,7 @@ from portkey_ai import Portkey portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="open-ai-xxx", + provider="@open-ai-xxx", config="pp-cache-xxx" ) @@ -176,7 +176,7 @@ import Portkey from 'portkey-ai'; const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", config: "pc-cache-xxx", - virtualKey: "open-ai-xxx" + provider:"@open-ai-xxx" }) async function main(){ @@ -229,7 +229,7 @@ import Portkey from 'portkey-ai'; const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", config: "pc-cache-xxx", - virtualKey: "open-ai-xxx" + provider:"@open-ai-xxx" }) async function main(){ @@ -251,7 +251,7 @@ from portkey_ai import Portkey portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="open-ai-xxx", + provider="@open-ai-xxx", config="pp-cache-xxx" ) @@ -269,7 +269,7 @@ response = portkey.with_options( curl https://api.portkey.ai/v1/chat/completions \ -H "Content-Type: application/json" \ -H "x-portkey-api-key: $PORTKEY_API_KEY" \ - -H "x-portkey-virtual-key: open-ai-xxx" \ + -H "x-portkey-provider: open-ai-xxx" \ -H "x-portkey-config: cache-config-xxx" \ -H "x-portkey-cache-namespace: user-123" \ -d '{ @@ -318,8 +318,8 @@ You can set cache at two levels: "cache": {"mode": "semantic", "max_age": 60}, "strategy": {"mode": "fallback"}, "targets": [ - {"virtual_key": "openai-key-1"}, - {"virtual_key": "openai-key-2"} + {"provider":"@openai-key-1"}, + {"provider":"@openai-key-2"} ] } ``` @@ -331,11 +331,11 @@ You can set cache at two levels: "strategy": {"mode": "fallback"}, "targets": [ { - "virtual_key": "openai-key-1", + "provider":"@openai-key-1", "cache": {"mode": "simple", "max_age": 200} }, { - "virtual_key": "openai-key-2", + "provider":"@openai-key-2", "cache": {"mode": "semantic", "max_age": 100} } ] diff --git a/product/ai-gateway/canary-testing.mdx b/product/ai-gateway/canary-testing.mdx index 15b642e6..f31843cb 100644 --- a/product/ai-gateway/canary-testing.mdx +++ b/product/ai-gateway/canary-testing.mdx @@ -21,11 +21,11 @@ The config object would look like this }, "targets": [ { - "virtual_key": "openai-virtual-key", + "provider":"@openai-virtual-key", "weight": 0.95 }, { - "virtual_key": "anyscale-virtual-key", + "provider":"@anyscale-virtual-key", "weight": 0.05, "override_params": { "model": "meta-llama/Llama-2-70b-chat-hf" diff --git a/product/ai-gateway/conditional-routing.mdx b/product/ai-gateway/conditional-routing.mdx index 437c8448..6f2ea28e 100644 --- a/product/ai-gateway/conditional-routing.mdx +++ b/product/ai-gateway/conditional-routing.mdx @@ -47,11 +47,11 @@ Here's how a sample `conditional` config looks (along with its simpler, tree vie "targets": [ { "name": "target_1", - "virtual_key":"xx" + "provider:"@xx" }, { "name": "target_2", - "virtual_key":"yy" + "provider:"@yy" } ] } @@ -254,14 +254,14 @@ This example routes based on the `model` parameter, allowing you to use aliases "targets": [ { "name": "smartest-model-target", - "virtual_key": "anthropic-vk", + "provider":"@anthropic-vk", "override_params": { "model": "claude-3.5-sonnet" } }, { "name": "fastest-model-target", - "virtual_key": "oai-vk", + "provider":"@oai-vk", "override_params": { "model": "gpt-4o-mini" } @@ -344,14 +344,14 @@ Portkey support [metadata](/product/observability/metadata)-based routing, which "targets": [ { "name": "finetuned-gpt4", - "virtual_key":"xx", + "provider:"@xx", "override_params": { "model": "ft://gpt4-xxxxx" } }, { "name": "base-gpt4", - "virtual_key":"yy", + "provider:"@yy", "override_params": { "model": "gpt-4" } @@ -387,9 +387,9 @@ Portkey support [metadata](/product/observability/metadata)-based routing, which "default": "default_target" }, "targets": [ - { "name": "gpt4_v2_target", "virtual_key": "openai-xx"}, - { "name": "app_target", "virtual_key": "openai-yy" }, - { "name": "default_target", "virtual_key": "openai-zz" } + { "name": "gpt4_v2_target", "provider":"@openai-xx"}, + { "name": "app_target", "provider":"@openai-yy" }, + { "name": "default_target", "provider":"@openai-zz" } ] } ``` @@ -471,12 +471,12 @@ Config example for combined conditions: "targets": [ { "name": "creative-premium-target", - "virtual_key": "anthropic-vk", + "provider":"@anthropic-vk", "override_params": { "model": "claude-3-opus" } }, { "name": "standard-target", - "virtual_key": "openai-vk", + "provider":"@openai-vk", "override_params": { "model": "gpt-3.5-turbo" } } ] @@ -516,11 +516,11 @@ Route premium users to advanced models and free users to cost-effective ones. }, "targets": [ { - "name": "premium-model", "virtual_key": "openai-vk", + "name": "premium-model", "provider":"@openai-vk", "override_params": { "model": "gpt-4o" } }, { - "name": "basic-model", "virtual_key": "openai-vk", + "name": "basic-model", "provider":"@openai-vk", "override_params": { "model": "gpt-3.5-turbo" } } ] @@ -545,11 +545,11 @@ Ensure GDPR compliance by routing EU users to EU-compliant deployments. }, "targets": [ { - "name": "eu-compliant", "virtual_key": "azure-eu-vk", + "name": "eu-compliant", "provider":"@azure-eu-vk", "override_params": { "model": "gpt-4" } }, { - "name": "standard-model", "virtual_key": "openai-vk", + "name": "standard-model", "provider":"@openai-vk", "override_params": { "model": "gpt-4o" } } ] @@ -578,11 +578,11 @@ Route requests to different models based on data sensitivity levels. }, "targets": [ { - "name": "public-model", "virtual_key": "openai-vk" }, + "name": "public-model", "provider":"@openai-vk" }, { - "name": "on-premises-model", "virtual_key": "private-vk" }, + "name": "on-premises-model", "provider":"@private-vk" }, { - "name": "cloud-model", "virtual_key": "azure-vk" } + "name": "cloud-model", "provider":"@azure-vk" } ] } ``` @@ -613,15 +613,15 @@ Use intuitive aliases instead of remembering specific model names. }, "targets": [ { - "name": "smart-model", "virtual_key": "anthropic-vk", + "name": "smart-model", "provider":"@anthropic-vk", "override_params": { "model": "claude-3.5-sonnet" } }, { - "name": "fast-model", "virtual_key": "openai-vk", + "name": "fast-model", "provider":"@openai-vk", "override_params": { "model": "gpt-4o-mini" } }, { - "name": "balanced-model", "virtual_key": "openai-vk", + "name": "balanced-model", "provider":"@openai-vk", "override_params": { "model": "gpt-4o" } } ] @@ -650,15 +650,15 @@ Route creative vs factual requests to different optimized models. }, "targets": [ { - "name": "creative-model", "virtual_key": "anthropic-vk", + "name": "creative-model", "provider":"@anthropic-vk", "override_params": { "model": "claude-3-sonnet" } }, { - "name": "factual-model", "virtual_key": "openai-vk", + "name": "factual-model", "provider":"@openai-vk", "override_params": { "model": "gpt-4o" } }, { - "name": "balanced-model", "virtual_key": "openai-vk", + "name": "balanced-model", "provider":"@openai-vk", "override_params": { "model": "gpt-3.5-turbo" } } ] @@ -683,11 +683,11 @@ Route high-token requests to models with larger context windows. }, "targets": [ { - "name": "high-capacity", "virtual_key": "anthropic-vk", + "name": "high-capacity", "provider":"@anthropic-vk", "override_params": { "model": "claude-3-opus" } }, { - "name": "standard-capacity", "virtual_key": "openai-vk", + "name": "standard-capacity", "provider":"@openai-vk", "override_params": { "model": "gpt-4o-mini" } } ] @@ -720,15 +720,15 @@ Route different tasks to specialized models for optimal results. }, "targets": [ { - "name": "coding-model", "virtual_key": "openai-vk", + "name": "coding-model", "provider":"@openai-vk", "override_params": { "model": "gpt-4o" } }, { - "name": "writing-model", "virtual_key": "anthropic-vk", + "name": "writing-model", "provider":"@anthropic-vk", "override_params": { "model": "claude-3-opus" } }, { - "name": "general-model", "virtual_key": "openai-vk", + "name": "general-model", "provider":"@openai-vk", "override_params": { "model": "gpt-3.5-turbo" } } ] @@ -753,11 +753,11 @@ Test new models with specific user groups before full rollout. }, "targets": [ { - "name": "new-model", "virtual_key": "anthropic-vk", + "name": "new-model", "provider":"@anthropic-vk", "override_params": { "model": "claude-3.5-sonnet" } }, { - "name": "current-model", "virtual_key": "openai-vk", + "name": "current-model", "provider":"@openai-vk", "override_params": { "model": "gpt-3.5-turbo" } } ] @@ -782,11 +782,11 @@ Control feature rollout with feature flags in metadata. }, "targets": [ { - "name": "new-model-target", "virtual_key": "openai-vk", + "name": "new-model-target", "provider":"@openai-vk", "override_params": { "model": "gpt-4o" } }, { - "name": "standard-model", "virtual_key": "openai-vk", + "name": "standard-model", "provider":"@openai-vk", "override_params": { "model": "gpt-3.5-turbo" } } ] @@ -823,12 +823,12 @@ You can apply input guardrails to specific targets in your conditional routing c "targets": [ { "name": "premium-model", - "virtual_key": "openai-vk-xxx", + "provider":"@openai-vk-xxx", "input_guardrails": ["pg-pii-detector-xxx", "pg-toxic-content-xxx"] }, { "name": "standard-model", - "virtual_key": "openai-vk-yyy" + "provider":"@openai-vk-yyy" } ] } @@ -857,7 +857,7 @@ You can also apply output guardrails to validate or transform the LLM's response "targets": [ { "name": "creative-model", - "virtual_key": "anthropic-vk-xxx", + "provider":"@anthropic-vk-xxx", "output_guardrails": [ "pg-fact-checker-xxx", "pg-content-moderator-xxx" @@ -865,7 +865,7 @@ You can also apply output guardrails to validate or transform the LLM's response }, { "name": "standard-model", - "virtual_key": "openai-vk-xxx" + "provider":"@openai-vk-xxx" } ] } @@ -900,13 +900,13 @@ For comprehensive protection, you can apply both input and output guardrails to "targets": [ { "name": "premium-model", - "virtual_key": "openai-vk-xxx", + "provider":"@openai-vk-xxx", "input_guardrails": ["pg-pii-detector-xxx", "pg-toxic-content-xxx"], "output_guardrails": ["pg-fact-checker-xxx", "pg-compliance-xxx"] }, { "name": "standard-model", - "virtual_key": "openai-vk-yyy", + "provider":"@openai-vk-yyy", "input_guardrails": ["pg-basic-filter-xxx"], "output_guardrails": ["pg-basic-moderator-xxx"] } diff --git a/product/ai-gateway/fallbacks.mdx b/product/ai-gateway/fallbacks.mdx index 72fb9ab0..d07fc557 100644 --- a/product/ai-gateway/fallbacks.mdx +++ b/product/ai-gateway/fallbacks.mdx @@ -23,13 +23,13 @@ Here's a quick example of a config to **fallback** to Anthropic's `claude-3.5-so }, "targets": [ { - "virtual_key": "openai-virtual-key", + "provider":"@openai-virtual-key", "override_params": { "model": "gpt-4o" } }, { - "virtual_key": "anthropic-virtual-key", + "provider":"@anthropic-virtual-key", "override_params": { "model": "claude-3.5-sonnet-20240620" } @@ -57,10 +57,10 @@ You can change this behaviour by setting the optional `on_status_codes` param in }, "targets": [ { - "virtual_key": "openai-virtual-key" + "provider":"@openai-virtual-key" }, { - "virtual_key": "azure-openai-virtual-key" + "provider":"@azure-openai-virtual-key" } ] } diff --git a/product/ai-gateway/fine-tuning.mdx b/product/ai-gateway/fine-tuning.mdx index 02949900..7519313a 100644 --- a/product/ai-gateway/fine-tuning.mdx +++ b/product/ai-gateway/fine-tuning.mdx @@ -55,7 +55,7 @@ curl -X POST --header 'x-portkey-api-key: ' \ curl -X POST --header 'Content-Type: application/json' \ --header 'x-portkey-api-key: ' \ --data \ - $'{"model": "", "suffix": "", "training_file": "", "portkey_options": {"x-portkey-virtual-key": ""}, "method": {"type": "supervised", "supervised": {"hyperparameters": {"n_epochs": 1}}}}\n' \ + $'{"model": "", "suffix": "", "training_file": "", "portkey_options": {"x-portkey-provider": ""}, "method": {"type": "supervised", "supervised": {"hyperparameters": {"n_epochs": 1}}}}\n' \ 'https://api.portkey.ai/v1/fine_tuning/jobs' ``` diff --git a/product/ai-gateway/load-balancing.mdx b/product/ai-gateway/load-balancing.mdx index c6b3c6ef..e5173f83 100644 --- a/product/ai-gateway/load-balancing.mdx +++ b/product/ai-gateway/load-balancing.mdx @@ -22,11 +22,11 @@ Here's a quick example to **load balance 75-25** between an OpenAI and an Azure }, "targets": [ { - "virtual_key": "openai-virtual-key", + "provider":"@openai-virtual-key", "weight": 0.75 }, { - "virtual_key": "azure-virtual-key", + "provider":"@azure-virtual-key", "weight": 0.25 } ] @@ -37,7 +37,7 @@ Here's a quick example to **load balance 75-25** between an OpenAI and an Azure ## How Load Balancing Works -1. **Defining the Loadbalance Targets & their Weights**: You provide a list of `virtual keys` (or `provider` \+ `api_key` pairs), and assign a `weight` value to each target. The weights represent the relative share of requests that should be routed to each target. +1. **Defining the Loadbalance Targets & their Weights**: You provide a list of `providers`, and assign a `weight` value to each target. The weights represent the relative share of requests that should be routed to each target. 2. **Weight Normalization**: Portkey first sums up all the weights you provided for the targets. It then divides each target's weight by the total sum to calculate the normalized weight for that target. This ensures the weights add up to 1 (or 100%), allowing Portkey to distribute the load proportionally. For example, let's say you have three targets with weights 5, 3, and 1\. The total sum of weights is 9 (5 + 3 + 1). Portkey will then normalize the weights as follows: * Target 1: 5 / 9 = 0.55 (55% of the traffic) diff --git a/product/ai-gateway/multimodal-capabilities/function-calling.mdx b/product/ai-gateway/multimodal-capabilities/function-calling.mdx index a9cd1476..6ad08464 100644 --- a/product/ai-gateway/multimodal-capabilities/function-calling.mdx +++ b/product/ai-gateway/multimodal-capabilities/function-calling.mdx @@ -16,7 +16,7 @@ import Portkey from 'portkey-ai'; // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key + provider:"@PROVIDER" }); // Generate a chat completion with streaming @@ -64,7 +64,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key + provider="@PROVIDER" ) tools = [ diff --git a/product/ai-gateway/multimodal-capabilities/image-generation.mdx b/product/ai-gateway/multimodal-capabilities/image-generation.mdx index c6ea2286..6544468e 100644 --- a/product/ai-gateway/multimodal-capabilities/image-generation.mdx +++ b/product/ai-gateway/multimodal-capabilities/image-generation.mdx @@ -20,7 +20,7 @@ import Portkey from 'portkey-ai'; // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key + provider:"@PROVIDER" }); async function main() { @@ -44,7 +44,7 @@ from IPython.display import display, Image # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key + provider="@PROVIDER" ) image = portkey.images.generate( @@ -116,7 +116,7 @@ display(Image(url=image.data[0].url)) curl "https://api.portkey.ai/v1/images/generations" \ -H "Content-Type: application/json" \ -H "x-portkey-api-key: $PORTKEY_API_KEY" \ - -H "x-portkey-virtual-key: $OPENAI_VIRTUAL_KEY" \ + -H "x-portkey-provider: $OPENAI_PROVIDER" \ -d '{ "model": "dall-e-3", "prompt": "Lucy in the sky with diamonds" diff --git a/product/ai-gateway/multimodal-capabilities/speech-to-text.mdx b/product/ai-gateway/multimodal-capabilities/speech-to-text.mdx index cdc86c11..f342b7ae 100644 --- a/product/ai-gateway/multimodal-capabilities/speech-to-text.mdx +++ b/product/ai-gateway/multimodal-capabilities/speech-to-text.mdx @@ -18,12 +18,8 @@ import OpenAI from "openai"; import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' const openai = new OpenAI({ - apiKey: "dummy", // We are using Virtual Key from Portkey - baseURL: PORTKEY_GATEWAY_URL, - defaultHeaders: createHeaders({ - apiKey: "PORTKEY_API_KEY", - virtualKey: "OPENAI_VIRTUAL_KEY" - }) + apiKey: "PORTKEY_API_KEY", + baseURL: PORTKEY_GATEWAY_URL }); // Transcription @@ -31,7 +27,7 @@ const openai = new OpenAI({ async function transcribe() { const transcription = await openai.audio.transcriptions.create({ file: fs.createReadStream("/path/to/file.mp3"), - model: "whisper-1", + model: "@openai/whisper-1", }); console.log(transcription.text); @@ -43,7 +39,7 @@ transcribe(); async function translate() { const translation = await openai.audio.translations.create({ file: fs.createReadStream("/path/to/file.mp3"), - model: "whisper-1", + model: "@openai/whisper-1", }); console.log(translation.text); } @@ -57,12 +53,8 @@ from openai import OpenAI from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders client = OpenAI( - api_key="dummy" #We are using Virtual Key from Portkey - base_url=PORTKEY_GATEWAY_URL, - default_headers=createHeaders( - api_key="PORTKEY_API_KEY", - virtual_key="OPENAI_VIRTUAL_KEY" - ) + api_key="PORTKEY_API_KEY", + base_url=PORTKEY_GATEWAY_URL ) audio_file= open("/path/to/file.mp3", "rb") @@ -70,7 +62,7 @@ audio_file= open("/path/to/file.mp3", "rb") # Transcription transcription = client.audio.transcriptions.create( - model="whisper-1", + model="@openai/whisper-1", file=audio_file ) print(transcription.text) @@ -78,7 +70,7 @@ print(transcription.text) # Translation translation = client.audio.translations.create( - model="whisper-1", + model="@openai/whisper-1", file=audio_file ) print(translation.text) @@ -93,20 +85,20 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key + provider="@PROVIDER" ) audio_file= open("/path/to/file.mp3", "rb") # Transcription transcription = portkey.audio.transcriptions.create( - model="whisper-1", + model="@openai/whisper-1", file=audio_file ) print(transcription.text) # Translation translation = portkey.audio.translations.create( - model="whisper-1", + model="@openai/whisper-1", file=audio_file ) print(translation.text) @@ -118,21 +110,17 @@ For Transcriptions: ```sh curl "https://api.portkey.ai/v1/audio/transcriptions" \ -H "x-portkey-api-key: $PORTKEY_API_KEY" \ - -H "x-portkey-provider: openai" \ - -H "Authorization: Bearer $OPENAI_API_KEY" \ -H 'Content-Type: multipart/form-data' \ --form file=@/path/to/file/audio.mp3 \ - --form model=whisper-1 + --form model=@openai/whisper-1 ``` For Translations: ```sh curl "https://api.portkey.ai/v1/audio/translations" \ -H "x-portkey-api-key: $PORTKEY_API_KEY" \ - -H "x-portkey-provider: openai" \ - -H "Authorization: Bearer $OPENAI_API_KEY" \ -H 'Content-Type: multipart/form-data' \ --form file=@/path/to/file/audio.mp3 \ - --form model=whisper-1 + --form model=@openai/whisper-1 ``` diff --git a/product/ai-gateway/multimodal-capabilities/text-to-speech.mdx b/product/ai-gateway/multimodal-capabilities/text-to-speech.mdx index 18daa51c..e75cf382 100644 --- a/product/ai-gateway/multimodal-capabilities/text-to-speech.mdx +++ b/product/ai-gateway/multimodal-capabilities/text-to-speech.mdx @@ -16,22 +16,18 @@ Here's an example: import fs from "fs"; import path from "path"; import OpenAI from "openai"; -import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' +import { PORTKEY_GATEWAY_URL } from 'portkey-ai' const openai = new OpenAI({ - apiKey: "dummy", // We are using Virtual Key from Portkey - baseURL: PORTKEY_GATEWAY_URL, - defaultHeaders: createHeaders({ - apiKey: "PORTKEY_API_KEY", - virtualKey: "OPENAI_VIRTUAL_KEY" - }) + apiKey: "PORTKEY_API_KEY", + baseURL: PORTKEY_GATEWAY_URL }); const speechFile = path.resolve("./speech.mp3"); async function main() { const mp3 = await openai.audio.speech.create({ - model: "tts-1", + model: "@openai/tts-1", voice: "alloy", input: "Today is a wonderful day to build something people love!", }); @@ -47,21 +43,17 @@ main(); ```py from pathlib import Path from openai import OpenAI -from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders +from portkey_ai import PORTKEY_GATEWAY_URL client = OpenAI( - api_key="dummy" #We are using Virtual Key from Portkey - base_url=PORTKEY_GATEWAY_URL, - default_headers=createHeaders( - api_key="PORTKEY_API_KEY", - virtual_key="OPENAI_VIRTUAL_KEY" - ) + api_key="PORTKEY_API_KEY", + base_url=PORTKEY_GATEWAY_URL ) speech_file_path = Path(__file__).parent / "speech.mp3" response = client.audio.speech.create( - model="tts-1", + model="@openai/tts-1", voice="alloy", input="Today is a wonderful day to build something people love!" ) @@ -82,13 +74,13 @@ from portkey_ai import Portkey portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key + provider="@PROVIDER" ) speech_file_path = Path(__file__).parent / "speech.mp3" response = portkey.audio.speech.create( - model="tts-1", + model="@openai/tts-1", voice="alloy", input="Today is a wonderful day to build something people love!" ) @@ -104,10 +96,8 @@ f.close() curl "https://api.portkey.ai/v1/audio/speech" \ -H "Content-Type: application/json" \ -H "x-portkey-api-key: $PORTKEY_API_KEY" \ - -H "x-portkey-provider: openai" \ - -H "Authorization: Bearer $OPENAI_API_KEY" \ -d '{ - "model": "tts-1", + "model": "@openai/tts-1", "input": "Today is a wonderful day to build something people love!", "voice": "alloy" }' \ @@ -117,14 +107,3 @@ curl "https://api.portkey.ai/v1/audio/speech" \ On completion, the request will get logged in the logs UI and show the cost and latency incurred. - -## Supported Providers and Models - -The following providers are supported for text-to-speech with more providers getting added soon. Please raise a [request](/integrations/llms/suggest-a-new-integration) or a [PR](https://github.com/Portkey-AI/gateway/pulls) to add model or provider to the AI gateway. - -| Provider | Models | -| ---------------------------------------- | -------------- | -| [OpenAI](/integrations/llms/openai) | tts-1 tts-1-hd | -| [Azure OpenAI](/integrations/llms/azure-openai) | tts-1 tts-1-hd | -| Deepgram (Coming Soon) | | -| ElevanLabs (Coming Soon) | | diff --git a/product/ai-gateway/multimodal-capabilities/thinking-mode.mdx b/product/ai-gateway/multimodal-capabilities/thinking-mode.mdx index afc843c3..a9f6b86b 100644 --- a/product/ai-gateway/multimodal-capabilities/thinking-mode.mdx +++ b/product/ai-gateway/multimodal-capabilities/thinking-mode.mdx @@ -39,7 +39,7 @@ Extended thinking API through Portkey is currently in beta. # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + provider="@PROVIDER", strict_open_ai_compliance=False # Required for thinking mode ) @@ -85,7 +85,7 @@ Extended thinking API through Portkey is currently in beta. // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY", // Add your provider's virtual key + provider:"@PROVIDER", strictOpenAiCompliance: false // Required for thinking mode }); @@ -255,7 +255,7 @@ For multi-turn conversations, include the previous thinking content in the conve # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + provider="@PROVIDER", strict_open_ai_compliance=False ) @@ -301,15 +301,15 @@ For multi-turn conversations, include the previous thinking content in the conve // Initialize the Portkey client const portkey = new Portkey({ - apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY", // Add your anthropic's virtual key + apiKey: "PORTKEY_API_KEY", + provider:"@PROVIDER", strictOpenAiCompliance: false }); // Generate a chat completion async function getChatCompletionFunctions() { const response = await portkey.chat.completions.create({ - model: "claude-3-7-sonnet-latest", + model: "@anthropic/claude-3-7-sonnet-latest", max_tokens: 3000, thinking: { type: "enabled", @@ -364,7 +364,7 @@ For multi-turn conversations, include the previous thinking content in the conve // Generate a chat completion with streaming async function getChatCompletionFunctions(){ const response = await openai.chat.completions.create({ - model: "claude-3-7-sonnet-latest", + model: "@anthropic/claude-3-7-sonnet-latest", max_tokens: 3000, thinking: { type: "enabled", @@ -408,18 +408,16 @@ For multi-turn conversations, include the previous thinking content in the conve from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders openai = OpenAI( - api_key='Anthropic_API_KEY', + api_key='PORTKEY_API_KEY', base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - provider="anthropic", - api_key="PORTKEY_API_KEY", strict_open_ai_compliance=False ) ) response = openai.chat.completions.create( - model="claude-3-7-sonnet-latest", + model="@anthropic/claude-3-7-sonnet-latest", max_tokens=3000, thinking={ "type": "enabled", @@ -459,11 +457,9 @@ For multi-turn conversations, include the previous thinking content in the conve curl "https://api.portkey.ai/v1/chat/completions" \ -H "Content-Type: application/json" \ -H "x-portkey-api-key: $PORTKEY_API_KEY" \ - -H "x-portkey-provider: anthropic" \ - -H "x-api-key: $ANTHROPIC_API_KEY" \ -H "x-portkey-strict-open-ai-compliance: false" \ -d '{ - "model": "claude-3-7-sonnet-latest", + "model": "@anthropic/claude-3-7-sonnet-latest", "max_tokens": 3000, "thinking": { "type": "enabled", diff --git a/product/ai-gateway/multimodal-capabilities/vision.mdx b/product/ai-gateway/multimodal-capabilities/vision.mdx index 4a6a45c7..e081e5b7 100644 --- a/product/ai-gateway/multimodal-capabilities/vision.mdx +++ b/product/ai-gateway/multimodal-capabilities/vision.mdx @@ -24,7 +24,7 @@ import Portkey from 'portkey-ai'; // Initialize the Portkey client const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key - virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key + provider:"@PROVIDER" }); // Generate a chat completion with streaming @@ -59,7 +59,7 @@ from portkey_ai import Portkey # Initialize the Portkey client portkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY" # Add your provider's virtual key + provider="@PROVIDER" ) response = client.chat.completions.create( diff --git a/product/ai-gateway/realtime-api.mdx b/product/ai-gateway/realtime-api.mdx index 9889dff1..885a217d 100644 --- a/product/ai-gateway/realtime-api.mdx +++ b/product/ai-gateway/realtime-api.mdx @@ -22,7 +22,7 @@ import asyncio async def main(): client = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="VIRTUAL_KEY", + provider="@PROVIDER", base_url=PORTKEY_GATEWAY_URL, ) @@ -66,7 +66,7 @@ import { createHeaders, PORTKEY_GATEWAY_URL } from 'portkey-ai'; const headers = createHeaders({ provider: "openai", apiKey: "PORTKEY_API_KEY", - virtualKey: 'VIRTUAL_KEY' + provider:'@PROVIDER' }) const openai = new OpenAI({ baseURL: PORTKEY_GATEWAY_URL, @@ -123,7 +123,7 @@ from openai import AsyncOpenAI from portkey_ai import createHeaders, PORTKEY_GATEWAY_URL async def main(): - headers = createHeaders(provider="openai", api_key="PORTKEY_API_KEY", virtual_key="VIRTUAL_KEY") + headers = createHeaders(provider="openai", api_key="PORTKEY_API_KEY", provider="@PROVIDER") client = AsyncOpenAI( base_url=PORTKEY_GATEWAY_URL, ) @@ -158,7 +158,7 @@ asyncio.run(main()) # we're using websocat for this example, but you can use any websocket client websocat "wss://api.portkey.ai/v1/realtime?model=gpt-4o-realtime-preview-2024-10-01" \ -H "x-portkey-provider: openai" \ - -H "x-portkey-virtual-key: VIRTUAL_KEY" \ + -H "x-portkey-provider: PROVIDER" \ -H "x-portkey-OpenAI-Beta: realtime=v1" \ -H "x-portkey-api-key: PORTKEY_API_KEY" # once connected, you can send your messages as you would with OpenAI's Realtime API diff --git a/product/ai-gateway/remote-mcp.mdx b/product/ai-gateway/remote-mcp.mdx index 928691ac..88ea9582 100644 --- a/product/ai-gateway/remote-mcp.mdx +++ b/product/ai-gateway/remote-mcp.mdx @@ -21,9 +21,8 @@ A Responses API request to OpenAI with MCP tools enabled. curl https://api.portkey.ai/v1/responses \ -H "Content-Type: application/json" \ -H "x-portkey-api-key: $PORTKEY_API_KEY" \ - -H "x-portkey-virtual-key: $OPENAI_VIRTUAL_KEY" \ -d '{ - "model": "gpt-4.1", + "model": "@OPENAI_PROVIDER/gpt-4.1", "tools": [ { "type": "mcp", @@ -41,16 +40,12 @@ import OpenAI from "openai"; import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' const client = new OpenAI({ - apiKey: "xx", // Can be left blank when using virtual keys - baseURL: PORTKEY_GATEWAY_URL, - defaultHeaders: createHeaders({ - apiKey: "PORTKEY_API_KEY", - virtualKey: "OPENAI_VIRTUAL_KEY" - }) + apiKey: "PORTKEY_API_KEY", + baseURL: PORTKEY_GATEWAY_URL }); const resp = await client.responses.create({ - model: "gpt-4.1", + model: "@OPENAI_PROVIDER/gpt-4.1", tools: [ { type: "mcp", @@ -70,16 +65,12 @@ from openai import OpenAI from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders client = OpenAI( - api_key="xx", # Can be left blank when using virtual keys + api_key="PORTKEY_API_KEY", base_url=PORTKEY_GATEWAY_URL, - default_headers=createHeaders( - api_key="PORTKEY_API_KEY", - virtual_key="OPENAI_VIRTUAL_KEY" - ) ) resp = client.responses.create( - model="gpt-4.1", + model="@OPENAI_PROVIDER/gpt-4.1", tools=[ { "type": "mcp", @@ -98,12 +89,11 @@ print(resp.output_text) import Portkey from 'portkey-ai'; const portkey = new Portkey({ - apiKey: "PORTKEY_API_KEY", - virtualKey: "OPENAI_VIRTUAL_KEY" + apiKey: "PORTKEY_API_KEY" }); const resp = await portkey.responses.create({ - model: "gpt-4.1", + model: "@OPENAI_PROVIDER/gpt-4.1", tools: [ { type: "mcp", @@ -122,12 +112,11 @@ console.log(resp.output_text); from portkey_ai import Portkey portkey = Portkey( - api_key="PORTKEY_API_KEY", - virtual_key="OPENAI_VIRTUAL_KEY" + api_key="PORTKEY_API_KEY" ) resp = portkey.responses.create( - model="gpt-4.1", + model="@OPENAI_PROVIDER/gpt-4.1", tools=[ { "type": "mcp", @@ -163,9 +152,8 @@ Use Stripe MCP tool curl https://api.portkey.ai/v1/responses \ -H "Content-Type: application/json" \ -H "x-portkey-api-key: $PORTKEY_API_KEY" \ - -H "x-portkey-virtual-key: $OPENAI_VIRTUAL_KEY" \ -d '{ - "model": "gpt-4.1", + "model": "@OPENAI_PROVIDER/gpt-4.1", "input": "Create a payment link for $20", "tools": [ { @@ -182,19 +170,15 @@ curl https://api.portkey.ai/v1/responses \ ```javascript OpenAI Node SDK import OpenAI from "openai"; -import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' +import { PORTKEY_GATEWAY_URL } from 'portkey-ai' const client = new OpenAI({ - apiKey: "xx", // Can be left blank when using virtual keys - baseURL: PORTKEY_GATEWAY_URL, - defaultHeaders: createHeaders({ - apiKey: "PORTKEY_API_KEY", - virtualKey: "OPENAI_VIRTUAL_KEY" - }) + apiKey: "PORTKEY_API_KEY", + baseURL: PORTKEY_GATEWAY_URL }); const resp = await client.responses.create({ - model: "gpt-4.1", + model: "@OPENAI_PROVIDER/gpt-4.1", input: "Create a payment link for $20", tools: [ { @@ -213,19 +197,15 @@ console.log(resp.output_text); ```python OpenAI Python SDK from openai import OpenAI -from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders +from portkey_ai import PORTKEY_GATEWAY_URL client = OpenAI( - api_key="xx", # Can be left blank when using virtual keys - base_url=PORTKEY_GATEWAY_URL, - default_headers=createHeaders( - api_key="PORTKEY_API_KEY", - virtual_key="OPENAI_VIRTUAL_KEY" - ) + api_key="PORTKEY_API_KEY", + base_url=PORTKEY_GATEWAY_URL ) resp = client.responses.create( - model="gpt-4.1", + model="@OPENAI_PROVIDER/gpt-4.1", input="Create a payment link for $20", tools=[ { @@ -246,12 +226,11 @@ print(resp.output_text) import Portkey from 'portkey-ai'; const portkey = new Portkey({ - apiKey: "PORTKEY_API_KEY", - virtualKey: "OPENAI_VIRTUAL_KEY" + apiKey: "PORTKEY_API_KEY" }); const resp = await portkey.responses.create({ - model: "gpt-4.1", + model: "@OPENAI_PROVIDER/gpt-4.1", input: "Create a payment link for $20", tools: [ { @@ -272,12 +251,11 @@ console.log(resp.output_text); from portkey_ai import Portkey portkey = Portkey( - api_key="PORTKEY_API_KEY", - virtual_key="OPENAI_VIRTUAL_KEY" + api_key="PORTKEY_API_KEY" ) resp = portkey.responses.create( - model="gpt-4.1", + model="@OPENAI_PROVIDER/gpt-4.1", input="Create a payment link for $20", tools=[ { diff --git a/product/ai-gateway/request-timeouts.mdx b/product/ai-gateway/request-timeouts.mdx index 013393c5..282a9687 100644 --- a/product/ai-gateway/request-timeouts.mdx +++ b/product/ai-gateway/request-timeouts.mdx @@ -26,16 +26,14 @@ Set request timeout while instantiating your Portkey client or if you're using t ```js import Portkey from 'portkey-ai'; -// Construct a client with a virtual key const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", - virtualKey: "VIRTUAL_KEY", requestTimeout: 3000 }) const chatCompletion = await portkey.chat.completions.create({ messages: [{ role: 'user', content: 'Say this is a test' }], - model: 'gpt-4o-mini', + model: '@openai/gpt-4o-mini', }); console.log(chatCompletion.choices); @@ -46,16 +44,14 @@ console.log(chatCompletion.choices); ```py from portkey_ai import Portkey -# Construct a client with a virtual key portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="VIRTUAL_KEY", request_timeout=3000 ) completion = portkey.chat.completions.create( messages = [{ "role": 'user', "content": 'Say this is a test' }], - model = 'gpt-4o-mini' + model = '@openai/gpt-4o-mini' ) ``` @@ -65,10 +61,9 @@ completion = portkey.chat.completions.create( curl "https://api.portkey.ai/v1/chat/completions" \ -H "Content-Type: application/json" \ -H "x-portkey-api-key: $PORTKEY_API_KEY" \ - -H "x-portkey-virtual-key: openai-virtual-key" \ -H "x-portkey-request-timeout:5000" \ -d '{ - "model": "gpt-4o-mini", + "model": "@openai/gpt-4o-mini", "messages": [{"role": "user", "content": "Hello!"}] }' ``` @@ -93,8 +88,8 @@ For a 10-second timeout, it will be: "strategy": { "mode": "fallback" }, "request_timeout": 10000, "targets": [ - { "virtual_key": "open-ai-xxx" }, - { "virtual_key": "azure-open-ai-xxx" } + { "provider":"@openai" }, + { "provider":"@azure-openai" } ] } ``` @@ -107,8 +102,8 @@ Here, the request timeout of 10 seconds will be applied to \* **all**\* the targ { "strategy": { "mode": "fallback" }, "targets": [ - { "virtual_key": "open-ai-xxx", "request_timeout": 10000, }, - { "virtual_key": "azure-open-ai-xxx", "request_timeout": 2000,} + { "provider":"@openai", "request_timeout": 10000, }, + { "provider":"@azure-openai", "request_timeout": 2000,} ] } ``` @@ -132,17 +127,17 @@ Nested target objects inherit the top-level timeout, with the option to override "request_timeout": 5000, "targets": [ { - "virtual_key":"open-ai-1-1" + "provider:"@openai" }, { - "virtual_key": "open-ai-1-2", + "provider":"@azure-openai", "request_timeout": 10000 } ], "weight": 1 }, { - "virtual_key": "azure-open-ai-1", + "provider":"@azure-openai", "weight": 1 } ] @@ -151,9 +146,9 @@ Nested target objects inherit the top-level timeout, with the option to override 1. We've set a global timeout of **2s** at line #3 2. The first target has a nested fallback strategy, with a top level request timeout of **5s** at line #7 -3. The first virtual key (at line #10), the **target-level** timeout of **5s** will be applied -4. For the second virtual key (i.e. `open-ai-1-2`), there is a timeout override, set at **10s**, which will be applied only to this target -5. For the last target (i.e. virtual key `azure-open-ai-1`), the top strategy-level timeout of **2s** will be applied +3. The first provider (at line #10), the **target-level** timeout of **5s** will be applied +4. For the second provider (i.e. `azure-openai`), there is a timeout override, set at **10s**, which will be applied only to this target +5. For the last provider (i.e. provider `azure-openai`), the top strategy-level timeout of **2s** will be applied ## Handling Request Timeouts @@ -168,8 +163,8 @@ Portkey issues a standard **408 error** for timed-out requests. You can leverage "on_status_codes": [408] }, "targets": [ - { "virtual_key": "open-ai-xxx", "request_timeout": 2000, }, - { "virtual_key": "azure-open-ai-xxx"} + { "provider":"@openai", "request_timeout": 2000, }, + { "provider":"@azure-openai"} ] } ``` @@ -182,7 +177,7 @@ Here, fallback from OpenAI to Azure OpenAI will only be triggered if the first r { "request_timeout": 1000, "retry": { "attempts": 3, "on_status_codes": [ 408 ] }, - "virtual_key": "open-ai-xxx" + "provider":"@openai" } ``` diff --git a/product/ai-gateway/strict-open-ai-compliance.mdx b/product/ai-gateway/strict-open-ai-compliance.mdx index 682495ee..65bcf933 100644 --- a/product/ai-gateway/strict-open-ai-compliance.mdx +++ b/product/ai-gateway/strict-open-ai-compliance.mdx @@ -21,8 +21,8 @@ To get those fields in the response, you can do one of the following: from portkey_ai import Portkey portkey = Portkey( - api_key="PORTKEY_API_KEY", # Replace with your Portkey API key - virtual_key="VIRTUAL_KEY", # Replace with your virtual key + api_key="PORTKEY_API_KEY", + provider="@PROVIDER", strict_open_ai_compliance=False ) ``` @@ -32,8 +32,8 @@ To get those fields in the response, you can do one of the following: import Portkey from 'portkey-ai' const portkey = new Portkey({ - apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY", // Your Virtual Key + apiKey: "PORTKEY_API_KEY", + provider:"@PROVIDER", strictOpenAiCompliance: false }) ``` diff --git a/product/ai-gateway/virtual-keys.mdx b/product/ai-gateway/virtual-keys.mdx index 4f3ab853..9c9e4c95 100644 --- a/product/ai-gateway/virtual-keys.mdx +++ b/product/ai-gateway/virtual-keys.mdx @@ -71,7 +71,7 @@ import Portkey from 'portkey-ai' const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Portkey supports a vault for your LLM Keys + provider:"@PROVIDER" // Portkey supports a vault for your LLM Keys }) ``` @@ -81,7 +81,7 @@ const portkey = new Portkey({ # Construct a client with a virtual key portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="VIRTUAL_KEY" + provider="@PROVIDER" ) ``` @@ -96,13 +96,13 @@ Alternatively, you can override the virtual key during the completions call as f const chatCompletion = await portkey.chat.completions.create({ messages: [{ role: 'user', content: 'Say this is a test' }], model: 'gpt-3.5-turbo', -}, {virtualKey: "OVERRIDING_VIRTUAL_KEY"}); +}, {provider:"@OVERRIDING_PROVIDER"}); ``` ```py -completion = portkey.with_options(virtual_key="...").chat.completions.create( +completion = portkey.with_options(provider="@...").chat.completions.create( messages = [{ "role": 'user', "content": 'Say this is a test' }], model = 'gpt-3.5-turbo' ) @@ -126,7 +126,7 @@ const openai = new OpenAI({ baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] - virtualKey: "VIRTUAL_KEY" // Portkey supports a vault for your LLM Keys + provider:"@PROVIDER" // Portkey supports a vault for your LLM Keys }) }); ``` @@ -143,7 +143,7 @@ client = OpenAI( base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( api_key="PORTKEY_API_KEY" # defaults to os.environ.get("PORTKEY_API_KEY") - virtual_key="VIRTUAL_KEY" # Portkey supports a vault for your LLM Keys + provider="@PROVIDER" # Portkey supports a vault for your LLM Keys ) ) ``` @@ -159,13 +159,13 @@ Alternatively, you can override the virtual key during the completions call as f const chatCompletion = await portkey.chat.completions.create({ messages: [{ role: 'user', content: 'Say this is a test' }], model: 'gpt-3.5-turbo', -}, {virtualKey: "OVERRIDING_VIRTUAL_KEY"}); +}, {provider:"@OVERRIDING_PROVIDER"}); ``` ```py -completion = portkey.with_options(virtual_key="...").chat.completions.create( +completion = portkey.with_options(provider="@...").chat.completions.create( messages = [{ "role": 'user', "content": 'Say this is a test' }], model = 'gpt-3.5-turbo' ) @@ -179,7 +179,7 @@ completion = portkey.with_options(virtual_key="...").chat.completions.create( const chatCompletion = await portkey.chat.completions.create({ messages: [{ role: 'user', content: 'Say this is a test' }], model: 'gpt-4o', // This will be the alias of the deployment -}, {virtualKey: "VIRTUAL_KEY"}); +}, {provider:"@PROVIDER"}); ``` @@ -216,5 +216,5 @@ Set the virtual key when utilizing Portkey's custom LLM as shown below: ```py # Example in Langchain -llm = PortkeyLLM(api_key="PORTKEY_API_KEY",virtual_key="VIRTUAL_KEY") +llm = PortkeyLLM(api_key="PORTKEY_API_KEY",provider="@PROVIDER") ``` diff --git a/product/ai-gateway/virtual-keys/bedrock-amazon-assumed-role.mdx b/product/ai-gateway/virtual-keys/bedrock-amazon-assumed-role.mdx index b9f19803..13df48c9 100644 --- a/product/ai-gateway/virtual-keys/bedrock-amazon-assumed-role.mdx +++ b/product/ai-gateway/virtual-keys/bedrock-amazon-assumed-role.mdx @@ -1,6 +1,6 @@ --- title: "Connect Bedrock with Amazon Assumed Role" -description: "How to create a virtual key for Bedrock using Amazon Assumed Role Authentication" +description: "How to create a new integration for Bedrock using Amazon Assumed Role Authentication" --- Available on all plans. @@ -13,7 +13,7 @@ Available on all plans. ## Select AWS Assumed Role Authentication -Create a new virtual key on Portkey, select **Bedrock** as the provider and **AWS Assumed Role** as the authentication method. +Create a new integration on Portkey, select **Bedrock** as the provider and **AWS Assumed Role** as the authentication method. @@ -21,7 +21,7 @@ Create a new virtual key on Portkey, select **Bedrock** as the provider and **AW ## Create an AWS Role for Portkey to Assume -This role you create will be used by Porktey to execute InvokeModel commands on Bedrock models in your AWS account. The setup process will establish a minimal-permission ("least privilege") role and set it up to allow Porktey to assume this role. +This role you create will be used by Portkey to execute InvokeModel commands on Bedrock models in your AWS account. The setup process will establish a minimal-permission ("least privilege") role and set it up to allow Portkey to assume this role. ### Create a permission policy in your AWS account using the following JSON @@ -116,11 +116,11 @@ If you set an external ID, add it to the condition as shown below. -## Configure the virtual key with the role ARN +## Configure the integration with the role ARN Once the role is created, copy the role ARN and paste it into the Bedrock integrations modal in Portkey along with the external ID if you set one and the AWS region you are using. - You're all set! You can now use the virtual key to invoke Bedrock models. + You're all set! You can now use the providers inherited from your integration to invoke Bedrock models. diff --git a/product/ai-gateway/virtual-keys/budget-limits.mdx b/product/ai-gateway/virtual-keys/budget-limits.mdx index 459913e7..286cb07e 100644 --- a/product/ai-gateway/virtual-keys/budget-limits.mdx +++ b/product/ai-gateway/virtual-keys/budget-limits.mdx @@ -1,21 +1,21 @@ --- title: "Budget Limits" -description: "Budget Limits lets you set cost limits on virtual keys" +description: "Budget Limits lets you set cost limits on providers/integrations" --- -Budget Limits lets you set cost or token limits on virtual keys +Budget Limits lets you set cost or token limits on providers/integrations Available on **Enterprise** plan and select **Pro** customers. -**Budget Limits on Virtual Keys** provide a simple way to manage your spending on AI providers (and LLMs) - giving you confidence and control over your application's costs. +**Budget Limits on Providers** provide a simple way to manage your spending on AI providers (and LLMs) - giving you confidence and control over your application's costs. Budget Limit is currently only available to **Portkey** [**Enterprise Plan**](https://portkey.ai/docs/product/enterprise-offering) customers. Email us at `support@portkey.ai` if you would like to enable it for your org. -## Setting Budget Limits on Virtual Keys +## Setting Budget Limits on Providers -When creating a new virtual key on Portkey, you can set limits in two ways: +When creating a new integration on Portkey, you can set limits in two ways: ### Cost-Based Limits Set a budget limit in USD that, once reached, will automatically expire the key to prevent further usage and overspending. @@ -23,9 +23,7 @@ Set a budget limit in USD that, once reached, will automatically expire the key ### Token-Based Limits Set a maximum number of tokens that can be consumed, allowing you to control usage independent of cost fluctuations. - - - +And you can set both these limtis for each workspace. > #### Key Considerations > @@ -35,7 +33,7 @@ Set a maximum number of tokens that can be consumed, allowing you to control usa > * Budget limits apply until exhausted or reset > * Budget limits are applied only to requests made after the limit is set; they do not apply retroactively > * Once set, budget limits **cannot be edited** by any organization member -> * Budget limits work for **all providers** available on Portkey and apply to **all organization members** who use the virtual key +> * Budget limit feature is available for **all LLMs** available on Portkey and will apply to **all members/workspaces** who have permission to use it. ## Alert Thresholds @@ -65,15 +63,15 @@ You can configure budget limits to automatically reset at regular intervals: ## Editing Budget Limits -If you need to change or update a budget limit, you can **duplicate** the existing virtual key and create a new one with the desired limit. +If you need to change or update a budget limit, you can **duplicate** the existing provider and create a new one with the desired limit. ## Monitoring Your Spending and Usage -You can track your spending and token usage for any specific virtual key by navigating to the Analytics tab and filtering by the **desired key** and **timeframe**. +You can track your spending and token usage for any specific provider by navigating to the Analytics tab and filtering by the **desired provider** and **timeframe**. ## Pricing Support and Limitations -Budget limits currently apply to all providers and models for which Portkey has pricing support. If a specific request log shows `0 cents` in the COST column, it means that Portkey does not currently track pricing for that model, and it will not count towards the virtual key's budget limit. +Budget limits currently apply to all providers and models for which Portkey has pricing support. If a specific request log shows `0 cents` in the COST column, it means that Portkey does not currently track pricing for that model, and it will not count towards the providers's budget limit. For token-based budgets, Portkey tracks both input and output tokens across all supported models. diff --git a/product/ai-gateway/virtual-keys/rate-limits.mdx b/product/ai-gateway/virtual-keys/rate-limits.mdx index 1e5b6083..07f202e7 100644 --- a/product/ai-gateway/virtual-keys/rate-limits.mdx +++ b/product/ai-gateway/virtual-keys/rate-limits.mdx @@ -1,21 +1,21 @@ --- title: "Rate Limits" -description: "Set Rate Limts to your virtual keys" +description: "Set Rate Limts to your Integrations/Providers" --- -Rate Limits lets you set request or token consumption limits on virtual keys +Rate Limits lets you set request or token consumption limits on providers/integrations Available on **Enterprise** plan and select **Pro** customers. -**Rate Limits on Virtual Keys** provide a powerful way to control the frequency of API requests or token consumption for your AI providers - giving you confidence and control over your application's usage patterns and performance. +**Rate Limits provide a powerful way to control the frequency of API requests or token consumption for your requests - giving you confidence and control over your application's usage patterns and performance. Rate Limit is currently only available to **Portkey** [**Enterprise Plan**](https://portkey.ai/docs/product/enterprise-offering) customers and select Pro users. Email us at `support@portkey.ai` if you would like to enable it for your org. -## Setting Rate Limits on Virtual Keys +## Setting Rate Limits on Providers -When creating a new virtual key on Portkey, you can set rate limits in two ways: +When creating a new provider on Portkey, you can set rate limits in two ways: ### Request-Based Limits Set a maximum number of requests that can be made within a specified time period (per minute, hour, or day). @@ -31,10 +31,10 @@ Set a maximum number of tokens that can be consumed within a specified time peri > > * Rate limits can be set as either request-based or token-based > * Time intervals can be configured as per minute, per hour, or per day -> * Setting the limit to 0 disables the virtual key +> * Setting the limit to 0 disables the provider > * Rate limits apply immediately after being set -> * Once set, budget limits **cannot be edited** by any organization member -> * Rate limits work for **all providers** available on Portkey and apply to **all organization members** who use the virtual key +> * Once set, rate limits **cannot be edited** by any organization member +> * Rate limits work for **all providers** available on Portkey and apply to **all organization members** who use the provider > * After a rate limit is reached, requests will be rejected until the time period resets ## Rate Limit Intervals @@ -54,11 +54,11 @@ When a rate limit is reached: ## Editing Rate Limits -If you need to change or update a rate limit, you can **duplicate** the existing virtual key and create a new one with the desired limit. +If you need to change or update a rate limit, you can **duplicate** the existing provider and create a new one with the desired limit. ## Monitoring Your Usage -You can track your request and token usage for any specific virtual key by navigating to the Analytics tab and filtering by the **desired key** and **timeframe**. +You can track your request and token usage for any specific provider by navigating to the Analytics tab and filtering by the **desired provider** and **timeframe**. ## Use Cases for Rate Limits diff --git a/product/enterprise-offering/access-control-management.mdx b/product/enterprise-offering/access-control-management.mdx index 2983fec3..1881e95b 100644 --- a/product/enterprise-offering/access-control-management.mdx +++ b/product/enterprise-offering/access-control-management.mdx @@ -12,7 +12,7 @@ At Portkey, we understand the critical importance of access control and data sec ## 1\. Isolated and Customizable Organizations -Portkey's enterprise version allows you to create multiple `organizations`, each serving as a secure and isolated environment for your teams or projects. This multi-tenant architecture ensures that your data, logs, analytics, prompts, virtual keys, configs, guardrails, and API keys are strictly confined within each `organization`, preventing unauthorized access and maintaining data confidentiality. +Portkey's enterprise version allows you to create multiple `organizations`, each serving as a secure and isolated environment for your teams or projects. This multi-tenant architecture ensures that your data, logs, analytics, prompts, integrations, providers, configs, guardrails, and API keys are strictly confined within each `organization`, preventing unauthorized access and maintaining data confidentiality. @@ -29,8 +29,8 @@ With the ability to create and manage multiple organizations, you can tailor acc Portkey offers a comprehensive Role-Based Access Control (RBAC) system that allows you to define and assign user roles with granular permissions. By default, Portkey provides three roles: `Owner`, `Admin`, and `Member`, each with a predefined set of permissions across various features. * `Owners` have complete control over the organization, including user management, billing, and all platform features. -* `Admins` have elevated privileges, allowing them to manage users, prompts, configs, guardrails, virtual keys, and API keys. -* `Members` have access to essential features like logs, analytics, prompts, configs, and virtual keys, with limited permissions. +* `Admins` have elevated privileges, allowing them to manage users, prompts, configs, guardrails, integrations, providers, and API keys. +* `Members` have access to essential features like logs, analytics, prompts, configs, and integrations, providers, with limited permissions. | Feature | Owner Role | Admin Role | Member Role | | ------------------ | ------------------------------------------- | ------------------------------------------- | -------------------------- | @@ -38,7 +38,9 @@ Portkey offers a comprehensive Role-Based Access Control (RBAC) system that allo | Prompts | List, View, Create, Update, Delete, Publish | List, View, Create, Update, Delete, Publish | List, View, Create, Update | | Configs | List, View, Create, Update, Delete | List, View, Create, Update, Delete | List, View, Create | | Guardrails | List, View, Create, Update, Delete | List, View, Create, Update, Delete | List, View, Create, Update | -| Virtual Keys | List, Create, Edit, Duplicate, Delete, Copy | List, Create, Edit, Duplicate, Delete, Copy | List, Copy | +| Integrations | List, Create, Edit, Duplicate, Delete, Copy | List, Create, Edit, Duplicate, Delete, Copy | List, Copy | +| Model Catalog: Providers | List, Create, Edit, Duplicate, Delete, Copy | List, Create, Edit, Duplicate, Delete, Copy | List, Copy | +| Model Catalog: Models | List, Create, Edit, Duplicate, Delete, Copy | List, Create, Edit, Duplicate, Delete, Copy | List, Copy | | Team | Add users, assign roles | Add users, assign roles | \- | | Organisation | Update | Update | \- | | API Keys | Create, Edit, Delete, Update, Rotate | Create, Edit, Delete, Update, Rotate | \- | @@ -52,7 +54,7 @@ You can easily add team members to your organization and assign them appropriate ## 3\. Secure and Customizable API Key Management -Portkey provides a secure and flexible API key management system that allows you to create and manage multiple API keys with fine-grained permissions. Each API key can be customized to grant specific access levels to different features, such as metrics, completions, prompts, configs, guardrails, virtual keys, team management, and API key management. +Portkey provides a secure and flexible API key management system that allows you to create and manage multiple API keys with fine-grained permissions. Each API key can be customized to grant specific access levels to different features, such as metrics, completions, prompts, configs, guardrails, integrations, providers, team management, and API key management. | Feature | Permissions | Default | | --------------------------- | ----------------------------- | -------- | @@ -61,7 +63,9 @@ Portkey provides a secure and flexible API key management system that allows you | Prompts | Disabled, Read, Write, Delete | Read | | Configs | Disabled, Read, Write, Delete | Disabled | | Guardrails | Disabled, Read, Write, Delete | Disabled | -| Virtual Keys | Disabled, Read, Write, Delete | Disabled | +| Integrations | Disabled, Read, Write, Delete | Disabled | +| Model Catalog: Providers | Disabled, Read, Write, Delete | Disabled | +| Model Catalog: Models | Disabled, Read, Write, Delete | Disabled | | Users (Team Management) | Disabled, Read, Write, Delete | Disabled | By default, a new organization is provisioned with a master API key that has all permissions enabled. Owners and admins can edit and manage these keys, as well as create new API keys with tailored permissions. This granular control enables you to enforce the principle of least privilege, ensuring that each API key has access only to the necessary resources. @@ -70,7 +74,7 @@ Portkey's API key management system provides a secure and auditable way to contr ## Audit Logs -Portkey maintains detailed audit logs that capture all administrative activities across the platform. These logs provide visibility into actions related to prompts, configs, guardrails, virtual keys, team management, organization updates, and API key modifications. +Portkey maintains detailed audit logs that capture all administrative activities across the platform. These logs provide visibility into actions related to prompts, configs, guardrails, integrations, providers, team management, organization updates, and API key modifications. Each log entry includes information about the user, the action performed, the affected resource, and a timestamp. This ensures traceability and accountability, helping teams monitor changes and investigate any unauthorized activity. diff --git a/product/enterprise-offering/audit-logs.mdx b/product/enterprise-offering/audit-logs.mdx index 7da163d5..d637ef7f 100644 --- a/product/enterprise-offering/audit-logs.mdx +++ b/product/enterprise-offering/audit-logs.mdx @@ -64,7 +64,8 @@ Portkey provides powerful filtering options to help you find specific audit even - **Resource Type**: Filter by type of resource affected: - Workspaces - API Keys - - Virtual Keys + - Integrations + - Model Catalog - Configs - Prompts - Guardrails diff --git a/product/enterprise-offering/cloud-marketplace/aws.mdx b/product/enterprise-offering/cloud-marketplace/aws.mdx index 6398b7c8..21e87bc1 100644 --- a/product/enterprise-offering/cloud-marketplace/aws.mdx +++ b/product/enterprise-offering/cloud-marketplace/aws.mdx @@ -18,7 +18,7 @@ description: This enterprise-focused document provides comprehensive instruction | --------------------------------------- | ------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------- | | AI Gateway | Deploy as a Docker container in your Kubernetes cluster using Helm Charts | AWS NodeGroup t4g.medium instance, with at least 4GiB of memory and two vCPUs For high reliability, deploy across multiple Availability Zones. | | Logs store | AWS S3 | Each log document is \~10kb in size (uncompressed) | -| Cache (Prompts, Configs & Virtual Keys) | Elasticache or self-hosted Redis | Deploy in the same VPC as the Portkey Gateway. | +| Cache (Prompts, Configs & Providers) | Elasticache or self-hosted Redis | Deploy in the same VPC as the Portkey Gateway. | ## Helm Chart diff --git a/product/enterprise-offering/kms.mdx b/product/enterprise-offering/kms.mdx index 7daf2e33..323911d9 100644 --- a/product/enterprise-offering/kms.mdx +++ b/product/enterprise-offering/kms.mdx @@ -36,7 +36,7 @@ sequenceDiagram ### Encrypted Fields - Configs - Full template -- Virtual Keys +- LLM Integrations - Auth Key - Auth Configuration - Prompts diff --git a/product/enterprise-offering/org-management.mdx b/product/enterprise-offering/org-management.mdx index 39a09775..872a0fdf 100644 --- a/product/enterprise-offering/org-management.mdx +++ b/product/enterprise-offering/org-management.mdx @@ -17,7 +17,7 @@ This hierarchy allows for efficient management of resources and access control a **Workspaces** is currently a feature only enabled on the **Enterprise Plans**. If you're looking to add this feature to your organisation, please reach out to us on our [Discord Community](https://portkey.ai/community) or via email on [support@portkey.ai](mailto:support@portkey.ai) -Organizations contain User Invites & Users, Admin API Keys, and Workspaces. Workspaces, in turn, have their own Team structure (with Managers and Members), Workspace API Keys, and various features like Virtual Keys, Configs, Prompts, and more. +Organizations contain User Invites & Users, Admin API Keys, and Workspaces. Workspaces, in turn, have their own Team structure (with Managers and Members), Workspace API Keys, and various features like Integrations, Model Catalog, Configs, Prompts, and more. This structure enables you to: diff --git a/product/enterprise-offering/org-management/jwt.mdx b/product/enterprise-offering/org-management/jwt.mdx index 046c305b..4f64bb62 100644 --- a/product/enterprise-offering/org-management/jwt.mdx +++ b/product/enterprise-offering/org-management/jwt.mdx @@ -123,27 +123,24 @@ Once the JWT is validated, the server checks for the required **scope**. Scopes - - - Create new virtual keys + + + Create new providers - - Update existing virtual keys + + Update existing providers - Delete virtual keys + Delete providers - - Duplicate existing virtual keys + + Duplicate existing providers - - View virtual key details + + View provider details - - List available virtual keys - - - Copy virtual keys between workspaces + + List available providers diff --git a/product/enterprise-offering/org-management/user-roles-and-permissions.mdx b/product/enterprise-offering/org-management/user-roles-and-permissions.mdx index 6a158ad5..a9bbc880 100644 --- a/product/enterprise-offering/org-management/user-roles-and-permissions.mdx +++ b/product/enterprise-offering/org-management/user-roles-and-permissions.mdx @@ -65,7 +65,7 @@ Workspaces are sub-organizational units that enable better team and project mana | View Workspace Resources | | | | - **Member Access**: Workspace Members have read-only access to workspace resources (logs, prompts, config, virtual keys etc.) but cannot create, update, or delete any resources. + **Member Access**: Workspace Members have read-only access to workspace resources (logs, prompts, config, providers, models etc.) but cannot create, update, or delete any resources. @@ -82,8 +82,8 @@ Organization Owners and Admins can configure access permissions for various reso Control which roles can view, filter, and export logs - - Manage access to provider API keys for different roles + + Manage access to LLM providers for different roles Configure API key creation and management rights diff --git a/product/enterprise-offering/org-management/workspaces.mdx b/product/enterprise-offering/org-management/workspaces.mdx index e845214c..a792b9e6 100644 --- a/product/enterprise-offering/org-management/workspaces.mdx +++ b/product/enterprise-offering/org-management/workspaces.mdx @@ -33,7 +33,7 @@ graph TD A --> H[Scoped Features] H --> I[Logs] H --> J[Prompts] - H --> K[Virtual Keys] + H --> K[Providers] H --> L[Configs] H --> M[Guardrails] H --> N[Other Features] @@ -63,7 +63,7 @@ When attempting to delete a workspace, you'll receive a confirmation dialog. If -Resources that must be deleted before removing a workspace like - Prompts, Prompt partials, Virtual keys, Configs, Guardrails and more. +Resources that must be deleted before removing a workspace like - Prompts, Prompt partials, Providers, Configs, Guardrails and more. Once all resources have been removed, enter the workspace name in the confirmation field to proceed with deletion. diff --git a/product/enterprise-offering/private-cloud-deployments/architecture.mdx b/product/enterprise-offering/private-cloud-deployments/architecture.mdx index 8bc9108e..0d3a30bd 100644 --- a/product/enterprise-offering/private-cloud-deployments/architecture.mdx +++ b/product/enterprise-offering/private-cloud-deployments/architecture.mdx @@ -58,7 +58,7 @@ The Control Plane is fully managed by Portkey and provides the administrative la The AI Gateway periodically synchronizes with the Control Plane: - **Frequency**: 30-second heartbeat intervals - - **Data Retrieved**: Prompt templates, routing configs, virtual keys, API keys + - **Data Retrieved**: Prompt templates, routing configs, integrations, providers, API keys - **Process**: Data is fetched, decrypted locally, and stored in the Gateway cache - **Resilience**: Gateway operates independently between syncs using cached configs @@ -96,7 +96,7 @@ Portkey AI Gateway is deployed as containerized workloads using Helm charts for | Component | Description | Configuration Options | |:----------|:------------|:----------------------| | **AI Gateway** | Core container running the routing logic | Deployed as stateless containers that can scale horizontally | -| **Cache System** | Stores routing configs, virtual keys, and more | Redis (in-cluster, AWS ElastiCache, or custom endpoint) | +| **Cache System** | Stores routing configs, integrations, providers, and more | Redis (in-cluster, AWS ElastiCache, or custom endpoint) | | **Log Storage** | Persistence for request/response data | Multiple options (see below) | ### Storage Options diff --git a/product/enterprise-offering/private-cloud-deployments/aws.mdx b/product/enterprise-offering/private-cloud-deployments/aws.mdx index 063d92bb..08957f6d 100644 --- a/product/enterprise-offering/private-cloud-deployments/aws.mdx +++ b/product/enterprise-offering/private-cloud-deployments/aws.mdx @@ -11,7 +11,7 @@ description: This enterprise-focused document provides comprehensive instruction | --------------------------------------- | ------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------- | | AI Gateway | Deploy as a Docker container in your Kubernetes cluster using Helm Charts | AWS EC2 t4g.medium instance, with at least 4GiB of memory and two vCPUs For high reliability, deploy across multiple Availability Zones. | | Logs Store (optional) | Hosted MongoDB, Document DB or AWS S3 | Each log document is \~10kb in size (uncompressed) | -| Cache (Prompts, Configs & Virtual Keys) | Elasticache or self-hosted Redis | Deploy in the same VPC as the Portkey Gateway. | +| Cache (Prompts, Configs & Providers) | Elasticache or self-hosted Redis | Deploy in the same VPC as the Portkey Gateway. | ## Deployment Steps diff --git a/product/enterprise-offering/private-cloud-deployments/azure.mdx b/product/enterprise-offering/private-cloud-deployments/azure.mdx index eb011cd0..56f0cee2 100644 --- a/product/enterprise-offering/private-cloud-deployments/azure.mdx +++ b/product/enterprise-offering/private-cloud-deployments/azure.mdx @@ -27,7 +27,7 @@ description: This enterprise-focused document provides comprehensive instruction * **Options:** Azure Cosmos DB, Azure Blob Storage. * **Sizing:** Each log document is \~10kb in size (uncompressed). -### Component: Cache (Prompts, Configs & Virtual Keys) +### Component: Cache (Prompts, Configs & Providers) * **Options:** Azure Cache for Redis or self-hosted Redis. * **Deployment:** Deploy in the same VNet as the Portkey Gateway. diff --git a/product/enterprise-offering/private-cloud-deployments/gcp.mdx b/product/enterprise-offering/private-cloud-deployments/gcp.mdx index f083bbb5..e904cebb 100644 --- a/product/enterprise-offering/private-cloud-deployments/gcp.mdx +++ b/product/enterprise-offering/private-cloud-deployments/gcp.mdx @@ -18,7 +18,7 @@ It includes specific recommendations for component sizing, high availability, di * **Options:** Hosted MongoDB, Google Cloud Storage (GCS), or Google Firestore. * **Sizing:** Each log document is \~10kb in size (uncompressed). -### Component: Cache (Prompts, Configs & Virtual Keys) +### Component: Cache (Prompts, Configs & Providers) * **Options:** Google Memorystore for Redis or self-hosted Redis. * **Deployment:** Deploy in the same VPC as the Portkey Gateway. diff --git a/product/enterprise-offering/security-portkey.mdx b/product/enterprise-offering/security-portkey.mdx index a761620b..27a9e529 100644 --- a/product/enterprise-offering/security-portkey.mdx +++ b/product/enterprise-offering/security-portkey.mdx @@ -12,7 +12,7 @@ At Portkey AI, we understand the critical importance of security in today's digi Portkey AI ensures secure API access through token-based authentication mechanisms, supporting Single Sign-On (SSO) via OIDC on enterprise plans. -We also implement [Virtual Keys](/product/ai-gateway/virtual-keys), which provide an added layer of security by securely storing provider API keys within a controlled and monitored environment. +We also implement key encryption, which provide an added layer of security by securely storing provider API keys within a controlled and monitored environment. This multi-tier authentication strategy is crucial for protecting against unauthorized access and ensuring the integrity of user interactions. diff --git a/product/guardrails.mdx b/product/guardrails.mdx index 64e22d2a..85259636 100644 --- a/product/guardrails.mdx +++ b/product/guardrails.mdx @@ -134,7 +134,7 @@ Portkey now offers a more intuitive way to add guardrails to your configurations "cache": { "mode": "simple" }, - "virtual_key": "openai-xxx", + "provider":"@openai-xxx", "input_guardrails": ["guardrails-id-xxx", "guardrails-id-yyy"], "output_guardrails": ["guardrails-id-xxx", "guardrails-id-yyy"] } @@ -158,7 +158,7 @@ You can also continue to use the original hook-based approach: "cache": { "mode": "simple" }, - "virtual_key": "openai-xxx", + "provider":"@openai-xxx", "before_request_hooks": [{ "id": "input-guardrail-id-xx" }], @@ -204,8 +204,8 @@ For requests where `async= FALSE`: "on_status_codes": [246, 446] }, "targets": [ - {"virtual_key": "openai-key-xxx"}, - {"virtual_key": "anthropic-key-xxx"} + {"provider":"@openai-key-xxx"}, + {"provider":"@anthropic-key-xxx"} ], "input_guardrails": ["guardrails-id-xxx"] } diff --git a/product/integrations.mdx b/product/integrations.mdx index 83201dcf..047c164c 100644 --- a/product/integrations.mdx +++ b/product/integrations.mdx @@ -34,6 +34,30 @@ If you are an existing Portkey user, this step is similar to creating a Virtual * **Credentials:** Securely enter your API keys or other authentication details. These are encrypted and will not be visible after saving. 3. Click **Next**. +##### **Special Case: Connecting to Azure OpenAI** + +Portkey allows you to manage multiple Azure deployments under a single integration. This is useful when different models or versions are in separate deployments. + +1. When connecting an Azure integration, you can add multiple deployments. +2. Assign a unique **`alias`** to each deployment (e.g., `gpt-4o-deploy`, `gpt-35-deploy`). +3. Set one deployment as the default. + +To use a specific deployment, developers will pass its `alias` as the `model` in their LLM request. If no alias is provided, the default deployment is used. + + + + + +##### **Special Case: Connecting to Self-Hosted LLMs** + +You can manage your privately hosted models alongside commercial providers. + +1. When creating a new integration, enable the **"Local/Privately hosted provider"** toggle. +2. Select the API specification your LLM implements (e.g., OpenAI). +3. Enter your model's base URL in the **"Custom Host"** field and add any required authentication headers. + +This allows you to use your self-hosted models with all Portkey features. For more details, see [Bring Your Own LLM](/product/ai-gateway/byollm). + #### **Step 2: Provision to Workspaces** Here, you decide which teams get access to this provider and under what conditions. @@ -53,4 +77,34 @@ This is where you enforce model governance and control costs. 3. **(Optional) For Dynamic Models:** If you're using a provider like Fireworks AI with many community models, you can toggle on **"Automatically enable new models"**. For providers like OpenAI or Azure, we recommend an explicit allow-list for better cost control. 4. Click **Create Integration**. -**That's it!** You have successfully created and provisioned a centrally managed integration. It will now appear in your **`Connected`** tab. The workspaces you provisioned will see this as an available "AI Provider" in their Model Catalog, with access only to the models you specified and constrained by the budgets you set. \ No newline at end of file +**That's it!** You have successfully created and provisioned a centrally managed integration. It will now appear in your **`Connected`** tab. The workspaces you provisioned will see this as an available "AI Provider" in their Model Catalog, with access only to the models you specified and constrained by the budgets you set. + +### **Setting Budgets and Rate Limits** + +When provisioning an integration to a workspace, you can enforce powerful governance rules by setting budget and rate limits. This gives you granular control over costs and usage patterns. + + +Budget and Rate Limits are available on **Enterprise** and select **Pro** plans. + + +Click on **Edit Budget & Rate Limits** for any workspace to configure the following: + +* **Cost-Based Budgets:** Set a maximum budget in USD. Once the limit is reached, the provider will be disabled for that workspace to prevent overspending. +* **Token-Based Budgets:** Set a maximum number of tokens that can be consumed. +* **Request-Based Rate Limits:** Control the number of requests allowed per minute, hour, or day. +* **Token-Based Rate Limits:** Control the number of tokens consumed per minute, hour, or day. + +You can also configure **periodic resets** (weekly or monthly) for these limits, which is perfect for managing recurring team budgets. + +--- + +### FAQs + + + + Your API keys are always encrypted and stored in secure, isolated vaults. They are only decrypted in-memory, within sandboxed workers, at the exact moment a request is made to the provider. This ensures the highest level of security for your credentials. + + + We generate unique, random identifiers for each AI Provider. These identifiers are linked to your securely stored keys, but there is no mathematical relationship between them. This means your raw API keys can never be reverse-engineered from the provider slugs. + + diff --git a/product/model-catalog.mdx b/product/model-catalog.mdx index f4353959..0e4419b0 100644 --- a/product/model-catalog.mdx +++ b/product/model-catalog.mdx @@ -55,4 +55,104 @@ You will see a list of providers that have been inherited from the organization. While Org Admins primarily work in the main **[Integrations](/product/integrations)** dashboard, the Model Catalog provides a crucial feedback loop: -When a Workspace Admin creates a new, workspace-exclusive integration (option #2 above), you gain full visibility. This new integration will automatically appear on your main Integrations page under the **"Workspace-Created"** tab, ensuring you always have a complete audit trail of all provider credentials being used across the organization. \ No newline at end of file +When a Workspace Admin creates a new, workspace-exclusive integration (option #2 above), you gain full visibility. This new integration will automatically appear on your main Integrations page under the **"Workspace-Created"** tab, ensuring you always have a complete audit trail of all provider credentials being used across the organization. + +### **SDK Integration and Advanced Usage** + +While the new `model` parameter is the recommended approach for its simplicity, Portkey maintains full backward compatibility and offers flexible integration options for various SDKs. + + +Remember, the term "Virtual Key" is now synonymous with the **AI Provider slug** found in your Model Catalog. + + +#### **Using with Portkey SDK** + +You can set a default AI Provider at initialization or override it per request. + + + + +```js +import Portkey from 'portkey-ai'; + +// Set a default AI Provider for the client +const portkey = new Portkey({ + apiKey: process.env.PORTKEY_API_KEY, + provider:"@YOUR_AI_PROVIDER_SLUG" +// provider: "@YOUR_AI_PROVIDER_SLUG" +}); + +// Or, override it for a specific call +const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'gpt-3.5-turbo', +}, { provider:"@ANOTHER_AI_PROVIDER_SLUG" }); +``` + + + + +```python +from portkey_ai import Portkey + +# Set a default AI Provider for the client +portkey = Portkey( + api_key="PORTKEY_API_KEY", + provider="@YOUR_AI_PROVIDER_SLUG" +# provider="@YOUR_AI_PROVIDER_SLUG" +) + +# Or, override it for a specific call +completion = portkey.with_options( + provider="@ANOTHER_AI_PROVIDER_SLUG" +).chat.completions.create( + messages = [{ "role": 'user', "content": 'Say this is a test' }], + model = 'gpt-3.5-turbo' +) +``` + + + + +#### **Using with OpenAI SDK** + +Simply point the OpenAI client to Portkey's gateway and pass your AI Provider slug in the headers. + + + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +client = OpenAI( + api_key="", # can be left blank + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key="PORTKEY_API_KEY", + provider="@YOUR_AI_PROVIDER_SLUG" +# provider="@YOUR_AI_PROVIDER_SLUG" + ) +) +``` + + + + +```javascript +import OpenAI from "openai"; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'; + +const openai = new OpenAI({ + apiKey: '', // can be left blank + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + apiKey: "PORTKEY_API_KEY", + provider:"@YOUR_AI_PROVIDER_SLUG" +// provider: "@YOUR_AI_PROVIDER_SLUG" + }) +}); +``` + + + \ No newline at end of file diff --git a/product/observability/filters.mdx b/product/observability/filters.mdx index 85746c0a..3a19b72f 100644 --- a/product/observability/filters.mdx +++ b/product/observability/filters.mdx @@ -13,7 +13,7 @@ You can filter analytics & logs by the following parameters: 4. **Status**: The API status of the response that was received from the LLM provider 5. **Meta**: The [metadata](/product/observability/metadata) properties sent to Portkey 6. **Avg Weighted Feedback**: The average weighted feedback scores calculated for the requests -7. **Virtual Key:** The virtual key that's used +7. **Provider:** The provider that's used 8. **Config:** The Config ID that's passed to the request 9. **Trace ID:** Request trace ID 10. **Time Range**: The date and time range for the analytics & logs diff --git a/product/observability/logs-export-deprecated.mdx b/product/observability/logs-export-deprecated.mdx index 5d0bf6a6..ac7d7d1c 100644 --- a/product/observability/logs-export-deprecated.mdx +++ b/product/observability/logs-export-deprecated.mdx @@ -55,7 +55,7 @@ The exported logs data will include the following columns: | retry\_success\_count | Number of retries after which request was successful | | trace\_id | Trace ID for the request | | mode | Config top level strategy (SINGLE, FALLBACK, LOADBALANCE) | -| virtual\_key | Virtual key used for the request | +| provider | Provider used for the request | | runtime | Runtime environment | | runtime\_version | Runtime environment version | | sdk\_version | Portkey SDK version | diff --git a/product/observability/logs.mdx b/product/observability/logs.mdx index b4134e0b..1f670e88 100644 --- a/product/observability/logs.mdx +++ b/product/observability/logs.mdx @@ -74,7 +74,7 @@ You can rerun any buggy request with just one click, straight from the log detai `Replay` **button will be inactive for a log in the following cases:** 1. If the request is sent to any endpoint other than `/chat/completions,` `/completions`, `/embeddings` -2. If the virtual key used in the log is archived on Portkey +2. If the provider used in the log is archived on Portkey 3. If the request originates from a prompt template which is called from inside a Config target @@ -93,7 +93,7 @@ To enable `DO NOT TRACK` for a specific request, set the `debug` flag to `false` import Portkey from 'portkey-ai'; const portkey = new Portkey({ - virtualKey: "OPENAI_VIRTUAL_KEY", + provider:"@OPENAI_PROVIDER", apiKey: "PORTKEY_API_KEY", debug: false }) @@ -115,7 +115,7 @@ from portkey_ai import Portkey portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="OPENAI_VIRTUAL_KEY", + provider="@OPENAI_PROVIDER", debug=False ) @@ -131,7 +131,7 @@ print(response.choices[0].message.content) ```sh curl 'https://api.portkey.ai/v1/chat/completions' \ -H 'Content-Type: application/json' \ - -H 'x-portkey-virtual-key: $OPENAI_VIRTUAL_KEY' \ + -H 'x-portkey-provider: $OPENAI_PROVIDER' \ -H 'x-portkey-api-key: $PORTKEY_API_KEY' \ -H 'x-portkey-debug: false' \ -d '{ @@ -158,7 +158,7 @@ from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders client = OpenAI( base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( - virtual_key="OPENAI_VIRTUAL_KEY", + provider="@OPENAI_PROVIDER", api_key="PORTKEY_API_KEY", debug=False ) @@ -181,7 +181,7 @@ import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' const openai = new OpenAI({ baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ - virtualKey: "OPENAI_VIRTUAL_KEY", + provider:"@OPENAI_PROVIDER", apiKey: "PORTKEY_API_KEY", debug: false }) diff --git a/product/observability/metadata.mdx b/product/observability/metadata.mdx index 8fa2b5fa..945144eb 100644 --- a/product/observability/metadata.mdx +++ b/product/observability/metadata.mdx @@ -35,7 +35,7 @@ from portkey_ai import Portkey portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="OPENAI_VIRTUAL_KEY" + provider="@OPENAI_PROVIDER" ) # Add metadata to track context @@ -58,7 +58,7 @@ import {Portkey} from 'portkey-ai' const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", - virtualKey: "OPENAI_VIRTUAL_KEY" + provider:"@OPENAI_PROVIDER" }) // Request with business context metadata @@ -106,7 +106,7 @@ const completion = await openai.chat.completions.create({ curl https://api.portkey.ai/v1/chat/completions \ -H "Content-Type: application/json" \ -H "x-portkey-api-key: $PORTKEY_API_KEY" \ - -H "x-portkey-virtual-key: $OPENAI_VIRTUAL_KEY" \ + -H "x-portkey-provider: $OPENAI_PROVIDER" \ -H "x-portkey-metadata: {\"_user\":\"user-123\",\"feature\":\"search\"}" \ -d '{ "model": "gpt-4", diff --git a/product/observability/traces.mdx b/product/observability/traces.mdx index 3e65cd03..61a55d67 100644 --- a/product/observability/traces.mdx +++ b/product/observability/traces.mdx @@ -74,7 +74,7 @@ import Portkey from 'portkey-ai'; const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", - virtualKey: "VIRTUAL_KEY", + provider:"@PROVIDER", traceId: "1729", spanId: "11", spanName: "LLM Call" @@ -101,7 +101,7 @@ from portkey_ai import Portkey portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="VIRTUAL_KEY", + provider="@PROVIDER", trace_id="1729", span_id="11", span_name="LLM Call" @@ -189,7 +189,7 @@ import Portkey from 'portkey-ai'; const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", - virtualKey: "VIRTUAL_KEY", + provider:"@PROVIDER", traceID: "TRACE_ID" }) ``` @@ -212,7 +212,7 @@ from portkey_ai import Portkey portkey = Portkey( api_key="PORTKEY_API_KEY", - virtual_key="VIRTUAL_KEY", + provider="@PROVIDER", trace_id="TRACE_ID" ) ``` diff --git a/product/product-feature-comparison.mdx b/product/product-feature-comparison.mdx index ded1fd03..b74cd79e 100644 --- a/product/product-feature-comparison.mdx +++ b/product/product-feature-comparison.mdx @@ -133,7 +133,7 @@ Here's a detailed comparison of our plans to help you choose the right solution | Automatic Retries | | | | | | Request Timeouts | | | | | | Config Management | | | | | -| Virtual Keys & Key Management | | | | (with Budgeting, Rate Limiting support) | +| LLM Key Management | | | | (with Budgeting, Rate Limiting support) | | Simple Caching | | 1 Day TTL SUport | Unlimited TTL Stream from Cache | Unlimited TTL Stream from Cache | | Semantic Caching | | | Unlimited TTL Stream from Cache | Unlimited TTL Stream from Cache | | Unified Construct for Fine-Tuning, Files, Batche APIs | | | | | diff --git a/product/prompt-engineering-studio.mdx b/product/prompt-engineering-studio.mdx index e729ea26..2721692a 100644 --- a/product/prompt-engineering-studio.mdx +++ b/product/prompt-engineering-studio.mdx @@ -20,7 +20,7 @@ You can easily access Prompt Engineering Studio using [https://prompt.new](https ## Setting Up AI Providers -Before you can create and manage prompts, you'll need to set up your [Virtual Keys](/product/ai-gateway/virtual-keys). After configuring your keys, the respective AI providers become available for running and managing prompts. +Before you can create and manage prompts, you'll need to set up your [LLM integrations](/product/integrations). After configuring your keys, the respective AI providers become available for running and managing prompts. Portkey supports over 1600+ models across all the major providers including OpenAI, Anthropic, Google, and many others. This allows you to build and test prompts across multiple models and providers from a single interface. diff --git a/virtual_key_old/integrations/agents.mdx b/virtual_key_old/integrations/agents.mdx new file mode 100644 index 00000000..64b728e6 --- /dev/null +++ b/virtual_key_old/integrations/agents.mdx @@ -0,0 +1,184 @@ +--- +title: "Overview" +description: "Portkey helps bring your agents to production" +--- + + + + OpenAI Agents + + + + + + OpenAI Agents + + + + + + Pydantic AI Agents + + + + + + Autogen + + + + + + CrewAI + + + + + + Phidata + + + + + + Llama Index + + + + + + LangChain + + + + + Langgraph + + + + + Langroid + + + + + + Swarm + + + + + Control Flow + + + + + + Strands + + + + + + Bring Your Agent + + + + + + +## Integrate Portkey with your agents with just 2 lines of code + +```py Langchain +from langchain_openai import ChatOpenAI +from portkey_ai import createHeaders, PORTKEY_GATEWAY_URL + +llm = ChatOpenAI( + api_key="OpenAI_API_Key", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", #choose your provider + api_key="PORTKEY_API_KEY" + ) +) +``` + +### Get Started with Portkey x Agent Cookbooks + +* [Autogen](https://dub.sh/Autogen-docs) +* [CrewAI](https://git.new/crewAI-docs) +* [Phidata](https://dub.sh/Phidata-docs) +* [Llama Index ](https://git.new/llama-agents) +* [Control Flow](https://dub.sh/Control-Flow-docs) + +*** + +## Key Production Features + +By routing your agent's requests through Portkey, you make your agents production-grade with the following features. + +### 1\. [Interoperability](/product/ai-gateway/universal-api) + +Easily switch between LLM providers. Call various LLMs such as Anthropic, Gemini, Mistral, Azure OpenAI, Google Vertex AI, AWS Bedrock and much more by simply changing the `provider ` and `API key` in the LLM object. + +### 2\. [Caching](/product/ai-gateway/cache-simple-and-semantic) + +Improve performance and reduce costs on your Agent's LLM calls by storing past responses in the Portkey cache. Choose between Simple and Semantic cache modes in your Portkey's gateway config. + + +```json +{ + "cache": { + "mode": "semantic" // Choose between "simple" or "semantic" + } +} +``` + +### 3\. [Reliability](/product/ai-gateway) + +Set up **fallbacks** between different LLMs or providers, **load balance** your requests across multiple instances or API keys, set **automatic retries**, and **request timeouts.** Ensure your agents' resilience with advanced reliability features. + +```json +{ + "retry": { + "attempts": 5 + }, + "strategy": { + "mode": "loadbalance" // Choose between "loadbalance" or "fallback" + }, + "targets": [ + { + "provider": "openai", + "api_key": "OpenAI_API_Key" + }, + { + "provider": "anthropic", + "api_key": "Anthropic_API_Key" + } + ] +} +``` + +### 4\. [Observability](/product/observability) + +Portkey automatically logs key details about your agent runs, including cost, tokens used, response time, etc. For agent-specific observability, add Trace IDs to the request headers for each agent. This enables filtering analytics by Trace IDs, ensuring deeper monitoring and analysis. + +### 5\. [Logs](/product/observability/logs) + +Access a dedicated section to view records of action executions, including parameters, outcomes, and errors. Filter logs of your agent run based on multiple parameters such as trace ID, model, tokens used, metadata, etc. + + +azure + + +### 6\. [Prompt Management](/product/prompt-library) + +Use Portkey as a centralized hub to store, version, and experiment with your agent's prompts across multiple LLMs. Easily modify your prompts and run A/B tests without worrying about the breaking prod. + +### 7\. [Continuous Improvement](/product/observability/feedback) + +Improve your Agent runs by capturing qualitative & quantitative user feedback on your requests, and then using that feedback to make your prompts AND LLMs themselves better. + +### 8\. [Security & Compliance](/product/enterprise-offering/security-portkey) + +Set budget limits on provider API keys and implement fine-grained user roles and permissions for both the app and the Portkey APIs. diff --git a/virtual_key_old/integrations/agents/autogen.mdx b/virtual_key_old/integrations/agents/autogen.mdx new file mode 100644 index 00000000..5e6fb1b0 --- /dev/null +++ b/virtual_key_old/integrations/agents/autogen.mdx @@ -0,0 +1,214 @@ +--- +title: "Autogen" +description: "Use Portkey with Autogen to take your AI Agents to production" +--- + +## Getting Started + +### 1\. Install the required packages: + +```sh +pip install -qU pyautogen portkey-ai +``` + +### **2\.** Configure your Autogen configs: + +```py +from autogen import AssistantAgent, UserProxyAgent, config_list_from_json +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +config = [ + { + "api_key": "OPENAI_API_KEY", + "model": "gpt-3.5-turbo", + "base_url": PORTKEY_GATEWAY_URL, + "api_type": "openai", + "default_headers": createHeaders( + api_key ="PORTKEY_API_KEY", #Replace with Your Portkey API key + provider = "openai", + ) + } +] +``` + +## Integration Guide + +Here's a simple Google Colab notebook that demonstrates Autogen with Portkey integration + +[![](/images/guides/colab-badge.svg)](https://dub.sh/Autogen-docs) + +## Make your agents Production-ready with Portkey + +Portkey makes your Autogen agents reliable, robust, and production-grade with its observability suite and AI Gateway. Seamlessly integrate 200+ LLMs with your Autogen agents using Portkey. Implement fallbacks, gain granular insights into agent performance and costs, and continuously optimize your AI operationsโ€”all with just 2 lines of code. + +Let's dive deep! Let's go through each of the use cases! + +### 1.[ Interoperability](/product/ai-gateway/universal-api) + +Easily switch between 200+ LLMs. Call various LLMs such as Anthropic, Gemini, Mistral, Azure OpenAI, Google Vertex AI, AWS Bedrock, and many more by simply changing the `provider ` and `API key` in the `ChatOpenAI` object. + + + + + If you are using OpenAI with autogen, your code would look like this: +```py +config = [ + { + "api_key": "OPENAI_API_KEY", + "model": "gpt-3.5-turbo", + "base_url": PORTKEY_GATEWAY_URL, + "api_type": "openai", + "api_type": "openai", # Portkey conforms to the openai api_type + "default_headers": createHeaders( + api_key ="PORTKEY_API_KEY", #Replace with Your Portkey API key + provider = "openai", + ) + } +] +``` +To switch to Azure as your provider, add your Azure details to Portley vault ([here's how](/integrations/llms/azure-openai)) and use Azure OpenAI using virtual keys + +```py +config = [ + { + "api_key": "api-key", + "model": "gpt-3.5-turbo", + "base_url": PORTKEY_GATEWAY_URL, + "api_type": "openai", # Portkey conforms to the openai api_type + "default_headers": createHeaders( + api_key ="PORTKEY_API_KEY", #Replace with Your Portkey API key + provider = "azure-openai", + virtual_key="AZURE_VIRTUAL_KEY" # Replace with Azure Virtual Key + ) + } +] +``` + + +If you are using Anthropic with CrewAI, your code would look like this: +```py +config = [ + { + "api_key": "ANTHROPIC_VIRTUAL_KEY", + "model": "gpt-3.5-turbo", + "api_type": "openai", # Portkey conforms to the openai api_type + "base_url": PORTKEY_GATEWAY_URL, + "default_headers": createHeaders( + api_key ="PORTKEY_API_KEY", #Replace with Your Portkey API key + provider = "anthropic", + ) + } +] +``` +To switch to AWS Bedrock as your provider, add your AWS Bedrock details to Portley vault ([here's how](/integrations/llms/aws-bedrock)) and use AWS Bedrock using virtual keys, + +```py +config = [ + { + "api_key": "api-key", #We are using Virtual Key + "model": "gpt-3.5-turbo", + "api_type": "openai", # Portkey conforms to the openai api_type + "base_url": PORTKEY_GATEWAY_URL, + "default_headers": createHeaders( + api_key ="PORTKEY_API_KEY", #Replace with Your Portkey API key + provider = "bedrock", + virtual_key="AWS_VIRTUAL_API_KEY" # Replace with Virtual Key + ) + } +] +``` + + + + +### 2\. [Reliability](/product/ai-gateway) + +Agents are _brittle_. Long agentic pipelines with multiple steps can fail at any stage, disrupting the entire process. Portkey solves this by offering built-in **fallbacks** between different LLMs or providers, **load-balancing** across multiple instances or API keys, and implementing automatic **retries** and request **timeouts**. This makes your agents more reliable and resilient. + +Here's how you can implement these features using Portkey's config + + + +```py +{ + "retry": { + "attempts": 5 + }, + "strategy": { + "mode": "loadbalance" // Choose between "loadbalance" or "fallback" + }, + "targets": [ + { + "provider": "openai", + "api_key": "OpenAI_API_Key" + }, + { + "provider": "anthropic", + "api_key": "Anthropic_API_Key" + } + ] +} +``` + +### 3\. [Metrics](/product/observability) + +Agent runs can be costly. Tracking agent metrics is crucial for understanding the performance and reliability of your AI agents. Metrics help identify issues, optimize runs, and ensure that your agents meet their intended goals. + +Portkey automatically logs comprehensive metrics for your AI agents, including **cost**, **tokens used**, **latency**, etc. Whether you need a broad overview or granular insights into your agent runs, Portkey's customizable filters provide the metrics you need. For agent-specific observability, add `Trace-id` to the request headers for each agent. + + + +```py +config = [ + { + "api_key": "OPENAI_API_KEY", + "model": "gpt-3.5-turbo", + "base_url": PORTKEY_GATEWAY_URL, + "api_type": "openai", + "default_headers": createHeaders( + api_key ="PORTKEY_API_KEY", #Replace with your Portkey API key + provider = "openai", + trace_id="research_agent1" #Add individual trace-id for your agent + ) + } +] +``` + +### 4\. [Logs](/product/observability/logs) + +Agent runs are complex. Logs are essential for diagnosing issues, understanding agent behavior, and improving performance. They provide a detailed record of agent activities and tool use, which is crucial for debugging and optimizing processes. + +Portkey offers comprehensive logging features that capture detailed information about every action and decision made by your AI agents. Access a dedicated section to view records of agent executions, including parameters, outcomes, function calls, and errors. Filter logs based on multiple parameters such as trace ID, model, tokens used, and metadata. + + +logs + + +### 5\. [Continuous Improvement](/product/observability/feedback) + +Improve your Agent runs by capturing qualitative & quantitative user feedback on your requests. Portkey's Feedback APIs provide a simple way to get weighted feedback from customers on any request you served, at any stage in your app. You can capture this feedback on a request or conversation level and analyze it by adding meta data to the relevant request. + +### 6\. [Caching](/product/ai-gateway/cache-simple-and-semantic) + +Agent runs are time-consuming and expensive due to their complex pipelines. Caching can significantly reduce these costs by storing frequently used data and responses. Portkey offers a built-in caching system that stores past responses, reducing the need for agent calls saving both time and money. + + + +```json +{ + "cache": { + "mode": "semantic" // Choose between "simple" or "semantic" + } +} +``` + +### 7\. [Security & Compliance](/product/enterprise-offering/security-portkey) + +Set budget limits on provider API keys and implement fine-grained user roles and permissions for both the app and the Portkey APIs. + + +## [Portkey Config](/product/ai-gateway/configs) + +Many of these features are driven by Portkey's Config architecture. The Portkey app simplifies creating, managing, and versioning your Configs. + +For more information on using these features and setting up your Config, please refer to the [Portkey documentation](https://docs.portkey.ai). diff --git a/virtual_key_old/integrations/agents/bring-your-own-agents.mdx b/virtual_key_old/integrations/agents/bring-your-own-agents.mdx new file mode 100644 index 00000000..0611978b --- /dev/null +++ b/virtual_key_old/integrations/agents/bring-your-own-agents.mdx @@ -0,0 +1,191 @@ +--- +title: "Bring Your own Agents" +description: "You can also use Portkey if you are doing custom agent orchestration!" +--- + +## Getting Started + +### 1\. Install the required packages: + +```sh +pip install portkey-ai openai +``` + +### **2\.** Configure your OpenAI object: + + +```py +client = OpenAI( + api_key="OPENAI_API_KEY", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY", + virtual_key="openai-latest-a4a53d" + ) +) +``` + +## Integrate Portkey with your custom Agents + +This notebook demonstrates integrating a ReAct agent with Portkey + +[![](/images/guides/colab-badge.svg)](https://dub.sh/ReAct-agent) + +## Make your agents Production-ready with Portkey + +Portkey makes your agents reliable, robust, and production-grade with its observability suite and AI Gateway. Seamlessly integrate 200+ LLMs with your custom agents using Portkey. Implement fallbacks, gain granular insights into agent performance and costs, and continuously optimize your AI operationsโ€”all with just 2 lines of code. + +Let's dive deep! Let's go through each of the use cases! + +### 1\. [Interoperability](/product/ai-gateway/universal-api) + +Easily switch between 200+ LLMs. Call various LLMs such as Anthropic, Gemini, Mistral, Azure OpenAI, Google Vertex AI, AWS Bedrock, and many more by simply changing the `provider ` and `API key` in the `ChatOpenAI` object. + + + +If you are using OpenAI with CrewAI, your code would look like this: + +```py +client = OpenAI( + api_key="OPENAI_API_KEY", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY", + ) +) +``` + +To switch to Azure as your provider, add your Azure details to Portley vault ([here's how](/integrations/llms/azure-openai)) and use Azure OpenAI using virtual keys + + + +```py +client = OpenAI( + api_key="API_KEY", #We will use Virtual Key in this + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="azure-openai", + api_key="PORTKEY_API_KEY", + virtual_key="AZURE_VIRTUAL_KEY" #Azure Virtual key + ) +) +``` + + +If you are using Anthropic with CrewAI, your code would look like this: + +```py +client = OpenAI( + api_key="ANTROPIC_API_KEY", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="anthropic", + api_key="PORTKEY_API_KEY", + ) +) +``` + +To switch to AWS Bedrock as your provider, add your AWS Bedrock details to Portley vault ([here's how](/integrations/llms/aws-bedrock)) and use AWS Bedrock using virtual keys, + + + +```py +client = OpenAI( + api_key="api_key", #We will use Virtual Key in this + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="bedrock", + api_key="PORTKEY_API_KEY", + virtual_key="AWS_VIRTUAL_KEY" #Bedrock Virtual Key + ) +) +``` + + + +### 2\. [Reliability](/product/ai-gateway) + +Agents are _brittle_. Long agentic pipelines with multiple steps can fail at any stage, disrupting the entire process. Portkey solves this by offering built-in **fallbacks** between different LLMs or providers, **load-balancing** across multiple instances or API keys, and implementing automatic **retries** and request **timeouts**. This makes your agents more reliable and resilient. + +Here's how you can implement these features using Portkey's config + +```py +{ + "retry": { + "attempts": 5 + }, + "strategy": { + "mode": "loadbalance" // Choose between "loadbalance" or "fallback" + }, + "targets": [ + { + "provider": "openai", + "api_key": "OpenAI_API_Key" + }, + { + "provider": "anthropic", + "api_key": "Anthropic_API_Key" + } + ] +} +``` + +### 3\. [Metrics](/product/observability) + +Agent runs can be costly. Tracking agent metrics is crucial for understanding the performance and reliability of your AI agents. Metrics help identify issues, optimize runs, and ensure that your agents meet their intended goals. + +Portkey automatically logs comprehensive metrics for your AI agents, including **cost**, **tokens used**, **latency**, etc. Whether you need a broad overview or granular insights into your agent runs, Portkey's customizable filters provide the metrics you need. For agent-specific observability, add `Trace-id` to the request headers for each agent. + +```py +llm2 = ChatOpenAI( + api_key="Anthropic_API_Key", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key="PORTKEY_API_KEY", + provider="anthropic", + trace_id="research_agent1" #Add individual trace-id for your agent analytics + ) +) +``` + +### 4\. [Logs](/product/observability/logs) + +Agent runs are complex. Logs are essential for diagnosing issues, understanding agent behavior, and improving performance. They provide a detailed record of agent activities and tool use, which is crucial for debugging and optimizing processes. + +Portkey offers comprehensive logging features that capture detailed information about every action and decision made by your AI agents. Access a dedicated section to view records of agent executions, including parameters, outcomes, function calls, and errors. Filter logs based on multiple parameters such as trace ID, model, tokens used, and metadata. + + +logs + + +### 5\. [Continuous Improvement](/product/observability/feedback) + +Improve your Agent runs by capturing qualitative & quantitative user feedback on your requests. Portkey's Feedback APIs provide a simple way to get weighted feedback from customers on any request you served, at any stage in your app. You can capture this feedback on a request or conversation level and analyze it by adding meta data to the relevant request. + +### 6\. [Caching](/product/ai-gateway/cache-simple-and-semantic) + +Agent runs are time-consuming and expensive due to their complex pipelines. Caching can significantly reduce these costs by storing frequently used data and responses. Portkey offers a built-in caching system that stores past responses, reducing the need for agent calls saving both time and money. + + + +```py +{ + "cache": { + "mode": "semantic" // Choose between "simple" or "semantic" + } +} +``` + +### 7.[ Security & Compliance](/product/enterprise-offering/security-portkey) + +Set budget limits on provider API keys and implement fine-grained user roles and permissions for both the app and the Portkey APIs. + +*** + +## [Portkey Config](/product/ai-gateway/configs) + +Many of these features are driven by Portkey's Config architecture. The Portkey app simplifies creating, managing, and versioning your Configs. + +For more information on using these features and setting up your Config, please refer to the [Portkey documentation](https://docs.portkey.ai). diff --git a/virtual_key_old/integrations/agents/control-flow.mdx b/virtual_key_old/integrations/agents/control-flow.mdx new file mode 100644 index 00000000..56748ab9 --- /dev/null +++ b/virtual_key_old/integrations/agents/control-flow.mdx @@ -0,0 +1,199 @@ +--- +title: "Control Flow" +description: "Use Portkey with Control Flow to take your AI Agents to production" +--- + +## Getting Started + +### 1\. Install the required packages: + +```sh +pip install -qU portkey-ai controlflow +``` + +### **2\.** Configure your Control FLow LLM objects: + +```py +import controlflow as cf +from langchain_openai import ChatOpenAI +from portkey_ai import createHeaders, PORTKEY_GATEWAY_URL + +llm = ChatOpenAI( + api_key="OpenAI_API_Key", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", #choose your provider + api_key="PORTKEY_API_KEY" + ) +) +``` + +## Integration Guide + +Here's a simple Google Colab notebook that demonstrates Control Flow with Portkey integration + +[![](/images/guides/colab-badge.svg)](https://dub.sh/Control-Flow-docs) + + +## Make your agents Production-ready with Portkey + +Portkey makes your Control Flow agents reliable, robust, and production-grade with its observability suite and AI Gateway. Seamlessly integrate 200+ LLMs with your Control Flow agents using Portkey. Implement fallbacks, gain granular insights into agent performance and costs, and continuously optimize your AI operationsโ€”all with just 2 lines of code. + +Let's dive deep! Let's go through each of the use cases! + +### 1\. [Interoperability](/product/ai-gateway/universal-api) + +Easily switch between 200+ LLMs. Call various LLMs such as Anthropic, Gemini, Mistral, Azure OpenAI, Google Vertex AI, AWS Bedrock, and many more by simply changing the `provider ` and `API key` in the `ChatOpenAI` object. + + + +If you are using OpenAI with Control Flow, your code would look like this: + +```py +llm = ChatOpenAI( + api_key="OpenAI_API_Key", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", #choose your provider + api_key="PORTKEY_API_KEY" + ) +) +``` + +To switch to Azure as your provider, add your Azure details to Portley vault ([here's how](/integrations/llms/azure-openai)) and use Azure OpenAI using virtual keys + + + +```py +llm = ChatOpenAI( + api_key="api-key", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="azure-openai", #choose your provider + api_key="PORTKEY_API_KEY", + virtual_key="AZURE_VIRTUAL_KEY" # Replace with your virtual key for Azure + ) +) +``` + + +If you are using Anthropic with CrewAI, your code would look like this: + + + +```py +llm = ChatOpenAI( + api_key="ANTHROPIC_API_KEY", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="anthropic", #choose your provider + api_key="PORTKEY_API_KEY" + ) +) +``` + +To switch to AWS Bedrock as your provider, add your AWS Bedrock details to Portley vault ([here's how](/integrations/llms/aws-bedrock)) and use AWS Bedrock using virtual keys, + + + +```py +llm = ChatOpenAI( + api_key="api-key", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="bedrock", #choose your provider + api_key="PORTKEY_API_KEY", + virtual_key="AWS_Bedrock_VIRTUAL_KEY" # Replace with your virtual key for Bedrock + ) +) +``` + + + + +### 2\. [Reliability](/product/ai-gateway) + +Agents are _brittle_. Long agentic pipelines with multiple steps can fail at any stage, disrupting the entire process. Portkey solves this by offering built-in **fallbacks** between different LLMs or providers, **load-balancing** across multiple instances or API keys, and implementing automatic **retries** and request **timeouts**. This makes your agents more reliable and resilient. + +Here's how you can implement these features using Portkey's config + + + +```py +{ + "retry": { + "attempts": 5 + }, + "strategy": { + "mode": "loadbalance" // Choose between "loadbalance" or "fallback" + }, + "targets": [ + { + "provider": "openai", + "api_key": "OpenAI_API_Key" + }, + { + "provider": "anthropic", + "api_key": "Anthropic_API_Key" + } + ] +} +``` + +### 3\. [Metrics](/product/observability) + +Agent runs can be costly. Tracking agent metrics is crucial for understanding the performance and reliability of your AI agents. Metrics help identify issues, optimize runs, and ensure that your agents meet their intended goals. + +Portkey automatically logs comprehensive metrics for your AI agents, including **cost**, **tokens used**, **latency**, etc. Whether you need a broad overview or granular insights into your agent runs, Portkey's customizable filters provide the metrics you need. For agent-specific observability, add `Trace-id` to the request headers for each agent. + + + +```py +llm2 = ChatOpenAI( + api_key="Anthropic_API_Key", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key="PORTKEY_API_KEY", + provider="anthropic", + trace_id="research_agent1" #Add individual trace-id for your agent analytics + ) + +) +``` + +### 4\. [Logs](/product/observability/logs) + +Agent runs are complex. Logs are essential for diagnosing issues, understanding agent behavior, and improving performance. They provide a detailed record of agent activities and tool use, which is crucial for debugging and optimizing processes. + +Portkey offers comprehensive logging features that capture detailed information about every action and decision made by your AI agents. Access a dedicated section to view records of agent executions, including parameters, outcomes, function calls, and errors. Filter logs based on multiple parameters such as trace ID, model, tokens used, and metadata. + + + + +### 5\. [Continuous Improvement](/product/observability/feedback) + +Improve your Agent runs by capturing qualitative & quantitative user feedback on your requests. Portkey's Feedback APIs provide a simple way to get weighted feedback from customers on any request you served, at any stage in your app. You can capture this feedback on a request or conversation level and analyze it by adding meta data to the relevant request. + +### 6\. [Caching](/product/ai-gateway/cache-simple-and-semantic) + +Agent runs are time-consuming and expensive due to their complex pipelines. Caching can significantly reduce these costs by storing frequently used data and responses. Portkey offers a built-in caching system that stores past responses, reducing the need for agent calls saving both time and money. + +```py +{ + "cache": { + "mode": "semantic" // Choose between "simple" or "semantic" + } +} +``` + +### 7\. [Security & Compliance](/product/enterprise-offering/security-portkey) + +Set budget limits on provider API keys and implement fine-grained user roles and permissions for both the app and the Portkey APIs. + +--- + +## Portkey Config + +Many of these features are driven by Portkey's Config architecture. The Portkey app simplifies creating, managing, and versioning your Configs. + +For more information on using these features and setting up your Config, please refer to the [Portkey documentation](https://docs.portkey.ai). diff --git a/virtual_key_old/integrations/agents/crewai.mdx b/virtual_key_old/integrations/agents/crewai.mdx new file mode 100644 index 00000000..263a86d9 --- /dev/null +++ b/virtual_key_old/integrations/agents/crewai.mdx @@ -0,0 +1,822 @@ +--- +title: "CrewAI" +description: "Use Portkey with CrewAI to take your AI Agents to production" +--- + +## Introduction + +CrewAI is a framework for orchestrating role-playing, autonomous AI agents designed to solve complex, open-ended tasks through collaboration. It provides a robust structure for agents to work together, leverage tools, and exchange insights to accomplish sophisticated objectives. + +Portkey enhances CrewAI with production-readiness features, turning your experimental agent crews into robust systems by providing: + +- **Complete observability** of every agent step, tool use, and interaction +- **Built-in reliability** with fallbacks, retries, and load balancing +- **Cost tracking and optimization** to manage your AI spend +- **Access to 200+ LLMs** through a single integration +- **Guardrails** to keep agent behavior safe and compliant +- **Version-controlled prompts** for consistent agent performance + + + Learn more about CrewAI's core concepts and features + + +### Installation & Setup + + + +```bash +pip install -U crewai portkey-ai +``` + + + +Create a Portkey API key with optional budget/rate limits from the [Portkey dashboard](https://app.portkey.ai/). You can also attach configurations for reliability, caching, and more to this key. More on this later. + + + +The integration is simple - you just need to update the LLM configuration in your CrewAI setup: + +```python +from crewai import LLM +from portkey_ai import createHeaders, PORTKEY_GATEWAY_URL + +# Create an LLM instance with Portkey integration +gpt_llm = LLM( + model="gpt-4o", + base_url=PORTKEY_GATEWAY_URL, + api_key="dummy", # We are using a Virtual key, so this is a placeholder + extra_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_LLM_VIRTUAL_KEY", + trace_id="unique-trace-id", # Optional, for request tracing + ) +) + +#Use them in your Crew Agents like this: + + @agent + def lead_market_analyst(self) -> Agent: + return Agent( + config=self.agents_config['lead_market_analyst'], + verbose=True, + memory=False, + llm=gpt_llm + ) + +``` + + +**What are Virtual Keys?** Virtual keys in Portkey securely store your LLM provider API keys (OpenAI, Anthropic, etc.) in an encrypted vault. They allow for easier key rotation and budget management. [Learn more about virtual keys here](/product/ai-gateway/virtual-keys). + + + + +## Production Features + +### 1. Enhanced Observability + +Portkey provides comprehensive observability for your CrewAI agents, helping you understand exactly what's happening during each execution. + + + + + + + +Traces provide a hierarchical view of your crew's execution, showing the sequence of LLM calls, tool invocations, and state transitions. + +```python +# Add trace_id to enable hierarchical tracing in Portkey +portkey_llm = LLM( + model="gpt-4o", + base_url=PORTKEY_GATEWAY_URL, + api_key="dummy", + extra_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + trace_id="unique-session-id" # Add unique trace ID + ) +) +``` + + + + + + + +Portkey logs every interaction with LLMs, including: + +- Complete request and response payloads +- Latency and token usage metrics +- Cost calculations +- Tool calls and function executions + +All logs can be filtered by metadata, trace IDs, models, and more, making it easy to debug specific crew runs. + + + + + + + +Portkey provides built-in dashboards that help you: + +- Track cost and token usage across all crew runs +- Analyze performance metrics like latency and success rates +- Identify bottlenecks in your agent workflows +- Compare different crew configurations and LLMs + +You can filter and segment all metrics by custom metadata to analyze specific crew types, user groups, or use cases. + + + + + Analytics with metadata filters + + +Add custom metadata to your CrewAI LLM configuration to enable powerful filtering and segmentation: + +```python +portkey_llm = LLM( + model="gpt-4o", + base_url=PORTKEY_GATEWAY_URL, + api_key="dummy", + extra_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + metadata={ + "crew_type": "research_crew", + "environment": "production", + "_user": "user_123", # Special _user field for user analytics + "request_source": "mobile_app" + } + ) +) +``` + +This metadata can be used to filter logs, traces, and metrics on the Portkey dashboard, allowing you to analyze specific crew runs, users, or environments. + + + +### 2. Reliability - Keep Your Crews Running Smoothly + +When running crews in production, things can go wrong - API rate limits, network issues, or provider outages. Portkey's reliability features ensure your agents keep running smoothly even when problems occur. + +It's simple to enable fallback in your CrewAI setup by using a Portkey Config: + +```python +from crewai import LLM +from portkey_ai import createHeaders, PORTKEY_GATEWAY_URL + +# Create LLM with fallback configuration +portkey_llm = LLM( + model="gpt-4o", + max_tokens=1000, + base_url=PORTKEY_GATEWAY_URL, + api_key="dummy", + extra_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + config={ + "strategy": { + "mode": "fallback" + }, + "targets": [ + { + "provider": "openai", + "api_key": "YOUR_OPENAI_API_KEY", + "override_params": {"model": "gpt-4o"} + }, + { + "provider": "anthropic", + "api_key": "YOUR_ANTHROPIC_API_KEY", + "override_params": {"model": "claude-3-opus-20240229"} + } + ] + } + ) +) + +# Use this LLM configuration with your agents +``` + +This configuration will automatically try Claude if the GPT-4o request fails, ensuring your crew can continue operating. + + + + Handles temporary failures automatically. If an LLM call fails, Portkey will retry the same request for the specified number of times - perfect for rate limits or network blips. + + + Prevent your agents from hanging. Set timeouts to ensure you get responses (or can fail gracefully) within your required timeframes. + + + Send different requests to different providers. Route complex reasoning to GPT-4, creative tasks to Claude, and quick responses to Gemini based on your needs. + + + Keep running even if your primary provider fails. Automatically switch to backup providers to maintain availability. + + + Spread requests across multiple API keys or providers. Great for high-volume crew operations and staying within rate limits. + + + +### 3. Prompting in CrewAI + +Portkey's Prompt Engineering Studio helps you create, manage, and optimize the prompts used in your CrewAI agents. Instead of hardcoding prompts or instructions, use Portkey's prompt rendering API to dynamically fetch and apply your versioned prompts. + + +![Prompt Playground Interface](/images/product/ai-gateway/ai-20.webp) + + + + +Prompt Playground is a place to compare, test and deploy perfect prompts for your AI application. It's where you experiment with different models, test variables, compare outputs, and refine your prompt engineering strategy before deploying to production. It allows you to: + +1. Iteratively develop prompts before using them in your agents +2. Test prompts with different variables and models +3. Compare outputs between different prompt versions +4. Collaborate with team members on prompt development + +This visual environment makes it easier to craft effective prompts for each step in your CrewAI agents' workflow. + + + +The Prompt Render API retrieves your prompt templates with all parameters configured: + +```python +from crewai import Agent, LLM +from portkey_ai import createHeaders, PORTKEY_GATEWAY_URL, Portkey + +# Initialize Portkey admin client +portkey_admin = Portkey(api_key="YOUR_PORTKEY_API_KEY") + +# Retrieve prompt using the render API +prompt_data = portkey_client.prompts.render( + prompt_id="YOUR_PROMPT_ID", + variables={ + "agent_role": "Senior Research Scientist", + } +) + +backstory_agent_prompt=prompt_data.data.messages[0]["content"] + + +# Set up LLM with Portkey integration +portkey_llm = LLM( + model="gpt-4o", + base_url=PORTKEY_GATEWAY_URL, + api_key="dummy", + extra_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_OPENAI_VIRTUAL_KEY" + ) +) + +# Create agent using the rendered prompt +researcher = Agent( + role="Senior Research Scientist", + goal="Discover groundbreaking insights about the assigned topic", + backstory=backstory_agent, # Use the rendered prompt + verbose=True, + llm=portkey_llm +) +``` + + + +You can: +- Create multiple versions of the same prompt +- Compare performance between versions +- Roll back to previous versions if needed +- Specify which version to use in your code: + +```python +# Use a specific prompt version +prompt_data = portkey_admin.prompts.render( + prompt_id="YOUR_PROMPT_ID@version_number", + variables={ + "agent_role": "Senior Research Scientist", + "agent_goal": "Discover groundbreaking insights" + } +) +``` + + + +Portkey prompts use Mustache-style templating for easy variable substitution: + +``` +You are a {{agent_role}} with expertise in {{domain}}. + +Your mission is to {{agent_goal}} by leveraging your knowledge +and experience in the field. + +Always maintain a {{tone}} tone and focus on providing {{focus_area}}. +``` + +When rendering, simply pass the variables: + +```python +prompt_data = portkey_admin.prompts.render( + prompt_id="YOUR_PROMPT_ID", + variables={ + "agent_role": "Senior Research Scientist", + "domain": "artificial intelligence", + "agent_goal": "discover groundbreaking insights", + "tone": "professional", + "focus_area": "practical applications" + } +) +``` + + + + + Learn more about Portkey's prompt management features + + +### 4. Guardrails for Safe Crews + +Guardrails ensure your CrewAI agents operate safely and respond appropriately in all situations. + +**Why Use Guardrails?** + +CrewAI agents can experience various failure modes: +- Generating harmful or inappropriate content +- Leaking sensitive information like PII +- Hallucinating incorrect information +- Generating outputs in incorrect formats + +Portkey's guardrails add protections for both inputs and outputs. + +**Implementing Guardrails** + +```python +from crewai import Agent, LLM +from portkey_ai import createHeaders, PORTKEY_GATEWAY_URL + +# Create LLM with guardrails +portkey_llm = LLM( + model="gpt-4o", + base_url=PORTKEY_GATEWAY_URL, + api_key="dummy", + extra_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + config={ + "input_guardrails": ["guardrails-id-xxx", "guardrails-id-yyy"], + "output_guardrails": ["guardrails-id-zzz"] + } + ) +) + +# Create agent with guardrailed LLM +researcher = Agent( + role="Senior Research Scientist", + goal="Discover groundbreaking insights about the assigned topic", + backstory="You are an expert researcher with deep domain knowledge.", + verbose=True, + llm=portkey_llm +) +``` + +Portkey's guardrails can: +- Detect and redact PII in both inputs and outputs +- Filter harmful or inappropriate content +- Validate response formats against schemas +- Check for hallucinations against ground truth +- Apply custom business logic and rules + + + Explore Portkey's guardrail features to enhance agent safety + + +### 5. User Tracking with Metadata + +Track individual users through your CrewAI agents using Portkey's metadata system. + +**What is Metadata in Portkey?** + +Metadata allows you to associate custom data with each request, enabling filtering, segmentation, and analytics. The special `_user` field is specifically designed for user tracking. + +```python +from crewai import Agent, LLM +from portkey_ai import createHeaders, PORTKEY_GATEWAY_URL + +# Configure LLM with user tracking +portkey_llm = LLM( + model="gpt-4o", + base_url=PORTKEY_GATEWAY_URL, + api_key="dummy", + extra_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + metadata={ + "_user": "user_123", # Special _user field for user analytics + "user_tier": "premium", + "user_company": "Acme Corp", + "session_id": "abc-123" + } + ) +) + +# Create agent with tracked LLM +researcher = Agent( + role="Senior Research Scientist", + goal="Discover groundbreaking insights about the assigned topic", + backstory="You are an expert researcher with deep domain knowledge.", + verbose=True, + llm=portkey_llm +) +``` + +**Filter Analytics by User** + +With metadata in place, you can filter analytics by user and analyze performance metrics on a per-user basis: + + + + + +This enables: +- Per-user cost tracking and budgeting +- Personalized user analytics +- Team or organization-level metrics +- Environment-specific monitoring (staging vs. production) + + + Explore how to use custom metadata to enhance your analytics + + +### 6. Caching for Efficient Crews + +Implement caching to make your CrewAI agents more efficient and cost-effective: + + + +```python +from crewai import Agent, LLM +from portkey_ai import createHeaders, PORTKEY_GATEWAY_URL + +# Configure LLM with simple caching +portkey_llm = LLM( + model="gpt-4o", + base_url=PORTKEY_GATEWAY_URL, + api_key="dummy", + extra_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + config={ + "cache": { + "mode": "simple" + } + } + ) +) + +# Create agent with cached LLM +researcher = Agent( + role="Senior Research Scientist", + goal="Discover groundbreaking insights about the assigned topic", + backstory="You are an expert researcher with deep domain knowledge.", + verbose=True, + llm=portkey_llm +) +``` + +Simple caching performs exact matches on input prompts, caching identical requests to avoid redundant model executions. + + + +```python +from crewai import Agent, LLM +from portkey_ai import createHeaders, PORTKEY_GATEWAY_URL + +# Configure LLM with semantic caching +portkey_llm = LLM( + model="gpt-4o", + base_url=PORTKEY_GATEWAY_URL, + api_key="dummy", + extra_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + config={ + "cache": { + "mode": "semantic" + } + } + ) +) + +# Create agent with semantically cached LLM +researcher = Agent( + role="Senior Research Scientist", + goal="Discover groundbreaking insights about the assigned topic", + backstory="You are an expert researcher with deep domain knowledge.", + verbose=True, + llm=portkey_llm +) +``` + +Semantic caching considers the contextual similarity between input requests, caching responses for semantically similar inputs. + + + +### 7. Model Interoperability + +CrewAI supports multiple LLM providers, and Portkey extends this capability by providing access to over 200 LLMs through a unified interface. You can easily switch between different models without changing your core agent logic: + +```python +from crewai import Agent, LLM +from portkey_ai import createHeaders, PORTKEY_GATEWAY_URL + +# Set up LLMs with different providers +openai_llm = LLM( + model="gpt-4o", + base_url=PORTKEY_GATEWAY_URL, + api_key="dummy", + extra_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_OPENAI_VIRTUAL_KEY" + ) +) + +anthropic_llm = LLM( + model="claude-3-5-sonnet-latest", + max_tokens=1000, + base_url=PORTKEY_GATEWAY_URL, + api_key="dummy", + extra_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_ANTHROPIC_VIRTUAL_KEY" + ) +) + +# Choose which LLM to use for each agent based on your needs +researcher = Agent( + role="Senior Research Scientist", + goal="Discover groundbreaking insights about the assigned topic", + backstory="You are an expert researcher with deep domain knowledge.", + verbose=True, + llm=openai_llm # Use anthropic_llm for Anthropic +) +``` + +Portkey provides access to LLMs from providers including: + +- OpenAI (GPT-4o, GPT-4 Turbo, etc.) +- Anthropic (Claude 3.5 Sonnet, Claude 3 Opus, etc.) +- Mistral AI (Mistral Large, Mistral Medium, etc.) +- Google Vertex AI (Gemini 1.5 Pro, etc.) +- Cohere (Command, Command-R, etc.) +- AWS Bedrock (Claude, Titan, etc.) +- Local/Private Models + + + See the full list of LLM providers supported by Portkey + + +## Set Up Enterprise Governance for CrewAI + +**Why Enterprise Governance?** +If you are using CrewAI inside your organization, you need to consider several governance aspects: +- **Cost Management**: Controlling and tracking AI spending across teams +- **Access Control**: Managing which teams can use specific models +- **Usage Analytics**: Understanding how AI is being used across the organization +- **Security & Compliance**: Maintaining enterprise security standards +- **Reliability**: Ensuring consistent service across all users + +Portkey adds a comprehensive governance layer to address these enterprise needs. Let's implement these controls step by step. + + + +Virtual Keys are Portkey's secure way to manage your LLM provider API keys. They provide essential controls like: +- Budget limits for API usage +- Rate limiting capabilities +- Secure API key storage + +To create a virtual key: +Go to [Virtual Keys](https://app.portkey.ai/virtual-keys) in the Portkey App. Save and copy the virtual key ID + + + + + + +Save your virtual key ID - you'll need it for the next step. + + + + +Configs in Portkey define how your requests are routed, with features like advanced routing, fallbacks, and retries. + +To create your config: +1. Go to [Configs](https://app.portkey.ai/configs) in Portkey dashboard +2. Create new config with: + ```json + { + "virtual_key": "YOUR_VIRTUAL_KEY_FROM_STEP1", + "override_params": { + "model": "gpt-4o" // Your preferred model name + } + } + ``` +3. Save and note the Config name for the next step + + + + + + + +Now create a Portkey API key and attach the config you created in Step 2: + +1. Go to [API Keys](https://app.portkey.ai/api-keys) in Portkey and Create new API key +2. Select your config from `Step 2` +3. Generate and save your API key + + + + + + + +After setting up your Portkey API key with the attached config, connect it to your CrewAI agents: + +```python +from crewai import Agent, LLM +from portkey_ai import PORTKEY_GATEWAY_URL + +# Configure LLM with your API key +portkey_llm = LLM( + model="gpt-4o", + base_url=PORTKEY_GATEWAY_URL, + api_key="YOUR_PORTKEY_API_KEY" +) + +# Create agent with Portkey-enabled LLM +researcher = Agent( + role="Senior Research Scientist", + goal="Discover groundbreaking insights about the assigned topic", + backstory="You are an expert researcher with deep domain knowledge.", + verbose=True, + llm=portkey_llm +) +``` + + + + + +### Step 1: Implement Budget Controls & Rate Limits + +Virtual Keys enable granular control over LLM access at the team/department level. This helps you: +- Set up [budget limits](/product/ai-gateway/virtual-keys/budget-limits) +- Prevent unexpected usage spikes using Rate limits +- Track departmental spending + +#### Setting Up Department-Specific Controls: +1. Navigate to [Virtual Keys](https://app.portkey.ai/virtual-keys) in Portkey dashboard +2. Create new Virtual Key for each department with budget limits and rate limits +3. Configure department-specific limits + + + + + + + +### Step 2: Define Model Access Rules + +As your AI usage scales, controlling which teams can access specific models becomes crucial. Portkey Configs provide this control layer with features like: + +#### Access Control Features: +- **Model Restrictions**: Limit access to specific models +- **Data Protection**: Implement guardrails for sensitive data +- **Reliability Controls**: Add fallbacks and retry logic + +#### Example Configuration: +Here's a basic configuration to route requests to OpenAI, specifically using GPT-4o: + +```json +{ + "strategy": { + "mode": "single" + }, + "targets": [ + { + "virtual_key": "YOUR_OPENAI_VIRTUAL_KEY", + "override_params": { + "model": "gpt-4o" + } + } + ] +} +``` + + Create your config on the [Configs page](https://app.portkey.ai/configs) in your Portkey dashboard. + + + Configs can be updated anytime to adjust controls without affecting running applications. + + + + + ### Step 3: Implement Access Controls + + Create User-specific API keys that automatically: + - Track usage per user/team with the help of virtual keys + - Apply appropriate configs to route requests + - Collect relevant metadata to filter logs + - Enforce access permissions + + Create API keys through: + - [Portkey App](https://app.portkey.ai/) + - [API Key Management API](/api-reference/admin-api/control-plane/api-keys/create-api-key) + + Example using Python SDK: + ```python + from portkey_ai import Portkey + + portkey = Portkey(api_key="YOUR_ADMIN_API_KEY") + + api_key = portkey.api_keys.create( + name="engineering-team", + type="organisation", + workspace_id="YOUR_WORKSPACE_ID", + defaults={ + "config_id": "your-config-id", + "metadata": { + "environment": "production", + "department": "engineering" + } + }, + scopes=["logs.view", "configs.read"] + ) + ``` + + For detailed key management instructions, see our [API Keys documentation](/api-reference/admin-api/control-plane/api-keys/create-api-key). + + + + ### Step 4: Deploy & Monitor + After distributing API keys to your team members, your enterprise-ready CrewAI setup is ready to go. Each team member can now use their designated API keys with appropriate access levels and budget controls. + + Monitor usage in Portkey dashboard: + - Cost tracking by department + - Model usage patterns + - Request volumes + - Error rates + + + + + +### Enterprise Features Now Available +**Your CrewAI integration now has:** +- Departmental budget controls +- Model access governance +- Usage tracking & attribution +- Security guardrails +- Reliability features + + +## Frequently Asked Questions + + + + Portkey adds production-readiness to CrewAI through comprehensive observability (traces, logs, metrics), reliability features (fallbacks, retries, caching), and access to 200+ LLMs through a unified interface. This makes it easier to debug, optimize, and scale your agent applications. + + + + Yes! Portkey integrates seamlessly with existing CrewAI applications. You just need to update your LLM configuration code with the Portkey-enabled version. The rest of your agent and crew code remains unchanged. + + + + Portkey supports all CrewAI features, including agents, tools, human-in-the-loop workflows, and all task process types (sequential, hierarchical, etc.). It adds observability and reliability without limiting any of the framework's functionality. + + + + Yes, Portkey allows you to use a consistent `trace_id` across multiple agents in a crew to track the entire workflow. This is especially useful for complex crews where you want to understand the full execution path across multiple agents. + + + + Portkey allows you to add custom metadata to your LLM configuration, which you can then use for filtering. Add fields like `crew_name`, `crew_type`, or `session_id` to easily find and analyze specific crew executions. + + + + Yes! Portkey uses your own API keys for the various LLM providers. It securely stores them as virtual keys, allowing you to easily manage and rotate keys without changing your code. + + + + +## Resources + + + +

Official CrewAI documentation

+
+ +

Get personalized guidance on implementing this integration

+
+
diff --git a/virtual_key_old/integrations/agents/langchain-agents.mdx b/virtual_key_old/integrations/agents/langchain-agents.mdx new file mode 100644 index 00000000..fed5c50c --- /dev/null +++ b/virtual_key_old/integrations/agents/langchain-agents.mdx @@ -0,0 +1,238 @@ +--- +title: "Langchain Agents" +--- + +## Getting Started + +### 1\. Install the required packages: + +```sh +pip install -qU langchain langchain-openai portkey-ai +``` + +### 2\. Configure your Langchain LLM objects: + +```py +from langchain_openai import ChatOpenAI, createHeaders +from portkey_ai import createHeaders, PORTKEY_GATEWAY_URL + +llm1 = ChatOpenAI( + api_key="OpenAI_API_Key", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY" + ) +) +``` + +That's all you need to do to use Portkey with Llama Index agents. Execute your agents and visit [Portkey.ai](https://portkey.ai) to observe your Agent's activity. + +## Integration Guide + +Here's a simple Google Colab notebook that demonstrates Llama Index with Portkey integration + +[![](/images/guides/colab-badge.svg)](https://colab.research.google.com/drive/1ab%5FXnSf-HR1KndEGBgXDW6RvONKoHdzL?usp=sharing) + +## Make your agents Production-ready with Portkey + +Portkey makes your Llama Index agents reliable, robust, and production-grade with its observability suite and AI Gateway. Seamlessly integrate 200+ LLMs with your Llama Index agents using Portkey. Implement fallbacks, gain granular insights into agent performance and costs, and continuously optimize your AI operationsโ€”all with just 2 lines of code. + +Let's dive deep! Let's go through each of the use cases! + +### 1\. [Interoperability](/product/ai-gateway/universal-api) + +Easily switch between 200+ LLMs. Call various LLMs such as Anthropic, Gemini, Mistral, Azure OpenAI, Google Vertex AI, AWS Bedrock, and many more by simply changing the `provider` and `API key` in the `ChatOpenAI` object. + + + +If you are using OpenAI with CrewAI, your code would look like this: + +```py +llm = ChatOpenAI( + api_key="OpenAI_API_Key", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", #choose your provider + api_key="PORTKEY_API_KEY" + ) +) +``` + +To switch to Azure as your provider, add your Azure details to Portley vault ([here's how](/integrations/llms/azure-openai)) and use Azure OpenAI using virtual keys + + +```py +llm = ChatOpenAI( + api_key="api-key", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="azure-openai", #choose your provider + api_key="PORTKEY_API_KEY", + virtual_key="AZURE_VIRTUAL_KEY" # Replace with your virtual key for Azure + ) +) +``` + + +If you are using Anthropic with CrewAI, your code would look like this: + +```py +llm = ChatOpenAI( + api_key="ANTHROPIC_API_KEY", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="anthropic", #choose your provider + api_key="PORTKEY_API_KEY" + ) +) +``` +To switch to AWS Bedrock as your provider, add your AWS Bedrock details to Portley vault ([here's how](/integrations/llms/aws-bedrock)) and use AWS Bedrock using virtual keys, +```py +llm = ChatOpenAI( + api_key="api-key", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="bedrock", #choose your provider + api_key="PORTKEY_API_KEY", + virtual_key="AWS_Bedrock_VIRTUAL_KEY" # Replace with your virtual key for Bedrock + ) +) +``` + + + +### 2\. [Reliability](/product/ai-gateway) + +Agents are _brittle_. Long agentic pipelines with multiple steps can fail at any stage, disrupting the entire process. Portkey solves this by offering built-in **fallbacks** between different LLMs or providers, **load-balancing** across multiple instances or API keys, and implementing automatic **retries** and request **timeouts**. This makes your agents more reliable and resilient. + +Here's how you can implement these features using Portkey's config + +```py +{ + "retry": { + "attempts": 5 + }, + "strategy": { + "mode": "loadbalance" // Choose between "loadbalance" or "fallback" + }, + "targets": [ + { + "provider": "openai", + "api_key": "OpenAI_API_Key" + }, + { + "provider": "anthropic", + "api_key": "Anthropic_API_Key" + } + ] +} +``` + +### 3\. [Metrics](/product/observability) + +Agent runs can be costly. Tracking agent metrics is crucial for understanding the performance and reliability of your AI agents. Metrics help identify issues, optimize runs, and ensure that your agents meet their intended goals. + +Portkey automatically logs comprehensive metrics for your AI agents, including **cost**, **tokens used**, **latency**, etc. Whether you need a broad overview or granular insights into your agent runs, Portkey's customizable filters provide the metrics you need. For agent-specific observability, add `Trace-id` to the request headers for each agent. + + + +```py +llm2 = ChatOpenAI( + api_key="Anthropic_API_Key", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key="PORTKEY_API_KEY", + provider="anthropic", + trace_id="research_agent1" #Add individual trace-id for your agent analytics + ) +) +``` + +### 4\. [Logs](/product/observability/logs) + +Agent runs are complex. Logs are essential for diagnosing issues, understanding agent behavior, and improving performance. They provide a detailed record of agent activities and tool use, which is crucial for debugging and optimizing processes. + +Portkey offers comprehensive logging features that capture detailed information about every action and decision made by your AI agents. Access a dedicated section to view records of agent executions, including parameters, outcomes, function calls, and errors. Filter logs based on multiple parameters such as trace ID, model, tokens used, and metadata. + + + + + +### 5\. [Traces](/product/observability/traces) + +With traces, you can see each agent run granularly on Portkey. Tracing your Langchain agent runs helps in debugging, performance optimzation, and visualizing how exactly your agents are running. + +### Using Traces in Langchain Agents + +#### Step 1: Import & Initialize the Portkey Langchain Callback Handler + + + +```py +from portkey_ai.langchain import LangchainCallbackHandler + +portkey_handler = LangchainCallbackHandler( + api_key="YOUR_PORTKEY_API_KEY", + metadata={ + "session_id": "session_1", # Use consistent metadata across your application + "agent_id": "research_agent_1", # Specific to the current agent + } +) + +``` + +#### Step 2: Configure Your LLM with the Portkey Callback + + + +```py +from langchain.chat_models import ChatOpenAI + +llm = ChatOpenAI( + api_key="YOUR_OPENAI_API_KEY_HERE", + callbacks=[portkey_handler], + # ... other parameters +) +``` + +With Portkey tracing, you can encapsulate the complete execution of your agent workflow. + + + + +### 6\. Guardrails + +LLMs are brittle - not just in API uptimes or their inexplicable `400`/`500` errors, but also in their core behavior. You can get a response with a `200` status code that completely errors out for your app's pipeline due to mismatched output. With Portkey's Guardrails, we now help you enforce LLM behavior in real-time with our _Guardrails on the Gateway_ pattern. + +Using Portkey's Guardrail platform, you can now verify your LLM inputs AND outputs to be adhering to your specifed checks; and since Guardrails are built on top of our [Gateway](https://github.com/portkey-ai/gateway), you can orchestrate your request exactly the way you want - with actions ranging from _denying the request_, _logging the guardrail result_, _creating an evals dataset_, _falling back to another LLM or prompt_, _retrying the request_, and more. + +### 7\. [Continuous Improvement](/product/observability/feedback) + +Improve your Agent runs by capturing qualitative & quantitative user feedback on your requests. Portkey's Feedback APIs provide a simple way to get weighted feedback from customers on any request you served, at any stage in your app. You can capture this feedback on a request or conversation level and analyze it by adding meta data to the relevant request. + +### 8\. [Caching](/product/ai-gateway/cache-simple-and-semantic) + +Agent runs are time-consuming and expensive due to their complex pipelines. Caching can significantly reduce these costs by storing frequently used data and responses. Portkey offers a built-in caching system that stores past responses, reducing the need for agent calls saving both time and money. + + + +```json +{ + "cache": { + "mode": "semantic" // Choose between "simple" or "semantic" + } +} +``` + +### 9\. [Security & Compliance](/product/enterprise-offering/security-portkey) + +Set budget limits on provider API keys and implement fine-grained user roles and permissions for both the app and the Portkey APIs. + +--- + +## [Portkey Config](/product/ai-gateway/configs) + +Many of these features are driven by Portkey's Config architecture. The Portkey app simplifies creating, managing, and versioning your Configs. + +For more information on using these features and setting up your Config, please refer to the [Portkey documentation](https://docs.portkey.ai). diff --git a/virtual_key_old/integrations/agents/langgraph.mdx b/virtual_key_old/integrations/agents/langgraph.mdx new file mode 100644 index 00000000..b7f32c65 --- /dev/null +++ b/virtual_key_old/integrations/agents/langgraph.mdx @@ -0,0 +1,1022 @@ +--- +title: "LangGraph" +description: "Use Portkey with LangGraph to take your AI agent workflows to production" +--- + +## Introduction + +LangGraph is a library for building stateful, multi-actor applications with LLMs, designed to make developing complex agent workflows easier. It provides a flexible framework to create directed graphs where nodes process information and edges define the flow between them. + +Portkey enhances LangGraph with production-readiness features, turning your experimental agent workflows into robust systems by providing: + +- **Complete observability** of every agent step, tool use, and state transition +- **Built-in reliability** with fallbacks, retries, and load balancing +- **Cost tracking and optimization** to manage your AI spend +- **Access to 200+ LLMs** through a single integration +- **Guardrails** to keep agent behavior safe and compliant +- **Version-controlled prompts** for consistent agent performance + + + Learn more about LangGraph's core concepts and features + + +### Installation & Setup + + + +```bash +pip install -U langgraph langchain langchain_openai portkey-ai +``` + +Depending on your use case, you may also need additional packages: +- For search capabilities: `pip install langchain_community` +- For memory functionality: `pip install langgraph[checkpoint]` + + + +Create a Portkey API key with optional budget/rate limits from the [Portkey dashboard](https://app.portkey.ai/). You can attach configurations for reliability, caching, and more to this key. + + + +For a simple setup, configure a LangChain ChatOpenAI instance to use Portkey: + +```python +from langchain_openai import ChatOpenAI +from portkey_ai import createHeaders, PORTKEY_GATEWAY_URL + +# Set up LangChain model with Portkey +llm = ChatOpenAI( + api_key="dummy", # This is just a placeholder + base_url="https://api.portkey.ai/v1", + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_PROVIDER_VIRTUAL_KEY", + trace_id="unique-trace-id", # Optional, for request tracing + metadata={ # Optional, for request segmentation + "app_env": "production", + "_user": "user_123" # Optional Special _user field for user analytics + } + ) +) +``` + + +**What are Virtual Keys?** Virtual keys in Portkey securely store your LLM provider API keys (OpenAI, Anthropic, etc.) in an encrypted vault. They allow for easier key rotation and budget management. [Learn more about virtual keys here](/product/ai-gateway/virtual-keys). + + + + +## Basic Agent Implementation + +Let's create a simple LangGraph chatbot using Portkey. This example shows how to set up a basic conversational agent: + +```python +from typing import Annotated +from langchain_openai import ChatOpenAI +from typing_extensions import TypedDict +from portkey_ai import createHeaders +from langgraph.graph import StateGraph +from langgraph.graph.message import add_messages + +# Define state structure with message history +class State(TypedDict): + messages: Annotated[list, add_messages] + +# Initialize graph builder +graph_builder = StateGraph(State) + +# Set up LLM with Portkey +llm = ChatOpenAI( + api_key="dummy", + base_url="https://api.portkey.ai/v1", + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_PROVIDER_VIRTUAL_KEY", + trace_id="chat-session-123" # Optional + ) +) + +# Define chatbot node function +def chatbot(state: State): + return {"messages": [llm.invoke(state["messages"])]} + +# Add node to graph and set entry/exit points +graph_builder.add_node("chatbot", chatbot) +graph_builder.set_entry_point("chatbot") +graph_builder.set_finish_point("chatbot") +graph = graph_builder.compile() + +# Function to handle streaming updates +def stream_graph_updates(user_input: str): + for event in graph.stream({"messages": [{"role": "user", "content": user_input}]}): + for value in event.values(): + print("Assistant:", value["messages"][-1].content) + +# Interactive chat loop +while True: + try: + user_input = input("User: ") + if user_input.lower() in ["quit", "exit", "q"]: + print("Goodbye!") + break + stream_graph_updates(user_input) + except Exception as e: + print(f"Error: {e}") + break +``` + +This basic implementation: +1. Creates a state graph with a message history +2. Configures a ChatOpenAI model with Portkey +3. Defines a simple chatbot node that processes messages with the LLM +4. Compiles the graph and provides a streaming interface for chat + +## Advanced Features + +### 1. Adding Tools to Your Agent + +LangGraph can be enhanced with tools to allow your agent to perform actions. Here's how to add the Tavily search tool: + +```python [expandable] +from langgraph.prebuilt import ToolNode, tools_condition +from typing import Annotated +from langchain_openai import ChatOpenAI +from typing_extensions import TypedDict +from langgraph.graph import StateGraph +from langgraph.graph.message import add_messages +from portkey_ai import createHeaders +from langchain_community.tools.tavily_search import TavilySearchResults + +class State(TypedDict): + messages: Annotated[list, add_messages] + +graph_builder = StateGraph(State) + +# Initialize the Tavily search tool +# Note: Requires TAVILY_API_KEY environment variable or passed as argument +tool = TavilySearchResults(max_results=2) +tools = [tool] + +# Set up LLM with Portkey +llm = ChatOpenAI( + api_key="dummy", + base_url="https://api.portkey.ai/v1", + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_PROVIDER_VIRTUAL_KEY", + trace_id="search-agent-session" # Optional + ) +) +# Bind tools to the LLM +llm_with_tools = llm.bind_tools(tools) + +def chatbot(state: State): + return {"messages": [llm_with_tools.invoke(state["messages"])]} + +graph_builder.add_node("chatbot", chatbot) + +# Add tool node to handle tool execution +tool_node = ToolNode(tools=[tool]) +graph_builder.add_node("tools", tool_node) + +# Add conditional routing based on tool usage +graph_builder.add_conditional_edges( + "chatbot", + tools_condition, +) +# Return to chatbot after tool execution +graph_builder.add_edge("tools", "chatbot") +graph_builder.set_entry_point("chatbot") +graph = graph_builder.compile() +``` + + +This example requires a Tavily API key for the search functionality. You can sign up for one at [Tavily's website](https://tavily.com/). + + +### 2. Creating Custom Tools + +You can create custom tools for your agents using the `@tool` decorator. Here's how to create a simple multiplication tool: + +```python [expandable] +from langchain_core.tools import tool +from pydantic import BaseModel, Field +from langgraph.prebuilt import ToolNode, tools_condition +from typing import Annotated +from langchain_openai import ChatOpenAI +from typing_extensions import TypedDict +from langgraph.graph import StateGraph +from langgraph.graph.message import add_messages +from portkey_ai import createHeaders + +class State(TypedDict): + messages: Annotated[list, add_messages] + +# Define input schema for the tool +class MultiplyInputSchema(BaseModel): + """Multiply two numbers""" + a: int = Field(description="First operand") + b: int = Field(description="Second operand") + +# Create the tool +@tool("multiply_tool", args_schema=MultiplyInputSchema) +def multiply(a: int, b: int) -> int: + return a * b + +graph_builder = StateGraph(State) + +tools = [multiply] + +# Set up LLM with Portkey +llm = ChatOpenAI( + api_key="dummy", + base_url="https://api.portkey.ai/v1", + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_PROVIDER_VIRTUAL_KEY", + ) +) +llm_with_tools = llm.bind_tools(tools) + +def chatbot(state: State): + return {"messages": [llm_with_tools.invoke(state["messages"])]} + +graph_builder.add_node("chatbot", chatbot) +tool_node = ToolNode(tools=tools) +graph_builder.add_node("tools", tool_node) + +graph_builder.add_conditional_edges( + "chatbot", + tools_condition, +) +graph_builder.add_edge("tools", "chatbot") +graph_builder.set_entry_point("chatbot") +graph = graph_builder.compile() +``` + +This example: +1. Defines a Pydantic model for the tool's input schema +2. Creates a custom multiplication tool with the `@tool` decorator +3. Integrates it into LangGraph with a tool node + +### 3. Adding Memory to Your Agent + +For persistent conversations, you can add memory to your LangGraph agents: + +```python [expandable] +from typing import Annotated +from langchain_openai import ChatOpenAI +from langchain_community.tools.tavily_search import TavilySearchResults +from typing_extensions import TypedDict +from portkey_ai import createHeaders +from langgraph.prebuilt import ToolNode, tools_condition +from langgraph.checkpoint.memory import MemorySaver +from langgraph.graph import StateGraph +from langgraph.graph.message import add_messages + +class State(TypedDict): + messages: Annotated[list, add_messages] + +graph_builder = StateGraph(State) + +# Set up tools and LLM with Portkey +tool = TavilySearchResults(max_results=2) +tools = [tool] +llm = ChatOpenAI( + api_key="dummy", + base_url="https://api.portkey.ai/v1", + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_PROVIDER_VIRTUAL_KEY", + trace_id="memory-agent-session" # Optional + ) +) +llm_with_tools = llm.bind_tools(tools) + +def chatbot(state: State): + return {"messages": [llm_with_tools.invoke(state["messages"])]} + +graph_builder.add_node("chatbot", chatbot) +tool_node = ToolNode(tools=[tool]) +graph_builder.add_node("tools", tool_node) + +graph_builder.add_conditional_edges( + "chatbot", + tools_condition, +) +graph_builder.add_edge("tools", "chatbot") +graph_builder.set_entry_point("chatbot") + +# Initialize memory saver for persistent state +memory = MemorySaver() +graph = graph_builder.compile(checkpointer=memory) + +# Configuration for memory thread id +config = {"configurable": {"thread_id": "1"}} + +# Function to handle streaming updates with memory +def stream_graph_updates(user_input: str): + events = graph.stream( + {"messages": [{"role": "user", "content": user_input}]}, + config, + stream_mode="values", + ) + for event in events: + if "messages" in event: + # Use pretty_print method for better formatting + event["messages"][-1].pretty_print() +``` + + +The `thread_id` in the config allows you to maintain separate conversation threads for different users or contexts. + + +## Production Features + +### 1. Enhanced Observability + +Portkey provides comprehensive observability for your LangGraph agents, helping you understand exactly what's happening during each execution. + + + + + + + +Traces provide a hierarchical view of your agent's execution, showing the sequence of LLM calls, tool invocations, and state transitions. + +```python +# Add trace_id to enable hierarchical tracing in Portkey +llm = ChatOpenAI( + api_key="dummy", + base_url="https://api.portkey.ai/v1", + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_LLM_PROVIDER_VIRTUAL_KEY", + trace_id="unique-session-id", # Add unique trace ID + metadata={"request_type": "user_query"} + ) +) +``` + +LangGraph also offers its own tracing via LangSmith, which can be used alongside Portkey for even more detailed workflow insights. + + + + + + + +Portkey logs every interaction with LLMs, including: + +- Complete request and response payloads +- Latency and token usage metrics +- Cost calculations +- Tool calls and function executions + +All logs can be filtered by metadata, trace IDs, models, and more, making it easy to debug specific agent runs. + + + + + + + +Portkey provides built-in dashboards that help you: + +- Track cost and token usage across all agent runs +- Analyze performance metrics like latency and success rates +- Identify bottlenecks in your agent workflows +- Compare different agent configurations and LLMs + +You can filter and segment all metrics by custom metadata to analyze specific agent types, user groups, or use cases. + + + + + Analytics with metadata filters + + +Add custom metadata to your LangGraph agent calls to enable powerful filtering and segmentation: + +```python +llm = ChatOpenAI( + api_key="dummy", + base_url="https://api.portkey.ai/v1", + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_LLM_PROVIDER_VIRTUAL_KEY", + metadata={ + "agent_type": "search_agent", + "environment": "production", + "_user": "user_123", # Special _user field for user analytics + "graph_id": "complex_workflow" + } + ) +) +``` + +This metadata can be used to filter logs, traces, and metrics on the Portkey dashboard, allowing you to analyze specific agent runs, users, or environments. + + + +### 2. Reliability - Keep Your Agents Running Smoothly + +When running agents in production, things can go wrong - API rate limits, network issues, or provider outages. Portkey's reliability features ensure your agents keep running smoothly even when problems occur. + +Enable fallback in your LangGraph agents by using a Portkey Config: + +```python +from portkey_ai import createHeaders + +# Create LLM with fallback configuration +llm = ChatOpenAI( + api_key="dummy", + base_url="https://api.portkey.ai/v1", + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + config={ + "strategy": { + "mode": "fallback" + }, + "targets": [ + { + "virtual_key": "YOUR_OPENAI_VIRTUAL_API_KEY", + "override_params": {"model": "gpt-4o"} + }, + { + "virtual_key": "YOUR_ANTHROPIC_VIRTUAL_API_KEY", + "override_params": {"model": "claude-3-opus-20240229"} + } + ] + } + ) +) +``` + +This configuration will automatically try Claude if the GPT-4o request fails, ensuring your agent can continue operating. + + + + Handles temporary failures automatically. If an LLM call fails, Portkey will retry the same request for the specified number of times - perfect for rate limits or network blips. + + + Prevent your agents from hanging. Set timeouts to ensure you get responses (or can fail gracefully) within your required timeframes. + + + Send different requests to different providers. Route complex reasoning to GPT-4, creative tasks to Claude, and quick responses to Gemini based on your needs. + + + Keep running even if your primary provider fails. Automatically switch to backup providers to maintain availability. + + + Spread requests across multiple API keys or providers. Great for high-volume agent operations and staying within rate limits. + + + +### 3. Prompting in LangGraph + +Portkey's Prompt Engineering Studio helps you create, manage, and optimize the prompts used in your LangGraph agents. Instead of hardcoding prompts or instructions, use Portkey's prompt rendering API to dynamically fetch and apply your versioned prompts. + + +![Prompt Playground Interface](/images/product/ai-gateway/ai-20.webp) + + + + +Prompt Playground is a place to compare, test and deploy perfect prompts for your AI application. It's where you experiment with different models, test variables, compare outputs, and refine your prompt engineering strategy before deploying to production. It allows you to: + +1. Iteratively develop prompts before using them in your agents +2. Test prompts with different variables and models +3. Compare outputs between different prompt versions +4. Collaborate with team members on prompt development + +This visual environment makes it easier to craft effective prompts for each step in your LangGraph agent's workflow. + + + +The Prompt Render API retrieves your prompt templates with all parameters configured: + +```python +from portkey_ai import Portkey +from langchain_openai import ChatOpenAI +from portkey_ai import createHeaders + +# Initialize Portkey clients +portkey_admin = Portkey(api_key="YOUR_PORTKEY_API_KEY") + +# Retrieve prompt using the render API +prompt_data = portkey_admin.prompts.render( + prompt_id="YOUR_PROMPT_ID", + variables={ + "user_input": "Tell me about artificial intelligence" + } +).data.dict() + +# Set up LLM with rendered system prompt +llm = ChatOpenAI( + api_key="dummy", + base_url="https://api.portkey.ai/v1", + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + ) +) + +# Define chatbot node with the rendered system prompt +def chatbot(state): + messages = state["messages"] + # Add the system prompt from Portkey to the beginning of the conversation + all_messages = [ + {"role": "system", "content": prompt_data["messages"][0]["content"]}, + *messages + ] + return {"messages": [llm.invoke(all_messages)]} +``` + + + +You can: +- Create multiple versions of the same prompt +- Compare performance between versions +- Roll back to previous versions if needed +- Specify which version to use in your code: + +```python +# Use a specific prompt version +prompt_data = portkey_admin.prompts.render( + prompt_id="YOUR_PROMPT_ID@version_number", + variables={ + "user_input": "Tell me about quantum computing" + } +) +``` + + + +Portkey prompts use Mustache-style templating for easy variable substitution: + +``` +You are an AI assistant specialized in {{agent_role}}. + +User question: {{user_input}} + +Please respond in a {{tone}} tone and include {{required_elements}}. +``` + +When rendering, simply pass the variables: + +```python +prompt_data = portkey_admin.prompts.render( + prompt_id="YOUR_PROMPT_ID", + variables={ + "agent_role": "search navigator", + "user_input": "Find information about climate change", + "tone": "informative", + "required_elements": "recent scientific findings" + } +) +``` + + + + + Learn more about Portkey's prompt management features + + +### 4. Guardrails for Safe Agents + +Guardrails ensure your LangGraph agents operate safely and respond appropriately in all situations. + +**Why Use Guardrails?** + +LangGraph agents can experience various failure modes: +- Generating harmful or inappropriate content +- Leaking sensitive information like PII +- Hallucinating incorrect information +- Generating outputs in incorrect formats + +Portkey's guardrails add protection for both inputs and outputs. + +**Implementing Guardrails** + +```python +from langchain_openai import ChatOpenAI +from portkey_ai import createHeaders + +# Create LLM with guardrails +llm = ChatOpenAI( + api_key="dummy", + base_url="https://api.portkey.ai/v1", + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + config={ + "input_guardrails": ["guardrails-id-xxx", "guardrails-id-yyy"], + "output_guardrails": ["guardrails-id-zzz"] + } + ) +) +``` + +Portkey's guardrails can: +- Detect and redact PII in both inputs and outputs +- Filter harmful or inappropriate content +- Validate response formats against schemas +- Check for hallucinations against ground truth +- Apply custom business logic and rules + + + Explore Portkey's guardrail features to enhance agent safety + + +### 5. User Tracking with Metadata + +Track individual users through your LangGraph agents using Portkey's metadata system. + +**What is Metadata in Portkey?** + +Metadata allows you to associate custom data with each request, enabling filtering, segmentation, and analytics. The special `_user` field is specifically designed for user tracking. + +```python +from langchain_openai import ChatOpenAI +from portkey_ai import createHeaders + +# Configure LLM with user tracking +llm = ChatOpenAI( + api_key="dummy", + base_url="https://api.portkey.ai/v1", + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + metadata={ + "_user": "user_123", # Special _user field for user analytics + "user_tier": "premium", + "user_company": "Acme Corp", + "session_id": "abc-123", + "graph_id": "search_workflow" + } + ) +) +``` + +**Filter Analytics by User** + +With metadata in place, you can filter analytics by user and analyze performance metrics on a per-user basis: + + + + + +This enables: +- Per-user cost tracking and budgeting +- Personalized user analytics +- Team or organization-level metrics +- Environment-specific monitoring (staging vs. production) + + + Explore how to use custom metadata to enhance your analytics + + +### 6. Caching for Efficient Agents + +Implement caching to make your LangGraph agents more efficient and cost-effective: + + + +```python +from langchain_openai import ChatOpenAI +from portkey_ai import createHeaders + +# Configure LLM with simple caching +llm = ChatOpenAI( + api_key="dummy", + base_url="https://api.portkey.ai/v1", + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + config={ + "cache": { + "mode": "simple" + } + } + ) +) +``` + +Simple caching performs exact matches on input prompts, caching identical requests to avoid redundant model executions. + + + +```python +from langchain_openai import ChatOpenAI +from portkey_ai import createHeaders + +# Configure LLM with semantic caching +llm = ChatOpenAI( + api_key="dummy", + base_url="https://api.portkey.ai/v1", + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + config={ + "cache": { + "mode": "semantic" + } + } + ) +) +``` + +Semantic caching considers the contextual similarity between input requests, caching responses for semantically similar inputs. + + + +### 7. Model Interoperability + +LangGraph works with multiple LLM providers, and Portkey extends this capability by providing access to over 200 LLMs through a unified interface. You can easily switch between different models without changing your core agent logic: + +```python +from langchain_openai import ChatOpenAI +from portkey_ai import createHeaders + +# OpenAI configuration +openai_llm = ChatOpenAI( + api_key="dummy", + base_url="https://api.portkey.ai/v1", + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_OPENAI_VIRTUAL_KEY" + ) +) + +# Anthropic configuration +anthropic_llm = ChatOpenAI( + api_key="dummy", + base_url="https://api.portkey.ai/v1", + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_ANTHROPIC_VIRTUAL_KEY" + ) +) + +# Choose which LLM to use based on your needs +active_llm = openai_llm # or anthropic_llm + +# Use in your LangGraph nodes +def chatbot(state): + return {"messages": [active_llm.invoke(state["messages"])]} +``` + +Portkey provides access to LLMs from providers including: + +- OpenAI (GPT-4o, GPT-4 Turbo, etc.) +- Anthropic (Claude 3.5 Sonnet, Claude 3 Opus, etc.) +- Mistral AI (Mistral Large, Mistral Medium, etc.) +- Google Vertex AI (Gemini 1.5 Pro, etc.) +- Cohere (Command, Command-R, etc.) +- AWS Bedrock (Claude, Titan, etc.) +- Local/Private Models + + + See the full list of LLM providers supported by Portkey + + +## Set Up Enterprise Governance for LangGraph + +**Why Enterprise Governance?** +If you are using LangGraph inside your organization, you need to consider several governance aspects: +- **Cost Management**: Controlling and tracking AI spending across teams +- **Access Control**: Managing which teams can use specific models +- **Usage Analytics**: Understanding how AI is being used across the organization +- **Security & Compliance**: Maintaining enterprise security standards +- **Reliability**: Ensuring consistent service across all users + +Portkey adds a comprehensive governance layer to address these enterprise needs. Let's implement these controls step by step. + + + +Virtual Keys are Portkey's secure way to manage your LLM provider API keys. They provide essential controls like: +- Budget limits for API usage +- Rate limiting capabilities +- Secure API key storage + +To create a virtual key: +Go to [Virtual Keys](https://app.portkey.ai/virtual-keys) in the Portkey App. Save and copy the virtual key ID + + + + + + +Save your virtual key ID - you'll need it for the next step. + + + + +Configs in Portkey define how your requests are routed, with features like advanced routing, fallbacks, and retries. + +To create your config: +1. Go to [Configs](https://app.portkey.ai/configs) in Portkey dashboard +2. Create new config with: + ```json + { + "virtual_key": "YOUR_VIRTUAL_KEY_FROM_STEP1", + "override_params": { + "model": "gpt-4o" // Your preferred model name + } + } + ``` +3. Save and note the Config name for the next step + + + + + + + +Now create a Portkey API key and attach the config you created in Step 2: + +1. Go to [API Keys](https://app.portkey.ai/api-keys) in Portkey and Create new API key +2. Select your config from `Step 2` +3. Generate and save your API key + + + + + + + +After setting up your Portkey API key with the attached config, connect it to your LangGraph agents: + +```python +from langchain_openai import ChatOpenAI +from portkey_ai import createHeaders + +# Configure LLM with your Portkey API key +llm = ChatOpenAI( + api_key="dummy", + base_url="https://api.portkey.ai/v1", + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY" # The API key with attached config from step 3 + ) +) +``` + + + + + +### Step 1: Implement Budget Controls & Rate Limits + +Virtual Keys enable granular control over LLM access at the team/department level. This helps you: +- Set up [budget limits](/product/ai-gateway/virtual-keys/budget-limits) +- Prevent unexpected usage spikes using Rate limits +- Track departmental spending + +#### Setting Up Department-Specific Controls: +1. Navigate to [Virtual Keys](https://app.portkey.ai/virtual-keys) in Portkey dashboard +2. Create new Virtual Key for each department with budget limits and rate limits +3. Configure department-specific limits + + + + + + + +### Step 2: Define Model Access Rules + +As your AI usage scales, controlling which teams can access specific models becomes crucial. Portkey Configs provide this control layer with features like: + +#### Access Control Features: +- **Model Restrictions**: Limit access to specific models +- **Data Protection**: Implement guardrails for sensitive data +- **Reliability Controls**: Add fallbacks and retry logic + +#### Example Configuration: +Here's a basic configuration to route requests to OpenAI, specifically using GPT-4o: + +```json +{ + "strategy": { + "mode": "single" + }, + "targets": [ + { + "virtual_key": "YOUR_OPENAI_VIRTUAL_KEY", + "override_params": { + "model": "gpt-4o" + } + } + ] +} +``` + + Create your config on the [Configs page](https://app.portkey.ai/configs) in your Portkey dashboard. + + + Configs can be updated anytime to adjust controls without affecting running applications. + + + + + ### Step 3: Implement Access Controls + + Create User-specific API keys that automatically: + - Track usage per user/team with the help of virtual keys + - Apply appropriate configs to route requests + - Collect relevant metadata to filter logs + - Enforce access permissions + + Create API keys through: + - [Portkey App](https://app.portkey.ai/) + - [API Key Management API](/api-reference/admin-api/control-plane/api-keys/create-api-key) + + Example using Python SDK: + ```python + from portkey_ai import Portkey + + portkey = Portkey(api_key="YOUR_ADMIN_API_KEY") + + api_key = portkey.api_keys.create( + name="engineering-team", + type="organisation", + workspace_id="YOUR_WORKSPACE_ID", + defaults={ + "config_id": "your-config-id", + "metadata": { + "environment": "production", + "department": "engineering" + } + }, + scopes=["logs.view", "configs.read"] + ) + ``` + + For detailed key management instructions, see our [API Keys documentation](/api-reference/admin-api/control-plane/api-keys/create-api-key). + + + + ### Step 4: Deploy & Monitor + After distributing API keys to your team members, your enterprise-ready LangGraph setup is ready to go. Each team member can now use their designated API keys with appropriate access levels and budget controls. + + Monitor usage in Portkey dashboard: + - Cost tracking by department + - Model usage patterns + - Request volumes + - Error rates + + + + + +### Enterprise Features Now Available +**Your LangGraph integration now has:** +- Departmental budget controls +- Model access governance +- Usage tracking & attribution +- Security guardrails +- Reliability features + + +## Frequently Asked Questions + + + + Portkey adds production-readiness to LangGraph through comprehensive observability (traces, logs, metrics), reliability features (fallbacks, retries, caching), and access to 200+ LLMs through a unified interface. This makes it easier to debug, optimize, and scale your agent workflows. + + + + Yes! Portkey integrates seamlessly with existing LangGraph applications. You just need to replace your LLM initialization code with the Portkey-enabled version. The rest of your graph code remains unchanged. + + + + Portkey supports all LangGraph features, including tools, memory, conditional routing, and complex multi-node workflows. It adds observability and reliability without limiting any of the framework's functionality. + + + + Portkey allows you to add custom metadata and trace IDs to your LLM calls, which you can then use for filtering. Add fields like `graph_id`, `workflow_type`, or `session_id` to easily find and analyze specific graph executions. + + + + Yes! The examples in this documentation show how to use LangGraph's `MemorySaver` checkpointer with Portkey-enabled LLMs. All the memory and state management features work seamlessly with Portkey. + + + + +## Resources + + + +

Official LangGraph documentation

+
+ + +

Official Portkey documentation

+
+ + +

Get personalized guidance on implementing this integration

+
+
diff --git a/virtual_key_old/integrations/agents/langroid.mdx b/virtual_key_old/integrations/agents/langroid.mdx new file mode 100644 index 00000000..1dcdbe90 --- /dev/null +++ b/virtual_key_old/integrations/agents/langroid.mdx @@ -0,0 +1,4 @@ +--- +title: Langroid +url: https://langroid.github.io/langroid/notes/portkey +--- diff --git a/virtual_key_old/integrations/agents/livekit.mdx b/virtual_key_old/integrations/agents/livekit.mdx new file mode 100644 index 00000000..e7a1b131 --- /dev/null +++ b/virtual_key_old/integrations/agents/livekit.mdx @@ -0,0 +1,484 @@ +--- +title: 'LiveKit' +description: "Build production-ready voice AI agents with Portkey's enterprise features" +--- + + +**Realtime API support is coming soon!** Join our [Discord community](https://portkey.sh/discord) to be the first to know when LiveKit's realtime model integration with Portkey is available. + + +LiveKit is a powerful platform for building real-time voice and video applications. When combined with Portkey, you get enterprise-grade features that make your LiveKit voice agents production-ready: + +- **Unified AI Gateway** - Single interface for 250+ LLMs with API key management +- **Centralized AI observability**: Real-time usage tracking for 40+ key metrics and logs for every request +- **Governance** - Real-time spend tracking, set budget limits and RBAC in your LiveKit agents +- **Security Guardrails** - PII detection, content filtering, and compliance controls + +This guide will walk you through integrating Portkey with LiveKit's STT-LLM-TTS pipeline to build enterprise-ready voice AI agents. + + + If you are an enterprise looking to deploy LiveKit agents in production, [check out this section](#3-set-up-enterprise-governance-for-livekit). + + +# 1. Setting up Portkey +Portkey allows you to use 250+ LLMs with your LiveKit agents, with minimal configuration required. Let's set up the core components in Portkey that you'll need for integration. + + + +Virtual Keys are Portkey's secure way to manage your LLM provider API keys. For LiveKit integration, you'll need to create a virtual key for OpenAI (or any other LLM provider you prefer). + +To create a virtual key: +1. Go to [Virtual Keys](https://app.portkey.ai/virtual-keys) in the Portkey App +2. Click "Add Virtual Key" and select OpenAI as the provider +3. Add your OpenAI API key +4. Save and copy the virtual key ID + + + + + + +Save your virtual key ID - you'll need it for the next step. + + + + + +Configs in Portkey define how your requests are routed and can enable features like fallbacks, caching, and more. + +To create your config: +1. Go to [Configs](https://app.portkey.ai/configs) in Portkey dashboard +2. Create new config with: + ```json + { + "virtual_key": "YOUR_VIRTUAL_KEY_FROM_STEP1" + } + ``` +3. Save and note the Config ID for the next step + + + + + + +This basic config connects to your virtual key. You can add advanced features like caching, fallbacks, and guardrails later. + + + + + +Now create a Portkey API key and attach the config you created: + +1. Go to [API Keys](https://app.portkey.ai/api-keys) in Portkey +2. Create new API key +3. Select your config from Step 2 +4. Generate and save your API key + + + + + + +Save your API key securely - you'll need it for LiveKit integration. + + + + +# 2. Integrate Portkey with LiveKit + +Now that you have your Portkey components set up, let's integrate them with LiveKit agents. + +## Installation + +Install the required packages: + +```bash +pip install \ + "livekit-agents[openai]~=1.0" +``` + +## Configuration + +```python +llm=openai.LLM(model="gpt-4o", # your preferred model + api_key="YOUR_PORTKEY_API_KEY", # you can also set OPENAI_API_KEY= in .env + base_url="https://api.portkey.ai/v1", # Portkey Base Url + ), +``` + + +Make sure your Portkey virtual key has sufficient budget and rate limits for your expected usage. + + + + + +## End-to-End Example using Portkey and LiveKit +Build a simple voice assistant with Python in less than 10 minutes. + + + + + + ```bash + pip install \ + "livekit-agents[deepgram,openai,cartesia,silero,turn-detector]~=1.0" \ + "livekit-plugins-noise-cancellation~=0.2" \ + "python-dotenv" + ``` + + + + ```bash + DEEPGRAM_API_KEY= + OPENAI_API_KEY= + CARTESIA_API_KEY= + LIVEKIT_API_KEY= + LIVEKIT_API_SECRET= + LIVEKIT_URL= + ``` + + + + ```py + from dotenv import load_dotenv + + from livekit import agents + from livekit.agents import AgentSession, Agent, RoomInputOptions + from livekit.plugins import ( + openai, + cartesia, + deepgram, + noise_cancellation, + silero, + ) + from livekit.plugins.turn_detector.multilingual import MultilingualModel + + load_dotenv() + + + class Assistant(Agent): + def __init__(self) -> None: + super().__init__(instructions="You are a helpful voice AI assistant.") + + + async def entrypoint(ctx: agents.JobContext): + session = AgentSession( + stt=deepgram.STT(model="nova-3", language="multi"), + llm=openai.LLM(model="gpt-4o", + api_key="YOUR_PORTKEY_API_KEY", + base_url="https://api.portkey.ai/v1", + ), + tts=cartesia.TTS(), + vad=silero.VAD.load(), + turn_detection=MultilingualModel(), + ) + + await session.start( + room=ctx.room, + agent=Assistant(), + room_input_options=RoomInputOptions( + # LiveKit Cloud enhanced noise cancellation + # - If self-hosting, omit this parameter + # - For telephony applications, use `BVCTelephony` for best results + noise_cancellation=noise_cancellation.BVC(), + ), + ) + + await ctx.connect() + + await session.generate_reply( + instructions="Greet the user and offer your assistance." + ) + + + if __name__ == "__main__": + agents.cli.run_app(agents.WorkerOptions(entrypoint_fnc=entrypoint)) + ``` + + + + + + + + +# 3. Set Up Enterprise Governance for Livekit + +**Why Enterprise Governance?** +If you are using Livekit inside your orgnaization, you need to consider several governance aspects: +- **Cost Management**: Controlling and tracking AI spending across teams +- **Access Control**: Managing which teams can use specific models +- **Usage Analytics**: Understanding how AI is being used across the organization +- **Security & Compliance**: Maintaining enterprise security standards +- **Reliability**: Ensuring consistent service across all users + +Portkey adds a comprehensive governance layer to address these enterprise needs. Let's implement these controls step by step. + +**Enterprise Implementation Guide** + + + +### Step 1: Implement Budget Controls & Rate Limits + +Virtual Keys enable granular control over LLM access at the team/department level. This helps you: +- Set up [budget limits](/product/ai-gateway/virtual-keys/budget-limits) +- Prevent unexpected usage spikes using Rate limits +- Track departmental spending + +#### Setting Up Department-Specific Controls: +1. Navigate to [Virtual Keys](https://app.portkey.ai/virtual-keys) in Portkey dashboard +2. Create new Virtual Key for each department with budget limits and rate limits +3. Configure department-specific limits + + + + + + + + +### Step 2: Define Model Access Rules + +As your AI usage scales, controlling which teams can access specific models becomes crucial. Portkey Configs provide this control layer with features like: + +#### Access Control Features: +- **Model Restrictions**: Limit access to specific models +- **Data Protection**: Implement guardrails for sensitive data +- **Reliability Controls**: Add fallbacks and retry logic + +#### Example Configuration: +Here's a basic configuration to route requests to OpenAI, specifically using GPT-4o: + +```json +{ + "strategy": { + "mode": "single" + }, + "targets": [ + { + "virtual_key": "YOUR_OPENAI_VIRTUAL_KEY", + "override_params": { + "model": "gpt-4o" + } + } + ] +} +``` + +Create your config on the [Configs page](https://app.portkey.ai/configs) in your Portkey dashboard. You'll need the config ID for connecting to Livekit's setup. + + + +Configs can be updated anytime to adjust controls without affecting running applications. + + + + +### Step 3: Implement Access Controls + +Create User-specific API keys that automatically: +- Track usage per user/team with the help of virtual keys +- Apply appropriate configs to route requests +- Collect relevant metadata to filter logs +- Enforce access permissions + + +Create API keys through: +- [Portkey App](https://app.portkey.ai/) +- [API Key Management API](/api-reference/admin-api/control-plane/api-keys/create-api-key) + + +Example using Python SDK: +```python +from portkey_ai import Portkey + +portkey = Portkey(api_key="YOUR_ADMIN_API_KEY") + +api_key = portkey.api_keys.create( + name="engineering-team", + type="organisation", + workspace_id="YOUR_WORKSPACE_ID", + defaults={ + "config_id": "your-config-id", + "metadata": { + "environment": "production", + "department": "engineering" + } + }, + scopes=["logs.view", "configs.read"] +) +``` + +For detailed key management instructions, see our [API Keys documentation](/api-reference/admin-api/control-plane/api-keys/create-api-key). + + + +### Step 4: Deploy & Monitor +After distributing API keys to your team members, your enterprise-ready Livekit setup is ready to go. Each team member can now use their designated API keys with appropriate access levels and budget controls. +Apply your governance setup using the integration steps from earlier sections +Monitor usage in Portkey dashboard: +- Cost tracking by department +- Model usage patterns +- Request volumes +- Error rates + + + + + +### Enterprise Features Now Available +**Livekit now has:** + +- Departmental budget controls +- Model access governance +- Usage tracking & attribution +- Security guardrails +- Reliability features + + + + + + +# Portkey Features +Now that you have enterprise-grade Livekit setup, let's explore the comprehensive features Portkey provides to ensure secure, efficient, and cost-effective AI operations. + +### 1. Comprehensive Metrics +Using Portkey you can track 40+ key metrics including cost, token usage, response time, and performance across all your LLM providers in real time. You can also filter these metrics based on custom metadata that you can set in your configs. Learn more about metadata here. + + + + + +### 2. Advanced Logs +Portkey's logging dashboard provides detailed logs for every request made to your LLMs. These logs include: +- Complete request and response tracking +- Metadata tags for filtering +- Cost attribution and much more... + + + + + + + + +### 3. Unified Access to 1600+ LLMs + +You can easily switch between 1600+ LLMs. Call various LLMs such as Anthropic, Gemini, Mistral, Azure OpenAI, Google Vertex AI, AWS Bedrock, and many more by simply changing the `virtual key` in your default `config` object. + + +### 4. Advanced Metadata Tracking +Using Portkey, you can add custom metadata to your LLM requests for detailed tracking and analytics. Use metadata tags to filter logs, track usage, and attribute costs across departments and teams. + + + + + + +### 5. Enterprise Access Management + + + +Set and manage spending limits across teams and departments. Control costs with granular budget limits and usage tracking. + + + +Enterprise-grade SSO integration with support for SAML 2.0, Okta, Azure AD, and custom providers for secure authentication. + + + +Hierarchical organization structure with workspaces, teams, and role-based access control for enterprise-scale deployments. + + + +Comprehensive access control rules and detailed audit logging for security compliance and usage tracking. + + + + +### 6. Reliability Features + + + Automatically switch to backup targets if the primary target fails. + + + Route requests to different targets based on specified conditions. + + + Distribute requests across multiple targets based on defined weights. + + + Enable caching of responses to improve performance and reduce costs. + + +Automatic retry handling with exponential backoff for failed requests + + + Set and manage budget limits across teams and departments. Control costs with granular budget limits and usage tracking. + + + + + +### 7. Advanced Guardrails + +Protect your Project's data and enhance reliability with real-time checks on LLM inputs and outputs. Leverage guardrails to: +- Prevent sensitive data leaks +- Enforce compliance with organizational policies +- PII detection and masking +- Content filtering +- Custom security rules +- Data compliance checks + + +Implement real-time protection for your LLM interactions with automatic detection and filtering of sensitive content, PII, and custom security rules. Enable comprehensive data protection while maintaining compliance with organizational policies. + + + +# FAQs + + + Yes! Portkey supports 250+ LLMs. Simply create a virtual key for your preferred provider (Anthropic, Google, Cohere, etc.) and update your config accordingly. The LiveKit OpenAI client will work seamlessly with any provider through Portkey. + + + + Use metadata tags when creating Portkey headers to segment costs: + - Add `agent_type`, `department`, or `customer_id` tags + - View costs filtered by these tags in the Portkey dashboard + - Set up separate virtual keys with budget limits for each use case + + + + Configure fallbacks in your Portkey config to automatically switch to backup providers. Your LiveKit agents will continue working without any code changes or downtime. + + + + Yes! Use Portkey's hooks and guardrails to: + - Filter sensitive information + - Add custom headers or modify requests + - Implement business-specific validation + - Route requests based on custom logic + + + + Migration is simple: + 1. Create virtual keys and configs in Portkey + 2. Update the OpenAI client initialization to use Portkey's base URL + 3. Add Portkey headers with your API key and config + 4. No other code changes needed! + + + +# Next Steps + +**Ready to build production voice AI?** +- [Join our Discord](https://portkey.sh/discord) for support and updates +- [Explore more integrations](/integrations/libraries) +- [Read about advanced configs](/product/ai-gateway/configs) +- [Learn about guardrails](/product/guardrails) + + +For enterprise support and custom features for your LiveKit deployment, contact our [enterprise team](https://calendly.com/portkey-ai). + diff --git a/virtual_key_old/integrations/agents/llama-agents.mdx b/virtual_key_old/integrations/agents/llama-agents.mdx new file mode 100644 index 00000000..7df70852 --- /dev/null +++ b/virtual_key_old/integrations/agents/llama-agents.mdx @@ -0,0 +1,251 @@ +--- +title: "Llama Agents by Llamaindex" +description: "Use Portkey with Llama Agents to take your AI Agents to production" +--- + +## Getting Started + +### 1\. Install the required packages: + +```sh +pip install -qU llama-agents llama-index portkey-ai +``` + +### 2\. Configure your Llama Index LLM objects: + +```py +from llama_index.llms.openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +gpt_4o_config = { + "provider": "openai", #Use the provider of choice + "api_key": "YOUR_OPENAI_KEY, + "override_params": { "model":"gpt-4o" } +} + +gpt_4o = OpenAI( + api_base=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key=userdata.get('PORTKEY_API_KEY'), + config=gpt_4o_config + ) +) +``` + +That's all you need to do to use Portkey with Llama Index agents. Execute your agents and visit [Portkey.ai](https://portkey.ai) to observe your Agent's activity. + +## Integration Guide + +Here's a simple Google Colab notebook that demonstrates Llama Index with Portkey integration + +[![](/images/guides/colab-badge.svg)](https://git.new/llama-agents) + +## Make your agents Production-ready with Portkey + +Portkey makes your Llama Index agents reliable, robust, and production-grade with its observability suite and AI Gateway. Seamlessly integrate 200+ LLMs with your Llama Index agents using Portkey. Implement fallbacks, gain granular insights into agent performance and costs, and continuously optimize your AI operationsโ€”all with just 2 lines of code. + +Let's dive deep! Let's go through each of the use cases! + +### 1\. [Interoperability](/product/ai-gateway/universal-api) + +Easily switch between 200+ LLMs. Call various LLMs such as Anthropic, Gemini, Mistral, Azure OpenAI, Google Vertex AI, AWS Bedrock, and many more by simply changing the `provider ` and `API key` in the `ChatOpenAI` object. + + + + If you are using OpenAI with autogen, your code would look like this: +```py +llm_config = { + "provider": "openai", #Use the provider of choice + "api_key": "YOUR_OPENAI_KEY, + "override_params": { "model":"gpt-4o" } +} + +llm = OpenAI( + api_base=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key="PORTKEY_API_KEY", + config=llm_config + ) +) +``` +To switch to Azure as your provider, add your Azure details to Portley vault ([here's how](/integrations/llms/azure-openai)) and use Azure OpenAI using virtual keys + + +```py +llm_config = { + provider="azure-openai", #choose your provider + "api_key": "YOUR_OPENAI_KEY, + "override_params": { "model":"gpt-4o" } +} + +llm = OpenAI( + api_base=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key="PORTKEY_API_KEY", + config=llm_config + ) +) +``` + + +If you are using Anthropic with CrewAI, your code would look like this: +```py +llm_config = { + "provider": "anthropic", #Use the provider of choice + "api_key": "YOUR_OPENAI_KEY, + "override_params": { "model":"claude-3-5-sonnet-20240620" } +} + +llm = OpenAI( + api_base=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key="PORTKEY_API_KEY", + config=llm_config + ) +) +``` +To switch to AWS Bedrock as your provider, add your AWS Bedrock details to Portley vault ([here's how](/integrations/llms/aws-bedrock)) and use AWS Bedrock using virtual keys, + +```py +llm_config = { + "provider": "bedrock", #Use the provider of choice + "api_key": "YOUR_AWS_KEY", + "override_params": { "model":"gpt-4o" } +} + +llm = OpenAI( + api_base=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key="PORTKEY_API_KEY", + config=llm_config + ) +) +``` + + + +### 2\. [Reliability](/product/ai-gateway) + +Agents are _brittle_. Long agentic pipelines with multiple steps can fail at any stage, disrupting the entire process. Portkey solves this by offering built-in **fallbacks** between different LLMs or providers, **load-balancing** across multiple instances or API keys, and implementing automatic **retries** and request **timeouts**. This makes your agents more reliable and resilient. + +Here's how you can implement these features using Portkey's config + + + +```py +{ + "retry": { + "attempts": 5 + }, + "strategy": { + "mode": "loadbalance" // Choose between "loadbalance" or "fallback" + }, + "targets": [ + { + "provider": "openai", + "api_key": "OpenAI_API_Key" + }, + { + "provider": "anthropic", + "api_key": "Anthropic_API_Key" + } + ] +} +``` + +### 3\. [Metrics](/product/observability) + +Agent runs can be costly. Tracking agent metrics is crucial for understanding the performance and reliability of your AI agents. Metrics help identify issues, optimize runs, and ensure that your agents meet their intended goals. + +Portkey automatically logs comprehensive metrics for your AI agents, including **cost**, **tokens used**, **latency**, etc. Whether you need a broad overview or granular insights into your agent runs, Portkey's customizable filters provide the metrics you need. For agent-specific observability, add `Trace-id` to the request headers for each agent. + + + +```py +llm2 = ChatOpenAI( + api_key="Anthropic_API_Key", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key="PORTKEY_API_KEY", + provider="anthropic", + trace_id="research_agent1" #Add individual trace-id for your agent analytics + ) +) +``` + +### 4\. [Logs](/product/observability/logs) + +Agent runs are complex. Logs are essential for diagnosing issues, understanding agent behavior, and improving performance. They provide a detailed record of agent activities and tool use, which is crucial for debugging and optimizing processes. + +Portkey offers comprehensive logging features that capture detailed information about every action and decision made by your AI agents. Access a dedicated section to view records of agent executions, including parameters, outcomes, function calls, and errors. Filter logs based on multiple parameters such as trace ID, model, tokens used, and metadata. + + + + +### 5\. [Traces](/product/observability/traces) + +With traces, you can see each agent run granularly on Portkey. Tracing your LlamaIndex agent runs helps in debugging, performance optimzation, and visualizing how exactly your agents are running. + +### Using Traces in LlamaIndex Agents + +#### Step 1: Import & Initialize the Portkey LlamaIndex Callback Handler + + + +```py +from portkey_ai.llamaindex import LlamaIndexCallbackHandler + +portkey_handler = LlamaIndexCallbackHandler( + api_key="YOUR_PORTKEY_API_KEY", + metadata={ + "session_id": "session_1", # Use consistent metadata across your application + "agent_id": "research_agent_1", # Specific to the current agent + } +) +``` + +#### Step 2: Configure Your LLM with the Portkey Callback + + + +```py +from llama_index.llms.openai import OpenAI + +llm = OpenAI( + api_key="YOUR_OPENAI_API_KEY_HERE", + callbacks=[portkey_handler], # Replace with your OpenAI API key + # ... other parameters +) +``` + +With Portkey tracing, you can encapsulate the complete execution of your agent workflow. + +### 6\. [Continuous Improvement](/product/observability/feedback) + +Improve your Agent runs by capturing qualitative & quantitative user feedback on your requests. Portkey's Feedback APIs provide a simple way to get weighted feedback from customers on any request you served, at any stage in your app. You can capture this feedback on a request or conversation level and analyze it by adding meta data to the relevant request. + +### 7\. [Caching](/product/ai-gateway/cache-simple-and-semantic) + +Agent runs are time-consuming and expensive due to their complex pipelines. Caching can significantly reduce these costs by storing frequently used data and responses. Portkey offers a built-in caching system that stores past responses, reducing the need for agent calls saving both time and money. + + + +```py +{ + "cache": { + "mode": "semantic" // Choose between "simple" or "semantic" + } +} +``` + +### 8\. [Security & Compliance](/product/enterprise-offering/security-portkey) + +Set budget limits on provider API keys and implement fine-grained user roles and permissions for both the app and the Portkey APIs. + +--- + +## [Portkey Config](/product/ai-gateway/configs) + +Many of these features are driven by Portkey's Config architecture. The Portkey app simplifies creating, managing, and versioning your Configs. + +For more information on using these features and setting up your Config, please refer to the [Portkey documentation](https://docs.portkey.ai). diff --git a/virtual_key_old/integrations/agents/openai-agents-ts.mdx b/virtual_key_old/integrations/agents/openai-agents-ts.mdx new file mode 100644 index 00000000..dba87cb7 --- /dev/null +++ b/virtual_key_old/integrations/agents/openai-agents-ts.mdx @@ -0,0 +1,923 @@ +--- +title: "OpenAI Agents SDK (TypeScript)" +description: "Use Portkey with OpenAI Agents SDK to take your AI Agents to production" +--- + +## Introduction +OpenAI Agents SDK enables the development of complex AI agents with tools, planning, and memory capabilities. Portkey enhances OpenAI Agents with observability, reliability, and production-readiness features. + +Portkey turns your experimental OpenAI Agents into production-ready systems by providing: + +- **Complete observability** of every agent step, tool use, and interaction +- **Built-in reliability** with fallbacks, retries, and load balancing +- **Cost tracking and optimization** to manage your AI spend +- **Access to 200+ LLMs** through a single integration +- **Guardrails** to keep agent behavior safe and compliant +- **Version-controlled prompts** for consistent agent performance + + + Learn more about OpenAI Agents SDK's core concepts + + +### Installation & Setup + + + +```bash +npm install @openai/agents portkey-ai +``` + + + Create a Portkey API key with optional budget/rate limits and attach your Config + + + + There are 3 ways to integrate Portkey with OpenAI Agents: + + 1. Set a client that applies to all agents in your application + 2. Use a custom provider for selective Portkey integration + 3. Configure each agent individually + + See the [Integration Approaches](#integration-approaches) section for more details. + + + For a simple setup, we'll use the global client approach: + +```typescript +import { Agent, run } from '@openai/agents'; +import { setDefaultOpenAIClient, setOpenAIAPI, setTracingDisabled } from '@openai/agents'; +import { OpenAI } from 'openai'; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'; + +// Set up Portkey as the global client +const portkey = new OpenAI({ + baseURL: PORTKEY_GATEWAY_URL, + apiKey: process.env.PORTKEY_API_KEY!, + defaultHeaders: createHeaders({ + virtualKey: "YOUR_OPENAI_VIRTUAL_KEY" + }) +}); + +// Register as the SDK-wide default +setDefaultOpenAIClient(portkey); +setOpenAIAPI('chat_completions'); // Responses API โ†’ Chat +setTracingDisabled(true); // Optional: disable OpenAI's tracing +``` + + +**What are Virtual Keys?** Virtual keys in Portkey securely store your LLM provider API keys (OpenAI, Anthropic, etc.) in an encrypted vault. They allow for easier key rotation and budget management. [Learn more about virtual keys here](/product/ai-gateway/virtual-keys). + + + + +### Getting Started + +Let's create a simple question-answering agent with OpenAI Agents SDK and Portkey. This agent will respond directly to user messages using a language model: + +```typescript +import { Agent, run } from '@openai/agents'; +import { setDefaultOpenAIClient, setOpenAIAPI, setTracingDisabled } from '@openai/agents'; +import { OpenAI } from 'openai'; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'; + +// Set up Portkey as the global client +const portkey = new OpenAI({ + baseURL: PORTKEY_GATEWAY_URL, + apiKey: process.env.PORTKEY_API_KEY!, + defaultHeaders: createHeaders({ + virtualKey: "YOUR_OPENAI_VIRTUAL_KEY" + }) +}); + +// Register as the SDK-wide default +setDefaultOpenAIClient(portkey); +setOpenAIAPI('chat_completions'); // Responses API โ†’ Chat +setTracingDisabled(true); // Optional: disable OpenAI's tracing + +// Create agent with any supported model +const agent = new Agent({ + name: "Assistant", + instructions: "You are a helpful assistant.", + model: "gpt-4o" +}); + +// Run the agent +const result = await run(agent, "Tell me about quantum computing."); +console.log(result.finalOutput); +``` + +In this example: +1. We set up Portkey as the global client for OpenAI Agents SDK +2. We create a simple agent with instructions and a model +3. We run the agent with a user query +4. We print the final output + +Visit your Portkey dashboard to see detailed logs of this agent's execution! + +## End-to-End Example + +**Research Agent with Tools**: Here's a more comprehensive agent that can use tools to perform tasks. + +```typescript [expandable] +import { Agent, run, tool } from '@openai/agents'; +import { setDefaultOpenAIClient } from '@openai/agents'; +import { OpenAI } from 'openai'; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'; +import { z } from 'zod'; + +// Configure Portkey client +const portkey = new OpenAI({ + apiKey: process.env.PORTKEY_API_KEY!, + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + virtualKey: "YOUR_OPENAI_VIRTUAL_KEY" + }) +}); +setDefaultOpenAIClient(portkey); + +// Define agent tools using the tool() helper +const getWeatherTool = tool({ + name: 'get_weather', + description: 'Get the current weather for a given city', + parameters: z.object({ + city: z.string(), + unit: z.enum(['celsius', 'fahrenheit']).nullable().optional() + }), + async execute({ city, unit = 'fahrenheit' }) { + return `The weather in ${city} is 72ยฐ${unit === 'celsius' ? 'C' : 'F'} and sunny.`; + } +}); + +const searchWebTool = tool({ + name: 'search_web', + description: 'Search the web for information', + parameters: z.object({ + query: z.string() + }), + async execute({ query }) { + return `Found information about: ${query}`; + } +}); + +// Create agent with tools +const agent = new Agent({ + name: "Research Assistant", + instructions: "You are a helpful assistant that can search for information and check the weather.", + model: "gpt-4o", + tools: [getWeatherTool, searchWebTool] +}); + +// Run the agent +const result = await run( + agent, + "What's the weather in San Francisco and find information about Golden Gate Bridge?" +); +console.log(result.finalOutput); +``` + +Visit your Portkey dashboard to see the complete execution flow visualized! + +--- + +## Production Features + +### 1. Enhanced Observability + +Portkey provides comprehensive observability for your OpenAI Agents, helping you understand exactly what's happening during each execution. + + + + + + + + +Traces provide a hierarchical view of your agent's execution, showing the sequence of LLM calls, tool invocations, and state transitions. + +```typescript +// Add tracing to your OpenAI Agents +const portkey = new OpenAI({ + baseURL: PORTKEY_GATEWAY_URL, + apiKey: process.env.PORTKEY_API_KEY!, + defaultHeaders: createHeaders({ + traceId: "unique_execution_trace_id", // Add unique trace ID + virtualKey: "YOUR_OPENAI_VIRTUAL_KEY" + }) +}); +setDefaultOpenAIClient(portkey); +``` + + + + + + + + + Portkey logs every interaction with LLMs, including: + + - Complete request and response payloads + - Latency and token usage metrics + - Cost calculations + - Tool calls and function executions + + All logs can be filtered by metadata, trace IDs, models, and more, making it easy to debug specific agent runs. + + + + + + + + Portkey provides built-in dashboards that help you: + + - Track cost and token usage across all agent runs + - Analyze performance metrics like latency and success rates + - Identify bottlenecks in your agent workflows + - Compare different agent configurations and LLMs + + You can filter and segment all metrics by custom metadata to analyze specific agent types, user groups, or use cases. + + + + + Analytics with metadata filters + + +Add custom metadata to your OpenAI agent calls to enable powerful filtering and segmentation: + +```typescript +// Add metadata to your OpenAI Agents +const portkey = new OpenAI({ + baseURL: PORTKEY_GATEWAY_URL, + apiKey: process.env.PORTKEY_API_KEY!, + defaultHeaders: createHeaders({ + metadata: {"agent_type": "research_agent"}, // Add custom metadata + virtualKey: "YOUR_OPENAI_VIRTUAL_KEY" + }) +}); +setDefaultOpenAIClient(portkey); +``` + +This metadata can be used to filter logs, traces, and metrics on the Portkey dashboard, allowing you to analyze specific agent runs, users, or environments. + + + +### 2. Reliability - Keep Your Agents Running Smoothly + +When running agents in production, things can go wrong - API rate limits, network issues, or provider outages. Portkey's reliability features ensure your agents keep running smoothly even when problems occur. + +It's this simple to enable fallback in your OpenAI Agents: + +```typescript +import { createHeaders, PORTKEY_GATEWAY_URL } from 'portkey-ai'; +import { OpenAI } from 'openai'; +import { setDefaultOpenAIClient } from '@openai/agents'; + +// Create a config with fallbacks, It's recommended that you create the Config in Portkey App rather than hard-code the config JSON directly +const config = { + "strategy": { + "mode": "fallback" + }, + "targets": [ + { + "provider": "openai", + "override_params": {"model": "gpt-4o"} + }, + { + "provider": "anthropic", + "override_params": {"model": "claude-3-opus-20240229"} + } + ] +}; + +// Configure Portkey client with fallback config +const portkey = new OpenAI({ + baseURL: PORTKEY_GATEWAY_URL, + apiKey: process.env.PORTKEY_API_KEY!, + defaultHeaders: createHeaders({ + config: config + }) +}); +setDefaultOpenAIClient(portkey); +``` + +This configuration will automatically try Claude if the GPT-4o request fails, ensuring your agent can continue operating. + + + + Handles temporary failures automatically. If an LLM call fails, Portkey will retry the same request for the specified number of times - perfect for rate limits or network blips. + + + Prevent your agents from hanging. Set timeouts to ensure you get responses (or can fail gracefully) within your required timeframes. + + + Send different requests to different providers. Route complex reasoning to GPT-4, creative tasks to Claude, and quick responses to Gemini based on your needs. + + + Keep running even if your primary provider fails. Automatically switch to backup providers to maintain availability. + + + Spread requests across multiple API keys or providers. Great for high-volume agent operations and staying within rate limits. + + + +### 3. Prompting in OpenAI Agents + +Portkey's Prompt Engineering Studio helps you create, manage, and optimize the prompts used in your OpenAI Agents. Instead of hardcoding prompts or instructions, use Portkey's prompt rendering API to dynamically fetch and apply your versioned prompts. + + +![Prompt Playground Interface](/images/product/ai-gateway/ai-20.webp) + + + + + +Prompt Playground is a place to compare, test and deploy perfect prompts for your AI application. It's where you experiment with different models, test variables, compare outputs, and refine your prompt engineering strategy before deploying to production. It allows you to: + +1. Iteratively develop prompts before using them in your agents +2. Test prompts with different variables and models +3. Compare outputs between different prompt versions +4. Collaborate with team members on prompt development + +This visual environment makes it easier to craft effective prompts for each step in your OpenAI Agents agent's workflow. + + + + +The Prompt Render API retrieves your prompt templates with all parameters configured: + +```typescript +import { OpenAI } from 'openai'; +import { Portkey } from 'portkey-ai'; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'; +import { Agent, run, setDefaultOpenAIClient } from '@openai/agents'; + +// Initialize Portkey client +const portkeyClient = new Portkey({ + apiKey: process.env.PORTKEY_API_KEY! +}); + +// Retrieve prompt using the render API +const promptData = await portkeyClient.prompts.render({ + promptId: "YOUR_PROMPT_ID", + variables: { + user_input: "Tell me about artificial intelligence" + } +}); + +// Configure OpenAI client with Portkey +const openaiClient = new OpenAI({ + baseURL: PORTKEY_GATEWAY_URL, + apiKey: process.env.PORTKEY_API_KEY!, + defaultHeaders: createHeaders({ + virtualKey: "YOUR_OPENAI_VIRTUAL_KEY" + }) +}); +setDefaultOpenAIClient(openaiClient); + +// Use the rendered prompt in your OpenAI Agent +const agent = new Agent({ + name: "Assistant", + instructions: promptData.data.messages[0].content, // Use the rendered prompt as instructions + model: "gpt-4o" +}); + +const result = await run(agent, "Tell me about artificial intelligence"); +console.log(result.finalOutput); +``` + + + + +You can: +- Create multiple versions of the same prompt +- Compare performance between versions +- Roll back to previous versions if needed +- Specify which version to use in your code: + +```typescript +// Use a specific prompt version +const promptData = await portkeyClient.prompts.render({ + promptId: "YOUR_PROMPT_ID@version_number", + variables: { + user_input: "Tell me about quantum computing" + } +}); +``` + + + + +Portkey prompts use Mustache-style templating for easy variable substitution: + +``` +You are an AI assistant helping with {{task_type}}. + +User question: {{user_input}} + +Please respond in a {{tone}} tone and include {{required_elements}}. +``` + +When rendering, simply pass the variables: + +```typescript +const promptData = await portkeyClient.prompts.render({ + promptId: "YOUR_PROMPT_ID", + variables: { + task_type: "research", + user_input: "Tell me about quantum computing", + tone: "professional", + required_elements: "recent academic references" + } +}); +``` + + + + + Learn more about Portkey's prompt management features + + +### 4. Guardrails for Safe Agents + +Guardrails ensure your OpenAI Agents operate safely and respond appropriately in all situations. + +**Why Use Guardrails?** + +OpenAI Agents can experience various failure modes: +- Generating harmful or inappropriate content +- Leaking sensitive information like PII +- Hallucinating incorrect information +- Generating outputs in incorrect formats + +Portkey's guardrails protect against these issues by validating both inputs and outputs. + +**Implementing Guardrails** + +```typescript +import { createHeaders, PORTKEY_GATEWAY_URL } from 'portkey-ai'; +import { OpenAI } from 'openai'; +import { setDefaultOpenAIClient } from '@openai/agents'; + +// Create a config with input and output guardrails, It's recommended you create Config in Portkey App and pass the config ID in the client +const config = { + "virtual_key": "openai-xxx", + "input_guardrails": ["guardrails-id-xxx", "guardrails-id-yyy"], + "output_guardrails": ["guardrails-id-xxx"] +}; + +// Configure OpenAI client with guardrails +const portkey = new OpenAI({ + baseURL: PORTKEY_GATEWAY_URL, + apiKey: process.env.PORTKEY_API_KEY!, + defaultHeaders: createHeaders({ + config: config, + virtualKey: "YOUR_OPENAI_VIRTUAL_KEY" + }) +}); +setDefaultOpenAIClient(portkey); +``` + +Portkey's guardrails can: +- Detect and redact PII in both inputs and outputs +- Filter harmful or inappropriate content +- Validate response formats against schemas +- Check for hallucinations against ground truth +- Apply custom business logic and rules + + + Explore Portkey's guardrail features to enhance agent safety + + +### 5. User Tracking with Metadata + +Track individual users through your OpenAI Agents using Portkey's metadata system. + +**What is Metadata in Portkey?** + +Metadata allows you to associate custom data with each request, enabling filtering, segmentation, and analytics. The special `_user` field is specifically designed for user tracking. + +```typescript +import { createHeaders, PORTKEY_GATEWAY_URL } from 'portkey-ai'; +import { OpenAI } from 'openai'; +import { setDefaultOpenAIClient } from '@openai/agents'; + +// Configure client with user tracking +const portkey = new OpenAI({ + baseURL: PORTKEY_GATEWAY_URL, + apiKey: process.env.PORTKEY_API_KEY!, + defaultHeaders: createHeaders({ + virtualKey: "YOUR_LLM_PROVIDER_VIRTUAL_KEY", + metadata: { + "_user": "user_123", // Special _user field for user analytics + "user_name": "John Doe", + "user_tier": "premium", + "user_company": "Acme Corp" + } + }) +}); +setDefaultOpenAIClient(portkey); +``` + +**Filter Analytics by User** + +With metadata in place, you can filter analytics by user and analyze performance metrics on a per-user basis: + + + + + +This enables: +- Per-user cost tracking and budgeting +- Personalized user analytics +- Team or organization-level metrics +- Environment-specific monitoring (staging vs. production) + + + Explore how to use custom metadata to enhance your analytics + + +### 6. Caching for Efficient Agents + +Implement caching to make your OpenAI Agents agents more efficient and cost-effective: + + + +```typescript +const portkeyConfig = { + "cache": { + "mode": "simple" + }, + "virtual_key": "YOUR_LLM_PROVIDER_VIRTUAL_KEY" +}; + +// Configure OpenAI client with chosen provider +const portkey = new OpenAI({ + baseURL: PORTKEY_GATEWAY_URL, + apiKey: process.env.PORTKEY_API_KEY!, + defaultHeaders: createHeaders({ + config: portkeyConfig + }) +}); +setDefaultOpenAIClient(portkey); +``` + +Simple caching performs exact matches on input prompts, caching identical requests to avoid redundant model executions. + + + +```typescript +const portkeyConfig = { + "cache": { + "mode": "semantic" + }, + "virtual_key": "YOUR_LLM_PROVIDER_VIRTUAL_KEY" +}; + +// Configure OpenAI client with chosen provider +const portkey = new OpenAI({ + baseURL: PORTKEY_GATEWAY_URL, + apiKey: process.env.PORTKEY_API_KEY!, + defaultHeaders: createHeaders({ + config: portkeyConfig + }) +}); +setDefaultOpenAIClient(portkey); +``` + +Semantic caching considers the contextual similarity between input requests, caching responses for semantically similar inputs. + + + +### 7. Model Interoperability + +With Portkey, you can easily switch between different LLMs in your OpenAI Agents without changing your core agent logic. + +```typescript +// Configure Portkey with different LLM providers +import { createHeaders, PORTKEY_GATEWAY_URL } from 'portkey-ai'; +import { OpenAI } from 'openai'; +import { setDefaultOpenAIClient, Agent, run } from '@openai/agents'; + +// Using OpenAI +const openaiConfig = { + "provider": "openai", + "api_key": "YOUR_OPENAI_API_KEY", + "override_params": { + "model": "gpt-4o" + } +}; + +// Using Anthropic +const anthropicConfig = { + "provider": "anthropic", + "api_key": "YOUR_ANTHROPIC_API_KEY", + "override_params": { + "model": "claude-3-opus-20240229" + } +}; + +// Choose which config to use +const activeConfig = openaiConfig; // or anthropicConfig + +// Configure OpenAI client with chosen provider +const portkey = new OpenAI({ + baseURL: PORTKEY_GATEWAY_URL, + apiKey: process.env.PORTKEY_API_KEY!, + defaultHeaders: createHeaders({ + config: activeConfig + }) +}); +setDefaultOpenAIClient(portkey); + +// Create and run agent - no changes needed in agent code +const agent = new Agent({ + name: "Assistant", + instructions: "You are a helpful assistant.", + // The model specified here will be used as a reference but the actual model + // is determined by the activeConfig + model: "gpt-4o" +}); + +const result = await run(agent, "Tell me about quantum computing."); +console.log(result.finalOutput); +``` + +Portkey provides access to over 200 LLMs through a unified interface, including: + +- OpenAI (GPT-4o, GPT-4 Turbo, etc.) +- Anthropic (Claude 3.5 Sonnet, Claude 3 Opus, etc.) +- Mistral AI (Mistral Large, Mistral Medium, etc.) +- Google Vertex AI (Gemini 1.5 Pro, etc.) +- Cohere (Command, Command-R, etc.) +- AWS Bedrock (Claude, Titan, etc.) +- Local/Private Models + + + See the full list of LLM providers supported by Portkey + + + +## Set Up Enterprise Governance for OpenAI Agents + +**Why Enterprise Governance?** +If you are using OpenAI Agents inside your orgnaization, you need to consider several governance aspects: +- **Cost Management**: Controlling and tracking AI spending across teams +- **Access Control**: Managing which teams can use specific models +- **Usage Analytics**: Understanding how AI is being used across the organization +- **Security & Compliance**: Maintaining enterprise security standards +- **Reliability**: Ensuring consistent service across all users + +Portkey adds a comprehensive governance layer to address these enterprise needs. Let's implement these controls step by step. + +**Enterprise Implementation Guide** + +Portkey allows you to use 1600+ LLMs with your OpenAI Agents setup, with minimal configuration required. Let's set up the core components in Portkey that you'll need for integration. + + + +Virtual Keys are Portkey's secure way to manage your LLM provider API keys. Think of them like disposable credit cards for your LLM API keys, providing essential controls like: +- Budget limits for API usage +- Rate limiting capabilities +- Secure API key storage + +To create a virtual key: +Go to [Virtual Keys](https://app.portkey.ai/virtual-keys) in the Portkey App. Save and copy the virtual key ID + + + + + + +Save your virtual key ID - you'll need it for the next step. + + + + + +Configs in Portkey are JSON objects that define how your requests are routed. They help with implementing features like advanced routing, fallbacks, and retries. + +We need to create a default config to route our requests to the virtual key created in Step 1. + +To create your config: +1. Go to [Configs](https://app.portkey.ai/configs) in Portkey dashboard +2. Create new config with: + ```json + { + "virtual_key": "YOUR_VIRTUAL_KEY_FROM_STEP1", + "override_params": { + "model": "gpt-4o" // Your preferred model name + } + } + + ``` +3. Save and note the Config name for the next step + + + + + + +This basic config connects to your virtual key. You can add more advanced portkey features later. + + + + + +Now create Portkey API key access point and attach the config you created in Step 2: + +1. Go to [API Keys](https://app.portkey.ai/api-keys) in Portkey and Create new API key +2. Select your config from `Step 2` +3. Generate and save your API key + + + + + + +Save your API key securely - you'll need it for OpenAI Agents integration. + + + + + Once you have created your API Key after attaching default config, you can directly pass the API key + base URL in the OpenAI client. Here's how: + +```typescript +import { createHeaders, PORTKEY_GATEWAY_URL } from 'portkey-ai'; +import { OpenAI } from 'openai'; + +const client = new OpenAI({ + apiKey: "YOUR_PORTKEY_API_KEY", // Your Portkey API Key from Step 3 + baseURL: PORTKEY_GATEWAY_URL +}); + +// your rest of the code remains same +``` + + + + + + +### Step 1: Implement Budget Controls & Rate Limits + +Virtual Keys enable granular control over LLM access at the team/department level. This helps you: +- Set up [budget limits](/product/ai-gateway/virtual-keys/budget-limits) +- Prevent unexpected usage spikes using Rate limits +- Track departmental spending + +#### Setting Up Department-Specific Controls: +1. Navigate to [Virtual Keys](https://app.portkey.ai/virtual-keys) in Portkey dashboard +2. Create new Virtual Key for each department with budget limits and rate limits +3. Configure department-specific limits + + + + + + + + +### Step 2: Define Model Access Rules + +As your AI usage scales, controlling which teams can access specific models becomes crucial. Portkey Configs provide this control layer with features like: + +#### Access Control Features: +- **Model Restrictions**: Limit access to specific models +- **Data Protection**: Implement guardrails for sensitive data +- **Reliability Controls**: Add fallbacks and retry logic + +#### Example Configuration: +Here's a basic configuration to route requests to OpenAI, specifically using GPT-4o: + +```json +{ + "strategy": { + "mode": "single" + }, + "targets": [ + { + "virtual_key": "YOUR_OPENAI_VIRTUAL_KEY", + "override_params": { + "model": "gpt-4o" + } + } + ] +} +``` + +Create your config on the [Configs page](https://app.portkey.ai/configs) in your Portkey dashboard. You'll need the config ID for connecting to OpenAI Agents's setup. + + +Configs can be updated anytime to adjust controls without affecting running applications. + + + + +### Step 3: Implement Access Controls + +Create User-specific API keys that automatically: +- Track usage per user/team with the help of virtual keys +- Apply appropriate configs to route requests +- Collect relevant metadata to filter logs +- Enforce access permissions + +Create API keys through: +- [Portkey App](https://app.portkey.ai/) +- [API Key Management API](/api-reference/admin-api/control-plane/api-keys/create-api-key) + +Example using TypeScript SDK: +```typescript +import { Portkey } from 'portkey-ai'; + +const portkey = new Portkey({ + apiKey: "YOUR_ADMIN_API_KEY" +}); + +const apiKey = await portkey.apiKeys.create({ + name: "engineering-team", + type: "organisation", + workspaceId: "YOUR_WORKSPACE_ID", + defaults: { + configId: "your-config-id", + metadata: { + environment: "production", + department: "engineering" + } + }, + scopes: ["logs.view", "configs.read"] +}); +``` + +For detailed key management instructions, see our [API Keys documentation](/api-reference/admin-api/control-plane/api-keys/create-api-key). + + + +### Step 4: Deploy & Monitor +After distributing API keys to your team members, your enterprise-ready OpenAI Agents setup is ready to go. Each team member can now use their designated API keys with appropriate access levels and budget controls. +Apply your governance setup using the integration steps from earlier sections +Monitor usage in Portkey dashboard: +- Cost tracking by department +- Model usage patterns +- Request volumes +- Error rates + + + + + +### Enterprise Features Now Available +**OpenAI Agents now has:** + +- Departmental budget controls +- Model access governance +- Usage tracking & attribution +- Security guardrails +- Reliability features + + + +## Frequently Asked Questions + + + + Portkey adds production-readiness to OpenAI Agents through comprehensive observability (traces, logs, metrics), reliability features (fallbacks, retries, caching), and access to 200+ LLMs through a unified interface. This makes it easier to debug, optimize, and scale your agent applications. + + + + Yes! Portkey integrates seamlessly with existing OpenAI Agents. You only need to replace your client initialization code with the Portkey-enabled version. The rest of your agent code remains unchanged. + + + + Portkey supports all OpenAI Agents SDK features, including tool use, memory, planning, and more. It adds observability and reliability without limiting any of the SDK's functionality. + + + + Portkey fully supports streaming responses in OpenAI Agents. You can enable streaming by using the appropriate methods in the OpenAI Agents SDK, and Portkey will properly track and log the streaming interactions. + + + + Portkey allows you to add custom metadata to your agent runs, which you can then use for filtering. Add fields like `agent_name`, `agent_type`, or `session_id` to easily find and analyze specific agent executions. + + + + Yes! Portkey uses your own API keys for the various LLM providers. It securely stores them as virtual keys, allowing you to easily manage and rotate keys without changing your code. + + + +## Resources + + + +

Official OpenAI Agents SDK documentation

+
+ + +

Example implementations for various use cases

+
+ + +

Get personalized guidance on implementing this integration

+
+
diff --git a/virtual_key_old/integrations/agents/openai-agents.mdx b/virtual_key_old/integrations/agents/openai-agents.mdx new file mode 100644 index 00000000..1288ca06 --- /dev/null +++ b/virtual_key_old/integrations/agents/openai-agents.mdx @@ -0,0 +1,1222 @@ +--- +title: "OpenAI Agents SDK (Python)" +description: "Use Portkey with OpenAI Agents SDK to take your AI Agents to production" +--- +## Introduction +OpenAI Agents SDK enables the development of complex AI agents with tools, planning, and memory capabilities. Portkey enhances OpenAI Agents with observability, reliability, and production-readiness features. + + +Portkey turns your experimental OpenAI Agents into production-ready systems by providing: + +- **Complete observability** of every agent step, tool use, and interaction +- **Built-in reliability** with fallbacks, retries, and load balancing +- **Cost tracking and optimization** to manage your AI spend +- **Access to 200+ LLMs** through a single integration +- **Guardrails** to keep agent behavior safe and compliant +- **Version-controlled prompts** for consistent agent performance + + + Learn more about OpenAI Agents SDK's core concepts + + +### Installation & Setup + + + +```bash +pip install -U openai-agents portkey-ai +``` + + + Create a Portkey API key with optional budget/rate limits and attach your Config + + + + There are 3 ways to integrate Portkey with OpenAI Agents: + + 1. Set a client that applies to all agents in your application + 2. Use a custom provider for selective Portkey integration + 3. Configure each agent individually + + See the [Integration Approaches](#integration-approaches) section for more details. + + + For a simple setup, we'll use the global client approach: + +```python +from agents import ( + set_default_openai_client, + set_default_openai_api, + Agent, Runner +) +from openai import AsyncOpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders +import os + +# Set up Portkey as the global client +portkey = AsyncOpenAI( + base_url=PORTKEY_GATEWAY_URL, + api_key=os.environ["PORTKEY_API_KEY"], + default_headers=createHeaders( + virtual_key="YOUR_OPENAI_VIRTUAL_KEY" + ) +) + +# Register as the SDK-wide default +set_default_openai_client(portkey, use_for_tracing=False) +set_default_openai_api("chat_completions") # Responses API โ†’ Chat +``` + + +**What are Virtual Keys?** Virtual keys in Portkey securely store your LLM provider API keys (OpenAI, Anthropic, etc.) in an encrypted vault. They allow for easier key rotation and budget management. [Learn more about virtual keys here](/product/ai-gateway/virtual-keys). + + + + +### Getting Started + +Let's create a simple question-answering agent with OpenAI Agents SDK and Portkey. This agent will respond directly to user messages using a language model: + +```python +from agents import ( + set_default_openai_client, + set_default_openai_api, + Agent, Runner +) +from openai import AsyncOpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders +import os + +# Set up Portkey as the global client +portkey = AsyncOpenAI( + base_url=PORTKEY_GATEWAY_URL, + api_key=os.environ["PORTKEY_API_KEY"], + default_headers=createHeaders( + virtual_key="YOUR_OPENAI_VIRTUAL_KEY" + ) +) + +# Register as the SDK-wide default +set_default_openai_client(portkey, use_for_tracing=False) +set_default_openai_api("chat_completions") # Responses API โ†’ Chat + +# Create agent with any supported model +agent = Agent( + name="Assistant", + instructions="You are a helpful assistant.", + model="gpt-4o" # Using Anthropic Claude through Portkey +) + +# Run the agent +result = Runner.run_sync(agent, "Tell me about quantum computing.") +print(result.final_output) +``` + +In this example: +1. We set up Portkey as the global client for OpenAI Agents SDK +2. We create a simple agent with instructions and a model +3. We run the agent synchronously with a user query +4. We print the final output + +Visit your Portkey dashboard to see detailed logs of this agent's execution! + +### Integration Approaches + +There are three ways to integrate Portkey with OpenAI Agents SDK, each suited for different scenarios: + + + + Set a global client that affects all agents in your application: + +```python +from agents import ( + set_default_openai_client, + set_default_openai_api, + set_tracing_disabled, + Agent, Runner +) +from openai import AsyncOpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders +import os + +# Set up Portkey as the global client +portkey = AsyncOpenAI( + base_url=PORTKEY_GATEWAY_URL, + api_key=os.environ["PORTKEY_API_KEY"], + default_headers=createHeaders( + virtual_key="YOUR_OPENAI_VIRTUAL_KEY" + ) +) + +# Register it as the SDK-wide default +set_default_openai_client(portkey, use_for_tracing=False) # skip OpenAI tracing +set_default_openai_api("chat_completions") # Responses API โ†’ Chat +set_tracing_disabled(True) # optional + +# Regular agent codeโ€”just a model name +agent = Agent( + name="Haiku Writer", + instructions="Respond only in haikus.", + model="claude-3-7-sonnet-latest" +) + +print(Runner.run_sync(agent, "Write a haiku on recursion.").final_output) +``` + +**Best for**: Whole application migration to Portkey with minimal code changes + + + + Use a custom ModelProvider to control which runs use Portkey: + +```python +from agents import ( + Model, + ModelProvider, + RunConfig, + Runner, + Agent +) +from agents import OpenAIChatCompletionsModel # concrete Model +from openai import AsyncOpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders +import os, asyncio + + +client = AsyncOpenAI( + base_url=PORTKEY_GATEWAY_URL, + api_key=os.environ["PORTKEY_API_KEY"], + default_headers=createHeaders( + virtual_key="YOUR_OPENAI_VIRTUAL_KEY" + ) +) + +class PortkeyProvider(ModelProvider): + def get_model(self, model_name: str | None) -> Model: + return OpenAIChatCompletionsModel( + model=model_name or "claude-3-7-sonnet-latest", + openai_client=client + ) + +PORTKEY = PortkeyProvider() # singleton is fine + +async def main(): + agent = Agent(name="Assistant", instructions="Haikus only.") + run_cfg = RunConfig(model_provider=PORTKEY) + + # Only this call uses Portkey + out = await Runner.run(agent, "Weather in Tokyo?", run_config=run_cfg) + print(out.final_output) + +asyncio.run(main()) +``` + +**Best for**: A/B testing, staged rollouts, or toggling between providers at runtime + + + + Attach a specific Model object to each Agent: + +```python +from agents import Agent, Runner, OpenAIChatCompletionsModel +from openai import AsyncOpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders +import os + + +portkey_client = AsyncOpenAI( + base_url=PORTKEY_GATEWAY_URL, + api_key=os.environ["PORTKEY_API_KEY"], + default_headers=createHeaders( + virtual_key="YOUR_OPENAI_VIRTUAL_KEY" + ) +) + + +agent = Agent( + name="Haiku Writer", + instructions="Classic Japanese form.", + model=OpenAIChatCompletionsModel( # concrete Model + model="claude-3-7-sonnet-latest", + openai_client=portkey_client + ), +) + +print(Runner.run_sync(agent, "Recursion haiku.").final_output) +``` + +**Best for**: Mixed agent environments where different agents need different providers or configurations + + + +**Comparing the 3 approaches** + +| Strategy | Code Touchpoints | Best For | +|:----------|:-----------------|:----------| +| **Global Client** via `set_default_openai_client` | One-time setup; agents need only model names | Whole app uses Portkey; simplest migration | +| **ModelProvider in RunConfig** | Add a provider + pass `run_config` | Toggle Portkey per run; A/B tests, staged rollouts | +| **Explicit Model per Agent** | Specify `OpenAIChatCompletionsModel` in agent | Mixed fleet: each agent can talk to a different provider | + +## End-to-End Example + +**Research Agent with Tools**: Here's a more comprehensive agent that can use tools to perform tasks. + +```python [expandable] +from agents import Agent, Runner, Tool, set_default_openai_client +from openai import AsyncOpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders +import os + +# Configure Portkey client +portkey = AsyncOpenAI( + api_key=os.environ.get("PORTKEY_API_KEY"), + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + virtual_key="YOUR_OPENAI_VIRTUAL_KEY" + ) +) +set_default_openai_client(portkey) + +# Define agent tools +def get_weather(location: str) -> str: + """Get the current weather for a location.""" + return f"It's 72ยฐF and sunny in {location}." + +def search_web(query: str) -> str: + """Search the web for information.""" + return f"Found information about: {query}" + +# Create agent with tools +agent = Agent( + name="Research Assistant", + instructions="You are a helpful assistant that can search for information and check the weather.", + model="claude-3-opus-20240229", + tools=[ + Tool( + name="get_weather", + description="Get current weather for a location", + input_schema={ + "location": { + "type": "string", + "description": "City and state, e.g. San Francisco, CA" + } + }, + callback=get_weather + ), + Tool( + name="search_web", + description="Search the web for information", + input_schema={ + "query": { + "type": "string", + "description": "Search query" + } + }, + callback=search_web + ) + ] +) + +# Run the agent +result = Runner.run_sync( + agent, + "What's the weather in San Francisco and find information about Golden Gate Bridge?" +) +print(result.final_output) +``` + +Visit your Portkey dashboard to see the complete execution flow visualized! + +--- + +## Production Features + +### 1. Enhanced Observability + +Portkey provides comprehensive observability for your OpenAI Agents, helping you understand exactly what's happening during each execution. + + + + + + + + + + Traces provide a hierarchical view of your agent's execution, showing the sequence of LLM calls, tool invocations, and state transitions. + + ```python + # Add tracing to your OpenAI Agents + portkey = AsyncOpenAI( + base_url=PORTKEY_GATEWAY_URL, + api_key=os.environ["PORTKEY_API_KEY"], + default_headers=createHeaders( + trace_id="unique_execution_trace_id", # Add unique trace ID + virtual_key="YOUR_OPENAI_VIRTUAL_KEY" + ) + ) + set_default_openai_client(portkey) + ``` + + + + + + + + + Portkey logs every interaction with LLMs, including: + + - Complete request and response payloads + - Latency and token usage metrics + - Cost calculations + - Tool calls and function executions + + All logs can be filtered by metadata, trace IDs, models, and more, making it easy to debug specific agent runs. + + + + + + + + Portkey provides built-in dashboards that help you: + + - Track cost and token usage across all agent runs + - Analyze performance metrics like latency and success rates + - Identify bottlenecks in your agent workflows + - Compare different agent configurations and LLMs + + You can filter and segment all metrics by custom metadata to analyze specific agent types, user groups, or use cases. + + + + + Analytics with metadata filters + + + + + Add custom metadata to your OpenAI agent calls to enable powerful filtering and segmentation: + ```python + + # Add tracing to your OpenAI Agents + portkey = AsyncOpenAI( + base_url=PORTKEY_GATEWAY_URL, + api_key=os.environ["PORTKEY_API_KEY"], + default_headers=createHeaders( + metadata={"agent_type": "research_agent"}, # Add custom metadata + virtual_key="YOUR_OPENAI_VIRTUAL_KEY" + ) + ) + set_default_openai_client(portkey) + ``` + + This metadata can be used to filter logs, traces, and metrics on the Portkey dashboard, allowing you to analyze specific agent runs, users, or environments. + + + + + +### 2. Reliability - Keep Your Agents Running Smoothly + +When running agents in production, things can go wrong - API rate limits, network issues, or provider outages. Portkey's reliability features ensure your agents keep running smoothly even when problems occur. + +It's this simple to enable fallback in your OpenAI Agents: + +```python +from portkey_ai import createHeaders, PORTKEY_GATEWAY_URL +from openai import AsyncOpenAI +from agents import set_default_openai_client + +# Create a config with fallbacks, It's recommended that you create the Config in Portkey App rather than hard-code the config JSON directly +config = { + "strategy": { + "mode": "fallback" + }, + "targets": [ + { + "provider": "openai", + "override_params": {"model": "gpt-4o"} + }, + { + "provider": "anthropic", + "override_params": {"model": "claude-3-opus-20240229"} + } + ] +} + +# Configure Portkey client with fallback config +portkey = AsyncOpenAI( + base_url=PORTKEY_GATEWAY_URL, + api_key=os.environ["PORTKEY_API_KEY"], + default_headers=createHeaders(config=config) +) +set_default_openai_client(portkey) +``` + +This configuration will automatically try Claude if the GPT-4o request fails, ensuring your agent can continue operating. + + + + Handles temporary failures automatically. If an LLM call fails, Portkey will retry the same request for the specified number of times - perfect for rate limits or network blips. + + + Prevent your agents from hanging. Set timeouts to ensure you get responses (or can fail gracefully) within your required timeframes. + + + Send different requests to different providers. Route complex reasoning to GPT-4, creative tasks to Claude, and quick responses to Gemini based on your needs. + + + Keep running even if your primary provider fails. Automatically switch to backup providers to maintain availability. + + + Spread requests across multiple API keys or providers. Great for high-volume agent operations and staying within rate limits. + + + +### 3. Prompting in OpenAI Agents + +Portkey's Prompt Engineering Studio helps you create, manage, and optimize the prompts used in your OpenAI Agents. Instead of hardcoding prompts or instructions, use Portkey's prompt rendering API to dynamically fetch and apply your versioned prompts. + + + +![Prompt Playground Interface](/images/product/ai-gateway/ai-20.webp) + + + + + + + +Prompt Playground is a place to compare, test and deploy perfect prompts for your AI application. Itโ€™s where you experiment with different models, test variables, compare outputs, and refine your prompt engineering strategy before deploying to production. It allows you to: + + 1. Iteratively develop prompts before using them in your agents + 2. Test prompts with different variables and models + 3. Compare outputs between different prompt versions + 4. Collaborate with team members on prompt development + + This visual environment makes it easier to craft effective prompts for each step in your OpenAI Agents agent's workflow. + + + + +The Prompt Render API retrieves your prompt templates with all parameters configured: + +```python +from openai import AsyncOpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders +from agents import Agent, Runner, set_default_openai_client + +# Initialize Portkey client +portkey_client = Portkey(api_key="PORTKEY_API_KEY") + +# Retrieve prompt using the render API +prompt_data = portkey_client.prompts.render( + prompt_id="YOUR_PROMPT_ID", + variables={ + "user_input": "Tell me about artificial intelligence" + } +) + +# Configure OpenAI client with Portkey +openai_client = AsyncOpenAI( + base_url=PORTKEY_GATEWAY_URL, + api_key="YOUR_PORTKEY_API_KEY", + default_headers=createHeaders( + virtual_key="YOUR_OPENAI_VIRTUAL_KEY" + ) +) +set_default_openai_client(openai_client) + +# Use the rendered prompt in your OpenAI Agent +agent = Agent( + name="Assistant", + instructions=prompt_data.data.messages[0]["content"], # Use the rendered prompt as instructions + model="gpt-4o" +) + +result = Runner.run_sync(agent, "Tell me about artificial intelligence") +print(result.final_output) +``` + + + + + +You can: +- Create multiple versions of the same prompt +- Compare performance between versions +- Roll back to previous versions if needed +- Specify which version to use in your code: + +```python +# Use a specific prompt version +prompt_data = portkey_client.prompts.render( + prompt_id="YOUR_PROMPT_ID@version_number", + variables={ + "user_input": "Tell me about quantum computing" + } +) +``` + + + + + + +Portkey prompts use Mustache-style templating for easy variable substitution: + +``` +You are an AI assistant helping with {{task_type}}. + +User question: {{user_input}} + +Please respond in a {{tone}} tone and include {{required_elements}}. +``` + +When rendering, simply pass the variables: + +```python +prompt_data = portkey_client.prompts.render( + prompt_id="YOUR_PROMPT_ID", + variables={ + "task_type": "research", + "user_input": "Tell me about quantum computing", + "tone": "professional", + "required_elements": "recent academic references" + } +) +``` + + + + + Learn more about Portkey's prompt management features + + +### 4. Guardrails for Safe Agents + +Guardrails ensure your OpenAI Agents operate safely and respond appropriately in all situations. + + +**Why Use Guardrails?** + +OpenAI Agents can experience various failure modes: +- Generating harmful or inappropriate content +- Leaking sensitive information like PII +- Hallucinating incorrect information +- Generating outputs in incorrect formats + +Portkey's guardrails protect against these issues by validating both inputs and outputs. + +**Implementing Guardrails** + +```python +from portkey_ai import createHeaders, PORTKEY_GATEWAY_URL +from openai import AsyncOpenAI +from agents import set_default_openai_client + +# Create a config with input and output guardrails, It's recommended you create Config in Portkey App and pass the config ID in the client +config = { + "virtual_key": "openai-xxx", + "input_guardrails": ["guardrails-id-xxx", "guardrails-id-yyy"], + "output_guardrails": ["guardrails-id-xxx"] +} + +# Configure OpenAI client with guardrails +portkey = AsyncOpenAI( + base_url=PORTKEY_GATEWAY_URL, + api_key=os.environ["PORTKEY_API_KEY"], + default_headers=createHeaders( + config=config, + virtual_key="YOUR_OPENAI_VIRTUAL_KEY" + ) +) +set_default_openai_client(portkey) +``` + +Portkey's guardrails can: +- Detect and redact PII in both inputs and outputs +- Filter harmful or inappropriate content +- Validate response formats against schemas +- Check for hallucinations against ground truth +- Apply custom business logic and rules + + + Explore Portkey's guardrail features to enhance agent safety + + +### 5. User Tracking with Metadata + +Track individual users through your OpenAI Agents using Portkey's metadata system. + +**What is Metadata in Portkey?** + +Metadata allows you to associate custom data with each request, enabling filtering, segmentation, and analytics. The special `_user` field is specifically designed for user tracking. + +```python +from portkey_ai import createHeaders, PORTKEY_GATEWAY_URL +from openai import AsyncOpenAI +from agents import set_default_openai_client + +# Configure client with user tracking +portkey = AsyncOpenAI( + base_url=PORTKEY_GATEWAY_URL, + api_key=os.environ["PORTKEY_API_KEY"], + default_headers=createHeaders( + virtual_key="YOUR_LLM_PROVIDER_VIRTUAL_KEY", + metadata={ + "_user": "user_123", # Special _user field for user analytics + "user_name": "John Doe", + "user_tier": "premium", + "user_company": "Acme Corp" + } + ) +) +set_default_openai_client(portkey) +``` + + + + +**Filter Analytics by User** + +With metadata in place, you can filter analytics by user and analyze performance metrics on a per-user basis: + + + + + + +This enables: +- Per-user cost tracking and budgeting +- Personalized user analytics +- Team or organization-level metrics +- Environment-specific monitoring (staging vs. production) + + + Explore how to use custom metadata to enhance your analytics + + + + + +### 6. Caching for Efficient Agents + +Implement caching to make your OpenAI Agents agents more efficient and cost-effective: + + + + ```python + portkey_config = { + "cache": { + "mode": "simple" + }, + "virtual_key": "YOUR_LLM_PROVIDER_VIRTUAL_KEY" + } + + # Configure OpenAI client with chosen provider + portkey = AsyncOpenAI( + base_url=PORTKEY_GATEWAY_URL, + api_key=os.environ["PORTKEY_API_KEY"], + default_headers=createHeaders(config=portkey_config) + ) + set_default_openai_client(portkey) + ``` + + Simple caching performs exact matches on input prompts, caching identical requests to avoid redundant model executions. + + + + ```python + portkey_config = { + "cache": { + "mode": "semantic" + }, + "virtual_key": "YOUR_LLM_PROVIDER_VIRTUAL_KEY" + } + + # Configure OpenAI client with chosen provider + portkey = AsyncOpenAI( + base_url=PORTKEY_GATEWAY_URL, + api_key=os.environ["PORTKEY_API_KEY"], + default_headers=createHeaders(config=portkey_config) + ) + set_default_openai_client(portkey) + ``` + + Semantic caching considers the contextual similarity between input requests, caching responses for semantically similar inputs. + + + + + + +### 7. Model Interoperability + +With Portkey, you can easily switch between different LLMs in your OpenAI Agents without changing your core agent logic. + +```python +# Configure Portkey with different LLM providers +from portkey_ai import createHeaders, PORTKEY_GATEWAY_URL +from openai import AsyncOpenAI +from agents import set_default_openai_client + +# Using OpenAI +openai_config = { + "provider": "openai", + "api_key": "YOUR_OPENAI_API_KEY", + "override_params": { + "model": "gpt-4o" + } +} + +# Using Anthropic +anthropic_config = { + "provider": "anthropic", + "api_key": "YOUR_ANTHROPIC_API_KEY", + "override_params": { + "model": "claude-3-opus-20240229" + } +} + +# Choose which config to use +active_config = openai_config # or anthropic_config + +# Configure OpenAI client with chosen provider +portkey = AsyncOpenAI( + base_url=PORTKEY_GATEWAY_URL, + api_key=os.environ["PORTKEY_API_KEY"], + default_headers=createHeaders(config=active_config) +) +set_default_openai_client(portkey) + +# Create and run agent - no changes needed in agent code +agent = Agent( + name="Assistant", + instructions="You are a helpful assistant.", + # The model specified here will be used as a reference but the actual model + # is determined by the active_config + model="gpt-4o" +) + +result = Runner.run_sync(agent, "Tell me about quantum computing.") +print(result.final_output) +``` + +Portkey provides access to over 200 LLMs through a unified interface, including: + +- OpenAI (GPT-4o, GPT-4 Turbo, etc.) +- Anthropic (Claude 3.5 Sonnet, Claude 3 Opus, etc.) +- Mistral AI (Mistral Large, Mistral Medium, etc.) +- Google Vertex AI (Gemini 1.5 Pro, etc.) +- Cohere (Command, Command-R, etc.) +- AWS Bedrock (Claude, Titan, etc.) +- Local/Private Models + + + See the full list of LLM providers supported by Portkey + + +### 8. Tracing + +Portkey provides an opentelemetry compatible backend to store and query your traces. + +You can trace your OpenAI Agents using any OpenTelemetry compatible tracing library. + +Here is an example of how to trace your OpenAI Agents using the [`logfire`](/integrations/tracing/logfire) library from Pydantic: + +```python Python +import logfire +from pydantic import BaseModel +from agents import Agent, Runner, function_tool +import asyncio +import os + +os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = "https://api.portkey.com/v1/logs/otel" +os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = f"x-portkey-api-key={YOUR_PORTKEY_API_KEY}" + +class Weather(BaseModel): + city: str + temperature_range: str + conditions: str + +@function_tool +def get_weather(city: str) -> Weather: + return Weather(city=city, temperature_range="14-20C", conditions="Sunny with wind.") + +agent = Agent( + name="Hello world", + instructions="You are a helpful agent.", + tools=[get_weather] +) + +async def main(): + logfire.configure( + service_name='my_agent_service', + send_to_logfire=False, + ) + logfire.instrument_openai_agents() + result = await Runner.run(agent, input="What's the weather in Tokyo?") + print(result.final_output) + +if __name__ == "__main__": + asyncio.run(main()) +``` + + +## Tool Use in OpenAI Agents + +OpenAI Agents SDK natively supports tools that enable your agents to interact with external systems and APIs. Portkey provides full observability for tool usage in your agents: + +```python [expandable] +from agents import Agent, Runner, Tool, set_default_openai_client +from openai import AsyncOpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders +import os + +# Configure Portkey client with tracing +portkey = AsyncOpenAI( + base_url=PORTKEY_GATEWAY_URL, + api_key=os.environ["PORTKEY_API_KEY"], + default_headers=createHeaders( + trace_id="tools_example", + metadata={"agent_type": "research"} + ) +) +set_default_openai_client(portkey) + +# Define tools +def get_weather(location: str, unit: str = "fahrenheit") -> str: + """Get the current weather in a given location""" + return f"The weather in {location} is 72 degrees {unit}" + +def get_population(city: str, country: str) -> str: + """Get the population of a city""" + return f"The population of {city}, {country} is 1,000,000" + +# Create agent with tools +agent = Agent( + name="Research Assistant", + instructions="You are a helpful assistant that can look up weather and population information.", + model="gpt-4o-mini", + tools=[ + Tool( + name="get_weather", + description="Get the current weather in a given location", + input_schema={ + "location": { + "type": "string", + "description": "City and state, e.g. San Francisco, CA" + }, + "unit": { + "type": "string", + "description": "Temperature unit (celsius or fahrenheit)", + "default": "fahrenheit" + } + }, + callback=get_weather + ), + Tool( + name="get_population", + description="Get the population of a city", + input_schema={ + "city": { + "type": "string", + "description": "City name" + }, + "country": { + "type": "string", + "description": "Country name" + } + }, + callback=get_population + ) + ] +) + +# Run the agent +result = Runner.run_sync( + agent, + "What's the weather in San Francisco and what's the population of Tokyo, Japan?" +) +print(result.final_output) +``` + + + + + +## Set Up Enterprise Governance for OpenAI Agents + +**Why Enterprise Governance?** +If you are using OpenAI Agents inside your orgnaization, you need to consider several governance aspects: +- **Cost Management**: Controlling and tracking AI spending across teams +- **Access Control**: Managing which teams can use specific models +- **Usage Analytics**: Understanding how AI is being used across the organization +- **Security & Compliance**: Maintaining enterprise security standards +- **Reliability**: Ensuring consistent service across all users + +Portkey adds a comprehensive governance layer to address these enterprise needs. Let's implement these controls step by step. + +**Enterprise Implementation Guide** + +Portkey allows you to use 1600+ LLMs with your OpenAI Agents setup, with minimal configuration required. Let's set up the core components in Portkey that you'll need for integration. + + + +Virtual Keys are Portkey's secure way to manage your LLM provider API keys. Think of them like disposable credit cards for your LLM API keys, providing essential controls like: +- Budget limits for API usage +- Rate limiting capabilities +- Secure API key storage + +To create a virtual key: +Go to [Virtual Keys](https://app.portkey.ai/virtual-keys) in the Portkey App. Save and copy the virtual key ID + + + + + + +Save your virtual key ID - you'll need it for the next step. + + + + + +Configs in Portkey are JSON objects that define how your requests are routed. They help with implementing features like advanced routing, fallbacks, and retries. + +We need to create a default config to route our requests to the virtual key created in Step 1. + +To create your config: +1. Go to [Configs](https://app.portkey.ai/configs) in Portkey dashboard +2. Create new config with: + ```json + { + "virtual_key": "YOUR_VIRTUAL_KEY_FROM_STEP1", + "override_params": { + "model": "gpt-4o" // Your preferred model name + } + } + + ``` +3. Save and note the Config name for the next step + + + + + + +This basic config connects to your virtual key. You can add more advanced portkey features later. + + + + + +Now create Portkey API key access point and attach the config you created in Step 2: + +1. Go to [API Keys](https://app.portkey.ai/api-keys) in Portkey and Create new API key +2. Select your config from `Step 2` +3. Generate and save your API key + + + + + + +Save your API key securely - you'll need it for OpenAI Agents integration. + + + + + Once you have creted your API Key after attaching default config, you can directly pass the API key + base URL in the AsyncOpenAI client. Here's how: + + ```Python + from portkey_ai import createHeaders, PORTKEY_GATEWAY_URL + from openai import AsyncOpenAI + + + client=AsyncOpenAI( + api_key="YOUR_PORTKEY_API_KEY", # Your Portkey API Key from Step 3 + base_url="PORTKEY_GATEWAY_URL" + ) + + # your rest of the code remains same + ``` + + + + + + + + +### Step 1: Implement Budget Controls & Rate Limits + +Virtual Keys enable granular control over LLM access at the team/department level. This helps you: +- Set up [budget limits](/product/ai-gateway/virtual-keys/budget-limits) +- Prevent unexpected usage spikes using Rate limits +- Track departmental spending + +#### Setting Up Department-Specific Controls: +1. Navigate to [Virtual Keys](https://app.portkey.ai/virtual-keys) in Portkey dashboard +2. Create new Virtual Key for each department with budget limits and rate limits +3. Configure department-specific limits + + + + + + + + +### Step 2: Define Model Access Rules + +As your AI usage scales, controlling which teams can access specific models becomes crucial. Portkey Configs provide this control layer with features like: + +#### Access Control Features: +- **Model Restrictions**: Limit access to specific models +- **Data Protection**: Implement guardrails for sensitive data +- **Reliability Controls**: Add fallbacks and retry logic + +#### Example Configuration: +Here's a basic configuration to route requests to OpenAI, specifically using GPT-4o: + +```json +{ + "strategy": { + "mode": "single" + }, + "targets": [ + { + "virtual_key": "YOUR_OPENAI_VIRTUAL_KEY", + "override_params": { + "model": "gpt-4o" + } + } + ] +} +``` + +Create your config on the [Configs page](https://app.portkey.ai/configs) in your Portkey dashboard. You'll need the config ID for connecting to OpenAI Agents's setup. + + + +Configs can be updated anytime to adjust controls without affecting running applications. + + + + +### Step 3: Implement Access Controls + +Create User-specific API keys that automatically: +- Track usage per user/team with the help of virtual keys +- Apply appropriate configs to route requests +- Collect relevant metadata to filter logs +- Enforce access permissions + + +Create API keys through: +- [Portkey App](https://app.portkey.ai/) +- [API Key Management API](/api-reference/admin-api/control-plane/api-keys/create-api-key) + + +Example using Python SDK: +```python +from portkey_ai import Portkey + +portkey = Portkey(api_key="YOUR_ADMIN_API_KEY") + +api_key = portkey.api_keys.create( + name="engineering-team", + type="organisation", + workspace_id="YOUR_WORKSPACE_ID", + defaults={ + "config_id": "your-config-id", + "metadata": { + "environment": "production", + "department": "engineering" + } + }, + scopes=["logs.view", "configs.read"] +) +``` + +For detailed key management instructions, see our [API Keys documentation](/api-reference/admin-api/control-plane/api-keys/create-api-key). + + + +### Step 4: Deploy & Monitor +After distributing API keys to your team members, your enterprise-ready OpenAI Agents setup is ready to go. Each team member can now use their designated API keys with appropriate access levels and budget controls. +Apply your governance setup using the integration steps from earlier sections +Monitor usage in Portkey dashboard: +- Cost tracking by department +- Model usage patterns +- Request volumes +- Error rates + + + + + +### Enterprise Features Now Available +**OpenAI Agents now has:** + +- Departmental budget controls +- Model access governance +- Usage tracking & attribution +- Security guardrails +- Reliability features + + + + + +## Frequently Asked Questions + + + + Portkey adds production-readiness to OpenAI Agents through comprehensive observability (traces, logs, metrics), reliability features (fallbacks, retries, caching), and access to 200+ LLMs through a unified interface. This makes it easier to debug, optimize, and scale your agent applications. + + + + Yes! Portkey integrates seamlessly with existing OpenAI Agents. You only need to replace your client initialization code with the Portkey-enabled version. The rest of your agent code remains unchanged. + + + + Portkey supports all OpenAI Agents SDK features, including tool use, memory, planning, and more. It adds observability and reliability without limiting any of the SDK's functionality. + + + + Portkey fully supports streaming responses in OpenAI Agents. You can enable streaming by using the appropriate methods in the OpenAI Agents SDK, and Portkey will properly track and log the streaming interactions. + + + + Portkey allows you to add custom metadata to your agent runs, which you can then use for filtering. Add fields like `agent_name`, `agent_type`, or `session_id` to easily find and analyze specific agent executions. + + + + Yes! Portkey uses your own API keys for the various LLM providers. It securely stores them as virtual keys, allowing you to easily manage and rotate keys without changing your code. + + + + + + +## Resources + + + + +

Official OpenAI Agents SDK documentation

+
+ + +

Example implementations for various use cases

+
+ + +

Get personalized guidance on implementing this integration

+
+
diff --git a/virtual_key_old/integrations/agents/openai-swarm.mdx b/virtual_key_old/integrations/agents/openai-swarm.mdx new file mode 100644 index 00000000..687b1ad7 --- /dev/null +++ b/virtual_key_old/integrations/agents/openai-swarm.mdx @@ -0,0 +1,326 @@ +--- +title: "OpenAI Swarm" +description: "The Portkey x Swarm integration brings advanced AI gateway capabilities, full-stack observability, and reliability features to build production-ready AI agents." + +--- + +Swarm is an experimental framework by OpenAI for building multi-agent systems. It showcases the handoff & routines pattern, making agent coordination and execution lightweight, highly controllable, and easily testable. Portkey integration extends Swarm's capabilities with production-ready features like observability, reliability, and more. + + +## Getting Started + + + +### 1\. Install the Portkey SDK +```sh +pip install -U portkey-ai +``` + + +### 2\. Configure the LLM Client used in OpenAI Swarm + +To build Swarm Agents with Portkey, you'll need two keys: + +- **Portkey API Key**: Sign up on the [Portkey app](https://app.portkey.ai) and copy your API key. + +- **Virtual Key**: Virtual Keys are a secure way to manage your LLM API KEYS in one place. Instead of handling multiple API keys in your code, you can store your LLM provider API Keys securely in Portkey's vault + + +Create a Virtual Key in the [Portkey app](https://app.portkey.ai) + +```py +from swarm import Swarm, Agent +from portkey_ai import Portkey + +portkey = Portkey( + api_key="YOUR_PORTKEY_API_KEY", # defaults to os.environ.get("PORTKEY_API_KEY") + virtual_key="YOUR_VIRTUAL_KEY" + ) + +client = Swarm(client=portkey) +``` + + + +### 3\. Create and Run an Agent +In this example we are building a simple Weather Agent using OpenAI Swarm with Portkey. +```py +def get_weather(location) -> str: + return "{'temp':67, 'unit':'F'}" + + +agent = Agent( + name="Agent", + instructions="You are a helpful agent.", + functions=[get_weather], +) + +messages = [{"role": "user", "content": "What's the weather in NYC?"}] + +response = client.run(agent=agent, messages=messages) +print(response.messages[-1]["content"]) +``` + + + + + +## E2E example with Function Calling in OpenAI Swarm +Here's a complete example showing function calling and agent interaction: +```py +from swarm import Swarm, Agent +from portkey_ai import Portkey + +portkey = Portkey( + api_key="YOUR_PORTKEY_API_KEY", # defaults to os.environ.get("PORTKEY_API_KEY") + virtual_key="YOUR_VIRTUAL_KEY" + ) + +client = Swarm(client=portkey) + + +def get_weather(location) -> str: + return "{'temp':67, 'unit':'F'}" + + +agent = Agent( + name="Agent", + instructions="You are a helpful agent.", + functions=[get_weather], +) + +messages = [{"role": "user", "content": "What's the weather in NYC?"}] + +response = client.run(agent=agent, messages=messages) +print(response.messages[-1]["content"]) +``` +> The current temperature in New York City is 67ยฐF. + + +## Enabling Portkey Features +By routing your OpenAI Swarm requests through Portkey, you get access to the following production-grade features: + + +

Call various LLMs like Anthropic, Gemini, Mistral, Azure OpenAI, Google Vertex AI, and AWS Bedrock with minimal code changes.

+
+ +

Speed up agent responses and save costs by storing past responses in the Portkey cache. Choose between Simple and Semantic cache modes.

+
+ +

Set up fallbacks between different LLMs, load balance requests across multiple instances, set automatic retries, and request timeouts.

+
+ +

Get comprehensive logs of agent interactions, including cost, tokens used, response time, and function calls. Send custom metadata for better analytics.

+
+ +

Access detailed logs of agent executions, function calls, and interactions. Debug and optimize your agents effectively.

+
+ +

Implement budget limits, role-based access control, and audit trails for your agent operations.

+
+ +

Capture and analyze user feedback to improve agent performance over time.

+
+
+ + + + + +## 1. Interoperability - Calling Different LLMs + +When building with Swarm, you might want to experiment with different LLMs or use specific providers for different agent tasks. Portkey makes this seamless - you can switch between OpenAI, Anthropic, Gemini, Mistral, or cloud providers without changing your agent code. + +Instead of managing multiple API keys and provider-specific configurations, Portkey's Virtual Keys give you a single point of control. Here's how you can use different LLMs with your Swarm agents: + + + +```python +portkey = Portkey( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="ANTHROPIC_VIRTUAL_KEY" #Just change the virtual key to your preferred LLM provider +) + +client = Swarm(client=portkey) +``` + + +```python +portkey = Portkey( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="AZURE_OPENAI_VIRTUAL_KEY" #Just change the virtual key to your preferred LLM provider +) + +client = Swarm(client=portkey) +``` + + + +## 2. Caching - Speed Up Agent Responses + +Agent operations often involve repetitive queries or similar tasks. Every time your agent makes an LLM call, you're paying for tokens and waiting for responses. Portkey's caching system can significantly reduce both costs and latency. + +Portkey offers two powerful caching modes: + +**Simple Cache**: Perfect for exact matches - when your agents make identical requests. Ideal for deterministic operations like function calling or FAQ-type queries. + +**Semantic Cache**: Uses embedding-based matching to identify similar queries. Great for natural language interactions where users might ask the same thing in different ways. + +```python +config = { + "cache": { + "mode": "semantic", # or "simple" for exact matching + "max_age": 3600000 # cache duration in milliseconds + } +} + +portkey = Portkey( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_VIRTUAL_KEY", + config=config +) +``` + +## 3. Reliability - Keep Your Agents Running Smoothly + +When running agents in production, things can go wrong - API rate limits, network issues, or provider outages. Portkey's reliability features ensure your agents keep running smoothly even when problems occur. + + + + Handles temporary failures automatically. If an LLM call fails, Portkey will retry the same request for the specified number of times - perfect for rate limits or network blips. + + + + Prevent your agents from hanging. Set timeouts to ensure you get responses (or can fail gracefully) within your required timeframes. + + + + Send different requests to different providers. Route complex reasoning to GPT-4, creative tasks to Claude, and quick responses to Gemini based on your needs. + + + + Keep running even if your primary provider fails. Automatically switch to backup providers to maintain availability. + + + + Spread requests across multiple API keys or providers. Great for high-volume agent operations and staying within rate limits. + + + +## 4. [Observability - Understand Your Agents](/product/observability) + +Building agents is the first step - but how do you know they're working effectively? Portkey provides comprehensive visibility into your agent operations through multiple lenses: + +**Metrics Dashboard**: Track 40+ key performance indicators like: +- Cost per agent interaction +- Response times and latency +- Token usage and efficiency +- Success/failure rates +- Cache hit rates + + + + + + + +#### Send Custom Metadata with your requests +Add trace IDs to track specific workflows: + + + +```python +portkey = Portkey( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_VIRTUAL_KEY", + trace_id="weather_workflow_123", + metadata={ + "agent": "weather_agent", + "environment": "production" + } +) +``` + + + + + +## 5. [Logs and Traces](/product/observability/logs) + +Logs are essential for understanding agent behavior, diagnosing issues, and improving performance. They provide a detailed record of agent activities and tool use, which is crucial for debugging and optimizing processes. + + +Access a dedicated section to view records of agent executions, including parameters, outcomes, function calls, and errors. Filter logs based on multiple parameters such as trace ID, model, tokens used, and metadata. + + + + + + + +## 6. [Security & Compliance - Enterprise-Ready Controls](/product/enterprise-offering/security-portkey) + +When deploying agents in production, security is crucial. Portkey provides enterprise-grade security features: + + + + Set and monitor spending limits per Virtual Key. Get alerts before costs exceed thresholds. + + + + Control who can access what. Assign roles and permissions for your team members. + + + + Track all changes and access. Know who modified agent settings and when. + + + + Configure data retention and processing policies to meet your compliance needs. + + + +Configure these settings in the [Portkey Dashboard](https://app.portkey.ai) or programmatically through the API. + + +## 7. Continuous Improvement + +Now that you know how to trace & log your Llamaindex requests to Portkey, you can also start capturing user feedback to improve your app! + +You can append qualitative as well as quantitative feedback to any `trace ID` with the `portkey.feedback.create` method: + +```py Adding Feedback +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY", + virtual_key="YOUR_OPENAI_VIRTUAL_KEY" +) + +feedback = portkey.feedback.create( + trace_id="YOUR_LLAMAINDEX_TRACE_ID", + value=5, # Integer between -10 and 10 + weight=1, # Optional + metadata={ + # Pass any additional context here like comments, _user and more + } +) + +print(feedback) +``` + + + +## [Portkey Config](/product/ai-gateway/configs) + +Many of these features are driven by Portkey's Config architecture. The Portkey app simplifies creating, managing, and versioning your Configs. + +For more information on using these features and setting up your Config, please refer to the [Portkey documentation](https://docs.portkey.ai). + + + + + + + + diff --git a/virtual_key_old/integrations/agents/phidata.mdx b/virtual_key_old/integrations/agents/phidata.mdx new file mode 100644 index 00000000..d8d83937 --- /dev/null +++ b/virtual_key_old/integrations/agents/phidata.mdx @@ -0,0 +1,180 @@ +--- +title: "Phidata" +description: "Use Portkey with Phidata to take your AI Agents to production" +--- + +## Getting started + +### 1\. Install the required packages: + +```sh +pip install phidata portkey-ai +``` + +### **2\.** Configure your Phidata LLM objects: + +```py +from phi.llm.openai import OpenAIChat +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +llm = OpenAIChat( + base_url=PORTKEY_GATEWAY_URL, + api_key="OPENAI_API_KEY", #Replace with Your OpenAI Key + default_headers=createHeaders( + provider="openai", + api_key=PORTKEY_API_KEY # Replace with your Portkey API key + ) +) +``` + +## Integration Guide + +Here's a simple Colab notebook that demonstrates Phidata with Portkey integration + +[![](/images/guides/colab-badge.svg)](https://dub.sh/Phidata-docs) + +## Make your agents Production-ready with Portkey + +Portkey makes your Phidata agents reliable, robust, and production-grade with its observability suite and AI Gateway. Seamlessly integrate 200+ LLMs with your Phidata agents using Portkey. Implement fallbacks, gain granular insights into agent performance and costs, and continuously optimize your AI operationsโ€”all with just 2 lines of code. + +Let's dive deep! Let's go through each of the use cases! + +### 1\. [Interoperability](/product/ai-gateway/universal-api) + +Easily switch between 200+ LLMs. Call various LLMs such as Anthropic, Gemini, Mistral, Azure OpenAI, Google Vertex AI, AWS Bedrock, and many more by simply changing the `provider ` and `API key` in the `ChatOpenAI` object. + + + + If you are using OpenAI with Phidata, your code would look like this: +```py +llm = OpenAIChat( + base_url=PORTKEY_GATEWAY_URL, + api_key="OPENAI_API_KEY", #Replace with Your OpenAI Key + default_headers=createHeaders( + provider="openai", + api_key=userdata.get('PORTKEY_API_KEY') # Replace with your Portkey API key + ) +) +``` +To switch to Azure as your provider, add your Azure details to Portley vault ([here's how](/integrations/llms/azure-openai)) and use Azure OpenAI using virtual keys + + +```py +llm = OpenAIChat( + base_url=PORTKEY_GATEWAY_URL, + api_key="api_key", #We will be using Virtual Key + default_headers=createHeaders( + provider="azure-openai", + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="AZURE_OPENAI_KEY" + ) +) +``` + + +If you are using Anthropic with Phidata, your code would look like this: +```py +llm = OpenAIChat( + base_url=PORTKEY_GATEWAY_URL, + api_key="ANTHROPIC_API_KEY", #Replace with Your OpenAI Key + default_headers=createHeaders( + provider="anthropic", + api_key="PORTKEY_API_KEY" # Replace with your Portkey API key + ) +) +``` +To switch to AWS Bedrock as your provider, add your AWS Bedrock details to Portley vault ([here's how](/integrations/llms/aws-bedrock)) and use AWS Bedrock using virtual keys, + +```py +llm = OpenAIChat( + base_url=PORTKEY_GATEWAY_URL, + api_key="api_key", #We will be using Virtual Key + default_headers=createHeaders( + provider="bedrock", + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="BEDROCK_OPENAI_KEY" #Bedrock Virtual Key + ) +) +``` + + + +### 2\. [Reliability](/product/ai-gateway) + +Agents are _brittle_. Long agentic pipelines with multiple steps can fail at any stage, disrupting the entire process. Portkey solves this by offering built-in **fallbacks** between different LLMs or providers, **load-balancing** across multiple instances or API keys, and implementing automatic **retries** and request **timeouts**. This makes your agents more reliable and resilient. + +Here's how you can implement these features using Portkey's config + +```py +{ + "retry": { + "attempts": 5 + }, + "strategy": { + "mode": "loadbalance" // Choose between "loadbalance" or "fallback" + }, + "targets": [ + { + "provider": "openai", + "api_key": "OpenAI_API_Key" + }, + { + "provider": "anthropic", + "api_key": "Anthropic_API_Key" + } + ] +} +``` + +### 3\. [Metrics](/product/observability) + +Agent runs can be costly. Tracking agent metrics is crucial for understanding the performance and reliability of your AI agents. Metrics help identify issues, optimize runs, and ensure that your agents meet their intended goals. + +Portkey automatically logs comprehensive metrics for your AI agents, including **cost**, **tokens used**, **latency**, etc. Whether you need a broad overview or granular insights into your agent runs, Portkey's customizable filters provide the metrics you need. For agent-specific observability, add `Trace-id` to the request headers for each agent. + +```py +llm2 = ChatOpenAI( + api_key="Anthropic_API_Key", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key="PORTKEY_API_KEY", + provider="anthropic", + trace_id="research_agent1" #Add individual trace-id for your agent analytics + ) +) +``` + +### 4\. [Logs](/product/observability/logs) + +Agent runs are complex. Logs are essential for diagnosing issues, understanding agent behavior, and improving performance. They provide a detailed record of agent activities and tool use, which is crucial for debugging and optimizing processes. + +Portkey offers comprehensive logging features that capture detailed information about every action and decision made by your AI agents. Access a dedicated section to view records of agent executions, including parameters, outcomes, function calls, and errors. Filter logs based on multiple parameters such as trace ID, model, tokens used, and metadata. + + + + +### 5\. [Continuous Improvement](/product/observability/feedback) + +Improve your Agent runs by capturing qualitative & quantitative user feedback on your requests. Portkey's Feedback APIs provide a simple way to get weighted feedback from customers on any request you served, at any stage in your app. You can capture this feedback on a request or conversation level and analyze it by adding meta data to the relevant request. + +### 6\. [Caching](/product/ai-gateway/cache-simple-and-semantic) + +Agent runs are time-consuming and expensive due to their complex pipelines. Caching can significantly reduce these costs by storing frequently used data and responses. Portkey offers a built-in caching system that stores past responses, reducing the need for agent calls saving both time and money. + +```py +{ + "cache": { + "mode": "semantic" // Choose between "simple" or "semantic" + } +} +``` + +### 7\. [Security & Compliance](/product/enterprise-offering/security-portkey) + +Set budget limits on provider API keys and implement fine-grained user roles and permissions for both the app and the Portkey APIs. + +## [Portkey Config](/product/ai-gateway/configs) + +Many of these features are driven by Portkey's Config architecture. The Portkey app simplifies creating, managing, and versioning your Configs. + +For more information on using these features and setting up your Config, please refer to the [Portkey documentation](https://docs.portkey.ai). diff --git a/virtual_key_old/integrations/agents/pydantic-ai.mdx b/virtual_key_old/integrations/agents/pydantic-ai.mdx new file mode 100644 index 00000000..a1aeb76d --- /dev/null +++ b/virtual_key_old/integrations/agents/pydantic-ai.mdx @@ -0,0 +1,1190 @@ +--- +title: "Pydantic AI" +description: "Use Portkey with PydanticAI to take your AI Agents to production" +--- + +## Introduction + +PydanticAI is a Python agent framework designed to make it less painful to build production-grade applications with Generative AI. It brings the same ergonomic design and developer experience to GenAI that FastAPI brought to web development. + +Portkey enhances PydanticAI with production-readiness features, turning your experimental agents into robust systems by providing: + +- **Complete observability** of every agent step, tool use, and interaction +- **Built-in reliability** with fallbacks, retries, and load balancing +- **Cost tracking and optimization** to manage your AI spend +- **Access to 200+ LLMs** through a single integration +- **Guardrails** to keep agent behavior safe and compliant +- **Version-controlled prompts** for consistent agent performance + + + Learn more about PydanticAI's core concepts and features + + +### Installation & Setup + + + +```bash +pip install -U pydantic-ai portkey-ai +``` + + + +Create a Portkey API key with optional budget/rate limits from the [Portkey dashboard](https://app.portkey.ai/). You can attach configurations for reliability, caching, and more to this key. + + + +For a simple setup, first configure the Portkey client that will be used with PydanticAI: + +```python +from portkey_ai import AsyncPortkey + +# Set up Portkey client with appropriate metadata for tracking +portkey_client = AsyncPortkey( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_PROVIDER_VIRTUAL_KEY", # Optional, if using virtual keys + trace_id="unique-trace-id", # Optional, for request tracing + metadata={ # Optional, for request segmentation + "app_env": "production", + "_user": "user_123" # Special _user field for user analytics + } +) +``` + + +**What are Virtual Keys?** Virtual keys in Portkey securely store your LLM provider API keys (OpenAI, Anthropic, etc.) in an encrypted vault. They allow for easier key rotation and budget management. [Learn more about virtual keys here](/product/ai-gateway/virtual-keys). + + + + +After setting up your Portkey client, you can integrate it with PydanticAI by connecting it to a model provider: + +```python +from pydantic_ai import Agent +from pydantic_ai.models.openai import OpenAIModel +from pydantic_ai.providers.openai import OpenAIProvider + +# Connect Portkey client to a PydanticAI model via provider +agent = Agent( + model=OpenAIModel( + model_name="gpt-4o", + provider=OpenAIProvider(openai_client=portkey_client), + ), + system_prompt="You are a helpful assistant." +) +``` + + + +## Basic Agent Implementation + +Let's create a simple structured output agent with PydanticAI and Portkey. This agent will respond to a query about Formula 1 and return structured data: + +```python +from portkey_ai import AsyncPortkey +from pydantic import BaseModel, Field +from pydantic_ai import Agent +from pydantic_ai.models.openai import OpenAIModel +from pydantic_ai.providers.openai import OpenAIProvider + +# Set up Portkey client with tracing and metadata +portkey_client = AsyncPortkey( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + trace_id="f1-data-request", + metadata={"app_env": "production", "_user": "user_123"} +) + +# Define structured output using Pydantic +class F1GrandPrix(BaseModel): + gp_name: str = Field(description="Grand Prix name, e.g. `Emilia Romagna Grand Prix`") + year: int = Field(description="The year of the Grand Prix") + constructor_winner: str = Field(description="The winning constructor of the Grand Prix") + podium: list[str] = Field(description="Names of the podium drivers (1st, 2nd, 3rd)") + +# Create the agent with structured output type +f1_gp_agent = Agent[None, F1GrandPrix]( + model=OpenAIModel( + model_name="gpt-4o", + provider=OpenAIProvider(openai_client=portkey_client), + ), + output_type=F1GrandPrix, + system_prompt="Assist the user by providing data about the specified Formula 1 Grand Prix" +) + +# Run the agent +async def main(): + result = await f1_gp_agent.run("Las Vegas 2023") + print(result.output) + +if __name__ == "__main__": + import asyncio + asyncio.run(main()) +``` + +The output will be a structured `F1GrandPrix` object with all fields properly typed and validated: + +```json +gp_name='Las Vegas Grand Prix' +year=2023 +constructor_winner='Red Bull Racing' +podium=['Max Verstappen', 'Charles Leclerc', 'Sergio Pรฉrez'] +``` + +You can also use the synchronous API if preferred: + +```python +result = f1_gp_agent.run_sync("Las Vegas 2023") +print(result.output) +``` + +## Advanced Features + +### Working with Images + +PydanticAI supports multimodal inputs including images. Here's how to use Portkey with a vision model: + +```python +from portkey_ai import AsyncPortkey +from pydantic_ai import Agent, ImageUrl +from pydantic_ai.models.openai import OpenAIModel +from pydantic_ai.providers.openai import OpenAIProvider + +# Set up Portkey client +portkey_client = AsyncPortkey( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + trace_id="vision-request", + metadata={"request_type": "image_analysis"} +) + +# Create a vision-capable agent +vision_agent = Agent( + model=OpenAIModel( + model_name="gpt-4o", # Vision-capable model + provider=OpenAIProvider(openai_client=portkey_client), + ), + system_prompt="Analyze images and provide detailed descriptions." +) + +# Process an image +result = vision_agent.run_sync([ + 'What company is this logo from?', + ImageUrl(url='https://iili.io/3Hs4FMg.png'), +]) +print(result.output) +``` + +Visit your Portkey dashboard to see detailed logs of this image analysis request, including token usage and costs. + +### Tools and Tool Calls + +PydanticAI provides a powerful tools system that integrates seamlessly with Portkey. Here's how to create an agent with tools: + +```python +import random +from portkey_ai import AsyncPortkey +from pydantic_ai import Agent, RunContext +from pydantic_ai.models.openai import OpenAIModel +from pydantic_ai.providers.openai import OpenAIProvider + +# Set up Portkey client +portkey_client = AsyncPortkey( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + trace_id="dice-game-session", + metadata={"game_type": "dice"} +) + +# Create an agent with dependency injection (player name) +dice_agent = Agent( + model=OpenAIModel( + model_name="gpt-4o", + provider=OpenAIProvider(openai_client=portkey_client), + ), + deps_type=str, # Dependency type (player name as string) + system_prompt=( + "You're a dice game host. Roll the die and see if it matches " + "the user's guess. If so, tell them they're a winner. " + "Use the player's name in your response." + ), +) + +# Define a plain tool (no context needed) +@dice_agent.tool_plain +def roll_die() -> str: + """Roll a six-sided die and return the result.""" + return str(random.randint(1, 6)) + +# Define a tool that uses the dependency +@dice_agent.tool +def get_player_name(ctx: RunContext[str]) -> str: + """Get the player's name.""" + return ctx.deps + +# Run the agent +dice_result = dice_agent.run_sync('My guess is 4', deps='Anne') +print(dice_result.output) +``` + + +Portkey logs each tool call separately, allowing you to analyze the full execution path of your agent, including both LLM calls and tool invocations. + + +### Multi-agent Applications + +PydanticAI excels at creating multi-agent systems where agents can call each other. Here's how to integrate Portkey with a multi-agent setup: + +This multi-agent system uses three specialized agents: +`search_agent` - Orchestrates the flow and validates flight selections +`extraction_agent` - Extracts structured flight data from raw text +`seat_preference_agent` - Interprets user's seat preferences + +With Portkey integration, you get: + +- Unified tracing across all three agents +- Token and cost tracking for the entire workflow +- Ability to set usage limits across the entire system +- Observability of both AI and human interaction points + +Here's a diagram of how these agents interact: + +```mermaid +graph TD + A[START] --> B[search agent] + B --> C[extraction agent] + C --> B + B --> D[human confirm] + D --> E[find seat function] + B --> F[FAILED] + E --> G[human seat choice] + G --> H[find seat agent] + H --> E + E --> I[buy flights] + I --> J[SUCCESS] +``` + + +```python [expandable] +import datetime +from dataclasses import dataclass +from typing import Literal + +from pydantic import BaseModel, Field +from rich.prompt import Prompt + +from pydantic_ai import Agent, ModelRetry, RunContext +from pydantic_ai.messages import ModelMessage +from pydantic_ai.usage import Usage, UsageLimits +from portkey_ai import AsyncPortkey + +# Set up Portkey clients with shared trace ID for connected tracing +portkey_client = AsyncPortkey( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + trace_id="flight-booking-session", + metadata={"app_type": "flight_booking"} +) + +# Define structured output types +class FlightDetails(BaseModel): + """Details of the most suitable flight.""" + flight_number: str + price: int + origin: str = Field(description='Three-letter airport code') + destination: str = Field(description='Three-letter airport code') + date: datetime.date + +class NoFlightFound(BaseModel): + """When no valid flight is found.""" + +class SeatPreference(BaseModel): + row: int = Field(ge=1, le=30) + seat: Literal['A', 'B', 'C', 'D', 'E', 'F'] + +class Failed(BaseModel): + """Unable to extract a seat selection.""" + +# Dependencies for flight search +@dataclass +class Deps: + web_page_text: str + req_origin: str + req_destination: str + req_date: datetime.date + +# This agent is responsible for controlling the flow of the conversation +from pydantic_ai.models.openai import OpenAIModel +from pydantic_ai.providers.openai import OpenAIProvider + +search_agent = Agent[Deps, FlightDetails | NoFlightFound]( + model=OpenAIModel( + model_name="gpt-4o", + provider=OpenAIProvider(openai_client=portkey_client), + ), + output_type=FlightDetails | NoFlightFound, # type: ignore + retries=4, + system_prompt=( + 'Your job is to find the cheapest flight for the user on the given date. ' + ), + instrument=True, # Enable instrumentation for better tracing +) + +# This agent is responsible for extracting flight details from web page text +extraction_agent = Agent( + model=OpenAIModel( + model_name="gpt-4o", + provider=OpenAIProvider(openai_client=portkey_client), + ), + output_type=list[FlightDetails], + system_prompt='Extract all the flight details from the given text.', +) + +# This agent is responsible for extracting the user's seat selection +seat_preference_agent = Agent[None, SeatPreference | Failed]( + model=OpenAIModel( + model_name="gpt-4o", + provider=OpenAIProvider(openai_client=portkey_client), + ), + output_type=SeatPreference | Failed, # type: ignore + system_prompt=( + "Extract the user's seat preference. " + 'Seats A and F are window seats. ' + 'Row 1 is the front row and has extra leg room. ' + 'Rows 14, and 20 also have extra leg room. ' + ), +) + +@search_agent.tool +async def extract_flights(ctx: RunContext[Deps]) -> list[FlightDetails]: + """Get details of all flights.""" + # Pass the usage to track nested agent calls + result = await extraction_agent.run(ctx.deps.web_page_text, usage=ctx.usage) + return result.output + +@search_agent.output_validator +async def validate_output( + ctx: RunContext[Deps], output: FlightDetails | NoFlightFound +) -> FlightDetails | NoFlightFound: + """Procedural validation that the flight meets the constraints.""" + if isinstance(output, NoFlightFound): + return output + + errors: list[str] = [] + if output.origin != ctx.deps.req_origin: + errors.append( + f'Flight should have origin {ctx.deps.req_origin}, not {output.origin}' + ) + if output.destination != ctx.deps.req_destination: + errors.append( + f'Flight should have destination {ctx.deps.req_destination}, not {output.destination}' + ) + if output.date != ctx.deps.req_date: + errors.append(f'Flight should be on {ctx.deps.req_date}, not {output.date}') + + if errors: + raise ModelRetry('\n'.join(errors)) + else: + return output + +# Sample flight data (in a real application, this would be from a web scraper) +flights_web_page = """ +1. Flight SFO-AK123 +- Price: $350 +- Origin: San Francisco International Airport (SFO) +- Destination: Ted Stevens Anchorage International Airport (ANC) +- Date: January 10, 2025 + +2. Flight SFO-AK456 +- Price: $370 +- Origin: San Francisco International Airport (SFO) +- Destination: Fairbanks International Airport (FAI) +- Date: January 10, 2025 + +... more flights ... +""" + +# Main application flow +async def main(): + # Restrict how many requests this app can make to the LLM + usage_limits = UsageLimits(request_limit=15) + + deps = Deps( + web_page_text=flights_web_page, + req_origin='SFO', + req_destination='ANC', + req_date=datetime.date(2025, 1, 10), + ) + message_history: list[ModelMessage] | None = None + usage: Usage = Usage() + + # Run the agent until a satisfactory flight is found + while True: + result = await search_agent.run( + f'Find me a flight from {deps.req_origin} to {deps.req_destination} on {deps.req_date}', + deps=deps, + usage=usage, + message_history=message_history, + usage_limits=usage_limits, + ) + if isinstance(result.output, NoFlightFound): + print('No flight found') + break + else: + flight = result.output + print(f'Flight found: {flight}') + answer = Prompt.ask( + 'Do you want to buy this flight, or keep searching? (buy/*search)', + choices=['buy', 'search', ''], + show_choices=False, + ) + if answer == 'buy': + seat = await find_seat(usage, usage_limits) + await buy_tickets(flight, seat) + break + else: + message_history = result.all_messages( + output_tool_return_content='Please suggest another flight' + ) + +async def find_seat(usage: Usage, usage_limits: UsageLimits) -> SeatPreference: + message_history: list[ModelMessage] | None = None + while True: + answer = Prompt.ask('What seat would you like?') + result = await seat_preference_agent.run( + answer, + message_history=message_history, + usage=usage, + usage_limits=usage_limits, + ) + if isinstance(result.output, SeatPreference): + return result.output + else: + print('Could not understand seat preference. Please try again.') + message_history = result.all_messages() + +async def buy_tickets(flight_details: FlightDetails, seat: SeatPreference): + print(f'Purchasing flight {flight_details=!r} {seat=!r}...') +``` + +Portkey preserves all the type safety of PydanticAI while adding production monitoring and reliability. + +## Production Features + +### 1. Enhanced Observability + +Portkey provides comprehensive observability for your PydanticAI agents, helping you understand exactly what's happening during each execution. + + + + + + + +Traces provide a hierarchical view of your agent's execution, showing the sequence of LLM calls, tool invocations, and state transitions. + +```python +# Add trace_id to enable hierarchical tracing in Portkey +portkey_client = AsyncPortkey( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_LLM_PROVIDER_VIRTUAL_KEY", + trace_id="unique-session-id", # Add unique trace ID + metadata={"request_type": "user_query"} +) +``` + + + + + + + +Portkey logs every interaction with LLMs, including: + +- Complete request and response payloads +- Latency and token usage metrics +- Cost calculations +- Tool calls and function executions + +All logs can be filtered by metadata, trace IDs, models, and more, making it easy to debug specific agent runs. + + + + + + + +Portkey provides built-in dashboards that help you: + +- Track cost and token usage across all agent runs +- Analyze performance metrics like latency and success rates +- Identify bottlenecks in your agent workflows +- Compare different agent configurations and LLMs + +You can filter and segment all metrics by custom metadata to analyze specific agent types, user groups, or use cases. + + + + + Analytics with metadata filters + + +Add custom metadata to your PydanticAI agent calls to enable powerful filtering and segmentation: + +```python +portkey_client = AsyncPortkey( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_LLM_PROVIDER_VIRTUAL_KEY", + metadata={ + "agent_type": "weather_agent", + "environment": "production", + "_user": "user_123", # Special _user field for user analytics + "request_source": "mobile_app" + } +) +``` + +This metadata can be used to filter logs, traces, and metrics on the Portkey dashboard, allowing you to analyze specific agent runs, users, or environments. + + + +### 2. Reliability - Keep Your Agents Running Smoothly + +When running agents in production, things can go wrong - API rate limits, network issues, or provider outages. Portkey's reliability features ensure your agents keep running smoothly even when problems occur. + +It's simple to enable fallback in your PydanticAI agents by using a Portkey Config: + +```python +from portkey_ai import AsyncPortkey + +# Create Portkey client with fallback config +portkey_client = AsyncPortkey( + api_key="YOUR_PORTKEY_API_KEY", + config={ + "strategy": { + "mode": "fallback" + }, + "targets": [ + { + "provider": "openai", + "api_key": "YOUR_OPENAI_API_KEY", + "override_params": {"model": "gpt-4o"} + }, + { + "provider": "anthropic", + "api_key": "YOUR_ANTHROPIC_API_KEY", + "override_params": {"model": "claude-3-opus-20240229"} + } + ] + } +) +``` + +This configuration will automatically try Claude if the GPT-4o request fails, ensuring your agent can continue operating. + + + + Handles temporary failures automatically. If an LLM call fails, Portkey will retry the same request for the specified number of times - perfect for rate limits or network blips. + + + Prevent your agents from hanging. Set timeouts to ensure you get responses (or can fail gracefully) within your required timeframes. + + + Send different requests to different providers. Route complex reasoning to GPT-4, creative tasks to Claude, and quick responses to Gemini based on your needs. + + + Keep running even if your primary provider fails. Automatically switch to backup providers to maintain availability. + + + Spread requests across multiple API keys or providers. Great for high-volume agent operations and staying within rate limits. + + + +### 3. Prompting in PydanticAI + +Portkey's Prompt Engineering Studio helps you create, manage, and optimize the prompts used in your PydanticAI agents. Instead of hardcoding prompts or instructions, use Portkey's prompt rendering API to dynamically fetch and apply your versioned prompts. + + +![Prompt Playground Interface](/images/product/ai-gateway/ai-20.webp) + + + + +Prompt Playground is a place to compare, test and deploy perfect prompts for your AI application. It's where you experiment with different models, test variables, compare outputs, and refine your prompt engineering strategy before deploying to production. It allows you to: + +1. Iteratively develop prompts before using them in your agents +2. Test prompts with different variables and models +3. Compare outputs between different prompt versions +4. Collaborate with team members on prompt development + +This visual environment makes it easier to craft effective prompts for each step in your PydanticAI agent's workflow. + + + +The Prompt Render API retrieves your prompt templates with all parameters configured: + +```python +from portkey_ai import Portkey, AsyncPortkey +from pydantic_ai import Agent +from pydantic_ai.models.openai import OpenAIModel +from pydantic_ai.providers.openai import OpenAIProvider + +# Initialize Portkey clients +portkey_admin = Portkey(api_key="YOUR_PORTKEY_API_KEY") +portkey_client = AsyncPortkey(api_key="YOUR_PORTKEY_API_KEY", virtual_key="YOUR_OPENAI_VIRTUAL_KEY") + +# Retrieve prompt using the render API +prompt_data = portkey_admin.prompts.render( + prompt_id="YOUR_PROMPT_ID", + variables={ + "user_input": "Tell me about artificial intelligence" + } +) + +# Use the rendered prompt in your PydanticAI agent +agent = Agent( + model=OpenAIModel( + model_name="gpt-4o", + provider=OpenAIProvider(openai_client=portkey_client), + ), + system_prompt=prompt_data.data.messages[0]["content"] # Use the rendered prompt +) + +result = agent.run_sync("Tell me about artificial intelligence") +print(result.output) +``` + + + +You can: +- Create multiple versions of the same prompt +- Compare performance between versions +- Roll back to previous versions if needed +- Specify which version to use in your code: + +```python +# Use a specific prompt version +prompt_data = portkey_admin.prompts.render( + prompt_id="YOUR_PROMPT_ID@version_number", + variables={ + "user_input": "Tell me about quantum computing" + } +) +``` + + + +Portkey prompts use Mustache-style templating for easy variable substitution: + +``` +You are an AI assistant helping with {{task_type}}. + +User question: {{user_input}} + +Please respond in a {{tone}} tone and include {{required_elements}}. +``` + +When rendering, simply pass the variables: + +```python +prompt_data = portkey_admin.prompts.render( + prompt_id="YOUR_PROMPT_ID", + variables={ + "task_type": "research", + "user_input": "Tell me about quantum computing", + "tone": "professional", + "required_elements": "recent academic references" + } +) +``` + + + + + Learn more about Portkey's prompt management features + + +### 4. Guardrails for Safe Agents + +Guardrails ensure your PydanticAI agents operate safely and respond appropriately in all situations. + +**Why Use Guardrails?** + +PydanticAI agents can experience various failure modes: +- Generating harmful or inappropriate content +- Leaking sensitive information like PII +- Hallucinating incorrect information +- Generating outputs in incorrect formats + +While PydanticAI provides type safety for outputs, Portkey's guardrails add additional protections for both inputs and outputs. + +**Implementing Guardrails** + +```python +from portkey_ai import AsyncPortkey +from pydantic_ai import Agent +from pydantic_ai.models.openai import OpenAIModel +from pydantic_ai.providers.openai import OpenAIProvider + +# Create Portkey client with guardrails +portkey_client = AsyncPortkey( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + config={ + "input_guardrails": ["guardrails-id-xxx", "guardrails-id-yyy"], + "output_guardrails": ["guardrails-id-zzz"] + } +) + +# Create agent with Portkey-enabled client +agent = Agent( + model=OpenAIModel( + model_name="gpt-4o", + provider=OpenAIProvider(openai_client=portkey_client), + ), + system_prompt="You are a helpful assistant." +) +``` + +Portkey's guardrails can: +- Detect and redact PII in both inputs and outputs +- Filter harmful or inappropriate content +- Validate response formats against schemas +- Check for hallucinations against ground truth +- Apply custom business logic and rules + + + Explore Portkey's guardrail features to enhance agent safety + + +### 5. User Tracking with Metadata + +Track individual users through your PydanticAI agents using Portkey's metadata system. + +**What is Metadata in Portkey?** + +Metadata allows you to associate custom data with each request, enabling filtering, segmentation, and analytics. The special `_user` field is specifically designed for user tracking. + +```python +from portkey_ai import AsyncPortkey +from pydantic_ai import Agent +from pydantic_ai.models.openai import OpenAIModel +from pydantic_ai.providers.openai import OpenAIProvider + +# Configure client with user tracking +portkey_client = AsyncPortkey( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + metadata={ + "_user": "user_123", # Special _user field for user analytics + "user_tier": "premium", + "user_company": "Acme Corp", + "session_id": "abc-123" + } +) + +# Create agent with Portkey client +agent = Agent( + model=OpenAIModel( + model_name="gpt-4o", + provider=OpenAIProvider(openai_client=portkey_client), + ), + system_prompt="You are a helpful assistant." +) +``` + +**Filter Analytics by User** + +With metadata in place, you can filter analytics by user and analyze performance metrics on a per-user basis: + + + + + +This enables: +- Per-user cost tracking and budgeting +- Personalized user analytics +- Team or organization-level metrics +- Environment-specific monitoring (staging vs. production) + + + Explore how to use custom metadata to enhance your analytics + + +### 6. Caching for Efficient Agents + +Implement caching to make your PydanticAI agents more efficient and cost-effective: + + + +```python +from portkey_ai import AsyncPortkey +from pydantic_ai import Agent +from pydantic_ai.models.openai import OpenAIModel +from pydantic_ai.providers.openai import OpenAIProvider + +# Configure Portkey client with simple caching +portkey_client = AsyncPortkey( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + config={ + "cache": { + "mode": "simple" + } + } +) + +# Create agent with cached LLM calls +agent = Agent( + model=OpenAIModel( + model_name="gpt-4o", + provider=OpenAIProvider(openai_client=portkey_client), + ), + system_prompt="You are a helpful assistant." +) +``` + +Simple caching performs exact matches on input prompts, caching identical requests to avoid redundant model executions. + + + +```python +from portkey_ai import AsyncPortkey +from pydantic_ai import Agent +from pydantic_ai.models.openai import OpenAIModel +from pydantic_ai.providers.openai import OpenAIProvider + +# Configure Portkey client with semantic caching +portkey_client = AsyncPortkey( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_OPENAI_VIRTUAL_KEY", + config={ + "cache": { + "mode": "semantic" + } + } +) + +# Create agent with semantically cached LLM calls +agent = Agent( + model=OpenAIModel( + model_name="gpt-4o", + provider=OpenAIProvider(openai_client=portkey_client), + ), + system_prompt="You are a helpful assistant." +) +``` + +Semantic caching considers the contextual similarity between input requests, caching responses for semantically similar inputs. + + + +### 7. Model Interoperability + +PydanticAI supports multiple LLM providers, and Portkey extends this capability by providing access to over 200 LLMs through a unified interface. You can easily switch between different models without changing your core agent logic: + +```python +from portkey_ai import AsyncPortkey +from pydantic_ai import Agent +from pydantic_ai.models.openai import OpenAIModel +from pydantic_ai.providers.openai import OpenAIProvider + +# OpenAI with Portkey +portkey_openai = AsyncPortkey( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_OPENAI_VIRTUAL_KEY" +) + +# Anthropic with Portkey +portkey_anthropic = AsyncPortkey( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_ANTHROPIC_VIRTUAL_KEY" +) + +# Create agents with different models +openai_agent = Agent( + model=OpenAIModel( + model_name="gpt-4o", + provider=OpenAIProvider(openai_client=portkey_openai), + ), + system_prompt="You are a helpful assistant." +) + +anthropic_agent = Agent( + model=OpenAIModel( + model_name="claude-3-opus-20240229", + provider=OpenAIProvider(anthropic_client=portkey_anthropic), + ), + system_prompt="You are a helpful assistant." +) + +# Choose which agent to use based on your needs +active_agent = openai_agent # or anthropic_agent + +result = active_agent.run_sync("Tell me about quantum computing.") +print(result.output) +``` + +Portkey provides access to LLMs from providers including: + +- OpenAI (GPT-4o, GPT-4 Turbo, etc.) +- Anthropic (Claude 3.5 Sonnet, Claude 3 Opus, etc.) +- Mistral AI (Mistral Large, Mistral Medium, etc.) +- Google Vertex AI (Gemini 1.5 Pro, etc.) +- Cohere (Command, Command-R, etc.) +- AWS Bedrock (Claude, Titan, etc.) +- Local/Private Models + + + See the full list of LLM providers supported by Portkey + + +## Set Up Enterprise Governance for PydanticAI + +**Why Enterprise Governance?** +If you are using PydanticAI inside your organization, you need to consider several governance aspects: +- **Cost Management**: Controlling and tracking AI spending across teams +- **Access Control**: Managing which teams can use specific models +- **Usage Analytics**: Understanding how AI is being used across the organization +- **Security & Compliance**: Maintaining enterprise security standards +- **Reliability**: Ensuring consistent service across all users + +Portkey adds a comprehensive governance layer to address these enterprise needs. Let's implement these controls step by step. + + + +Virtual Keys are Portkey's secure way to manage your LLM provider API keys. They provide essential controls like: +- Budget limits for API usage +- Rate limiting capabilities +- Secure API key storage + +To create a virtual key: +Go to [Virtual Keys](https://app.portkey.ai/virtual-keys) in the Portkey App. Save and copy the virtual key ID + + + + + + +Save your virtual key ID - you'll need it for the next step. + + + + +Configs in Portkey define how your requests are routed, with features like advanced routing, fallbacks, and retries. + +To create your config: +1. Go to [Configs](https://app.portkey.ai/configs) in Portkey dashboard +2. Create new config with: + ```json + { + "virtual_key": "YOUR_VIRTUAL_KEY_FROM_STEP1", + "override_params": { + "model": "gpt-4o" // Your preferred model name + } + } + ``` +3. Save and note the Config name for the next step + + + + + + + +Now create a Portkey API key and attach the config you created in Step 2: + +1. Go to [API Keys](https://app.portkey.ai/api-keys) in Portkey and Create new API key +2. Select your config from `Step 2` +3. Generate and save your API key + + + + + + + +After setting up your Portkey API key with the attached config, connect it to your PydanticAI agents: + +```python +from portkey_ai import AsyncPortkey +from pydantic_ai import Agent +from pydantic_ai.models.openai import OpenAIModel +from pydantic_ai.providers.openai import OpenAIProvider + +# Configure Portkey client with your API key +portkey_client = AsyncPortkey( + api_key="YOUR_PORTKEY_API_KEY" # The API key with attached config from step 3 +) + +# Create agent with Portkey client +agent = Agent( + model=OpenAIModel( + model_name="gpt-4o", + provider=OpenAIProvider(openai_client=portkey_client), + ), + system_prompt="You are a helpful assistant." +) +``` + + + + + +### Step 1: Implement Budget Controls & Rate Limits + +Virtual Keys enable granular control over LLM access at the team/department level. This helps you: +- Set up [budget limits](/product/ai-gateway/virtual-keys/budget-limits) +- Prevent unexpected usage spikes using Rate limits +- Track departmental spending + +#### Setting Up Department-Specific Controls: +1. Navigate to [Virtual Keys](https://app.portkey.ai/virtual-keys) in Portkey dashboard +2. Create new Virtual Key for each department with budget limits and rate limits +3. Configure department-specific limits + + + + + + + +### Step 2: Define Model Access Rules + +As your AI usage scales, controlling which teams can access specific models becomes crucial. Portkey Configs provide this control layer with features like: + +#### Access Control Features: +- **Model Restrictions**: Limit access to specific models +- **Data Protection**: Implement guardrails for sensitive data +- **Reliability Controls**: Add fallbacks and retry logic + +#### Example Configuration: +Here's a basic configuration to route requests to OpenAI, specifically using GPT-4o: + +```json +{ + "strategy": { + "mode": "single" + }, + "targets": [ + { + "virtual_key": "YOUR_OPENAI_VIRTUAL_KEY", + "override_params": { + "model": "gpt-4o" + } + } + ] +} +``` + + Create your config on the [Configs page](https://app.portkey.ai/configs) in your Portkey dashboard. + + + Configs can be updated anytime to adjust controls without affecting running applications. + + + + + ### Step 3: Implement Access Controls + + Create User-specific API keys that automatically: + - Track usage per user/team with the help of virtual keys + - Apply appropriate configs to route requests + - Collect relevant metadata to filter logs + - Enforce access permissions + + Create API keys through: + - [Portkey App](https://app.portkey.ai/) + - [API Key Management API](/api-reference/admin-api/control-plane/api-keys/create-api-key) + + Example using Python SDK: + ```python + from portkey_ai import Portkey + + portkey = Portkey(api_key="YOUR_ADMIN_API_KEY") + + api_key = portkey.api_keys.create( + name="engineering-team", + type="organisation", + workspace_id="YOUR_WORKSPACE_ID", + defaults={ + "config_id": "your-config-id", + "metadata": { + "environment": "production", + "department": "engineering" + } + }, + scopes=["logs.view", "configs.read"] + ) + ``` + + For detailed key management instructions, see our [API Keys documentation](/api-reference/admin-api/control-plane/api-keys/create-api-key). + + + + ### Step 4: Deploy & Monitor + After distributing API keys to your team members, your enterprise-ready PydanticAI setup is ready to go. Each team member can now use their designated API keys with appropriate access levels and budget controls. + + Monitor usage in Portkey dashboard: + - Cost tracking by department + - Model usage patterns + - Request volumes + - Error rates + + + + + +### Enterprise Features Now Available +**Your PydanticAI integration now has:** +- Departmental budget controls +- Model access governance +- Usage tracking & attribution +- Security guardrails +- Reliability features + + +## Frequently Asked Questions + + + + Portkey adds production-readiness to PydanticAI through comprehensive observability (traces, logs, metrics), reliability features (fallbacks, retries, caching), and access to 200+ LLMs through a unified interface. This makes it easier to debug, optimize, and scale your agent applications, all while preserving PydanticAI's strong type safety. + + + + Yes! Portkey integrates seamlessly with existing PydanticAI applications. You just need to replace your client initialization code with the Portkey-enabled version. The rest of your agent code remains unchanged and continues to benefit from PydanticAI's strong typing. + + + + Portkey supports all PydanticAI features, including structured outputs, tool use, multi-agent systems, and more. It adds observability and reliability without limiting any of the framework's functionality. + + + + Yes, Portkey allows you to use a consistent `trace_id` across multiple agents and requests to track the entire workflow. This is especially useful for multi-agent systems where you want to understand the full execution path. + + + + Portkey allows you to add custom metadata to your agent runs, which you can then use for filtering. Add fields like `agent_name`, `agent_type`, or `session_id` to easily find and analyze specific agent executions. + + + + Yes! Portkey uses your own API keys for the various LLM providers. It securely stores them as virtual keys, allowing you to easily manage and rotate keys without changing your code. + + + + +## Resources + + + +

Official PydanticAI documentation

+
+ + +

Official Portkey documentation

+
+ + +

Get personalized guidance on implementing this integration

+
+
diff --git a/virtual_key_old/integrations/agents/strands-backup.mdx b/virtual_key_old/integrations/agents/strands-backup.mdx new file mode 100644 index 00000000..6fb6d8ee --- /dev/null +++ b/virtual_key_old/integrations/agents/strands-backup.mdx @@ -0,0 +1,520 @@ +--- +title: "Strands Agents" +description: "Use Portkey with AWS's Strands Agents to take your AI Agents to production" +--- + +Strands Agents is a simple-to-use agent framework built by AWS. + +Portkey enhances Strands Agents with production-readiness features, turning your experimental agents into robust systems by providing: + +- **Complete observability** of every agent step, tool use, and interaction +- **Built-in reliability** with fallbacks, retries, and load balancing +- **Cost tracking and optimization** to manage your AI spend +- **Access to 200+ LLMs** through a single integration +- **Guardrails** to keep agent behavior safe and compliant +- **Version-controlled prompts** for consistent agent performance + + + Learn more about Strands Agents' core concepts and features + + + + +```bash +pip install -U strands-agents strands-agents-tools openai portkey-ai +``` + + +Instantiate your Strands `OpenAIModel` with Portkey: +```python +from strands.models.openai import OpenAIModel +from portkey_ai import PORTKEY_GATEWAY_URL + +model = OpenAIModel( + client_args={"api_key": "YOUR_PORTKEY_API_KEY", "base_url": PORTKEY_GATEWAY_URL}, + model_id="gpt-4o", + params={"temperature": 0.7} +) +``` + + +```python +from strands import Agent +from strands_tools import calculator + +agent = Agent(model=model, tools=[calculator]) +response = agent("What is 2+2") +print(response) +``` + + + +## Integration + +Portkey works out of the box with Strands Agents and supports all of Portkey + Strands functionalities because of our end-to-end support for the OpenAI API. You can directly import the OpenAI Model class inside Strands, set the base URL to Portkey Gateway URL, and unlock all of Portkey functionalities. Here's how: + +### Portkey Setup + +First, let's setup your provider keys and settings on Portkey, that you can later use in Strands with your Portkey API key. + + + +Go to [Virtual Keys](https://app.portkey.ai/virtual-keys) in the Portkey App to add your AI provider key and copy the virtual key ID. + + + +Go to [Configs](https://app.portkey.ai/configs) in the Portkey App, create a new config that uses your virtual key, then save the Config ID. + + + +Go to [API Keys](https://app.portkey.ai/api-keys) in the Portkey App to generate a new API key and attach your Config as the default routing. + + + +That's it! With this, you unlock all Portkey functionalities for use with your Strands Agents! + +### Strands Setup + +Now, let's setup Strands Agents to use the Portkey API key we just created. + + + +```bash +pip install -U strands-agents strands-agents-tools openai portkey-ai +``` + + + + +When you instantiate the `OpenAIModel`, set the `base_url` to Portkey's Gateway URL and pass your Portkey API Key directly in as the main API key. +```python +from strands.models.openai import OpenAIModel +from portkey_ai import PORTKEY_GATEWAY_URL + +portkey_model = OpenAIModel( + client_args={ + "api_key": "YOUR_PORTKEY_API_KEY", + "base_url": PORTKEY_GATEWAY_URL + }, + model_id="gpt-4o", + params={ + "temperature": 0.7 + } +) +``` + +That's it! With this, you unlock all Portkey functionalities to be used along with your Strands Agents! + + +Portkey logs all of your Strands requests in the [Logs dashboard](https://app.portkey.ai/logs). +Portkey Logs screenshot + + + +## End-to-end Example + +```python +from strands import Agent +from strands.models.openai import OpenAIModel +from strands_tools import calculator +from portkey_ai import PORTKEY_GATEWAY_URL + +model = OpenAIModel( + client_args={ + "api_key": "YOUR_PORTKEY_API_KEY", + "base_url": PORTKEY_GATEWAY_URL + }, + model_id="gpt-4o", + params={ + "max_tokens": 1000, + "temperature": 0.7, + } +) + +agent = Agent(model=model, tools=[calculator]) +response = agent("What is 2+2") +print(response) +``` + +We've demonstrated a simple working integration between Portkey & Strands. Check below for all the advanced functionalities Portkey offers for your Strands Agents. + +## Production Features + +### 1. Enhanced Observability + +Portkey provides comprehensive observability for your Strands agents, helping you understand exactly what's happening during each execution. + + + + + + + +Traces provide a hierarchical view of your agent's execution, showing the sequence of LLM calls, tool invocations, and state transitions. + +```python {10} +from strands import Agent +from strands.models.openai import OpenAIModel +from strands_tools import calculator +from portkey_ai import PORTKEY_GATEWAY_URL,createHeaders + +model = OpenAIModel( + client_args={ + "api_key": "YOUR_PORTKEY_API_KEY", + "base_url": PORTKEY_GATEWAY_URL, + "default_headers":createHeaders(trace_id="strands") + }, + model_id="gpt-4o", + params={ + "max_tokens": 1000, + "temperature": 0.7, + } +) + +agent = Agent(model=model, tools=[calculator]) +response = agent("What is 2+2") +print(response) +``` + + + + + + + +Portkey logs every interaction with LLMs, including: + +- Complete request and response payloads +- Latency and token usage metrics +- Cost calculations +- Tool calls and function executions + +All logs can be filtered by metadata, trace IDs, models, and more, making it easy to debug specific agent runs. + + + + + + + +Portkey provides built-in dashboards that help you: + +- Track cost and token usage across all agent runs +- Analyze performance metrics like latency and success rates +- Identify bottlenecks in your agent workflows +- Compare different agent configurations and LLMs + +You can filter and segment all metrics by custom metadata to analyze specific agent types, user groups, or use cases. + + + + + Analytics with metadata filters + + +Add custom metadata to your Strands calls to enable powerful filtering and segmentation: + +```python +from strands import Agent +from strands.models.openai import OpenAIModel +from strands_tools import calculator +from portkey_ai import PORTKEY_GATEWAY_URL,createHeaders + +model = OpenAIModel( + client_args={ + "api_key": "YOUR_PORTKEY_API_KEY", + "base_url": PORTKEY_GATEWAY_URL, + "default_headers":createHeaders( + trace_id="strands", + metadata={ + "agent_type": "strands_agent", + "environment": "production", + "_user": "user_123", + "request_source": "mobile_app" + } + ) + }, + model_id="gpt-4o", + params={ + "max_tokens": 1000, + "temperature": 0.7, + } +) + +agent = Agent(model=model, tools=[calculator]) +response = agent("What is 2+2") +print(response) +``` + +This metadata can be used to filter logs, traces, and metrics on the Portkey dashboard, allowing you to analyze specific agent runs, users, or environments. + + + +### 2. Reliability - Keep Your Agents Running Smoothly + +When running agents in production, things can go wrong - API rate limits, network issues, or provider outages. Portkey's reliability features ensure your agents keep running smoothly even when problems occur. + +It's simple to enable fallback in your Strands Agents by using a Portkey Config that you can attach at runtime or directly to your Portkey API key. Here's an example of attaching a Config at runtime: + +```python {11-20} +from strands import Agent +from strands.models.openai import OpenAIModel +from strands_tools import calculator +from portkey_ai import PORTKEY_GATEWAY_URL,createHeaders + +model = OpenAIModel( + client_args={ + "api_key": "YOUR_PORTKEY_API_KEY", + "base_url": PORTKEY_GATEWAY_URL, + "default_headers":createHeaders( + config={ + "strategy": { + "mode": "fallback", + "on_status_codes": [429] + }, + "targets": [ + { "virtual_key": "azure-81fddb" }, + { "virtual_key": "open-ai-key-66a67d" } + ] + } + ) + }, + model_id="gpt-4o", + params={ + "max_tokens": 1000, + "temperature": 0.7, + } +) + +agent = Agent(model=model, tools=[calculator]) +response = agent("What is 2+2") +print(response) +``` + +This configuration will automatically try GPT-4o on OpenAI if the Azure deployment fails, ensuring your agent can continue operating. + + + + Handles temporary failures automatically. If an LLM call fails, Portkey will retry the same request for the specified number of times - perfect for rate limits or network blips. + + + Prevent your agents from hanging. Set timeouts to ensure you get responses (or can fail gracefully) within your required timeframes. + + + Send different requests to different providers. Route complex reasoning to GPT-4, creative tasks to Claude, and quick responses to Gemini based on your needs. + + + Keep running even if your primary provider fails. Automatically switch to backup providers to maintain availability. + + + Spread requests across multiple API keys or providers. Great for high-volume agent operations and staying within rate limits. + + + +### 3. Guardrails for Safe Agents + +Guardrails ensure your Strands agents operate safely and respond appropriately in all situations. + +**Why Use Guardrails?** + +Strands agents can experience various failure modes: +- Generating harmful or inappropriate content +- Leaking sensitive information like PII +- Hallucinating incorrect information +- Generating outputs in incorrect formats + +Portkey's guardrails can: +- Detect and redact PII in both inputs and outputs +- Filter harmful or inappropriate content +- Validate response formats against schemas +- Check for hallucinations against ground truth +- Apply custom business logic and rules + + + Explore Portkey's guardrail features to enhance agent safety + + +### 4. Model Interoperability + +Strands supports multiple LLM providers, and Portkey extends this capability by providing access to over 200 LLMs through a unified interface. You can easily switch between different models without changing your core agent logic: + +```python +from strands import Agent +from strands.models.openai import OpenAIModel +from strands_tools import calculator +from portkey_ai import PORTKEY_GATEWAY_URL,createHeaders + +model = OpenAIModel( + client_args={ + "api_key": "YOUR_PORTKEY_API_KEY", + "base_url": PORTKEY_GATEWAY_URL, + "default_headers":createHeaders( + provider="anthropic", + api_key="ANTHROPIC_API_KEY" + ) + }, + model_id="claude-3-7-sonnet-latest", + params={ + "max_tokens": 1000, + "temperature": 0.7, + } +) + +agent = Agent(model=model, tools=[calculator]) +response = agent("What is 2+2") +print(response) +``` + +Portkey provides access to LLMs from providers including: + +- OpenAI (GPT-4o, GPT-4 Turbo, etc.) +- Anthropic (Claude 3.5 Sonnet, Claude 3 Opus, etc.) +- Mistral AI (Mistral Large, Mistral Medium, etc.) +- Google Vertex AI (Gemini 1.5 Pro, etc.) +- Cohere (Command, Command-R, etc.) +- AWS Bedrock (Claude, Titan, etc.) +- Local/Private Models + + + See the full list of LLM providers supported by Portkey + + +## Enterprise Governance + +**Why Enterprise Governance?** +If you are using Strands inside your organization, you need to consider several governance aspects: +- **Cost Management**: Controlling and tracking AI spending across teams +- **Access Control**: Managing which teams can use specific models +- **Usage Analytics**: Understanding how AI is being used across the organization +- **Security & Compliance**: Maintaining enterprise security standards +- **Reliability**: Ensuring consistent service across all users + +## Enterprise Governance + + + +Define budget and rate limits with a Virtual Key in the Portkey App. +Create Virtual Key screenshot +For SSO/SCIM setup, see @[product/enterprise-offering/org-management/sso.mdx] and @[product/enterprise-offering/org-management/scim/scim.mdx]. + + +Configure routing, fallbacks, and overrides. +Create Config screenshot + + +Assign scopes and metadata defaults. +Create API Key screenshot + + +Distribute keys and track usage in the Portkey dashboard. +Monitor screenshot +View audit logs: @[product/enterprise-offering/audit-logs.mdx]. + + + +## Troubleshooting + + + +

When your requests exceed quota, catch RateLimitError:

+```python +from portkey_ai import exceptions + +try: + response = agent("โ€ฆ") +except exceptions.RateLimitError as e: + print("Rate limited, retryingโ€ฆ", e) +``` +
+ +

Set timeouts and catch TimeoutError:

+```python +from portkey_ai import exceptions + +try: + response = agent("โ€ฆ", timeout=5) +except exceptions.TimeoutError: + print("Request timed out") +``` +
+ +

Verify your API key and header settings:

+```python +from portkey_ai import exceptions + +try: + response = agent("โ€ฆ") +except exceptions.AuthenticationError: + print("Invalid API key or permissions") +``` +
+ +

Use a simple exponential backoff:

+```python +import time +from portkey_ai import exceptions + +for attempt in range(3): + try: + result = agent("โ€ฆ") + break + except exceptions.RateLimitError: + time.sleep(2 ** attempt) +``` +
+
+ +## Contact & Support + + + + Get dedicated SLA-backed support. + + + Join our forums and Slack channel. + + + +## Frequently Asked Questions + + + + Portkey adds production-readiness to Strands Agents through comprehensive observability (traces, logs, metrics), reliability features (fallbacks, retries, caching), and access to 200+ LLMs through a unified interface. This makes it easier to debug, optimize, and scale your agent applications, all while preserving Strands Agents' strong type safety. + + + + Yes! Portkey integrates seamlessly with existing Strands Agents applications. You just need to replace your client initialization code with the Portkey-enabled version. The rest of your agent code remains unchanged and continues to benefit from Strands Agents' strong typing. + + + + Portkey supports all Strands Agents features, including tool use, multi-agent systems, and more. It adds observability and reliability without limiting any of the framework's functionality. + + + + Yes, Portkey allows you to use a consistent `trace_id` across multiple agents and requests to track the entire workflow. This is especially useful for multi-agent systems where you want to understand the full execution path. + + + + Portkey allows you to add custom metadata to your agent runs, which you can then use for filtering. Add fields like `agent_name`, `agent_type`, or `session_id` to easily find and analyze specific agent executions. + + + + Yes! Portkey uses your own API keys for the various LLM providers. It securely stores them as virtual keys, allowing you to easily manage and rotate keys without changing your code. + + + + +## Resources + + + + + + +

Official Portkey documentation

+
+ + +

Get personalized guidance on implementing this integration

+
+
diff --git a/virtual_key_old/integrations/agents/strands.mdx b/virtual_key_old/integrations/agents/strands.mdx new file mode 100644 index 00000000..fb41312e --- /dev/null +++ b/virtual_key_old/integrations/agents/strands.mdx @@ -0,0 +1,710 @@ +--- +title: "Strands Agents" +description: "Use Portkey with AWS's Strands Agents to take your AI Agents to production" +--- + +Strands Agents is a simple-to-use agent framework built by AWS. Portkey enhances Strands Agents with production-grade observability, reliability, and multi-provider supportโ€”all through a single integration that requires no changes to your existing agent logic. + +**What you get with this integration:** +- **Complete observability** of every agent step, tool use, and LLM interaction +- **Built-in reliability** with automatic fallbacks, retries, and load balancing +- **200+ LLMs** accessible through the same OpenAI-compatible interface +- **Production monitoring** with traces, logs, and real-time metrics +- **Zero code changes** to your existing Strands agent implementations + + + Learn more about Strands Agents' core concepts and features + + +## Quick Start + + + +```bash +pip install -U strands-agents strands-agents-tools openai portkey-ai +``` + + + +Instead of initializing your OpenAI model directly: +```python +# Before: Direct OpenAI +from strands.models.openai import OpenAIModel + +model = OpenAIModel( + client_args={"api_key": "sk-..."}, + model_id="gpt-4o", + params={"temperature": 0.7} +) +``` + +Initialize it through Portkey's gateway: +```python +# After: Through Portkey +from strands.models.openai import OpenAIModel +from portkey_ai import PORTKEY_GATEWAY_URL + +model = OpenAIModel( + client_args={ + "api_key": "YOUR_PORTKEY_API_KEY", # Your Portkey API key + "base_url": PORTKEY_GATEWAY_URL # Routes through Portkey + }, + model_id="gpt-4o", + params={"temperature": 0.7} +) +``` + + + +```python +from strands import Agent +from strands_tools import calculator + +agent = Agent(model=model, tools=[calculator]) +response = agent("What is 2+2?") +print(response) +``` + +Your agent works exactly the same way, but now all interactions are automatically logged, traced, and monitored in your Portkey dashboard. + + + +## How the Integration Works + +The integration leverages Strands' flexible `client_args` parameter, which passes any arguments directly to the OpenAI client constructor. By setting `base_url` to Portkey's gateway, all requests route through Portkey while maintaining full compatibility with the OpenAI API. + +```python +# This is what happens under the hood in Strands: +client_args = client_args or {} +self.client = openai.OpenAI(**client_args) # Your Portkey config gets passed here +``` + +This means you get all of Portkey's features without any changes to your agent logic, tool usage, or response handling. + +## Setting Up Portkey + +Before using the integration, you need to configure your AI providers and create a Portkey API key. + + + +Go to [Virtual Keys](https://app.portkey.ai/virtual-keys) in the Portkey dashboard and add your actual AI provider keys (OpenAI, Anthropic, etc.). Each provider key gets a virtual key ID that you'll reference in configs. + + + +Go to [Configs](https://app.portkey.ai/configs) to define how requests should be routed. A basic config looks like: + +```json +{ + "virtual_key": "openai-key-abc123" +} +``` + +For production setups, you can add fallbacks, load balancing, and conditional routing here. + + + +Go to [API Keys](https://app.portkey.ai/api-keys) to create a new API key. Attach your config as the default routing config, and you'll get an API key that routes to your configured providers. + + + +## Complete Integration Example + +Here's a full example showing how to set up a Strands agent with Portkey integration: + +```python [expandable] +from strands import Agent +from strands.models.openai import OpenAIModel +from strands_tools import calculator, web_search +from portkey_ai import PORTKEY_GATEWAY_URL + +# Initialize model through Portkey +model = OpenAIModel( + client_args={ + "api_key": "YOUR_PORTKEY_API_KEY", + "base_url": PORTKEY_GATEWAY_URL + }, + model_id="gpt-4o", + params={ + "max_tokens": 1000, + "temperature": 0.7, + } +) + +# Create agent with tools (unchanged from standard Strands usage) +agent = Agent( + model=model, + tools=[calculator, web_search] +) + +# Use the agent (unchanged from standard Strands usage) +response = agent("Calculate the compound interest on $10,000 at 5% for 10 years, then search for current inflation rates") +print(response) +``` + +The agent will automatically use both tools as needed, and every step will be logged in your Portkey dashboard with full request/response details, timing, and token usage. + +## Production Features + +### 1. Enhanced Observability + +Portkey provides comprehensive visibility into your agent's behavior without requiring any code changes. + + + +Track the complete execution flow of your agents with hierarchical traces that show: + +- **LLM calls**: Every request to language models with full payloads +- **Tool invocations**: Which tools were called, with what parameters, and their responses +- **Decision points**: How the agent chose between different tools or approaches +- **Performance metrics**: Latency, token usage, and cost for each step + + + + + +```python {11} +from strands import Agent +from strands.models.openai import OpenAIModel +from strands_tools import calculator +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +model = OpenAIModel( + client_args={ + "api_key": "YOUR_PORTKEY_API_KEY", + "base_url": PORTKEY_GATEWAY_URL, + # Add trace ID to group related requests + "default_headers": createHeaders(trace_id="user_session_123") + }, + model_id="gpt-4o", + params={"temperature": 0.7} +) + +agent = Agent(model=model, tools=[calculator]) +response = agent("What's 15% of 2,847?") +``` + +All requests from this agent will be grouped under the same trace, making it easy to analyze the complete interaction flow. + + + +Add business context to your agent runs for better filtering and analysis: + +```python {8-13} +from portkey_ai import createHeaders + +model = OpenAIModel( + client_args={ + "api_key": "YOUR_PORTKEY_API_KEY", + "base_url": PORTKEY_GATEWAY_URL, + "default_headers": createHeaders( + trace_id="customer_support_bot", + metadata={ + "agent_type": "customer_support", + "user_tier": "premium", + "session_id": "sess_789", + "department": "billing" + } + ) + }, + model_id="gpt-4o", + params={"temperature": 0.3} # Lower temperature for support tasks +) +``` + +This metadata appears in your Portkey dashboard, allowing you to filter logs and analyze performance by user type, session, or any custom dimension. + + + +Monitor your agents in production with built-in dashboards that track: + +- **Success rates**: Percentage of successful agent completions +- **Average latency**: Response times across different agent types +- **Token usage**: Track consumption and costs across models +- **Error patterns**: Common failure modes and their frequency + +All metrics can be segmented by the metadata you provide, giving you insights like "premium user agents have 15% higher success rates" or "billing department queries take 2x longer on average." + + + +### 2. Reliability & Fallbacks + +When running agents in production, things can go wrong - API rate limits, network issues, or provider outages. Portkey's reliability features ensure your agents keep running smoothly even when problems occur. + +It's simple to enable fallback in your Strands Agents by using a Portkey Config that you can attach at runtime or directly to your Portkey API key. Here's an example of attaching a Config at runtime: + + + +Configure multiple providers so your agents keep working even when one provider fails: + +```python {7-18} +from portkey_ai import createHeaders + +model = OpenAIModel( + client_args={ + "api_key": "YOUR_PORTKEY_API_KEY", + "base_url": PORTKEY_GATEWAY_URL, + "default_headers": createHeaders( + config={ + "strategy": { + "mode": "fallback", + "on_status_codes": [429, 503, 502] # Rate limits and server errors + }, + "targets": [ + { "virtual_key": "openai-key-primary" }, # Try OpenAI first + { "virtual_key": "anthropic-key-backup" } # Fall back to Claude + ] + } + ) + }, + model_id="gpt-4o", # Will map to equivalent models on each provider + params={"temperature": 0.7} +) +``` + +If OpenAI returns a rate limit error (429), Portkey automatically retries the request with Anthropic's Claude, using default model mappings. + + + + +Distribute requests across multiple API keys to stay within rate limits: + +```python {7-15} +from portkey_ai import createHeaders + +model = OpenAIModel( + client_args={ + "api_key": "YOUR_PORTKEY_API_KEY", + "base_url": PORTKEY_GATEWAY_URL, + "default_headers": createHeaders( + config={ + "strategy": {"mode": "loadbalance"}, + "targets": [ + { "virtual_key": "openai-key-1", "weight": 70 }, + { "virtual_key": "openai-key-2", "weight": 30 } + ] + } + ) + }, + model_id="gpt-4o", + params={"temperature": 0.7} +) +``` + +Requests will be distributed 70/30 across your two OpenAI keys, helping you maximize throughput without hitting individual key limits. + + + + +Route requests to different providers/models based on custom logic (like metadata, input content, or user attributes) using Portkey's Conditional Routing feature. + +See the [Conditional Routing documentation](/product/ai-gateway/conditional-routing) for full guidance and advanced examples. + + + + +### 3. LLM Interoperability + +Access 1,600+ models through the same Strands interface by changing just the provider configuration: + + + +```python +from portkey_ai import createHeaders + +# Use Claude instead of GPT-4 +model = OpenAIModel( + client_args={ + "api_key": "YOUR_PORTKEY_API_KEY", + "base_url": PORTKEY_GATEWAY_URL, + "default_headers": createHeaders( + provider="anthropic", + api_key="YOUR_ANTHROPIC_KEY" # Can also use virtual keys + ) + }, + model_id="claude-3-7-sonnet-latest", # Claude model ID + params={"max_tokens": 1000, "temperature": 0.7} +) + +# Agent code remains exactly the same +agent = Agent(model=model, tools=[calculator]) +response = agent("Explain quantum computing in simple terms") +``` + + + +```python +# Create different model instances for different tasks +reasoning_model = OpenAIModel( + client_args={ + "api_key": "YOUR_PORTKEY_API_KEY", + "base_url": PORTKEY_GATEWAY_URL, + "default_headers": createHeaders(virtual_key="openai-key") + }, + model_id="gpt-4o", + params={"temperature": 0.1} # Low temperature for reasoning +) + +creative_model = OpenAIModel( + client_args={ + "api_key": "YOUR_PORTKEY_API_KEY", + "base_url": PORTKEY_GATEWAY_URL, + "default_headers": createHeaders(virtual_key="gemini-key") + }, + model_id="gemini-2.0-flash-exp", + params={"temperature": 0.8} # Higher temperature for creativity +) + +# Use different models for different agent types +reasoning_agent = Agent(model=reasoning_model, tools=[calculator]) +creative_agent = Agent(model=creative_model, tools=[]) +``` + + + +Portkey provides access to LLMs from providers including: + +- OpenAI (GPT-4o, GPT-4 Turbo, etc.) +- Anthropic (Claude 3.5 Sonnet, Claude 3 Opus, etc.) +- Mistral AI (Mistral Large, Mistral Medium, etc.) +- Google Vertex AI (Gemini 1.5 Pro, etc.) +- Cohere (Command, Command-R, etc.) +- AWS Bedrock (Claude, Titan, etc.) +- Local/Private Models + + + See the full list of LLM providers supported by Portkey + + + +### 4. Guardrails for Safe Agents + +Guardrails ensure your Strands agents operate safely and respond appropriately in all situations. + +**Why Use Guardrails?** + +Strands agents can experience various failure modes: +- Generating harmful or inappropriate content +- Leaking sensitive information like PII +- Hallucinating incorrect information +- Generating outputs in incorrect formats + +Portkey's guardrails can: +- Detect and redact PII in both inputs and outputs +- Filter harmful or inappropriate content +- Validate response formats against schemas +- Check for hallucinations against ground truth +- Apply custom business logic and rules + + + Explore Portkey's guardrail features to enhance agent safety + + +## Advanced Configuration + + + +Configure different behavior for development, staging, and production: + +```python [expandable] +import os +from portkey_ai import createHeaders + +def create_model(environment="production"): + if environment == "development": + # Use faster, cheaper models for development + headers = createHeaders( + config={"targets": [{"virtual_key": "openai-dev-key"}]}, + metadata={"environment": "dev"} + ) + model_id = "gpt-4o-mini" + params = {"temperature": 0.5, "max_tokens": 500} + + elif environment == "production": + # Use high-performance models with fallbacks for production + headers = createHeaders( + config={ + "strategy": {"mode": "fallback"}, + "targets": [ + {"virtual_key": "openai-prod-key"}, + {"virtual_key": "anthropic-prod-key"} + ] + }, + metadata={"environment": "prod"} + ) + model_id = "gpt-4o" + params = {"temperature": 0.7, "max_tokens": 2000} + + return OpenAIModel( + client_args={ + "api_key": "YOUR_PORTKEY_API_KEY", + "base_url": PORTKEY_GATEWAY_URL, + "default_headers": headers + }, + model_id=model_id, + params=params + ) + +# Use environment-specific configuration +model = create_model(os.getenv("APP_ENV", "production")) +agent = Agent(model=model, tools=[calculator]) +``` + + + +Override configuration for specific requests without changing the model: + +```python [expandable] +from portkey_ai import createHeaders + +model = OpenAIModel( + client_args={ + "api_key": "YOUR_PORTKEY_API_KEY", + "base_url": PORTKEY_GATEWAY_URL + }, + model_id="gpt-4o", + params={"temperature": 0.7} +) + +# For high-priority requests, override to use faster/more reliable providers +agent = Agent(model=model, tools=[calculator]) + +# Normal request +response1 = agent("What's 2+2?") + +# High-priority request with overrides (if your agent supports custom headers) +# This would require custom implementation in your agent wrapper +response2 = agent( + "Critical calculation: What's the compound interest?", + headers=createHeaders( + config={"targets": [{"virtual_key": "premium-openai-key"}]}, + metadata={"priority": "high"} + ) +) +``` + + + +--- + +## Enterprise Governance + +If you are using Strands inside your organization, you need to consider several governance aspects: +- **Cost Management**: Controlling and tracking AI spending across teams +- **Access Control**: Managing which teams can use specific models +- **Usage Analytics**: Understanding how AI is being used across the organization +- **Security & Compliance**: Maintaining enterprise security standards +- **Reliability**: Ensuring consistent service across all users + + +### Centralized Key Management +Instead of distributing raw API keys to developers, use Portkey API keys that you can control centrally: + +```python +# Developers use Portkey API keys (not raw provider keys) +model = OpenAIModel( + client_args={ + "api_key": "pk-team-frontend-xyz123", # Team-specific Portkey key + "base_url": PORTKEY_GATEWAY_URL + }, + model_id="gpt-4o", + params={"temperature": 0.7} +) +``` + +You can: +- **Rotate provider keys** without updating any code +- **Set spending limits** per team or API key +- **Control model access** (which teams can use which models) +- **Monitor usage** across all teams and projects +- **Revoke access** instantly if needed + +### Usage Analytics & Budgets +Track and control AI spending across your organization: + +- **Per-team budgets**: Set monthly spending limits for different teams +- **Model usage analytics**: See which teams are using which models most +- **Cost attribution**: Understand costs by project, team, or user +- **Usage alerts**: Get notified when teams approach their limits + +All of this works automatically with your existing Strands agentsโ€”no code changes required. + +--- + +## Contact & Support + + + + Get dedicated SLA-backed support. + + + Join our forums and Slack channel. + + + +--- + +## Resources + + + + + + +

Get personalized guidance on implementing this integration

+
+
+ +### Troubleshooting + + + +**Problem**: `ModuleNotFoundError` when importing Portkey components + +**Solution**: Ensure all dependencies are installed: +```bash +pip install -U strands-agents strands-agents-tools openai portkey-ai +``` + +Verify versions are compatible: +```python +import strands +import portkey_ai +print(f"Strands: {strands.__version__}") +print(f"Portkey: {portkey_ai.__version__}") +``` + + + +**Problem**: `AuthenticationError` when making requests + +**Solution**: Verify your Portkey API key and provider setup: Verify your Portkey API key and provider setup. Test your Portkey API key directly and check for common issues such as wrong API key format, misconfigured provider virtual keys, and missing config attachments. +```python +# Test your Portkey API key directly +from portkey_ai import Portkey + +portkey = Portkey(api_key="YOUR_PORTKEY_API_KEY") +response = portkey.chat.completions.create( + messages=[{"role": "user", "content": "test"}], + model="gpt-4o" +) +print(response) +``` + + + + +**Problem**: Hitting rate limits despite having fallbacks configured + +**Solution**: Check your fallback configuration: +```python +# Ensure fallbacks are configured for rate limit status codes +headers = createHeaders( + config={ + "strategy": { + "mode": "fallback", + "on_status_codes": [429, 503, 502, 504] + }, + "targets": [ + {"virtual_key": "primary-key"}, + {"virtual_key": "backup-key"} + ] + } +) +``` + +Also verify that your backup providers have sufficient quota. + + + + +**Problem**: Model not found or unsupported model errors + +**Solution**: Check that your model ID is correct for the provider: +```python +# OpenAI models +model_id = "gpt-4o" # Correct +model_id = "gpt-4-turbo" # Correct + +# Anthropic models +model_id = "claude-3-5-sonnet-20241022" # Correct +model_id = "claude-3-sonnet" # Wrong format + +# Use provider-specific model IDs, not generic names +``` + + + + +**Problem**: Not seeing traces or logs in Portkey dashboard + +**Solution**: Verify your requests are going through Portkey: +```python +# Check that base_url is set correctly +print(model.client.base_url) # Should be https://api.portkey.ai/v1 + +# Add trace IDs for easier debugging +headers = createHeaders( + trace_id="debug-session-123", + metadata={"debug": "true"} +) +``` + +Also check the Logs section in your Portkey dashboard and filter by your metadata. + + + +### Frequently Asked Questions + + + + Portkey adds production-readiness to Strands Agents through comprehensive observability (traces, logs, metrics), reliability features (fallbacks, retries, caching), and access to 200+ LLMs through a unified interface. This makes it easier to debug, optimize, and scale your agent applications, all while preserving Strands Agents' strong type safety. + + + + Yes! Portkey integrates seamlessly with existing Strands Agents applications. You just need to replace your client initialization code with the Portkey-enabled version. The rest of your agent code remains unchanged and continues to benefit from Strands Agents' strong typing. + + + + Portkey supports all Strands Agents features, including tool use, multi-agent systems, and more. It adds observability and reliability without limiting any of the framework's functionality. + + + + Yes, Portkey allows you to use a consistent `trace_id` across multiple agents and requests to track the entire workflow. This is especially useful for multi-agent systems where you want to understand the full execution path. + + + + Portkey allows you to add custom metadata to your agent runs, which you can then use for filtering. Add fields like `agent_name`, `agent_type`, or `session_id` to easily find and analyze specific agent executions. + + + + Yes! Portkey uses your own API keys for the various LLM providers. It securely stores them as virtual keys, allowing you to easily manage and rotate keys without changing your code. + + + + + +--- + +## Next Steps + +Now that you have Portkey integrated with your Strands agents: + +1. **Monitor your agents** in the [Portkey dashboard](https://app.portkey.ai/logs) to understand their behavior +2. **Set up fallbacks** for critical production agents using multiple providers +3. **Add custom metadata** to track different agent types or user segments +4. **Configure budgets and alerts** if you're deploying multiple agents +5. **Explore advanced routing** to optimize for cost, latency, or quality + + + + View your agent logs, traces, and analytics + + + + See all 200+ models you can use with this integration + + + + Explore governance, security, and compliance features + + \ No newline at end of file diff --git a/virtual_key_old/integrations/ai-apps.mdx b/virtual_key_old/integrations/ai-apps.mdx new file mode 100644 index 00000000..65027a52 --- /dev/null +++ b/virtual_key_old/integrations/ai-apps.mdx @@ -0,0 +1,38 @@ +--- +title: "Overview" +--- + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/virtual_key_old/integrations/cloud/azure.mdx b/virtual_key_old/integrations/cloud/azure.mdx new file mode 100644 index 00000000..b5ab704f --- /dev/null +++ b/virtual_key_old/integrations/cloud/azure.mdx @@ -0,0 +1,201 @@ +--- +title: "Microsoft Azure" +description: "Discover how you can build your Gen AI platform on Azure using Portkey" +--- + +Portkey supercharges your Azure AI infrastructure with an enterprise-ready production stack. Teams building on Azure achieve **75% faster time-to-market**, **significant cost optimization**, and **4ร— faster deployments** while maintaining seamless integration with existing Azure investments. + +Our solution empowers you to build robust, scalable AI applications that leverage the full security and compliance capabilities of Azure. + +**Key benefits for Azure AI teams:** +- Accelerated development cycles with unified API access +- Granular cost tracking and optimization for Azure AI services +- Enhanced governance with centralized security controls +- Simplified deployment and scaling of production AI applications +- Full compatibility with Azure's native security model + +Schedule a 30-min strategy call + +## What You Can Do Today with Portkey on Azure + + + +Spin up a managed Portkey instance directly inside your Azure subscription for maximum security and control. + + +Integrate seamlessly with your existing identity provider for enterprise-grade access control and automated user provisioning. + + +Route all your Azure OpenAI requests (chat, vision, function-calling, DALL-E images) through Portkey while retaining full Azure compliance and gaining enhanced observability. + + +Bring any model deployed via Azure AI Studio (formerly AI Foundry) under the Portkey gateway for consistent caching, retries, fallbacks, and observability. + + +Apply robust, configurable content moderation and PII detection to every AI requestโ€”no code changes required in your applications. + + +Developers can continue using familiar Microsoft tooling. Portkey transparently adds value by handling routing, cost attribution, observability, and guardrails. + + +Expose Portkey configurations as APIM endpoints, ensuring consistent governance and policies across all your APIs, not just AI. + + + +## Get Started in Minutes + +1. **Deploy from the Azure Marketplace**: Launch Portkey directly within your Azure subscription. +2. **Connect Microsoft Entra ID for SSO & SCIM**: Follow our [SSO guide](/product/enterprise-offering/org-management/sso) and [Azure SCIM setup](/product/enterprise-offering/org-management/scim/azure-ad). +3. **Create Virtual Keys in Portkey**: Link your Azure OpenAI resources or Azure AI Foundry model deployments. +4. **Enable Azure Content Safety Guardrails**: Configure content filters within your Portkey Configs. +5. **Instrument Your Applications**: Use your preferred SDK (like the C# example below) to route requests through Portkey. + +```csharp C# [expandable] +using OpenAI; +using OpenAI.Chat; +using System; +using System.ClientModel; +using System.ClientModel.Primitives; +using System.Collections.Generic; +using System.Threading.Tasks; + +public static class PortkeyAzureClient +{ + private class PortkeyHeadersPolicy : PipelinePolicy + { + private readonly Dictionary _headers; + public PortkeyHeadersPolicy(Dictionary headers) => _headers = headers; + + public override void Process(PipelineMessage message, IReadOnlyList pipeline, int index) + { + foreach (var header in _headers) message.Request.Headers.Set(header.Key, header.Value); + if (index < pipeline.Count) pipeline[index].Process(message, pipeline, index + 1); + } + + public override ValueTask ProcessAsync(PipelineMessage message, IReadOnlyList pipeline, int index) + { + Process(message, pipeline, index); + return ValueTask.CompletedTask; + } + } + + public static OpenAIClient CreateOpenAIClientWithPortkey( + Uri azureEndpoint, + string azureApiKey, + Dictionary portkeyHeaders + ) + { + var options = new OpenAIClientOptions + { + Endpoint = azureEndpoint, // Your Azure OpenAI endpoint + }; + options.AddPolicy(new PortkeyHeadersPolicy(portkeyHeaders), PipelinePosition.PerCall); + + return new OpenAIClient(new ApiKeyCredential(azureApiKey), options); + } +} + +public class ExampleAzureIntegration +{ + public static async Task Main(string[] args) + { + // 1. Define Azure OpenAI credentials + var azureEndpoint = new Uri("YOUR_AZURE_OPENAI_ENDPOINT"); // Eg: https://.openai.azure.com/ + var azureApiKey = "YOUR_AZURE_OPENAI_KEY"; + + // 2. Define Portkey headers + // Get your Portkey API Key from https://app.portkey.ai/settings + // Create a virtual key for your Azure OpenAI setup in Portkey: https://app.portkey.ai/virtual-keys + var portkeyHeaders = new Dictionary + { + { "x-portkey-api-key", "YOUR_PORTKEY_API_KEY" }, + { "x-portkey-virtual-key", "YOUR_AZURE_OPENAI_VIRTUAL_KEY" } // Connects to your Azure setup + // Optional: { "x-portkey-trace-id", "my-azure-app-trace" }, + // Optional: { "x-portkey-metadata", "{\"userId\": \"user-123\"}" } + }; + + // 3. Create Azure OpenAI client with Portkey integration + var openAIClient = PortkeyAzureClient.CreateOpenAIClientWithPortkey( + azureEndpoint, + azureApiKey, + portkeyHeaders + ); + + // 4. Get the ChatClient + // The model/deployment name for Azure OpenAI is specified here. + var chatClient = openAIClient.GetChatClient("YOUR_AZURE_DEPLOYMENT_NAME"); // Eg: gpt-4, gpt-35-turbo + + // 5. Make a request + try + { + Console.WriteLine("Sending request to Azure OpenAI via Portkey..."); + ChatCompletion completion = await chatClient.CompleteChatAsync( + new List + { + new SystemChatMessage("You are an AI assistant that helps people find information."), + new UserChatMessage("Give me 3 Azure best practices for cloud security.") + }); + + Console.WriteLine($"[ASSISTANT]: {completion.Content[0].Text}"); + } + catch (ClientResultException ex) + { + Console.WriteLine($"API Call Error: {ex.Status}: {ex.Message}"); + // For more details from Portkey, you can inspect ex.Content and ex.Headers + // if (ex.Headers.TryGetValue("x-portkey-error-details", out var errorDetails)) + // { + // Console.WriteLine($"Portkey Error Details: {string.Join(", ", errorDetails)}"); + // } + } + } +} +``` + +## Why Portkey + Azure? The Strategic Advantages + +Enterprises choose to combine Portkey with Microsoft Azure to gain a competitive edge in their AI development. This powerful synergy offers: + + +```mermaid +flowchart LR + A[Your Azure AI Services\nOpenAI, Foundry, etc.] --> B[Portkey AI Gateway] + B --> C[Unified Observability\n& Cost Control] + B --> D[Centralized Guardrails\n& Security] + B --> E[Enhanced Reliability\n& Performance] + B --> F[Accelerated Development\nCycles] + C --> G[Actionable Insights &\nROI on Azure Spend] + D --> G + E --> G + F --> G +``` + + +1. **Complete Cost Visibility & Control on Azure Spend**: Go beyond standard Azure billing. Portkey allows you to tag every AI request (by application, environment, user, or custom dimension) and export granular metrics to Azure Monitor or your data warehouse for precise cost attribution and budget management. +2. **Unified Governance & Security Across Your Azure AI Landscape**: Enforce organization-wide policies consistently. From robust content moderation with Azure Content Safety to PII detection, rate limits, and SSO via Microsoft Entra ID, Portkey centralizes governance across all your Azure AI services and Portkey workspaces. +3. **Fortified Security with Azure-Native Integration**: Leverage Azure's robust security model. With Portkey's Azure Marketplace deployment or Private Cloud options, secrets remain securely in Azure Key Vault, and AI traffic can be configured to never leave Azureโ€™s backbone, ensuring compliance and data integrity. +4. **Boosted Developer Velocity & Efficiency**: Eliminate redundant setups and streamline AI development. Portkey provides a single, consistent gateway layer, removing the need for per-team Azure OpenAI subscriptions or duplicated infrastructure, allowing your teams to build faster. +5. **Seamless Integration with the Azure Ecosystem**: Portkey is built for Azure. Enjoy native support for Azure OpenAI (chat, vision, function-calling, images), Azure AI Foundry Models, and even familiar Microsoft tooling like the OpenAI C# SDK and Semantic Kernel, all while Portkey handles the complexities of routing, cost attribution, and guardrails. + +## Book an Enterprise Demo + + + +--- + +### Additional Resources + +- [Azure OpenAI integration](/integrations/llms/azure-openai) +- [Azure AI Foundry integration](/integrations/llms/azure-foundry) +- [Azure Content Safety guardrails](/product/guardrails/azure-guardrails) +- [Azure Private Cloud deployment guide](/product/enterprise-offering/private-cloud-deployments/azure) + + +Need something bespoke? Reach out to our team for a tailored architecture review. + diff --git a/virtual_key_old/integrations/ecosystem.mdx b/virtual_key_old/integrations/ecosystem.mdx new file mode 100644 index 00000000..fec42ccb --- /dev/null +++ b/virtual_key_old/integrations/ecosystem.mdx @@ -0,0 +1,164 @@ +--- +title: "Integrations" +--- + + + + + + + + + + + + +# Preferred Partners + + + +OpenAI Agents SDK enables building powerful AI agents with tools, memory, and multi-step reasoning capabilities. + + + + Cline is an AI coding assistant that integrates directly into your VS Code environment, providing autonomous coding capabilities + + + + Jan is an open source alternative to ChatGPT that runs 100% offline on your computer + + + All in one AI with built in RAG, Agents and Chat Interface. + + + Fast, AI-assisted code editor for power users. + + + Composable security platform against LLM threats like prompt injection and sensitive data leakage. + + + Low-code platform for building internal tools + + + + Network security and application delivery solutions + + + + Qdrant is an Open-Source Vector Database and Vector Search Engine written in Rust. + + + + Roo is an AI coding assistant that integrates directly into your VS Code environment, providing autonomous coding capabilities. + + + + Goose is a local, extensible, open source AI agent that automates engineering tasks. + + + + Qdrant is an Open-Source Vector Database and Vector Search Engine written in Rust. + + + + Flexible and scalable document database + + + + Open-source framework focused on simplifying internal tool development. + + + + Cloud computing services and marketplace + + + + Cloud platform and services marketplace + + + + AI-powered solutions tailored for business efficiency. + + + + Automated evaluation and security platform for AI models, focusing on performance scoring and reliability. + + + + ML observability and monitoring + + + + All-in-one platform that empowers organizations to monitor, assess risks, and secure their AI activities. + + + + Frontend deployment and hosting platform + + + + Framework for developing LLM applications + + + + Data framework for LLM applications + + + + Prompt engineering and testing tool + + + + Multi-agent framework for AI applications + + + + Framework for automated AI agent creation + + + + Programming framework for AI-powered applications + + + + Open-source chat platform + + + + Tools for structured outputs from LLMs + + + + Distributed computing platform for AI + + + + AI-powered solutions for sustainability + + + + AI-powered development assistant + + + Your LLM with the built-in power to answer data questions for agents & apps. + + + Improve the experience of deploying, managing, and scaling Postgres + + + + +# Become Portkey Partner + +**To Join Portkey's Partner Ecosystem, Schedule a Call Below** + + + + diff --git a/virtual_key_old/integrations/guardrails/acuvity.mdx b/virtual_key_old/integrations/guardrails/acuvity.mdx new file mode 100644 index 00000000..f6709e56 --- /dev/null +++ b/virtual_key_old/integrations/guardrails/acuvity.mdx @@ -0,0 +1,259 @@ +--- +title: "Acuvity" +description: "Acuvity is model agnostic GenAI security solution. It is built to secure existing and future GenAI models, apps, services, tools, plugins and more." +--- + +[Acuvity](https://acuvity.ai/) provides AI Guard service for scanning LLM inputs and outputs to avoid manipulation of the model, addition of malicious content, and other undesirable data transfers. + +To get started with Acuvity, visit their website: + + +## Using Acuvity with Portkey + +### 1. Add Acuvity Credentials to Portkey + +* Navigate to the `Integration` page under `Sidebar` +* Click on the edit button for the Acuvity integration +* Add your Acuvity API Key + + +### 2. Add Acuvity's Guardrail Check + +* Navigate to the `Guardrails` page and click the `Create` button +* Search for Acuvity Scan and click `Add` +* Configure your guardrail settings: toxicity, jail break, biased etc. +* Set any `actions` you want on your check, and create the Guardrail! + + + Guardrail Actions allow you to orchestrate your guardrails logic. You can learn them [here](/product/guardrails#there-are-6-types-of-guardrail-actions) + + +Here's your updated table with just the parameter names: + +| Check Name | Description | Parameters | Supported Hooks | +|------------|-------------|------------|-----------------| +| Acuvity Scan | Comprehensive content safety and security checks | `Prompt Injection`, `Toxicity`, `Jail Break`, `Malicious Url`, `Biased`, `Harmful`, `Language`, `PII`, `Secrets`, `Timeout` | `beforeRequestHook`, `afterRequestHook` | + +### 3. Add Guardrail ID to a Config and Make Your Request + +* When you save a Guardrail, you'll get an associated Guardrail ID - add this ID to the `input_guardrails` or `output_guardrails` params in your Portkey Config +* Create these Configs in Portkey UI, save them, and get an associated Config ID to attach to your requests. [More here](/product/ai-gateway/configs). + +Here's an example config: + +```json +{ + "input_guardrails": ["guardrails-id-xxx", "guardrails-id-yyy"], + "output_guardrails": ["guardrails-id-xxx", "guardrails-id-yyy"] +} +``` + + + + + + +```js +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + config: "pc-***" // Supports a string config id or a config object +}); +``` + + + +```py +portkey = Portkey( + api_key="PORTKEY_API_KEY", + config="pc-***" # Supports a string config id or a config object +) +``` + + + +```js +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + apiKey: "PORTKEY_API_KEY", + config: "CONFIG_ID" + }) +}); +``` + + + +```py +client = OpenAI( + api_key="OPENAI_API_KEY", # defaults to os.environ.get("OPENAI_API_KEY") + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY", # defaults to os.environ.get("PORTKEY_API_KEY") + config="CONFIG_ID" + ) +) +``` + + + +```sh +curl https://api.portkey.ai/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-config: $CONFIG_ID" \ + -d '{ + "model": "gpt-3.5-turbo", + "messages": [{ + "role": "user", + "content": "Hello!" + }] + }' +``` + + + +For more, refer to the [Config documentation](/product/ai-gateway/configs). + +Your requests are now guarded by Acuvity AI's Guardrail and you can see the Verdict and any action you take directly on Portkey logs! + +--- + +## Using Raw Guardrails with Acuvity + +You can define Acuvity guardrails directly in your code for more programmatic control without using the Portkey UI. This "raw guardrails" approach lets you dynamically configure guardrails based on your application's needs. + + + We recommend that you create guardrails using the Portkey UI whenever possible. Raw guardrails are more complex and require you to manage credentials and configurations directly in your code. + + + + + +### Available Acuvity Guardrails + + +| Guardrail Name | ID | Description | Parameters | +| -------------- | -- | ----------- | ---------- | +| Acuvity Scan | acuvity.scan | Comprehensive content safety and security checks | `Prompt Injection`, `Toxicity`, `Jail Break`, `Malicious Url`, `Biased`, `Harmful`, `Language`, `PII`, `Secrets`, `Timeout` | + +### Implementation Examples + + + +- **`type`**: Always set to `"guardrail"` for guardrail checks +- **`id`**: A unique identifier for your guardrail +- **`credentials`**: Authentication details for Acuvity + - `api_key`: Your Acuvity API key +- **`checks`**: Array of guardrail checks to run + - `id`: The specific guardrail ID from the table above + - `parameters`: Configuration options specific to each guardrail +- **`deny`**: Whether to block the request if guardrail fails (true/false) +- **`async`**: Whether to run guardrail asynchronously (true/false) +- **`on_success`/`on_fail`**: Optional callbacks for success/failure scenarios + - `feedback`: Data for logging and analytics + - `weight`: Importance of this feedback (0-1) + - `value`: Feedback score (-10 to 10) + + + +```json +{ + "before_request_hooks": [ + { + "type": "guardrail", + "id": "acuvity-scan-guard", + "credentials": { + "api_key": "your_acuvity_api_key", + "domain": "your_acuvity_domain" + }, + "checks": [ + { + "id": "acuvity.scan", + "parameters": { + "prompt_injection": true, + "prompt_injection_threshold": 0.5, // between 0-1 + "toxic": true, + "toxic_threshold": 0.5, // between 0-1 + "jail_break": true, + "jail_break_threshold": 0.5, // between 0-1 + "malicious_url": true, + "malicious_url_threshold": 0.5, // between 0-1 + "biased": true, + "biased_threshold": 0.5, // between 0-1 + "harmful": true, + "harmful_threshold": 0.5, // between 0-1 + "language": true, + "language_values": "eng_Latn", + "pii": true, + "pii_redact": false, + "pii_categories": [ + "email_address", + "ssn", + "person", + "aba_routing_number", + "address", + "bank_account", + "bitcoin_wallet", + "credit_card", + "driver_license", + "itin_number", + "location", + "medical_license", + "money_amount", + "passport_number", + "phone_number" + ], + "secrets": true, + "secrets_redact": false, + "secrets_categories": [ + "credentials", + "aws_secret_key", + "private_key", + "alibaba", + "anthropic" + //... and more refer Acuvity docs for more + ], + "timeout": 5000 // timeout in ms + } + } + ], + "deny": true, + "async": false, + "on_success": { + "feedback": { + "weight": 1, + "value": 1, + "metadata": { + "user": "user_xyz" + } + } + }, + "on_fail": { + "feedback": { + "weight": 1, + "value": -1, + "metadata": { + "user": "user_xyz" + } + } + } + } + ] +} +``` + + +When using raw guardrails, you must provide valid credentials for the Acuvity service directly in your config. Make sure to handle these credentials securely and consider using environment variables or secrets management. + + + +## Get Support + +If you face any issues with the Acuvity integration, just ping the Portkey team on the [community forum](https://discord.gg/portkey-llms-in-prod-1143393887742861333). + +## Learn More + +- [Acuvity Website](https://acuvity.ai/) diff --git a/virtual_key_old/integrations/guardrails/aporia.mdx b/virtual_key_old/integrations/guardrails/aporia.mdx new file mode 100644 index 00000000..b811190d --- /dev/null +++ b/virtual_key_old/integrations/guardrails/aporia.mdx @@ -0,0 +1,53 @@ +--- +title: "Aporia" +--- + +[Aporia](https://www.aporia.com/) provides state-of-the-art Guardrails for any AI workload. With Aporia, you can setup powerful, multimodal AI Guardrails and just add your Project ID to Portkey to enable them for your Portkey requests. + +Aporia supports Guardrails for `Prompt Injections`, `Prompt Leakage`, `SQL Enforcement`, `Data Leakage`, `PII Leakage`, `Profanity Detection`, and many more! + +Browse Aporia's docs for more info on each of the Guardrail: + +} > + Learn more about Patronus AI and their offerings. + + +## Using Aporia with Portkey + +### 1\. Add Aporia API Key to Portkey + +* Navigate to the `Integrations` page under `Settings` +* Click on the edit button for the Aporia integration and add your API key + + + + + +### 2\. Add Aporia's Guardrail Check + +* Now, navigate to the `Guardrails` page +* Search for `Validate - Project` Guardrail Check and click on `Add` +* Input your corresponding Aporia Project ID where you are defining the policies +* Save the check, set any actions you want on the check, and create the Guardrail! + +| Check Name | Description | Parameters | Supported Hooks | +| :------------------- | :----------------------------------------------------------------------------------- | :------------------ | :------------------------------------ | +| Validate - Projects | Runs a project containing policies set in Aporia and returns a PASS or FAIL verdict | Project ID: string | beforeRequestHooks afterRequestHooks | + + + + +Your Aporia Guardrail is now ready to be added to any Portkey request you'd like! + +### 3\. Add Guardrail ID to a Config and Make Your Request + +* When you save a Guardrail, you'll get an associated Guardrail ID - add this ID to the `before_request_hooks` or `after_request_hooks` params in your Portkey Config +* Save this Config and pass it along with any Portkey request you're making! + +Your requests are now guarded by your Aporia policies and you can see the Verdict and any action you take directly on Portkey logs! More detailed logs for your requests will also be available on your Aporia dashboard. + +--- + +## Get Support + +If you face any issues with the Aporia integration, just ping the @Aporia team on the [community forum](https://discord.gg/portkey-llms-in-prod-1143393887742861333). diff --git a/virtual_key_old/integrations/guardrails/azure-guardrails.mdx b/virtual_key_old/integrations/guardrails/azure-guardrails.mdx new file mode 100644 index 00000000..8ca7ebac --- /dev/null +++ b/virtual_key_old/integrations/guardrails/azure-guardrails.mdx @@ -0,0 +1,221 @@ +--- +title: "Azure Guardrails" +description: "Integrate Microsoft Azure's powerful content moderation services & PII guardrails with Portkey" +--- + +Microsoft Azure offers robust content moderation and PII readaction services that can now be seamlessly integrated with Portkey's guardrails ecosystem. This integration supports two powerful Azure services: + + + + A comprehensive content moderation service that detects harmful content including hate speech, violence, sexual content, and self-harm references in text. + + + Advanced detection of personally identifiable information (PII) and protected health information (PHI) to safeguard sensitive data. + + + +## Setting Up Azure Guardrails + +Follow these steps to integrate Azure's content moderation services with Portkey: + +### 1. Configure Azure Authentication + +Navigate to the **Integrations** page under **Settings** to set up your Azure credentials. You can authenticate using three different methods: + +- **API Key** - Uses a simple API key for authentication +- **Entra** (formerly Azure AD) - Uses Azure Active Directory authentication +- **Managed** - Uses managed identity authentication within Azure + +Each authentication method requires specific credentials from your Azure account: + + + + - **Resource Name**: Your Azure resource name + - **API Key**: Your Azure API key + + + - **Resource Name**: Your Azure resource name + - **Client ID**: Your Azure client ID + - **Client Secret**: Your client secret + - **Tenant ID**: Your Azure tenant ID + + + - **Resource Name**: Your Azure resource name + - **Client ID**: Your Azure client ID (for managed identity) + + + +### 2. Create Azure Guardrail Checks + +Once authentication is set up, you can add Azure guardrail checks to your Portkey workflow: + +1. Navigate to the **Guardrails** page +2. Search for either **Azure Content Safety** or **Azure PII Detection** +3. Click **Add** to configure your chosen guardrail +4. Configure the specific settings for your guardrail +5. Save your configuration and create the guardrail + + + Guardrail Actions allow you to orchestrate your guardrails logic. You can learn more about them [here](/product/guardrails#there-are-6-types-of-guardrail-actions) + + +## Azure Content Safety + +Azure Content Safety analyzes text for harmful content across several categories. + +**Configuration Options** + +| Parameter | Description | Values | +|-----------|-------------|--------| +| Blocklist Names | Custom Blocklist names from your azure setup | `blocklist-1`, `blocklist-2`, `blocklist-3` | +| API Version | Azure Content Safety API version | Default: `2024-09-01` | +| Severity | Minimum severity threshold for flagging content | `2`, `4`, `6`, or `8` | +| Categories | Content categories to monitor | Hate, SelfHarm, Sexual, Violence | +| Timeout | Maximum time in milliseconds for the check | Default: `5000` | + +### Using Blocklists + +Blocklists allow you to define custom terms or patterns to be flagged. You'll need to create Content Safety blocklists in your Azure account first, then reference them in the Blocklist Names field. + + +For more information on Azure Content Safety blocklists, visit the [official documentation](https://learn.microsoft.com/en-us/azure/ai-services/content-safety/quickstart-blocklist). + + +## Azure PII Detection + +Azure PII Detection identifies and can help protect personal and health-related information in your content. + +**Configuration Options** + +| Parameter | Description | Values | +|-----------|-------------|--------| +| Domain | The type of sensitive information to detect | `none` (both PII and PHI) or `phi` (only PHI) | +| API Version | Azure PII Detection API version | Default: `2024-11-01` | +| Model Version | Version of the detection model to use | Default: `latest` | +| Redact | Option to redact detected information | `true` or `false` | +| Timeout | Maximum time in milliseconds for the check | Default: `5000` | + +## Add Guardrail ID to a Config and Make Your Request + +* When you save a Guardrail, you'll get an associated Guardrail ID - add this ID to the `input_guardrails` or `output_guardrails` params in your Portkey Config +* Create these Configs in Portkey UI, save them, and get an associated Config ID to attach to your requests. [More here](/product/ai-gateway/configs). + +Here's an example configuration: + +```json +{ + "input_guardrails": ["guardrails-id-xxx", "guardrails-id-yyy"], + "output_guardrails": ["guardrails-id-xxx", "guardrails-id-yyy"] +} +``` + + + +```js +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + config: "pc-***" // Supports a string config id or a config object +}); +``` + + +```py +portkey = Portkey( + api_key="PORTKEY_API_KEY", + config="pc-***" # Supports a string config id or a config object +) +``` + + +```js +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + apiKey: "PORTKEY_API_KEY", + config: "CONFIG_ID" + }) +}); +``` + + +```py +client = OpenAI( + api_key="OPENAI_API_KEY", # defaults to os.environ.get("OPENAI_API_KEY") + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY", # defaults to os.environ.get("PORTKEY_API_KEY") + config="CONFIG_ID" + ) +) +``` + + +```sh +curl https://api.portkey.ai/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-config: $CONFIG_ID" \ + -d '{ + "model": "gpt-3.5-turbo", + "messages": [{ + "role": "user", + "content": "Hello!" + }] + }' +``` + + + +For more, refer to the [Config documentation](/product/ai-gateway/configs). + + + + +## Monitoring and Logs + +All guardrail actions and verdicts are visible in your Portkey logs, allowing you to: + +- Track which content has been flagged +- See guardrail verdicts and actions +- Monitor the performance of your content moderation pipeline + + + + + + + +## Using Azure Guardrails - Scenarios + +After setting up your guardrails, there are different ways to use them depending on your security requirements: + +### Detect and Monitor Only + +To simply detect but not block content: +- Configure your guardrail actions without enabling "Deny" +- Monitor the guardrail results in your Portkey logs +- If any issues are detected, the response will include a `hook_results` object with details + +### Redact PII Automatically + +To automatically remove sensitive information: +- Enable the `Redact` option for Azure PII Detection +- When PII is detected, it will be automatically redacted and replaced with standardized identifiers +- The response will include a `transformed` flag set to `true` in the results + +### Block Harmful Content + +To completely block requests that violate your policies: +- Enable the `Deny` option in the guardrails action tab +- If harmful content is detected, the request will fail with an appropriate status code +- You can customize denial messages to provide guidance to users + + +--- + +## Need Support? + +If you encounter any issues with Azure Guardrails, please reach out to our support team through the [Portkey community forum](https://discord.gg/portkey-llms-in-prod-1143393887742861333). diff --git a/virtual_key_old/integrations/guardrails/bedrock-guardrails.mdx b/virtual_key_old/integrations/guardrails/bedrock-guardrails.mdx new file mode 100644 index 00000000..9e847fdf --- /dev/null +++ b/virtual_key_old/integrations/guardrails/bedrock-guardrails.mdx @@ -0,0 +1,250 @@ +--- +title: "AWS Bedrock Guardrails" +description: "Secure your AI applications with AWS Bedrock's guardrail capabilities through Portkey." +--- + +[AWS Bedrock Guardrails](https://aws.amazon.com/bedrock/) provides a comprehensive solution for securing your LLM applications, including content filtering, PII detection and redaction, and more. + +To get started with AWS Bedrock Guardrails, visit their documentation: + + + +## Using AWS Bedrock Guardrails with Portkey + + +### 1. Create a guardrail on AWS Bedrock + +* Navigate to `AWS Bedrock` -> `Guardrails` -> `Create guardrail` +* Configure the guardrail according to your requirements +* For `PII redaction`, we recommend setting the Guardrail behavior as **BLOCK** for the required entity types. This is necessary because Bedrock does not apply PII checks on input (request message) if the behavior is set to MASK +* Once the guardrail is created, note the **ID** and **version** displayed on the console - you'll need these to enable the guardrail in Portkey + + +### 2. Enable Bedrock Plugin on Portkey +* Navigate to the `Integration` page under `Sidebar` +* Click on the edit button for the Bedrock integration +* Add your Bedrock `Region`, `AwsAuthType`, `Role ARN` & `External ID`credentials (refer to [Bedrock's documentation](https://pangea.cloud/docs/ai-guard) for how to obtain these credentials) + + +### 3. Create a Guardrail on Portkey + +* Navigate to the `Guardrails` page and click the `Create` button +* Search for `Apply bedrock guardrail` and click `Add` +* Enter the Guardrials ID and version of the guardrail you created in step 1 +* Enable or disable the `Redact PII` toggle as needed +* Set any actions you want on your guardrail check, and click `Create` + + + Guardrail Actions allow you to orchestrate your guardrails logic. You can learn them [here](/product/guardrails#there-are-6-types-of-guardrail-actions) + + + +### 4. Add Guardrail ID to a Config and Make Your Request + +* When you save a Guardrail, you'll get an associated Guardrail ID - add this ID to the `before_request_hooks` or `after_request_hooks` params in your Portkey Config +* Create these Configs in Portkey UI, save them, and get an associated Config ID to attach to your requests. [More here](/product/ai-gateway/configs). + + +Here's an example configuration: + +```json +{ + "input_guardrails": ["guardrails-id-xxx", "guardrails-id-yyy"], + "output_guardrails": ["guardrails-id-xxx", "guardrails-id-yyy"] +} +``` + + + + + +```js +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + config: "pc-***" // Supports a string config id or a config object +}); +``` + + + +```py +portkey = Portkey( + api_key="PORTKEY_API_KEY", + config="pc-***" # Supports a string config id or a config object +) +``` + + + +```js +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + apiKey: "PORTKEY_API_KEY", + config: "CONFIG_ID" + }) +}); +``` + + + +```py +client = OpenAI( + api_key="OPENAI_API_KEY", # defaults to os.environ.get("OPENAI_API_KEY") + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY", # defaults to os.environ.get("PORTKEY_API_KEY") + config="CONFIG_ID" + ) +) +``` + + + +```sh +curl https://api.portkey.ai/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-config: $CONFIG_ID" \ + -d '{ + "model": "gpt-3.5-turbo", + "messages": [{ + "role": "user", + "content": "Hello!" + }] + }' +``` + + + +For more, refer to the [Config documentation](/product/ai-gateway/configs). + +## Using AWS Bedrock Guardrails - Scenarios + +After setting up your guardrails, there are different ways to use them depending on your security requirements: + +### Only Detect PII, Harmful Content, etc. + +To simply detect but not redact content: +- Keep the `Redact PII` flag disabled when creating the guardrail on Portkey +- If any filters are triggered, the response status code will be 246 (instead of 200) +- The response will include a `hook_results` object with details for all checks + +### Redact PII and Detect Other Filters + +To automatically redact PII while still checking for other issues: +- Enable the `Redact PII` flag when creating the guardrail on Portkey +- If PII is detected, it will be automatically redacted and the status code will be 200 +- If other issues (like harmful content) are detected, the response code will be 246 +- The response will include a `hook_results` object with all check details +- If PII was redacted, the results will have a flag named `transformed` set to `true` + +### Deny Requests with Policy Violations + +To completely block requests that violate your policies: +- Enable the `Deny` option in the guardrails action tab +- If any filters are detected, the request will fail with response status code 446 +- However, if only PII is detected and redaction is enabled, the request will still be processed (since the issue was automatically resolved) + + + + +## Using Raw Guardrails with AWS Bedrock + +You can define AWS Bedrock guardrails directly in your code for more programmatic control without using the Portkey UI. This "raw guardrails" approach lets you dynamically configure guardrails based on your application's needs. + + + We recommend creating guardrails using the Portkey UI whenever possible. Raw guardrails are more complex and require you to manage credentials and configurations directly in your code. + + + + +### Available AWS Bedrock Guardrails + +| Guardrail Name | ID | Description | Parameters | +| -------------- | -- | ----------- | ---------- | +| Apply bedrock guardrail | `bedrock.guard` | Applies AWS Bedrock guardrail checks for LLM requests/responses | `guardrailId` (string), `guardrailVersion` (string), `redact` (boolean), `timeout` (number) | + +### Key Configuration Properties + +- **`type`**: Always set to `"guardrail"` for guardrail checks +- **`id`**: A unique identifier for your guardrail +- **`credentials`**: Authentication details for AWS Bedrock (if using assumedRole) +- **`checks`**: Array of guardrail checks to run + - `id`: The specific guardrail ID - in this case, `bedrock.guard` + - `parameters`: Configuration options for the guardrail +- **`deny`**: Whether to block the request if guardrail fails (true/false) +- **`async`**: Whether to run guardrail asynchronously (true/false) +- **`on_success`/`on_fail`**: Optional callbacks for success/failure scenarios + - `feedback`: Data for logging and analytics + - `weight`: Importance of this feedback (0-1) + - `value`: Feedback score (-10 to 10) + +### Implementation Example + +```json +{ + "before_request_hooks": [ + { + "type": "guardrail", + "id": "bedrock-guardrail", + "credentials": { + // You can choose EITHER set of credentials for bedrock + "awsAccessKeyId": "string", + "awsSecretAccessKey": "string", + "awsSessionToken": "string", //(optional) + "awsRegion": "string", + // OR + "awsAuthType": "assumedRole", + "awsRoleArn": "string", + "awsExternalId": "string", + "awsRegion": "string", + }, + "checks": [ + { + "id": "bedrock.guard", + "parameters": { + "guardrailId": "YOUR_GUARDRAIL_ID", + "guardrailVersion": "GUARDRAIL_VERSION", + "redact": true, // or false + "timeout": 5000 // timeout in ms + } + } + ], + "deny": true, + "async": false, + "on_success": { + "feedback": { + "weight": 1, + "value": 1, + "metadata": { + "user": "user_xyz" + } + } + }, + "on_fail": { + "feedback": { + "weight": 1, + "value": -1, + "metadata": { + "user": "user_xyz" + } + } + } + } + ] +} +``` + + +When using raw guardrails, you must provide valid credentials for AWS Bedrock directly in your config. Make sure to handle these credentials securely and consider using environment variables or secrets management. + + + + +## Get Support + +If you face any issues with the AWS Bedrock Guardrails integration, just ping us on the [community forum](https://discord.gg/portkey-llms-in-prod-1143393887742861333). diff --git a/virtual_key_old/integrations/guardrails/bring-your-own-guardrails.mdx b/virtual_key_old/integrations/guardrails/bring-your-own-guardrails.mdx new file mode 100644 index 00000000..e69bab6d --- /dev/null +++ b/virtual_key_old/integrations/guardrails/bring-your-own-guardrails.mdx @@ -0,0 +1,421 @@ +--- +title: "Bring Your Own Guardrails" +description: "Integrate your custom guardrails with Portkey using webhooks" +--- + +Portkey's webhook guardrails allow you to integrate your existing guardrail infrastructure with our AI Gateway. This is perfect for teams that have already built custom guardrail pipelines (like PII redaction, sensitive content filtering, or data validation) and want to: + +- Enforce guardrails directly within the AI request flow +- Make existing guardrail systems production-ready +- Modify AI requests and responses in real-time + +## How It Works + +1. You add a Webhook as a Guardrail Check in Portkey +2. When a request passes through Portkey's Gateway: + - Portkey sends relevant data to your webhook endpoint + - Your webhook evaluates the request/response and returns a verdict + - Based on your webhook's response, Portkey either allows the request to proceed, modifies it if required, or applies your configured guardrail actions + +## Setting Up a Webhook Guardrail + +### Configure Your Webhook in Portkey App + + + + + +In the Guardrail configuration UI, you'll need to provide: + +| Field | Description | Type | +| :-- | :-- | :-- | +| **Webhook URL** | Your webhook's endpoint URL | `string` | +| **Headers** | Headers to include with webhook requests | `JSON` | +| **Timeout** | Maximum wait time for webhook response | `number` (ms) | + +#### Webhook URL + +This should be a publicly accessible URL where your webhook is hosted. + + +**Enterprise Feature**: Portkey Enterprise customers can configure secure access to webhooks within private networks. + + +#### Headers + +Specify headers as a JSON object: + +```json +{ + "Authorization": "Bearer YOUR_API_KEY", + "Content-Type": "application/json" +} +``` + +#### Timeout + +The maximum time Portkey will wait for your webhook to respond before proceeding with a default `verdict: true`. + +- Default: `3000ms` (3 seconds) +- If your webhook processing is time-intensive, consider increasing this value + +### Webhook Request Structure + +Your webhook should accept `POST` requests with the following structure: + +#### Request Headers + +| Header | Description | +| :-- | :-- | +| `Content-Type` | Always set to `application/json` | +| Custom Headers | Any headers you configured in the Portkey UI | + +#### Request Body + +Portkey sends comprehensive information about the AI request to your webhook: + + + + Information about the user's request to the LLM + + OpenAI compliant request body json. + Last message/prompt content from the overall request body. + Whether the request uses streaming + + + + + Information about the LLM's response (empty for beforeRequestHook) + + OpenAI compliant response body json. + Last message/prompt content from the overall response body. + HTTP status code from LLM provider + + + + Portkey provider slug. Example: `openai`, `azure-openai`, etc. + Type of request: `chatComplete`, `complete`, or `embed` +Custom metadata passed with the request. Can come from: 1) the `x-portkey-metadata` header, 2) default API key settings, or 3) workspace defaults. +When the hook is triggered: `beforeRequestHook` or `afterRequestHook` + + +#### Event Types + +Your webhook can be triggered at two points: + +- **beforeRequestHook**: Before the request is sent to the LLM provider +- **afterRequestHook**: After receiving a response from the LLM provider + + +```JSON beforeRequestHook Example [expandable] + { + "request": { + "json": { + "stream": false, + "messages": [ + { + "role": "system", + "content": "You are a helpful assistant" + }, + { + "role": "user", + "content": "Say Hi" + } + ], + "max_tokens": 20, + "n": 1, + "model": "gpt-4o-mini" + }, + "text": "Say Hi", + "isStreamingRequest": false, + "isTransformed": false + }, + "response": { + "json": {}, + "text": "", + "statusCode": null, + "isTransformed": false + }, + "provider": "openai", + "requestType": "chatComplete", + "metadata": { + "_user": "visarg123" + }, + "eventType": "beforeRequestHook" +} +``` +```JSON afterRequestHook Example [expandable] +{ + "request": { + "json": { + "stream": false, + "messages": [ + { + "role": "system", + "content": "You are a helpful assistant" + }, + { + "role": "user", + "content": "Say Hi" + } + ], + "max_tokens": 20, + "n": 1, + "model": "gpt-4o-mini" + }, + "text": "Say Hi", + "isStreamingRequest": false, + "isTransformed": false + }, + "response": { + "json": { + "id": "chatcmpl-B9SAAj7zd4mq12omkeEImYvYnjbOr", + "object": "chat.completion", + "created": 1741592910, + "model": "gpt-4o-mini-2024-07-18", + "choices": [ + { + "index": 0, + "message": { + "role": "assistant", + "content": "Hi! How can I assist you today?", + "refusal": null + }, + "logprobs": null, + "finish_reason": "stop" + } + ], + "usage": { + "prompt_tokens": 18, + "completion_tokens": 10, + "total_tokens": 28, + "prompt_tokens_details": { + "cached_tokens": 0, + "audio_tokens": 0 + }, + "completion_tokens_details": { + "reasoning_tokens": 0, + "audio_tokens": 0, + "accepted_prediction_tokens": 0, + "rejected_prediction_tokens": 0 + } + }, + "service_tier": "default", + "system_fingerprint": "fp_06737a9306" + }, + "text": "Hi! How can I assist you today?", + "statusCode": 200, + "isTransformed": false + }, + "provider": "openai", + "requestType": "chatComplete", + "metadata": { + "_user": "visarg123" + }, + "eventType": "afterRequestHook" +} +``` + + +### Webhook Response Structure + +Your webhook must return a response that follows this structure: + +#### Response Body + + + + Whether the request/response passes your guardrail check: + - `true`: No violations detected + - `false`: Violations detected + + + + Optional field to modify the request or response + + + Modified request data (only for beforeRequestHook) + If this field is found in the Webhook response, Portkey will fully override the existing request body with the returned data. + + + Modified response data (only for afterRequestHook) + If this field is found in the Webhook response, Portkey will fully override the existing response body with the returned data. + + + + + +## Webhook Capabilities + +Your webhook can perform three main actions: + +### Simple Validation + +Return a verdict without modifying the request/response: + +```json +{ + "verdict": true // or false if the request violates your guardrails +} +``` + +### Request Transformation + +Modify the user's request before it reaches the LLM provider: + + + +```json +{ + "verdict": true, + "transformedData": { + "request": { + "json": { + "messages": [ + { + "role": "system", + "content": "You are a helpful assistant. Do not provide harmful content." + }, + { + "role": "user", + "content": "Original user message" + } + ], + "max_tokens": 100, + "model": "gpt-4o" + } + } + } +} +``` + + +```json +{ + "verdict": true, + "transformedData": { + "request": { + "json": { + "messages": [ + { + "role": "system", + "content": "You are a helpful assistant" + }, + { + "role": "user", + "content": "My name is [REDACTED] and my email is [REDACTED]" + } + ], + "max_tokens": 100, + "model": "gpt-4o" + } + } + } +} +``` + + + +### Response Transformation + +Modify the LLM's response before it reaches the user: + + + +```json +{ + "verdict": true, + "transformedData": { + "response": { + "json": { + "id": "chatcmpl-123", + "object": "chat.completion", + "created": 1741592832, + "model": "gpt-4o-mini", + "choices": [ + { + "index": 0, + "message": { + "role": "assistant", + "content": "I've filtered this response to comply with our content policies." + }, + "finish_reason": "stop" + } + ], + "usage": { + "prompt_tokens": 23, + "completion_tokens": 12, + "total_tokens": 35 + } + }, + "text": "I've filtered this response to comply with our content policies." + } + } +} +``` + + +```json +{ + "verdict": true, + "transformedData": { + "response": { + "json": { + "id": "chatcmpl-123", + "object": "chat.completion", + "created": 1741592832, + "model": "gpt-4o-mini", + "choices": [ + { + "index": 0, + "message": { + "role": "assistant", + "content": "Original response with additional disclaimer: This response is provided for informational purposes only." + }, + "finish_reason": "stop" + } + ], + "usage": { + "prompt_tokens": 23, + "completion_tokens": 20, + "total_tokens": 43 + } + }, + "text": "Original response with additional disclaimer: This response is provided for informational purposes only." + } + } +} +``` + + + +## Passing Metadata to Your Webhook + +You can include additional context with each request using Portkey's metadata feature: + +```json +// In your API request to Portkey +"x-portkey-metadata": {"user": "john", "context": "customer_support"} +``` + +This metadata will be forwarded to your webhook in the `metadata` field. [Learn more about metadata](/product/observability/metadata). + +## Important Implementation Notes + +1. **Complete Transformations**: When using `transformedData`, include all fields in your transformed object, not just the changed portions. + +2. **Independent Verdict and Transformation**: The `verdict` and any transformations are independent. You can return `verdict: false` while still returning transformations. + +3. **Default Behavior**: If your webhook fails to respond within the timeout period, Portkey will default to `verdict: true`. + +4. **Event Type Awareness**: When implementing transformations, ensure your webhook checks the `eventType` field to determine whether it's being called before or after the LLM request. + +## Example Implementation + +Check out our Guardrail Webhook implementation on GitHub: + + + +## Get Help + +Building custom webhooks? Join the [Portkey Discord community](https://portkey.ai/community) for support and to share your implementation experiences! diff --git a/virtual_key_old/integrations/guardrails/lasso.mdx b/virtual_key_old/integrations/guardrails/lasso.mdx new file mode 100644 index 00000000..e346125a --- /dev/null +++ b/virtual_key_old/integrations/guardrails/lasso.mdx @@ -0,0 +1,134 @@ +--- +title: "Lasso Security" +description: "Lasso Security protects your GenAI apps from data leaks, prompt injections, and other potential risks, keeping your systems safe and secure." +--- + +[Lasso Security](https://www.lasso.security/) provides comprehensive protection for your GenAI applications against various security threats including prompt injections, data leaks, and other potential risks that could compromise your AI systems. + +To get started with Lasso Security, visit their documentation: + + +## Using Lasso with Portkey + +### 1. Add Lasso Credentials to Portkey + +* Navigate to the `Integrations` page under `Settings` +* Click on the edit button for the Lasso integration +* Add your Lasso API Key (obtain this from your Lasso Security account) + +### 2. Add Lasso's Guardrail Check + +* Navigate to the `Guardrails` page and click the `Create` button +* Search for "Scan Content" and click `Add` +* Set the timeout in milliseconds (default: 10000ms) +* Set any `actions` you want on your check, and create the Guardrail! + + + Guardrail Actions allow you to orchestrate your guardrails logic. You can learn more about them [here](/product/guardrails#there-are-6-types-of-guardrail-actions) + + +| Check Name | Description | Parameters | Supported Hooks | +|------------|-------------|------------|-----------------| +| Scan Content | Lasso Security's Deputies analyze content for various security risks including jailbreak attempts, custom policy violations, sexual content, hate speech, illegal content, and more. | `Timeout` (number) | `beforeRequestHook` | + + + +### 3. Add Guardrail ID to a Config and Make Your Request + +* When you save a Guardrail, you'll get an associated Guardrail ID - add this ID to the `input_guardrails` or `output_guardrails` params in your Portkey Config +* Create these Configs in Portkey UI, save them, and get an associated Config ID to attach to your requests. [More here](/product/ai-gateway/configs). + +Here's an example config: + +```json +{ + "input_guardrails": ["guardrails-id-xxx", "guardrails-id-yyy"], + "output_guardrails": ["guardrails-id-xxx", "guardrails-id-yyy"] +} +``` + + + + + +```js +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + config: "pc-***" // Supports a string config id or a config object +}); +``` + + + +```py +portkey = Portkey( + api_key="PORTKEY_API_KEY", + config="pc-***" # Supports a string config id or a config object +) +``` + + + +```js +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + apiKey: "PORTKEY_API_KEY", + config: "CONFIG_ID" + }) +}); +``` + + + +```py +client = OpenAI( + api_key="OPENAI_API_KEY", # defaults to os.environ.get("OPENAI_API_KEY") + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY", # defaults to os.environ.get("PORTKEY_API_KEY") + config="CONFIG_ID" + ) +) +``` + + + +```sh +curl https://api.portkey.ai/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-config: $CONFIG_ID" \ + -d '{ + "model": "gpt-3.5-turbo", + "messages": [{ + "role": "user", + "content": "Hello!" + }] + }' +``` + + + +For more, refer to the [Config documentation](/product/ai-gateway/configs). + +Your requests are now guarded by Lasso Security's protective measures, and you can see the verdict and any actions taken directly in your Portkey logs! + +## Key Security Features + +Lasso Security's Deputies analyze content for various security risks across multiple categories: + +1. **Prompt Injections**: Detects attempts to manipulate AI behavior through crafted inputs +2. **Data Leaks**: Prevents sensitive information from being exposed through AI interactions +3. **Jailbreak Attempts**: Identifies attempts to bypass AI safety mechanisms +4. **Custom Policy Violations**: Enforces your organization's specific security policies +5. **Harmful Content Detection**: Flags sexual content, hate speech, illegal content, and more + +Learn more about Lasso Security's features [here](https://www.lasso.security/features). + +## Get Support + +If you face any issues with the Lasso Security integration, join the [Portkey community forum](https://discord.gg/portkey-llms-in-prod-1143393887742861333) for assistance. diff --git a/virtual_key_old/integrations/guardrails/mistral.mdx b/virtual_key_old/integrations/guardrails/mistral.mdx new file mode 100644 index 00000000..56720fa3 --- /dev/null +++ b/virtual_key_old/integrations/guardrails/mistral.mdx @@ -0,0 +1,150 @@ +--- +title: "Mistral" +description: "Mistral moderation service helps detect and filter harmful content across multiple policy dimensions to secure your AI applications." +--- + +[Mistral AI](https://mistral.ai/) provides a sophisticated content moderation service that enables users to detect harmful text content across multiple policy dimensions, helping to secure LLM applications and ensure safe AI interactions. + +To get started with Mistral, visit their documentation: + + +## Using Mistral with Portkey + +### 1. Add Mistral Credentials to Portkey + +* Navigate to the `Integrations` page under `Settings` +* Click on the edit button for the Mistral integration +* Add your Mistral La Plateforme API Key (obtain this from your Mistral account) + +### 2. Add Mistral's Guardrail Check + +* Navigate to the `Guardrails` page and click the `Create` button +* Search for "Moderate Content" and click `Add` +* Configure your moderation checks by selecting which categories to filter: + - Sexual + - Hate and discrimination + - Violence and threats + - Dangerous and criminal content + - Selfharm + - Health + - Financial + - Law + - PII (Personally Identifiable Information) +* Set the timeout in milliseconds (default: 5000ms) +* Set any `actions` you want on your check, and create the Guardrail! + + + Guardrail Actions allow you to orchestrate your guardrails logic. You can learn more about them [here](/product/guardrails#there-are-6-types-of-guardrail-actions) + + +| Check Name | Description | Parameters | Supported Hooks | +|------------|-------------|------------|-----------------| +| Moderate Content | Checks if content passes selected content moderation checks | `Moderation Checks` (array), `Timeout` (number) | `beforeRequestHook`, `afterRequestHook` | + +### 3. Add Guardrail ID to a Config and Make Your Request + +* When you save a Guardrail, you'll get an associated Guardrail ID - add this ID to the `input_guardrails` or `output_guardrails` params in your Portkey Config +* Create these Configs in Portkey UI, save them, and get an associated Config ID to attach to your requests. [More here](/product/ai-gateway/configs). + +Here's an example config: + +```json +{ + "input_guardrails": ["guardrails-id-xxx", "guardrails-id-yyy"], + "output_guardrails": ["guardrails-id-xxx", "guardrails-id-yyy"] +} +``` + + + + +```js +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + config: "pc-***" // Supports a string config id or a config object +}); +``` + + + +```py +portkey = Portkey( + api_key="PORTKEY_API_KEY", + config="pc-***" # Supports a string config id or a config object +) +``` + + + +```js +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + apiKey: "PORTKEY_API_KEY", + config: "CONFIG_ID" + }) +}); +``` + + + +```py +client = OpenAI( + api_key="OPENAI_API_KEY", # defaults to os.environ.get("OPENAI_API_KEY") + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY", # defaults to os.environ.get("PORTKEY_API_KEY") + config="CONFIG_ID" + ) +) +``` + + + +```sh +curl https://api.portkey.ai/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-config: $CONFIG_ID" \ + -d '{ + "model": "gpt-3.5-turbo", + "messages": [{ + "role": "user", + "content": "Hello!" + }] + }' +``` + + + +For more, refer to the [Config documentation](/product/ai-gateway/configs). + +Your requests are now guarded by Mistral's moderation service, and you can see the verdict and any actions taken directly in your Portkey logs! + +## Content Moderation Categories + +Mistral's moderation service can detect content across 9 key policy categories: + +1. **Sexual**: Content of sexual nature or adult content +2. **Hate and Discrimination**: Content expressing hatred or promoting discrimination +3. **Violence and Threats**: Content depicting violence or threatening language +4. **Dangerous and Criminal Content**: Instructions for illegal activities or harmful actions +5. **Self-harm**: Content related to self-injury, suicide, or eating disorders +6. **Health**: Unqualified medical advice or health misinformation +7. **Financial**: Unqualified financial advice or dubious investment schemes +8. **Law**: Unqualified legal advice or recommendations +9. **PII**: Personally identifiable information, including email addresses, phone numbers, etc. + +Mistral's moderation service is natively multilingual, with support for Arabic, Chinese, English, French, German, Italian, Japanese, Korean, Portuguese, Russian, and Spanish. + +## Get Support + +If you face any issues with the Mistral integration, join the [Portkey community forum](https://discord.gg/portkey-llms-in-prod-1143393887742861333) for assistance. + +## Learn More + +- [Mistral AI Website](https://mistral.ai/) +- [Mistral Moderation Documentation](https://docs.mistral.ai/capabilities/guardrailing/) diff --git a/virtual_key_old/integrations/guardrails/pangea.mdx b/virtual_key_old/integrations/guardrails/pangea.mdx new file mode 100644 index 00000000..c79d57a1 --- /dev/null +++ b/virtual_key_old/integrations/guardrails/pangea.mdx @@ -0,0 +1,216 @@ +--- +title: "Pangea" +description: "Pangea AI Guard helps analyze and redact text to prevent model manipulation and malicious content." +--- + +[Pangea](https://pangea.cloud) provides AI Guard service for scanning LLM inputs and outputs to avoid manipulation of the model, addition of malicious content, and other undesirable data transfers. + +To get started with Pangea, visit their documentation: + + +## Using Pangea with Portkey + +### 1. Add Pangea Credentials to Portkey + +* Navigate to the `Plugins` page under `Sidebar` +* Click on the edit button for the Pangea integration +* Add your Pangea token and domain information (refer to [Pangea's documentation](https://pangea.cloud/docs/ai-guard) for how to obtain these credentials) + + +### 2. Add Pangea's Guardrail Check + +* Navigate to the `Guardrails` page and click the `Create` button +* Search for Pangea's AI Guard and click `Add` +* Configure your guardrail settings: recipe & debug (see [Pangea's API documentation](https://pangea.cloud/docs/ai-guard/apis) for more details) +* Set any actions you want on your check, and create the Guardrail! + + + Guardrail Actions allow you to orchestrate your guardrails logic. You can learn them [here](/product/guardrails#there-are-6-types-of-guardrail-actions) + + +| Check Name | Description | Parameters | Supported Hooks | +|------------|-------------|------------|-----------------| +| AI Guard | Analyze and redact text to avoid manipulation of the model and malicious content | recipe (string), debug (boolean) | `beforeRequestHook`, `afterRequestHook` | + +### 3. Add Guardrail ID to a Config and Make Your Request + +* When you save a Guardrail, you'll get an associated Guardrail ID - add this ID to the `input_guardrails` or `output_guardrails` params in your Portkey Config +* Create these Configs in Portkey UI, save them, and get an associated Config ID to attach to your requests. [More here](/product/ai-gateway/configs). + +Here's an example config: + +```json +{ + "input_guardrails": ["guardrails-id-xxx", "guardrails-id-yyy"], + "output_guardrails": ["guardrails-id-xxx", "guardrails-id-yyy"] +} +``` + + + + + +```js +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + config: "pc-***" // Supports a string config id or a config object +}); +``` + + + +```py +portkey = Portkey( + api_key="PORTKEY_API_KEY", + config="pc-***" # Supports a string config id or a config object +) +``` + + + +```js +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + apiKey: "PORTKEY_API_KEY", + config: "CONFIG_ID" + }) +}); +``` + + + +```py +client = OpenAI( + api_key="OPENAI_API_KEY", # defaults to os.environ.get("OPENAI_API_KEY") + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY", # defaults to os.environ.get("PORTKEY_API_KEY") + config="CONFIG_ID" + ) +) +``` + + + +```sh +curl https://api.portkey.ai/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-config: $CONFIG_ID" \ + -d '{ + "model": "gpt-3.5-turbo", + "messages": [{ + "role": "user", + "content": "Hello!" + }] + }' +``` + + + +For more, refer to the [Config documentation](/product/ai-gateway/configs). + +Your requests are now guarded by Pangea AI Guard and you can see the Verdict and any action you take directly on Portkey logs! More detailed logs for your requests will also be available on your Pangea dashboard. + +--- + +## Using Raw Guardrails with Pangea + +You can define Pangea guardrails directly in your code for more programmatic control without using the Portkey UI. This "raw guardrails" approach lets you dynamically configure guardrails based on your application's needs. + + + We recommend that you create guardrails using the Portkey UI whenever possible. Raw guardrails are more complex and require you to manage credentials and configurations directly in your code. + + + + + +### Available Pangea Guardrails + +| Guardrail Name | ID | Description | Parameters | +| -------------- | -- | ----------- | ---------- | +| Pangea AI Guard | `pangea.textGuard` |Scans LLM inputs/outputs for malicious content, harmful patterns, etc.| `recipe` (string), `debug` (boolean) | +| Pangea PII Guard | `pangea.pii` | Detects and optionally redacts personal identifiable information | `redact` (boolean) | + +### Implementation Examples + + + +- **`type`**: Always set to `"guardrail"` for guardrail checks +- **`id`**: A unique identifier for your guardrail +- **`credentials`**: Authentication details for Pangea + - `api_key`: Your Pangea API key + - `domain`: Your Pangea domain (e.g., `aws.us-east-1.pangea.cloud`) +- **`checks`**: Array of guardrail checks to run + - `id`: The specific guardrail ID from the table above + - `parameters`: Configuration options specific to each guardrail +- **`deny`**: Whether to block the request if guardrail fails (true/false) +- **`async`**: Whether to run guardrail asynchronously (true/false) +- **`on_success`/`on_fail`**: Optional callbacks for success/failure scenarios + - `feedback`: Data for logging and analytics + - `weight`: Importance of this feedback (0-1) + - `value`: Feedback score (-10 to 10) + + + +```json +{ + "before_request_hooks": [ + { + "type": "guardrail", + "id": "pangea-org-guard", + "credentials": { + "api_key": "your_pangea_api_key", + "domain": "your_pangea_domain" + }, + "checks": [ + { + "id": "pangea.textGuard", + "parameters": { + "recipe": "security_recipe", + "debug": true, + } + } + ], + "deny": true, + "async": false, + "on_success": { + "feedback": { + "weight": 1, + "value": 1, + "metadata": { + "user": "user_xyz", + } + } + }, + "on_fail": { + "feedback": { + "weight": 1, + "value": -1, + "metadata": { + "user": "user_xyz", + } + } + } + } + ] +} +``` + + +When using raw guardrails, you must provide valid credentials for the Pangea service directly in your config. Make sure to handle these credentials securely and consider using environment variables or secrets management. + + + +## Get Support + +If you face any issues with the Pangea integration, just ping the @pangea team on the [community forum](https://discord.gg/portkey-llms-in-prod-1143393887742861333). + +## Learn More + +- [Pangea Documentation](https://pangea.cloud/docs/) +- [AI Guard Service Overview](https://pangea.cloud/docs/ai-guard) diff --git a/virtual_key_old/integrations/guardrails/patronus-ai.mdx b/virtual_key_old/integrations/guardrails/patronus-ai.mdx new file mode 100644 index 00000000..62c231e9 --- /dev/null +++ b/virtual_key_old/integrations/guardrails/patronus-ai.mdx @@ -0,0 +1,66 @@ +--- +title: "Patronus AI" +description: "Patronus excels in industry-specific guardrails for RAG workflows." +--- + It has a SOTA hallucination detection model Lynx, which is also [open source](https://www.patronus.ai/blog/lynx-state-of-the-art-open-source-hallucination-detection-model). Portkey integrates with multiple Patronus evaluators to help you enforce LLM behavior. + + Browse Patronus' docs for more info: + +}> + Learn more about Patronus AI and their offerings. + + +## Using Patronus with Portkey + +### 1\. Add Patronus API Key to Portkey + +Grab your Patronus API [key from here](https://app.patronus.ai/). + +On the `Integrations` page, click on the edit button for the Patronus and add your API key. + + + + + +### 2\. Add Patronus' Guardrail Checks & Actions + +Navigate to the `Guardrails` page and you will see the Guardrail Checks offered by Patronus there. Add the ones you want, set actions, and create the Guardrail! + + + + +#### List of Patronus Guardrail Checks + +| Check Name | Description | Parameters | Supported Hooks | +| :-------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------- | :------------------------------------ | :----------------- | +| Retrieval Answer Relevance | Checks whether the answer is on-topic to the input question. Does not measure correctness. | **ON** or **OFF** | afterRequestHooks | +| Custom Evaluator | Checks against custom criteria, based on Patronus evaluator profile name. | **string**(evaluator's profile name) | afterRequestHooks | +| Is Concise | Check that the output is clear and concise. | **ON** or **OFF** | afterRequestHooks | +| Is Helpful | Check that the output is helpful in its tone of voice. | **ON** or **OFF** | afterRequestHooks | +| Is Polite | Check that the output is polite in conversation. | **ON** or **OFF** | afterRequestHooks | +| No Apologies | Check that the output does not contain apologies. | **ON** or **OFF** | afterRequestHooks | +| No Gender Bias | Check whether the output contains gender stereotypes. Useful to mitigate PR risk from sexist or gendered model outputs. | **ON** or **OFF** | afterRequestHooks | +| No Racias Bias | Check whether the output contains any racial stereotypes or not. | **ON** or **OFF** | afterRequestHooks | +| Detect Toxicity | Checks output for abusive and hateful messages. | **ON** or **OFF** | afterRequestHooks | +| Detect PII | Checks for personally identifiable information (PII) - this is information that, in conjunction with other data, can identify an individual. | **ON** or **OFF** | afterRequestHooks | +| Detect PHI | Checks for protected health information (PHI), defined broadly as any information about an individual's health status or provision of healthcare. | **ON** or **OFF** | afterRequestHooks | + +Your Patronus Guardrail is now ready to be added to any Portkey request you'd like! + +### 3\. Add Guardrail ID to a Config and Make Your Request + + + +Patronus integration on Portkey currently only works on model outputs and not inputs. + + +* When you save a Guardrail, you'll get an associated Guardrail ID - add this ID to the `after_request_hooks` params in your Portkey Config. +* Save this Config and pass it along with any Portkey request you're making! + +Your requests are now guarded by your Patronus evaluators and you can see the Verdict and any action you take directly on Portkey logs! More detailed logs for your requests will also be available on your Patronus dashboard. + +--- + +## Get Support + +If you face any issues with the Patronus integration, just ping the @patronusai team on the [community forum](https://discord.gg/portkey-llms-in-prod-1143393887742861333). diff --git a/virtual_key_old/integrations/guardrails/pillar.mdx b/virtual_key_old/integrations/guardrails/pillar.mdx new file mode 100644 index 00000000..27d4561e --- /dev/null +++ b/virtual_key_old/integrations/guardrails/pillar.mdx @@ -0,0 +1,48 @@ +--- +title: "Pillar" +--- + +[Pillar Security](https://www.pillar.security/) is an all-in-one platform that empowers organizations to monitor, assess risks, and secure their AI activities. + +You can now use Pillar's advanced detection & evaluation models on Portkey with our open source integration, and make your app secure and reliable. + +To get started with Pillar, chat with their team here: +} /> + + +## Using Pillar with Portkey + +### 1\. Add Pillar API Key to Portkey + +* Navigate to the `Integrations` page under `Settings` +* Click on the edit button for the Pillar integration and add your API key + +### 2\. Add Pillar's Guardrail Check + +* Now, navigate to the "Guardrails" page +* Search for Pillar's Guardrail Checks `Scan Prompt` or `Scan Response` and click on `Add` +* Pick the info you'd like scanned and save the check. +* Set any actions you want on your check, and create the Guardrail! + +| Check Name | Description | Parameters | Supported Hooks | +| :------------- | :------------------------------------------------------------------------------------------------ | :---------- | :------------------ | +| Scan Prompt | Analyses your inputs for `prompt injection`, `PII`, `Secrets`, `Toxic Language`, and `Invisible Character` | Dropdown | `beforeRequestHooks` | +| Scan Response | Analyses your outputs for `PII`, Secrets, and `Toxic Language` | Dropdown | `afterRequestHooks` | + + + +Your Pillar Guardrail is now ready to be added to any Portkey request you'd like! + +### 3\. Add Guardrail ID to a Config and Make Your Request + +* When you save a Guardrail, you'll get an associated Guardrail ID - add this ID to the `before_request_hooks` or `after_request_hooks` params in your Portkey Config +* Save this Config and pass it along with any Portkey request you're making! + +Your requests are now guarded by your Pillar checks and you can see the Verdict and any action you take directly on Portkey logs! More detailed logs for your requests will also be available on your Pillar dashboard. + +--- + +## Get Support + +If you face any issues with the Pillar integration, just ping the @Pillar team on the [community forum](https://discord.gg/portkey-llms-in-prod-1143393887742861333). diff --git a/virtual_key_old/integrations/guardrails/prompt-security.mdx b/virtual_key_old/integrations/guardrails/prompt-security.mdx new file mode 100644 index 00000000..065a4903 --- /dev/null +++ b/virtual_key_old/integrations/guardrails/prompt-security.mdx @@ -0,0 +1,120 @@ +--- +title: "Prompt Security" +description: "Prompt Security detects and protects against prompt injection, sensitive data exposure, and other AI security threats." +--- + +[Prompt Security](https://www.prompt.security/solutions/employees) provides advanced protection for your AI applications against various security threats including prompt injections and sensitive data exposure, helping ensure safe interactions with LLMs. + +To get started with Prompt Security, visit their website: + + +## Using Prompt Security with Portkey + +### 1. Add Prompt Security Credentials to Portkey + +* Click on the `Admin Settings` button on Sidebar +* Navigate to `Plugins` tab under Organisation Settings +* Click on the edit button for the Prompt Security integration +* Add your Prompt Security API Key and API Domain (obtain these from your Prompt Security account) + +### 2. Add Prompt Security's Guardrail Check + +* Navigate to the `Guardrails` page and click the `Create` button +* Search for either "Protect Prompt" or "Protect Response" depending on your needs and click `Add` +* Set any `actions` you want on your check, and create the Guardrail! + + + Guardrail Actions allow you to orchestrate your guardrails logic. You can learn more about them [here](/product/guardrails#there-are-6-types-of-guardrail-actions) + + +| Check Name | Description | Parameters | Supported Hooks | +|------------|-------------|------------|-----------------| +| Protect Prompt | Protect a user prompt before it is sent to the LLM | None | `beforeRequestHook` | +| Protect Response | Protect a LLM response before it is sent to the user | None | `afterRequestHook` | + +### 3. Add Guardrail ID to a Config and Make Your Request + +* When you save a Guardrail, you'll get an associated Guardrail ID - add this ID to the `before_request_hooks` or `after_request_hooks` params in your Portkey Config +* Create these Configs in Portkey UI, save them, and get an associated Config ID to attach to your requests. [More here](/product/ai-gateway/configs). + +Here's an example configuration: + +```json +{ + "input_guardrails": ["guardrails-id-xxx"], + "output_guardrails": ["guardrails-id-yyy"], +} +``` + + + + +```js +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + config: "pc-***" // Supports a string config id or a config object +}); +``` + + + +```py +portkey = Portkey( + api_key="PORTKEY_API_KEY", + config="pc-***" # Supports a string config id or a config object +) +``` + + + +```js +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + apiKey: "PORTKEY_API_KEY", + config: "CONFIG_ID" + }) +}); +``` + + + +```py +client = OpenAI( + api_key="OPENAI_API_KEY", # defaults to os.environ.get("OPENAI_API_KEY") + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY", # defaults to os.environ.get("PORTKEY_API_KEY") + config="CONFIG_ID" + ) +) +``` + + + +```sh +curl https://api.portkey.ai/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-config: $CONFIG_ID" \ + -d '{ + "model": "gpt-3.5-turbo", + "messages": [{ + "role": "user", + "content": "Hello!" + }] + }' +``` + + + +For more, refer to the [Config documentation](/product/ai-gateway/configs). + +Your requests are now guarded by Prompt Security's protection mechanisms, and you can see the verdict and any actions taken directly in your Portkey logs! + +## Get Support + +If you face any issues with the Prompt Security integration, join the [Portkey community forum](https://discord.gg/portkey-llms-in-prod-1143393887742861333) for assistance. diff --git a/virtual_key_old/integrations/libraries.mdx b/virtual_key_old/integrations/libraries.mdx new file mode 100644 index 00000000..888b0f75 --- /dev/null +++ b/virtual_key_old/integrations/libraries.mdx @@ -0,0 +1,59 @@ +--- +title: "Overview" +--- + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/virtual_key_old/integrations/libraries/anthropic-computer-use.mdx b/virtual_key_old/integrations/libraries/anthropic-computer-use.mdx new file mode 100644 index 00000000..f21b0c02 --- /dev/null +++ b/virtual_key_old/integrations/libraries/anthropic-computer-use.mdx @@ -0,0 +1,296 @@ +--- +title: "Anthropic Computer Use" +--- + +Anthropic computer use is fully supported in Portkey. +For more information on the computer use tool, please refer to the [Anthropic documentation](https://docs.anthropic.com/en/docs/agents-and-tools/tool-use/computer-use-tool). + + +### Usage + + ```py Python + from portkey_ai import Portkey + + # Initialize the Portkey client + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY", + strict_open_ai_compliance="false" + + + ) + + # Create the request + response = portkey.chat.completions.create( + anthropic_beta="computer-use-2025-01-24", + model="claude-opus-4-20250514", + max_tokens=3000, + thinking={ + "type": "enabled", + "budget_tokens": 2030 + }, + stream=False, + + tools=[ + { + "type": "computer", + "computer": { + "name": "computer_20250124", + "display_width_px": 1024, + "display_height_px": 768, + "display_number": 1, + } + }, + { + "type": "text_editor_20250429", + "name": "str_replace_based_edit_tool" + }, + { + "type": "bash_20250124", + "name": "bash" + } + ], + messages=[ + { + "role": "user", + "content": "Save a picture of a cat to my desktop." + } + ] + ) + print(response) + ``` + ```ts NodeJS + import Portkey from 'portkey-ai'; + + // Initialize the Portkey client + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY", // Add your anthropic's virtual key + strictOpenAiCompliance: false + }); + + // Generate a chat completion + async function getChatCompletionFunctions() { + const response = await portkey.chat.completions.create({ + model: "claude-4-opus-20250514", + anthropic_beta: "computer-use-2025-01-24", + max_tokens: 3000, + thinking: { + type: "enabled", + budget_tokens: 2030 + }, + stream: false, + tools: [ + { + type: "computer", + computer: { + name: "computer_20250124", // This is the version of the tool + display_width_px: 1024, + display_height_px: 768, + display_number: 1 + } + }, + { + type: "text_editor_20250429", + name: "str_replace_based_edit_tool" + }, + { + type: "bash_20250124", + name: "bash" + } + ], + "messages": [ + { + role: "user", + content: "Save a picture of a cat to my desktop." + } + ] + }); + console.log(response); + } + // Call the function + getChatCompletionFunctions(); + ``` + ```sh cURL + curl "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-provider: anthropic" \ + -H "x-api-key: $ANTHROPIC_API_KEY" \ + -H "x-portkey-strict-open-ai-compliance: false" \ + -d '{ + "model": "claude-4-opus-20250514", + "anthropic_beta": "computer-use-2025-01-24", + "max_tokens": 3000, + "thinking": { + "type": "enabled", + "budget_tokens": 2030 + }, + "stream": false, + "tools": [ + { + "type": "computer", + "computer": { + "name": "computer_20250124", + "display_width_px": 1024, + "display_height_px": 768, + "display_number": 1 + } + }, + { + "type": "text_editor_20250429", + "name": "str_replace_based_edit_tool" + }, + { + "type": "bash_20250124", + "name": "bash" + } + ], + "messages": [ + { + "role": "user", + "content": "Save a picture of a cat to my desktop." + } + ] + }' + ``` + + + + + + +# Portkey Features +Now that you have enterprise-grade Anthropic Computer Use setup, let's explore the comprehensive features Portkey provides to ensure secure, efficient, and cost-effective AI-assisted development. + +### 1. Comprehensive Metrics +Using Portkey you can track 40+ key metrics including cost, token usage, response time, and performance across all your LLM providers in real time. Filter these metrics by developer, team, or project using custom metadata. + + + + + +### 2. Advanced Logs +Portkey's logging dashboard provides detailed logs for every request made by Anthropic Computer Use. These logs include: +- Complete request and response tracking +- Code context and generation metrics +- Developer attribution +- Cost breakdown per coding session + + + + + +### 3. Unified Access to 250+ LLMs + +Easily switch between 250+ LLMs for different coding tasks. Use GPT-4 for complex architecture decisions, Claude for detailed code reviews, or specialized models for specific languages - all through a single interface. + +### 4. Advanced Metadata Tracking +Track coding patterns and productivity metrics with custom metadata: +- Language and framework usage +- Code generation vs completion tasks +- Time-of-day productivity patterns +- Project-specific metrics + + + + +### 5. Enterprise Access Management + + + +Set and manage spending limits per developer or team. Prevent budget overruns with automatic cutoffs. + + + +Enterprise-grade SSO integration for seamless developer onboarding and offboarding. + + + +Hierarchical structure with teams, projects, and role-based access control for development organizations. + + + +Comprehensive audit logging for security compliance and code generation tracking. + + + +### 6. Reliability Features + + + Automatically switch between models if one fails, ensuring uninterrupted coding. + + + Route requests based on code complexity or language requirements. + + + Distribute requests across multiple API keys or providers. + + + Cache common code patterns to reduce costs and improve response times. + + + Automatic retry handling for failed requests with exponential backoff. + + + Enforce spending limits to control development costs. + + + +### 7. Advanced Guardrails + +Protect your codebase and enhance security with real-time checks on AI interactions: +- Prevent exposure of API keys and secrets +- Block generation of malicious code patterns +- Enforce coding standards and best practices +- Custom security rules for your organization +- License compliance checks + + +Implement real-time protection for your development environment with automatic detection and filtering of sensitive code, credentials, and security vulnerabilities. + + +# FAQs + + + +Portkey provides several ways to track developer costs: +- Create separate Virtual Keys for each developer +- Use metadata tags to identify developers +- Set up developer-specific API keys +- View detailed analytics in the dashboard + + + +When a developer reaches their budget limit: +1. Further requests will be blocked +2. The developer and admin receive notifications +3. Coding history remains available +4. Admins can adjust limits as needed + + + +Yes! Portkey supports local models through Ollama and other self-hosted solutions. Configure your local endpoint as a custom provider in Portkey and use it with Anthropic Computer Use just like any other provider. + + + +Portkey provides multiple security layers: +- Guardrails to prevent sensitive data exposure +- Request/response filtering +- Audit logs for all interactions +- Custom security rules +- PII detection and masking + + + +# Next Steps + +**Join our Community** +- [Discord Community](https://portkey.sh/discord-report) +- [GitHub Repository](https://github.com/Portkey-AI) + + + + +For enterprise support and custom features for your development teams, contact our [enterprise team](https://calendly.com/portkey-ai). + diff --git a/virtual_key_old/integrations/libraries/anythingllm.mdx b/virtual_key_old/integrations/libraries/anythingllm.mdx new file mode 100644 index 00000000..a7a3b85a --- /dev/null +++ b/virtual_key_old/integrations/libraries/anythingllm.mdx @@ -0,0 +1,375 @@ +--- +title: 'Anything LLM' +description: 'Add usage tracking, cost controls, and security guardrails to your Anything LLM deployment' +--- +Anything LLM all-in-one Desktop & Docker AI application with built-in RAG, AI agents, and more. +While Anything LLM delivers robust chat capabilities, Portkey adds essential enterprise controls for production deployments: + +- **Unified AI Gateway** - Single interface for 1600+ LLMs with API key management. (not just OpenAI & Anthropic) +- **Centralized AI observability**: Real-time usage tracking for 40+ key metrics and logs for every request +- **Governance** - Real-time spend tracking, set budget limits and RBAC in your Anything LLM setup +- **Security Guardrails** - PII detection, content filtering, and compliance controls + +This guide will walk you through integrating Portkey with Anything LLM and setting up essential enterprise features including usage tracking, access controls, and budget management. + + + If you are an enterprise looking to use Anything LLM in your organisation, [check out this section](#3-set-up-enterprise-governance-for-anything-llm). + + +# 1. Setting up Portkey +Portkey allows you to use 1600+ LLMs with your Anything LLM setup, with minimal configuration required. Let's set up the core components in Portkey that you'll need for integration. + + + +Virtual Keys are Portkey's secure way to manage your LLM provider API keys. Think of them like disposable credit cards for your LLM API keys, providing essential controls like: +- Budget limits for API usage +- Rate limiting capabilities +- Secure API key storage + +To create a virtual key: +Go to [Virtual Keys](https://app.portkey.ai/virtual-keys) in the Portkey App. Save and copy the virtual key ID + + + + + + +Save your virtual key ID - you'll need it for the next step. + + + + + +Configs in Portkey are JSON objects that define how your requests are routed. They help with implementing features like advanced routing, fallbacks, and retries. + +We need to create a default config to route our requests to the virtual key created in Step 1. + +To create your config: +1. Go to [Configs](https://app.portkey.ai/configs) in Portkey dashboard +2. Create new config with: + ```json + { + "virtual_key": "YOUR_VIRTUAL_KEY_FROM_STEP1", + } + ``` +3. Save and note the Config name for the next step + + + + + + +This basic config connects to your virtual key. You can add more advanced portkey features later. + + + + + +Now create Portkey API key access point and attach the config you created in Step 2: + +1. Go to [API Keys](https://app.portkey.ai/api-keys) in Portkey and Create new API key +2. Select your config from `Step 2` +3. Generate and save your API key + + + + + + +Save your API key securely - you'll need it for Anything LLM integration. + + + + +# 2. Integrate Portkey with AnythingLLM + +Now that you have your Portkey components set up, let's connect them to AnythingLLM. Since Portkey provides OpenAI API compatibility, integration is straightforward and requires just a few configuration steps in your AnythingLLM interface. + + +You need your Portkey API Key from [Step 1](#Getting-started-with-portkey) before going further. + + +1. Launch your AnythingLLM application +2. Navigate to `Settings > AI Providers > LLM` +3. In the LLM Provider dropdown, select `Generic OpenAI` +4. Configure the following settings: + - Base URL: `https://api.portkey.ai/v1` + - API Key: Your Portkey API key from the setup + - Chat Model: Your preferred model name (e.g., `gpt-4`, `claude-2`) + - Token Context Window: Set based on your model's limits + - Max Tokens: Configure according to your needs + + +You can monitor your requests and usage in the [Portkey Dashboard](https://app.portkey.ai/dashboard). + + +Make sure your virtual key has sufficient budget and rate limits for your expected usage. Also use the complete model name given by the provider. + + + + +# 3. Set Up Enterprise Governance for Anything LLM + +**Why Enterprise Governance?** +If you are using Anything LLM inside your orgnaization, you need to consider several governance aspects: +- **Cost Management**: Controlling and tracking AI spending across teams +- **Access Control**: Managing which teams can use specific models +- **Usage Analytics**: Understanding how AI is being used across the organization +- **Security & Compliance**: Maintaining enterprise security standards +- **Reliability**: Ensuring consistent service across all users + +Portkey adds a comprehensive governance layer to address these enterprise needs. Let's implement these controls step by step. + +**Enterprise Implementation Guide** + + + +### Step 1: Implement Budget Controls & Rate Limits + +Virtual Keys enable granular control over LLM access at the team/department level. This helps you: +- Set up [budget limits](/product/ai-gateway/virtual-keys/budget-limits) +- Prevent unexpected usage spikes using Rate limits +- Track departmental spending + +#### Setting Up Department-Specific Controls: +1. Navigate to [Virtual Keys](https://app.portkey.ai/virtual-keys) in Portkey dashboard +2. Create new Virtual Key for each department with budget limits and rate limits +3. Configure department-specific limits + + + + + + + + +### Step 2: Define Model Access Rules + +As your AI usage scales, controlling which teams can access specific models becomes crucial. Portkey Configs provide this control layer with features like: + +#### Access Control Features: +- **Model Restrictions**: Limit access to specific models +- **Data Protection**: Implement guardrails for sensitive data +- **Reliability Controls**: Add fallbacks and retry logic + +#### Example Configuration: +Here's a basic configuration to route requests to OpenAI, specifically using GPT-4o: + +```json +{ + "strategy": { + "mode": "single" + }, + "targets": [ + { + "virtual_key": "YOUR_OPENAI_VIRTUAL_KEY", + "override_params": { + "model": "gpt-4o" + } + } + ] +} +``` + +Create your config on the [Configs page](https://app.portkey.ai/configs) in your Portkey dashboard. You'll need the config ID for connecting to Anything LLM's setup. + + + +Configs can be updated anytime to adjust controls without affecting running applications. + + + + +### Step 3: Implement Access Controls + +Create User-specific API keys that automatically: +- Track usage per user/team with the help of virtual keys +- Apply appropriate configs to route requests +- Collect relevant metadata to filter logs +- Enforce access permissions + + +Create API keys through: +- [Portkey App](https://app.portkey.ai/) +- [API Key Management API](/api-reference/admin-api/control-plane/api-keys/create-api-key) + + +Example using Python SDK: +```python +from portkey_ai import Portkey + +portkey = Portkey(api_key="YOUR_ADMIN_API_KEY") + +api_key = portkey.api_keys.create( + name="engineering-team", + type="organisation", + workspace_id="YOUR_WORKSPACE_ID", + defaults={ + "config_id": "your-config-id", + "metadata": { + "environment": "production", + "department": "engineering" + } + }, + scopes=["logs.view", "configs.read"] +) +``` + +For detailed key management instructions, see our [API Keys documentation](/api-reference/admin-api/control-plane/api-keys/create-api-key). + + + +### Step 4: Deploy & Monitor +After distributing API keys to your team members, your enterprise-ready Anything LLM setup is ready to go. Each team member can now use their designated API keys with appropriate access levels and budget controls. +Apply your governance setup using the integration steps from earlier sections +Monitor usage in Portkey dashboard: +- Cost tracking by department +- Model usage patterns +- Request volumes +- Error rates + + + + + +### Enterprise Features Now Available +**Anything LLM now has:** + +- Departmental budget controls +- Model access governance +- Usage tracking & attribution +- Security guardrails +- Reliability features + + + +# Portkey Features +Now that you have enterprise-grade Anything LLM setup, let's explore the comprehensive features Portkey provides to ensure secure, efficient, and cost-effective AI operations. + +### 1. Comprehensive Metrics +Using Portkey you can track 40+ key metrics including cost, token usage, response time, and performance across all your LLM providers in real time. You can also filter these metrics based on custom metadata that you can set in your configs. Learn more about metadata here. + + + + + +### 2. Advanced Logs +Portkey's logging dashboard provides detailed logs for every request made to your LLMs. These logs include: +- Complete request and response tracking +- Metadata tags for filtering +- Cost attribution and much more... + + + + + + + + +### 3. Unified Access to 1600+ LLMs + +You can easily switch between 1600+ LLMs. Call various LLMs such as Anthropic, Gemini, Mistral, Azure OpenAI, Google Vertex AI, AWS Bedrock, and many more by simply changing the `virtual key` in your default `config` object. + + +### 4. Advanced Metadata Tracking +Using Portkey, you can add custom metadata to your LLM requests for detailed tracking and analytics. Use metadata tags to filter logs, track usage, and attribute costs across departments and teams. + + + + + + +### 5. Enterprise Access Management + + + +Set and manage spending limits across teams and departments. Control costs with granular budget limits and usage tracking. + + + +Enterprise-grade SSO integration with support for SAML 2.0, Okta, Azure AD, and custom providers for secure authentication. + + + +Hierarchical organization structure with workspaces, teams, and role-based access control for enterprise-scale deployments. + + + +Comprehensive access control rules and detailed audit logging for security compliance and usage tracking. + + + + +### 6. Reliability Features + + + Automatically switch to backup targets if the primary target fails. + + + Route requests to different targets based on specified conditions. + + + Distribute requests across multiple targets based on defined weights. + + + Enable caching of responses to improve performance and reduce costs. + + +Automatic retry handling with exponential backoff for failed requests + + + Set and manage budget limits across teams and departments. Control costs with granular budget limits and usage tracking. + + + + + +### 7. Advanced Guardrails + +Protect your Project's data and enhance reliability with real-time checks on LLM inputs and outputs. Leverage guardrails to: +- Prevent sensitive data leaks +- Enforce compliance with organizational policies +- PII detection and masking +- Content filtering +- Custom security rules +- Data compliance checks + + +Implement real-time protection for your LLM interactions with automatic detection and filtering of sensitive content, PII, and custom security rules. Enable comprehensive data protection while maintaining compliance with organizational policies. + + + +# FAQs + + + You can update your Virtual Key limits at any time from the Portkey dashboard:1. Go to Virtual Keys section2. Click on the Virtual Key you want to modify3. Update the budget or rate limits4. Save your changes + + + Yes! You can create multiple Virtual Keys (one for each provider) and attach them to a single config. This config can then be connected to your API key, allowing you to use multiple providers through a single API key. + + +Portkey provides several ways to track team costs: +- Create separate Virtual Keys for each team +- Use metadata tags in your configs +- Set up team-specific API keys +- Monitor usage in the analytics dashboard + + +When a team reaches their budget limit: +1. Further requests will be blocked +2. Team admins receive notifications +3. Usage statistics remain available in dashboard +4. Limits can be adjusted if needed + + + + +# Next Steps + + **Join our Community** +- [Discord Community](https://portkey.sh/discord-report) +- [GitHub Repository](https://github.com/Portkey-AI) + + +For enterprise support and custom features, contact our [enterprise team](https://calendly.com/portkey-ai). + diff --git a/virtual_key_old/integrations/libraries/autogen.mdx b/virtual_key_old/integrations/libraries/autogen.mdx new file mode 100644 index 00000000..1d5e5e78 --- /dev/null +++ b/virtual_key_old/integrations/libraries/autogen.mdx @@ -0,0 +1,159 @@ +--- +title: "Autogen (DEPRECATED)" +description: "AutoGen is a framework that enables the development of LLM applications using multiple agents that can converse with each other to solve tasks." +--- + + + +Click here to check out the latest integration of Autogen with Portkey + + + + + + + +Find more information about Autogen here: [https://microsoft.github.io/autogen/docs/Getting-Started](https://microsoft.github.io/autogen/docs/Getting-Started) + +## Quick Start Integration + +Autogen supports a concept of [config\_list](https://microsoft.github.io/autogen/docs/llm%5Fconfiguration) which allows definitions of the LLM provider and model to be used. Portkey seamlessly integrates into the Autogen framework through a custom config we create. + +### Example using minimal configuration + +```py +from autogen import AssistantAgent, UserProxyAgent, config_list_from_json + +# Import the portkey library to fetch helper functions +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +config_list = [ + { + "api_key": 'Your OpenAI Key', + "model": "gpt-3.5-turbo", + "base_url": PORTKEY_GATEWAY_URL, + "api_type": "openai", + "default_headers": createHeaders( + api_key = "Your Portkey API Key", + provider = "openai", + ) + } +] + +assistant = AssistantAgent("assistant", llm_config={"config_list": config_list}) +user_proxy = UserProxyAgent("user_proxy", code_execution_config={"work_dir": "coding", "use_docker": False}) # IMPORTANT: set to True to run code in docker, recommended +user_proxy.initiate_chat(assistant, message="Say this is also a test - part 2.") +# This initiates an automated chat between the two agents to solve the task +``` + +Notice that we updated the `base_url` to Portkey's AI Gateway and then added `default_headers` to enable Portkey specific features. + +When we execute this script, it would yield the same results as without Portkey, but every request can now be inspected in the Portkey Analytics & Logs UI - including token, cost, accuracy calculations. + + + + +All the config parameters supported in Portkey are available for use as part of the headers. Let's look at some examples: + +## Using 100+ models in Autogen through Portkey + +Since Portkey [seamlessly connects to 150+ models across providers](/integrations/llms), you can easily connect any of these to now run with Autogen. + +Let's see an example using **Mistral-7B on Anyscale** running with Autogen seamlessly: + +```py +from autogen import AssistantAgent, UserProxyAgent, config_list_from_json + +# Import the portkey library to fetch helper functions +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +config_list = [ + { + "api_key": 'Your Anyscale API Key', + "model": "mistralai/Mistral-7B-Instruct-v0.1", + "base_url": PORTKEY_GATEWAY_URL, + "api_type": "openai", # Portkey conforms to the openai api_type + "default_headers": createHeaders( + api_key = "Your Portkey API Key", + provider = "anyscale", + ) + } +] + +assistant = AssistantAgent("assistant", llm_config={"config_list": config_list}) +user_proxy = UserProxyAgent("user_proxy", code_execution_config={"work_dir": "coding", "use_docker": False}) # IMPORTANT: set to True to run code in docker, recommended +user_proxy.initiate_chat(assistant, message="Say this is also a test - part 2.") +# This initiates an automated chat between the two agents to solve the task +``` + +## Using a Virtual Key + +[Virtual keys](/product/ai-gateway/virtual-keys) in Portkey allow you to easily switch between providers without manually having to store and change their API keys. Let's use the same Mistral example above, but this time using a Virtual Key. + + + +```py +from autogen import AssistantAgent, UserProxyAgent, config_list_from_json + +# Import the portkey library to fetch helper functions +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +config_list = [ + { + # Set a dummy value, since we'll pick the API key from the virtual key + "api_key": 'X', + + # Pick the model from the provider of your choice + "model": "mistralai/Mistral-7B-Instruct-v0.1", + "base_url": PORTKEY_GATEWAY_URL, + "api_type": "openai", # Portkey conforms to the openai api_type + + "default_headers": createHeaders( + api_key = "Your Portkey API Key", + # Add your virtual key here + virtual_key = "Your Anyscale Virtual Key", + ) + } +] + +assistant = AssistantAgent("assistant", llm_config={"config_list": config_list}) +user_proxy = UserProxyAgent("user_proxy", code_execution_config={"work_dir": "coding", "use_docker": False}) # IMPORTANT: set to True to run code in docker, recommended +user_proxy.initiate_chat(assistant, message="Say this is also a test - part 2.") +# This initiates an automated chat between the two agents to solve the task +``` + +## Using Configs + +[Configs](/product/ai-gateway/configs) in Portkey unlock advanced management and routing functionality including [load balancing](/product/ai-gateway/load-balancing), [fallbacks](/product/ai-gateway/fallbacks), [canary testing](/product/ai-gateway/canary-testing), [switching models](/product/ai-gateway/universal-api) and more. + +You can use Portkey configs in Autogen like this: + +```py +from autogen import AssistantAgent, UserProxyAgent, config_list_from_json + +# Import the portkey library to fetch helper functions +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +config_list = [ + { + # Set a dummy value, since we'll pick the API key from the virtual key + "api_key": 'X', + + # Pick the model from the provider of your choice + "model": "mistralai/Mistral-7B-Instruct-v0.1", + "base_url": PORTKEY_GATEWAY_URL, + "api_type": "openai", # Portkey conforms to the openai api_type + + "default_headers": createHeaders( + api_key = "Your Portkey API Key", + # Add your Portkey config id + config = "Your Config ID", + ) + } +] + +assistant = AssistantAgent("assistant", llm_config={"config_list": config_list}) +user_proxy = UserProxyAgent("user_proxy", code_execution_config={"work_dir": "coding", "use_docker": False}) # IMPORTANT: set to True to run code in docker, recommended +user_proxy.initiate_chat(assistant, message="Say this is also a test - part 2.") +# This initiates an automated chat between the two agents to solve the task +``` diff --git a/virtual_key_old/integrations/libraries/claude-code.mdx b/virtual_key_old/integrations/libraries/claude-code.mdx new file mode 100644 index 00000000..ab1ed1f5 --- /dev/null +++ b/virtual_key_old/integrations/libraries/claude-code.mdx @@ -0,0 +1,342 @@ +--- +title: 'Claude Code' +description: 'Integrate Portkey with Claude Code for enterprise-grade AI coding assistance with observability and governance' +--- + +Claude Code is Anthropic's agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster through natural language commands. + +With Portkey integration, you can enhance Claude Code with enterprise features: + +- **Unified AI Gateway** - Route Claude Code through multiple providers (Anthropic, Bedrock, Vertex AI) +- **Centralized Logs** - Track all Claude Code interactions +- **Security & Governance** - Set rate limits, implement access controls, and audit all AI usage + + + This integration uses Portkey's Gateway Passthrough feature. Advanced Portkey capabilities - such as cost calculation, reliability features, and more are not supported yet. + + +# 1. Setting up Portkey + +Before integrating with Claude Code, you'll need to set up the core Portkey components. + + + +Virtual Keys securely manage your LLM provider API keys with built-in controls: +- Budget limits and rate limiting +- Secure credential storage +- Provider switching without code changes + +To create a virtual key: +1. Go to [Virtual Keys](https://app.portkey.ai/virtual-keys) in Portkey +2. Add your provider (Anthropic, Bedrock, or Vertex AI) credentials +3. Save and copy the virtual key ID + + + + + + +Save your virtual key ID - you'll need it for Claude Code configuration. + + + + +Configs enable advanced routing features. You can either use a virtual key directly or create a config for more control. + +To create a config: +1. Go to [Configs](https://app.portkey.ai/configs) in Portkey +2. Create new config: + ```json + { + "virtual_key": "YOUR_VIRTUAL_KEY_ID", + "override_params": { + "model": "us.anthropic.claude-sonnet-4-20250514-v1:0" + } + } + ``` +3. Save and note the Config ID + + +Configs are optional but recommended for production deployments with fallbacks and load balancing. + + + + +1. Go to [API Keys](https://app.portkey.ai/api-keys) in Portkey +2. Create or copy your existing API key +3. Keep it ready for the next steps + + + +# 2. Integrate Portkey with Claude Code + +Claude Code uses a `settings.json` file for configuration. You'll need to modify the environment variables to route requests through Portkey. + + +For more details on Claude Code proxy configuration, see [Anthropic's documentation](https://docs.anthropic.com/en/docs/claude-code/bedrock-vertex-proxies). + + + + +Edit your Claude Code settings file (`~/.claude/settings.json` for global or `.claude/settings.json` in your project): + +```json +{ + "env": { + "ANTHROPIC_BEDROCK_BASE_URL": "https://api.portkey.ai/v1", + "ANTHROPIC_CUSTOM_HEADERS": "x-portkey-api-key: YOUR_PORTKEY_API_KEY\nx-portkey-provider: bedrock\nx-portkey-virtual-key: YOUR_VIRTUAL_KEY", + "ANTHROPIC_MODEL": "us.anthropic.claude-sonnet-4-20250514-v1:0", + "CLAUDE_CODE_SKIP_BEDROCK_AUTH": 1, + "CLAUDE_CODE_USE_BEDROCK": 1 + } +} +``` + + +The `CLAUDE_CODE_SKIP_BEDROCK_AUTH` and `CLAUDE_CODE_USE_BEDROCK` must be set as numbers (not strings). + + +Replace: +- `YOUR_PORTKEY_API_KEY` with your Portkey API key +- `YOUR_VIRTUAL_KEY` with the virtual key ID from Step 1 +- Update the model name if using a different Claude model + + + +For Vertex AI integration: + +```json +{ + "env": { + "ANTHROPIC_VERTEX_BASE_URL": "https://api.portkey.ai/v1", + "ANTHROPIC_CUSTOM_HEADERS": "x-portkey-api-key: YOUR_PORTKEY_API_KEY\nx-portkey-provider: vertex-ai\nx-portkey-virtual-key: YOUR_VIRTUAL_KEY", + "ANTHROPIC_MODEL": "claude-3-7-sonnet@20250219", + "CLAUDE_CODE_SKIP_VERTEX_AUTH": 1, + "CLAUDE_CODE_USE_VERTEX": 1 + } +} +``` + + +Use `ANTHROPIC_VERTEX_BASE_URL` specifically for Vertex AI, not the generic base URL. + + + + +For direct Anthropic API usage: + +```json +{ + "env": { + "ANTHROPIC_BASE_URL": "https://api.portkey.ai/v1", + "ANTHROPIC_CUSTOM_HEADERS": "x-portkey-api-key: YOUR_PORTKEY_API_KEY\nx-portkey-provider: anthropic\nx-portkey-virtual-key: YOUR_VIRTUAL_KEY", + "ANTHROPIC_MODEL": "claude-sonnet-4-20250514" + } +} +``` + + + +If you created a Portkey config for any provider: + +```json +{ + "env": { + "ANTHROPIC_BEDROCK_BASE_URL": "https://api.portkey.ai/v1", + "ANTHROPIC_CUSTOM_HEADERS": "x-portkey-api-key: YOUR_PORTKEY_API_KEY\nx-portkey-config: YOUR_CONFIG_ID", + "ANTHROPIC_MODEL": "us.anthropic.claude-sonnet-4-20250514-v1:0", + "CLAUDE_CODE_SKIP_BEDROCK_AUTH": 1, + "CLAUDE_CODE_USE_BEDROCK": 1 + } +} +``` + + +When using a config, Portkey will use the provider settings defined in your config. + + + + +## Complete Configuration Example + +Here's a full `settings.json` example with permissions and Bedrock configuration: + +```json +{ + "permissions": { + "allow": [ + "Bash(npm run lint)", + "Bash(npm run test:*)", + "Read(~/.zshrc)" + ], + "deny": [ + "Bash(curl:*)" + ] + }, + "env": { + "ANTHROPIC_BEDROCK_BASE_URL": "https://api.portkey.ai/v1", + "ANTHROPIC_CUSTOM_HEADERS": "x-portkey-api-key: YOUR_PORTKEY_API_KEY\nx-portkey-provider: bedrock\nx-portkey-virtual-key: YOUR_VIRTUAL_KEY", + "ANTHROPIC_MODEL": "us.anthropic.claude-sonnet-4-20250514-v1:0", + "CLAUDE_CODE_SKIP_BEDROCK_AUTH": 1, + "CLAUDE_CODE_USE_BEDROCK": 1 + } +} +``` + +## Adding Custom Metadata + +Track Claude Code usage by team, project, or user: + +```json +{ + "env": { + "ANTHROPIC_BEDROCK_BASE_URL": "https://api.portkey.ai/v1", + "ANTHROPIC_CUSTOM_HEADERS": "x-portkey-api-key: YOUR_PORTKEY_API_KEY\nx-portkey-config: YOUR_CONFIG_ID\nx-portkey-metadata: {\"_user\": \"john.doe\", \"team\": \"engineering\", \"project\": \"backend-api\"}", + "CLAUDE_CODE_SKIP_BEDROCK_AUTH": 1, + "CLAUDE_CODE_USE_BEDROCK": 1 + } +} +``` + +## Verifying the Integration + +After configuring, test Claude Code: + +```bash +# Start Claude Code +claude + +# Ask it to perform a task +> explain the authentication flow in this codebase +``` + +You should see: +1. Claude Code functioning normally in your terminal +2. Requests appearing in your [Portkey logs](https://app.portkey.ai/logs) +3. Metrics updating in your [Portkey dashboard](https://app.portkey.ai/dashboard) + +# 3. Set Up Enterprise Governance for Claude Code + +When deploying Claude Code across your organization, implement these governance controls: + + + +Create separate virtual keys for each team with specific limits: + +1. **Engineering Team** + - Budget: $500/month + - Models: Claude Sonnet only + - Rate limit: 100 requests/minute + +2. **Data Science Team** + - Budget: $1000/month + - Models: All Claude models + - Rate limit: 200 requests/minute + +Deploy team-specific `settings.json` through your configuration management: + +```json +{ + "env": { + "ANTHROPIC_BEDROCK_BASE_URL": "https://api.portkey.ai/v1", + "ANTHROPIC_CUSTOM_HEADERS": "x-portkey-api-key: TEAM_PORTKEY_API_KEY\nx-portkey-virtual-key: TEAM_VIRTUAL_KEY", + "ANTHROPIC_MODEL": "us.anthropic.claude-sonnet-4-20250514-v1:0", + "CLAUDE_CODE_SKIP_BEDROCK_AUTH": 1, + "CLAUDE_CODE_USE_BEDROCK": 1 + } +} +``` + + + +For enterprise deployments, use policy files to enforce settings: + +**macOS**: `/Library/Application Support/ClaudeCode/policies.json` +**Linux/WSL**: `/etc/claude-code/policies.json` + +Example policy file: +```json +{ + "env": { + "ANTHROPIC_BEDROCK_BASE_URL": "https://api.portkey.ai/v1", + "ANTHROPIC_CUSTOM_HEADERS": "x-portkey-api-key: ENTERPRISE_API_KEY\nx-portkey-config: ENTERPRISE_CONFIG_ID", + "CLAUDE_CODE_SKIP_BEDROCK_AUTH": 1, + "CLAUDE_CODE_USE_BEDROCK": 1 + }, + "permissions": { + "deny": [ + "Bash(rm -rf*)", + "Edit(/etc/*)" + ] + } +} +``` + + + +Set up monitoring in Portkey: + +1. **Create Alerts** + - Budget threshold warnings at 80% + - Unusual usage patterns + - Failed request spikes + +2. **Regular Reviews** + - Weekly cost reports by team + - Model usage distribution + - Performance metrics + +3. **Compliance Tracking** + - Export logs for audit trails + - Monitor for sensitive data in prompts + - Track user activity patterns + + + +# Advanced Features + +## Caching for Faster Responses + +Enable caching in your Portkey config to speed up repeated queries: + +```json +{ + "virtual_key": "YOUR_VIRTUAL_KEY", + "cache": { + "mode": "simple", + } +} +``` + +## Security & Compliance + + + + +Set up fine-grained permissions and audit logging + + + +Prevent overspending with hard budget limits + + + +Track all Claude Code usage for compliance + + + +# References + +- [Claude Code Proxy Documentation](https://docs.anthropic.com/en/docs/claude-code/bedrock-vertex-proxies) - Official Anthropic documentation for proxy configuration +- [Portkey Virtual Keys](/product/ai-gateway/virtual-keys) - Secure credential management +- [Portkey Configs](/product/ai-gateway/configs) - Advanced routing configuration + +# Next Steps + +- [Set up team workflows](/docs/product/enterprise-offering/org-management) with role-based access +- [Configure advanced routing](/docs/product/ai-gateway) for reliability +- [Monitor usage patterns](/docs/product/observability) across your organization + + +For enterprise support and custom features, contact our [enterprise team](https://portkey.sh/claude-code-enterprise). + diff --git a/virtual_key_old/integrations/libraries/cline.mdx b/virtual_key_old/integrations/libraries/cline.mdx new file mode 100644 index 00000000..493c6361 --- /dev/null +++ b/virtual_key_old/integrations/libraries/cline.mdx @@ -0,0 +1,417 @@ +--- +title: 'Cline' +description: 'Add enterprise-grade observability, cost tracking, and governance to your Cline AI coding assistant' +--- + +Cline is an AI coding assistant that integrates directly into your VS Code environment, providing autonomous coding capabilities. While Cline offers powerful AI assistance for development tasks, Portkey adds essential enterprise controls for production deployments: + +- **Unified AI Gateway** - Single interface for 250+ LLMs with API key management +- **Centralized AI observability**: Real-time usage tracking for 40+ key metrics and logs for every request +- **Governance** - Real-time spend tracking, set budget limits and RBAC in your Cline setup +- **Security Guardrails** - PII detection, content filtering, and compliance controls + +This guide will walk you through integrating Portkey with Cline and setting up essential enterprise features including usage tracking, access controls, and budget management. + + + If you are an enterprise looking to standardize Cline usage across your development teams, [check out this section](#3-set-up-enterprise-governance-for-cline). + + +# 1. Setting up Portkey +Portkey allows you to use 250+ LLMs with your Cline setup, with minimal configuration required. Let's set up the core components in Portkey that you'll need for integration. + + + +Virtual Keys are Portkey's secure way to manage your LLM provider API keys. Think of them like disposable credit cards for your LLM API keys, providing essential controls like: +- Budget limits for API usage +- Rate limiting capabilities +- Secure API key storage + +To create a virtual key: +Go to [Virtual Keys](https://app.portkey.ai/virtual-keys) in the Portkey App. Save and copy the virtual key ID + + + + + + +Save your virtual key ID - you'll need it for the next step. + + + + + +Configs in Portkey are JSON objects that define how your requests are routed. They help with implementing features like advanced routing, fallbacks, and retries. + +We need to create a default config to route our requests to the virtual key created in Step 1. + +To create your config: +1. Go to [Configs](https://app.portkey.ai/configs) in Portkey dashboard +2. Create new config with: + ```json + { + "virtual_key": "YOUR_VIRTUAL_KEY_FROM_STEP1", + "override_params": { + "model": "gpt-4o" // Your preferred model name + } + } + ``` +3. Save and note the Config name for the next step + + + + + + +This basic config connects to your virtual key. You can add more advanced portkey features later. + + + + + +Now create Portkey API key access point and attach the config you created in Step 2: + +1. Go to [API Keys](https://app.portkey.ai/api-keys) in Portkey and Create new API key +2. Select your config from `Step 2` +3. Generate and save your API key + + + + + + +Save your API key securely - you'll need it for Cline integration. + + + + +# 2. Integrate Portkey with Cline + +Now that you have your Portkey components set up, let's connect them to Cline. Since Portkey provides OpenAI API compatibility, integration is straightforward and requires just a few configuration steps in your VS Code settings. + + +You need your Portkey API Key from [Step 1](#1-setting-up-portkey) before going further. + + +### Opening Cline Settings + +1. Open VS Code with Cline installed +2. Press `Cmd/Ctrl + Shift + P` to open the command palette +3. Search for `Cline: Open in new tab` +4. Click on the settings gear icon โš™๏ธ in the Cline tab + +### Method 1: Using Default Config (Recommended) + +This method uses the default config you created in Portkey, making it easier to manage model settings centrally. + +1. In the Cline settings, navigate to **API Configuration** +2. Configure the following settings: + - **API Provider**: Select `OpenAI Compatible` + - **Base URL**: `https://api.portkey.ai/v1` + - **API Key**: Your Portkey API key from the setup + - **Model ID**: `dummy` (since the model is defined in your Portkey config) + + +Using a default config with `override_params` is recommended as it allows you to manage all model settings centrally in Portkey, reducing maintenance overhead. + + +### Method 2: Using Custom Headers + +If you prefer more direct control or need to use multiple providers dynamically, you can pass Portkey headers directly: + +1. Configure the basic settings as in Method 1: + - **API Provider**: `OpenAI Compatible` + - **Base URL**: `https://api.portkey.ai/v1` + - **API Key**: Your Portkey API key + - **Model ID**: Your desired model (e.g., `gpt-4o`, `claude-3-opus-20240229`) + +2. Add custom headers by clicking "Add Header" and include: + ``` + x-portkey-virtual-key: YOUR_VIRTUAL_KEY + ``` + + Optional headers: + ``` + x-portkey-provider: google // Only if not using virtual key + x-portkey-config: YOUR_CONFIG_ID // For additional config + ``` + + +Custom headers give you flexibility but require updating headers in Cline whenever you want to change providers or models. + + +You can now use Cline with all of Portkey's enterprise features enabled. Monitor your requests and usage in the [Portkey Dashboard](https://app.portkey.ai/dashboard). + +# 3. Set Up Enterprise Governance for Cline + +**Why Enterprise Governance?** +When deploying Cline across development teams in your organization, you need to consider several governance aspects: +- **Cost Management**: Controlling and tracking AI spending across developers +- **Access Control**: Managing which teams can use specific models +- **Usage Analytics**: Understanding how AI is being used in development workflows +- **Security & Compliance**: Protecting sensitive code and maintaining security standards +- **Reliability**: Ensuring consistent service across all developers + +Portkey adds a comprehensive governance layer to address these enterprise needs. Let's implement these controls step by step. + +**Enterprise Implementation Guide** + + + +### Step 1: Implement Budget Controls & Rate Limits + +Virtual Keys enable granular control over LLM access at the team/developer level. This helps you: +- Set up [budget limits](/product/ai-gateway/virtual-keys/budget-limits) per developer +- Prevent unexpected usage spikes using Rate limits +- Track individual developer spending + +#### Setting Up Developer-Specific Controls: +1. Navigate to [Virtual Keys](https://app.portkey.ai/virtual-keys) in Portkey dashboard +2. Create new Virtual Key for each developer or team with budget limits +3. Configure appropriate limits based on seniority or project needs + + + + + + + + +### Step 2: Define Model Access Rules + +As your development team scales, controlling which developers can access specific models becomes crucial. Portkey Configs provide this control layer with features like: + +#### Access Control Features: +- **Model Restrictions**: Limit access to expensive models +- **Code Protection**: Implement guardrails for sensitive code +- **Reliability Controls**: Add fallbacks and retry logic + +#### Example Configuration for Development Teams: +```json +{ + "strategy": { + "mode": "fallback" + }, + "targets": [ + { + "virtual_key": "YOUR_OPENAI_VIRTUAL_KEY", + "override_params": { + "model": "gpt-4o" + } + }, + { + "virtual_key": "YOUR_ANTHROPIC_VIRTUAL_KEY", + "override_params": { + "model": "claude-3-sonnet-20240229" + } + } + ] +} +``` + +Create your config on the [Configs page](https://app.portkey.ai/configs) in your Portkey dashboard. + + +Configs can be updated anytime to adjust controls without developers needing to update their Cline settings. + + + + +### Step 3: Implement Developer Access Controls + +Create Developer-specific API keys that automatically: +- Track usage per developer with virtual keys +- Apply appropriate configs to route requests +- Collect metadata about coding sessions +- Enforce access permissions + +Create API keys through: +- [Portkey App](https://app.portkey.ai/) +- [API Key Management API](/api-reference/admin-api/control-plane/api-keys/create-api-key) + +Example using Python SDK: +```python +from portkey_ai import Portkey + +portkey = Portkey(api_key="YOUR_ADMIN_API_KEY") + +api_key = portkey.api_keys.create( + name="frontend-team-developer", + type="organisation", + workspace_id="YOUR_WORKSPACE_ID", + defaults={ + "config_id": "your-config-id", + "metadata": { + "environment": "development", + "team": "frontend", + "developer": "john.doe" + } + }, + scopes=["logs.view", "configs.read"] +) +``` + +For detailed key management instructions, see our [API Keys documentation](/api-reference/admin-api/control-plane/api-keys/create-api-key). + + + +### Step 4: Deploy & Monitor +After distributing API keys to your developers, your enterprise-ready Cline setup is ready. Each developer can now use their designated API keys with appropriate access levels and budget controls. + +Monitor usage in Portkey dashboard: +- Cost tracking by developer +- Model usage patterns +- Code generation metrics +- Error rates and performance + + + + + +### Enterprise Features Now Available +**Cline now has:** + +- Per-developer budget controls +- Model access governance +- Usage tracking & attribution +- Code security guardrails +- Reliability features for development workflows + + + +# Portkey Features +Now that you have enterprise-grade Cline setup, let's explore the comprehensive features Portkey provides to ensure secure, efficient, and cost-effective AI-assisted development. + +### 1. Comprehensive Metrics +Using Portkey you can track 40+ key metrics including cost, token usage, response time, and performance across all your LLM providers in real time. Filter these metrics by developer, team, or project using custom metadata. + + + + + +### 2. Advanced Logs +Portkey's logging dashboard provides detailed logs for every request made by Cline. These logs include: +- Complete request and response tracking +- Code context and generation metrics +- Developer attribution +- Cost breakdown per coding session + + + + + +### 3. Unified Access to 250+ LLMs + +Easily switch between 250+ LLMs for different coding tasks. Use GPT-4 for complex architecture decisions, Claude for detailed code reviews, or specialized models for specific languages - all through a single interface. + +### 4. Advanced Metadata Tracking +Track coding patterns and productivity metrics with custom metadata: +- Language and framework usage +- Code generation vs completion tasks +- Time-of-day productivity patterns +- Project-specific metrics + + + + +### 5. Enterprise Access Management + + + +Set and manage spending limits per developer or team. Prevent budget overruns with automatic cutoffs. + + + +Enterprise-grade SSO integration for seamless developer onboarding and offboarding. + + + +Hierarchical structure with teams, projects, and role-based access control for development organizations. + + + +Comprehensive audit logging for security compliance and code generation tracking. + + + +### 6. Reliability Features + + + Automatically switch between models if one fails, ensuring uninterrupted coding. + + + Route requests based on code complexity or language requirements. + + + Distribute requests across multiple API keys or providers. + + + Cache common code patterns to reduce costs and improve response times. + + + Automatic retry handling for failed requests with exponential backoff. + + + Enforce spending limits to control development costs. + + + +### 7. Advanced Guardrails + +Protect your codebase and enhance security with real-time checks on AI interactions: +- Prevent exposure of API keys and secrets +- Block generation of malicious code patterns +- Enforce coding standards and best practices +- Custom security rules for your organization +- License compliance checks + + +Implement real-time protection for your development environment with automatic detection and filtering of sensitive code, credentials, and security vulnerabilities. + + +# FAQs + + + +Portkey provides several ways to track developer costs: +- Create separate Virtual Keys for each developer +- Use metadata tags to identify developers +- Set up developer-specific API keys +- View detailed analytics in the dashboard + + + +When a developer reaches their budget limit: +1. Further requests will be blocked +2. The developer and admin receive notifications +3. Coding history remains available +4. Admins can adjust limits as needed + + + +Yes! Portkey supports local models through Ollama and other self-hosted solutions. Configure your local endpoint as a custom provider in Portkey and use it with Cline just like any other provider. + + + +Portkey provides multiple security layers: +- Guardrails to prevent sensitive data exposure +- Request/response filtering +- Audit logs for all interactions +- Custom security rules +- PII detection and masking + + + +# Next Steps + +**Join our Community** +- [Discord Community](https://portkey.sh/discord-report) +- [GitHub Repository](https://github.com/Portkey-AI) + + + +Schedule a 1:1 call with our team to see how Portkey can transform your development workflow with Cline. Get personalized recommendations for your team's specific needs. + + + +For enterprise support and custom features for your development teams, contact our [enterprise team](https://calendly.com/portkey-ai). + diff --git a/virtual_key_old/integrations/libraries/codex.mdx b/virtual_key_old/integrations/libraries/codex.mdx new file mode 100644 index 00000000..788d089e --- /dev/null +++ b/virtual_key_old/integrations/libraries/codex.mdx @@ -0,0 +1,396 @@ +--- +title: 'OpenAI Codex CLI' +description: 'Add usage tracking, cost controls, and security guardrails to your Codex CLI deployment' +--- + +OpenAI Codex CLI is a lightweight coding agent that runs directly in your terminal, letting you interact with AI to analyze, modify, and execute code in your projects. While Codex delivers powerful code generation and execution capabilities, Portkey adds essential enterprise controls for production deployments: + +- **Unified AI Gateway** - Single interface for 250+ LLMs with API key management (beyond Codex's default model options) +- **Centralized AI observability**: Real-time usage tracking for 40+ key metrics and logs for every request +- **Governance** - Real-time spend tracking, set budget limits and RBAC in your Codex setup +- **Security Guardrails** - PII detection, content filtering, and compliance controls + +This guide will walk you through integrating Portkey with OpenAI Codex CLI and setting up essential enterprise features including usage tracking, access controls, and budget management. + + + If you are an enterprise looking to use Codex CLI in your organization, [check out this section](#3-set-up-enterprise-governance-for-codex). + + +# 1. Setting up Portkey +Portkey allows you to use 250+ LLMs with your Codex CLI setup, with minimal configuration required. Let's set up the core components in Portkey that you'll need for integration. + + + +Virtual Keys are Portkey's secure way to manage your LLM provider API keys. Think of them like disposable credit cards for your LLM API keys, providing essential controls like: +- Budget limits for API usage +- Rate limiting capabilities +- Secure API key storage + +To create a virtual key: +Go to [Virtual Keys](https://app.portkey.ai/virtual-keys) in the Portkey App. Save and copy the virtual key ID + + + + + + +Save your virtual key ID - you'll need it for the next step. + + + + + +Configs in Portkey are JSON objects that define how your requests are routed. They help with implementing features like advanced routing, fallbacks, and retries. + +We need to create a default config to route our requests to the virtual key created in Step 1. + +To create your config: +1. Go to [Configs](https://app.portkey.ai/configs) in Portkey dashboard +2. Create new config with: + ```json + { + "virtual_key": "YOUR_VIRTUAL_KEY_FROM_STEP1", + "override_params": { + "model": "gpt-4o" // Your preferred model name + } + } + ``` +3. Save and note the Config name for the next step + + + + + + +This basic config connects to your virtual key. You can add more advanced portkey features later. + + + + + +Now create Portkey API key access point and attach the config you created in Step 2: + +1. Go to [API Keys](https://app.portkey.ai/api-keys) in Portkey and Create new API key +2. Select your config from `Step 2` +3. Generate and save your API key + + + + + + +Save your API key securely - you'll need it for Codex CLI integration. + + + + +# 2. Integrate Portkey with OpenAI Codex CLI + +Now that you have your Portkey components set up, let's connect them to OpenAI Codex CLI. The integration leverages Codex's provider configuration options to route all requests through Portkey's AI Gateway. + + +You need your Portkey API Key from [Step 1](#1-setting-up-portkey) before going further. + + +## Using Portkey Configuration File + +OpenAI Codex CLI supports a configuration file where you can specify your AI provider settings. Let's set up this configuration to use Portkey: + +1. Create or edit the Codex configuration file at `~/.codex/config.json`: + +```json +{ + "provider": "portkey", + "model": "gpt-4o", + "providers": { + "portkey": { + "name": "Portkey", + "baseURL": "https://api.portkey.ai/v1", + "envKey": "PORTKEY_API_KEY" + } + } +} +``` + +2. Set your Portkey API key as an environment variable: + +```shell +export PORTKEY_API_KEY="your-portkey-api-key-here" +``` + +> **Note:** This command sets the key only for your current terminal session. You can add the `export` line to your shell's configuration file (e.g., `~/.zshrc`) for persistence, or place it in a `.env` file at the root of your project. + +3. Test your integration: + +```shell +codex "explain this repository to me" +``` + + +# 3. Set Up Enterprise Governance for Codex + +**Why Enterprise Governance?** +If you are using Codex CLI inside your organization, you need to consider several governance aspects: +- **Cost Management**: Controlling and tracking AI spending across teams +- **Access Control**: Managing which teams can use specific models +- **Usage Analytics**: Understanding how AI is being used across the organization +- **Security & Compliance**: Maintaining enterprise security standards +- **Reliability**: Ensuring consistent service across all users + +Portkey adds a comprehensive governance layer to address these enterprise needs. Let's implement these controls step by step. + +**Enterprise Implementation Guide** + + + +### Step 1: Implement Budget Controls & Rate Limits + +Virtual Keys enable granular control over LLM access at the team/department level. This helps you: +- Set up [budget limits](/product/ai-gateway/virtual-keys/budget-limits) +- Prevent unexpected usage spikes using Rate limits +- Track departmental spending + +#### Setting Up Department-Specific Controls: +1. Navigate to [Virtual Keys](https://app.portkey.ai/virtual-keys) in Portkey dashboard +2. Create new Virtual Key for each department with budget limits and rate limits +3. Configure department-specific limits + + + + + + + + +### Step 2: Define Model Access Rules + +As your AI usage scales, controlling which teams can access specific models becomes crucial. Portkey Configs provide this control layer with features like: + +#### Access Control Features: +- **Model Restrictions**: Limit access to specific models +- **Data Protection**: Implement guardrails for sensitive data +- **Reliability Controls**: Add fallbacks and retry logic + +#### Example Configuration: +Here's a basic configuration to route requests to OpenAI, specifically using GPT-4o: + +```json +{ + "strategy": { + "mode": "single" + }, + "targets": [ + { + "virtual_key": "YOUR_OPENAI_VIRTUAL_KEY", + "override_params": { + "model": "gpt-4o" + } + } + ] +} +``` + +Create your config on the [Configs page](https://app.portkey.ai/configs) in your Portkey dashboard. You'll need the config ID for connecting to Codex CLI's setup. + + + +Configs can be updated anytime to adjust controls without affecting running applications. + + + + +### Step 3: Implement Access Controls + +Create User-specific API keys that automatically: +- Track usage per user/team with the help of virtual keys +- Apply appropriate configs to route requests +- Collect relevant metadata to filter logs +- Enforce access permissions + + +Create API keys through: +- [Portkey App](https://app.portkey.ai/) +- [API Key Management API](/api-reference/admin-api/control-plane/api-keys/create-api-key) + + +Example using Python SDK: +```python +from portkey_ai import Portkey + +portkey = Portkey(api_key="YOUR_ADMIN_API_KEY") + +api_key = portkey.api_keys.create( + name="engineering-team", + type="organisation", + workspace_id="YOUR_WORKSPACE_ID", + defaults={ + "config_id": "your-config-id", + "metadata": { + "environment": "production", + "department": "engineering" + } + }, + scopes=["logs.view", "configs.read"] +) +``` + +For detailed key management instructions, see our [API Keys documentation](/api-reference/admin-api/control-plane/api-keys/create-api-key). + + + +### Step 4: Deploy & Monitor +After distributing API keys to your team members, your enterprise-ready Codex CLI setup is ready to go. Each team member can now use their designated API keys with appropriate access levels and budget controls. +Apply your governance setup using the integration steps from earlier sections +Monitor usage in Portkey dashboard: +- Cost tracking by department +- Model usage patterns +- Request volumes +- Error rates + + + + + +### Enterprise Features Now Available +**Codex CLI now has:** + +- Departmental budget controls +- Model access governance +- Usage tracking & attribution +- Security guardrails +- Reliability features + + + +# Portkey Features +Now that you have enterprise-grade Codex CLI setup, let's explore the comprehensive features Portkey provides to ensure secure, efficient, and cost-effective AI operations. + +### 1. Comprehensive Metrics +Using Portkey you can track 40+ key metrics including cost, token usage, response time, and performance across all your LLM providers in real time. You can also filter these metrics based on custom metadata that you can set in your configs. Learn more about metadata here. + + + + + +### 2. Advanced Logs +Portkey's logging dashboard provides detailed logs for every request made to your LLMs. These logs include: +- Complete request and response tracking +- Metadata tags for filtering +- Cost attribution and much more... + + + + + + + + +### 3. Unified Access to 250+ LLMs + +You can easily switch between 250+ LLMs. Call various LLMs such as Anthropic, Gemini, Mistral, Azure OpenAI, Google Vertex AI, AWS Bedrock, and many more by simply changing the `virtual_key` in your default `config` object. + + +### 4. Advanced Metadata Tracking +Using Portkey, you can add custom metadata to your LLM requests for detailed tracking and analytics. Use metadata tags to filter logs, track usage, and attribute costs across departments and teams. + + + + + + +### 5. Enterprise Access Management + + + +Set and manage spending limits across teams and departments. Control costs with granular budget limits and usage tracking. + + + +Enterprise-grade SSO integration with support for SAML 2.0, Okta, Azure AD, and custom providers for secure authentication. + + + +Hierarchical organization structure with workspaces, teams, and role-based access control for enterprise-scale deployments. + + + +Comprehensive access control rules and detailed audit logging for security compliance and usage tracking. + + + + +### 6. Reliability Features + + + Automatically switch to backup targets if the primary target fails. + + + Route requests to different targets based on specified conditions. + + + Distribute requests across multiple targets based on defined weights. + + + Enable caching of responses to improve performance and reduce costs. + + +Automatic retry handling with exponential backoff for failed requests + + + Set and manage budget limits across teams and departments. Control costs with granular budget limits and usage tracking. + + + + + +### 7. Advanced Guardrails + +Protect your Codex CLI's data and enhance reliability with real-time checks on LLM inputs and outputs. Leverage guardrails to: +- Prevent sensitive data leaks +- Enforce compliance with organizational policies +- PII detection and masking +- Content filtering +- Custom security rules +- Data compliance checks + + +Implement real-time protection for your LLM interactions with automatic detection and filtering of sensitive content, PII, and custom security rules. Enable comprehensive data protection while maintaining compliance with organizational policies. + + +# FAQs + + + You can update your Virtual Key limits at any time from the Portkey dashboard:1. Go to Virtual Keys section2. Click on the Virtual Key you want to modify3. Update the budget or rate limits4. Save your changes + + + Yes! You can create multiple Virtual Keys (one for each provider) and attach them to a single config. This config can then be connected to your API key, allowing you to use multiple providers through a single API key. + + +Portkey provides several ways to track team costs: +- Create separate Virtual Keys for each team +- Use metadata tags in your configs +- Set up team-specific API keys +- Monitor usage in the analytics dashboard + + +When a team reaches their budget limit: +1. Further requests will be blocked +2. Team admins receive notifications +3. Usage statistics remain available in dashboard +4. Limits can be adjusted if needed + + +Yes! Portkey fully integrates with [OpenAI's open source Codex CLI](https://github.com/openai/codex). This gives you the flexibility to leverage both the power of open source with the enterprise controls of Portkey. + + + + +# Next Steps + + **Join our Community** +- [Discord Community](https://portkey.sh/discord-report) +- [GitHub Repository](https://github.com/Portkey-AI) + + +For enterprise support and custom features, contact our [enterprise team](https://calendly.com/portkey-ai). + diff --git a/virtual_key_old/integrations/libraries/dspy.mdx b/virtual_key_old/integrations/libraries/dspy.mdx new file mode 100644 index 00000000..2e4e4f8d --- /dev/null +++ b/virtual_key_old/integrations/libraries/dspy.mdx @@ -0,0 +1,241 @@ +--- +title: 'DSPy' +description: 'Integrate DSPy with Portkey for production-ready LLM pipelines' +--- + +DSPy is a framework for algorithmically optimizing language model prompts and weights. + +Portkey's integration with DSPy makes your DSPy pipelines production-ready with detailed insights on costs & performance metrics for each run, and also makes your existing DSPy code work across 250+ LLMs. + +## Getting Started + +### Installation + +```sh +pip install dspy-ai==2.4.14 # Use Version 2.4.14 or higher +pip install portkey-ai +``` + +### Setting up + +Portkey extends the existing `OpenAI` client in DSPy and makes it work with 250+ LLMs and gives you detailed cost insights. Just change `api_base` and add Portkey related headers in the `default_headers` param. + + +Grab your Portkey API key from [here](https://app.portkey.ai/). + + +```python +import os +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders +import dspy + +# Set up your Portkey client +turbo = dspy.OpenAI( + api_base=PORTKEY_GATEWAY_URL + "/", + model='gpt-4o', + max_tokens=250, + api_key="YOUR_OPENAI_API_KEY", # Enter Your OpenAI key + model_type="chat", + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", # Enter Your Portkey API Key + metadata={'_user': "dspy"}, + provider="openai" + ) +) + +# Configure DSPy to use the Portkey-enabled client +dspy.settings.configure(lm=turbo) +``` + +๐ŸŽ‰ Voila! that's all you need to do integrate Portkey with DSPy. Let's try making our first request. + +## Let's make your first Request + +Here's a simple Google Colab notebook that demonstrates DSPy with Portkey integration + + + Open In Colab + + +```python +import dspy + +# Set up the Portkey-enabled client (as shown in the Getting Started section) + +class QA(dspy.Signature): + """Given the question, generate the answer""" + question = dspy.InputField(desc="User's question") + answer = dspy.OutputField(desc="often between 1 and 3 words") + +dspy.settings.configure(lm=turbo) + +predict = dspy.Predict(QA) + +# Make a prediction +prediction = predict(question="Who won the Golden Boot in the 2022 FIFA World Cup?") +print(prediction.answer) +``` + +When you make a request using Portkey with DSPy, you can view detailed information about the request in the Portkey dashboard. Here's what you'll see: + + + Portkey Dashboard + + +* `Request Details`: Information about the specific request, including the model used, input, and output. +* `Metrics`: Performance metrics such as latency, token usage, and cost. +* `Logs`: Detailed logs of the request, including any errors or warnings. +* `Traces`: A visual representation of the request flow, especially useful for complex DSPy modules. + +## Portkey Features with DSPy + +### 1. Interoperability + +Portkey's Unified API enables you to easily switch between **250**+ language models. This includes the LLMs that are not natively integrated with DSPy. Here's how you can modify your DSPy setup to use Claude from Gpt-4 model: + + + +```python OpenAI to Anthropic +# OpenAI setup +turbo = dspy.OpenAI( + api_base=PORTKEY_GATEWAY_URL + "/", + model='gpt-4o', + api_key="YOUR_OPENAI_API_KEY", # Enter your OpenAI API key + model_type="chat", + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + metadata={'_user': "dspy"}, + provider="openai" + ) +) +dspy.settings.configure(lm=turbo) + +# Anthropic setup +turbo = dspy.OpenAI( + api_base=PORTKEY_GATEWAY_URL + "/", + model='claude-3-opus-20240229', # Change the model name from Gpt-4 to claude + api_key="YOUR_Anthropic_API_KEY", # Enter your Anthropic API key + model_type="chat", + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + metadata={'_user': "dspy"}, # Enter any key-value pair for filtering logs + trace_id="test_dspy_trace", + provider="anthropic" # Change your provider, you can find the provider slug in Portkey's docs + ) +) +dspy.settings.configure(lm=turbo) +``` + + + +### 2. Logs and Traces + +Portkey provides detailed tracing for each request. This is especially useful for complex DSPy modules with multiple LLM calls. You can view these traces in the Portkey dashboard to understand the flow of your DSPy application. + + + DSPy Traces + + +### 3. Metrics + +Portkey's Observability suite helps you track key metrics like **cost** and **token** usage, which is crucial for managing the high cost of DSPy. The observability dashboard helps you track 40+ key metrics, giving you detailed insights into your DSPy run. + + + Portkey Metrics Dashboard + + +### 4. Caching + +Caching can significantly reduce these costs by storing frequently used data and responses. While DSPy has built-in simple caching, Portkey also offers advanced semantic caching to help you save more time and money. + +Just modify your Portkey config as shown below and pass it with the `config` key in the `default_headers` param: + +```python +config={ "cache": { "mode": "semantic" } } + +turbo = dspy.OpenAI( + api_base=PORTKEY_GATEWAY_URL + "/", + model='gpt-4o', + api_key="YOUR_OPENAI_API_KEY", # Enter your OpenAI API key + model_type="chat", + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + metadata={'_user': "dspy"}, + provider="openai", + config=config + ) +) +dspy.settings.configure(lm=turbo) +``` + +### 5. Reliability + +Portkey offers built-in **fallbacks** between different LLMs or providers, **load-balancing** across multiple instances or API keys, and implementing automatic **retries** and request **timeouts**. This makes your DSPy more reliable and resilient. + +Similar to caching example above, just define your Config and pass it with the `Config` key in the `default_headers` param. + +```json +{ + "retry": { + "attempts": 5 + }, + "strategy": { + "mode": "loadbalance" // Choose between "loadbalance" or "fallback" + }, + "targets": [ + { + "provider": "openai", + "api_key": "OpenAI_API_Key" + }, + { + "provider": "anthropic", + "api_key": "Anthropic_API_Key" + } + ] +} +``` + +### 6. Virtual Keys + +Securely store your LLM API keys in Portkey vault and get a disposable virtual key with custom budget limits. + +Add your API key in Portkey UI [here](https://app.portkey.ai/) to get a virtual key, and pass it in your request like this: + +```python +turbo = dspy.OpenAI( + api_base=PORTKEY_GATEWAY_URL + "/", + model='gpt-4o', + api_key="xx", + model_type="chat", + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="MY_OPENAI_VIRTUAL_KEY" + ) +) +dspy.settings.configure(lm=turbo) +``` + +## Advanced Examples + +### Retrieval-Augmented Generation (RAG) system + +Make your RAG prompts better with Portkey x DSPy + + + Open In Colab + + +## Troubleshoot - Missing LLM Calls in Traces + + +DSPy uses caching for LLM calls by default, which means repeated identical requests won't generate new API calls or new traces in Langtrace. To ensure you capture every LLM call, follow these steps: + +1. **Disable Caching**: For full tracing during debugging, turn off DSPy's caching. Check the DSPy documentation for detailed instructions on how to disable caching. +2. **Use Unique Inputs**: To test effectively, make sure each run uses different inputs to avoid triggering the cache. +3. **Clear the Cache**: If you need to test the same inputs again, clear DSPy's cache between runs to ensure fresh API requests. +4. **Verify Configuration**: Confirm that your DSPy setup is correctly configured to use the intended LLM provider. + +If you still face issues after following these steps, please reach out to our support team for additional help. + +Remember to manage caching wisely in production to strike the right balance between thorough tracing and performance efficiency. + diff --git a/virtual_key_old/integrations/libraries/goose.mdx b/virtual_key_old/integrations/libraries/goose.mdx new file mode 100644 index 00000000..66134c3c --- /dev/null +++ b/virtual_key_old/integrations/libraries/goose.mdx @@ -0,0 +1,395 @@ +--- +title: 'Goose' +description: 'Add usage tracking, cost controls, and security guardrails to your Goose AI agent' +--- + +Goose is a local, extensible, open source AI agent that automates engineering tasks. While Goose delivers powerful autonomous coding capabilities, Portkey adds essential enterprise controls for production deployments: + +- **Unified AI Gateway** - Single interface for 1600+ LLMs with API key management. (not just OpenAI & Anthropic) +- **Centralized AI observability**: Real-time usage tracking for 40+ key metrics and logs for every request +- **Governance** - Real-time spend tracking, set budget limits and RBAC in your Goose setup +- **Security Guardrails** - PII detection, content filtering, and compliance controls + +This guide will walk you through integrating Portkey with Goose and setting up essential enterprise features including usage tracking, access controls, and budget management. + + + If you are an enterprise looking to use Goose in your organisation, [check out this section](#3-set-up-enterprise-governance-for-goose). + + +# 1. Setting up Portkey +Portkey allows you to use 1600+ LLMs with your Goose setup, with minimal configuration required. Let's set up the core components in Portkey that you'll need for integration. + + + +Virtual Keys are Portkey's secure way to manage your LLM provider API keys. Think of them like disposable credit cards for your LLM API keys, providing essential controls like: +- Budget limits for API usage +- Rate limiting capabilities +- Secure API key storage + +To create a virtual key: +Go to [Virtual Keys](https://app.portkey.ai/virtual-keys) in the Portkey App. Save and copy the virtual key ID + + + + + + +Save your virtual key ID - you'll need it for the next step. + + + + + +Configs in Portkey are JSON objects that define how your requests are routed. They help with implementing features like advanced routing, fallbacks, and retries. + +We need to create a default config to route our requests to the virtual key created in Step 1. + +To create your config: +1. Go to [Configs](https://app.portkey.ai/configs) in Portkey dashboard +2. Create new config with: + ```json + { + "virtual_key": "YOUR_VIRTUAL_KEY_FROM_STEP1", + "override_params": { + "model": "gpt-4o" // Your preferred model name + } + } + + ``` +3. Save and note the Config name for the next step + + + + + + +This basic config connects to your virtual key. You can add more advanced portkey features later. + + + + + +Now create Portkey API key access point and attach the config you created in Step 2: + +1. Go to [API Keys](https://app.portkey.ai/api-keys) in Portkey and Create new API key +2. Select your config from `Step 2` +3. Generate and save your API key + + + + + + +Save your API key securely - you'll need it for Goose integration. + + + + +# 2. Integrate Portkey with Goose + +Now that you have your Portkey components set up, let's connect them to Goose. Since Portkey provides OpenAI API compatibility, integration is straightforward and requires just a few configuration steps in your Goose setup. + + +You need your Portkey API Key from [Step 1](#Getting-started-with-portkey) before going further. + + + + +Open Goose either through: +- The desktop application +- The CLI by running `goose` in your terminal + + + +1. Navigate to Goose's LLM provider settings +2. Select **OpenAI** as your provider + + + +In the OpenAI provider settings, configure: +- **Base URL**: `https://api.portkey.ai/v1` +- **API Key**: Your Portkey API key from the setup + +That's it! Goose will now route all LLM requests through Portkey. + + + +You can monitor your requests and usage in the [Portkey Dashboard](https://app.portkey.ai/dashboard). + + +Make sure your virtual key has sufficient budget and rate limits for your expected usage. Also use the complete model name given by the provider. + + + + +# 3. Set Up Enterprise Governance for Goose + +**Why Enterprise Governance?** +If you are using Goose inside your orgnaization, you need to consider several governance aspects: +- **Cost Management**: Controlling and tracking AI spending across engineering teams +- **Access Control**: Managing which teams can use specific models +- **Usage Analytics**: Understanding how AI agents are being used across the organization +- **Security & Compliance**: Maintaining enterprise security standards +- **Reliability**: Ensuring consistent service across all developers + +Portkey adds a comprehensive governance layer to address these enterprise needs. Let's implement these controls step by step. + +**Enterprise Implementation Guide** + + + +### Step 1: Implement Budget Controls & Rate Limits + +Virtual Keys enable granular control over LLM access at the team/department level. This helps you: +- Set up [budget limits](/product/ai-gateway/virtual-keys/budget-limits) +- Prevent unexpected usage spikes using Rate limits +- Track departmental spending + +#### Setting Up Department-Specific Controls: +1. Navigate to [Virtual Keys](https://app.portkey.ai/virtual-keys) in Portkey dashboard +2. Create new Virtual Key for each engineering team with budget limits and rate limits +3. Configure department-specific limits + + + + + + + + +### Step 2: Define Model Access Rules + +As your AI usage scales, controlling which teams can access specific models becomes crucial. Portkey Configs provide this control layer with features like: + +#### Access Control Features: +- **Model Restrictions**: Limit access to specific models +- **Data Protection**: Implement guardrails for sensitive code and data +- **Reliability Controls**: Add fallbacks and retry logic + +#### Example Configuration: +Here's a basic configuration to route requests to OpenAI, specifically using GPT-4o: + +```json +{ + "strategy": { + "mode": "single" + }, + "targets": [ + { + "virtual_key": "YOUR_OPENAI_VIRTUAL_KEY", + "override_params": { + "model": "gpt-4o" + } + } + ] +} +``` + +Create your config on the [Configs page](https://app.portkey.ai/configs) in your Portkey dashboard. You'll need the config ID for connecting to Goose's setup. + + + +Configs can be updated anytime to adjust controls without affecting running applications. + + + + +### Step 3: Implement Access Controls + +Create User-specific API keys that automatically: +- Track usage per developer/team with the help of virtual keys +- Apply appropriate configs to route requests +- Collect relevant metadata to filter logs +- Enforce access permissions + + +Create API keys through: +- [Portkey App](https://app.portkey.ai/) +- [API Key Management API](/api-reference/admin-api/control-plane/api-keys/create-api-key) + + +Example using Python SDK: +```python +from portkey_ai import Portkey + +portkey = Portkey(api_key="YOUR_ADMIN_API_KEY") + +api_key = portkey.api_keys.create( + name="frontend-engineering", + type="organisation", + workspace_id="YOUR_WORKSPACE_ID", + defaults={ + "config_id": "your-config-id", + "metadata": { + "environment": "development", + "department": "engineering", + "team": "frontend" + } + }, + scopes=["logs.view", "configs.read"] +) +``` + +For detailed key management instructions, see our [API Keys documentation](/api-reference/admin-api/control-plane/api-keys/create-api-key). + + + +### Step 4: Deploy & Monitor +After distributing API keys to your engineering teams, your enterprise-ready Goose setup is ready to go. Each developer can now use their designated API keys with appropriate access levels and budget controls. +Apply your governance setup using the integration steps from earlier sections +Monitor usage in Portkey dashboard: +- Cost tracking by engineering team +- Model usage patterns for AI agent tasks +- Request volumes +- Error rates and debugging logs + + + + + +### Enterprise Features Now Available +**Goose now has:** + +- Team-level budget controls +- Model access governance +- Usage tracking & attribution +- Security guardrails for code generation +- Reliability features for uninterrupted development + + + +# Portkey Features +Now that you have enterprise-grade Goose setup, let's explore the comprehensive features Portkey provides to ensure secure, efficient, and cost-effective AI agent operations. + +### 1. Comprehensive Metrics +Using Portkey you can track 40+ key metrics including cost, token usage, response time, and performance across all your LLM providers in real time. You can also filter these metrics based on custom metadata that you can set in your configs. Learn more about metadata here. + + + + + +### 2. Advanced Logs +Portkey's logging dashboard provides detailed logs for every request made by Goose. These logs include: +- Complete request and response tracking for debugging +- Metadata tags for filtering by team or project +- Cost attribution per task +- Complete conversation history with the AI agent + + + + + + + + +### 3. Unified Access to 1600+ LLMs + +You can easily switch between 1600+ LLMs. Call various LLMs such as Anthropic, Gemini, Mistral, Azure OpenAI, Google Vertex AI, AWS Bedrock, and many more by simply changing the `virtual key` in your default `config` object. + + +### 4. Advanced Metadata Tracking +Using Portkey, you can add custom metadata to your LLM requests for detailed tracking and analytics. Use metadata tags to filter logs, track usage, and attribute costs across engineering teams and projects. + + + + + + +### 5. Enterprise Access Management + + + +Set and manage spending limits across teams and departments. Control costs with granular budget limits and usage tracking. + + + +Enterprise-grade SSO integration with support for SAML 2.0, Okta, Azure AD, and custom providers for secure authentication. + + + +Hierarchical organization structure with workspaces, teams, and role-based access control for enterprise-scale deployments. + + + +Comprehensive access control rules and detailed audit logging for security compliance and usage tracking. + + + + +### 6. Reliability Features + + + Automatically switch to backup targets if the primary target fails. + + + Route requests to different targets based on specified conditions. + + + Distribute requests across multiple targets based on defined weights. + + + Enable caching of responses to improve performance and reduce costs. + + +Automatic retry handling with exponential backoff for failed requests + + + Set and manage budget limits across teams and departments. Control costs with granular budget limits and usage tracking. + + + + + +### 7. Advanced Guardrails + +Protect your codebase and enhance reliability with real-time checks on LLM inputs and outputs. Leverage guardrails to: +- Prevent sensitive code or API key leaks +- Enforce compliance with coding standards +- PII detection and masking in generated code +- Content filtering for inappropriate code generation +- Custom security rules for your organization +- Compliance checks for internal coding policies + + +Implement real-time protection for your AI agent interactions with automatic detection and filtering of sensitive content, PII, and custom security rules. Enable comprehensive data protection while maintaining compliance with organizational policies. + + + +# FAQs + + + You can update your Virtual Key limits at any time from the Portkey dashboard: + 1. Go to Virtual Keys section + 2. Click on the Virtual Key you want to modify + 3. Update the budget or rate limits + 4. Save your changes + + + Yes! You can create multiple Virtual Keys (one for each provider) and attach them to a single config. This config can then be connected to your API key, allowing you to use multiple providers through a single API key. This enables Goose to leverage different models for different tasks. + + +Portkey provides several ways to track team costs: +- Create separate Virtual Keys for each team +- Use metadata tags in your configs +- Set up team-specific API keys +- Monitor usage in the analytics dashboard + + +When a team reaches their budget limit: +1. Further requests will be blocked +2. Team admins receive notifications +3. Usage statistics remain available in dashboard +4. Limits can be adjusted if needed + + + + +# Next Steps + + **Join our Community** +- [Discord Community](https://portkey.sh/discord-report) +- [GitHub Repository](https://github.com/Portkey-AI) + + +For enterprise support and custom features, contact our [enterprise team](https://calendly.com/portkey-ai). + diff --git a/virtual_key_old/integrations/libraries/instructor.mdx b/virtual_key_old/integrations/libraries/instructor.mdx new file mode 100644 index 00000000..75cfea6e --- /dev/null +++ b/virtual_key_old/integrations/libraries/instructor.mdx @@ -0,0 +1,190 @@ +--- +title: "Instructor" +description: "With Portkey, you can confidently take your Instructor pipelines to production and get complete observability over all of your calls + make them reliable - all with a 2 LOC change!" +--- + +**Instructor** is a framework for extracting structured outputs from LLMs, available in [Python](https://python.useinstructor.com/) & [JS](https://instructor-ai.github.io/instructor-js/). + +## Integrating Portkey with Instructor + + + +```python +import instructor +from pydantic import BaseModel +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +portkey = OpenAI( + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + virtual_key="OPENAI_VIRTUAL_KEY", + api_key="PORTKEY_API_KEY" + ) +) + +class User(BaseModel): + name: str + age: int + +client = instructor.from_openai(portkey) + +user_info = client.chat.completions.create( + model="gpt-4-turbo", + max_tokens=1024, + response_model=User, + messages=[{"role": "user", "content": "John Doe is 30 years old."}], +) + +print(user_info.name) +print(user_info.age) +``` + + + + +```python +import Instructor from "@instructor-ai/instructor"; +import OpenAI from "openai"; +import { z } from "zod"; +import { PORTKEY_GATEWAY_URL, createHeaders } from "portkey-ai"; + +const portkey = new OpenAI({ + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "OPENAI_API_KEY", + }), +}); + +const client = Instructor({ + client: portkey, + mode: "TOOLS", +}); + +const UserSchema = z.object({ + age: z.number().describe("The age of the user"), + name: z.string(), +}); + +const user = await client.chat.completions.create({ + messages: [{ role: "user", content: "Jason Liu is 30 years old" }], + model: "claude-3-sonnet-20240229", + // model: "gpt-4", + max_tokens: 512, + response_model: { + schema: UserSchema, + name: "User", + }, +}); + +console.log(user); +``` + + + +## Caching Your Requests + +Let's now bring down the cost of running your Instructor pipeline with Portkey caching. You can just create a Config object where you define your cache setting: + +```py +{ + "cache": { + "mode": "simple" + } +} +``` + +You can write it raw, or use Portkey's Config builder and get a corresponding `config id`. Then, just pass it while instantiating your OpenAI client: + + + + +```python +import instructor +from pydantic import BaseModel +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +cache_config = { + "cache": { + "mode": "simple" + } +} + +portkey = OpenAI( + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + virtual_key="OPENAI_VIRTUAL_KEY", + api_key="PORTKEY_API_KEY", + config=cache_config # Or pass your Config ID saved from Portkey app + ) +) + +class User(BaseModel): + name: str + age: int + +client = instructor.from_openai(portkey) + +user_info = client.chat.completions.create( + model="gpt-4-turbo", + max_tokens=1024, + response_model=User, + messages=[{"role": "user", "content": "John Doe is 30 years old."}], +) + +print(user_info.name) +print(user_info.age) +``` + + + +```python +import Instructor from "@instructor-ai/instructor"; +import OpenAI from "openai"; +import { z } from "zod"; +import { PORTKEY_GATEWAY_URL, createHeaders } from "portkey-ai"; + +const cache_config = { + "cache": { + "mode": "simple" + } +} + +const portkey = new OpenAI({ + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "OPENAI_API_KEY", + config: cache_config // Or pass your Config ID saved from Portkey app + }), +}); + +const client = Instructor({ + client: portkey, + mode: "TOOLS", +}); + +const UserSchema = z.object({ + age: z.number().describe("The age of the user"), + name: z.string(), +}); + +const user = await client.chat.completions.create({ + messages: [{ role: "user", content: "Jason Liu is 30 years old" }], + model: "claude-3-sonnet-20240229", + // model: "gpt-4", + max_tokens: 512, + response_model: { + schema: UserSchema, + name: "User", + }, +}); + +console.log(user); +``` + + + +Similarly, you can add [Fallback](/product/ai-gateway/fallbacks), [Loadbalancing](/product/ai-gateway/load-balancing), [Timeout](/product/ai-gateway/request-timeouts), or [Retry](/product/ai-gateway/automatic-retries) settings to your Configs and make your Instructor requests robust & reliable. diff --git a/virtual_key_old/integrations/libraries/janhq.mdx b/virtual_key_old/integrations/libraries/janhq.mdx new file mode 100644 index 00000000..c69a8bc0 --- /dev/null +++ b/virtual_key_old/integrations/libraries/janhq.mdx @@ -0,0 +1,373 @@ +--- +title: 'Jan' +description: 'Add usage tracking, cost controls, and security guardrails to your Jan deployment' +--- +Jan is an open source alternative to ChatGPT that runs 100% offline on your computer. While Jan delivers robust chat capabilities, Portkey adds essential enterprise controls for production deployments: + +- **Unified AI Gateway** - Single interface for 1600+ LLMs with API key management. (not just OpenAI & Anthropic) +- **Centralized AI observability**: Real-time usage tracking for 40+ key metrics and logs for every request +- **Governance** - Real-time spend tracking, set budget limits and RBAC in your Jan setup +- **Security Guardrails** - PII detection, content filtering, and compliance controls + +This guide will walk you through integrating Portkey with Jan and setting up essential enterprise features including usage tracking, access controls, and budget management. + + + If you are an enterprise looking to use Jan in your organisation, [check out this section](#3-set-up-enterprise-governance-for-jan). + + +# 1. Setting up Portkey +Portkey allows you to use 1600+ LLMs with your Jan setup, with minimal configuration required. Let's set up the core components in Portkey that you'll need for integration. + + + +Virtual Keys are Portkey's secure way to manage your LLM provider API keys. Think of them like disposable credit cards for your LLM API keys, providing essential controls like: +- Budget limits for API usage +- Rate limiting capabilities +- Secure API key storage + +To create a virtual key: +Go to [Virtual Keys](https://app.portkey.ai/virtual-keys) in the Portkey App. Save and copy the virtual key ID + + + + + + +Save your virtual key ID - you'll need it for the next step. + + + + + +Configs in Portkey are JSON objects that define how your requests are routed. They help with implementing features like advanced routing, fallbacks, and retries. + +We need to create a default config to route our requests to the virtual key created in Step 1. + +To create your config: +1. Go to [Configs](https://app.portkey.ai/configs) in Portkey dashboard +2. Create new config with: + ```json + { + "virtual_key": "YOUR_VIRTUAL_KEY_FROM_STEP1", + "override_params": { + "model": "gpt-4o" // Your preferred model name + } + } + + ``` +3. Save and note the Config name for the next step + + + + + + +This basic config connects to your virtual key. You can add more advanced portkey features later. + + + + + +Now create Portkey API key access point and attach the config you created in Step 2: + +1. Go to [API Keys](https://app.portkey.ai/api-keys) in Portkey and Create new API key +2. Select your config from `Step 2` +3. Generate and save your API key + + + + + + +Save your API key securely - you'll need it for Jan integration. + + + + +# 2. Integrate Portkey with JanHQ + +Now that you have your Portkey components set up, let's connect them to JanHQ. Since Portkey provides OpenAI API compatibility, integration is straightforward and requires just a few configuration steps in your Jan interface. + + +You need your Portkey API Key from [Step 1](#Getting-started-with-portkey) before going further. + + +1. Launch your Jan application +2. Navigate to `Settings > Model Providers > OpenAI` +3. In the OpenAI settings, configure the following settings: + - Chat Completions Endpoint: `https://api.portkey.ai/v1/chat/completions` + - API Key: Your Portkey API key from the setup + +You can monitor your requests and usage in the [Portkey Dashboard](https://app.portkey.ai/dashboard). + + +Make sure your virtual key has sufficient budget and rate limits for your expected usage. Also use the complete model name given by the provider. + + + + +# 3. Set Up Enterprise Governance for Jan + +**Why Enterprise Governance?** +If you are using Jan inside your orgnaization, you need to consider several governance aspects: +- **Cost Management**: Controlling and tracking AI spending across teams +- **Access Control**: Managing which teams can use specific models +- **Usage Analytics**: Understanding how AI is being used across the organization +- **Security & Compliance**: Maintaining enterprise security standards +- **Reliability**: Ensuring consistent service across all users + +Portkey adds a comprehensive governance layer to address these enterprise needs. Let's implement these controls step by step. + +**Enterprise Implementation Guide** + + + +### Step 1: Implement Budget Controls & Rate Limits + +Virtual Keys enable granular control over LLM access at the team/department level. This helps you: +- Set up [budget limits](/product/ai-gateway/virtual-keys/budget-limits) +- Prevent unexpected usage spikes using Rate limits +- Track departmental spending + +#### Setting Up Department-Specific Controls: +1. Navigate to [Virtual Keys](https://app.portkey.ai/virtual-keys) in Portkey dashboard +2. Create new Virtual Key for each department with budget limits and rate limits +3. Configure department-specific limits + + + + + + + + +### Step 2: Define Model Access Rules + +As your AI usage scales, controlling which teams can access specific models becomes crucial. Portkey Configs provide this control layer with features like: + +#### Access Control Features: +- **Model Restrictions**: Limit access to specific models +- **Data Protection**: Implement guardrails for sensitive data +- **Reliability Controls**: Add fallbacks and retry logic + +#### Example Configuration: +Here's a basic configuration to route requests to OpenAI, specifically using GPT-4o: + +```json +{ + "strategy": { + "mode": "single" + }, + "targets": [ + { + "virtual_key": "YOUR_OPENAI_VIRTUAL_KEY", + "override_params": { + "model": "gpt-4o" + } + } + ] +} +``` + +Create your config on the [Configs page](https://app.portkey.ai/configs) in your Portkey dashboard. You'll need the config ID for connecting to Jan's setup. + + + +Configs can be updated anytime to adjust controls without affecting running applications. + + + + +### Step 3: Implement Access Controls + +Create User-specific API keys that automatically: +- Track usage per user/team with the help of virtual keys +- Apply appropriate configs to route requests +- Collect relevant metadata to filter logs +- Enforce access permissions + + +Create API keys through: +- [Portkey App](https://app.portkey.ai/) +- [API Key Management API](/api-reference/admin-api/control-plane/api-keys/create-api-key) + + +Example using Python SDK: +```python +from portkey_ai import Portkey + +portkey = Portkey(api_key="YOUR_ADMIN_API_KEY") + +api_key = portkey.api_keys.create( + name="engineering-team", + type="organisation", + workspace_id="YOUR_WORKSPACE_ID", + defaults={ + "config_id": "your-config-id", + "metadata": { + "environment": "production", + "department": "engineering" + } + }, + scopes=["logs.view", "configs.read"] +) +``` + +For detailed key management instructions, see our [API Keys documentation](/api-reference/admin-api/control-plane/api-keys/create-api-key). + + + +### Step 4: Deploy & Monitor +After distributing API keys to your team members, your enterprise-ready Jan setup is ready to go. Each team member can now use their designated API keys with appropriate access levels and budget controls. +Apply your governance setup using the integration steps from earlier sections +Monitor usage in Portkey dashboard: +- Cost tracking by department +- Model usage patterns +- Request volumes +- Error rates + + + + + +### Enterprise Features Now Available +**Jan now has:** + +- Departmental budget controls +- Model access governance +- Usage tracking & attribution +- Security guardrails +- Reliability features + + + +# Portkey Features +Now that you have enterprise-grade Jan setup, let's explore the comprehensive features Portkey provides to ensure secure, efficient, and cost-effective AI operations. + +### 1. Comprehensive Metrics +Using Portkey you can track 40+ key metrics including cost, token usage, response time, and performance across all your LLM providers in real time. You can also filter these metrics based on custom metadata that you can set in your configs. Learn more about metadata here. + + + + + +### 2. Advanced Logs +Portkey's logging dashboard provides detailed logs for every request made to your LLMs. These logs include: +- Complete request and response tracking +- Metadata tags for filtering +- Cost attribution and much more... + + + + + + + + +### 3. Unified Access to 1600+ LLMs + +You can easily switch between 1600+ LLMs. Call various LLMs such as Anthropic, Gemini, Mistral, Azure OpenAI, Google Vertex AI, AWS Bedrock, and many more by simply changing the `virtual key` in your default `config` object. + + +### 4. Advanced Metadata Tracking +Using Portkey, you can add custom metadata to your LLM requests for detailed tracking and analytics. Use metadata tags to filter logs, track usage, and attribute costs across departments and teams. + + + + + + +### 5. Enterprise Access Management + + + +Set and manage spending limits across teams and departments. Control costs with granular budget limits and usage tracking. + + + +Enterprise-grade SSO integration with support for SAML 2.0, Okta, Azure AD, and custom providers for secure authentication. + + + +Hierarchical organization structure with workspaces, teams, and role-based access control for enterprise-scale deployments. + + + +Comprehensive access control rules and detailed audit logging for security compliance and usage tracking. + + + + +### 6. Reliability Features + + + Automatically switch to backup targets if the primary target fails. + + + Route requests to different targets based on specified conditions. + + + Distribute requests across multiple targets based on defined weights. + + + Enable caching of responses to improve performance and reduce costs. + + +Automatic retry handling with exponential backoff for failed requests + + + Set and manage budget limits across teams and departments. Control costs with granular budget limits and usage tracking. + + + + + +### 7. Advanced Guardrails + +Protect your Project's data and enhance reliability with real-time checks on LLM inputs and outputs. Leverage guardrails to: +- Prevent sensitive data leaks +- Enforce compliance with organizational policies +- PII detection and masking +- Content filtering +- Custom security rules +- Data compliance checks + + +Implement real-time protection for your LLM interactions with automatic detection and filtering of sensitive content, PII, and custom security rules. Enable comprehensive data protection while maintaining compliance with organizational policies. + + + +# FAQs + + + You can update your Virtual Key limits at any time from the Portkey dashboard:1. Go to Virtual Keys section2. Click on the Virtual Key you want to modify3. Update the budget or rate limits4. Save your changes + + + Yes! You can create multiple Virtual Keys (one for each provider) and attach them to a single config. This config can then be connected to your API key, allowing you to use multiple providers through a single API key. + + +Portkey provides several ways to track team costs: +- Create separate Virtual Keys for each team +- Use metadata tags in your configs +- Set up team-specific API keys +- Monitor usage in the analytics dashboard + + +When a team reaches their budget limit: +1. Further requests will be blocked +2. Team admins receive notifications +3. Usage statistics remain available in dashboard +4. Limits can be adjusted if needed + + + + +# Next Steps + + **Join our Community** +- [Discord Community](https://portkey.sh/discord-report) +- [GitHub Repository](https://github.com/Portkey-AI) + + +For enterprise support and custom features, contact our [enterprise team](https://calendly.com/portkey-ai). + diff --git a/virtual_key_old/integrations/libraries/langchain-js.mdx b/virtual_key_old/integrations/libraries/langchain-js.mdx new file mode 100644 index 00000000..8c70323a --- /dev/null +++ b/virtual_key_old/integrations/libraries/langchain-js.mdx @@ -0,0 +1,268 @@ +--- +title: "Langchain (JS/TS)" +description: "Portkey adds core production capabilities to any Langchain app." +--- + + +This guide covers the integration for the **Javascript / Typescript** flavour of Langchain. Docs for the Python Langchain integration are [here](/integrations/libraries/langchain-python). + + +**LangChain** is a framework for developing applications powered by language models. It enables applications that: + +* **Are context-aware**: connect a language model to sources of context (prompt instructions, few shot examples, content to ground its response in, etc.) +* **Reason**: rely on a language model to reason (about how to answer based on provided context, what actions to take, etc.) + +You can find more information about it [here](https://python.langchain.com/docs/tutorials/). + +When using Langchain, **Portkey can help take it to production** by adding a fast AI gateway, observability, prompt management and more to your Langchain app. + +## Quick Start Integration + +Install the Portkey and Langchain SDKs to get started. + +```sh +npm install langchain portkey-ai @langchain/openai +``` + + +Since Portkey is fully compatible with the OpenAI signature, you can connect to the Portkey Ai Gateway through the `ChatOpenAI` interface. + +* Set the `baseURL` as `PORTKEY_GATEWAY_URL` +* Add `defaultHeaders` to consume the headers needed by Portkey using the `createHeaders` helper method. + + +We can now initialise the model and update the model to use Portkey's AI gateway + +```js +import { ChatOpenAI } from "@langchain/openai"; +import { createHeaders, PORTKEY_GATEWAY_URL} from "portkey-ai" + +const PORTKEY_API_KEY = "..." +const PROVIDER_API_KEY = "..." // Add the API key of the AI provider being used + +const portkeyConf = { + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({apiKey: PORTKEY_API_KEY, provider: "openai"}) +} + +const chatModel = new ChatOpenAI({ + apiKey: PROVIDER_API_KEY, + configuration: portkeyConf +}); + +const response = await chatModel.invoke("What is the meaning of life, universe and everything?"); +console.log("Response:", response); +``` + +Response + + + +```js +AIMessage { + lc_serializable: true, + lc_kwargs: { + content: `The phrase "the meaning of life, universe, and everything" is a reference to Douglas Adams' science fiction series, "The Hitchhiker's Guide to the Galaxy." In the series, a supercomputer called Deep Thought was asked to calculate the Answer to the Ultimate Question of Life, the Universe, and Everything. After much deliberation, Deep Thought revealed that the answer was simply the number 42.\n` + + '\n' + + 'In the context of the series, the number 42 is meant to highlight the absurdity and unknowability of the ultimate meaning of life and the universe. It is a humorous and satirical take on the deep philosophical questions that have puzzled humanity for centuries.\n' + + '\n' + + 'Ultimately, the meaning of life, universe, and everything is a complex and deeply personal question that each individual must grapple with and find their own answer to. It may be different for each person and can encompass a wide range of beliefs, values, and experiences.', + tool_calls: [], + invalid_tool_calls: [], + additional_kwargs: { function_call: undefined, tool_calls: undefined }, + response_metadata: {} + }, + lc_namespace: [ 'langchain_core', 'messages' ], + content: `The phrase "the meaning of life, universe, and everything" is a reference to Douglas Adams' science fiction series, "The Hitchhiker's Guide to the Galaxy." In the series, a supercomputer called Deep Thought was asked to calculate the Answer to the Ultimate Question of Life, the Universe, and Everything. After much deliberation, Deep Thought revealed that the answer was simply the number 42.\n` + + '\n' + + 'In the context of the series, the number 42 is meant to highlight the absurdity and unknowability of the ultimate meaning of life and the universe. It is a humorous and satirical take on the deep philosophical questions that have puzzled humanity for centuries.\n' + + '\n' + + 'Ultimately, the meaning of life, universe, and everything is a complex and deeply personal question that each individual must grapple with and find their own answer to. It may be different for each person and can encompass a wide range of beliefs, values, and experiences.', + name: undefined, + additional_kwargs: { function_call: undefined, tool_calls: undefined }, + response_metadata: { + tokenUsage: { completionTokens: 186, promptTokens: 18, totalTokens: 204 }, + finish_reason: 'stop' + }, + tool_calls: [], + invalid_tool_calls: [] +} +``` + +The call and the corresponding prompt will also be visible on the Portkey logs tab. + + + + + +## Using Virtual Keys for Multiple Models + +Portkey supports [Virtual Keys](/product/ai-gateway/virtual-keys) which are an easy way to store and manage API keys in a secure vault. Lets try using a Virtual Key to make LLM calls. + +#### 1\. Create a Virtual Key in your Portkey account and the id + +Let's try creating a new Virtual Key for Mistral like this + + + + + +#### 2\. Use Virtual Keys in the Portkey Headers + +The `virtualKey` parameter sets the authentication and provider for the AI provider being used. In our case we're using the Mistral Virtual key. + + +Notice that the `apiKey` can be left blank as that authentication won't be used. + + + + +```js +import { ChatOpenAI } from "@langchain/openai"; +import { createHeaders, PORTKEY_GATEWAY_URL} from "portkey-ai" + +const PORTKEY_API_KEY = "..." +const MISTRAL_VK = "..." // Add the virtual key for mistral that we just created + +const portkeyConf = { + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({apiKey: PORTKEY_API_KEY, virtualKey: MISTRAL_VK}) +} + +const chatModel = new ChatOpenAI({ + apiKey: "X", + configuration: portkeyConf, + model: "mistral-large-latest" +}); + +const response = await chatModel.invoke("What is the meaning of life, universe and everything?"); +console.log("Response:", response); +``` + +The Portkey AI gateway will authenticate the API request to Mistral and get the response back in the OpenAI format for you to consume. + +The AI gateway extends Langchain's `ChatOpenAI` class making it a single interface to call any provider and any model. + +## Embeddings + +Embeddings in Langchain through Portkey work the same way as the Chat Models using the `OpenAIEmbeddings` class. Let's try to create an embedding using OpenAI's embedding model + +```js +import { OpenAIEmbeddings } from "@langchain/openai"; +import { createHeaders, PORTKEY_GATEWAY_URL} from "portkey-ai" + +const PORTKEY_API_KEY = "..."; +const OPENAI_VK = "..." // Add OpenAI's Virtual Key created in Portkey + +const portkeyConf = { + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({apiKey: PORTKEY_API_KEY, virtualKey: OPENAI_VK}) +} + +/* Create instance */ +const embeddings = new OpenAIEmbeddings({ + apiKey: "X", + configuration: portkeyConf, +}); + +/* Embed queries */ +const res = await embeddings.embedQuery("Hello world"); +console.log("Response:", res); +``` + +## Chains & Prompts + +[Chains](https://python.langchain.com/docs/modules/chains/) enable the integration of various Langchain concepts for simultaneous execution while Langchain supports [Prompt Templates](https://python.langchain.com/docs/modules/model%5Fio/prompts/) to construct inputs for language models. Lets see how this would work with Portkey + +```js +import { ChatOpenAI } from "@langchain/openai"; +import { ChatPromptTemplate } from "@langchain/core/prompts"; +import { createHeaders, PORTKEY_GATEWAY_URL} from "portkey-ai" + +const PORTKEY_API_KEY = "..."; +const OPENAI_VK = "..." // Add OpenAI's Virtual Key created in Portkey + +const portkeyConf = { + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({apiKey: PORTKEY_API_KEY, virtualKey: OPENAI_VK}) +} + +// Initialise the chat model +const chatModel = new ChatOpenAI({ + apiKey: "X", + configuration: portkeyConf +}); + +// Define the chat prompt template +const prompt = ChatPromptTemplate.fromMessages([ + ["human", "Tell me a short joke about {topic}"], +]); + +// Invoke the chain with the prompt and chat model +const chain = prompt.pipe(chatModel); +const res = await chain.invoke({ topic: "ice cream" }); + +console.log(res) +``` + +We'd be able to view the exact prompt that was used to make the call to OpenAI in the Portkey logs dashboards. + +## Using Advanced Routing + +The Portkey AI Gateway brings capabilities like load-balancing, fallbacks, experimentation and canary testing to Langchain through a configuration-first approach. + +Let's take an **example** where we might want to split traffic between gpt-4 and claude-opus 50:50 to test the two large models. The gateway configuration for this would look like the following: + +```js +const config = { + "strategy": { + "mode": "loadbalance" + }, + "targets": [{ + "virtual_key": OPENAI_VK, // OpenAI's virtual key + "override_params": {"model": "gpt4"}, + "weight": 0.5 + }, { + "virtual_key": ANTHROPIC_VK, // Anthropic's virtual key + "override_params": {"model": "claude-3-opus-20240229"}, + "weight": 0.5 + }] +} +``` + +We can then use this `config` in our requests being made from langchain. + + + +```js +const portkeyConf = { + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({apiKey: PORTKEY_API_KEY, config: config}) +} + +const chatModel = new ChatOpenAI({ + apiKey: "X", + configuration: portkeyConf, + maxTokens: 100 +}); + +const res = await chatModel.invoke("What is the meaning of life, universe and everything?") +``` + +When the LLM is invoked, Portkey will distribute the requests to `gpt-4` and `claude-3-opus-20240229` in the ratio of the defined weights. + +You can find more config examples [here](/api-reference/config-object#examples). + +## Agents & Tracing + +A powerful capability of Langchain is creating Agents. The challenge with agentic workflows is that prompts are often abstracted out and it's hard to get a visibility into what the agent is doing. This also makes debugging harder. + +Connect the Portkey configuration to the `ChatOpenAI` model and we'd be able to use all the benefits of the AI gateway as shown above. + +Also, Portkey would capture the logs from the agent API calls giving us full visibility. + + + + + +This is extremely powerful since we gain control and visibility over the agent flows so we can identify problems and make updates as needed. diff --git a/virtual_key_old/integrations/libraries/langchain-python.mdx b/virtual_key_old/integrations/libraries/langchain-python.mdx new file mode 100644 index 00000000..1e5e8e85 --- /dev/null +++ b/virtual_key_old/integrations/libraries/langchain-python.mdx @@ -0,0 +1,420 @@ +--- +title: "Langchain (Python)" +description: "Supercharge Langchain apps with Portkey: Multi-LLM, observability, caching, reliability, and prompt management." +--- + + +This guide covers Langchain **Python**. For JS, see [Langchain JS](/integrations/libraries/langchain-js). + + +Portkey extends Langchain's `ChatOpenAI` to effortlessly work with **200+ LLMs** (Anthropic, Gemini, Mistral, etc.) without needing different SDKs. Portkey enhances your Langchain apps with interoperability, reliability, speed, cost-efficiency, and deep observability. + +## Getting Started + +Integrate Portkey into Langchain easily. + +### 1. Install Packages + +```sh +pip install -U langchain-openai portkey-ai +``` + + +`langchain-openai` includes `langchain-core`. Install `langchain` or other specific packages if you need more components. + + +### 2. Basic Setup: `ChatOpenAI` with Portkey + +Configure `ChatOpenAI` to route requests via Portkey using your Portkey API Key and `createHeaders` method. + +```python +from langchain_openai import ChatOpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders +import os + +# Use environment variables for API keys +PORTKEY_API_KEY = os.environ.get("PORTKEY_API_KEY") +PROVIDER_API_KEY = os.environ.get("OPENAI_API_KEY") # Example: OpenAI API Key + +# Configure Portkey headers +portkey_headers = createHeaders( + api_key=PORTKEY_API_KEY, + provider="openai" # Specify target LLM provider +) + +llm = ChatOpenAI( + api_key=PROVIDER_API_KEY, # Provider's API key + base_url=PORTKEY_GATEWAY_URL, # Route via Portkey + default_headers=portkey_headers, # Portkey specific headers + model="gpt-4o" # Specify provider model +) + +# response = llm.invoke("Tell me a joke about AI.") +# print(response.content) +``` + +**Key `ChatOpenAI` Parameters:** + +* `api_key`: The underlying provider's API key. +* `base_url`: Set to `PORTKEY_GATEWAY_URL` to route via Portkey. +* `default_headers`: Uses `createHeaders` for your `PORTKEY_API_KEY`. Can also include a `virtual_key` (for provider credentials) or a `config` ID (for advanced routing). + +All LLM calls via this `llm` instance now use Portkey, starting with observability. + + + + + +This setup enables Portkey's advanced features for Langchain. + +## Key Portkey Features for Langchain + +Routing Langchain requests via Portkey's `ChatOpenAI` interface unlocks powerful capabilities: + + + +

Use `ChatOpenAI` for OpenAI, Anthropic, Gemini, Mistral, and more. Switch providers easily with Virtual Keys or Configs.

+
+ +

Reduce latency and costs with Portkey's Simple, Semantic, or Hybrid caching, enabled via Configs.

+
+ +

Build robust apps with retries, timeouts, fallbacks, and load balancing, configured in Portkey.

+
+ +

Get deep insights: LLM usage, costs, latency, and errors are automatically logged in Portkey.

+
+ +

Manage, version, and use prompts from Portkey's Prompt Library within Langchain.

+
+ +

Securely manage LLM provider API keys using Portkey Virtual Keys in your Langchain setup.

+
+
+ +--- + +## 1. Multi-LLM Integration + +Portkey simplifies using different LLM providers. `ChatOpenAI` becomes your universal client for numerous models. + +**Mechanism: Portkey Headers with Virtual Keys** + +Switch LLMs by changing the `virtual_key` in `createHeaders` and the `model` in `ChatOpenAI`. Portkey manages provider specifics. + +### Example: Anthropic (Claude) + +1. **Create Anthropic Virtual Key:** In Portkey, add your Anthropic API key and get the Virtual Key ID. + +2. **Update Langchain code:** + +```python +from langchain_openai import ChatOpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders +import os + +PORTKEY_API_KEY = os.environ.get("PORTKEY_API_KEY") +ANTHROPIC_VIRTUAL_KEY = os.environ.get("ANTHROPIC_VIRTUAL_KEY") + +portkey_anthropic_headers = createHeaders( + api_key=PORTKEY_API_KEY, + virtual_key=ANTHROPIC_VIRTUAL_KEY +) + +llm_claude = ChatOpenAI( + api_key="placeholder_anthropic_key", # Placeholder; Portkey uses Virtual Key + base_url=PORTKEY_GATEWAY_URL, + default_headers=portkey_anthropic_headers, + model="claude-3-5-sonnet-latest" # Anthropic model +) + +# response_claude = llm_claude.invoke("Haiku principles?") +# print(response_claude.content) +``` + +### Example: Vertex (Gemini) + +1. **Create Google Virtual Key:** In Portkey, add your [Vertex AI credentials](/integrations/llms/vertex-ai#how-to-find-your-google-vertex-project-details) and get the Virtual Key ID. + +2. **Update Langchain code:** + +```python +from langchain_openai import ChatOpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders +import os + +PORTKEY_API_KEY = os.environ.get("PORTKEY_API_KEY") +GOOGLE_VIRTUAL_KEY = os.environ.get("GOOGLE_VIRTUAL_KEY") + +portkey_gemini_headers = createHeaders( + api_key=PORTKEY_API_KEY, + virtual_key=GOOGLE_VIRTUAL_KEY +) + +llm_gemini = ChatOpenAI( + api_key="placeholder_google_key", # Placeholder; Portkey uses Virtual Key + base_url=PORTKEY_GATEWAY_URL, + default_headers=portkey_gemini_headers, + model="gemini-2.5-pro-preview" # Gemini model +) + +# response_gemini = llm_gemini.invoke("Zero-knowledge proofs?") +# print(response_gemini.content) +``` + +Core `ChatOpenAI` structure remains. Only `virtual_key` and `model` change. Portkey maps responses to the OpenAI format Langchain expects. This extends to Mistral, Cohere, Azure, Bedrock via their Virtual Keys. + +--- + +## 2. Advanced Caching + +Portkey's caching reduces latency and LLM costs. Enable it via a Portkey [Config object](/api-reference/config-object) or a saved Config ID in `createHeaders`. + +A Portkey Config can specify `mode` (`simple`, `semantic`, `hybrid`) and `max_age` (cache duration). + +### Example: Semantic Caching + +1. **Define/Save Portkey Config:** Create a Config in Portkey (e.g., `langchain-semantic-cache`) specifying caching strategy. + + ```json + // Example Portkey Config JSON for semantic cache + { + "cache": { "mode": "semantic", "max_age": "2h" }, + "virtual_key": "your_openai_virtual_key_id", + "override_params": { "model": "gpt-4o" } + } + ``` + Assume saved Config ID is `cfg_semantic_cache_123`. + +2. **Use Config ID in `createHeaders`:** + +```python +from langchain_openai import ChatOpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders +import os + +PORTKEY_API_KEY = os.environ.get("PORTKEY_API_KEY") +PORTKEY_CONFIG_ID = "cfg_semantic_cache_123" + +portkey_cached_headers = createHeaders( + api_key=PORTKEY_API_KEY, + config=PORTKEY_CONFIG_ID +) + +llm_cached = ChatOpenAI( + api_key="placeholder_key", # Config can handle auth via virtual_key + base_url=PORTKEY_GATEWAY_URL, + default_headers=portkey_cached_headers +) + +# response1 = llm_cached.invoke("Capital of France?") +# response2 = llm_cached.invoke("Tell me France's capital.") +``` + +Similar requests can now hit the cache. Monitor cache performance in your Portkey dashboard. + +--- + +## 3. Enhanced Reliability + +Portkey improves Langchain app resilience via Configs: + +* **Retries:** Auto-retry failed LLM requests. +* **Fallbacks:** Define backup LLMs if a primary fails. +* **Load Balancing:** Distribute requests across keys or models. +* **Timeouts:** Set max request durations. + +### Example: Fallbacks with Retries + +1. **Define/Save Portkey Config:** Create a Config for retries and fallbacks (e.g., `gpt-4o` then `claude-3-sonnet`). + + ```json + // Example Portkey Config for reliability + { + "strategy": { "mode": "fallback" }, + "targets": [ + { "override_params": {"model": "gpt-4o"}, "virtual_key": "vk_openai", "retry": {"count": 2} }, + { "override_params": {"model": "claude-3-sonnet-20240229"}, "virtual_key": "vk_anthropic" } + ] + } + ``` + Assume saved Config ID is `cfg_reliable_123`. + +2. **Use Config ID in `createHeaders`:** + +```python +from langchain_openai import ChatOpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders +import os + +PORTKEY_API_KEY = os.environ.get("PORTKEY_API_KEY") +PORTKEY_RELIABLE_CONFIG_ID = "cfg_reliable_123" + +portkey_reliable_headers = createHeaders( + api_key=PORTKEY_API_KEY, + config=PORTKEY_RELIABLE_CONFIG_ID +) + +llm_reliable = ChatOpenAI( + api_key="placeholder_key", + base_url=PORTKEY_GATEWAY_URL, + default_headers=portkey_reliable_headers +) + +# response = llm_reliable.invoke("Poem on resilient AI.") +``` + +Offload complex logic to Portkey Configs, keeping Langchain code clean. + +--- + +## 4. Full Observability + +Routing Langchain `ChatOpenAI` via Portkey provides instant, comprehensive observability: + +* **Logged Requests:** Detailed logs of requests, responses, latencies, costs. +* **Tracing:** Understand call lifecycles. +* **Performance Analytics:** Monitor metrics, track usage. +* **Debugging:** Pinpoint errors quickly. + +This is crucial for monitoring and optimizing production Langchain apps. + + + + + +--- + +## 5. Prompt Management + +Portkey's Prompt Library helps manage prompts effectively: + +* **Version Control:** Store and track prompt changes. +* **Parameterized Prompts:** Use variables with [mustache templating](/product/prompt-library/prompt-templates#templating-engine). +* **Sandbox:** Test prompts with different LLMs in Portkey. + +### Using Portkey Prompts in Langchain + +1. Create prompt in Portkey, get `Prompt ID`. +2. Use Portkey SDK to render prompt with variables. +3. Transform rendered prompt to Langchain message format. +4. Pass messages to Portkey-configured `ChatOpenAI`. + +```python +import os +from langchain_openai import ChatOpenAI +from langchain_core.messages import SystemMessage, HumanMessage +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders, Portkey + +PORTKEY_API_KEY = os.environ.get("PORTKEY_API_KEY") +client = Portkey(api_key=PORTKEY_API_KEY) + +PROMPT_ID = "pp-story-generator" # Your Portkey Prompt ID + +rendered_prompt = client.prompts.render( + prompt_id=PROMPT_ID, + variables={"character": "brave knight", "object": "magic sword"} +).data + +langchain_messages = [] +if rendered_prompt and rendered_prompt.prompt: + for msg in rendered_prompt.prompt: + if msg.get("role") == "user": langchain_messages.append(HumanMessage(content=msg.get("content"))) + elif msg.get("role") == "system": langchain_messages.append(SystemMessage(content=msg.get("content"))) + +OPENAI_VIRTUAL_KEY = os.environ.get("OPENAI_VIRTUAL_KEY") +portkey_headers = createHeaders(api_key=PORTKEY_API_KEY, virtual_key=OPENAI_VIRTUAL_KEY) + +llm_portkey_prompt = ChatOpenAI( + api_key="placeholder_key", + base_url=PORTKEY_GATEWAY_URL, + default_headers=portkey_headers, + model=rendered_prompt.model if rendered_prompt and rendered_prompt.model else "gpt-4o" +) + +# if langchain_messages: response = llm_portkey_prompt.invoke(langchain_messages) +``` + +Manage prompts centrally in Portkey for versioning and collaboration. + +--- + +## 6. Secure Virtual Keys + +Portkey's [Virtual Keys](/product/ai-gateway/virtual-keys) are vital for secure, flexible LLM ops with Langchain. + +**Benefits:** +* **Secure Credentials:** Store provider API keys in Portkey's vault. Code uses Virtual Key IDs. +* **Easy Configuration:** Switch providers/keys by changing `virtual_key` in `createHeaders`. +* **Access Control:** Manage Virtual Key permissions in Portkey. +* **Auditability:** Track usage via Portkey logs. + +Using Virtual Keys boosts security and simplifies config management. + +--- + +## Langchain Embeddings + +Create embeddings with `OpenAIEmbeddings` via Portkey. + +```python +from langchain_openai import OpenAIEmbeddings +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders +import os + +PORTKEY_API_KEY = os.environ.get("PORTKEY_API_KEY") +OPENAI_EMBEDDINGS_VIRTUAL_KEY = os.environ.get("OPENAI_EMBEDDINGS_VIRTUAL_KEY") + +portkey_headers = createHeaders(api_key=PORTKEY_API_KEY, virtual_key=OPENAI_EMBEDDINGS_VIRTUAL_KEY) + +embeddings_model = OpenAIEmbeddings( + api_key="placeholder_key", + base_url=PORTKEY_GATEWAY_URL, + default_headers=portkey_headers, + model="text-embedding-3-small" +) + +# embeddings = embeddings_model.embed_documents(["Hello world!", "Test."]) +``` + + +Portkey supports OpenAI embeddings via Langchain's `OpenAIEmbeddings`. For other providers (Cohere, Gemini), use the **Portkey SDK directly** ([docs](/api-reference/create-embeddings)). + + +--- + +## Langchain Chains & Prompts + +Standard Langchain [Chains](https://python.langchain.com/docs/modules/chains/) and [PromptTemplates](https://python.langchain.com/docs/modules/model_io/prompts/) work seamlessly with Portkey-configured `ChatOpenAI` instances. Portkey features (logging, caching) apply automatically. + +```python +from langchain_openai import ChatOpenAI +from langchain_core.prompts import ChatPromptTemplate +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders +import os + +PORTKEY_API_KEY = os.environ.get("PORTKEY_API_KEY") +OPENAI_VIRTUAL_KEY = os.environ.get("OPENAI_VIRTUAL_KEY") + +portkey_headers = createHeaders(api_key=PORTKEY_API_KEY, virtual_key=OPENAI_VIRTUAL_KEY) + +chat_llm = ChatOpenAI( + api_key="placeholder_key", + base_url=PORTKEY_GATEWAY_URL, + default_headers=portkey_headers, + model="gpt-4o" +) + +prompt = ChatPromptTemplate.from_messages([ + ("system", "You are a world-class technical writer."), + ("user", "{input}") +]) + +chain = prompt | chat_llm + +# response = chain.invoke({"input": "Explain API gateways simply."}) +``` + +All chain requests via `chat_llm` are processed by Portkey. + +This concludes the main features. Redundant examples have been removed for clarity. diff --git a/virtual_key_old/integrations/libraries/librechat.mdx b/virtual_key_old/integrations/libraries/librechat.mdx new file mode 100644 index 00000000..ddb5e766 --- /dev/null +++ b/virtual_key_old/integrations/libraries/librechat.mdx @@ -0,0 +1,439 @@ +--- +title: 'LibreChat' +description: 'Cost tracking, observability, and more for LibreChat' +--- + +Portkey **natively integrates** with LibreChat and makes your LibreChat deployments **production-grade** and **reliable** with our suite of features: + +- **Unified AI Gateway** - Single interface for 1600+ LLMs with API key management. (not just OpenAI & Anthropic) +- **Centralized AI observability**: Real-time usage tracking for 40+ key metrics and logs for every request +- **Governance** - Real-time spend tracking, set budget limits and RBAC in your LibreChat setup +- **Security Guardrails** - PII detection, content filtering, and compliance controls + +This guide will walk you through integrating Portkey with LibreChat and setting up essential enterprise features including usage tracking, access controls, and budget management. + + + If you are an enterprise looking to use LibreChat in your organisation, [check out this section](#3-set-up-enterprise-governance-for-librechat). + + +# 1. Setting up Portkey +Portkey allows you to use 1600+ LLMs with your LibreChat setup, with minimal configuration required. Let's set up the core components in Portkey that you'll need for integration. + + + +Virtual Keys are Portkey's secure way to manage your LLM provider API keys. Think of them like disposable credit cards for your LLM API keys, providing essential controls like: +- Budget limits for API usage +- Rate limiting capabilities +- Secure API key storage + +To create a virtual key: +Go to [Virtual Keys](https://app.portkey.ai/virtual-keys) in the Portkey App. Save and copy the virtual key ID + + + + + + +Save your virtual key ID - you'll need it for the next step. + + + + + +Configs in Portkey are JSON objects that define how your requests are routed. They help with implementing features like advanced routing, fallbacks, and retries. + +We need to create a default config to route our requests to the virtual key created in Step 1. + +To create your config: +1. Go to [Configs](https://app.portkey.ai/configs) in Portkey dashboard +2. Create new config with: + ```json + { + "virtual_key": "YOUR_VIRTUAL_KEY_FROM_STEP1", + "override_params": { + "model": "gpt-4o" // Your preferred model name + } + } + + ``` +3. Save and note the Config name for the next step + + + + + + +This basic config connects to your virtual key. You can add more advanced portkey features later. + + + + + +Now create Portkey API key access point and attach the config you created in Step 2: + +1. Go to [API Keys](https://app.portkey.ai/api-keys) in Portkey and Create new API key +2. Select your config from `Step 2` +3. Generate and save your API key + + + + + + +Save your API key securely - you'll need it for LibreChat integration. + + + + + +## 2. Integrate Portkey with LibreChat + +**Create the `docker-compose-override.yaml` file** + +Create this file [following the instructions here](https://www.librechat.ai/docs/quick_start/custom_endpoints). +This file will point to the `librechat.yaml` file where we will configure our Portkey settings (in Step 3). + +```yaml docker-compose.override.yml +services: + api: + volumes: + - type: bind + source: ./librechat.yaml + target: /app/librechat.yaml +``` + +**Configure the `.env` file** + +Edit your existing `.env` file at the project root (if the file does not exist, copy the `.env.example` file and rename to `.env`). We will add: + +```env .env +PORTKEY_API_KEY=YOUR_PORTKEY_API_KEY +PORTKEY_GATEWAY_URL=https://api.portkey.ai/v1 +``` + +**Edit the `librechat.yaml` file** + + +Edit this file [following the instructions here](https://www.librechat.ai/docs/quick_start/custom_endpoints). +Here, you can either pass your **Config** (containing provider/model configurations) or direct provider **Virtual key** saved on Portkey. + + + LibreChat requires that the API key field is present. Since we don't need it for the Portkey integration, we can pass a dummy string for it. + + + +```yaml librechat.yaml with Portkey Config +version: 1.1.4 +cache: true +endpoints: + custom: + - name: "Portkey" + apiKey: "dummy" + baseURL: ${PORTKEY_GATEWAY_URL} + headers: + x-portkey-api-key: "${PORTKEY_API_KEY}" + x-portkey-config: "pc-libre-xxx" + models: + default: ["llama-3.2"] + fetch: true + titleConvo: true + titleModel: "current_model" + summarize: false + summaryModel: "current_model" + forcePrompt: false + modelDisplayLabel: "Portkey:Llama" +``` + +```yaml librechat.yaml with Portkey Virtual Key +version: 1.1.4 +cache: true +endpoints: + custom: + - name: "Portkey" + apiKey: "dummy" + baseURL: ${PORTKEY_GATEWAY_URL} + headers: + x-portkey-api-key: "${PORTKEY_API_KEY}" + x-portkey-virtual-key: "PORTKEY_OPENAI_VIRTUAL_KEY" + models: + default: ["gpt-4o-mini"] + fetch: true + titleConvo: true + titleModel: "current_model" + summarize: false + summaryModel: "current_model" + forcePrompt: false + modelDisplayLabel: "Portkey:OpenAI" +``` + + + +If you're a system admin, and you're looking to track the costs/user on a centralized instance of LibreChat, here's [a community guide by Tim Manik](https://github.com/timmanik/librechat-for-portkey). + + + +# 3. Set Up Enterprise Governance for LibreChat + +**Why Enterprise Governance?** +If you are using LibreChat inside your orgnaization, you need to consider several governance aspects: +- **Cost Management**: Controlling and tracking AI spending across teams +- **Access Control**: Managing which teams can use specific models +- **Usage Analytics**: Understanding how AI is being used across the organization +- **Security & Compliance**: Maintaining enterprise security standards +- **Reliability**: Ensuring consistent service across all users + +Portkey adds a comprehensive governance layer to address these enterprise needs. Let's implement these controls step by step. + +**Enterprise Implementation Guide** + + + +### Step 1: Implement Budget Controls & Rate Limits + +Virtual Keys enable granular control over LLM access at the team/department level. This helps you: +- Set up [budget limits](/product/ai-gateway/virtual-keys/budget-limits) +- Prevent unexpected usage spikes using Rate limits +- Track departmental spending + +#### Setting Up Department-Specific Controls: +1. Navigate to [Virtual Keys](https://app.portkey.ai/virtual-keys) in Portkey dashboard +2. Create new Virtual Key for each department with budget limits and rate limits +3. Configure department-specific limits + + + + + + + + +### Step 2: Define Model Access Rules + +As your AI usage scales, controlling which teams can access specific models becomes crucial. Portkey Configs provide this control layer with features like: + +#### Access Control Features: +- **Model Restrictions**: Limit access to specific models +- **Data Protection**: Implement guardrails for sensitive data +- **Reliability Controls**: Add fallbacks and retry logic + +#### Example Configuration: +Here's a basic configuration to route requests to OpenAI, specifically using GPT-4o: + +```json +{ + "strategy": { + "mode": "single" + }, + "targets": [ + { + "virtual_key": "YOUR_OPENAI_VIRTUAL_KEY", + "override_params": { + "model": "gpt-4o" + } + } + ] +} +``` + +Create your config on the [Configs page](https://app.portkey.ai/configs) in your Portkey dashboard. You'll need the config ID for connecting to LibreChat's setup. + + + +Configs can be updated anytime to adjust controls without affecting running applications. + + + + +### Step 3: Implement Access Controls + +Create User-specific API keys that automatically: +- Track usage per user/team with the help of virtual keys +- Apply appropriate configs to route requests +- Collect relevant metadata to filter logs +- Enforce access permissions + + +Create API keys through: +- [Portkey App](https://app.portkey.ai/) +- [API Key Management API](/api-reference/admin-api/control-plane/api-keys/create-api-key) + + +Example using Python SDK: +```python +from portkey_ai import Portkey + +portkey = Portkey(api_key="YOUR_ADMIN_API_KEY") + +api_key = portkey.api_keys.create( + name="engineering-team", + type="organisation", + workspace_id="YOUR_WORKSPACE_ID", + defaults={ + "config_id": "your-config-id", + "metadata": { + "environment": "production", + "department": "engineering" + } + }, + scopes=["logs.view", "configs.read"] +) +``` + +For detailed key management instructions, see our [API Keys documentation](/api-reference/admin-api/control-plane/api-keys/create-api-key). + + + +### Step 4: Deploy & Monitor +After distributing API keys to your team members, your enterprise-ready LibreChat setup is ready to go. Each team member can now use their designated API keys with appropriate access levels and budget controls. +Apply your governance setup using the integration steps from earlier sections +Monitor usage in Portkey dashboard: +- Cost tracking by department +- Model usage patterns +- Request volumes +- Error rates + + + + + +### Enterprise Features Now Available +**LibreChat now has:** + +- Departmental budget controls +- Model access governance +- Usage tracking & attribution +- Security guardrails +- Reliability features + + + +# Portkey Features +Now that you have enterprise-grade LibreChat setup, let's explore the comprehensive features Portkey provides to ensure secure, efficient, and cost-effective AI operations. + +### 1. Comprehensive Metrics +Using Portkey you can track 40+ key metrics including cost, token usage, response time, and performance across all your LLM providers in real time. You can also filter these metrics based on custom metadata that you can set in your configs. Learn more about metadata here. + + + + + +### 2. Advanced Logs +Portkey's logging dashboard provides detailed logs for every request made to your LLMs. These logs include: +- Complete request and response tracking +- Metadata tags for filtering +- Cost attribution and much more... + + + + + + + + +### 3. Unified Access to 1600+ LLMs + +You can easily switch between 1600+ LLMs. Call various LLMs such as Anthropic, Gemini, Mistral, Azure OpenAI, Google Vertex AI, AWS Bedrock, and many more by simply changing the `virtual key` in your default `config` object. + + +### 4. Advanced Metadata Tracking +Using Portkey, you can add custom metadata to your LLM requests for detailed tracking and analytics. Use metadata tags to filter logs, track usage, and attribute costs across departments and teams. + + + + + + +### 5. Enterprise Access Management + + + +Set and manage spending limits across teams and departments. Control costs with granular budget limits and usage tracking. + + + +Enterprise-grade SSO integration with support for SAML 2.0, Okta, Azure AD, and custom providers for secure authentication. + + + +Hierarchical organization structure with workspaces, teams, and role-based access control for enterprise-scale deployments. + + + +Comprehensive access control rules and detailed audit logging for security compliance and usage tracking. + + + + +### 6. Reliability Features + + + Automatically switch to backup targets if the primary target fails. + + + Route requests to different targets based on specified conditions. + + + Distribute requests across multiple targets based on defined weights. + + + Enable caching of responses to improve performance and reduce costs. + + +Automatic retry handling with exponential backoff for failed requests + + + Set and manage budget limits across teams and departments. Control costs with granular budget limits and usage tracking. + + + + + +### 7. Advanced Guardrails + +Protect your Project's data and enhance reliability with real-time checks on LLM inputs and outputs. Leverage guardrails to: +- Prevent sensitive data leaks +- Enforce compliance with organizational policies +- PII detection and masking +- Content filtering +- Custom security rules +- Data compliance checks + + +Implement real-time protection for your LLM interactions with automatic detection and filtering of sensitive content, PII, and custom security rules. Enable comprehensive data protection while maintaining compliance with organizational policies. + + + +# FAQs + + + You can update your Virtual Key limits at any time from the Portkey dashboard:1. Go to Virtual Keys section2. Click on the Virtual Key you want to modify3. Update the budget or rate limits4. Save your changes + + + Yes! You can create multiple Virtual Keys (one for each provider) and attach them to a single config. This config can then be connected to your API key, allowing you to use multiple providers through a single API key. + + +Portkey provides several ways to track team costs: +- Create separate Virtual Keys for each team +- Use metadata tags in your configs +- Set up team-specific API keys +- Monitor usage in the analytics dashboard + + +When a team reaches their budget limit: +1. Further requests will be blocked +2. Team admins receive notifications +3. Usage statistics remain available in dashboard +4. Limits can be adjusted if needed + + + + +# Next Steps + + **Join our Community** +- [Discord Community](https://portkey.sh/discord-report) +- [GitHub Repository](https://github.com/Portkey-AI) + + +For enterprise support and custom features, contact our [enterprise team](https://calendly.com/portkey-ai). + diff --git a/virtual_key_old/integrations/libraries/llama-index-python.mdx b/virtual_key_old/integrations/libraries/llama-index-python.mdx new file mode 100644 index 00000000..20875365 --- /dev/null +++ b/virtual_key_old/integrations/libraries/llama-index-python.mdx @@ -0,0 +1,895 @@ +--- +title: "LlamaIndex (Python)" +description: The **Portkey x LlamaIndex** integration brings advanced **AI gateway** capabilities, full-stack **observability**, and **prompt management** to apps built on LlamaIndex. +--- + + + +In a nutshell, Portkey extends the familiar OpenAI schema to make Llamaindex work with **200+ LLMs** without the need for importing different classes for each provider or having to configure your code separately. Portkey makes your Llamaindex apps _reliable_, _fast_, and _cost-efficient_. + +## Getting Started + +### 1\. Install the Portkey SDK + +```sh +pip install -U portkey-ai +``` + +### 2\. Import the necessary classes and functions + +Import the `OpenAI` class in Llamaindex as you normally would, along with Portkey's helper functions `createHeaders` and `PORTKEY_GATEWAY_URL`. + +```py +from llama_index.llms.openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders +``` + +### 3\. Configure model details + +Configure your model details using Portkey's [**Config object schema**](/api-reference/config-object). This is where you can define the provider and model name, model parameters, set up fallbacks, retries, and more. + +```py +config = { + "provider":"openai", + "api_key":"YOUR_OPENAI_API_KEY", + "override_params": { + "model":"gpt-4o", + "max_tokens":64 + } +} +``` + +### 4\. Pass Config details to OpenAI client with necessary headers + +```py +portkey = OpenAI( + api_base=PORTKEY_GATEWAY_URL, + api_key="xx" # Placeholder, no need to set + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + config=config + ) +) +``` + +## Example: OpenAI + +Here are basic integrations examples on using the `complete` and `chat` methods with `streaming` on & off. + + + + +```python +from llama_index.llms.openai import OpenAI +from llama_index.core.llms import ChatMessage +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +config = { + "provider":"openai", + "api_key":"YOUR_OPENAI_API_KEY", + "override_params": { + "model":"gpt-4o", + "max_tokens":64 + } +} + +#### You can also reference a saved Config #### +#### config = "pc-anthropic-xx" + +portkey = OpenAI( + api_base=PORTKEY_GATEWAY_URL, + api_key="xx" # Placeholder, no need to set + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + config=config + ) +) + +messages = [ + ChatMessage(role="system", content="You are a pirate with a colorful personality"), + ChatMessage(role="user", content="What is your name"), +] + +resp = portkey.chat(messages) +print(resp) + +##### Streaming Mode ##### + +resp = portkey.stream_chat(messages) + +for r in resp: + print(r.delta, end="") +``` +> assistant: Arrr, matey! They call me Captain Barnacle Bill, the most colorful pirate to ever sail the seven seas! With a parrot on me shoulder and a treasure map in me hand, I'm always ready for adventure! What be yer name, landlubber? + + + + +```python +from llama_index.llms.openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +config = { + "provider":"openai", + "api_key":"YOUR_OPENAI_API_KEY", + "override_params": { + "model":"gpt-4o", + "max_tokens":64 + } +} + +#### You can also reference a saved Config #### +#### config = "pc-anthropic-xx" + +portkey = OpenAI( + api_base=PORTKEY_GATEWAY_URL, + api_key="xx" # Placeholder, no need to set + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + config=config + ) +) + +resp=portkey.complete("Paul Graham is ") +print(resp) + +##### Streaming Mode ##### + +resp=portkey.stream_complete("Paul Graham is ") +for r in resp: + print(r.delta, end="") +``` +> a computer scientist, entrepreneur, and venture capitalist. He is best known for co-founding the startup accelerator Y Combinator and for his work on programming languages and web development. Graham is also a prolific writer and has published essays on a wide range of topics, including startups, technology, and education. + + + +```python +import asyncio +from llama_index.llms.openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +config = { + "provider":"openai", + "api_key":"YOUR_OPENAI_API_KEY", + "override_params": { + "model":"gpt-4o", + "max_tokens":64 + } +} + +#### You can also reference a saved Config #### +#### config = "pc-anthropic-xx" + +async def main(): + portkey = OpenAI( + api_base=PORTKEY_GATEWAY_URL, + api_key="xx" # Placeholder, no need to set + default_headers=createHeaders( + api_key="PORTKEY_API_KEY", + config=config + ) + ) + + resp = await portkey.acomplete("Paul Graham is ") + print(resp) + + ##### Streaming Mode ##### + + resp = await portkey.astream_complete("Paul Graham is ") + async for delta in resp: + print(delta.delta, end="") + +asyncio.run(main()) +``` + + + +## Enabling Portkey Features + +By routing your LlamaIndex requests through Portkey, you get access to the following production-grade features: + + + +

Call various LLMs like Anthropic, Gemini, Mistral, Azure OpenAI, Google Vertex AI, and AWS Bedrock with minimal code changes.

+
+ + +

Speed up your requests and save money on LLM calls by storing past responses in the Portkey cache. Choose between Simple and Semantic cache modes.

+
+ + +

Set up fallbacks between different LLMs or providers, load balance your requests across multiple instances or API keys, set automatic retries, and request timeouts.

+
+ + +

Portkey automatically logs all the key details about your requests, including cost, tokens used, response time, request and response bodies, and more. Send custom metadata and trace IDs for better analytics and debugging.

+
+ + +

Use Portkey as a centralized hub to store, version, and experiment with prompts across multiple LLMs, and seamlessly retrieve them in your LlamaIndex app for easy integration.

+
+ + +

Improve your LlamaIndex app by capturing qualitative & quantitative user feedback on your requests.

+
+ + +

Set budget limits on provider API keys and implement fine-grained user roles and permissions for both the app and the Portkey APIs.

+
+
+ + +Much of these features are driven by **Portkey's Config architecture**. On the Portkey app, we make it easy to help you _create_, _manage_, and _version_ your Configs so that you can reference them easily in Llamaindex. + +## Saving Configs in the Portkey App + +Head over to the Configs tab in Portkey app where you can save various provider Configs along with the reliability and caching features. Each Config has an associated slug that you can reference in your Llamaindex code. + + + + +## Overriding a Saved Config + +If you want to use a saved Config from the Portkey app in your LlamaIndex code but need to modify certain parts of it before making a request, you can easily achieve this using Portkey's Configs API. This approach allows you to leverage the convenience of saved Configs while still having the flexibility to adapt them to your specific needs. + +#### Here's an example of how you can fetch a saved Config using the Configs API and override the `model` parameter: + +```py Overriding Model in a Saved Config +from llama_index.llms.openai import OpenAI +from llama_index.core.llms import ChatMessage +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders +import requests +import os + +def create_config(config_slug,model): + url = f'https://api.portkey.ai/v1/configs/{config_slug}' + headers = { + 'x-portkey-api-key': os.environ.get("PORTKEY_API_KEY"), + 'content-type': 'application/json' + } + response = requests.get(url, headers=headers).json() + config = json.loads(response['config']) + config['override_params']['model']=model + return config + +config=create_config("pc-llamaindex-xx","gpt-4-turbo") + +portkey = OpenAI( + api_base=PORTKEY_GATEWAY_URL, + api_key="xx" # Placeholder, no need to set + default_headers=createHeaders( + api_key=os.environ.get("PORTKEY_API_KEY"), + config=config + ) +) + +messages = [ChatMessage(role="user", content="1729")] + +resp = portkey.chat(messages) +print(resp) +``` + +In this example: + +1. We define a helper function `get_customized_config` that takes a `config_slug` and a `model` as parameters. +2. Inside the function, we make a GET request to the Portkey Configs API endpoint to fetch the saved Config using the provided `config_slug`. +3. We extract the `config` object from the API response. +4. We update the `model` parameter in the `override_params` section of the Config with the provided `custom_model`. +5. Finally, we return the customized Config. + +We can then use this customized Config when initializing the OpenAI client from LlamaIndex, ensuring that our specific `model` override is applied to the saved Config. + +For more details on working with Configs in Portkey, refer to the [**Config documentation**.](/product/ai-gateway/configs) + +--- + +## 1\. Interoperability - Calling Anthropic, Gemini, Mistral, and more + +Now that we have the OpenAI code up and running, let's see how you can use Portkey to send the request across multiple LLMs - we'll show **Anthropic**, **Gemini**, and **Mistral**. For the full list of providers & LLMs supported, check out [**this doc**](/guides/integrations). + +Switching providers just requires **changing 3 lines of code:** + +1. Change the `provider name` +2. Change the `API key`, and +3. Change the `model name` + + + + + +```python +from llama_index.llms.openai import OpenAI +from llama_index.core.llms import ChatMessage +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +config = { + "provider":"anthropic", + "api_key":"YOUR_ANTHROPIC_API_KEY", + "override_params": { + "model":"claude-3-opus-20240229", + "max_tokens":64 + } +} + +#### You can also reference a saved Config #### +#### config = "pc-anthropic-xx" + +portkey = OpenAI( + api_base=PORTKEY_GATEWAY_URL, + api_key="xx" # Placeholder, no need to set + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + config=config + ) +) + +messages = [ + ChatMessage(role="system", content="You are a pirate with a colorful personality"), + ChatMessage(role="user", content="What is your name"), +] + +resp = portkey.chat(messages) +print(resp) +``` + + + + +```python +from llama_index.llms.openai import OpenAI +from llama_index.core.llms import ChatMessage +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +config = { + "provider":"google", + "api_key":"YOUR_GOOGLE_GEMINI_API_KEY", + "override_params": { + "model":"gemini-1.5-flash-latest", + "max_tokens":64 + } +} + +#### You can also reference a saved Config instead #### +#### config = "pc-gemini-xx" + +portkey = OpenAI( + api_base=PORTKEY_GATEWAY_URL, + api_key="xx" # Placeholder, no need to set + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + config=config + ) +) + +messages = [ + ChatMessage(role="system", content="You are a pirate with a colorful personality"), + ChatMessage(role="user", content="What is your name"), +] + +resp = portkey.chat(messages) +print(resp) +``` + + + +```python +from llama_index.llms.openai import OpenAI +from llama_index.core.llms import ChatMessage +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +config = { + "provider":"mistral-ai", + "api_key":"YOUR_MISTRAL_AI_API_KEY", + "override_params": { + "model":"codestral-latest", + "max_tokens":64 + } +} + +#### You can also reference a saved Config instead #### +#### config = "pc-mistral-xx" + +portkey = OpenAI( + api_base=PORTKEY_GATEWAY_URL, + api_key="xx" # Placeholder, no need to set + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + config=config + ) +) + +messages = [ + ChatMessage(role="system", content="You are a pirate with a colorful personality"), + ChatMessage(role="user", content="What is your name"), +] + +resp = portkey.chat(messages) +print(resp) +``` + + + +### Calling Azure, Google Vertex, AWS Bedrock + + +We recommend saving your cloud details to [**Portkey vault**](/product/ai-gateway/virtual-keys) and getting a corresponding Virtual Key. + +[**Explore the Virtual Key documentation here**](/product/ai-gateway/virtual-keys)**.** + + + + + + + +```python +from llama_index.llms.openai import OpenAI +from llama_index.core.llms import ChatMessage +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +config = { + "virtual_key":"AZURE_OPENAI_PORTKEY_VIRTUAL_KEY" +} + +#### You can also reference a saved Config instead #### +#### config = "pc-azure-xx" + +portkey = OpenAI( + api_base=PORTKEY_GATEWAY_URL, + api_key="xx" # Placeholder, no need to set + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + config=config + ) +) + +messages = [ + ChatMessage(role="system", content="You are a pirate with a colorful personality"), + ChatMessage(role="user", content="What is your name"), +] + +resp = portkey.chat(messages) +print(resp) +``` + + + + +```python +from llama_index.llms.openai import OpenAI +from llama_index.core.llms import ChatMessage +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +config = { + "virtual_key":"AWS_BEDROCK_PORTKEY_VIRTUAL_KEY" +} + +#### You can also reference a saved Config instead #### +#### config = "pc-bedrock-xx" + +portkey = OpenAI( + api_base=PORTKEY_GATEWAY_URL, + api_key="xx" # Placeholder, no need to set + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + config=config + ) +) + +messages = [ + ChatMessage(role="system", content="You are a pirate with a colorful personality"), + ChatMessage(role="user", content="What is your name"), +] + +resp = portkey.chat(messages) +print(resp) +``` + + + + Vertex AI uses OAuth2 to authenticate its requests, so you need to send the **access token** additionally along with the request - you can do this while by sending it as the `api_key` in the OpenAI client. Run `gcloud auth print-access-token` in your terminal to get your Vertex AI access token. + + + +```python +from llama_index.llms.openai import OpenAI +from llama_index.core.llms import ChatMessage +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +config = { + "virtual_key":"VERTEX_AI_PORTKEY_VIRTUAL_KEY" +} + +#### You can also reference a saved Config instead #### +#### config = "pc-vertex-xx" + +portkey = OpenAI( + api_key="YOUR_VERTEX_AI_ACCESS_TOKEN", # Get by running gcloud auth print-access-token in terminal + api_base=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + config=config + ) +) + +messages = [ + ChatMessage(role="system", content="You are a pirate with a colorful personality"), + ChatMessage(role="user", content="What is your name"), +] + +resp = portkey.chat(messages) +print(resp) +``` + + + +### Calling Local or Privately Hosted Models like Ollama + +Check out [**Portkey docs for Ollama**](/integrations/llms/ollama) and [**other privately hosted models**](/integrations/llms/byollm). + +```py Ollama +from llama_index.llms.openai import OpenAI +from llama_index.core.llms import ChatMessage +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +config = { + "provider":"ollama", + "custom_host":"https://7cc4-3-235-157-146.ngrok-free.app", # Your Ollama ngrok URL + "override_params": { + "model":"llama3" + } +} + +#### You can also reference a saved Config instead #### +#### config = "pc-azure-xx" + +portkey = OpenAI( + api_base=PORTKEY_GATEWAY_URL, + api_key="xx" # Placeholder, no need to set + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + config=config + ) +) + +messages = [ + ChatMessage(role="system", content="You are a pirate with a colorful personality"), + ChatMessage(role="user", content="What is your name"), +] + +resp = portkey.chat(messages) +print(resp) +``` + +[**Explore full list of the providers supported on Portkey here**](/guides/integrations). + +--- + +## 2\. Caching + +You can speed up your requests and save money on your LLM requests by storing past responses in the Portkey cache. There are 2 cache modes: + +* **Simple:** Matches requests verbatim. Perfect for repeated, identical prompts. Works on **all models** including image generation models. +* **Semantic:** Matches responses for requests that are semantically similar. Ideal for denoising requests with extra prepositions, pronouns, etc. + +To enable Portkey cache, just add the `cache` params to your [config object](https://portkey.ai/docs/api-reference/config-object#cache-object-details). + + + + +```python +config = { + "provider":"mistral-ai", + "api_key":"YOUR_MISTRAL_AI_API_KEY", + "override_params": { + "model":"codestral-latest", + "max_tokens":64 + }, + "cache": { + "mode": "simple", + "max_age": 60000 + } +} +``` + + + + +```python +config = { + "provider":"mistral-ai", + "api_key":"YOUR_MISTRAL_AI_API_KEY", + "override_params": { + "model":"codestral-latest", + "max_tokens":64 + }, + "cache": { + "mode": "semantic", + "max_age": 60000 + } +} +``` + + +[**For more cache settings, check out the documentation here**](/product/ai-gateway/cache-simple-and-semantic)**.** + +--- + +## 3\. Reliability + +Set up fallbacks between different LLMs or providers, load balance your requests across multiple instances or API keys, set automatic retries, or set request timeouts - all set through **Configs**. + + + + + +```python +config = { + "strategy": { + "mode": "fallback" + }, + "targets": [ + { + "virtual_key": "openai-virtual-key", + "override_params": { + "model": "gpt-4o" + } + }, + { + "virtual_key": "anthropic-virtual-key", + "override_params": { + "model": "claude-3-opus-20240229", + "max_tokens":64 + } + } + ] +} +``` + + + +```python +config = { + "strategy": { + "mode": "loadbalance" + }, + "targets": [ + { + "virtual_key": "openai-virtual-key-1", + "weight":1 + }, + { + "virtual_key": "openai-virtual-key-2", + "weight":1 + } + ] +} +``` + + + +```python +config = { + "retry": { + "attempts": 5 + }, + "virtual_key": "virtual-key-xxx" +} +``` + + + +```python +config = { + "strategy": { "mode": "fallback" }, + "request_timeout": 10000, + "targets": [ + { "virtual_key": "open-ai-xxx" }, + { "virtual_key": "azure-open-ai-xxx" } + ] +} +``` + + + + +Explore deeper documentation for each feature here - [**Fallbacks**](/product/ai-gateway/fallbacks), [**Loadbalancing**](/product/ai-gateway/load-balancing), [**Retries**](/product/ai-gateway/automatic-retries), [**Timeouts**](/product/ai-gateway/request-timeouts). + + +## 4\. Observability + +Portkey automatically logs all the key details about your requests, including cost, tokens used, response time, request and response bodies, and more. + +Using Portkey, you can also send custom metadata with each of your requests to further segment your logs for better analytics. Similarly, you can also trace multiple requests to a single trace ID and filter or view them separately in Portkey logs. + +**Custom Metadata and Trace ID information is sent in** `default_headers` **.** + + + + +```python +from llama_index.llms.openai import OpenAI +from llama_index.core.llms import ChatMessage +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +config = "pc-xxxx" + +portkey = OpenAI( + api_base=PORTKEY_GATEWAY_URL, + api_key="xx" # Placeholder, no need to set + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + config=config, + metadata={ + "_user": "USER_ID", + "environment": "production", + "session_id": "1729" + } + ) +) + +messages = [ + ChatMessage(role="system", content="You are a pirate with a colorful personality"), + ChatMessage(role="user", content="What is your name"), +] + +resp = portkey.chat(messages) +print(resp) +``` + + + + +```python +from llama_index.llms.openai import OpenAI +from llama_index.core.llms import ChatMessage +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +config = "pc-xxxx" + +portkey = OpenAI( + api_base=PORTKEY_GATEWAY_URL, + api_key="xx" # Placeholder, no need to set + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + config=config, + trace_id="YOUR_TRACE_ID_HERE" + ) +) + +messages = [ + ChatMessage(role="system", content="You are a pirate with a colorful personality"), + ChatMessage(role="user", content="What is your name"), +] + +resp = portkey.chat(messages) +print(resp) +``` + + + +#### Portkey shows these details separately for each log: + + + + + +[**Check out Observability docs here.**](/product/observability) + + +## 5\. Prompt Management + +Portkey features an advanced Prompts platform tailor-made for better prompt engineering. With Portkey, you can: + +* **Store Prompts with Access Control and Version Control:** Keep all your prompts organized in a centralized location, easily track changes over time, and manage edit/view permissions for your team. +* **Parameterize Prompts**: Define variables and [mustache-approved tags](/product/prompt-library/prompt-templates#templating-engine) within your prompts, allowing for dynamic value insertion when calling LLMs. This enables greater flexibility and reusability of your prompts. +* **Experiment in a Sandbox Environment**: Quickly iterate on different LLMs and parameters to find the optimal combination for your use case, without modifying your LlamaIndex code. + +#### Here's how you can leverage Portkey's Prompt Management in your LlamaIndex application: + +1. Create your prompt template on the Portkey app, and save it to get an associated `Prompt ID` +2. Before making a Llamaindex request, render the prompt template using the Portkey SDK +3. Transform the retrieved prompt to be compatible with LlamaIndex and send the request! + +#### Example: Using a Portkey Prompt Template in LlamaIndex + +```py Portkey Prompts in LlamaIndex +import json +import os +from llama_index.llms.openai import OpenAI +from llama_index.core.llms import ChatMessage +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders, Portkey + +### Initialize Portkey client with API key + +client = Portkey(api_key=os.environ.get("PORTKEY_API_KEY")) + +### Render the prompt template with your prompt ID and variables + +prompt_template = client.prompts.render( + prompt_id="pp-prompt-id", + variables={ "movie":"Dune 2" } +).data.dict() + +config = { + "virtual_key":"GROQ_VIRTUAL_KEY", # You need to send the virtual key separately + "override_params":{ + "model":prompt_template["model"], # Set the model name based on the value in the prompt template + "temperature":prompt_template["temperature"] # Similarly, you can also set other model params + } +} + +portkey = OpenAI( + api_base=PORTKEY_GATEWAY_URL, + api_key="xx" # Placeholder, no need to set + default_headers=createHeaders( + api_key=os.environ.get("PORTKEY_API_KEY"), + config=config + ) +) + +### Transform the rendered prompt into LlamaIndex-compatible format + +messages = [ChatMessage(content=msg["content"], role=msg["role"]) for msg in prompt_template["messages"]] + +resp = portkey.chat(messages) + +print(resp) +``` + +[**Explore Prompt Management docs here**](/product/prompt-library). + +--- + +## 6\. Continuous Improvement + +Now that you know how to trace & log your Llamaindex requests to Portkey, you can also start capturing user feedback to improve your app! + +You can append qualitative as well as quantitative feedback to any `trace ID` with the `portkey.feedback.create` method: + +```py Adding Feedback +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY" +) + +feedback = portkey.feedback.create( + trace_id="YOUR_LLAMAINDEX_TRACE_ID", + value=5, # Integer between -10 and 10 + weight=1, # Optional + metadata={ + # Pass any additional context here like comments, _user and more + } +) + +print(feedback) +``` + +[**Check out the Feedback documentation for a deeper dive**](/product/observability/feedback). + + +## 7\. Security & Compliance + +When you onboard more team members to help out on your Llamaindex app - permissioning, budgeting, and access management can become a mess! Using Portkey, you can set **budget limits** on provide API keys and implement **fine-grained user roles** and **permissions** to: + +* **Control access**: Restrict team members' access to specific features, Configs, or API endpoints based on their roles and responsibilities. +* **Manage costs**: Set budget limits on API keys to prevent unexpected expenses and ensure that your LLM usage stays within your allocated budget. +* **Ensure compliance**: Implement strict security policies and audit trails to maintain compliance with industry regulations and protect sensitive data. +* **Simplify onboarding**: Streamline the onboarding process for new team members by assigning them appropriate roles and permissions, eliminating the need to share sensitive API keys or secrets. +* **Monitor usage**: Gain visibility into your team's LLM usage, track costs, and identify potential security risks or anomalies through comprehensive monitoring and reporting. + + + + + +[**Read more about Portkey's Security & Enterprise offerings here**](/product/enterprise-offering). + + +## Join Portkey Community + +Join the Portkey Discord to connect with other practitioners, discuss your LlamaIndex projects, and get help troubleshooting your queries. + +[**Link to Discord**](https://portkey.ai/community) + +For more detailed information on each feature and how to use them, please refer to the [Portkey Documentation](https://portkey.ai/docs). diff --git a/virtual_key_old/integrations/libraries/microsoft-semantic-kernel.mdx b/virtual_key_old/integrations/libraries/microsoft-semantic-kernel.mdx new file mode 100644 index 00000000..96eb25f0 --- /dev/null +++ b/virtual_key_old/integrations/libraries/microsoft-semantic-kernel.mdx @@ -0,0 +1,4 @@ +--- +title: Microsoft Semantic Kernel +url: /api-reference/inference-api/sdks/c-sharp#using-portkey-with-semantic-kernel +--- diff --git a/virtual_key_old/integrations/libraries/mindsdb.mdx b/virtual_key_old/integrations/libraries/mindsdb.mdx new file mode 100644 index 00000000..6ed0a397 --- /dev/null +++ b/virtual_key_old/integrations/libraries/mindsdb.mdx @@ -0,0 +1,80 @@ +--- +title: "MindsDb" +description: "Integrate MindsDB with Portkey to build enterprise-grade AI use-cases" +--- + +MindsDB connects to various data sources and LLMs, bringing data and AI together for easy AI automation. + +With Portkey, you can run MindsDB AI systems with 250+ LLMs and implement enterprise-grade features like [LLM observability](/product/observability), [caching](/product/ai-gateway/cache-simple-and-semantic), [advanced routing](/product/ai-gateway), and more to build production-grade MindsDB AI apps. + +## Prerequisites + +Before proceeding, ensure the following prerequisites are met: + +1. Install MindsDB locally via [Docker](https://docs.mindsdb.com/setup/self-hosted/docker) or [Docker Desktop](https://docs.mindsdb.com/setup/self-hosted/docker-desktop). +2. To use Portkey within MindsDB, install the required dependencies following [this instruction](https://docs.mindsdb.com/setup/self-hosted/docker#install-dependencies). +3. Obtain the [Portkey API key](https://app.portkey.ai) required to deploy and use Portkey within MindsDB. + +## Setup + + +* You can pass all the parameters that are supported by Portkey inside the `USING` clause. +* Check out the Portkey handler implementation [here](https://github.com/mindsdb/mindsdb/tree/main/mindsdb/integrations/handlers/portkey_handler). + + + +```sql Using Portkey's Configs +CREATE ML_ENGINE portkey_engine +FROM portkey +USING + portkey_api_key = '{PORTKEY_API_KEY}', + config = '{PORTKEY_CONFIG_ID}'; +``` +```sql Using Portkey's Virtual Keys +CREATE ML_ENGINE portkey_engine +FROM portkey +USING + portkey_api_key = '{PORTKEY_API_KEY}', + virtual_key = '{YOUR_PROVIDER_VIRTUAL_KEY}', -- choose from 200+ provider + provider = '{PROVIDER_NAME}'; --- ex- openai, anthropic, bedrock, etc. +``` + + +Portkey's configs are a powerful way to build robust AI systems. You can use them to implement [guardrails](/product/guardrails), [caching](/product/ai-gateway/cache-simple-and-semantic), [conditional routing](/product/ai-gateway/conditional-routing) and much more in your AI apps. + + + + +* You can pass all the parameters supported by Portkey Chat completions here inside the `USING` clause. + +```sql Create Portkey Model +CREATE MODEL portkey_model +PREDICT answer +USING + engine = 'portkey_engine', + temperature = 0.2; +``` + +Learn more about the supported paramteres in Chat Completions [here](https://portkey.ai/docs/api-reference/inference-api/chat). + + + + +```sql Query Portkey Model +SELECT question, answer +FROM portkey_model +WHERE question = 'Where is Stockholm located?'; +``` +Here is the output: +```md Output ++-----------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------+ +| question | answer | ++-----------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------+ +| Where is Stockholm located? | Stockholm is the capital and largest city of Sweden. It is located on Sweden's south-central east coast, where Lake Mรคlaren meets the Baltic Sea. | ++-----------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------+ +``` + + + +### Next Steps +Check out the [MindsDB Use Cases](https://docs.mindsdb.com/use-cases/overview) page to see more examples. diff --git a/virtual_key_old/integrations/libraries/mongodb.mdx b/virtual_key_old/integrations/libraries/mongodb.mdx new file mode 100644 index 00000000..19816453 --- /dev/null +++ b/virtual_key_old/integrations/libraries/mongodb.mdx @@ -0,0 +1,86 @@ +--- +title: "MongoDB" +--- + + +This integration is available for Portkey Enterprise users. + + +When deploying your AI app to production, having a robust, scalable, and high-performance logging solution is crucial. That's where Portkey and MongoDB combine โ€” Portkey's Enterprise version easily lets you store all your LLM logs on the most popular database services of your choice. This is made even easier with our ready-to-use Kubernetes Configs (Helm charts). + + +Portkey is part of MongoDB partner ecosystem to help you build & deploy your AI apps with confidence. [Learn more](https://cloud.mongodb.com/ecosystem/portkey-ai) + +## Getting Started with MongoDB Log Storage + +#### Prerequisites + +* Portkey Enterprise account +* Access to a MongoDB instance +* Kubernetes cluster + + + + +#### Configuration + +To use MongoDB for Log storage, you'll need to provide the following values in your `values.yaml` file for the Helm chart deployment: + +```yaml +MONGO_DB_CONNECTION_URL: +MONGO_DATABASE: +MONGO_COLLECTION_NAME: +MONGO_GENERATION_HOOKS_COLLECTION_NAME: +``` + +#### Authentication with PEM File + +If you're using a PEM file for authentication, follow these additional steps: + + + +In the `resources-config.yaml` file, supply PEM file details under the `data` section: + +```yaml +data: + document_db.pem: | + -----BEGIN CERTIFICATE----- + Your certificate content here + -----END CERTIFICATE----- +``` + + +In `values.yaml`, add the following configuration: +```yaml +volumes: +- name: shared-folder + configMap: + name: resource-config +volumeMounts: +- name: shared-folder + mountPath: /etc/shared/ + subPath: +``` + + +Update your `MONGO_DB_CONNECTION_URL` to use the PEM file: + +```sh +mongodb://:@?tls=true&tlsCAFile=/etc/shared/document_db.pem&retryWrites=false +``` + + + + +[**Find more details in this repo**](https://github.com/Portkey-AI/helm-chart/blob/main/helm/enterprise/README.md)**.** + + +## Cloud Deployment + +Portkey with MongoDB integration can be deployed to all major cloud providers. For cloud-specific documentation, please refer to: + +* [AWS](/product/enterprise-offering/private-cloud-deployments/aws) +* [Azure](/product/enterprise-offering/private-cloud-deployments/azure) +* [GCP](/product/enterprise-offering/private-cloud-deployments/gcp) + +[Get started with Portkey Enterprise here](/product/enterprise-offering). diff --git a/virtual_key_old/integrations/libraries/n8n.mdx b/virtual_key_old/integrations/libraries/n8n.mdx new file mode 100644 index 00000000..a515bac2 --- /dev/null +++ b/virtual_key_old/integrations/libraries/n8n.mdx @@ -0,0 +1,398 @@ +--- +title: 'n8n' +description: 'Add usage tracking, cost controls, and security guardrails to your n8n workflows' +--- +n8n is a workflow automation platform that helps you connect different systems and automate tasks. While n8n delivers robust automation capabilities, Portkey adds essential enterprise controls for production deployments: + +- **Unified AI Gateway** - Single interface for 1600+ LLMs with API key management. (not just OpenAI & Anthropic) +- **Centralized AI observability**: Real-time usage tracking for 40+ key metrics and logs for every request +- **Governance** - Real-time spend tracking, set budget limits and RBAC in your n8n setup +- **Security Guardrails** - PII detection, content filtering, and compliance controls + +This guide will walk you through integrating Portkey with n8n and setting up essential enterprise features including usage tracking, access controls, and budget management. + + + If you are an enterprise looking to use n8n in your organisation, [check out this section](#3-set-up-enterprise-governance-for-n8n). + + +# 1. Setting up Portkey +Portkey allows you to use 1600+ LLMs with your n8n setup, with minimal configuration required. Let's set up the core components in Portkey that you'll need for integration. + + + +Virtual Keys are Portkey's secure way to manage your LLM provider API keys. Think of them like disposable credit cards for your LLM API keys, providing essential controls like: +- Budget limits for API usage +- Rate limiting capabilities +- Secure API key storage + +To create a virtual key: +Go to [Virtual Keys](https://app.portkey.ai/virtual-keys) in the Portkey App. Save and copy the virtual key ID + + + + + + +Save your virtual key ID - you'll need it for the next step. + + + + + +Configs in Portkey are JSON objects that define how your requests are routed. They help with implementing features like advanced routing, fallbacks, and retries. + +We need to create a default config to route our requests to the virtual key created in Step 1. + +To create your config: +1. Go to [Configs](https://app.portkey.ai/configs) in Portkey dashboard +2. Create new config with: + ```json + { + "virtual_key": "YOUR_VIRTUAL_KEY_FROM_STEP1", + "override_params": { + "model": "gpt-4o" // Your preferred model name + } + } + + ``` +3. Save and note the Config name for the next step + + + + + + +This basic config connects to your virtual key. You can add more advanced portkey features later. + + + + + +Now create Portkey API key access point and attach the config you created in Step 2: + +1. Go to [API Keys](https://app.portkey.ai/api-keys) in Portkey and Create new API key +2. Select your config from `Step 2` +3. Generate and save your API key + + + + + + +Save your API key securely - you'll need it for n8n integration. + + + + +# 2. Integrate Portkey with n8n + +Now that you have your Portkey components set up, let's connect them to n8n. Since Portkey provides OpenAI API compatibility, integration is straightforward and requires just a few configuration steps in your n8n workflow. + + +You need your Portkey API Key from [Step 1](#Setting-up-portkey) before going further. + + +1. In your n8n workflow, add the OpenAI node where you want to use an LLM +2. Configure the OpenAI credentials with the following settings: + - API Key: Your Portkey API key from the setup + - Base URL: `https://api.portkey.ai/v1` + + + +When saving your Portkey credentials in n8n, you may encounter an "Internal Server Error" or connection warning. This happens because n8n attempts to fetch available models from the API, but Portkey doesn't expose a models endpoint in the same way OpenAI does. Despite this warning, your credentials are saved properly and will work in your workflows. + + + + + + +3. In your workflow, configure the OpenAI node to use your preferred model + - The model parameter in your config will override the default model in your n8n workflow + + + + + + +It is recommended that you define a comprehensive config in Portkey with your preferred LLM settings. This allows you to maintain all LLM settings in one place. + + + +Make sure your virtual key has sufficient budget and rate limits for your expected usage. Also use the complete model name given by the provider. + + + +You can monitor your requests and usage in the [Portkey Dashboard](https://app.portkey.ai/dashboard). + +# 3. Set Up Enterprise Governance for n8n + +**Why Enterprise Governance?** +If you are using n8n inside your organization, you need to consider several governance aspects: +- **Cost Management**: Controlling and tracking AI spending across teams +- **Access Control**: Managing which teams can use specific models +- **Usage Analytics**: Understanding how AI is being used across the organization +- **Security & Compliance**: Maintaining enterprise security standards +- **Reliability**: Ensuring consistent service across all users + +Portkey adds a comprehensive governance layer to address these enterprise needs. Let's implement these controls step by step. + +**Enterprise Implementation Guide** + + + +### Step 1: Implement Budget Controls & Rate Limits + +Virtual Keys enable granular control over LLM access at the team/department level. This helps you: +- Set up [budget limits](/product/ai-gateway/virtual-keys/budget-limits) +- Prevent unexpected usage spikes using Rate limits +- Track departmental spending + +#### Setting Up Department-Specific Controls: +1. Navigate to [Virtual Keys](https://app.portkey.ai/virtual-keys) in Portkey dashboard +2. Create new Virtual Key for each department with budget limits and rate limits +3. Configure department-specific limits + + + + + + + + +### Step 2: Define Model Access Rules + +As your AI usage scales, controlling which teams can access specific models becomes crucial. Portkey Configs provide this control layer with features like: + +#### Access Control Features: +- **Model Restrictions**: Limit access to specific models +- **Data Protection**: Implement guardrails for sensitive data +- **Reliability Controls**: Add fallbacks and retry logic + +#### Example Configuration: +Here's a basic configuration to route requests to OpenAI, specifically using GPT-4o: + +```json +{ + "strategy": { + "mode": "single" + }, + "targets": [ + { + "virtual_key": "YOUR_OPENAI_VIRTUAL_KEY", + "override_params": { + "model": "gpt-4o" + } + } + ] +} +``` + +Create your config on the [Configs page](https://app.portkey.ai/configs) in your Portkey dashboard. You'll need the config ID for connecting to n8n's setup. + + + +Configs can be updated anytime to adjust controls without affecting running applications. + + + + +### Step 3: Implement Access Controls + +Create User-specific API keys that automatically: +- Track usage per user/team with the help of virtual keys +- Apply appropriate configs to route requests +- Collect relevant metadata to filter logs +- Enforce access permissions + + +Create API keys through: +- [Portkey App](https://app.portkey.ai/) +- [API Key Management API](/api-reference/admin-api/control-plane/api-keys/create-api-key) + + +Example using Python SDK: +```python +from portkey_ai import Portkey + +portkey = Portkey(api_key="YOUR_ADMIN_API_KEY") + +api_key = portkey.api_keys.create( + name="engineering-team", + type="organisation", + workspace_id="YOUR_WORKSPACE_ID", + defaults={ + "config_id": "your-config-id", + "metadata": { + "environment": "production", + "department": "engineering" + } + }, + scopes=["logs.view", "configs.read"] +) +``` + +For detailed key management instructions, see our [API Keys documentation](/api-reference/admin-api/control-plane/api-keys/create-api-key). + + + +### Step 4: Deploy & Monitor +After distributing API keys to your team members, your enterprise-ready n8n setup is ready to go. Each team member can now use their designated API keys with appropriate access levels and budget controls. +Apply your governance setup using the integration steps from earlier sections +Monitor usage in Portkey dashboard: +- Cost tracking by department +- Model usage patterns +- Request volumes +- Error rates + + + + + +### Enterprise Features Now Available +**n8n now has:** + +- Departmental budget controls +- Model access governance +- Usage tracking & attribution +- Security guardrails +- Reliability features + + + +# Portkey Features +Now that you have enterprise-grade n8n setup, let's explore the comprehensive features Portkey provides to ensure secure, efficient, and cost-effective AI operations. + +### 1. Comprehensive Metrics +Using Portkey you can track 40+ key metrics including cost, token usage, response time, and performance across all your LLM providers in real time. You can also filter these metrics based on custom metadata that you can set in your configs. Learn more about metadata here. + + + + + +### 2. Advanced Logs +Portkey's logging dashboard provides detailed logs for every request made to your LLMs. These logs include: +- Complete request and response tracking +- Metadata tags for filtering +- Cost attribution and much more... + + + + + + + + +### 3. Unified Access to 1600+ LLMs + +You can easily switch between 1600+ LLMs. Call various LLMs such as Anthropic, Gemini, Mistral, Azure OpenAI, Google Vertex AI, AWS Bedrock, and many more by simply changing the `virtual_key` in your default `config` object. + + +### 4. Advanced Metadata Tracking +Using Portkey, you can add custom metadata to your LLM requests for detailed tracking and analytics. Use metadata tags to filter logs, track usage, and attribute costs across departments and teams. + + + + + + +### 5. Enterprise Access Management + + + +Set and manage spending limits across teams and departments. Control costs with granular budget limits and usage tracking. + + + +Enterprise-grade SSO integration with support for SAML 2.0, Okta, Azure AD, and custom providers for secure authentication. + + + +Hierarchical organization structure with workspaces, teams, and role-based access control for enterprise-scale deployments. + + + +Comprehensive access control rules and detailed audit logging for security compliance and usage tracking. + + + + +### 6. Reliability Features + + + Automatically switch to backup targets if the primary target fails. + + + Route requests to different targets based on specified conditions. + + + Distribute requests across multiple targets based on defined weights. + + + Enable caching of responses to improve performance and reduce costs. + + +Automatic retry handling with exponential backoff for failed requests + + + Set and manage budget limits across teams and departments. Control costs with granular budget limits and usage tracking. + + + + + +### 7. Advanced Guardrails + +Protect your Project's data and enhance reliability with real-time checks on LLM inputs and outputs. Leverage guardrails to: +- Prevent sensitive data leaks +- Enforce compliance with organizational policies +- PII detection and masking +- Content filtering +- Custom security rules +- Data compliance checks + + +Implement real-time protection for your LLM interactions with automatic detection and filtering of sensitive content, PII, and custom security rules. Enable comprehensive data protection while maintaining compliance with organizational policies. + + + +# FAQs + + + You can update your Virtual Key limits at any time from the Portkey dashboard:1. Go to Virtual Keys section2. Click on the Virtual Key you want to modify3. Update the budget or rate limits4. Save your changes + + + Yes! You can create multiple Virtual Keys (one for each provider) and attach them to a single config. This config can then be connected to your API key, allowing you to use multiple providers through a single API key. + + +Portkey provides several ways to track team costs: +- Create separate Virtual Keys for each team +- Use metadata tags in your configs +- Set up team-specific API keys +- Monitor usage in the analytics dashboard + + +When a team reaches their budget limit: +1. Further requests will be blocked +2. Team admins receive notifications +3. Usage statistics remain available in dashboard +4. Limits can be adjusted if needed + + +You can control model access by: +1. Setting specific models in your Portkey configs +2. Creating different Virtual Keys with different provider access +3. Using model-specific configs attached to different API keys +4. Implementing conditional routing based on workflow metadata + + + + +# Next Steps + + **Join our Community** +- [Discord Community](https://portkey.sh/discord-report) +- [GitHub Repository](https://github.com/Portkey-AI) + + +For enterprise support and custom features, contact our [enterprise team](https://calendly.com/portkey-ai). + diff --git a/virtual_key_old/integrations/libraries/openai-compatible.mdx b/virtual_key_old/integrations/libraries/openai-compatible.mdx new file mode 100644 index 00000000..e4f25c06 --- /dev/null +++ b/virtual_key_old/integrations/libraries/openai-compatible.mdx @@ -0,0 +1,376 @@ +--- +title: "Portkey with Any OpenAI Compatible Project" +description: "Learn how to integrate Portkey's enterprise features with any OpenAI Compliant project for enhanced observability, reliability and governance." +--- + +Portkey enhances any OpenAI API compliant project by adding enterprise-grade features like observability, reliability, rate limiting, access control, and budget managementโ€”all without requiring code changes. + + +It is a drop-in replacement for your existing OpenAI-compatible applications. This guide explains how to integrate Portkey with minimal changes to your project settings. + +While OpenAI (or any other provider) provides an API for AI model access. Commercial usage often require additional features like: + +- **Advanced Observability**: Real-time usage tracking for 40+ key metrics and logs for every request +- **Unified AI Gateway** - Single interface for 250+ LLMs with API key management +- **Governance** - Real-time spend tracking, set budget limits and RBAC in your AI systems +- **Security Guardrails** - PII detection, content filtering, and compliance controls + +# 1. Getting Started with Portkey +Portkey allows you to use 250+ LLMs with your Project setup, with minimal configuration required. Let's set up the core components in Portkey that you'll need for integration. + + + +Virtual Keys are Portkey's secure way to manage your LLM provider API keys. Think of them like disposable credit cards for your LLM API keys, providing essential controls like: +- Budget limits for API usage +- Rate limiting capabilities +- Secure API key storage + +To create a virtual key: +Go to [Virtual Keys](https://app.portkey.ai/virtual-keys) in the Portkey App. Save and copy the virtual key ID + + + + + + +Save your virtual key ID - you'll need it for the next step. + + + + + +Configs in Portkey are JSON objects that define how your requests are routed. They help with implementing features like advanced routing, fallbacks, and retries. + +We need to create a default config to route our requests to the virtual key created in Step 1. + +To create your config: +1. Go to [Configs](https://app.portkey.ai/configs) in Portkey dashboard +2. Create new config with: + ```json + { + "virtual_key": "YOUR_VIRTUAL_KEY_FROM_STEP1", + } + ``` +3. Save and note the Config name for the next step + + + + + + +This basic config connects to your virtual key. You can add more advanced portkey features later. + + + + + +Now create Portkey API key access point and attach the config you created in Step 2: + +1. Go to [API Keys](https://app.portkey.ai/api-keys) in Portkey and Create new API key +2. Select your config from `Step 2` +3. Generate and save your API key + + + + + + +Save your API key securely - you'll need it for Chat UI integration. + + + + + +# 2. Integrating Portkey with Your Project +You can integrate Portkey with any OpenAI API-compatible project through a simple configuration change. This integration enables advanced monitoring, security features, and analytics for your LLM applications. Here's how you do it: + + +1. **Locate LLM Settings** +Navigate to your project's LLM settings page and find the OpenAI configuration section (usually labeled 'OpenAI-Compatible' or 'Generic OpenAI')." + +2. **Configure Base URL** + Set the base URL to: + ``` + https://api.portkey.ai/v1 + ``` + +3. **Add API Key** + Enter your Portkey API key in the appropriate field. You can generate this key from your Portkey dashboard under API Keys section. + +4. **Configure Model Settings** + If your integration allows direct model configuration, you can specify it in the LLM settings. Otherwise, create a configuration object: + + ```json + { + "virtual_key": "", + "override_params": { + "model": "gpt-4o" // Specify your desired model + } + } + ``` + +# 3. Set Up Enterprise Governance for your Project + +**Why Enterprise Governance?** +When you are using any AI tool in an enterprise setting, you need to consider several governance aspects: +- **Cost Management**: Controlling and tracking AI spending across teams +- **Access Control**: Managing which teams can use specific models +- **Usage Analytics**: Understanding how AI is being used across the organization +- **Security & Compliance**: Maintaining enterprise security standards +- **Reliability**: Ensuring consistent service across all users + +Portkey adds a comprehensive governance layer to address these enterprise needs. Let's implement these controls step by step. + +### Enterprise Implementation Guide + + + +### Step 1: Implement Budget Controls & Rate Limits + +Virtual Keys enable granular control over LLM access at the team/department level. This helps you: +- Set up [budget limits](/product/ai-gateway/virtual-keys/budget-limits) +- Prevent unexpected usage spikes using Rate limits +- Track departmental spending + +#### Setting Up Department-Specific Controls: +1. Navigate to [Virtual Keys](https://app.portkey.ai/virtual-keys) in Portkey dashboard +2. Create new Virtual Key for each department with budget limits and rate limits +3. Configure department-specific limits + + + + + + + + +### Step 2: Define Model Access Rules + +As your Project scales, controlling which teams can access specific models becomes crucial. Portkey Configs provide this control layer with features like: + +#### Access Control Features: +- **Model Restrictions**: Limit access to specific models +- **Data Protection**: Implement guardrails for sensitive data +- **Reliability Controls**: Add fallbacks and retry logic + +#### Example Configuration: +Here's a basic configuration to route requests to OpenAI, specifically using GPT-4o: + +```json +{ + "strategy": { + "mode": "single" + }, + "targets": [ + { + "virtual_key": "YOUR_OPENAI_VIRTUAL_KEY", + "override_params": { + "model": "gpt-4o" + } + } + ] +} +``` + +Create your config on the [Configs page](https://app.portkey.ai/configs) in your Portkey dashboard. You'll need the config ID for connecting to your Project's setup. + + + +Configs can be updated anytime to adjust controls without affecting running applications. + + + + +### Step 3: Implement Access Controls + +Create User-specific API keys that automatically: +- Track usage per user/team with the help of virtual keys +- Apply appropriate configs to route requests +- Collect relevant metadata to filter logs +- Enforce access permissions + + +Create API keys through: +- [Portkey App](https://app.portkey.ai/) +- [API Key Management API](/api-reference/admin-api/control-plane/api-keys/create-api-key) + + +Example using Python SDK: +```python +from portkey_ai import Portkey + +portkey = Portkey(api_key="YOUR_ADMIN_API_KEY") + +api_key = portkey.api_keys.create( + name="engineering-team", + type="organisation", + workspace_id="YOUR_WORKSPACE_ID", + defaults={ + "config_id": "your-config-id", + "metadata": { + "environment": "production", + "department": "engineering" + } + }, + scopes=["logs.view", "configs.read"] +) +``` + +For detailed key management instructions, see our [API Keys documentation](/api-reference/admin-api/control-plane/api-keys/create-api-key). + + + +### Step 4: Deploy & Monitor +After distributing API keys to your team members, your enterprise-ready Project setup is ready to go. Each team member can now use their designated API keys with appropriate access levels and budget controls. +Apply your governance setup using the integration steps from earlier sections +Monitor usage in Portkey dashboard: +- Cost tracking by department +- Model usage patterns +- Request volumes +- Error rates + + + + + +### Enterprise Features Now Available +**Your Project now has:** + +- Departmental budget controls +- Model access governance +- Usage tracking & attribution +- Security guardrails +- Reliability features + + + +# Portkey Features +Now that you have set up your enterprise-grade Project environment, let's explore the comprehensive features Portkey provides to ensure secure, efficient, and cost-effective AI operations. + +### 1. Comprehensive Metrics +Using Portkey you can track 40+ key metrics including cost, token usage, response time, and performance across all your LLM providers in real time. You can also filter these metrics based on custom metadata that you can set in your configs. Learn more about metadata here. + + + + + +### 2. Advanced Logs +Portkey's logging dashboard provides detailed logs for every request made to your LLMs. These logs include: +- Complete request and response tracking +- Metadata tags for filtering +- Cost attribution and much more... + + + + + + + + +### 3. Unified Access to 250+ LLMs + +You can easily switch between 250+ LLMs. Call various LLMs such as Anthropic, Gemini, Mistral, Azure OpenAI, Google Vertex AI, AWS Bedrock, and many more by simply changing the `virtual key` in your default `config` object. + + +### 4. Advanced Metadata Tracking +Using Portkey, you can add custom metadata to your LLM requests for detailed tracking and analytics. Use metadata tags to filter logs, track usage, and attribute costs across departments and teams. + + + + + + +### 5. Enterprise Access Management + + + +Set and manage spending limits across teams and departments. Control costs with granular budget limits and usage tracking. + + + +Enterprise-grade SSO integration with support for SAML 2.0, Okta, Azure AD, and custom providers for secure authentication. + + + +Hierarchical organization structure with workspaces, teams, and role-based access control for enterprise-scale deployments. + + + +Comprehensive access control rules and detailed audit logging for security compliance and usage tracking. + + + + +### 6. Reliability Features + + + Automatically switch to backup targets if the primary target fails. + + + Route requests to different targets based on specified conditions. + + + Distribute requests across multiple targets based on defined weights. + + + Enable caching of responses to improve performance and reduce costs. + + +Automatic retry handling with exponential backoff for failed requests + + + Set and manage budget limits across teams and departments. Control costs with granular budget limits and usage tracking. + + + + + +### 7. Advanced Guardrails + +Protect your Project's data and enhance reliability with real-time checks on LLM inputs and outputs. Leverage guardrails to: +- Prevent sensitive data leaks +- Enforce compliance with organizational policies +- PII detection and masking +- Content filtering +- Custom security rules +- Data compliance checks + + +Implement real-time protection for your LLM interactions with automatic detection and filtering of sensitive content, PII, and custom security rules. Enable comprehensive data protection while maintaining compliance with organizational policies. + + + +# FAQs + + + You can update your Virtual Key limits at any time from the Portkey dashboard:1. Go to Virtual Keys section2. Click on the Virtual Key you want to modify3. Update the budget or rate limits4. Save your changes + + + Yes! You can create multiple Virtual Keys (one for each provider) and attach them to a single config. This config can then be connected to your API key, allowing you to use multiple providers through a single API key. + + +Portkey provides several ways to track team costs: +- Create separate Virtual Keys for each team +- Use metadata tags in your configs +- Set up team-specific API keys +- Monitor usage in the analytics dashboard + + +When a team reaches their budget limit: +1. Further requests will be blocked +2. Team admins receive notifications +3. Usage statistics remain available in dashboard +4. Limits can be adjusted if needed + + + + +# Next Steps + + **Join our Community** +- [Discord Community](https://portkey.sh/discord-report) +- [GitHub Repository](https://github.com/Portkey-AI) + + +For enterprise support and custom features, contact our [enterprise team](https://portkey.sh/chat-ui-docs). + diff --git a/virtual_key_old/integrations/libraries/openwebui.mdx b/virtual_key_old/integrations/libraries/openwebui.mdx new file mode 100644 index 00000000..61006053 --- /dev/null +++ b/virtual_key_old/integrations/libraries/openwebui.mdx @@ -0,0 +1,379 @@ +--- +title: 'Open WebUI' +description: 'Cost tracking, observability, and more for Open WebUI' +--- +This guide will help you implement enterprise-grade security, observability, and governance for OpenWebUI using Portkey. While OpenWebUI supports various provider plugins, Portkey provides a unified interface for all your LLM providers, offering comprehensive features for model management, cost tracking, observability, and metadata logging. + +For IT administrators deploying centralized instances of OpenWebUI, Portkey enables essential enterprise features including usage tracking, access controls, and budget management. Let's walk through implementing these features step by step. + +# Understanding the Implementation +When implementing Portkey with OpenWebUI in your organization, we'll follow these key steps: + +1. Basic OpenWebUI integration with Portkey +2. Setting up organizational governance using Virtual Keys and Configs +3. Managing user access and permissions + + +If you're an individual user just looking to use Portkey with OpenWebUI, you only need to complete Steps 1 and 2 to get started. + + +## 1. Basic Integration + +Let's start by integrating Portkey with your OpenWebUI installation. This integration uses OpenWebUI's pipeline functionality to route all requests through Portkey's Platform. + +**Installing the Portkey Plugin** +1. Start your OpenWebUI server +2. Navigate to `Workspace` and then go to the `Functions` section +3. Click on the `+` button in UI +4. Copy and paste the [Portkey plugin](https://openwebui.com/f/nath/portkey/) code + + + + + +## 2. Setting Up Portkey Pipeline + +To use OpenWebUI with Portkey, you'll need to configure three key components: + +**Portkey API Key**: +Get your Portkey API key from [here](https://app.portkey.ai/api-keys). You'll need this for authentication with Portkey's services. + +**Virtual Keys**: +Virtual Keys are Portkey's secure way to manage your LLM provider API keys. They provide essential controls like: +- Budget limits for API usage +- Rate limiting capabilities +- Secure API key storage + + +Craeate a [Virtual Key](https://app.portkey.ai/virtual-keys) in your Portkey dashboard and save it for future use. + +For detailed information on budget limits, [refer to this documentation](/product/ai-gateway/virtual-keys/budget-limits) + +**Using Configs (Optional)**: +Configs in Portkey enhance your implementation with features like advanced routing, fallbacks, and retries. Here's a simple config example that implements 5 retry attempts on server errors: + +```json +{ + "retry": { + "attempts": 5 + }, + "virtual_key": "virtual-key-xxx" +} +``` +You can create and store these configs in Portkey's config library. This can later be accessed on using the Config Slug in Open WebUI. + +Configs are highly flexible and can be customized for various use cases. Learn more in our [Configs documentation](https://docs.portkey.ai/configs). + + +## 3. Configure Pipeline Variables + +The pipeline setup involves configuring both credentials and model access in OpenWebUI. + +**Credentials Setup**: +1. In OpenWebUI, navigate to `Workspace` โ†’ `Functions` +2. Click the `Valves` button to open the configuration interface +3. Add the following credentials: + - Your Portkey API Key + - Config slug (if using Configs) + - Base URL (only needed for Open Source Gateway users) + + + + + +**Model Configuration** +1. In the Functions section, click the `...` button and select `Edit` +2. Find the virtual keys JSON object in the Portkey function code +3. Update it with your virtual keys: + ```json + "virtual_keys": { + "openai": "YOUR_OPENAI_VIRTUAL_KEY", + "anthropic": "YOUR_ANTHROPIC_VIRTUAL_KEY" + } + ``` +4. Configure model names in the pipe function in this format: + ```json + { + "id": "provider_slug_from_portkey/model_id_from_provider", // syntax for ID + "name": "provider_slug_from_portkey/model_id_from_provider", // for easier navigation + } + + ``` + Example: +```json + { + "id": "openai/gpt-4o", + "name": "openai/gpt-4o", + } + ``` + + Make sure you use the correct provider slug from Portke docs. Ex: `perplexity-ai` is correct. `perplexity` is wrong +5. Save your changes + + + +# 4. Set Up Enterprise Governance for OpenWebUI + +**Why Enterprise Governance?** +If you are using OpenWeb UI inside your orgnaization, you need to consider several governance aspects: +- **Cost Management**: Controlling and tracking AI spending across teams +- **Access Control**: Managing which teams can use specific models +- **Usage Analytics**: Understanding how AI is being used across the organization +- **Security & Compliance**: Maintaining enterprise security standards +- **Reliability**: Ensuring consistent service across all users + +Portkey adds a comprehensive governance layer to address these enterprise needs. Let's implement these controls step by step. + +**Enterprise Implementation Guide** + + + +### Step 1: Implement Budget Controls & Rate Limits + +Virtual Keys enable granular control over LLM access at the team/department level. This helps you: +- Set up [budget limits](/product/ai-gateway/virtual-keys/budget-limits) +- Prevent unexpected usage spikes using Rate limits +- Track departmental spending + +#### Setting Up Department-Specific Controls: +1. Navigate to [Virtual Keys](https://app.portkey.ai/virtual-keys) in Portkey dashboard +2. Create new Virtual Key for each department with budget limits and rate limits +3. Configure department-specific limits + + + + + + + + +### Step 2: Define Model Access Rules + +As your AI usage scales, controlling which teams can access specific models becomes crucial. Portkey Configs provide this control layer with features like: + +#### Access Control Features: +- **Model Restrictions**: Limit access to specific models +- **Data Protection**: Implement guardrails for sensitive data +- **Reliability Controls**: Add fallbacks and retry logic + +#### Example Configuration: +Here's a basic configuration to route requests to OpenAI, specifically using GPT-4o: + +```json +{ + "strategy": { + "mode": "single" + }, + "targets": [ + { + "virtual_key": "YOUR_OPENAI_VIRTUAL_KEY", + "override_params": { + "model": "gpt-4o" + } + } + ] +} +``` + +Create your config on the [Configs page](https://app.portkey.ai/configs) in your Portkey dashboard. You'll need the config ID for connecting to OpenWeb UI's setup. + + + +Configs can be updated anytime to adjust controls without affecting running applications. + + + + +### Step 3: Implement Access Controls + +Create User-specific API keys that automatically: +- Track usage per user/team with the help of virtual keys +- Apply appropriate configs to route requests +- Collect relevant metadata to filter logs +- Enforce access permissions + + +Create API keys through: +- [Portkey App](https://app.portkey.ai/) +- [API Key Management API](/api-reference/admin-api/control-plane/api-keys/create-api-key) + + +Example using Python SDK: +```python +from portkey_ai import Portkey + +portkey = Portkey(api_key="YOUR_ADMIN_API_KEY") + +api_key = portkey.api_keys.create( + name="engineering-team", + type="organisation", + workspace_id="YOUR_WORKSPACE_ID", + defaults={ + "config_id": "your-config-id", + "metadata": { + "environment": "production", + "department": "engineering" + } + }, + scopes=["logs.view", "configs.read"] +) +``` + +For detailed key management instructions, see our [API Keys documentation](/api-reference/admin-api/control-plane/api-keys/create-api-key). + + + +### Step 4: Deploy & Monitor +After distributing API keys to your team members, your enterprise-ready OpenWeb UI setup is ready to go. Each team member can now use their designated API keys with appropriate access levels and budget controls. +Apply your governance setup using the integration steps from earlier sections +Monitor usage in Portkey dashboard: +- Cost tracking by department +- Model usage patterns +- Request volumes +- Error rates + + + + + +### Enterprise Features Now Available +**OpenWeb UI now has:** + +- Departmental budget controls +- Model access governance +- Usage tracking & attribution +- Security guardrails +- Reliability features + + + +# Portkey Features +Now that you have enterprise-grade Zed setup, let's explore the comprehensive features Portkey provides to ensure secure, efficient, and cost-effective AI operations. + +### 1. Comprehensive Metrics +Using Portkey you can track 40+ key metrics including cost, token usage, response time, and performance across all your LLM providers in real time. You can also filter these metrics based on custom metadata that you can set in your configs. Learn more about metadata here. + + + + + +### 2. Advanced Logs +Portkey's logging dashboard provides detailed logs for every request made to your LLMs. These logs include: +- Complete request and response tracking +- Metadata tags for filtering +- Cost attribution and much more... + + + + + + + + +### 3. Unified Access to 1600+ LLMs + +You can easily switch between 1600+ LLMs. Call various LLMs such as Anthropic, Gemini, Mistral, Azure OpenAI, Google Vertex AI, AWS Bedrock, and many more by simply changing the `virtual key` in your default `config` object. + + +### 4. Advanced Metadata Tracking +Using Portkey, you can add custom metadata to your LLM requests for detailed tracking and analytics. Use metadata tags to filter logs, track usage, and attribute costs across departments and teams. + + + + + + +### 5. Enterprise Access Management + + + +Set and manage spending limits across teams and departments. Control costs with granular budget limits and usage tracking. + + + +Enterprise-grade SSO integration with support for SAML 2.0, Okta, Azure AD, and custom providers for secure authentication. + + + +Hierarchical organization structure with workspaces, teams, and role-based access control for enterprise-scale deployments. + + + +Comprehensive access control rules and detailed audit logging for security compliance and usage tracking. + + + + +### 6. Reliability Features + + + Automatically switch to backup targets if the primary target fails. + + + Route requests to different targets based on specified conditions. + + + Distribute requests across multiple targets based on defined weights. + + + Enable caching of responses to improve performance and reduce costs. + + +Automatic retry handling with exponential backoff for failed requests + + + Set and manage budget limits across teams and departments. Control costs with granular budget limits and usage tracking. + + + + + +### 7. Advanced Guardrails + +Protect your Project's data and enhance reliability with real-time checks on LLM inputs and outputs. Leverage guardrails to: +- Prevent sensitive data leaks +- Enforce compliance with organizational policies +- PII detection and masking +- Content filtering +- Custom security rules +- Data compliance checks + + +Implement real-time protection for your LLM interactions with automatic detection and filtering of sensitive content, PII, and custom security rules. Enable comprehensive data protection while maintaining compliance with organizational policies. + + + +# FAQs + + + You can update your Virtual Key limits at any time from the Portkey dashboard:1. Go to Virtual Keys section2. Click on the Virtual Key you want to modify3. Update the budget or rate limits4. Save your changes + + + Yes! You can create multiple Virtual Keys (one for each provider) and attach them to a single config. This config can then be connected to your API key, allowing you to use multiple providers through a single API key. + + +Portkey provides several ways to track team costs: +- Create separate Virtual Keys for each team +- Use metadata tags in your configs +- Set up team-specific API keys +- Monitor usage in the analytics dashboard + + +When a team reaches their budget limit: +1. Further requests will be blocked +2. Team admins receive notifications +3. Usage statistics remain available in dashboard +4. Limits can be adjusted if needed + + + + +# Next Steps + + **Join our Community** +- [Discord Community](https://portkey.sh/discord-report) +- [GitHub Repository](https://github.com/Portkey-AI) + + +For enterprise support and custom features, contact our [enterprise team](https://calendly.com/portkey-ai). + diff --git a/virtual_key_old/integrations/libraries/promptfoo.mdx b/virtual_key_old/integrations/libraries/promptfoo.mdx new file mode 100644 index 00000000..724feadc --- /dev/null +++ b/virtual_key_old/integrations/libraries/promptfoo.mdx @@ -0,0 +1,266 @@ +--- +title: "Promptfoo" +description: Portkey brings advanced **AI gateway** capabilities, full-stack **observability**, and **prompt management** + **versioning** to your **Promptfoo** projects. This document provides an overview of how to leverage the strengths of both the platforms to streamline your AI development workflow. +--- + +[**promptfoo**](https://promptfoo.dev/docs/intro) is an open source library (and CLI) for evaluating LLM output quality. + +#### By using Portkey with Promptfoo you can: + +1. [Manage, version, and collaborate on various prompts with Portkey and easily call them in Promptfoo](/integrations/libraries/promptfoo#id-1.-reference-prompts-from-portkey-in-promptfoo) +2. [Run Promptfoo on 200+ LLMs, including locally or privately hosted LLMs](/integrations/libraries/promptfoo#id-2.-route-to-anthropic-google-groq-and-more) +3. [Log all requests, segment them as needed with custom metadata, and get granular cost, performance metrics for all Promptfoo runs](/integrations/libraries/promptfoo#id-3.-segment-requests-view-cost-and-performance-metrics) +4. [Avoid Promptfoo rate limits & leverage cache](/integrations/libraries/promptfoo#id-4.-avoid-promptfoo-rate-limits-and-leverage-cache) + +Letโ€™s see how these work! + +## 1\. Reference Prompts from Portkey in Promptfoo + +1. Set the `PORTKEY_API_KEY` environment variable in your Promptfoo project +2. In your configuration YAML, use the `portkey://` prefix for your prompts, followed by your Portkey prompt ID. + +For example: + +```yaml +prompts: + - portkey://pp-test-promp-669f48 +providers: + - openai:gpt-3.5-turbo-0613 +tests: + - vars: + topic: ... +``` + +Variables from your Promptfoo test cases will be automatically plugged into the Portkey prompt as variables. The resulting prompt will be rendered and returned to promptfoo, and used as the prompt for the test case. + + + Note that promptfoo does not follow the temperature, model, and other parameters set in Portkey. You must set them in the providers configuration yourself. + + +--- + +## 2\. Route to Anthropic, Google, Groq, and More + +1. Set the `PORTKEY_API_KEY` environment variable +2. While adding the provider in your config YAML, set the model name with `portkey` prefix (like `portkey:gpt-4o`) +3. And in the `config`param, set the relevant provider for the above chosen model with `portkeyProvider` (like `portkeyProvider:openai`) + +### For Example, to Call OpenAI + +```yaml +providers: + id: portkey:gpt-4o + config: + portkeyProvider: openai +``` + +That's it! With this, all your Promptfoo calls will now start showing up on your Portkey dashboard. + +### Let's now call `Anthropic`, `Google`, `Groq`, `Ollama` + + + + + +```yaml +providers: + id: portkey:claude-3-opus20240229 + config: + portkeyProvider: anthropic + # You can also add your Anthropic API key to Portkey and pass the virtual key here + portkeyVirtualKey: ANTHROPIC_VIRTUAL_KEY +``` + + + + +```yaml +providers: + id: portkey:gemini-1.5-flash-latest + config: + portkeyProvider: google + # You can also add your Gemini API key to Portkey and pass the virtual key here + portkeyVirtualKey: GEMINI_VIRTUAL_KEY +``` + + + +```yaml +providers: + id: portkey:llama3-8b-8192 + config: + portkeyProvider: groq + # You can also add your Groq API key to Portkey and pass the virtual key here + portkeyVirtualKey: GROQ_VIRTUAL_KEY +``` + + + +```yaml +providers: + id: portkey:llama3 + config: + portkeyProvider: ollama + portkeyCustomHost: YOUR_OLLAMA_NGROK_URL +``` + + + +### Examples for `Azure OpenAI`, `AWS Bedrock`, `Google Vertex AI` + + + + #### Using [Virtual Keys](/integrations/llms/azure-openai#id-2.-initialize-portkey-with-the-virtual-key) + +```yaml +providers: + id: portkey:xxx + config: + portkeyVirtualKey: YOUR_PORTKEY_AZURE_OPENAI_VIRTUAL_KEY +``` +#### Without Using Virtual Keys + +First, set the `AZURE_OPENAI_API_KEY` environment variable. + +```yaml +providers: + id: portkey:xxx + config: + portkeyProvider: azure-openai + portkeyAzureResourceName: AZURE_RESOURCE_NAME + portkeyAzureDeploymentId: AZURE_DEPLOYMENT_NAME + portkeyAzureApiVersion: AZURE_API_VERSION +``` +#### Using Client Credentials (JSON Web Token) + +You can generate a JSON web token for your client creds, and add it to the `AZURE_OPENAI_API_KEY` environment variable. + +```yaml +providers: + id: portkey:xxx + config: + portkeyProvider: azure-openai + portkeyAzureResourceName: AZURE_RESOURCE_NAME + portkeyAzureDeploymentId: AZURE_DEPLOYMENT_NAME + portkeyAzureApiVersion: AZURE_API_VERSION + # Pass your JSON Web Token with AZURE_OPENAI_API_KEY env var + portkeyForwardHeaders: [ "Authorization" ] +``` + + +#### Using [Virtual Keys](/integrations/llms/aws-bedrock#id-2.-initialize-portkey-with-the-virtual-key) + +```yaml +providers: + id: portkey:anthropic.claude-3-sonnet-20240229-v1:0 + config: + portkeyVirtualKey: YOUR_PORTKEY_AWS_BEDROCK_VIRTUAL_KEY + # If you're using AWS Security Token Service, you can set it here + awsSessionToken: "AWS_SESSION_TOKEN" +``` + +#### Without Using Virtual Keys + +First, set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` env vars. + +```yaml +providers: + id: portkey:anthropic.claude-3-sonnet-20240229-v1:0 + config: + portkeyProvider: bedrock + portkeyAwsRegion: "us-east-1" + portkeyAwsSecretAccessKey: ${AWS_SECRET_ACCESS_KEY} + portkeyAwsAccessKeyId: ${AWS_ACCESS_KEY_ID} + # You can also set AWS STS (Security Token Service) + awsSessionToken: "AWS_SESSION_TOKEN" +``` + + + +#### Using [Virtual Keys](/integrations/llms/vertex-ai#id-2.-initialize-portkey-with-the-virtual-key) + +Set your Vertex AI access token with the `VERTEX_API_KEY` env var, and pass the rest of your Vertex AI details with Portkey virtual key. + +```yaml +providers: + id: portkey:gemini-1.5-flash-latest + config: + portkeyVirtualKey: YOUR_PORTKEY_GOOGLE_VERTEX_AI_VIRTUAL_KEY +``` + +#### Without Using Virtual Keys + +First, set the `VERTEX_API_KEY`, `VERTEX_PROJECT_ID`, `VERTEX_REGION` env vars. + +```yaml +providers: + id: portkey:xxx + config: + portkeyProvider: vertex-ai + portkeyVertexProjectId: ${VERTEX_PROJECT_ID} + portkeyVertexRegion: ${VERTEX_REGION} +``` + + + + +--- + +## 3\. Segment Requests, View Cost & Performance Metrics + +Portkey automatically logs all the key details about your requests, including cost, tokens used, response time, request and response bodies, and more. + +Using Portkey, you can also send custom metadata with each of your requests to further segment your logs for better analytics. Similarly, you can also trace multiple requests to a single trace ID and filter or view them separately in Portkey logs. + +```yaml +providers: + id: portkey:claude-3-opus20240229 + config: + portkeyVirtualKey: ANTHROPIC_VIRTUAL_KEY + portkeyMetadata: + team: alpha9 + prompt: classification + portkeyTraceId: run_1 +``` + +You can filter or group data by these metadata keys on Portkey dashboards. + + + + + +--- + +## 4\. Avoid Promptfoo Rate Limits & Leverage Cache + +Since promptfoo can make a lot of calls very quickly, you can use a loadbalanced config in Portkey with cache enabled. You can pass the config header similar to virtual keys in promptfoo. + +Here's a sample Config that you can save in the Portkey UI and get a respective config slug: + +```json +{ + "cache": { "mode": "simple" }, + "strategy": { "mode": "loadbalance" }, + "targets": [ + { "virtual_key": "ACCOUNT_ONE" }, + { "virtual_key": "ACCOUNT_TWO" }, + { "virtual_key": "ACCOUNT_THREE" } + ] +} +``` + +And then we can just add the saved Config's slug in the YAML: + +```yaml +providers: + id: portkey:claude-3-opus20240229 + config: + portkeyVirtualKey: ANTHROPIC_VIRTUAL_KEY + portkeyConfig: PORTKEY_CONFIG_SLUG +``` + +--- + +## \[Roadmap\] View the Results of Promptfoo Evals in Portkey + +Weโ€™re building support to view the eval results of promptfoo in Portkey that will let you view the results of promptfoo evals within the feedback section of Portkey. diff --git a/virtual_key_old/integrations/libraries/roo-code.mdx b/virtual_key_old/integrations/libraries/roo-code.mdx new file mode 100644 index 00000000..0cb07a6a --- /dev/null +++ b/virtual_key_old/integrations/libraries/roo-code.mdx @@ -0,0 +1,416 @@ +--- +title: 'Roo Code' +description: 'Add enterprise-grade observability, cost tracking, and governance to your Roo AI coding assistant' +--- + +Roo is an AI coding assistant that integrates directly into your VS Code environment, providing autonomous coding capabilities. While Roo offers powerful AI assistance for development tasks, Portkey adds essential enterprise controls for production deployments: + +- **Unified AI Gateway** - Single interface for 1600+ LLMs with API key management +- **Centralized AI observability**: Real-time usage tracking for 40+ key metrics and logs for every request +- **Governance** - Real-time spend tracking, set budget limits and RBAC in your Roo setup +- **Security Guardrails** - PII detection, content filtering, and compliance controls + +This guide will walk you through integrating Portkey with Roo and setting up essential enterprise features including usage tracking, access controls, and budget management. + + + If you are an enterprise looking to standardize Roo usage across your development teams, [check out this section](#3-set-up-enterprise-governance-for-roo). + + +# 1. Setting up Portkey +Portkey allows you to use 250+ LLMs with your Roo setup, with minimal configuration required. Let's set up the core components in Portkey that you'll need for integration. + + + +Virtual Keys are Portkey's secure way to manage your LLM provider API keys. Think of them like disposable credit cards for your LLM API keys, providing essential controls like: +- Budget limits for API usage +- Rate limiting capabilities +- Secure API key storage + +To create a virtual key: +Go to [Virtual Keys](https://app.portkey.ai/virtual-keys) in the Portkey App. Save and copy the virtual key ID + + + + + + +Save your virtual key ID - you'll need it for the next step. + + + + + +Configs in Portkey are JSON objects that define how your requests are routed. They help with implementing features like advanced routing, fallbacks, and retries. + +We need to create a default config to route our requests to the virtual key created in Step 1. + +To create your config: +1. Go to [Configs](https://app.portkey.ai/configs) in Portkey dashboard +2. Create new config with: + ```json + { + "virtual_key": "YOUR_VIRTUAL_KEY_FROM_STEP1", + "override_params": { + "model": "gpt-4o" // Your preferred model name + } + } + ``` +3. Save and note the Config name for the next step + + + + + + +This basic config connects to your virtual key. You can add more advanced portkey features later. + + + + + +Now create Portkey API key access point and attach the config you created in Step 2: + +1. Go to [API Keys](https://app.portkey.ai/api-keys) in Portkey and Create new API key +2. Select your config from `Step 2` +3. Generate and save your API key + + + + + + +Save your API key securely - you'll need it for Roo integration. + + + + +# 2. Integrate Portkey with Roo + +Now that you have your Portkey components set up, let's connect them to Roo. Since Portkey provides OpenAI API compatibility, integration is straightforward and requires just a few configuration steps in your VS Code settings. + + +You need your Portkey API Key from [Step 1](#1-setting-up-portkey) before going further. + + +### Opening Roo Settings + +1. Open VS Code with Roo installed +2. Click on the Roo icon in your github activity bar +3. Click on the settings gear icon โš™๏ธ in the Roo tab + +### Method 1: Using Default Config (Recommended) + +This method uses the default config you created in Portkey, making it easier to manage model settings centrally. + +1. In the Roo settings, navigate to **Providers** +2. Configure the following settings: + - **API Provider**: Select `OpenAI Compatible` + - **Base URL**: `https://api.portkey.ai/v1` + - **OpenAI API Key**: Your Portkey API key from the setup + - **Model**: `dummy` (since the model is defined in your Portkey config) + + +Using a default config with `override_params` is recommended as it allows you to manage all model settings centrally in Portkey, reducing maintenance overhead. + + +### Method 2: Using Custom Headers + +If you prefer more direct control or need to use multiple providers dynamically, you can pass Portkey headers directly: + +1. Configure the basic settings as in Method 1: + - **API Provider**: `OpenAI Compatible` + - **Base URL**: `https://api.portkey.ai/v1` + - **API Key**: Your Portkey API key + - **Model ID**: Your desired model (e.g., `gpt-4o`, `claude-3-opus-20240229`) + +2. Add custom headers by clicking the `+` button in the Custom Headers section: + ``` + x-portkey-virtual-key: YOUR_VIRTUAL_KEY + ``` + + Optional headers: + ``` + x-portkey-provider: google // Only if not using virtual key + x-portkey-config: YOUR_CONFIG_ID // For additional config + ``` + + +Custom headers give you flexibility but require updating headers in Roo whenever you want to change providers or models. + + +You can now use Roo with all of Portkey's enterprise features enabled. Monitor your requests and usage in the [Portkey Dashboard](https://app.portkey.ai/dashboard). + +# 3. Set Up Enterprise Governance for Roo + +**Why Enterprise Governance?** +When deploying Roo across development teams in your organization, you need to consider several governance aspects: +- **Cost Management**: Controlling and tracking AI spending across developers +- **Access Control**: Managing which teams can use specific models +- **Usage Analytics**: Understanding how AI is being used in development workflows +- **Security & Compliance**: Protecting sensitive code and maintaining security standards +- **Reliability**: Ensuring consistent service across all developers + +Portkey adds a comprehensive governance layer to address these enterprise needs. Let's implement these controls step by step. + +**Enterprise Implementation Guide** + + + +### Step 1: Implement Budget Controls & Rate Limits + +Virtual Keys enable granular control over LLM access at the team/developer level. This helps you: +- Set up [budget limits](/product/ai-gateway/virtual-keys/budget-limits) per developer +- Prevent unexpected usage spikes using Rate limits +- Track individual developer spending + +#### Setting Up Developer-Specific Controls: +1. Navigate to [Virtual Keys](https://app.portkey.ai/virtual-keys) in Portkey dashboard +2. Create new Virtual Key for each developer or team with budget limits +3. Configure appropriate limits based on seniority or project needs + + + + + + + + +### Step 2: Define Model Access Rules + +As your development team scales, controlling which developers can access specific models becomes crucial. Portkey Configs provide this control layer with features like: + +#### Access Control Features: +- **Model Restrictions**: Limit access to expensive models +- **Code Protection**: Implement guardrails for sensitive code +- **Reliability Controls**: Add fallbacks and retry logic + +#### Example Configuration for Development Teams: +```json +{ + "strategy": { + "mode": "fallback" + }, + "targets": [ + { + "virtual_key": "YOUR_OPENAI_VIRTUAL_KEY", + "override_params": { + "model": "gpt-4o" + } + }, + { + "virtual_key": "YOUR_ANTHROPIC_VIRTUAL_KEY", + "override_params": { + "model": "claude-3-sonnet-20240229" + } + } + ] +} +``` + +Create your config on the [Configs page](https://app.portkey.ai/configs) in your Portkey dashboard. + + +Configs can be updated anytime to adjust controls without developers needing to update their Roo settings. + + + + +### Step 3: Implement Developer Access Controls + +Create Developer-specific API keys that automatically: +- Track usage per developer with virtual keys +- Apply appropriate configs to route requests +- Collect metadata about coding sessions +- Enforce access permissions + +Create API keys through: +- [Portkey App](https://app.portkey.ai/) +- [API Key Management API](/api-reference/admin-api/control-plane/api-keys/create-api-key) + +Example using Python SDK: +```python +from portkey_ai import Portkey + +portkey = Portkey(api_key="YOUR_ADMIN_API_KEY") + +api_key = portkey.api_keys.create( + name="frontend-team-developer", + type="organisation", + workspace_id="YOUR_WORKSPACE_ID", + defaults={ + "config_id": "your-config-id", + "metadata": { + "environment": "development", + "team": "frontend", + "developer": "john.doe" + } + }, + scopes=["logs.view", "configs.read"] +) +``` + +For detailed key management instructions, see our [API Keys documentation](/api-reference/admin-api/control-plane/api-keys/create-api-key). + + + +### Step 4: Deploy & Monitor +After distributing API keys to your developers, your enterprise-ready Roo setup is ready. Each developer can now use their designated API keys with appropriate access levels and budget controls. + +Monitor usage in Portkey dashboard: +- Cost tracking by developer +- Model usage patterns +- Code generation metrics +- Error rates and performance + + + + + +### Enterprise Features Now Available +**Roo now has:** + +- Per-developer budget controls +- Model access governance +- Usage tracking & attribution +- Code security guardrails +- Reliability features for development workflows + + + +# Portkey Features +Now that you have enterprise-grade Roo setup, let's explore the comprehensive features Portkey provides to ensure secure, efficient, and cost-effective AI-assisted development. + +### 1. Comprehensive Metrics +Using Portkey you can track 40+ key metrics including cost, token usage, response time, and performance across all your LLM providers in real time. Filter these metrics by developer, team, or project using custom metadata. + + + + + +### 2. Advanced Logs +Portkey's logging dashboard provides detailed logs for every request made by Roo. These logs include: +- Complete request and response tracking +- Code context and generation metrics +- Developer attribution +- Cost breakdown per coding session + + + + + +### 3. Unified Access to 250+ LLMs + +Easily switch between 250+ LLMs for different coding tasks. Use GPT-4 for complex architecture decisions, Claude for detailed code reviews, or specialized models for specific languages - all through a single interface. + +### 4. Advanced Metadata Tracking +Track coding patterns and productivity metrics with custom metadata: +- Language and framework usage +- Code generation vs completion tasks +- Time-of-day productivity patterns +- Project-specific metrics + + + + +### 5. Enterprise Access Management + + + +Set and manage spending limits per developer or team. Prevent budget overruns with automatic cutoffs. + + + +Enterprise-grade SSO integration for seamless developer onboarding and offboarding. + + + +Hierarchical structure with teams, projects, and role-based access control for development organizations. + + + +Comprehensive audit logging for security compliance and code generation tracking. + + + +### 6. Reliability Features + + + Automatically switch between models if one fails, ensuring uninterrupted coding. + + + Route requests based on code complexity or language requirements. + + + Distribute requests across multiple API keys or providers. + + + Cache common code patterns to reduce costs and improve response times. + + + Automatic retry handling for failed requests with exponential backoff. + + + Enforce spending limits to control development costs. + + + +### 7. Advanced Guardrails + +Protect your codebase and enhance security with real-time checks on AI interactions: +- Prevent exposure of API keys and secrets +- Block generation of malicious code patterns +- Enforce coding standards and best practices +- Custom security rules for your organization +- License compliance checks + + +Implement real-time protection for your development environment with automatic detection and filtering of sensitive code, credentials, and security vulnerabilities. + + +# FAQs + + + +Portkey provides several ways to track developer costs: +- Create separate Virtual Keys for each developer +- Use metadata tags to identify developers +- Set up developer-specific API keys +- View detailed analytics in the dashboard + + + +When a developer reaches their budget limit: +1. Further requests will be blocked +2. The developer and admin receive notifications +3. Coding history remains available +4. Admins can adjust limits as needed + + + +Yes! Portkey supports local models through Ollama and other self-hosted solutions. Configure your local endpoint as a custom provider in Portkey and use it with Roo just like any other provider. + + + +Portkey provides multiple security layers: +- Guardrails to prevent sensitive data exposure +- Request/response filtering +- Audit logs for all interactions +- Custom security rules +- PII detection and masking + + + +# Next Steps + +**Join our Community** +- [Discord Community](https://portkey.sh/discord-report) +- [GitHub Repository](https://github.com/Portkey-AI) + + + +Schedule a 1:1 call with our team to see how Portkey can transform your development workflow with Roo. Get personalized recommendations for your team's specific needs. + + + +For enterprise support and custom features for your development teams, contact our [enterprise team](https://calendly.com/portkey-ai). + diff --git a/virtual_key_old/integrations/libraries/supabase.mdx b/virtual_key_old/integrations/libraries/supabase.mdx new file mode 100644 index 00000000..f7ab5e21 --- /dev/null +++ b/virtual_key_old/integrations/libraries/supabase.mdx @@ -0,0 +1,113 @@ +--- +title: 'Supabase' +--- + +Supabase provides an open source toolkit for developing AI applications using Postgres and pgvector. With Portkey integration, you can seamlessly generate embeddings using AI models like OpenAI and store them in Supabase, enabling efficient data retrieval. Portkeyโ€™s unified API supports over 250 models, making AI management more streamlined and secure + +## Prerequisites + +1. Supabase project API Key +2. [Portkey](https://app.portkey.ai/?utm_source=supabase&utm_medium=content&utm_campaign=external) API key + +## Setting up your environment + +First, let's set up our Python environment with the necessary libraries: + +```sh +pip install portkey-ai supabase +``` + +## Preparing your database + + +Go to [Supabase](https://supabase.com/dashboard/sign-in) and create a new project. + + +`pgvector` is an extension for PostgreSQL that allows you to both store and query vector embeddings within your database. We can enable it from the web portal through Database โ†’ Extensions. You can also do this in SQL by running: + +```sql +create extension vector; +``` + + +`pgvector` introduces a new data type called `vector`. In the code above, we create a column named `embedding` with the `vector` data type. The size of the vector defines how many dimensions the vector holds. OpenAI's `text-embedding-ada-002` model outputs 1536 dimensions, so we will use that for our vector size. +We also create a `text` column named `content` to store the original document text that produced this embedding. Depending on your use case, you might just store a reference (URL or foreign key) to a document here instead. +```sql +create table documents ( + id bigserial primary key, + content text, + embedding vector(1536) +); +``` + + + +## Configuring Supabase and Portkey + +Now, let's import the required libraries and set up our Supabase and Portkey clients: + +```python +from portkey_ai import Portkey +from supabase import create_client, Client + +# Supabase setup +supabase_url = "YOUR_SUPABASE_PROJECT_URL" +supabase_key = "YOUR_SUPABASE_API_KEY" +supabase: Client = create_client(supabase_url, supabase_key) + +# Portkey setup +portkey_client = Portkey( + api_key="YOUR_PORTKEY_API_KEY", + provider="openai", + virtual_key="YOUR_OPENAI_VIRTUAL_KEY", +) +``` + +Replace the placeholder values with your actual Supabase and Portkey credentials. + + +## Generating and storing embeddings + +Let's create a function to generate embeddings using Portkey and OpenAI, and store them in Supabase: + +```python +#Generate Embedding +embedding_response = client.embeddings.create( + model="text-embedding-ada-002", + input="The food was delicious and the waiter...", + encoding_format="float" +) + +embedding = embedding_response.data[0].embedding + +# Store in Supabase +result = supabase.table('documents').insert({ + "content": text, + "embedding": embedding +}).execute() +``` + +This function takes a text input, generates an embedding using through Portkey, and then stores both the original text and its embedding in the Supabase `documents` table. + +Portkey supports 250+ Models, you can choose any model just by changing the `provider` and `virtual_key` + +Here's an example on how to use `Cohere` with Portkey + +```python +client = Portkey( + api_key="YOUR_PORTKEY_API_KEY", # defaults to os.environ.get("PORTKEY_API_KEY") + provider="cohere", + virtual_key="YOUR_COHERE_VIRTUAL_KEY", +) + +embeddings = client.embeddings.create( + model="embed-english-v3.0", + input_type="search_query", + input="The food was delicious and the waiter...", + encoding_format="float" +) +``` + + +Note that you will need to make a new table with `1024` dimensions instead of `1536` dimensions for Cohere's `embed-english-v3.0` model. + diff --git a/virtual_key_old/integrations/libraries/tooljet.mdx b/virtual_key_old/integrations/libraries/tooljet.mdx new file mode 100644 index 00000000..a90a5960 --- /dev/null +++ b/virtual_key_old/integrations/libraries/tooljet.mdx @@ -0,0 +1,285 @@ +--- +title: 'ToolJet' +description: 'ToolJet is a low-code platform that lets you build apps by connecting APIs and data sources, with Portkey integration adding AI features like chat interfaces and automation.' +--- + +This guide provides a **streamlined process** for integrating **Portkey AI** into ToolJet using the ToolJet Marketplace Plugin. Follow this guide to add **AI-powered capabilities** such as chat completions and automations into your ToolJet apps, helping developers create powerful, low-code applications backed by Portkey's AI for chat-based interfaces and automated workflows. + +## Prerequisites + +- **Portkey API Key** and **Virtual Key** from your Portkey dashboard. +- **ToolJet Account** with access to the **Marketplace Plugin feature**. +- Basic familiarity with ToolJet UI components. + + + Watch this demo for a quick walkthrough on ToolJet's UI components. + + + + Before following this guide, ensure that you have completed the setup for using marketplace plugins in ToolJet. + + + +## Step-by-Step Onboarding + + + + 1. Go to **ToolJet Dashboard > Plugins > Marketplace**. + 2. Search for **Portkey Plugin**. + 3. Click **Install** to add the plugin to your project. + + + If Portkey has already been added to your marketplace, you can skip this step. + + + + + 1. Navigate to **Data Sources** in your ToolJet workspace. + 2. Open **Plugins**. + 3. Click **Add Portkey**. + + + + + + + + 1. Enter the following details: + - **Authorization:** Your Portkey API Key + - **Default Virtual Key:** Your Portkey Virtual Key + 2. **Test** the connection to ensure the keys are configured correctly. + + + + + + + 1. Go to **Queries > Add Datasource > Select Portkey Plugin**. + 2. From the dropdown, **Select an Operation** (details in the next section). + 3. **Run the query** to verify it responds correctly. + + + + + + + 1. Add **Text Input** and **Button** widgets to your app's interface. + 2. Configure the Button's **onClick action** to **Execute Query** using the Portkey API. + 3. Use a **Text Box** widget to display the query results. + + + 1. Use **Preview Mode** to test the interaction between your app's UI and the Portkey API. + 2. Deploy the app from the ToolJet dashboard. + + + +## Supported Operations + +Portkey supports the following operations within ToolJet: + + + + #### Completion + + Generates text completions based on a given prompt. + + **Parameters:** + - **Prompt:** Input text to generate completions for. + - **Model:** The AI model to use. + - **Max Tokens:** Maximum number of tokens to generate. + - **Temperature:** Controls randomness. + - **Stop Sequences:** Sequences where the API stops generating further tokens. + - **Metadata:** Additional metadata for the request. + - **Other Parameters:** Additional request parameters. + + + + + + ```javascript + { + "id": "cmpl-9vNUfM8OP0SwSqXcnPwkqzR7ep8Sy", + "object": "text_completion", + "created": 1723462033, + "model": "gpt-3.5-turbo-instruct", + "choices": [ + { + "text": "nn"Experience the perfect brew at Bean There."", + "index": 0, + "logprobs": null, + "finish_reason": "stop" + } + ], + "usage": { + "prompt_tokens": 13, + "completion_tokens": 10, + "total_tokens": 23 + } + } + ``` + + + + + + #### Chat + + Generates chat completions based on a series of messages. + + **Parameters:** + - **Messages:** Array of message objects representing the conversation. + - **Model:** The AI model to use. + - **Max Tokens:** Maximum number of tokens to generate. + - **Temperature:** Controls randomness. + - **Stop Sequences:** Sequences where the API stops generating further tokens. + - **Metadata:** Additional metadata for the request. + - **Other Parameters:** Additional request parameters. + + + + + + ```javascript Response Example + { + "id": "chatcmpl-9vNIlfllXOPEmroKFajK2nlJHzhXA", + "object": "chat.completion", + "created": 1723461295, + "model": "gpt-3.5-turbo-0125", + "choices": [ + { + "index": 0, + "message": { + "role": "assistant", + "content": "The capital of France is Paris.", + "refusal": null + }, + "logprobs": null, + "finish_reason": "stop" + } + ], + "usage": { + "prompt_tokens": 24, + "completion_tokens": 7, + "total_tokens": 31 + }, + "system_fingerprint": null + } + ``` + + + + + #### Prompt Completion + + Generates completions based on a pre-defined prompt. + + **Parameters:** + - **Prompt ID:** The ID of the pre-defined prompt. + - **Variables:** Variables to use in the prompt. + - **Parameters:** Additional parameters for the completion. + - **Metadata:** Additional metadata for the request. + + + + + + ```javascript Response Example + { + "id": "chatcmpl-9w6D8jZciWVf1DzkgqNZK14KUvA4d", + "object": "chat.completion", + "created": 1723633926, + "model": "gpt-4o-mini-2024-07-18", + "choices": [ + { + "index": 0, + "message": { + "role": "assistant", + "content": "The Industrial Revolution, starting in the late 18th century, transformed production from hand methods to machine-based processes, introducing new manufacturing techniques, steam power, and machine tools. It marked a shift from bio-fuels to coal, with the textile industry leading the way. This period resulted in significant population growth, increased average income, and improved living standards.", + "refusal": null + }, + "logprobs": null, + "finish_reason": "stop" + } + ], + "usage": { + "prompt_tokens": 145, + "completion_tokens": 71, + "total_tokens": 216 + }, + "system_fingerprint": "fp_48196bc67a" + } + ``` + + + + + #### Create Embedding + + Generates embeddings for a given input. + + **Parameters:** + - **Input:** Text to create embeddings for. + - **Model:** The AI model to use for embeddings. + - **Metadata:** Additional metadata for the request. + + + + + + ```javascript Response Example + { + "object": "list", + "data": [ + { + "object": "embedding", + "index": 0, + "embedding": [ + -0.02083237, + -0.016892163, + -0.0045676464, + -0.05084554, + -0.025968939, + 0.029597048, + 0.029987168, + 0.02907689, + 0.0105982395, + -0.024356445, + -0.00935636, + 0.0066352785, + 0.034018397, + -0.042002838, + 0.03856979, + -0.014681488, + ..., + 0.024707552 + ] + } + ], + "model": "text-embedding-3-small", + "usage": { + "prompt_tokens": 9, + "total_tokens": 9 + } + } + ``` + + + + + + + For all operations, you can optionally specify: + - **Config:** Configuration options for the request. + - **Virtual Key:** Override the default virtual key with a specific one. + + +## Troubleshooting + +#### - Authentication Error + Double-check your **API key** and **configuration**. +#### - Slow Response + Adjust the **temperature** or **max_tokens** to optimize performance. +#### - CORS Issues + Ensure your API settings allow access from **ToolJet's domain**. + + diff --git a/virtual_key_old/integrations/libraries/vercel.mdx b/virtual_key_old/integrations/libraries/vercel.mdx new file mode 100644 index 00000000..a11cde45 --- /dev/null +++ b/virtual_key_old/integrations/libraries/vercel.mdx @@ -0,0 +1,291 @@ +--- +title: 'Vercel' +description: 'Integrate Portkey with Vercel AI SDK for production-ready and reliable AI apps' +--- + +Portkey natively integrates with the Vercel AI SDK to make your apps production-ready and reliable. Just import Portkey's Vercel package and use it as a provider in your Vercel AI app to enable all of Portkey features: + +* Full-stack observability and tracing for all requests +* Interoperability across 250+ LLMS +* Built-in 50+ SOTA guardrails +* Simple & semantic caching to save costs & time +* Route requests conditionally and make them robust with fallbacks, load-balancing, automatic retries, and more +* Continuous improvement based on user feedback + +## Getting Started + +### 1. Installation + +```sh +npm install @portkey-ai/vercel-provider +``` + +### 2. Import & Configure Portkey Object + +[Sign up for Portkey ](https://portkey.ai)and get your API key, and configure Portkey provider in your Vercel app: + +```typescript +import { createPortkey } from '@portkey-ai/vercel-provider'; + +const portkeyConfig = { + "provider": "openai", // Choose your provider (e.g., 'anthropic') + "api_key": "OPENAI_API_KEY", + "override_params": { + "model": "gpt-4o" // Select from 250+ models + } +}; + +const portkey = createPortkey({ + apiKey: 'YOUR_PORTKEY_API_KEY', + config: portkeyConfig, +}); +``` + + +Portkey's configs are a powerful way to manage & govern your app's behaviour. Learn more about Configs [here](/product/ai-gateway/configs). + + +## Using Vercel Functions + +Portkey provider works with all of Vercel functions `generateText` & `streamText`. + +Here's how to use them with Portkey: + + + +```typescript generateText +import { createPortkey } from '@portkey-ai/vercel-provider'; +import { generateText } from 'ai'; + +const portkeyConfig = { + "provider": "openai", // Choose your provider (e.g., 'anthropic') + "api_key": "OPENAI_API_KEY", + "override_params": { + "model": "gpt-4o" + } +}; + +const portkey = createPortkey({ + apiKey: 'YOUR_PORTKEY_API_KEY', + config: portkeyConfig, +}); + +const { text } = await generateText({ + model: portkey.chatModel(''), // Provide an empty string, we defined the model in the config + prompt: 'What is Portkey?', +}); + +console.log(text); +``` + +```typescript streamText +import { createPortkey } from '@portkey-ai/vercel-provider'; +import { streamText } from 'ai'; + +const portkeyConfig = { + "provider": "openai", // Choose your provider (e.g., 'anthropic') + "api_key": "OPENAI_API_KEY", + "override_params": { + "model": "gpt-4o" // Select from 250+ models + } +}; + +const portkey = createPortkey({ + apiKey: 'YOUR_PORTKEY_API_KEY', + config: portkeyConfig, +}); + +const result = await streamText({ + model: portkey('gpt-4-turbo'), // This gets overwritten by config + prompt: 'Invent a new holiday and describe its traditions.', +}); + +for await (const chunk of result) { + console.log(chunk); +} +``` + + + + +Portkey supports `chatModel` and `completionModel` to easily handle chatbots or text completions. In the above examples, we used `portkey.chatModel` for generateText and `portkey.completionModel` for streamText. + + +### Tool Calling with Portkey + +Portkey supports Tool calling with Vercel AI SDK. Here's how- + +```typescript +import { z } from 'zod'; +import { generateText, tool } from 'ai'; + +const result = await generateText({ + model: portkey.chatModel('gpt-4-turbo'), + tools: { + weather: tool({ + description: 'Get the weather in a location', + parameters: z.object({ + location: z.string().describe('The location to get the weather for'), + }), + execute: async ({ location }) => ({ + location, + temperature: 72 + Math.floor(Math.random() * 21) - 10, + }), + }), + }, + prompt: 'What is the weather in San Francisco?', +}); +``` + +## Portkey Features + +Portkey Helps you make your Vercel app more robust and reliable. The portkey config is a modular way to make it work for you in whatever way you want. + +### [Interoperability](/product/ai-gateway/universal-api) + +Portkey allows you to easily switch between 250+ AI models by simply changing the model name in your configuration. This flexibility enables you to adapt to the evolving AI landscape without significant code changes. + + + +```javascript Switch from OpenAI to Anthropic +const portkeyConfig = { + "provider": "openai", + "api_key": "OPENAI_API_KEY", + "override_params": { + "model": "gpt-4o" + } +}; +``` + +```javascript Anthropic +const portkeyConfig = { + "provider": "anthropic", + "api_key": "Anthropic_API_KEY", + "override_params": { + "model": "claude-3-5-sonnet-20240620" + } + +}; +``` + + + +### [Observability](/product/observability) + +Portkey's OpenTelemetry-compliant observability suite gives you complete control over all your requests. And Portkey's analytics dashboards provide **40**+ key insights you're looking for including cost, tokens, latency, etc. Fast. + + + Portkey's Observability Dashboard + + +### Reliability + +Portkey enhances the robustness of your AI applications with built-in features such as [Caching](/product/ai-gateway/cache-simple-and-semantic), [Fallback](/product/ai-gateway/fallbacks) mechanisms, [Load balancing](/product/ai-gateway/load-balancing), [Conditional routing](/product/ai-gateway/conditional-routing), [Request timeouts](/product/ai-gateway/request-timeouts), etc. + +Here is how you can modify your config to include the following Portkey features- + + + +```javascript Fallback +import { createPortkey } from '@portkey-ai/vercel-provider'; +import { generateText } from 'ai'; + +const portkeyConfig = { + "strategy": { + "mode": "fallback" + }, + "targets": [ + { + "provider": "anthropic", + "api_key": "Anthropic_API_KEY", + "override_params": { + "model": "claude-3-5-sonnet-20240620" + } }, + { + "provider": "openai", + "api_key": "OPENAI_API_KEY", + "override_params": { + "model": "gpt-4o" + } } + ] +} + +const portkey = createPortkey({ + apiKey: 'YOUR_PORTKEY_API_KEY', + config: portkeyConfig, +}); + +const { text } = await generateText({ + model: portkey.chatModel(''), + prompt: 'What is Portkey?', +}); + +console.log(text); +``` + +```javascript Caching +import { createPortkey } from '@portkey-ai/vercel-provider'; +import { generateText } from 'ai'; + +const portkeyConfig = { "cache": { "mode": "semantic" } } + +const portkey = createPortkey({ + apiKey: 'YOUR_PORTKEY_API_KEY', + config: portkeyConfig, +}); + +const { text } = await generateText({ + model: portkey.chatModel(''), + prompt: 'What is Portkey?', +}); + +console.log(text); +``` + +```javascript Conditional routing +const portkey_config = { + "strategy": { + "mode": "conditional", + "conditions": [ + ...conditions + ], + "default": "target_1" + }, + "targets": [ + { + "name": "target_1", + "provider": "anthropic", + "api_key": "Anthropic_API_KEY", + "override_params": { + "model": "claude-3-5-sonnet-20240620" + } + }, + { + "name": "target_2", + "provider": "openai", + "api_key": "OpenAI_api_key", + "override_params": { + "model": "gpt-4o" + } + } + ] +} +``` + + + +Learn more about Portkey's AI gateway features in detail [here](/product/ai-gateway/). + +### [Guardrails](/product/guardrails/) + +Portkey Guardrails allow you to enforce LLM behavior in real-time, verifying both inputs and outputs against specified checks. + +You can create Guardrail checks in UI and then pass them in your Portkey Configs with before request or after request hooks. + +[Read more about Guardrails here](/product/guardrails/). + +## [Portkey Config](/product/ai-gateway/configs) + +Many of these features are driven by Portkey's Config architecture. The Portkey app simplifies creating, managing, and versioning your Configs. + +For more information on using these features and setting up your Config, please refer to the [Portkey documentation](/product/ai-gateway/configs). diff --git a/virtual_key_old/integrations/libraries/zed.mdx b/virtual_key_old/integrations/libraries/zed.mdx new file mode 100644 index 00000000..6b02fafd --- /dev/null +++ b/virtual_key_old/integrations/libraries/zed.mdx @@ -0,0 +1,385 @@ +--- +title: "Zed" +description: "Learn how to integrate Portkey's enterprise features with Zed for enhanced observability, reliability and governance." +--- + +Zed is a next-generation code editor engineered for high-performance collaboration between developers and AI. Integrating Portkey with Zed allows you to secure, observe, and govern your LLM workflows with enterprise-grade control. This guide walks through setting up Portkey as the gateway for all your Zed requests, enabling centralized monitoring, caching, cost control, and compliance. + +Why Integrate Portkey with Zed? + +- **Unified AI Gateway** - Single interface for 1600+ LLMs with API key management. (not just OpenAI & Anthropic) +- **Centralized AI observability**: Real-time usage tracking for 40+ key metrics and logs for every request +- **Governance** - Real-time spend tracking, set budget limits and RBAC in your Zed setup +- **Security Guardrails** - PII detection, content filtering, and compliance controls + + + If you are an enterprise looking to use Zed in your organisation, [check out this section](#3-set-up-enterprise-governance-for-zed). + + +# 1. Setting up Portkey +Portkey allows you to use 1600+ LLMs with your Zed setup, with minimal configuration required. Let's set up the core components in Portkey that you'll need for integration. + + + +Virtual Keys are Portkey's secure way to manage your LLM provider API keys. Think of them like disposable credit cards for your LLM API keys, providing essential controls like: +- Budget limits for API usage +- Rate limiting capabilities +- Secure API key storage + +To create a virtual key: +Go to [Virtual Keys](https://app.portkey.ai/virtual-keys) in the Portkey App. Save and copy the virtual key ID + + + + + + +Save your virtual key ID - you'll need it for the next step. + + + + + +Configs in Portkey are JSON objects that define how your requests are routed. They help with implementing features like advanced routing, fallbacks, and retries. + +We need to create a default config to route our requests to the virtual key created in Step 1. + +To create your config: +1. Go to [Configs](https://app.portkey.ai/configs) in Portkey dashboard +2. Create new config with: + ```json + { + "virtual_key": "YOUR_VIRTUAL_KEY_FROM_STEP1", + } + ``` +3. Save and note the Config name for the next step + + + + + + +This basic config connects to your virtual key. You can add more advanced portkey features later. + + + + + +Now create Portkey API key access point and attach the config you created in Step 2: + +1. Go to [API Keys](https://app.portkey.ai/api-keys) in Portkey and Create new API key +2. Select your config from `Step 2` +3. Generate and save your API key + + + + + + +Save your API key securely - you'll need it for Zed integration. + + + + +## 2. Integrated Portkey with Zed + + You will need your Portkey API key created in [Step 1](#Setting-up-portkey) for this integration + + +Portkey is an OpenAI compatible API, which means it can be easily integrated with zed without any changes to your setup. Here's how you do it + +1. Open `Settings.json` in Zed using Command Palette (cmd-shift-p / ctrl-shift-p) and run `zed: open settings` +Add the following configuration to your `settings.json` file: +This configuration will allow you to use Portkey's GPT-4o model in Zed. You can change the model according to your virtual key setup in step 1.ll power of GPT-4o. + **Configure Zed** + Update your `settings.json` in Zed: + ```json + { + "language_models": { + "openai": { + "api_url": "https://api.portkey.ai/v1", + "available_models": [ + { + "name": "gpt-4o", // choose the model you want to use according to your virtual key setup + "display_name": "Portkey-Model", // you can change this to any name + "max_tokens": 131072 // this is a required field + } + ], + "version": "1" + } + } + } + ``` + + +3. **Add API Key to Zed** + - Open Command Palette (cmd-shift-p / ctrl-shift-p) + - Run "assistant: show configuration" + - Add your Portkey API key under OpenAI configuration + + +# 3. Set Up Enterprise Governance for Zed + +**Why Enterprise Governance?** +If you are using Zed inside your orgnaization, you need to consider several governance aspects: +- **Cost Management**: Controlling and tracking AI spending across teams +- **Access Control**: Managing which teams can use specific models +- **Usage Analytics**: Understanding how AI is being used across the organization +- **Security & Compliance**: Maintaining enterprise security standards +- **Reliability**: Ensuring consistent service across all users + +Portkey adds a comprehensive governance layer to address these enterprise needs. Let's implement these controls step by step. + +**Enterprise Implementation Guide** + + + +### Step 1: Implement Budget Controls & Rate Limits + +Virtual Keys enable granular control over LLM access at the team/department level. This helps you: +- Set up [budget limits](/product/ai-gateway/virtual-keys/budget-limits) +- Prevent unexpected usage spikes using Rate limits +- Track departmental spending + +#### Setting Up Department-Specific Controls: +1. Navigate to [Virtual Keys](https://app.portkey.ai/virtual-keys) in Portkey dashboard +2. Create new Virtual Key for each department with budget limits and rate limits +3. Configure department-specific limits + + + + + + + + +### Step 2: Define Model Access Rules + +As your AI usage scales, controlling which teams can access specific models becomes crucial. Portkey Configs provide this control layer with features like: + +#### Access Control Features: +- **Model Restrictions**: Limit access to specific models +- **Data Protection**: Implement guardrails for sensitive data +- **Reliability Controls**: Add fallbacks and retry logic + +#### Example Configuration: +Here's a basic configuration to route requests to OpenAI, specifically using GPT-4o: + +```json +{ + "strategy": { + "mode": "single" + }, + "targets": [ + { + "virtual_key": "YOUR_OPENAI_VIRTUAL_KEY", + "override_params": { + "model": "gpt-4o" + } + } + ] +} +``` + +Create your config on the [Configs page](https://app.portkey.ai/configs) in your Portkey dashboard. You'll need the config ID for connecting to Zed's setup. + + + +Configs can be updated anytime to adjust controls without affecting running applications. + + + + +### Step 3: Implement Access Controls + +Create User-specific API keys that automatically: +- Track usage per user/team with the help of virtual keys +- Apply appropriate configs to route requests +- Collect relevant metadata to filter logs +- Enforce access permissions + + +Create API keys through: +- [Portkey App](https://app.portkey.ai/) +- [API Key Management API](/api-reference/admin-api/control-plane/api-keys/create-api-key) + + +Example using Python SDK: +```python +from portkey_ai import Portkey + +portkey = Portkey(api_key="YOUR_ADMIN_API_KEY") + +api_key = portkey.api_keys.create( + name="engineering-team", + type="organisation", + workspace_id="YOUR_WORKSPACE_ID", + defaults={ + "config_id": "your-config-id", + "metadata": { + "environment": "production", + "department": "engineering" + } + }, + scopes=["logs.view", "configs.read"] +) +``` + +For detailed key management instructions, see our [API Keys documentation](/api-reference/admin-api/control-plane/api-keys/create-api-key). + + + +### Step 4: Deploy & Monitor +After distributing API keys to your team members, your enterprise-ready Zed setup is ready to go. Each team member can now use their designated API keys with appropriate access levels and budget controls. +Apply your governance setup using the integration steps from earlier sections +Monitor usage in Portkey dashboard: +- Cost tracking by department +- Model usage patterns +- Request volumes +- Error rates + + + + + +### Enterprise Features Now Available +**Zed now has:** + +- Departmental budget controls +- Model access governance +- Usage tracking & attribution +- Security guardrails +- Reliability features + + + +# Portkey Features +Now that you have enterprise-grade Zed setup, let's explore the comprehensive features Portkey provides to ensure secure, efficient, and cost-effective AI operations. + +### 1. Comprehensive Metrics +Using Portkey you can track 40+ key metrics including cost, token usage, response time, and performance across all your LLM providers in real time. You can also filter these metrics based on custom metadata that you can set in your configs. Learn more about metadata here. + + + + + +### 2. Advanced Logs +Portkey's logging dashboard provides detailed logs for every request made to your LLMs. These logs include: +- Complete request and response tracking +- Metadata tags for filtering +- Cost attribution and much more... + + + + + + + + +### 3. Unified Access to 1600+ LLMs + +You can easily switch between 1600+ LLMs. Call various LLMs such as Anthropic, Gemini, Mistral, Azure OpenAI, Google Vertex AI, AWS Bedrock, and many more by simply changing the `virtual key` in your default `config` object. + + +### 4. Advanced Metadata Tracking +Using Portkey, you can add custom metadata to your LLM requests for detailed tracking and analytics. Use metadata tags to filter logs, track usage, and attribute costs across departments and teams. + + + + + + +### 5. Enterprise Access Management + + + +Set and manage spending limits across teams and departments. Control costs with granular budget limits and usage tracking. + + + +Enterprise-grade SSO integration with support for SAML 2.0, Okta, Azure AD, and custom providers for secure authentication. + + + +Hierarchical organization structure with workspaces, teams, and role-based access control for enterprise-scale deployments. + + + +Comprehensive access control rules and detailed audit logging for security compliance and usage tracking. + + + + +### 6. Reliability Features + + + Automatically switch to backup targets if the primary target fails. + + + Route requests to different targets based on specified conditions. + + + Distribute requests across multiple targets based on defined weights. + + + Enable caching of responses to improve performance and reduce costs. + + +Automatic retry handling with exponential backoff for failed requests + + + Set and manage budget limits across teams and departments. Control costs with granular budget limits and usage tracking. + + + + + +### 7. Advanced Guardrails + +Protect your Project's data and enhance reliability with real-time checks on LLM inputs and outputs. Leverage guardrails to: +- Prevent sensitive data leaks +- Enforce compliance with organizational policies +- PII detection and masking +- Content filtering +- Custom security rules +- Data compliance checks + + +Implement real-time protection for your LLM interactions with automatic detection and filtering of sensitive content, PII, and custom security rules. Enable comprehensive data protection while maintaining compliance with organizational policies. + + + +# FAQs + + + You can update your Virtual Key limits at any time from the Portkey dashboard:1. Go to Virtual Keys section2. Click on the Virtual Key you want to modify3. Update the budget or rate limits4. Save your changes + + + Yes! You can create multiple Virtual Keys (one for each provider) and attach them to a single config. This config can then be connected to your API key, allowing you to use multiple providers through a single API key. + + +Portkey provides several ways to track team costs: +- Create separate Virtual Keys for each team +- Use metadata tags in your configs +- Set up team-specific API keys +- Monitor usage in the analytics dashboard + + +When a team reaches their budget limit: +1. Further requests will be blocked +2. Team admins receive notifications +3. Usage statistics remain available in dashboard +4. Limits can be adjusted if needed + + + + +# Next Steps + + **Join our Community** +- [Discord Community](https://portkey.sh/discord-report) +- [GitHub Repository](https://github.com/Portkey-AI) + + +For enterprise support and custom features, contact our [enterprise team](https://calendly.com/portkey-ai). + diff --git a/virtual_key_old/integrations/llms.mdx b/virtual_key_old/integrations/llms.mdx new file mode 100644 index 00000000..25a95670 --- /dev/null +++ b/virtual_key_old/integrations/llms.mdx @@ -0,0 +1,230 @@ +--- +title: "Overview" +description: "Portkey connects with all major LLM providers and orchestration frameworks." +--- + +## Supported AI Providers + + + + {/* Priority LLM Providers */} + + OpenAI + + + + Anthropic + + + + Azure OpenAI + + + + AWS Bedrock + + + + Cohere + + + + Google Gemini + + + + Vertex AI + + + + Perplexity AI + + + {/* All other LLM providers in alphabetical order */} + + AI21 + + + + Anyscale + + + + + + Dashscope + + + + Deepbricks + + + + DeepInfra + + + + DeepSeek + + + + Fireworks AI + + + + Github + + + + Google Palm + + + + Groq + + + + Jina AI + + + + Lambda + + + + Lemonfox AI + + + + + Lepton AI + + + + Lingyi (01.ai) + + + + LocalAI + + + + Mistral AI + + + + MonsterAPI + + + + Moonshot + + + + nCompass + + + + Nebius + + + + Nomic AI + + + + Nscale + + + + Ollama + + + + Openrouter + + + + Predibase + + + + Recraft + + + + Siliconflow + + + + Snowflake + + + + Stability AI + + + + Together AI + + + + Triton + + + + Upstage + + + + Voyage-AI + + + + Workers AI + + + + ZhipuAI + + + +## Endpoints Supported + +For a detailed breakdown of supported endpoints and features per provider, refer to the table below: + + + + +## Supported Frameworks + +Portkey has native integrations with the following frameworks. Click to read their getting started guides. + + + + Langchain + + + LlamaIndex + + + Autogen + + + Vercel + + + Instructor + + + DSPy + + + Promptfoo + + + + +Have a suggestion for an integration with Portkey? Tell us on [Discord](https://discord.gg/DD7vgKK299), or drop a message on support@portkey.ai. + +While you're here, why not [give us a star](https://git.new/ai-gateway-docs)? It helps us a lot! diff --git a/virtual_key_old/integrations/llms/ai21.mdx b/virtual_key_old/integrations/llms/ai21.mdx new file mode 100644 index 00000000..d471f671 --- /dev/null +++ b/virtual_key_old/integrations/llms/ai21.mdx @@ -0,0 +1,110 @@ +--- +title: "AI21" +--- + +Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including [AI21](https://ai21.com). + +With Portkey, you can take advantage of features like fast AI gateway access, observability, prompt management, and more, all while ensuring the secure management of your LLM API keys through a [virtual key](/product/ai-gateway/virtual-keys) system. + + +Provider Slug. **ai21** + +## Portkey SDK Integration with AI21 Models + +Portkey provides a consistent API to interact with models from various providers. To integrate AI21 with Portkey: + +### 1\. Install the Portkey SDK + +Add the Portkey SDK to your application to interact with AI21 AI's API through Portkey's gateway. + + + + ```sh + npm install --save portkey-ai + ``` + + + +```sh +pip install portkey-ai +``` + + + + + +### 2\. Initialize Portkey with the Virtual Key + +To use AI21 with Portkey, [get your API key from here](https://studio.ai21.com/account/api-key), then add it to Portkey to create the virtual key. + + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your AI21 Virtual Key + }) + ``` + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Groq + ) + ``` + + + + + +### 3\. Invoke Chat Completions with AI21 + +Use the Portkey instance to send requests to AI21\. You can also override the virtual key directly in the API call if needed. + + + ```js + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'jamba-1-5-large', + }); + + console.log(chatCompletion.choices);d + ``` + + + ```python + completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model= 'jamba-1-5-large' + ) + + print(completion) + ``` + + + + + + +## Managing AI21 Prompts + +You can manage all prompts to A121 in the [Prompt Library](/product/prompt-library). All the current models of AI21 are supported and you can easily start testing different prompts. + +Once you're ready with your prompt, you can use the `portkey.prompts.completions.create` interface to use the prompt in your application. + +The complete list of features supported in the SDK are available on the link below. + + + + +You'll find more information in the relevant sections + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your A121 requests](/product/ai-gateway/configs) +3. [Tracing A121 requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to A121 APIs](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/anthropic.mdx b/virtual_key_old/integrations/llms/anthropic.mdx new file mode 100644 index 00000000..a292b619 --- /dev/null +++ b/virtual_key_old/integrations/llms/anthropic.mdx @@ -0,0 +1,1226 @@ +--- +title: "Anthropic" +--- + +Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including [Anthropic's Claude APIs](https://docs.anthropic.com/claude/reference/getting-started-with-the-api). + +With Portkey, you can take advantage of features like fast AI gateway access, observability, prompt management, and more, all while ensuring the secure management of your LLM API keys through a [virtual key](/product/ai-gateway/virtual-keys) system. +Provider Slug. `anthropic` + + +## Portkey SDK Integration with Anthropic + +Portkey provides a consistent API to interact with models from various providers. To integrate Anthropic with Portkey: + +### 1\. Install the Portkey SDK + +Add the Portkey SDK to your application to interact with Anthropic's API through Portkey's gateway. + + + + ```sh + npm install --save portkey-ai + ``` + + + ```sh + pip install portkey-ai + ``` + + + + + + +### 2\. Initialize Portkey with the Virtual Key + +To use Anthropic with Portkey, [get your Anthropic API key from here](https://console.anthropic.com/settings/keys), then add it to Portkey to create your Anthropic virtual key. + + + + +```js +import Portkey from 'portkey-ai' + +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your Anthropic Virtual Key +}) +``` + + + + +```python +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Anthropic +) +``` + + + +```python +from openai import OpenAI + +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +client = OpenAI( + api_key="ANTHROPIC_API_KEY", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key="PORTKEY_API_KEY", + provider="anthropic" + ) +) +``` + + + + +```js +import OpenAI from "openai"; + +import { PORTKEY_GATEWAY_URL, createHeaders } from "portkey-ai"; + +const client = new OpenAI({ + apiKey: "ANTHROPIC_API_KEY", + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "anthropic", + apiKey: "PORTKEY_API_KEY", + }), +}); +``` + + + + + +### 3\. Invoke Chat Completions with Anthropic + +Use the Portkey instance to send requests to Anthropic. You can also override the virtual key directly in the API call if needed. + + + + + +```js +const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'claude-3-opus-20240229', + max_tokens: 250 // Required field for Anthropic +}); + +console.log(chatCompletion.choices[0].message.content); +``` + + + + +```python +chat_completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model= 'claude-3-opus-20240229', + max_tokens=250 # Required field for Anthropic +) + +print(chat_completion.choices[0].message.content) +``` + + + + +```python +chat_completion = client.chat.completions.create( + messages = [{ "role": 'user', "content": 'Say this is a test' }], + model = 'claude-3-opus-20240229', + max_tokens = 250 +) + +print(chat_completion.choices[0].message.content) +``` + + + + +```js +async function main() { + const chatCompletion = await client.chat.completions.create({ + model: "claude-3-opus-20240229", + max_tokens: 1024, + messages: [{ role: "user", content: "Hello, Claude" }], + }); + + console.log(chatCompletion.choices[0].message.content); + +} + +main(); +``` + + + + + + +## How to Use Anthropic System Prompt + +With Portkey, we make Anthropic models interoperable with the OpenAI schema and SDK methods. So, instead of passing the `system` prompt separately, you can pass it as part of the `messages` body, similar to OpenAI: + + + + +```js +const chatCompletion = await portkey.chat.completions.create({ + messages: [ + { role: 'system', content: 'Your system prompt' }, + { role: 'user', content: 'Say this is a test' } + ], + model: 'claude-3-opus-20240229', + max_tokens: 250 +}); + +console.log(chatCompletion.choices); +``` + + + + +```python +completion = portkey.chat.completions.create( + messages= [ + { "role": 'system', "content": 'Your system prompt' }, + { "role": 'user', "content": 'Say this is a test' } + ], + model= 'claude-3-opus-20240229', + max_tokens=250 # Required field for Anthropic +) + +print(completion.choices) +``` + + + + + + + +## Vision Chat Completion Usage + + +Portkey's multimodal Gateway fully supports Anthropic's vision models `claude-3-sonnet`, `claude-3-haiku`, `claude-3-opus`, and the latest `claude-3.5-sonnet`. +Portkey follows the OpenAI schema, which means you can send your image data to Anthropic in the same format as OpenAI. + + + + - Anthropic ONLY accepts `base64` -encoded images. Unlike OpenAI, it **does not** support `image URLs`. +- With Portkey, you can use the same format to send base64-encoded images to both Anthropic and OpenAI models. + + + + +Here's an example using Anthropic `claude-3.5-sonnet` model + + + + ```python + import base64 + import httpx + from portkey_ai import Portkey + + # Fetch and encode the image + image_url = "https://upload.wikimedia.org/wikipedia/commons/a/a7/Camponotus_flavomarginatus_ant.jpg" + image_data = base64.b64encode(httpx.get(image_url).content).decode("utf-8") + + # Initialize the Portkey client + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Add your provider's virtual key + ) + + # Create the request + response = portkey.chat.completions.create( + model="claude-3-5-sonnet-20240620", + messages=[ + { + "role": "system", + "content": "You are a helpful assistant, who describes imagse" + }, + { + "role": "user", + "content": [ + { + "type": "image_url", + "image_url": { + "url": f"data:image/jpeg;base64,{image_data}" + } + } + ] + } + ], + max_tokens=1400, + ) + print(response) + ``` + + + ```javascript + import Portkey from 'portkey-ai'; + + // Initialize the Portkey client + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY" // Add your anthropic's virtual key + }); + + // Generate a chat completion + async function getChatCompletionFunctions() { + const response = await portkey.chat.completions.create({ + model: "claude-3-5-sonnet-20240620", + messages: [ + { + role: "system", + content: "You are a helpful assistant who describes images." + }, + { + role: "user", + content: [ + { type: "text", text: "What's in this image?" }, + { + type: "image_url", + image_url: { + url: "data:image/jpeg;base64,BASE64_IMAGE_DATA" + } + } + ] + } + ], + max_tokens: 300 + }); + console.log(response); + } + // Call the function + getChatCompletionFunctions(); + ``` + + + ```javascript + import OpenAI from 'openai'; // We're using the v4 SDK + import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + + const openai = new OpenAI({ + apiKey: 'ANTHROPIC_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "anthropic", + apiKey: "PORTKEY_API_KEY" // defaults to process.env["PORTKEY_API_KEY"] + }) + }); + + // Generate a chat completion with streaming + async function getChatCompletionFunctions(){ + const response = await openai.chat.completions.create({ + model: "claude-3-5-sonnet-20240620", + messages: [ + { + role: "user", + content: [ + { type: "text", text: "What's in this image?" }, + { + type: "image_url", + image_url: + "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg", + }, + ], + }, + ], + }); + + console.log(response) + + } + await getChatCompletionFunctions(); + ``` + + + ```python + from openai import OpenAI + from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + + openai = OpenAI( + api_key='Anthropic_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="anthropic", + api_key="PORTKEY_API_KEY" + ) + ) + + + response = openai.chat.completions.create( + model="claude-3-5-sonnet-20240620", + messages=[ + { + "role": "system", + "content": "You are a helpful assistant, who describes imagse" + }, + { + "role": "user", + "content": [ + { + "type": "image_url", + "image_url": { + "url": f"data:image/jpeg;base64,{base_64_encoded_image}" + } + } + ] + } + ], + max_tokens=1400, + ) + + print(response) + ``` + + + ```sh + curl "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-provider: anthropic" \ + -H "x-api-key: $ANTHROPIC_API_KEY" \ + -d '{ + "model": "claude-3-5-sonnet-20240620", + "messages": [ + { + "role": "system", + "content": "You are a helpful assistant who describes images." + }, + { + "role": "user", + "content": [ + { + "type": "text", + "text": "What's in this image?" + }, + { + "type": "image_url", + "image_url": { + "url": "data:image/jpeg;base64,BASE64_IMAGE_DATA" + } + } + ] + } + ], + "max_tokens": 300 + }' + ``` + + + + + To prompt with pdfs, simply update the "url" field inside the "image_url" object to this pattern: `data:application/pdf;base64,BASE64_PDF_DATA` + + + +#### [API Reference](#vision-chat-completion-usage) + +On completion, the request will get logged in Portkey where any image inputs or outputs can be viewed. Portkey will automatically render the base64 images to help you debug any issues quickly. + + + +## Calude PDF Support + +Anthropic Claude can now process PDFs to extract text, analyze charts, and understand visual content from documents. With Portkey, you can seamlessly integrate this capability into your applications using the familiar OpenAI-compatible API schema. + + +PDF support is available on the following Claude models: +- Claude 3.7 Sonnet (`claude-3-7-sonnet-20250219`) +- Claude 3.5 Sonnet (`claude-3-5-sonnet-20241022`, `claude-3-5-sonnet-20240620`) +- Claude 3.5 Haiku (`claude-3-5-haiku-20241022`) + + +When using PDF support with Portkey, be aware of these limitations: +- Maximum request size: 32MB +- Maximum pages per request: 100 +- Format: Standard PDF (no passwords/encryption) + +### Processing PDFs with Claude + +Currently, Portkey supports PDF processing using base64-encoded PDF documents, following the same pattern as image handling in Claude's multimodal capabilities. + + + + ```python + from portkey_ai import Portkey + import base64 + import httpx + + # Initialize the Portkey client + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Anthropic + ) + + # Fetch and encode the PDF + pdf_url = "https://assets.anthropic.com/m/1cd9d098ac3e6467/original/Claude-3-Model-Card-October-Addendum.pdf" + pdf_data = "data:application/pdf;base64," + base64.standard_b64encode(httpx.get(pdf_url).content).decode("utf-8") + + # Alternative: Load from a local file + # with open("document.pdf", "rb") as f: + # pdf_data = "data:application/pdf;base64," + base64.standard_b64encode(f.read()).decode("utf-8") + + # Create the request + response = portkey.chat.completions.create( + model="claude-3-5-sonnet-20240620", + max_tokens=1024, + messages=[ + { + "role": "system", + "content": "You are a helpful document analysis assistant." + }, + { + "role": "user", + "content": [ + { + "type": "text", + "text": "What are the key findings in this document?" + }, + { + "type": "file", + "file": { + # "file_url": "https://pdfobject.com/pdf/sample.pdf", # if you want to pass a pdf file from a url + "mime_type": "application/pdf", + "file_data": "BASE64_PDF_DATA" + } + } + # { # if you want to pass a plain text file + # "type": "file", + # "file": { + # "mime_type": "text/plain", + # "file_data": "This is a plain text file" + # } + # } + ] + } + ] + ) + + print(response.choices[0].message.content) + ``` + + + ```javascript + import Portkey from 'portkey-ai'; + import axios from 'axios'; + import fs from 'fs'; + + // Initialize the Portkey client + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY" // Replace with your virtual key for Anthropic + }); + + async function processPdf() { + // Method 1: Fetch PDF from URL + const pdfUrl = "https://assets.anthropic.com/m/1cd9d098ac3e6467/original/Claude-3-Model-Card-October-Addendum.pdf"; + const response = await axios.get(pdfUrl, { responseType: 'arraybuffer' }); + const pdfBase64 = Buffer.from(response.data).toString('base64'); + const pdfData = `data:application/pdf;base64,${pdfBase64}`; + + // Method 2: Load PDF from local file + // const pdfFile = fs.readFileSync('document.pdf'); + // const pdfBase64 = Buffer.from(pdfFile).toString('base64'); + // const pdfData = `data:application/pdf;base64,${pdfBase64}`; + + // Send to Claude + const result = await portkey.chat.completions.create({ + model: "claude-3-5-sonnet-20240620", + max_tokens: 1024, + messages: [ + { + role: "system", + content: "You are a helpful document analysis assistant." + }, + { + role: "user", + content: [ + { + type: "text", + text: "What are the key findings in this document?" + }, + { + type: "file", + file: { + mime_type: "application/pdf", + file_data: pdfData, + // file_url: "https://pdfobject.com/pdf/sample.pdf" // if you want to pass a pdf file from a url + } + }, + // { // if you want to pass a plain text file + // "type": "file", + // "file": { + // "mime_type": "text/plain", + // "file_data": "This is a plain text file" + // } + // } + ] + } + ] + }); + + console.log(result.choices[0].message.content); + } + + processPdf(); + ``` + + + ```sh + # First, encode your PDF to base64 (this example uses a command line approach) + # For example using curl + base64: + PDF_BASE64=$(curl -s "https://assets.anthropic.com/m/1cd9d098ac3e6467/original/Claude-3-Model-Card-October-Addendum.pdf" | base64) + + # Alternatively, from a local file: + # PDF_BASE64=$(base64 -i document.pdf) + + # Then make the API call with the base64-encoded PDF + curl "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-provider: anthropic" \ + -H "x-api-key: $ANTHROPIC_API_KEY" \ + -d '{ + "model": "claude-3-5-sonnet-20240620", + "max_tokens": 1024, + "messages": [ + { + "role": "system", + "content": "You are a helpful document analysis assistant." + }, + { + "role": "user", + "content": [ + { + "type": "text", + "text": "What are the key findings in this document?" + }, + { + "type": "file", + "file": { + "mime_type": "application/pdf", + "file_data": "BASE64_PDF_DATA" + } + } + // { // if you want to pass a plain text file + // "type": "file", + // "file": { + // "mime_type": "text/plain", + // "file_data": "This is a plain text file" + // } + // } + ] + } + ] + }' + ``` + + + + +### Best Practices for PDF Processing + +For optimal results when working with PDFs: + +- Place PDFs before any text content in your requests +- Ensure PDFs contain standard fonts and clear, legible text +- Verify that pages are properly oriented +- Split large PDFs into smaller chunks when they approach size limits +- Be specific in your questions to get more targeted analysis + +### Calculating Costs + +When processing PDFs, token usage is calculated based on both text content and the visual representation of pages: + +- Text tokens: Typically 1,500-3,000 tokens per page, depending on content density +- Image tokens: Each page converted to an image adds to the token count similar to image processing + + + +**For more info, check out this guide:** + + + + +## Prompt Caching + +Portkey also works with Anthropic's new prompt caching feature and helps you save time & money for all your Anthropic requests. Refer to this guide to learn how to enable it: + + + + + +## Extended Thinking (Reasoning Models) (Beta) + + +The assistants thinking response is returned in the `response_chunk.choices[0].delta.content_blocks` array, not the `response.choices[0].message.content` string. + + +Models like `claude-3-7-sonnet-latest` support [extended thinking](https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking#streaming-extended-thinking). +This is similar to openai thinking, but you get the model's reasoning as it processes the request as well. + +Note that you will have to set [`strict_open_ai_compliance=False`](/product/ai-gateway/strict-open-ai-compliance) in the headers to use this feature. + +### Single turn conversation + + ```py Python + from portkey_ai import Portkey + + # Initialize the Portkey client + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + strict_open_ai_compliance=False + ) + + # Create the request + response = portkey.chat.completions.create( + model="claude-3-7-sonnet-latest", + max_tokens=3000, + thinking={ + "type": "enabled", + "budget_tokens": 2030 + }, + stream=False, + messages=[ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "when does the flight from new york to bengaluru land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + } + ] + ) + print(response) + + # in case of streaming responses you'd have to parse the response_chunk.choices[0].delta.content_blocks array + # response = portkey.chat.completions.create( + # ...same config as above but with stream: true + # ) + # for chunk in response: + # if chunk.choices[0].delta: + # content_blocks = chunk.choices[0].delta.get("content_blocks") + # if content_blocks is not None: + # for content_block in content_blocks: + # print(content_block) + + ``` + ```ts NodeJS + import Portkey from 'portkey-ai'; + + // Initialize the Portkey client + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY", // Add your anthropic's virtual key + strictOpenAiCompliance: false + }); + + // Generate a chat completion + async function getChatCompletionFunctions() { + const response = await portkey.chat.completions.create({ + model: "claude-3-7-sonnet-latest", + max_tokens: 3000, + thinking: { + type: "enabled", + budget_tokens: 2030 + }, + stream: false, + messages: [ + { + role: "user", + content: [ + { + type: "text", + text: "when does the flight from new york to bengaluru land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + } + ] + }); + console.log(response); + // in case of streaming responses you'd have to parse the response_chunk.choices[0].delta.content_blocks array + // const response = await portkey.chat.completions.create({ + // ...same config as above but with stream: true + // }); + // for await (const chunk of response) { + // if (chunk.choices[0].delta?.content_blocks) { + // for (const contentBlock of chunk.choices[0].delta.content_blocks) { + // console.log(contentBlock); + // } + // } + // } + } + // Call the function + getChatCompletionFunctions(); + ``` + ```js OpenAI NodeJS + import OpenAI from 'openai'; // We're using the v4 SDK + import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + + const openai = new OpenAI({ + apiKey: 'ANTHROPIC_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "anthropic", + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + strictOpenAiCompliance: false + }) + }); + + // Generate a chat completion with streaming + async function getChatCompletionFunctions(){ + const response = await openai.chat.completions.create({ + model: "claude-3-7-sonnet-latest", + max_tokens: 3000, + thinking: { + type: "enabled", + budget_tokens: 2030 + }, + stream: false, + messages: [ + { + role: "user", + content: [ + { + type: "text", + text: "when does the flight from new york to bengaluru land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + } + ], + }); + + console.log(response) + // in case of streaming responses you'd have to parse the response_chunk.choices[0].delta.content_blocks array + // const response = await openai.chat.completions.create({ + // ...same config as above but with stream: true + // }); + // for await (const chunk of response) { + // if (chunk.choices[0].delta?.content_blocks) { + // for (const contentBlock of chunk.choices[0].delta.content_blocks) { + // console.log(contentBlock); + // } + // } + // } + } + await getChatCompletionFunctions(); + ``` + ```py OpenAI Python + from openai import OpenAI + from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + + openai = OpenAI( + api_key='Anthropic_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="anthropic", + api_key="PORTKEY_API_KEY", + strict_open_ai_compliance=False + ) + ) + + + response = openai.chat.completions.create( + model="claude-3-7-sonnet-latest", + max_tokens=3000, + thinking={ + "type": "enabled", + "budget_tokens": 2030 + }, + stream=False, + messages=[ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "when does the flight from new york to bengaluru land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + } + ] + ) + + print(response) + ``` + ```sh cURL + curl "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-provider: anthropic" \ + -H "x-api-key: $ANTHROPIC_API_KEY" \ + -H "x-portkey-strict-open-ai-compliance: false" \ + -d '{ + "model": "claude-3-7-sonnet-latest", + "max_tokens": 3000, + "thinking": { + "type": "enabled", + "budget_tokens": 2030 + }, + "stream": false, + "messages": [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "when does the flight from new york to bengaluru land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + } + ] + }' + ``` + + +### Multi turn conversation + + + ```py Python + from portkey_ai import Portkey + + # Initialize the Portkey client + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + strict_open_ai_compliance=False + ) + + # Create the request + response = portkey.chat.completions.create( + model="claude-3-7-sonnet-latest", + max_tokens=3000, + thinking={ + "type": "enabled", + "budget_tokens": 2030 + }, + stream=False, + messages=[ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "when does the flight from baroda to bangalore land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + }, + { + "role": "assistant", + "content": [ + { + "type": "thinking", + "thinking": "The user is asking several questions about a flight from Baroda (also known as Vadodara) to Bangalore:\n1. When does the flight land tomorrow\n2. What time does it land\n3. What is the flight number\n4. What is the baggage belt number at the arrival airport\n\nTo properly answer these questions, I would need access to airline flight schedules and airport information systems. However, I don't have:\n- Real-time or scheduled flight information\n- Access to airport baggage claim allocation systems\n- Information about specific flights between these cities\n- The ability to look up tomorrow's specific flight schedules\n\nThis question requires current, specific flight information that I don't have access to. Instead of guessing or providing potentially incorrect information, I should explain this limitation and suggest ways the user could find this information.", + "signature": "EqoBCkgIARABGAIiQBVA7FBNLRtWarDSy9TAjwtOpcTSYHJ+2GYEoaorq3V+d3eapde04bvEfykD/66xZXjJ5yyqogJ8DEkNMotspRsSDKzuUJ9FKhSNt/3PdxoMaFZuH+1z1aLF8OeQIjCrA1+T2lsErrbgrve6eDWeMvP+1sqVqv/JcIn1jOmuzrPi2tNz5M0oqkOO9txJf7QqEPPw6RG3JLO2h7nV1BMN6wE=" + } + ] + }, + { + "role": "user", + "content": "thanks that's good to know, how about to chennai?" + } + ] + ) + print(response) + ``` + ```ts NodeJS + import Portkey from 'portkey-ai'; + + // Initialize the Portkey client + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY", // Add your anthropic's virtual key + strictOpenAiCompliance: false + }); + + // Generate a chat completion + async function getChatCompletionFunctions() { + const response = await portkey.chat.completions.create({ + model: "claude-3-7-sonnet-latest", + max_tokens: 3000, + thinking: { + type: "enabled", + budget_tokens: 2030 + }, + stream: false, + messages: [ + { + role: "user", + content: [ + { + type: "text", + text: "when does the flight from baroda to bangalore land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + }, + { + role: "assistant", + content: [ + { + type: "thinking", + thinking: "The user is asking several questions about a flight from Baroda (also known as Vadodara) to Bangalore:\n1. When does the flight land tomorrow\n2. What time does it land\n3. What is the flight number\n4. What is the baggage belt number at the arrival airport\n\nTo properly answer these questions, I would need access to airline flight schedules and airport information systems. However, I don't have:\n- Real-time or scheduled flight information\n- Access to airport baggage claim allocation systems\n- Information about specific flights between these cities\n- The ability to look up tomorrow's specific flight schedules\n\nThis question requires current, specific flight information that I don't have access to. Instead of guessing or providing potentially incorrect information, I should explain this limitation and suggest ways the user could find this information.", + signature: "EqoBCkgIARABGAIiQBVA7FBNLRtWarDSy9TAjwtOpcTSYHJ+2GYEoaorq3V+d3eapde04bvEfykD/66xZXjJ5yyqogJ8DEkNMotspRsSDKzuUJ9FKhSNt/3PdxoMaFZuH+1z1aLF8OeQIjCrA1+T2lsErrbgrve6eDWeMvP+1sqVqv/JcIn1jOmuzrPi2tNz5M0oqkOO9txJf7QqEPPw6RG3JLO2h7nV1BMN6wE=" + } + ] + }, + { + role: "user", + content: "thanks that's good to know, how about to chennai?" + } + ] + }); + console.log(response); + } + // Call the function + getChatCompletionFunctions(); + ``` + ```js OpenAI NodeJS + import OpenAI from 'openai'; // We're using the v4 SDK + import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + + const openai = new OpenAI({ + apiKey: 'ANTHROPIC_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "anthropic", + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + strict_open_ai_compliance: false + }) + }); + + // Generate a chat completion with streaming + async function getChatCompletionFunctions(){ + const response = await openai.chat.completions.create({ + model: "claude-3-7-sonnet-latest", + max_tokens: 3000, + thinking: { + type: "enabled", + budget_tokens: 2030 + }, + stream: false, + messages: [ + { + role: "user", + content: [ + { + type: "text", + text: "when does the flight from baroda to bangalore land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + }, + { + role: "assistant", + content: [ + { + type: "thinking", + thinking: "The user is asking several questions about a flight from Baroda (also known as Vadodara) to Bangalore:\n1. When does the flight land tomorrow\n2. What time does it land\n3. What is the flight number\n4. What is the baggage belt number at the arrival airport\n\nTo properly answer these questions, I would need access to airline flight schedules and airport information systems. However, I don't have:\n- Real-time or scheduled flight information\n- Access to airport baggage claim allocation systems\n- Information about specific flights between these cities\n- The ability to look up tomorrow's specific flight schedules\n\nThis question requires current, specific flight information that I don't have access to. Instead of guessing or providing potentially incorrect information, I should explain this limitation and suggest ways the user could find this information.", + signature: "EqoBCkgIARABGAIiQBVA7FBNLRtWarDSy9TAjwtOpcTSYHJ+2GYEoaorq3V+d3eapde04bvEfykD/66xZXjJ5yyqogJ8DEkNMotspRsSDKzuUJ9FKhSNt/3PdxoMaFZuH+1z1aLF8OeQIjCrA1+T2lsErrbgrve6eDWeMvP+1sqVqv/JcIn1jOmuzrPi2tNz5M0oqkOO9txJf7QqEPPw6RG3JLO2h7nV1BMN6wE=" + } + ] + }, + { + role: "user", + content: "thanks that's good to know, how about to chennai?" + } + ], + }); + + console.log(response) + + } + await getChatCompletionFunctions(); + ``` + ```py OpenAI Python + from openai import OpenAI + from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + + openai = OpenAI( + api_key='Anthropic_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="anthropic", + api_key="PORTKEY_API_KEY", + strict_open_ai_compliance=False + ) + ) + + + response = openai.chat.completions.create( + model="claude-3-7-sonnet-latest", + max_tokens=3000, + thinking={ + "type": "enabled", + "budget_tokens": 2030 + }, + stream=False, + messages=[ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "when does the flight from baroda to bangalore land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + }, + { + "role": "assistant", + "content": [ + { + "type": "thinking", + "thinking": "The user is asking several questions about a flight from Baroda (also known as Vadodara) to Bangalore:\n1. When does the flight land tomorrow\n2. What time does it land\n3. What is the flight number\n4. What is the baggage belt number at the arrival airport\n\nTo properly answer these questions, I would need access to airline flight schedules and airport information systems. However, I don't have:\n- Real-time or scheduled flight information\n- Access to airport baggage claim allocation systems\n- Information about specific flights between these cities\n- The ability to look up tomorrow's specific flight schedules\n\nThis question requires current, specific flight information that I don't have access to. Instead of guessing or providing potentially incorrect information, I should explain this limitation and suggest ways the user could find this information.", + signature: "EqoBCkgIARABGAIiQBVA7FBNLRtWarDSy9TAjwtOpcTSYHJ+2GYEoaorq3V+d3eapde04bvEfykD/66xZXjJ5yyqogJ8DEkNMotspRsSDKzuUJ9FKhSNt/3PdxoMaFZuH+1z1aLF8OeQIjCrA1+T2lsErrbgrve6eDWeMvP+1sqVqv/JcIn1jOmuzrPi2tNz5M0oqkOO9txJf7QqEPPw6RG3JLO2h7nV1BMN6wE=" + } + ] + }, + { + "role": "user", + "content": "thanks that's good to know, how about to chennai?" + } + ] + ) + + print(response) + ``` + ```sh cURL + curl "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-provider: anthropic" \ + -H "x-api-key: $ANTHROPIC_API_KEY" \ + -H "x-portkey-strict-open-ai-compliance: false" \ + -d '{ + "model": "claude-3-7-sonnet-latest", + "max_tokens": 3000, + "thinking": { + "type": "enabled", + "budget_tokens": 2030 + }, + "stream": false, + "messages": [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "when does the flight from baroda to bangalore land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + }, + { + "role": "assistant", + "content": [ + { + "type": "thinking", + "thinking": "The user is asking several questions about a flight from Baroda (also known as Vadodara) to Bangalore:\n1. When does the flight land tomorrow\n2. What time does it land\n3. What is the flight number\n4. What is the baggage belt number at the arrival airport\n\nTo properly answer these questions, I would need access to airline flight schedules and airport information systems. However, I don't have:\n- Real-time or scheduled flight information\n- Access to airport baggage claim allocation systems\n- Information about specific flights between these cities\n- The ability to look up tomorrow's specific flight schedules\n\nThis question requires current, specific flight information that I don't have access to. Instead of guessing or providing potentially incorrect information, I should explain this limitation and suggest ways the user could find this information.", + "signature": "EqoBCkgIARABGAIiQBVA7FBNLRtWarDSy9TAjwtOpcTSYHJ+2GYEoaorq3V+d3eapde04bvEfykD/66xZXjJ5yyqogJ8DEkNMotspRsSDKzuUJ9FKhSNt/3PdxoMaFZuH+1z1aLF8OeQIjCrA1+T2lsErrbgrve6eDWeMvP+1sqVqv/JcIn1jOmuzrPi2tNz5M0oqkOO9txJf7QqEPPw6RG3JLO2h7nV1BMN6wE=" + } + ] + }, + { + "role": "user", + "content": "thanks that's good to know, how about to chennai?" + } + ] + }' + ``` + + + + Extended thinking API through portkey is currently in beta. + + + +## Beta features + +Portkey supports anthropic's beta features through this header, just pass the beta feature name as the value: + + +```sh cURL +"x-portkey-anthropic-beta": "token-efficient-tools-2025-02-19" +``` +for the SDKs, you can pass it during the client initialization, or with createHeaders if you're using the OpenAI SDK: + +```py python +from portkey_ai import Portkey + +client = Portkey( + api_key="PORTKEY_API_KEY", + virtual_key="VIRTUAL_KEY", + anthropic_beta="token-efficient-tools-2025-02-19", + strict_open_ai_compliance=False +) +``` + +```ts NodeJS +import Portkey from 'portkey-ai'; + +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "VIRTUAL_KEY", + anthropicBeta: "token-efficient-tools-2025-02-19", + strictOpenAiCompliance: false +}); +``` + +```py OpenAI Python +from portkey_ai import createHeaders + +headers = createHeaders( + provider="anthropic", + api_key="PORTKEY_API_KEY", + virtual_key="VIRTUAL_KEY", + strict_open_ai_compliance=False, + anthropic_beta="token-efficient-tools-2025-02-19" +) +``` + +```ts OpenAI NodeJS +import { createHeaders } from 'portkey-ai' + +const headers = createHeaders({ + provider: "anthropic", + apiKey: "PORTKEY_API_KEY", + virtual_key="VIRTUAL_KEY", + strictOpenAiCompliance: false, + anthropicBeta: "token-efficient-tools-2025-02-19" +}) +``` + + +## Managing Anthropic Prompts + +You can manage all prompts to Anthropic in the [Prompt Library](/product/prompt-library). All the current models of Anthropic are supported and you can easily start testing different prompts. + +Once you're ready with your prompt, you can use the `portkey.prompts.completions.create` interface to use the prompt in your application. + +## Next Steps + +The complete list of features supported in the SDK are available on the link below. + + + + + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your Anthropic requests](/product/ai-gateway/configs) +3. [Tracing Anthropic requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to Anthropic's Claude APIs](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/anthropic/computer-use.mdx b/virtual_key_old/integrations/llms/anthropic/computer-use.mdx new file mode 100644 index 00000000..8cc0dc89 --- /dev/null +++ b/virtual_key_old/integrations/llms/anthropic/computer-use.mdx @@ -0,0 +1,4 @@ +--- +title: Computer use tool +url: /integrations/libraries/anthropic-computer-use +--- diff --git a/virtual_key_old/integrations/llms/anthropic/prompt-caching.mdx b/virtual_key_old/integrations/llms/anthropic/prompt-caching.mdx new file mode 100644 index 00000000..82bf8c26 --- /dev/null +++ b/virtual_key_old/integrations/llms/anthropic/prompt-caching.mdx @@ -0,0 +1,199 @@ +--- +title: 'Prompt Caching' +--- + +Prompt caching on Anthropic lets you cache individual messages in your request for repeat use. With caching, you can free up your tokens to include more context in your prompt, and also deliver responses significantly faster and cheaper. + +You can use this feature on our OpenAI-compliant universal API as well as with our prompt templates. + +## API Support + +Just set the `cache_control` param in your respective message body: + + + +```javascript NodeJS +import Portkey from 'portkey-ai' + +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your Anthropic Virtual Key +}) + +const chatCompletion = await portkey.chat.completions.create({ + messages: [ + { "role": 'system', "content": [ + { + "type":"text","text":"You are a helpful assistant" + }, + { + "type":"text","text":"", + "cache_control": {"type": "ephemeral"} + } + ]}, + { "role": 'user', "content": 'Summarize the above story for me in 20 words' } + ], + model: 'claude-3-5-sonnet-20240620', + max_tokens: 250 // Required field for Anthropic +}); + +console.log(chatCompletion.choices[0].message.content); +``` + +```python Python +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY", + virtual_key="ANTHROPIC_VIRTUAL_KEY", +) + +chat_completion = portkey.chat.completions.create( + messages= [ + { "role": 'system', "content": [ + { + "type":"text","text":"You are a helpful assistant" + }, + { + "type":"text","text":"", + "cache_control": {"type": "ephemeral"} + } + ]}, + { "role": 'user', "content": 'Summarize the above story in 20 words' } + ], + model= 'claude-3-5-sonnet-20240620', + max_tokens=250 +) + +print(chat_completion.choices[0].message.content) +``` + +```javascript OpenAI NodeJS +import OpenAI from "openai"; +import { PORTKEY_GATEWAY_URL, createHeaders } from "portkey-ai"; + +const portkey = new OpenAI({ + apiKey: "ANTHROPIC_API_KEY", + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "anthropic", + apiKey: "PORTKEY_API_KEY", + }), +}); + +const chatCompletion = await portkey.chat.completions.create({ + messages: [ + { "role": 'system', "content": [ + { + "type":"text","text":"You are a helpful assistant" + }, + { + "type":"text","text":"", + "cache_control": {"type": "ephemeral"} + } + ]}, + { "role": 'user', "content": 'Summarize the above story for me in 20 words' } + ], + model: 'claude-3-5-sonnet-20240620', + max_tokens: 250 +}); + +console.log(chatCompletion.choices[0].message.content); +``` + +```python OpenAI Python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +client = OpenAI( + api_key="ANTHROPIC_API_KEY", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key="PORTKEY_API_KEY", + provider="anthropic", + ) +) + +chat_completion = portkey.chat.completions.create( + messages= [ + { "role": 'system', "content": [ + { + "type":"text","text":"You are a helpful assistant" + }, + { + "type":"text","text":"", + "cache_control": {"type": "ephemeral"} + } + ]}, + { "role": 'user', "content": 'Summarize the above story in 20 words' } + ], + model= 'claude-3-5-sonnet-20240620', + max_tokens=250 +) + +print(chat_completion.choices[0].message.content) +``` + +```sh REST API +curl https://api.portkey.ai/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $ANTHROPIC_API_KEY" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-provider: anthropic" \ + -d '{ + "model": "claude-3-5-sonnet-20240620", + "max_tokens": 1024, + "messages": [ + { "role": "system", "content": [ + { + "type":"text","text":"You are a helpful assistant" + }, + { + "type":"text","text":"", + "cache_control": {"type": "ephemeral"} + } + ]}, + { "role": "user", "content": "Summarize the above story for me in 20 words" } + ] + }' +``` + + + +## Prompt Templates Support + +Set any message in your prompt template to be cached by just toggling the `Cache Control` setting in the UI: + + + + + + +Anthropic currently has certain restrictions on prompt caching, like: + +* Cache TTL is set at **5 minutes** and can not be changed +* The message you are caching needs to cross minimum length to enable this feature + * 1024 tokens for Claude 3.5 Sonnet and Claude 3 Opus + * 2048 tokens for Claude 3 Haiku + + +For more, refer to Anthropic's prompt caching documentation [here](https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching). + + + +## Seeing Cache Results in Portkey + +Portkey automatically calculate the correct pricing for your prompt caching requests & responses based on Anthropic's calculations here: + + + Anthropic's pricing calculations + + +In the individual log for any request, you can also see the exact status of your request and verify if it was cached, or delivered from cache with two `usage` parameters: + +* `cache_creation_input_tokens`: Number of tokens written to the cache when creating a new entry. +* `cache_read_input_tokens`: Number of tokens retrieved from the cache for this request. + + + Cache status in Portkey logs + diff --git a/virtual_key_old/integrations/llms/anyscale-llama2-mistral-zephyr.mdx b/virtual_key_old/integrations/llms/anyscale-llama2-mistral-zephyr.mdx new file mode 100644 index 00000000..743bec60 --- /dev/null +++ b/virtual_key_old/integrations/llms/anyscale-llama2-mistral-zephyr.mdx @@ -0,0 +1,356 @@ +--- +title: "Anyscale" +description: "Integrate Anyscale endpoints with Portkey seamlessly and make your OSS models production-ready" +--- + +Portkey's suite of features - AI gateway, observability, prompt management, and continuous fine-tuning are all enabled for the OSS models (Llama2, Mistral, Zephyr, and more) available on Anyscale endpoints. + +Provider Slug. `anyscale` + +## Portkey SDK Integration with Anyscale + +### 1\. Install the Portkey SDK + + + ```sh + npm install --save portkey-ai + ``` + + + ```sh + pip install portkey-ai + ``` + + + + + + +### 2\. Initialize Portkey with Anyscale Virtual Key + +To use Anyscale with Portkey, [get your Anyscale API key from here](https://console.anyscale.com/v2/api-keys), then add it to Portkey to create the virtual key. + + + +```js +import Portkey from 'portkey-ai' + +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "ANYSCALE_VIRTUAL_KEY" // Your Anyscale Virtual Key +}) +``` + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="ANYSCALE_VIRTUAL_KEY" # Replace with your virtual key for Anyscale + ) + ``` + + + + + + +### **3\. Invoke Chat Completions with Anyscale** + + + ```js + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'mistralai/Mistral-7B-Instruct-v0.1', + }); + + console.log(chatCompletion.choices); + ``` + + + ```python + completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model= 'mistralai/Mistral-7B-Instruct-v0.1' + ) + + print(completion.choices) + ``` + + + + + + + +### Directly Using Portkey's REST API + +Alternatively, you can also directly call Anyscale models through Portkey's REST API - it works exactly the same as OpenAI API, with 2 differences: + +1. You send your requests to Portkey's complete Gateway URL `https://api.portkey.ai/v1/chat/completions` +2. You have to add Portkey specific headers. + 1. `x-portkey-api-key` for sending your Portkey API Key + 2. `x-portkey-virtual-key` for sending your provider's virtual key (Alternatively, if you are not using Virtual keys, you can send your Auth header for your provider, and pass the `x-portkey-provider` header along with it) + + + + + + ```sh + curl https://api.portkey.ai/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $ANYSCALE_API_KEY" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-provider: anyscale" \ + -d '{ + "model": "mistralai/Mistral-7B-Instruct-v0.1", + "messages": [{"role": "user","content": "Hello!"}] + }' + ``` + + [List of all possible Portkey headers](/api-reference/portkey-sdk-client#parameters). + + + + + + + +## Using the OpenAI Python or Node SDKs for Anyscale + +You can also use the `baseURL` param in the standard OpenAI SDKs and make calls to Portkey + Anyscale directly from there. Like the Rest API example, you are only required to change the `baseURL` and add `defaultHeaders` to your instance. You can use the Portkey SDK to make it simpler: + + +```js +import OpenAI from 'openai'; // We're using the v4 SDK +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const anyscale = new OpenAI({ + apiKey: 'ANYSCALE_API_KEY', + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "anyscale", + apiKey: "PORTKEY_API_KEY" // defaults to process.env["PORTKEY_API_KEY"] + }) +}); + +async function main() { + const chatCompletion = await anyscale.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'mistralai/Mistral-7B-Instruct-v0.1', + }); + + console.log(chatCompletion.choices); + +} + +main(); +``` + + + + ```python + from openai import OpenAI + from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + + anyscale = OpenAI( + api_key="ANYSCALE_API_KEY", # defaults to os.environ.get("OPENAI_API_KEY") + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="anyscale", + api_key="PORTKEY_API_KEY" # defaults to os.environ.get("PORTKEY_API_KEY") + ) + ) + + chat_complete = anyscale.chat.completions.create( + model="mistralai/Mistral-7B-Instruct-v0.1", + messages=[{"role": "user", "content": "Say this is a test"}], + ) + + print(chat_complete.choices[0].message.content) + ``` + + + + +This request will be automatically logged by Portkey. You can view this in your logs dashboard. Portkey logs the tokens utilized, execution time, and cost for each request. Additionally, you can delve into the details to review the precise request and response data. + +## Managing Anyscale Prompts + +You can manage all prompts for Anyscale's OSS models in the [Prompt Library](/product/prompt-library). All the current models of Anyscale are supported. + +### Creating Prompts + +Use the Portkey prompt playground to set variables and try out various model params to get the right output. + +![Logo](/images/llms/promp.png) + +### Using Prompts + +Deploy the prompts using the Portkey SDK or REST API + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + }) + + // Make the prompt creation call with the variables + + const promptCompletion = await portkey.prompts.completions.create({ + promptID: "YOUR_PROMPT_ID", + variables: { + //Required variables for prompt + } + }) + ``` + We can also override the hyperparameters: + +```js +const promptCompletion = await portkey.prompts.completions.create({ + promptID: "YOUR_PROMPT_ID", + variables: { + //Required variables for prompt + }, + max_tokens: 250, + presence_penalty: 0.2 +}) +``` + + + ```python + from portkey_ai import Portkey + + client = Portkey( + api_key="PORTKEY_API_KEY", # defaults to os.environ.get("PORTKEY_API_KEY") + ) + + prompt_completion = client.prompts.completions.create( + prompt_id="YOUR_PROMPT_ID", + variables={ + #Required variables for prompt + } + ) + + print(prompt_completion.data) + ``` + + We can also override the hyperparameters: + + ```python + prompt_completion = client.prompts.completions.create( + prompt_id="YOUR_PROMPT_ID", + variables={ + #Required variables for prompt + }, + max_tokens=250, + presence_penalty=0.2 + ) + + print(prompt_completion.data) + ``` + + + +```sh +curl -X POST "https://api.portkey.ai/v1/prompts/9218b4e6-52db-41a4-b963-4ee6505ed758/completions" \ +-H "Content-Type: application/json" \ +-H "x-portkey-api-key: $PORTKEY_API_KEY" \ +-d '{ + "variables": { + "title": "The impact of AI on middle school teachers", + "num_sections": "5" + }, + "max_tokens": 250, # Optional + "presence_penalty": 0.2 # Optional +}' +``` + + + + + +Observe how this streamlines your code readability and simplifies prompt updates via the UI without altering the codebase. + +--- + +## Advanced Use Cases + +### Streaming Responses + +Portkey supports streaming responses using Server Sent Events (SSE). + + + +```js +import OpenAI from 'openai'; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const anyscale = new OpenAI({ + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + mode: "anyscale", + apiKey: "PORTKEY_API_KEY" // defaults to process.env["PORTKEY_API_KEY"] + }) +}); + +async function main() { + const stream = await anyscale.chat.completions.create({ + model: 'mistralai/Mistral-7B-Instruct-v0.1', + messages: [{ role: 'user', content: 'Say this is a test' }], + stream: true, + }); + + for await (const chunk of stream) { + process.stdout.write(chunk.choices[0]?.delta?.content || ''); + } +} + +main(); +``` + + + ```python + from openai import OpenAI + from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + + anyscale = OpenAI( + api_key="ANYSCALE-API-KEY", # defaults to os.environ.get("OPENAI_API_KEY") + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="anyscale", + api_key="PORTKEY-API-KEY" # defaults to os.environ.get("PORTKEY_API_KEY") + ) + ) + + chat_complete = anyscale.chat.completions.create( + model="mistralai/Mistral-7B-Instruct-v0.1", + messages=[{"role": "user", "content": "Say this is a test"}], + stream=True + ) + + for chunk in chat_complete: + print(chunk.choices[0].delta.content, end="", flush=True) + ``` + + + +### Fine-tuning + +Please refer to our fine-tuning guides to take advantage of Portkey's advanced [continuous fine-tuning](/product/autonomous-fine-tuning) capabilities. + +![Logo](/images/llms/setup.png) + +### Portkey Features + +Portkey supports the complete host of it's functionality via the OpenAI SDK so you don't need to migrate away from it. + +Please find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to the client or a single request](/product/ai-gateway/configs) +3. [Trace Anyscale requests](/product/observability/traces) +4. [Setup a fallback to Azure OpenAI](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/aws-sagemaker.mdx b/virtual_key_old/integrations/llms/aws-sagemaker.mdx new file mode 100644 index 00000000..bec08e0b --- /dev/null +++ b/virtual_key_old/integrations/llms/aws-sagemaker.mdx @@ -0,0 +1,236 @@ +--- +title: "AWS SageMaker" +description: Route to your AWS Sagemaker models through Portkey +--- + +Sagemaker allows users to host any ML model on their own AWS infrastructure. + +With portkey you can manage/restrict access, log requests, and more. + + +Provider Slug. `sagemaker` + + +## Portkey SDK Integration with AWS Sagemaker + +### 1\. Install the Portkey SDK + +Add the Portkey SDK to your application to interact with Sagemaker's API through Portkey's gateway. + + + + + +```sh +npm install --save portkey-ai +``` + + + ```sh + pip install portkey-ai + ``` + + + + +### 2\. Initialize Portkey with a Virtual Key + +There are multiple ways to integrate Sagemaker with Portkey. +You can use your AWS credentials, or use an assumed role. + +In this example we will create a virtual key and use it to interact with Sagemaker. +This helps you restrict access (specific models, few endpoints, etc). + + + +Here's how to find your AWS credentials: + + +
Use your `AWS Secret Access Key`, `AWS Access Key Id`, and `AWS Region` to create your Virtual key.

+ +[**Integration Guide**](/integrations/llms/aws-bedrock#how-to-find-your-aws-credentials) +
+ +
Take your `AWS Assumed Role ARN` and `AWS Region` to create the virtaul key.


+ +[**Integration Guide**](/product/ai-gateway/virtual-keys/bedrock-amazon-assumed-role) +
+
+
+ +Create a virtual key in the Portkey dashboard in the virtual keys section. +You can select sagemaker as the provider, and fill in deployment details. + +Initialize the Portkey SDK with the virtual key. (If you are using the REST API, skip to next step) + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY" // Replace with your Sagemaker Virtual Key + }) + ``` + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your Sagemaker Virtual Key + ) + ``` + + + +### 3\. Invoke the Sagemaker model + + + + + ```python + response = portkey.post( + url="endpoints/{endpoint_name}/invocations", + # You can pass any key value pair required by the model, apart from `url`, they are passed as kwargs to the Sagemaker endpoint + inputs="my_custom_value", + my_custom_key="my_custom_value", + ) + + print(response) + ``` + + + ```js + const response = await portkey.post( + url="endpoints/{endpoint_name}/invocations", + // You can pass any key value pair required by the model, apart from `url`, they are passed as kwargs to the Sagemaker endpoint + inputs="my_custom_value", + my_custom_key="my_custom_value", + ) + + + console.log(response); + ``` + + + ```cURL + curl --location 'https://api.portkey.ai/v1/endpoints/{endpoint_name}/invocations' \ + --header 'x-portkey-virtual-key: {VIRTUAL_KEY}' \ + --header 'x-portkey-api-key: {PORTKEY_API_KEY}' \ + --header 'Content-Type: application/json' \ + --data '{ + # You can pass any key value pair required by the model, they are passed as kwargs to the Sagemaker endpoint + "inputs": "my_custom_value", + "my_custom_key": "my_custom_value" + }' + ``` + + + + +## Making Requests without Virtual Keys + +If you do not want to add your AWS details to Portkey vault, you can also directly pass them while instantiating the Portkey client. + +These are the supported headers/parameters for Sagemaker (Not required if you're using a virtual key): + + +| Node SDK | Python SDK | REST Headers | +| ----------------------------------- | -------------------------------------- | ------------------------------------------------- | +| awsAccessKeyId | aws_access_key_id | x-portkey-aws-access-key-id | +| awsSecretAccessKey | aws_secret_access_key | x-portkey-aws-secret-access-key | +| awsRegion | aws_region | x-portkey-aws-region | +| awsSessionToken | aws_session_token | x-portkey-aws-session-token | +| sagemakerCustomAttributes | sagemaker_custom_attributes | x-portkey-amzn-sagemaker-custom-attributes | +| sagemakerTargetModel | sagemaker_target_model | x-portkey-amzn-sagemaker-target-model | +| sagemakerTargetVariant | sagemaker_target_variant | x-portkey-amzn-sagemaker-target-variant | +| sagemakerTargetContainerHostname | sagemaker_target_container_hostname | x-portkey-amzn-sagemaker-target-container-hostname | +| sagemakerInferenceId | sagemaker_inference_id | x-portkey-amzn-sagemaker-inference-id | +| sagemakerEnableExplanations | sagemaker_enable_explanations | x-portkey-amzn-sagemaker-enable-explanations | +| sagemakerInferenceComponent | sagemaker_inference_component | x-portkey-amzn-sagemaker-inference-component | +| sagemakerSessionId | sagemaker_session_id | x-portkey-amzn-sagemaker-session-id | +| sagemakerModelName | sagemaker_model_name | x-portkey-amzn-sagemaker-model-name | + +### Example + + + + +```python +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + provider="sagemaker", + aws_region="us-east-1", # Replace with your AWS region + aws_access_key_id="AWS_ACCESS_KEY_ID", # Replace with your AWS access key id + aws_secret_access_key="AWS_SECRET_ACCESS_KEY", # Replace with your AWS secret access key + amzn_sagemaker_inference_component="SAGEMAKER_INFERENCE_COMPONENT" # Replace with your Sagemaker inference component +) + +response = portkey.post( + url="endpoints/{endpoint_name}/invocations", + # You can pass any key value pair required by the model, apart from `url`, they are passed as kwargs to the Sagemaker endpoint + inputs="my_custom_value", + my_custom_key="my_custom_value" +) + +print(response) +``` + + + +```js +import Portkey from 'portkey-ai' + +const portkey = new Portkey({ + api_key:"PORTKEY_API_KEY", + provider:"sagemaker", + aws_access_key_id:"AWS_ACCESS_KEY_ID", + aws_secret_access_key:"AWS_SECRET_ACCESS_KEY", + aws_region:"us-east-1", + amzn_sagemaker_inference_component:"SAGEMAKER_INFERENCE_COMPONENT" +}) + +const response = await portkey.post( + url="endpoints/{endpoint_name}/invocations", + // You can pass any key value pair required by the model, apart from `url`, they are passed as kwargs to the Sagemaker endpoint + inputs="my_custom_value", + my_custom_key="my_custom_value" +) + +console.log(response) +``` + + + +```sh +curl https://api.portkey.ai/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-provider: sagemaker" \ + -H "x-portkey-aws-access-key-id: $AWS_ACCESS_KEY_ID" \ + -H "x-portkey-aws-secret-access-key: $AWS_SECRET_ACCESS_KEY" \ + -H "x-portkey-aws-region: $AWS_REGION" \ + -H "x-portkey-amzn-sagemaker-inference-component: $SAGEMAKER_INFERENCE_COMPONENT" \ + -d '{ + # You can pass any key value pair apart from `url` required by the model, they are passed as kwargs to the Sagemaker endpoint + "inputs": "my_custom_value", + "my_custom_key": "my_custom_value" + }' +``` + + + +## Next Steps + +The complete list of features supported in the SDK are available on the link below. + + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your Sagemaker requests](/product/ai-gateway/configs) +3. [Tracing Sagemaker requests](/product/observability/traces) diff --git a/virtual_key_old/integrations/llms/azure-foundry.mdx b/virtual_key_old/integrations/llms/azure-foundry.mdx new file mode 100644 index 00000000..358a61da --- /dev/null +++ b/virtual_key_old/integrations/llms/azure-foundry.mdx @@ -0,0 +1,492 @@ +--- +title: "Azure AI Foundry" +description: "Learn how to integrate Azure AI Foundry with Portkey to access a wide range of AI models with enhanced observability and reliability features." +--- + +Azure AI Foundry provides a unified platform for enterprise AI operations, model building, and application development. With Portkey, you can seamlessly integrate with various models available on Azure AI Foundry and take advantage of features like observability, prompt management, fallbacks, and more. + +## Understanding Azure AI Foundry Deployments + +Azure AI Foundry offers three different ways to deploy models, each with unique endpoints and configurations: + +1. **AI Services**: Azure-managed models accessed through Azure AI Services endpoints +2. **Managed**: User-managed deployments running on dedicated Azure compute resources +3. **Serverless**: Seamless, scalable deployment without managing infrastructure + +You can learn more about the Azure AI Foundry deployment [here](https://learn.microsoft.com/en-us/azure/ai-foundry/concepts/deployments-overview). + + +If you're specifically looking to use OpenAI models on Azure, you might want to use [Azure OpenAI](/integrations/llms/azure-openai) instead, which is optimized for OpenAI models. + + +## Integrate + +To integrate Azure AI Foundry with Portkey, you'll need to create a virtual key. Virtual keys securely store your Azure AI Foundry credentials in Portkey's vault, allowing you to use a simple identifier in your code instead of handling sensitive authentication details directly. + +Navigate to the [Virtual Keys](https://app.portkey.ai/virtual-keys) section in Portkey and select "Azure AI Foundry" as your provider. + + + +## Creating Your Virtual Key + +You can create a virtual key for Azure AI Foundry using one of three authentication methods. Each method requires different information from your Azure deployment: + + + + The recommended authentication mode using API Keys: + + + Required parameters: + - **API Key**: Your Azure AI Foundry API key + - **Azure Foundry URL**: The base endpoint URL for your deployment, formatted according to your deployment type: + * For AI Services: `https://your-resource-name.services.ai.azure.com/models` + * For Managed: `https://your-model-name.region.inference.ml.azure.com/score` + * For Serverless: `https://your-model-name.region.models.ai.azure.com` + + - **Azure API Version**: The API version to use (e.g., "2024-05-01-preview"). This is required if you have api version in your deployment url. For example: + * If your URL is `https://mycompany-ai.westus2.services.ai.azure.com/models?api-version=2024-05-01-preview`, the API version is `2024-05-01-preview` + - **Azure Deployment Name**: (Optional) Required only when a single resource contains multiple deployments. + + + Default Authentication Setup + + + + + For managed Azure deployments: + + + + Required parameters: + - **Azure Managed ClientID**: Your managed client ID + - **Azure Foundry URL**: The base endpoint URL for your deployment, formatted according to your deployment type: + * For AI Services: `https://your-resource-name.services.ai.azure.com/models` + * For Managed: `https://your-model-name.region.inference.ml.azure.com/score` + * For Serverless: `https://your-model-name.region.models.ai.azure.com` + + - **Azure API Version**: The API version to use (e.g., "2024-05-01-preview"). This is required if you have api version in your deployment url. + **Examples:** + * If your URL is `https://mycompany-ai.westus2.services.ai.azure.com/models?api-version=2024-05-01-preview`, the API version is `2024-05-01-preview` + - **Azure Deployment Name**: (Optional) Required only when a single resource contains multiple deployments. + + + + Default Authentication Setup + + + + + + To use this authentication your azure application need to have the role of: `conginitive services user`. + Enterprise-level authentication with Azure Entra ID: + + Required parameters: + - **Azure Entra ClientID**: Your Azure Entra client ID + - **Azure Entra Secret**: Your client secret + - **Azure Entra Tenant ID**: Your tenant ID + - **Azure Foundry URL**: The base endpoint URL for your deployment, formatted according to your deployment type: + * For AI Services: `https://your-resource-name.services.ai.azure.com/models` + * For Managed: `https://your-model-name.region.inference.ml.azure.com/score` + * For Serverless: `https://your-model-name.region.models.ai.azure.com` + + - **Azure API Version**: The API version to use (e.g., "2024-05-01-preview"). This is required if you have api version in your deployment url. + **Examples:** + * If your URL is `https://mycompany-ai.westus2.services.ai.azure.com/models?api-version=2024-05-01-preview`, the API version is `2024-05-01-preview` + - **Azure Deployment Name**: (Optional) Required only when a single resource contains multiple deployments. Common in Managed deployments. + + +You can Learn more about these [Azure Entra Resources here](https://learn.microsoft.com/en-us/azure/ai-services/authentication) + + + + Default Authentication Setup + + + + + +## Sample Request + +Once you've created your virtual key, you can start making requests to Azure AI Foundry models through Portkey. + + + +Install the Portkey SDK with npm +```sh +npm install portkey-ai +``` +```js +import Portkey from 'portkey-ai'; + +const client = new Portkey({ + apiKey: 'PORTKEY_API_KEY', + virtualKey: 'AZURE_FOUNDRY_VIRTUAL_KEY' +}); + +async function main() { + const response = await client.chat.completions.create({ + messages: [{ role: "user", content: "Tell me about cloud computing" }], + model: "DeepSeek-V3-0324", // Replace with your deployed model name + }); + + console.log(response.choices[0].message.content); +} + +main(); +``` + + +Install the Portkey SDK with pip +```sh +pip install portkey-ai +``` +```python +from portkey_ai import Portkey + +client = Portkey( + api_key = "PORTKEY_API_KEY", + virtual_key = "AZURE_FOUNDRY_VIRTUAL_KEY" +) + +response = client.chat.completions.create( + model="DeepSeek-V3-0324", # Replace with your deployed model name + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "Tell me about cloud computing"} + ] +) + +print(response.choices[0].message.content) +``` + + +```sh +curl https://api.portkey.ai/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-virtual-key: $AZURE_FOUNDRY_VIRTUAL_KEY" \ + -d '{ + "model": "DeepSeek-V3-0324", + "messages": [ + { "role": "user", "content": "Tell me about cloud computing" } + ] + }' +``` + + + +## Advanced Features + +### Function Calling + +Azure AI Foundry supports function calling (tool calling) for compatible models. Here's how to implement it with Portkey: + + + +```javascript +let tools = [{ + type: "function", + function: { + name: "getWeather", + description: "Get the current weather", + parameters: { + type: "object", + properties: { + location: { type: "string", description: "City and state" }, + unit: { type: "string", enum: ["celsius", "fahrenheit"] } + }, + required: ["location"] + } + } +}]; + +let response = await portkey.chat.completions.create({ + model: "DeepSeek-V3-0324", // Use a model that supports function calling + messages: [ + { role: "system", content: "You are a helpful assistant." }, + { role: "user", content: "What's the weather like in Delhi?" } + ], + tools, + tool_choice: "auto", +}); + +console.log(response.choices[0]); +``` + + +```python +tools = [{ + "type": "function", + "function": { + "name": "getWeather", + "description": "Get the current weather", + "parameters": { + "type": "object", + "properties": { + "location": {"type": "string", "description": "City and state"}, + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]} + }, + "required": ["location"] + } + } +}] + +response = portkey.chat.completions.create( + model="DeepSeek-V3-0324", # Use a model that supports function calling + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "What's the weather like in Delhi?"} + ], + tools=tools, + tool_choice="auto" +) + +print(response.choices[0]) +``` + + + +### Vision Capabilities + +Process images alongside text using Azure AI Foundry's vision capabilities: + + + +```javascript +const response = await portkey.chat.completions.create({ + model: "Llama-4-Scout-17B-16E", // Use a model that supports vision + messages: [ + { + role: "user", + content: [ + { type: "text", text: "What's in this image?" }, + { + type: "image_url", + image_url: "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg", + }, + ], + }, + ], + max_tokens: 500, +}); + +console.log(response.choices[0].message.content); +``` + + +```python +response = portkey.chat.completions.create( + model="Llama-4-Scout-17B-16E", # Use a model that supports vision + messages=[ + { + "role": "user", + "content": [ + {"type": "text", "text": "What's in this image?"}, + { + "type": "image_url", + "image_url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg", + }, + ], + } + ], + max_tokens=500, +) + +print(response.choices[0].message.content) +``` + + + +### Structured Outputs + +Get consistent, parseable responses in specific formats: + + + +```javascript +const response = await portkey.chat.completions.create({ + model: "cohere-command-a", // Use a model that supports response formats + messages: [ + { role: "system", content: "You are a helpful assistant." }, + { role: "user", content: "List the top 3 cloud providers with their main services" } + ], + response_format: { type: "json_object" }, + temperature: 0 +}); + +console.log(JSON.parse(response.choices[0].message.content)); +``` + + +```python +response = portkey.chat.completions.create( + model="cohere-command-a", # Use a model that supports response formats + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "List the top 3 cloud providers with their main services"} + ], + response_format={"type": "json_object"}, + temperature=0 +) + +import json +print(json.loads(response.choices[0].message.content)) +``` + + + +## Relationship with Azure OpenAI + +For Azure OpenAI specific models and deployments, we recommend using the existing Azure OpenAI provider in Portkey: + + + Learn how to integrate Azure OpenAI with Portkey for access to OpenAI models hosted on Azure. + + +## Portkey Features with Azure AI Foundry + +### Setting Up Fallbacks + +Create fallback configurations to ensure reliability when working with Azure AI Foundry models: + +```json +{ + "strategy": { + "mode": "fallback" + }, + "targets": [ + { + "virtual_key": "azure-foundry-virtual-key", + "override_params": { + "model": "DeepSeek-V3-0324" + } + }, + { + "virtual_key": "openai-virtual-key", + "override_params": { + "model": "gpt-4o" + } + } + ] +} +``` + +### Load Balancing Between Models + +Distribute requests across multiple models for optimal performance: + +```json +{ + "strategy": { + "mode": "loadbalance" + }, + "targets": [ + { + "virtual_key": "azure-foundry-virtual-key-1", + "override_params": { + "model": "DeepSeek-V3-0324" + }, + "weight": 0.7 + }, + { + "virtual_key": "azure-foundry-virtual-key-2", + "override_params": { + "model": "cohere-command-a" + }, + "weight": 0.3 + } + ] +} +``` + +### Conditional Routing + +Route requests based on specific conditions like user type or content requirements: + +```json +{ + "strategy": { + "mode": "conditional", + "conditions": [ + { + "query": { "metadata.user_type": { "$eq": "premium" } }, + "then": "high-performance-model" + }, + { + "query": { "metadata.content_type": { "$eq": "code" } }, + "then": "code-specialized-model" + } + ], + "default": "standard-model" + }, + "targets": [ + { + "name": "high-performance-model", + "virtual_key": "azure-foundry-virtual-key-1", + "override_params": { + "model": "Llama-4-Scout-17B-16E" + } + }, + { + "name": "code-specialized-model", + "virtual_key": "azure-foundry-virtual-key-2", + "override_params": { + "model": "DeepSeek-V3-0324" + } + }, + { + "name": "standard-model", + "virtual_key": "azure-foundry-virtual-key-3", + "override_params": { + "model": "cohere-command-a" + } + } + ] +} +``` + +## Managing Prompts with Azure AI Foundry + +You can manage all prompts to Azure AI Foundry in the [Prompt Library](/product/prompt-library). Once you've created and tested a prompt in the library, use the `portkey.prompts.completions.create` interface to use the prompt in your application. + + + +```js +const promptCompletion = await portkey.prompts.completions.create({ + promptID: "Your Prompt ID", + variables: { + // The variables specified in the prompt + } +}) +``` + + +```python +prompt_completion = portkey.prompts.completions.create( + prompt_id="Your Prompt ID", + variables={ + # The variables specified in the prompt + } +) +``` + + + +## Next Steps + +Explore these additional resources to make the most of your Azure AI Foundry integration with Portkey: + + + + Learn how to add custom metadata to your Azure AI Foundry requests. + + + Configure advanced gateway features for your Azure AI Foundry requests. + + + Trace your Azure AI Foundry requests for better observability. + + + Create fallback configurations between different providers. + + diff --git a/virtual_key_old/integrations/llms/azure-openai/azure-openai.mdx b/virtual_key_old/integrations/llms/azure-openai/azure-openai.mdx new file mode 100644 index 00000000..8af47508 --- /dev/null +++ b/virtual_key_old/integrations/llms/azure-openai/azure-openai.mdx @@ -0,0 +1,317 @@ +--- +title: "Azure OpenAI" +description: "Azure OpenAI is a great alternative to accessing the best models including GPT-4 and more in your private environments. Portkey provides complete support for Azure OpenAI." +--- + +With Portkey, you can take advantage of features like fast AI gateway access, observability, prompt management, and more, all while ensuring the secure management of your LLM API keys through a [virtual key](/product/ai-gateway/virtual-keys) system. + +Provider Slug. `azure-openai` + +## Portkey SDK Integration with Azure OpenAI + +Portkey provides a consistent API to interact with models from various providers. To integrate Azure OpenAI with Portkey: + +### First, add your Azure details to Portkey's Virtual Keys + +azure + + + +**Here's a step-by-step guide:** + +1. Request access to Azure OpenAI [here](https://aka.ms/oai/access). +2. Create a resource in the Azure portal [here](https://portal.azure.com/?microsoft%5Fazure%5Fmarketplace%5FItemHideKey=microsoft%5Fopenai%5Ftip#create/Microsoft.CognitiveServicesOpenAI). (This will be your **Resource Name**) +3. Deploy a model in Azure OpenAI Studio [here](https://oai.azure.com/). (This will be your **Deployment Name)** +4. Select your `Foundation Model` from the dropdowon on the modal. +5. Now, on Azure OpenAI studio, go to any playground (chat or completions), click on a UI element called "View code". Note down the API version & API key from here. (This will be your **Azure API Version** & **Azure API Key**) + +When you input these details, the foundation model will be auto populated. More details in [this guide](https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/create-resource?pivots=web-portal). + + +If you do not want to add your Azure details to Portkey, you can also directly pass them while instantiating the Portkey client. [More on that here.](/integrations/llms/azure-openai/azure-openai#making-requests-without-virtual-keys) + +**Now, let's make a request using this virtual key!** + +### 1\. Install the Portkey SDK + +Add the Portkey SDK to your application to interact with Azure OpenAI's API through Portkey's gateway. + + + + +```sh +npm install --save portkey-ai +``` + + + +```sh +pip install portkey-ai +``` + + + + + + +### 2\. Initialize Portkey with the Virtual Key + +Set up Portkey with your virtual key as part of the initialization configuration. You can create a [virtual key](/product/ai-gateway/virtual-keys) for Azure in the Portkey UI. + + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "AZURE_VIRTUAL_KEY" // Your Azure Virtual Key + }) + ``` + + +```python +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="AZURE_VIRTUAL_KEY" # Replace with your virtual key for Azure +) +``` + + + +### **3\. Invoke Chat Completions with Azure OpenAI** + +Use the Portkey instance to send requests to your Azure deployments. You can also override the virtual key directly in the API call if needed. + + + + ```js + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'gpt4', // This would be your deployment or model name + }); + + console.log(chatCompletion.choices); + ``` + + + +```python +completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model= 'custom_model_name' +) + +print(completion.choices) +``` + + + + + + +## Managing Azure OpenAI Prompts + +You can manage all prompts to Azure OpenAI in the [Prompt Library](/product/prompt-library). All the current models of OpenAI are supported and you can easily start testing different prompts. + +Once you're ready with your prompt, you can use the `portkey.prompts.completions.create` interface to use the prompt in your application. + +## Image Generation + +Portkey supports multiple modalities for Azure OpenAI and you can make image generation requests through Portkey's AI Gateway the same way as making completion calls. + + + +```js +import Portkey from 'portkey-ai' + +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "DALL-E_VIRTUAL_KEY" // Referencing a Dall-E Azure deployment with Virtual Key +}) + +const image = await portkey.images.generate({ + prompt:"Lucy in the sky with diamonds", + size:"1024x1024" +}) +``` + + +```python +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY", + virtual_key="DALL-E_VIRTUAL_KEY" # Referencing a Dall-E Azure deployment with Virtual Key +) + +image = portkey.images.generate( + prompt="Lucy in the sky with diamonds", + size="1024x1024" +) +``` + + + + +Portkey's fast AI gateway captures the information about the request on your Portkey Dashboard. On your logs screen, you'd be able to see this request with the request and response. + + + api + + +Log view for an image generation request on Azure OpenAI + +More information on image generation is available in the [API Reference](https://portkey.ai/docs/api-reference/completions-1#create-image). + +--- + +## Making Requests Without Virtual Keys + +Here's how you can pass your Azure OpenAI details & secrets directly without using the Virtual Keys feature. + +### Key Mapping + +In a typical Azure OpenAI request, + + + +```sh +curl https://{YOUR_RESOURCE_NAME}.openai.azure.com/openai/deployments/{YOUR_DEPLOYMENT_NAME}/chat/completions?api-version={API_VERSION} \ + -H "Content-Type: application/json" \ + -H "api-key: {YOUR_API_KEY}" \ + -d '{ + "model": "gpt-4o", + "messages": [ + { + "role": "system", + "content": "You are a helpful assistant" + }, + { + "role": "user", + "content": "what is a portkey?" + } + ] +}' +``` + +| Parameter | Node SDK | Python SDK | REST Headers | +| --------------------- | ------------------------------------ | ------------------------------------- | ----------------------------- | +| AZURE RESOURCE NAME | azureResourceName | azure_resource_name | x-portkey-azure-resource-name | +| AZURE DEPLOYMENT NAME | azureDeploymentId | azure_deployment_id | x-portkey-azure-deployment-id | +| API VERSION | azureApiVersion | azure_api_version | x-portkey-azure-api-version | +| AZURE API KEY | Authorization: "Bearer + {API_KEY}" | Authorization = "Bearer + {API_KEY}" | Authorization | +| AZURE MODEL NAME | azureModelName | azure_model_name | x-portkey-azure-model-name | + +### Example + + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + provider: "azure-openai", + azureResourceName: "AZURE_RESOURCE_NAME", + azureDeploymentId: "AZURE_DEPLOYMENT_NAME", + azureApiVersion: "AZURE_API_VERSION", + azureModelName: "AZURE_MODEL_NAME" + Authorization: "Bearer API_KEY" + }) + ``` + + + +```python +from portkey_ai import Portkey + +portkey = Portkey( + api_key = "PORTKEY_API_KEY", + provider = "azure-openai", + azure_resource_name = "AZURE_RESOURCE_NAME", + azure_deployment_id = "AZURE_DEPLOYMENT_NAME", + azure_api_version = "AZURE_API_VERSION", + azure_model_name = "AZURE_MODEL_NAME", + Authorization = "Bearer API_KEY" +) +``` + + + ```sh + curl https://api.portkey.ai/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $AZURE_OPENAI_API_KEY" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-provider: azure-openai" \ + -H "x-portkey-azure-resource-name: $AZURE_RESOURCE_NAME" \ + -H "x-portkey-azure-deployment-id: $AZURE_DEPLOYMENY_ID" \ + -H "x-portkey-azure-model-name: $AZURE_MODEL_NAME" \ + -H "x-portkey-azure-api-version: $AZURE_API_VERSION" \ + -d '{ + "model": "gpt-4o", + "messages": [{"role": "user","content": "Hello!"}] + }' + ``` + + + + +### How to Pass JWT (JSON Web Tokens) + +If you have configured fine-grained access for Azure OpenAI and need to use `JSON web token (JWT)` in the `Authorization` header instead of the regular `API Key`, you can use the `forwardHeaders` parameter to do this. + + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + provider: "azure-openai", + azureResourceName: "AZURE_RESOURCE_NAME", + azureDeploymendId: "AZURE_DEPLOYMENT_NAME", + azureApiVersion: "AZURE_API_VERSION", + azureModelName: "AZURE_MODEL_NAME", + Authorization: "Bearer JWT_KEY", // Pass your JWT here + forwardHeaders: [ "Authorization" ] + }) + ``` + + + +```js +import Portkey from 'portkey-ai' + +const portkey = new Portkey({ + api_key = "PORTKEY_API_KEY", + provider = "azure-openai", + azure_resource_name = "AZURE_RESOURCE_NAME", + azure_deploymend_id = "AZURE_DEPLOYMENT_NAME", + azure_api_version = "AZURE_API_VERSION", + azure_model_name = "AZURE_MODEL_NAME", + Authorization = "Bearer API_KEY", # Pass your JWT here + forward_headers= [ "Authorization" ] +) +``` + + + +For further questions on custom Azure deployments or fine-grained access tokens, reach out to us on support@portkey.ai + +## Next Steps + +The complete list of features supported in the SDK are available on the link below. + + + + + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your Azure OpenAI requests](/product/ai-gateway/configs) +3. [Tracing Azure OpenAI requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to Azure OpenAI APIs](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/azure-openai/batches.mdx b/virtual_key_old/integrations/llms/azure-openai/batches.mdx new file mode 100644 index 00000000..6b1073ed --- /dev/null +++ b/virtual_key_old/integrations/llms/azure-openai/batches.mdx @@ -0,0 +1,497 @@ +--- +title: Batches +description: Perform batch inference with Azure OpenAI +--- + + +With Portkey, you can perform [Azure OpenAI Batch Inference](https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/batch?tabs=global-batch%2Cstandard-input%2Cpython-secure&pivots=rest-api) operations. +This is the most efficient way to +- Test your data with different foundation models +- Perform A/B testing with different foundation models +- Perform batch inference with different foundation models + +## Create Batch Job + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Add your provider's virtual key +) + +start_batch_response = portkey.batches.create( + input_file_id="file_id", # file id of the input file + endpoint="endpoint", # ex: /v1/chat/completions + completion_window="completion_window", # ex: 24h + metadata={} # metadata for the batch +) + +print(start_batch_response) +``` + + +```js +import { Portkey } from 'portkey-ai'; + +// Initialize the Portkey client +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key +}); + +const startBatch = async () => { + const startBatchResponse = await portkey.batches.create({ + input_file_id: "file_id", // file id of the input file + endpoint: "endpoint", // ex: /v1/chat/completions + completion_window: "completion_window", // ex: 24h + metadata: {} // metadata for the batch + }); + + console.log(startBatchResponse); +} + +await startBatch(); + +``` + + +```sh +curl --location 'https://api.portkey.ai/v1/batches' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' \ +--header 'Content-Type: application/json' \ +--data '{ + "input_file_id": "", + "endpoint": "", + "completion_window": "", + "metadata": {}, +}' +``` + + +```js +import OpenAI from 'openai'; // We're using the v4 SDK +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY" // defaults to process.env["PORTKEY_API_KEY"] + }) +}); + +const startBatch = async () => { + const startBatchResponse = await openai.batches.create({ + input_file_id: "file_id", // file id of the input file + endpoint: "endpoint", // ex: /v1/chat/completions + completion_window: "completion_window", // ex: 24h + metadata: {} // metadata for the batch + }); + + console.log(startBatchResponse); +} + +await startBatch(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY" + ) +) + +start_batch_response = openai.batches.create( + input_file_id="file_id", # file id of the input file + endpoint="endpoint", # ex: /v1/chat/completions + completion_window="completion_window", # ex: 24h + metadata={} # metadata for the batch +) + +print(start_batch_response) +``` + + + + +## List Batch Jobs + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Add your provider's virtual key +) + +batches = portkey.batches.list() + +print(batches) +``` + + +```js +import { Portkey } from 'portkey-ai'; + +// Initialize the Portkey client +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key +}); + +const listBatches = async () => { + const batches = await portkey.batches.list(); + + console.log(batches); +} + +await listBatches(); + +``` + + +```sh +curl --location 'https://api.portkey.ai/v1/batches' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' +``` + + +```js +import OpenAI from 'openai'; // We're using the v4 SDK +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY" // defaults to process.env["PORTKEY_API_KEY"] + }) +}); + +const listBatches = async () => { + const batches = await openai.batches.list(); + + console.log(batches); +} + +await listBatches(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY" + ) +) + +batches = openai.batches.list() + +print(batches) +``` + + + + + +## Get Batch Job Details + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Add your provider's virtual key +) + +batch = portkey.batches.retrieve(batch_id="batch_id") + +print(batch) +``` + + +```js +import { Portkey } from 'portkey-ai'; + +// Initialize the Portkey client +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key +}); + +const getBatch = async () => { + const batch = await portkey.batches.retrieve(batch_id="batch_id"); + + console.log(batch); +} + +await getBatch(); + +``` + + +```sh +curl --location 'https://api.portkey.ai/v1/batches/' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' +``` + + +```js +import OpenAI from 'openai'; // We're using the v4 SDK +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY" // defaults to process.env["PORTKEY_API_KEY"] + }) +}); + +const getBatch = async () => { + const batch = await openai.batches.retrieve(batch_id="batch_id"); + + console.log(batch); +} + +await getBatch(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY" + ) +) + +batch = openai.batches.retrieve(batch_id="batch_id") + +print(batch) +``` + + + + + +## Get Batch Output + + +```sh +curl --location 'https://api.portkey.ai/v1/batches//output' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' +``` + + + + +## List Batch Jobs + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Add your provider's virtual key +) + +batches = portkey.batches.list() + +print(batches) +``` + + +```js +import { Portkey } from 'portkey-ai'; + +// Initialize the Portkey client +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key +}); + +const listBatchingJobs = async () => { + const batching_jobs = await portkey.batches.list(); + + console.log(batching_jobs); +} + +await listBatchingJobs(); + +``` + + +```sh +curl --location 'https://api.portkey.ai/v1/batches' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' +``` + + +```js +import OpenAI from 'openai'; // We're using the v4 SDK +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY" // defaults to process.env["PORTKEY_API_KEY"] + }) +}); + +const listBatchingJobs = async () => { + const batching_jobs = await openai.batches.list(); + + console.log(batching_jobs); +} + +await listBatchingJobs(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY" + ) +) + +batching_jobs = openai.batches.list() + +print(batching_jobs) +``` + + + + +## Cancel Batch Job + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Add your provider's virtual key +) + +cancel_batch_response = portkey.batches.cancel(batch_id="batch_id") + +print(cancel_batch_response) +``` + + +```js +import { Portkey } from 'portkey-ai'; + +// Initialize the Portkey client +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key +}); + +const cancelBatch = async () => { + const cancel_batch_response = await portkey.batches.cancel(batch_id="batch_id"); + + console.log(cancel_batch_response); +} + +await cancelBatch(); + +``` + + +```sh +curl --request POST --location 'https://api.portkey.ai/v1/batches//cancel' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' +``` + + +```js +import OpenAI from 'openai'; // We're using the v4 SDK +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY" // defaults to process.env["PORTKEY_API_KEY"] + }) +}); + +const cancelBatch = async () => { + const cancel_batch_response = await openai.batches.cancel(batch_id="batch_id"); + + console.log(cancel_batch_response); +} + +await cancelBatch(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY" + ) +) + +cancel_batch_response = openai.batches.cancel(batch_id="batch_id") + +print(cancel_batch_response) +``` + + + diff --git a/virtual_key_old/integrations/llms/azure-openai/files.mdx b/virtual_key_old/integrations/llms/azure-openai/files.mdx new file mode 100644 index 00000000..82ee50a0 --- /dev/null +++ b/virtual_key_old/integrations/llms/azure-openai/files.mdx @@ -0,0 +1,466 @@ +--- +title: Files +description: Upload files to Azure OpenAI +--- + +## Uploading Files + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Add your provider's virtual key +) + +upload_file_response = portkey.files.create( + purpose="batch", + file=open("file.pdf", "rb") +) + +print(upload_file_response) +``` + + +```js +import { Portkey } from 'portkey-ai'; + +// Initialize the Portkey client +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key +}); + +const uploadFile = async () => { + const file = await portkey.files.create({ + purpose: "batch", + file: fs.createReadStream("file.pdf") + }); + + console.log(file); +} + +await uploadFile(); + +``` + + +```sh +curl --location --request POST 'https://api.portkey.ai/v1/files' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' \ +--form 'purpose=""' \ +--form 'file=@""' +``` + + +```js +import OpenAI from 'openai'; // We're using the v4 SDK +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY" // defaults to process.env["PORTKEY_API_KEY"] + }) +}); + +const uploadFile = async () => { + const file = await openai.files.create({ + purpose: "batch", + file: fs.createReadStream("file.pdf") + }); + + console.log(file); +} + +await uploadFile(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY" + ) +) + +upload_file_response = openai.files.create( + purpose="batch", + file=open("file.pdf", "rb") +) + +print(upload_file_response) +``` + + + + +## List Files + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Add your provider's virtual key +) + +files = portkey.files.list() + +print(files) +``` + + +```js +import { Portkey } from 'portkey-ai'; + +// Initialize the Portkey client +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key +}); + +const listFiles = async () => { + const files = await portkey.files.list(); + + console.log(files); +} + +await listFiles(); + +``` + + +```sh +curl --location 'https://api.portkey.ai/v1/files' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' +``` + + +```js +import OpenAI from 'openai'; // We're using the v4 SDK +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY" // defaults to process.env["PORTKEY_API_KEY"] + }) +}); + +const listFiles = async () => { + const files = await openai.files.list(); + + console.log(file); +} + +await listFiles(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY" + ) +) + +files = openai.files.list() + +print(files) +``` + + + + + +## Get File + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Add your provider's virtual key +) + +file = portkey.files.retrieve(file_id="file_id") + +print(file) +``` + + +```js +import { Portkey } from 'portkey-ai'; + +// Initialize the Portkey client +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key +}); + +const getFile = async () => { + const file = await portkey.files.retrieve(file_id="file_id"); + + console.log(file); +} + +await getFile(); + +``` + + +```sh +curl --location 'https://api.portkey.ai/v1/files/' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' +``` + + +```js +import OpenAI from 'openai'; // We're using the v4 SDK +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY" // defaults to process.env["PORTKEY_API_KEY"] + }) +}); + +const getFile = async () => { + const file = await openai.files.retrieve(file_id="file_id"); + + console.log(file); +} + +await getFile(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY" + ) +) + +file = openai.files.retrieve(file_id="file_id") + +print(file) +``` + + + + + +## Get File Content + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Add your provider's virtual key +) + +file_content = portkey.files.content(file_id="file_id") + +print(file_content) +``` + + +```js +import { Portkey } from 'portkey-ai'; + +// Initialize the Portkey client +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key +}); + +const getFileContent = async () => { + const file_content = await portkey.files.content(file_id="file_id"); + + console.log(file_content); +} + +await getFileContent(); + +``` + + +```sh +curl --location 'https://api.portkey.ai/v1/files//content' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' +``` + + +```js +import OpenAI from 'openai'; // We're using the v4 SDK +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY" // defaults to process.env["PORTKEY_API_KEY"] + }) +}); + +const getFileContent = async () => { + const file_content = await openai.files.content(file_id="file_id"); + + console.log(file_content); +} + +await getFileContent(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY" + ) +) + +file_content = openai.files.content(file_id="file_id") + +print(file_content) +``` + + + + + +## Delete File + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Add your provider's virtual key +) + +delete_file_response = portkey.files.delete(file_id="file_id") + +print(delete_file_response) +``` + + +```js +import { Portkey } from 'portkey-ai'; + +// Initialize the Portkey client +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key +}); + +const deleteFile = async () => { + const delete_file_response = await portkey.files.delete(file_id="file_id"); + + console.log(delete_file_response); +} + +await deleteFile(); + +``` + + +```sh +curl --location --request DELETE 'https://api.portkey.ai/v1/files/' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' +``` + + +```js +import OpenAI from 'openai'; // We're using the v4 SDK +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY" // defaults to process.env["PORTKEY_API_KEY"] + }) +}); + +const deleteFile = async () => { + const delete_file_response = await openai.files.delete(file_id="file_id"); + + console.log(delete_file_response); +} + +await deleteFile(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY" + ) +) + +delete_file_response = openai.files.delete(file_id="file_id") + +print(delete_file_response) +``` + + + diff --git a/virtual_key_old/integrations/llms/azure-openai/fine-tuning.mdx b/virtual_key_old/integrations/llms/azure-openai/fine-tuning.mdx new file mode 100644 index 00000000..d04a5644 --- /dev/null +++ b/virtual_key_old/integrations/llms/azure-openai/fine-tuning.mdx @@ -0,0 +1,183 @@ +--- +title: "Fine-tune" +description: "Fine-tune your models with Azure OpenAI" +--- + +Azure OpenAI follows a similar fine-tuning process as OpenAI, with some Azure-specific configurations. The examples below show how to use Portkey with Azure OpenAI for fine-tuning. + +### Upload a file + + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Add your provider's virtual key for Azure OpenAI +) + +# Upload a file for fine-tuning +file = portkey.files.create( + file="dataset.jsonl", + purpose="fine-tune" +) + +print(file) +``` + + +```typescript +import { Portkey } from "portkey-ai"; +import * as fs from 'fs'; + +// Initialize the Portkey client +const portkey = Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key for Azure OpenAI +}); + +(async () => { + // Upload a file for fine-tuning + const file = await portkey.files.create({ + file: fs.createReadStream("dataset.jsonl"), + purpose: "fine-tune" + }); + + console.log(file); +})(); +``` + + +```python +from openai import AzureOpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +client = AzureOpenAI( + api_key="AZURE_OPENAI_API_KEY", + api_version="2023-05-15", + azure_endpoint=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + virtual_key="VIRTUAL_KEY", + api_key="PORTKEY_API_KEY" + ) +) + +# Upload a file for fine-tuning +file = client.files.create( + file=open("dataset.jsonl", "rb"), + purpose="fine-tune" +) + +print(file) +``` + + +```sh +curl -X POST --header 'x-portkey-api-key: ' \ + --header 'x-portkey-virtual-key: ' \ + --form 'file=@dataset.jsonl' \ + --form 'purpose=fine-tune' \ + 'https://api.portkey.ai/v1/files' +``` + + + +### Create a fine-tuning job + + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Add your provider's virtual key for Azure OpenAI +) + +# Create a fine-tuning job +fine_tune_job = portkey.fine_tuning.jobs.create( + model="gpt-35-turbo", # Base model to fine-tune + training_file="file_id", # ID of the uploaded training file + validation_file="file_id", # Optional: ID of the uploaded validation file + suffix="finetune_name", # Custom suffix for the fine-tuned model name + hyperparameters={ + "n_epochs": 1 + } +) + +print(fine_tune_job) +``` + + +```typescript +import { Portkey } from "portkey-ai"; + +// Initialize the Portkey client +const portkey = Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key for Azure OpenAI +}); + +(async () => { + // Create a fine-tuning job + const fineTuneJob = await portkey.fineTuning.jobs.create({ + model: "gpt-35-turbo", // Base model to fine-tune + training_file: "file_id", // ID of the uploaded training file + validation_file: "file_id", // Optional: ID of the uploaded validation file + suffix: "finetune_name", // Custom suffix for the fine-tuned model name + hyperparameters: { + n_epochs: 1 + } + }); + + console.log(fineTuneJob); +})(); +``` + + +```python +from openai import AzureOpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +client = AzureOpenAI( + api_key="AZURE_OPENAI_API_KEY", + api_version="2023-05-15", + azure_endpoint=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + virtual_key="VIRTUAL_KEY", + api_key="PORTKEY_API_KEY" + ) +) + +# Create a fine-tuning job +fine_tune_job = client.fine_tuning.jobs.create( + model="gpt-35-turbo", # Base model to fine-tune + training_file="file_id", # ID of the uploaded training file + validation_file="file_id", # Optional: ID of the uploaded validation file + suffix="finetune_name", # Custom suffix for the fine-tuned model name + hyperparameters={ + "n_epochs": 1 + } +) + +print(fine_tune_job) +``` + + +```sh +curl -X POST --header 'Content-Type: application/json' \ + --header 'x-portkey-api-key: ' \ + --header 'x-portkey-virtual-key: ' \ + --data \ + $'{"model": "", "suffix": "", "training_file": "", "validation_file": "", "hyperparameters": {"n_epochs": 1}}\n' \ +'https://api.portkey.ai/v1/fine_tuning/jobs' +``` + + + +For more detailed examples and other fine-tuning operations (listing jobs, retrieving job details, canceling jobs, and getting job events), please refer to the [OpenAI fine-tuning documentation](/integrations/llms/openai/fine-tuning). + +The Azure OpenAI fine-tuning API documentation is available at [Azure OpenAI API](https://learn.microsoft.com/en-us/rest/api/azureopenai/fine-tuning/create?view=rest-azureopenai-2025-01-01-preview&tabs=HTTP). \ No newline at end of file diff --git a/virtual_key_old/integrations/llms/bedrock/aws-bedrock.mdx b/virtual_key_old/integrations/llms/bedrock/aws-bedrock.mdx new file mode 100644 index 00000000..1029581f --- /dev/null +++ b/virtual_key_old/integrations/llms/bedrock/aws-bedrock.mdx @@ -0,0 +1,790 @@ +--- +title: "AWS Bedrock" +--- + +Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including models hosted on AWS Bedrock. + +With Portkey, you can take advantage of features like fast AI gateway access, observability, prompt management, and more, all while ensuring the secure management of your LLM API keys through a [virtual key](/product/ai-gateway/virtual-keys) system. + + +Provider Slug. `bedrock` + +## Portkey SDK Integration with AWS Bedrock + +Portkey provides a consistent API to interact with models from various providers. To integrate Bedrock with Portkey: + +### 1\. Install the Portkey SDK + +Add the Portkey SDK to your application to interact with Anthropic's API through Portkey's gateway. + + + + + +```sh +npm install --save portkey-ai +``` + + + ```sh + pip install portkey-ai + ``` + + + + + + + +### 2\. Initialize Portkey with the Virtual Key + +There are two ways to integrate AWS Bedrock with Portkey: + + + +
Use your `AWS Secret Access Key`, `AWS Access Key Id`, and `AWS Region` to create your Virtual key.

+ +[**Integration Guide**](/integrations/llms/aws-bedrock#how-to-find-your-aws-credentials) +
+ +
Take your `AWS Assumed Role ARN` and `AWS Region` to create the virtaul key.


+ +[**Integration Guide**](/product/ai-gateway/virtual-keys/bedrock-amazon-assumed-role) +
+
+ + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your Bedrock Virtual Key + }) + ``` + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Bedrock + ) + ``` + + + +#### Using Virtual Key with AWS STS + +If you're using [AWS Security Token Service](https://docs.aws.amazon.com/STS/latest/APIReference/welcome.html), you can pass your `aws_session_token` along with the Virtual key: + + + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your Bedrock Virtual Key, + aws_session_token: "" + }) + ``` + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Bedrock, + aws_session_token="" + ) + ``` + + + + + +#### Not using Virtual Keys? + +[Check out this example on how you can directly use your AWS details to make a Bedrock request through Portkey.](/integrations/llms/bedrock/aws-bedrock#making-requests-without-virtual-keys) + +### **3\. Invoke Chat Completions with AWS bedrock** + +Use the Portkey instance to send requests to Anthropic. You can also override the virtual key directly in the API call if needed. + + + + + ```js + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'anthropic.claude-v2:1', + max_tokens: 250 // Required field for Anthropic + }); + + console.log(chatCompletion.choices); + ``` + + + ```python + completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model= 'anthropic.claude-v2:1', + max_tokens=250 # Required field for Anthropic + ) + + print(completion.choices) + ``` + + + + + + + +## Using Vision Models + +Portkey's multimodal Gateway fully supports Bedrock's vision models `anthropic.claude-3-sonnet`, `anthropic.claude-3-haiku`, and `anthropic.claude-3-opus` + +For more info, check out this guide: + +[Vision](/product/ai-gateway/multimodal-capabilities/vision) + +## Extended Thinking (Reasoning Models) (Beta) + + +The assistants thinking response is returned in the `response_chunk.choices[0].delta.content_blocks` array, not the `response.choices[0].message.content` string. + + +Models like `us.anthropic.claude-3-7-sonnet-20250219-v1:0` support [extended thinking](https://aws.amazon.com/blogs/aws/anthropics-claude-3-7-sonnet-the-first-hybrid-reasoning-model-is-now-available-in-amazon-bedrock/). +This is similar to openai thinking, but you get the model's reasoning as it processes the request as well. + +Note that you will have to set [`strict_open_ai_compliance=False`](/product/ai-gateway/strict-open-ai-compliance) in the headers to use this feature. + +### Single turn conversation + + ```py Python + from portkey_ai import Portkey + + # Initialize the Portkey client + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + strict_openai_compliance=False + ) + + # Create the request + response = portkey.chat.completions.create( + model="us.anthropic.claude-3-7-sonnet-20250219-v1:0", + max_tokens=3000, + thinking={ + "type": "enabled", + "budget_tokens": 2030 + }, + stream=True, + messages=[ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "when does the flight from new york to bengaluru land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + } + ] + ) + print(response) + # in case of streaming responses you'd have to parse the response_chunk.choices[0].delta.content_blocks array + # response = portkey.chat.completions.create( + # ...same config as above but with stream: true + # ) + # for chunk in response: + # if chunk.choices[0].delta: + # content_blocks = chunk.choices[0].delta.get("content_blocks") + # if content_blocks is not None: + # for content_block in content_blocks: + # print(content_block) + ``` + ```ts NodeJS + import Portkey from 'portkey-ai'; + + // Initialize the Portkey client + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY", // Add your bedrock's virtual key + strictOpenAICompliance: false + }); + + // Generate a chat completion + async function getChatCompletionFunctions() { + const response = await portkey.chat.completions.create({ + model: "us.anthropic.claude-3-7-sonnet-20250219-v1:0", + max_tokens: 3000, + thinking: { + type: "enabled", + budget_tokens: 2030 + }, + stream: true, + messages: [ + { + role: "user", + content: [ + { + type: "text", + text: "when does the flight from new york to bengaluru land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + } + ] + }); + console.log(response); + // in case of streaming responses you'd have to parse the response_chunk.choices[0].delta.content_blocks array + // const response = await portkey.chat.completions.create({ + // ...same config as above but with stream: true + // }); + // for await (const chunk of response) { + // if (chunk.choices[0].delta?.content_blocks) { + // for (const contentBlock of chunk.choices[0].delta.content_blocks) { + // console.log(contentBlock); + // } + // } + // } + } + // Call the function + getChatCompletionFunctions(); + ``` + ```js OpenAI NodeJS + import OpenAI from 'openai'; // We're using the v4 SDK + import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + + const openai = new OpenAI({ + apiKey: 'BEDROCK_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "bedrock", + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + strictOpenAICompliance: false + }) + }); + + // Generate a chat completion with streaming + async function getChatCompletionFunctions(){ + const response = await openai.chat.completions.create({ + model: "us.anthropic.claude-3-7-sonnet-20250219-v1:0", + max_tokens: 3000, + thinking: { + type: "enabled", + budget_tokens: 2030 + }, + stream: true, + messages: [ + { + role: "user", + content: [ + { + type: "text", + text: "when does the flight from new york to bengaluru land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + } + ], + }); + + console.log(response) + // in case of streaming responses you'd have to parse the response_chunk.choices[0].delta.content_blocks array + // const response = await openai.chat.completions.create({ + // ...same config as above but with stream: true + // }); + // for await (const chunk of response) { + // if (chunk.choices[0].delta?.content_blocks) { + // for (const contentBlock of chunk.choices[0].delta.content_blocks) { + // console.log(contentBlock); + // } + // } + // } + } + await getChatCompletionFunctions(); + ``` + ```py OpenAI Python + from openai import OpenAI + from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + + openai = OpenAI( + api_key='BEDROCK_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="bedrock", + api_key="PORTKEY_API_KEY", + strict_openai_compliance=False + ) + ) + + + response = openai.chat.completions.create( + model="us.anthropic.claude-3-7-sonnet-20250219-v1:0", + max_tokens=3000, + thinking={ + "type": "enabled", + "budget_tokens": 2030 + }, + stream=True, + messages=[ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "when does the flight from new york to bengaluru land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + } + ] + ) + + print(response) + ``` + ```sh cURL + curl "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-provider: bedrock" \ + -H "x-api-key: $BEDROCK_API_KEY" \ + -H "x-portkey-strict-openai-compliance: false" \ + -d '{ + "model": "us.anthropic.claude-3-7-sonnet-20250219-v1:0", + "max_tokens": 3000, + "thinking": { + "type": "enabled", + "budget_tokens": 2030 + }, + "stream": true, + "messages": [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "when does the flight from new york to bengaluru land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + } + ] + }' + ``` + + +### Multi turn conversation + + + ```py Python + from portkey_ai import Portkey + + # Initialize the Portkey client + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + strict_openai_compliance=False + ) + + # Create the request + response = portkey.chat.completions.create( + model="us.anthropic.claude-3-7-sonnet-20250219-v1:0", + max_tokens=3000, + thinking={ + "type": "enabled", + "budget_tokens": 2030 + }, + stream=True, + messages=[ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "when does the flight from baroda to bangalore land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + }, + { + "role": "assistant", + "content": [ + { + "type": "thinking", + "thinking": "The user is asking several questions about a flight from Baroda (also known as Vadodara) to Bangalore:\n1. When does the flight land tomorrow\n2. What time does it land\n3. What is the flight number\n4. What is the baggage belt number at the arrival airport\n\nTo properly answer these questions, I would need access to airline flight schedules and airport information systems. However, I don't have:\n- Real-time or scheduled flight information\n- Access to airport baggage claim allocation systems\n- Information about specific flights between these cities\n- The ability to look up tomorrow's specific flight schedules\n\nThis question requires current, specific flight information that I don't have access to. Instead of guessing or providing potentially incorrect information, I should explain this limitation and suggest ways the user could find this information.", + "signature": "EqoBCkgIARABGAIiQBVA7FBNLRtWarDSy9TAjwtOpcTSYHJ+2GYEoaorq3V+d3eapde04bvEfykD/66xZXjJ5yyqogJ8DEkNMotspRsSDKzuUJ9FKhSNt/3PdxoMaFZuH+1z1aLF8OeQIjCrA1+T2lsErrbgrve6eDWeMvP+1sqVqv/JcIn1jOmuzrPi2tNz5M0oqkOO9txJf7QqEPPw6RG3JLO2h7nV1BMN6wE=" + } + ] + }, + { + "role": "user", + "content": "thanks that's good to know, how about to chennai?" + } + ] + ) + print(response) + ``` + ```ts NodeJS + import Portkey from 'portkey-ai'; + + // Initialize the Portkey client + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY", // Add your bedrock's virtual key + strictOpenAICompliance: false + }); + + // Generate a chat completion + async function getChatCompletionFunctions() { + const response = await portkey.chat.completions.create({ + model: "us.anthropic.claude-3-7-sonnet-20250219-v1:0", + max_tokens: 3000, + thinking: { + type: "enabled", + budget_tokens: 2030 + }, + stream: true, + messages: [ + { + role: "user", + content: [ + { + type: "text", + text: "when does the flight from baroda to bangalore land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + }, + { + role: "assistant", + content: [ + { + type: "thinking", + thinking: "The user is asking several questions about a flight from Baroda (also known as Vadodara) to Bangalore:\n1. When does the flight land tomorrow\n2. What time does it land\n3. What is the flight number\n4. What is the baggage belt number at the arrival airport\n\nTo properly answer these questions, I would need access to airline flight schedules and airport information systems. However, I don't have:\n- Real-time or scheduled flight information\n- Access to airport baggage claim allocation systems\n- Information about specific flights between these cities\n- The ability to look up tomorrow's specific flight schedules\n\nThis question requires current, specific flight information that I don't have access to. Instead of guessing or providing potentially incorrect information, I should explain this limitation and suggest ways the user could find this information.", + signature: "EqoBCkgIARABGAIiQBVA7FBNLRtWarDSy9TAjwtOpcTSYHJ+2GYEoaorq3V+d3eapde04bvEfykD/66xZXjJ5yyqogJ8DEkNMotspRsSDKzuUJ9FKhSNt/3PdxoMaFZuH+1z1aLF8OeQIjCrA1+T2lsErrbgrve6eDWeMvP+1sqVqv/JcIn1jOmuzrPi2tNz5M0oqkOO9txJf7QqEPPw6RG3JLO2h7nV1BMN6wE=" + } + ] + }, + { + role: "user", + content: "thanks that's good to know, how about to chennai?" + } + ] + }); + console.log(response); + } + // Call the function + getChatCompletionFunctions(); + ``` + ```js OpenAI NodeJS + import OpenAI from 'openai'; // We're using the v4 SDK + import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + + const openai = new OpenAI({ + apiKey: 'BEDROCK_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "bedrock", + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + strictOpenAICompliance: false + }) + }); + + // Generate a chat completion with streaming + async function getChatCompletionFunctions(){ + const response = await openai.chat.completions.create({ + model: "us.anthropic.claude-3-7-sonnet-20250219-v1:0", + max_tokens: 3000, + thinking: { + type: "enabled", + budget_tokens: 2030 + }, + stream: true, + messages: [ + { + role: "user", + content: [ + { + type: "text", + text: "when does the flight from baroda to bangalore land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + }, + { + role: "assistant", + content: [ + { + type: "thinking", + thinking: "The user is asking several questions about a flight from Baroda (also known as Vadodara) to Bangalore:\n1. When does the flight land tomorrow\n2. What time does it land\n3. What is the flight number\n4. What is the baggage belt number at the arrival airport\n\nTo properly answer these questions, I would need access to airline flight schedules and airport information systems. However, I don't have:\n- Real-time or scheduled flight information\n- Access to airport baggage claim allocation systems\n- Information about specific flights between these cities\n- The ability to look up tomorrow's specific flight schedules\n\nThis question requires current, specific flight information that I don't have access to. Instead of guessing or providing potentially incorrect information, I should explain this limitation and suggest ways the user could find this information.", + signature: "EqoBCkgIARABGAIiQBVA7FBNLRtWarDSy9TAjwtOpcTSYHJ+2GYEoaorq3V+d3eapde04bvEfykD/66xZXjJ5yyqogJ8DEkNMotspRsSDKzuUJ9FKhSNt/3PdxoMaFZuH+1z1aLF8OeQIjCrA1+T2lsErrbgrve6eDWeMvP+1sqVqv/JcIn1jOmuzrPi2tNz5M0oqkOO9txJf7QqEPPw6RG3JLO2h7nV1BMN6wE=" + } + ] + }, + { + role: "user", + content: "thanks that's good to know, how about to chennai?" + } + ], + }); + + console.log(response) + + } + await getChatCompletionFunctions(); + ``` + ```py OpenAI Python + from openai import OpenAI + from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + + openai = OpenAI( + api_key='BEDROCK_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="bedrock", + api_key="PORTKEY_API_KEY", + strict_openai_compliance=False + ) + ) + + + response = openai.chat.completions.create( + model="us.anthropic.claude-3-7-sonnet-20250219-v1:0", + max_tokens=3000, + thinking={ + "type": "enabled", + "budget_tokens": 2030 + }, + stream=True, + messages=[ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "when does the flight from baroda to bangalore land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + }, + { + "role": "assistant", + "content": [ + { + "type": "thinking", + "thinking": "The user is asking several questions about a flight from Baroda (also known as Vadodara) to Bangalore:\n1. When does the flight land tomorrow\n2. What time does it land\n3. What is the flight number\n4. What is the baggage belt number at the arrival airport\n\nTo properly answer these questions, I would need access to airline flight schedules and airport information systems. However, I don't have:\n- Real-time or scheduled flight information\n- Access to airport baggage claim allocation systems\n- Information about specific flights between these cities\n- The ability to look up tomorrow's specific flight schedules\n\nThis question requires current, specific flight information that I don't have access to. Instead of guessing or providing potentially incorrect information, I should explain this limitation and suggest ways the user could find this information.", + signature: "EqoBCkgIARABGAIiQBVA7FBNLRtWarDSy9TAjwtOpcTSYHJ+2GYEoaorq3V+d3eapde04bvEfykD/66xZXjJ5yyqogJ8DEkNMotspRsSDKzuUJ9FKhSNt/3PdxoMaFZuH+1z1aLF8OeQIjCrA1+T2lsErrbgrve6eDWeMvP+1sqVqv/JcIn1jOmuzrPi2tNz5M0oqkOO9txJf7QqEPPw6RG3JLO2h7nV1BMN6wE=" + } + ] + }, + { + "role": "user", + "content": "thanks that's good to know, how about to chennai?" + } + ] + ) + + print(response) + ``` + ```sh cURL + curl "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-provider: bedrock" \ + -H "x-api-key: $BEDROCK_API_KEY" \ + -H "x-portkey-strict-openai-compliance: false" \ + -d '{ + "model": "us.anthropic.claude-3-7-sonnet-20250219-v1:0", + "max_tokens": 3000, + "thinking": { + "type": "enabled", + "budget_tokens": 2030 + }, + "stream": true, + "messages": [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "when does the flight from baroda to bangalore land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + }, + { + "role": "assistant", + "content": [ + { + "type": "thinking", + "thinking": "The user is asking several questions about a flight from Baroda (also known as Vadodara) to Bangalore:\n1. When does the flight land tomorrow\n2. What time does it land\n3. What is the flight number\n4. What is the baggage belt number at the arrival airport\n\nTo properly answer these questions, I would need access to airline flight schedules and airport information systems. However, I don't have:\n- Real-time or scheduled flight information\n- Access to airport baggage claim allocation systems\n- Information about specific flights between these cities\n- The ability to look up tomorrow's specific flight schedules\n\nThis question requires current, specific flight information that I don't have access to. Instead of guessing or providing potentially incorrect information, I should explain this limitation and suggest ways the user could find this information.", + "signature": "EqoBCkgIARABGAIiQBVA7FBNLRtWarDSy9TAjwtOpcTSYHJ+2GYEoaorq3V+d3eapde04bvEfykD/66xZXjJ5yyqogJ8DEkNMotspRsSDKzuUJ9FKhSNt/3PdxoMaFZuH+1z1aLF8OeQIjCrA1+T2lsErrbgrve6eDWeMvP+1sqVqv/JcIn1jOmuzrPi2tNz5M0oqkOO9txJf7QqEPPw6RG3JLO2h7nV1BMN6wE=" + } + ] + }, + { + "role": "user", + "content": "thanks that's good to know, how about to chennai?" + } + ] + }' + ``` + + +## Inference Profiles + +[Inference profiles](https://docs.aws.amazon.com/bedrock/latest/userguide/inference-profiles.html) are a resource in Amazon Bedrock that define a model and one or more Regions to which the inference profile can route model invocation requests. + +To use inference profiles, your IAM role needs to additionally have the following permissions: +```json +{ + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Action": [ + "bedrock:GetInferenceProfile" + ], + "Resource": [ + "arn:aws:bedrock:*:*:inference-profile/*", + "arn:aws:bedrock:*:*:application-inference-profile/*" + ] + } + ] +} +``` +This is a pre-requisite for using inference profiles, as the gateway needs to fetch the foundation model to process the request. + +For reference, see the following documentation: +https://docs.aws.amazon.com/bedrock/latest/userguide/inference-profiles-prereq.html + + +## Bedrock Converse API + +Portkey uses the [AWS Converse API](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Converse.html) internally for making chat completions requests. + +If you need to pass [additional input fields or parameters](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Converse.html#API_runtime_Converse_RequestSyntax) like `anthropic_beta`, `top_k`, `frequency_penalty` etc. that are specific to a model, you can pass it with this key: +```json +"additionalModelRequestFields": { + "frequency_penalty": 0.4 +} +``` + +If you require the model to [respond with certain fields](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Converse.html#API_runtime_Converse_RequestSyntax) that are specific to a model, you need to pass this key: +```json +"additionalModelResponseFieldPaths": [ "/stop_sequence" ] +``` + +## Managing AWS Bedrock Prompts + +You can manage all prompts to AWS bedrock in the [Prompt Library](/product/prompt-library). All the current models of Anthropic are supported and you can easily start testing different prompts. + +Once you're ready with your prompt, you can use the `portkey.prompts.completions.create` interface to use the prompt in your application. + +## Making Requests without Virtual Keys + +If you do not want to add your AWS details to Portkey vault, you can also directly pass them while instantiating the Portkey client. + +### Mapping the Bedrock Details + +| Node SDK | Python SDK | REST Headers | +| ------------------ | ------------------------ | ------------------------------- | +| awsAccessKeyId | aws\_access\_key\_id | x-portkey-aws-access-key-id | +| awsSecretAccessKey | aws\_secret\_access\_key | x-portkey-aws-secret-access-key | +| awsRegion | aws\_region | x-portkey-aws-region | +| awsSessionToken | aws\_session\_token | x-portkey-aws-session-token | + +### Example + + + + +```js +import Portkey from 'portkey-ai' + +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + provider: "bedrock", + awsAccessKeyId: "AWS_ACCESS_KEY_ID", + awsSecretAccessKey: "AWS_SECRET_ACCESS_KEY", + awsRegion: "us-east-1", + awsSessionToken: "AWS_SESSION_TOKEN" +}) +``` + + + +```python +from portkey_ai import Portkey + +client = Portkey( + api_key="PORTKEY_API_KEY", + provider="bedrock", + aws_access_key_id="", + aws_secret_access_key="", + aws_region="us-east-1", + aws_session_token="" +) +``` + + + +```sh +curl https://api.portkey.ai/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-provider: bedrock" \ + -H "x-portkey-aws-access-key-id: $AWS_ACCESS_KEY_ID" \ + -H "x-portkey-aws-secret-access-key: $AWS_SECRET_ACCESS_KEY" \ + -H "x-portkey-aws-region: $AWS_REGION" \ + -H "x-portkey-aws-session-token: $AWS_TOKEN" \ + -d '{ + "model": "gpt-4o", + "messages": [{"role": "user","content": "Hello!"}] + }' +``` + + + +--- + +## Supported Models + + +--- + +## How to Find Your AWS Credentials + +[Navigate here in the AWS Management Console](https://us-east-1.console.aws.amazon.com/iam/home#/security%5Fcredentials) to obtain your **AWS Access Key ID** and **AWS** **Secret Access Key.** + +* In the console, you'll find the '**Access keys'** section. Click on '**Create access key**'. +* Copy the `Secret Access Key` once it is generated, and you can view the `Access Key ID` along with it. + + + acess + + +* On the same [page](https://us-east-1.console.aws.amazon.com/iam/home#/security%5Fcredentials) under the '**Access keys'** section, where you created your Secret Access key, you will also find your **Access Key ID.** + + retrieve + + +* And lastly, get Your `AWS Region` from the Home Page of[ AWS Bedrock](https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/overview) as shown in the image below. + + bedrock + + +--- + +## Next Steps + +The complete list of features supported in the SDK are available on the link below. + + + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your Bedrock requests](/product/ai-gateway/configs) +3. [Tracing Bedrock requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to Beckrock APIs](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/bedrock/batches.mdx b/virtual_key_old/integrations/llms/bedrock/batches.mdx new file mode 100644 index 00000000..e2b6b3f1 --- /dev/null +++ b/virtual_key_old/integrations/llms/bedrock/batches.mdx @@ -0,0 +1,696 @@ +--- +title: Batches +description: Perform batch inference with Bedrock +--- + +To perform batch inference with Bedrock, you need to upload files to S3. +This process can be cumbersome and duplicative in nature because you need to transform your data into model specific formats. + +With Portkey, you can upload the file in [OpenAI format](https://platform.openai.com/docs/guides/batch#1-preparing-your-batch-file) and portkey will handle transforming the file into the format required by Bedrock on the fly! + +This is the most efficient way to +- Test your data with different foundation models +- Perform A/B testing with different foundation models +- Perform batch inference with different foundation models + +## Create Batch Job + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + provider="bedrock", + aws_access_key_id="YOUR_AWS_ACCESS_KEY_ID", + aws_secret_access_key="YOUR_AWS_SECRET_ACCESS_KEY", + aws_region="YOUR_AWS_REGION", + aws_s3_bucket="YOUR_AWS_S3_BUCKET", + aws_s3_object_key="YOUR_AWS_S3_OBJECT_KEY", + aws_bedrock_model="YOUR_AWS_BEDROCK_MODEL" +) + +start_batch_response = portkey.batches.create( + input_file_id="file_id", # file id of the input file + endpoint="endpoint", # ex: /v1/chat/completions + completion_window="completion_window", # ex: 24h + metadata={}, # metadata for the batch, + role_arn="arn:aws:iam::12312:role/BedrockBatchRole", # the role to use for creating the batch job + model="anthropic.claude-3-5-sonnet-20240620-v1:0", # the model to use for the batch + output_data_config={ + "s3OutputDataConfig": { + "s3Uri": "s3://generations-raw/", + "s3EncryptionKeyId": "arn:aws:kms:us-west-2:517194595696:key/89b483cb-130d-497b-aa37-7db177e7cd32" # this is optional, if you want to use a KMS key to encrypt the output data + } + }, # output_data_config is optional, if you want to specify a different output location for the batch job, default is the same as the input file + job_name="anthropi-requests-test" # optional +) + +print(start_batch_response) +``` + + +```js +import { Portkey } from 'portkey-ai'; + +// Initialize the Portkey client +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + provider="bedrock", + awsAccessKeyId="YOUR_AWS_ACCESS_KEY_ID", + awsSecretAccessKey="YOUR_AWS_SECRET_ACCESS_KEY", + awsRegion="YOUR_AWS_REGION", + awsS3Bucket="YOUR_AWS_S3_BUCKET", + awsS3ObjectKey="YOUR_AWS_S3_OBJECT_KEY", + awsBedrockModel="YOUR_AWS_BEDROCK_MODEL" +}); + +const startBatch = async () => { + const startBatchResponse = await portkey.batches.create({ + input_file_id: "file_id", // file id of the input file + endpoint: "endpoint", // ex: /v1/chat/completions + completion_window: "completion_window", // ex: 24h + metadata: {}, // metadata for the batch + role_arn: "arn:aws:iam::12312:role/BedrockBatchRole", // the role to use for creating the batch job + model: "anthropic.claude-3-5-sonnet-20240620-v1:0", // the model to use for the batch + output_data_config: { + s3OutputDataConfig: { + s3Uri: "s3://generations-raw/", + s3EncryptionKeyId: "arn:aws:kms:us-west-2:517194595696:key/89b483cb-130d-497b-aa37-7db177e7cd32" // this is optional, if you want to use a KMS key to encrypt the output data + } + }, // output_data_config is optional, if you want to specify a different output location for the batch job, default is the same as the input file + job_name: "anthropi-requests-test" // optional + }); + + console.log(startBatchResponse); +} + +await startBatch(); + +``` + + +```sh +curl --location 'https://api.portkey.ai/v1/batches' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-aws-access-key-id: {YOUR_AWS_ACCESS_KEY_ID}' \ +--header 'x-portkey-aws-secret-access-key: {YOUR_AWS_SECRET_ACCESS_KEY}' \ +--header 'x-portkey-aws-region: {YOUR_AWS_REGION}' \ +--header 'Content-Type: application/json' \ +--data '{ + "model": "meta.llama3-1-8b-instruct-v1:0", + "input_file_id": "s3%3A%2F%2Fgenerations-raw-west-2%2Fbatch_files%2Fllama2%2Fbatch_chat_completions_101_requests.jsonl", + "role_arn": "arn:aws:iam::12312:role/BedrockBatchRole", // the role to use for creating the batch job + "output_data_config": { // output_data_config is optional, if you want to specify a different output location for the batch job, default is the same as the input file + "s3OutputDataConfig": { + "s3Uri": "s3://generations-raw/", + "s3EncryptionKeyId": "arn:aws:kms:us-west-2:517194595696:key/89b483cb-130d-497b-aa37-7db177e7cd32" // this is optional, if you want to use a KMS key to encrypt the output data + } + }, + "job_name": "anthropi-requests" // optional +}' +``` + + +```js +import OpenAI from 'openai'; // We're using the v4 SDK +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'PLACEHOLDER_NOT_USED', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + awsAccessKeyId: "YOUR_AWS_ACCESS_KEY_ID", + awsSecretAccessKey: "YOUR_AWS_SECRET_ACCESS_KEY", + awsRegion: "YOUR_AWS_REGION", + awsS3Bucket: "YOUR_AWS_S3_BUCKET", + awsS3ObjectKey: "YOUR_AWS_S3_OBJECT_KEY", + awsBedrockModel: "YOUR_AWS_BEDROCK_MODEL" + }) +}); + +const startBatch = async () => { + const startBatchResponse = await openai.batches.create({ + input_file_id: "file_id", // file id of the input file + endpoint: "endpoint", // ex: /v1/chat/completions + completion_window: "completion_window", // ex: 24h + metadata: {}, // metadata for the batch + role_arn: "arn:aws:iam::12312:role/BedrockBatchRole", // the role to use for creating the batch job + model: "anthropic.claude-3-5-sonnet-20240620-v1:0", // the model to use for the batch + output_data_config: { + s3OutputDataConfig: { + s3Uri: "s3://generations-raw/", + s3EncryptionKeyId: "arn:aws:kms:us-west-2:517194595696:key/89b483cb-130d-497b-aa37-7db177e7cd32" // this is optional, if you want to use a KMS key to encrypt the output data + } + }, // output_data_config is optional, if you want to specify a different output location for the batch job, default is the same as the input file + job_name: "anthropi-requests-test" // optional + }); + + console.log(startBatchResponse); +} + +await startBatch(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='PLACEHOLDER_NOT_USED', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY", + aws_access_key_id="YOUR_AWS_ACCESS_KEY_ID", + aws_secret_access_key="YOUR_AWS_SECRET_ACCESS_KEY", + aws_region="YOUR_AWS_REGION", + aws_s3_bucket="YOUR_AWS_S3_BUCKET", + aws_s3_object_key="YOUR_AWS_S3_OBJECT_KEY", + aws_bedrock_model="YOUR_AWS_BEDROCK_MODEL" + ) +) + +start_batch_response = openai.batches.create( + input_file_id="file_id", # file id of the input file + endpoint="endpoint", # ex: /v1/chat/completions + completion_window="completion_window", # ex: 24h + metadata={}, # metadata for the batch + role_arn="arn:aws:iam::12312:role/BedrockBatchRole", # the role to use for creating the batch job + model="anthropic.claude-3-5-sonnet-20240620-v1:0", # the model to use for the batch + output_data_config={ + "s3OutputDataConfig": { + "s3Uri": "s3://generations-raw/", + "s3EncryptionKeyId": "arn:aws:kms:us-west-2:517194595696:key/89b483cb-130d-497b-aa37-7db177e7cd32" // this is optional, if you want to use a KMS key to encrypt the output data + } + }, # output_data_config is optional, if you want to specify a different output location for the batch job, default is the same as the input file + job_name="anthropi-requests-test" # optional +) +print(start_batch_response) +``` + + + + +## List Batch Jobs + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + provider="bedrock", + aws_access_key_id="YOUR_AWS_ACCESS_KEY_ID", + aws_secret_access_key="YOUR_AWS_SECRET_ACCESS_KEY", + aws_region="YOUR_AWS_REGION", +) + +batches = portkey.batches.list() + +print(batches) +``` + + +```js +import { Portkey } from 'portkey-ai'; + +// Initialize the Portkey client +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + provider="bedrock", + awsAccessKeyId="YOUR_AWS_ACCESS_KEY_ID", + awsSecretAccessKey="YOUR_AWS_SECRET_ACCESS_KEY", + awsRegion="YOUR_AWS_REGION", +}); + +const listBatches = async () => { + const batches = await portkey.batches.list(); + + console.log(batches); +} + +await listBatches(); + +``` + + +```sh +curl --location 'https://api.portkey.ai/v1/batches' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' \ +``` + + +```js +import OpenAI from 'openai'; // We're using the v4 SDK +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'PLACEHOLDER_NOT_USED', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + awsAccessKeyId: "YOUR_AWS_ACCESS_KEY_ID", + awsSecretAccessKey: "YOUR_AWS_SECRET_ACCESS_KEY", + awsRegion: "YOUR_AWS_REGION" + }) +}); + +const listBatches = async () => { + const batches = await openai.batches.list(); + + console.log(batches); +} + +await listBatches(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='PLACEHOLDER_NOT_USED', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY", + aws_access_key_id="YOUR_AWS_ACCESS_KEY_ID", + aws_secret_access_key="YOUR_AWS_SECRET_ACCESS_KEY", + aws_region="YOUR_AWS_REGION" + ) +) + +batches = openai.batches.list() + +print(batches) +``` + + + + + +## Get Batch Job Details + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY", # Add your provider's virtual key +) + +batch = portkey.batches.retrieve(batch_id="batch_id") + +print(batch) +``` + + +```js +import { Portkey } from 'portkey-ai'; + +// Initialize the Portkey client +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY", // Add your provider's virtual key +}); + +const getBatch = async () => { + const batch = await portkey.batches.retrieve(batch_id="batch_id"); + + console.log(batch); +} + +await getBatch(); + +``` + + +```sh +curl --location 'https://api.portkey.ai/v1/batches/' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' \ +``` + + +```js +import OpenAI from 'openai'; // We're using the v4 SDK +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'PLACEHOLDER_NOT_USED', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "BEDROCK_VIRTUAL_KEY", + }) +}); + +const getBatch = async () => { + const batch = await openai.batches.retrieve(batch_id="batch_id"); + + console.log(batch); +} + +await getBatch(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='PLACEHOLDER_NOT_USED', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY", + virtualKey: "BEDROCK_VIRTUAL_KEY", + ) +) + +batch = openai.batches.retrieve(batch_id="batch_id") + +print(batch) +``` + + + + + +## Get Batch Output + + +```sh +curl --location 'https://api.portkey.ai/v1/batches//output' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' \ +``` + + + + +## List Batch Jobs + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY", # Add your provider's virtual key +) + +batches = portkey.batches.list() + +print(batches) +``` + + +```js +import { Portkey } from 'portkey-ai'; + +// Initialize the Portkey client +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY", // Add your provider's virtual key +}); + +const listBatchingJobs = async () => { + const batching_jobs = await portkey.batches.list(); + + console.log(batching_jobs); +} + +await listBatchingJobs(); + +``` + + +```sh +curl --location 'https://api.portkey.ai/v1/batches' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' \ +``` + + +```js +import OpenAI from 'openai'; // We're using the v4 SDK +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'PLACEHOLDER_NOT_USED', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "BEDROCK_VIRTUAL_KEY", + }) +}); + +const listBatchingJobs = async () => { + const batching_jobs = await openai.batches.list(); + + console.log(batching_jobs); +} + +await listBatchingJobs(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='PLACEHOLDER_NOT_USED', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY", + virtualKey: "BEDROCK_VIRTUAL_KEY", + ) +) + +batching_jobs = openai.batches.list() + +print(batching_jobs) +``` + + + + +## Cancel Batch Job + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY", # Add your provider's virtual key +) + +cancel_batch_response = portkey.batches.cancel(batch_id="batch_id") + +print(cancel_batch_response) +``` + + +```js +import { Portkey } from 'portkey-ai'; + +// Initialize the Portkey client +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY", // Add your provider's virtual key +}); + +const cancelBatch = async () => { + const cancel_batch_response = await portkey.batches.cancel(batch_id="batch_id"); + + console.log(cancel_batch_response); +} + +await cancelBatch(); + +``` + + +```sh +curl --request POST --location 'https://api.portkey.ai/v1/batches//cancel' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' \ +``` + + +```js +import OpenAI from 'openai'; // We're using the v4 SDK +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'PLACEHOLDER_NOT_USED', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "BEDROCK_VIRTUAL_KEY", + }) +}); + +const cancelBatch = async () => { + const cancel_batch_response = await openai.batches.cancel(batch_id="batch_id"); + + console.log(cancel_batch_response); +} + +await cancelBatch(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='PLACEHOLDER_NOT_USED', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY", + virtualKey: "BEDROCK_VIRTUAL_KEY", + ) +) + +cancel_batch_response = openai.batches.cancel(batch_id="batch_id") + +print(cancel_batch_response) +``` + + + + + +## Information about Permissions and IAM Roles + + + These are the minimum permissions required to use the Bedrock Batch APIs. + ```json + { + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Action": [ + "bedrock:ListFoundationModels", + "bedrock:GetFoundationModel", + "bedrock:ListInferenceProfiles", + "bedrock:GetInferenceProfile", + "bedrock:ListCustomModels", + "bedrock:GetCustomModel", + "bedrock:TagResource", + "bedrock:UntagResource", + "bedrock:ListTagsForResource", + "bedrock:CreateModelInvocationJob", + "bedrock:GetModelInvocationJob", + "bedrock:ListModelInvocationJobs", + "bedrock:StopModelInvocationJob" + ], + "Resource": [ + "arn:aws:bedrock:::model-customization-job/*", + "arn:aws:bedrock:::custom-model/*", + "arn:aws:bedrock:::foundation-model/*" + ] + }, + { + "Effect": "Allow", + "Action": [ + "s3:ListBucket", + "s3:PutObject", + "s3:GetObject", + "s3:GetObjectAttributes" + ], + "Resource": [ + "arn:aws:s3:::", + "arn:aws:s3:::/*" + ] + }, + { + "Action": [ + "iam:PassRole" + ], + "Effect": "Allow", + "Resource": "arn:aws:iam:::role/", + "Condition": { + "StringEquals": { + "iam:PassedToService": [ + "bedrock.amazonaws.com" + ] + } + } + } + ] +} + ``` + + + + These are the minimum permissions required to use the Bedrock Batch APIs. + + Trust relationship: + ```json + { + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Principal": { + "Service": "bedrock.amazonaws.com" + }, + "Action": "sts:AssumeRole", + "Condition": { + "StringEquals": { + "aws:SourceAccount": "" + }, + "ArnEquals": { + "aws:SourceArn": "arn:aws:bedrock:::model-invocation-job/*" + } + } + ] + } + ``` + Permission Policy: + ```json + { + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Action": [ + "s3:GetObject", + "s3:PutObject", + "s3:ListBucket" + ], + "Resource": [ + "arn:aws:s3:::", + "arn:aws:s3:::/*" + ] + } + ] + } + ``` + \ No newline at end of file diff --git a/virtual_key_old/integrations/llms/bedrock/embeddings.mdx b/virtual_key_old/integrations/llms/bedrock/embeddings.mdx new file mode 100644 index 00000000..e00f7cfe --- /dev/null +++ b/virtual_key_old/integrations/llms/bedrock/embeddings.mdx @@ -0,0 +1,455 @@ +--- +title: "Embeddings" +description: "Get embeddings from Bedrock" +--- + +Bedrock supports embedding text and images through Amazon Titan and Cohere models. +Portkey provides a standardized interface for embedding multiple modalities. + +# Bedrock Titan + +## Embedding Text + + ```python Python + from portkey_ai import Portkey + + client = Portkey( + api_key="YOUR_PORTKEY_API_KEY", # defaults to os.environ.get("PORTKEY_API_KEY") + virtual_key="VIRTUAL_KEY", + ) + + embeddings = client.embeddings.create( + model="amazon.titan-embed-text-v2:0", + input="Hello this is a test", + # normalize=False # if you would like to disable normalization + # dimensions=1024, # embedding dimensions + # encoding_format="float", # embedding format + ) + ``` + + ```javascript NodeJS + import { Portkey } from 'portkey-ai'; + + const portkey = new Portkey({ + apiKey: "YOUR_API_KEY", + virtualKey: "YOUR_VIRTUAL_KEY" + }); + + const embedding = await portkey.embeddings.create({ + model: "amazon.titan-embed-text-v2:0", + input: "Hello this is a test", + // normalize: false, // if you would like to disable normalization + // dimensions: 1024, // embedding dimensions + // encoding_format: "float", // embedding format + }); + + console.log(embedding); + ``` + + ```sh cURL + curl --location 'https://api.portkey.ai/v1/embeddings' \ + --header 'Content-Type: application/json' \ + --header 'x-portkey-api-key: PORTKEY_API_KEY' \ + --header 'x-portkey-virtual-key: PORTKEY_VIRTUAL_KEY' \ + --data-raw '{ + "model": "amazon.titan-embed-text-v2:0", + "input": "Hello this is a test", + "normalize": false, // if you would like to disable normalization + "dimensions": 1024, // embedding dimensions + "encoding_format": "float" // embedding format + }' + ``` + + ```python OpenAI Python + from openai import OpenAI + from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + + portkey_client = OpenAI( + api_key='NOT_REQUIRED', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY" + ) + ) + + embeddings = portkey_client.embeddings.create( + model="amazon.titan-embed-text-v2:0", + input="Hello this is a test", + # normalize=False # if you would like to disable normalization + # dimensions=1024, # embedding dimensions + # encoding_format="float", # embedding format + ) + ``` + + ```js OpenAI NodeJS + import OpenAI from 'openai'; // We're using the v4 SDK + import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + + const portkeyClient = new OpenAI({ + apiKey: 'NOT_REQUIRED', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "vertex-ai", + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "PORTKEY_VIRTUAL_KEY" + }) + }); + + const embedding = await portkeyClient.embeddings.create({ + model: "amazon.titan-embed-text-v2:0", + input: "Hello this is a test", + // normalize=False, // if you would like to disable normalization + // dimensions=1024, // embedding dimensions + // encoding_format="float", // embedding format + }); + + console.log(embedding); + ``` + + + +## Embeddings Images + + + ```python Python + from portkey_ai import Portkey + + client = Portkey( + api_key="YOUR_PORTKEY_API_KEY", # defaults to os.environ.get("PORTKEY_API_KEY") + virtual_key="VIRTUAL_KEY", + ) + + embeddings = client.embeddings.create( + model="amazon.titan-embed-image-v1", + dimensions=256, + input=[ + { + "text": "this is the caption of the image", + "image": { + "base64": "UklGRkacAABXRUJQVlA4IDqcAACQggKdASqpAn8B....." + } + } + ] + ) + ``` + + ```javascript NodeJS + import { Portkey } from 'portkey-ai'; + + const portkey = new Portkey({ + apiKey: "YOUR_API_KEY", + virtualKey: "YOUR_VIRTUAL_KEY" + }); + + const embedding = await portkey.embeddings.create({ + model: "amazon.titan-embed-image-v1", + dimensions: 256, + input: [ + { + "text": "this is the caption of the image", + "image": { + "base64": "UklGRkacAABXRUJQVlA4IDqcAACQggKdASqpAn8B....." + } + } + ] + }); + + console.log(embedding); + ``` + + ```sh cURL + curl --location 'https://api.portkey.ai/v1/embeddings' \ + --header 'Content-Type: application/json' \ + --header 'x-portkey-api-key: PORTKEY_API_KEY' \ + --header 'x-portkey-virtual-key: PORTKEY_VIRTUAL_KEY' \ + --data-raw '{ + "model": "amazon.titan-embed-image-v1", + "dimensions": 256, + "input": [ + { + "text": "this is the caption of the image", + "image": { + "base64": "UklGRkacAABXRUJQVlA4IDqcAACQggKdASqpAn8B....." + } + } + ] + }' + ``` + + ```python OpenAI Python + from openai import OpenAI + from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + + portkey_client = OpenAI( + api_key='NOT_REQUIRED', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY" + ) + ) + + embeddings = portkey_client.embeddings.create( + model="amazon.titan-embed-image-v1", + dimensions=256, + input=[ + { + "text": "this is the caption of the image", + "image": { + "base64": "UklGRkacAABXRUJQVlA4IDqcAACQggKdASqpAn8B....." + } + } + ] + ) + ``` + + ```js OpenAI NodeJS + import OpenAI from 'openai'; // We're using the v4 SDK + import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + + const portkeyClient = new OpenAI({ + apiKey: 'NOT_REQUIRED', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "PORTKEY_VIRTUAL_KEY" + }) + }); + + const embedding = await portkeyClient.embeddings.create({ + model: "amazon.titan-embed-image-v1", + dimensions: 256, + input: [ + { + text: "this is the caption of the image", + image: { + base64: "UklGRkacAABXRUJQVlA4IDqcAACQggKdASqpAn8B....." + } + } + ] + }); + + console.log(embedding); + ``` + + + +# Cohere + +## Embedding Text + + ```python Python + from portkey_ai import Portkey + + client = Portkey( + api_key="YOUR_PORTKEY_API_KEY", # defaults to os.environ.get("PORTKEY_API_KEY") + virtual_key="VIRTUAL_KEY", + ) + + embeddings = client.embeddings.create( + model="cohere.embed-english-v3", + input=["Hello this is a test", "skibidi"], + input_type="classification" + ) + ``` + + ```javascript NodeJS + import { Portkey } from 'portkey-ai'; + + const portkey = new Portkey({ + apiKey: "YOUR_API_KEY", + virtualKey: "YOUR_VIRTUAL_KEY" + }); + + const embedding = await portkey.embeddings.create({ + model: "cohere.embed-english-v3", + input: ["Hello this is a test", "skibidi"], + input_type: "classification" + }); + + console.log(embedding); + ``` + + ```sh cURL + curl --location 'https://api.portkey.ai/v1/embeddings' \ + --header 'Content-Type: application/json' \ + --header 'x-portkey-api-key: PORTKEY_API_KEY' \ + --header 'x-portkey-virtual-key: PORTKEY_VIRTUAL_KEY' \ + --data-raw '{ + "model": "cohere.embed-english-v3", + "input": ["Hello this is a test", "skibidi"], + "input_type": "classification" + }' + ``` + + ```python OpenAI Python + from openai import OpenAI + from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + + portkey_client = OpenAI( + api_key='NOT_REQUIRED', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY" + ) + ) + + embeddings = portkey_client.embeddings.create( + model="cohere.embed-english-v3", + input=["Hello this is a test", "skibidi"], + input_type="classification" + ) + ``` + + ```js OpenAI NodeJS + import OpenAI from 'openai'; // We're using the v4 SDK + import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + + const portkeyClient = new OpenAI({ + apiKey: 'NOT_REQUIRED', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "vertex-ai", + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "PORTKEY_VIRTUAL_KEY" + }) + }); + + const embedding = await portkeyClient.embeddings.create({ + model: "cohere.embed-english-v3", + input: ["Hello this is a test", "skibidi"], + input_type: "classification" + }); + + console.log(embedding); + ``` + + + +## Embeddings Images + + + ```python Python + from portkey_ai import Portkey + + client = Portkey( + api_key="YOUR_PORTKEY_API_KEY", # defaults to os.environ.get("PORTKEY_API_KEY") + virtual_key="VIRTUAL_KEY", + ) + + embeddings = client.embeddings.create( + model="cohere.embed-english-v3", + input_type="image", + dimensions=256, + input=[ + { + "image": { + "base64": "Data:image/webp;base64,UklGRkacAABXRUJQVlA4IDqcAACQggKdASqpAn8B....." + } + } + ] + ) + ``` + + ```javascript NodeJS + import { Portkey } from 'portkey-ai'; + + const portkey = new Portkey({ + apiKey: "YOUR_API_KEY", + virtualKey: "YOUR_VIRTUAL_KEY" + }); + + const embedding = await portkey.embeddings.create({ + "model": "cohere.embed-english-v3", + "input_type": "image", + "dimensions": 256, + "input": [ + { + "image": { + "base64": "Data:image/webp;base64,UklGRkacAABXRUJQVlA4IDqcAACQggKdASqpAn8B....." + } + } + ] + }); + + console.log(embedding); + ``` + + ```sh cURL + curl --location 'https://api.portkey.ai/v1/embeddings' \ + --header 'Content-Type: application/json' \ + --header 'x-portkey-api-key: PORTKEY_API_KEY' \ + --header 'x-portkey-virtual-key: PORTKEY_VIRTUAL_KEY' \ + --data-raw '{ + "model": "cohere.embed-english-v3", + "input_type": "image", + "dimensions": 256, + "input": [ + { + "image": { + "base64": "Data:image/webp;base64,UklGRkacAABXRUJQVlA4IDqcAACQggKdASqpAn8B....." + } + } + ] + }' + ``` + + ```python OpenAI Python + from openai import OpenAI + from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + + portkey_client = OpenAI( + api_key='NOT_REQUIRED', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY" + ) + ) + + embeddings = portkey_client.embeddings.create( + model="cohere.embed-english-v3", + input_type="image", + dimensions=256, + input=[ + { + image: { + base64: "Data:image/webp;base64,UklGRkacAABXRUJQVlA4IDqcAACQggKdASqpAn8B....." + } + } + ] + ) + ``` + + ```js OpenAI NodeJS + import OpenAI from 'openai'; // We're using the v4 SDK + import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + + const portkeyClient = new OpenAI({ + apiKey: 'NOT_REQUIRED', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "PORTKEY_VIRTUAL_KEY" + }) + }); + + const embedding = await portkeyClient.embeddings.create({ + model: "cohere.embed-english-v3", + input_type: "image", + dimensions: 256, + input: [ + { + image: { + base64: "Data:image/webp;base64,UklGRkacAABXRUJQVlA4IDqcAACQggKdASqpAn8B....." + } + } + ] + }); + + console.log(embedding); + ``` + + \ No newline at end of file diff --git a/virtual_key_old/integrations/llms/bedrock/files.mdx b/virtual_key_old/integrations/llms/bedrock/files.mdx new file mode 100644 index 00000000..8f5fa583 --- /dev/null +++ b/virtual_key_old/integrations/llms/bedrock/files.mdx @@ -0,0 +1,351 @@ +--- +title: Files +description: Upload files to S3 for Bedrock batch inference +--- + +To perform batch inference with Bedrock, you need to upload files to S3. +This process can be cumbersome and duplicative in nature because you need to transform your data into model specific formats. + +With Portkey, you can upload the file in [OpenAI format](https://platform.openai.com/docs/guides/batch#1-preparing-your-batch-file) and portkey will handle transforming the file into the format required by Bedrock on the fly! + +This is the most efficient way to +- Test your data with different foundation models +- Perform A/B testing with different foundation models +- Perform batch inference with different foundation models + +## Uploading Files + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + provider="bedrock", + aws_region="YOUR_AWS_REGION", + aws_s3_bucket="YOUR_AWS_S3_BUCKET", + aws_s3_object_key="YOUR_AWS_S3_OBJECT_KEY", + aws_bedrock_model="YOUR_AWS_BEDROCK_MODEL", + amz_server_side_encryption: "ENCRYPTION_TYPE", # [optional] default is aws:kms + amz_server_side_encryption_aws_kms_key_id: "KMS_KEY_ID" # [optional] use this only if you want to use a KMS key to encrypt the file at rest +) + +upload_file_response = portkey.files.create( + purpose="batch", + file=open("file.pdf", "rb") +) + +print(upload_file_response) +``` + + +```js +import { Portkey } from 'portkey-ai'; + +// Initialize the Portkey client +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY", // Add your provider's virtual key + provider: "bedrock", + awsRegion: "YOUR_AWS_REGION", + awsS3Bucket: "YOUR_AWS_S3_BUCKET", + awsS3ObjectKey: "YOUR_AWS_S3_OBJECT_KEY", + awsBedrockModel: "YOUR_AWS_BEDROCK_MODEL", + amzServerSideEncryption: "ENCRYPTION_TYPE", // [optional] default is aws:kms + amzServerSideEncryptionAwsKmsKeyId: "KMS_KEY_ID" // [optional] use this only if you want to use a KMS key to encrypt the file at rest +}); + +const uploadFile = async () => { + const file = await portkey.files.create({ + purpose: "batch", + file: fs.createReadStream("file.pdf") + }); + + console.log(file); +} + +await uploadFile(); + +``` + + +```sh +# you can also use a virtual key here +curl --location 'https://api.portkey.ai/v1/files' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-provider: bedrock' \ +--header 'Content-Type: application/json' \ +--header 'x-portkey-aws-access-key-id: {YOUR_AWS_ACCESS_KEY_ID}' \ +--header 'x-portkey-aws-secret-access-key: {YOUR_AWS_SECRET_ACCESS_KEY}' \ +--header 'x-portkey-aws-region: {YOUR_AWS_REGION}' \ +--header 'x-portkey-aws-s3-bucket: {YOUR_AWS_S3_BUCKET}' \ +--header 'x-portkey-aws-s3-object-key: {YOUR_AWS_S3_OBJECT_KEY}' \ +--header 'x-portkey-aws-bedrock-model: {YOUR_AWS_BEDROCK_MODEL}' \ +--header 'x-portkey-amz-server-side-encryption: {ENCRYPTION_TYPE}' \ +--header 'x-portkey-amz-server-side-encryption-aws-kms-key-id: {KMS_KEY_ID}' \ +--form 'file=@"{YOUR_FILE_PATH}"', +--form 'purpose="batch"' +``` + + +```js +import OpenAI from 'openai'; // We're using the v4 SDK +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + awsRegion: "YOUR_AWS_REGION", + awsS3Bucket: "YOUR_AWS_S3_BUCKET", + awsS3ObjectKey: "YOUR_AWS_S3_OBJECT_KEY", + awsBedrockModel: "YOUR_AWS_BEDROCK_MODEL", + amzServerSideEncryption: "ENCRYPTION_TYPE", // [optional] default is aws:kms + amzServerSideEncryptionAwsKmsKeyId: "KMS_KEY_ID" // [optional] use this only if you want to use a KMS key to encrypt the file at rest + }) +}); + +const uploadFile = async () => { + const file = await openai.files.create({ + purpose: "batch", + file: fs.createReadStream("file.pdf") + }); + + console.log(file); +} + +await uploadFile(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY", + aws_region="YOUR_AWS_REGION", + aws_s3_bucket="YOUR_AWS_S3_BUCKET", + aws_s3_object_key="YOUR_AWS_S3_OBJECT_KEY", + aws_bedrock_model="YOUR_AWS_BEDROCK_MODEL", + amz_server_side_encryption: "ENCRYPTION_TYPE", # [optional] default is aws:kms + amz_server_side_encryption_aws_kms_key_id: "KMS_KEY_ID" # [optional] use this only if you want to use a KMS key to encrypt the file at rest + ) +) + +upload_file_response = openai.files.create( + purpose="batch", + file=open("file.pdf", "rb") +) + +print(upload_file_response) +``` + + + + + +## Get File + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + aws_region="YOUR_AWS_REGION", +) + +file = portkey.files.retrieve(file_id="file_id") + +print(file) +``` + + +```js +import { Portkey } from 'portkey-ai'; + +// Initialize the Portkey client +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY", // Add your provider's virtual key + awsRegion="YOUR_AWS_REGION", +}); + +const getFile = async () => { + const file = await portkey.files.retrieve(file_id="file_id"); + + console.log(file); +} + +await getFile(); + +``` + + +```sh +curl --location 'https://api.portkey.ai/v1/files/' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-aws-region: ' +``` + + +```js +import OpenAI from 'openai'; // We're using the v4 SDK +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + awsRegion="YOUR_AWS_REGION", + }) +}); + +const getFile = async () => { + const file = await openai.files.retrieve(file_id="file_id"); + + console.log(file); +} + +await getFile(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY", + aws_region="YOUR_AWS_REGION", + ) +) + +file = openai.files.retrieve(file_id="file_id") + +print(file) +``` + + + + + +## Get File Content + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + aws_region="YOUR_AWS_REGION", +) + +file_content = portkey.files.content(file_id="file_id") + +print(file_content) +``` + + +```js +import { Portkey } from 'portkey-ai'; + +// Initialize the Portkey client +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY", // Add your provider's virtual key + awsRegion="YOUR_AWS_REGION", +}); + +const getFileContent = async () => { + const file_content = await portkey.files.content(file_id="file_id"); + + console.log(file_content); +} + +await getFileContent(); + +``` + + +```sh +curl --location 'https://api.portkey.ai/v1/files//content' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-aws-region: ' +``` + + +```js +import OpenAI from 'openai'; // We're using the v4 SDK +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + awsRegion="YOUR_AWS_REGION", + }) +}); + +const getFileContent = async () => { + const file_content = await openai.files.content(file_id="file_id"); + + console.log(file_content); +} + +await getFileContent(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY", + aws_region="YOUR_AWS_REGION", + ) +) + +file_content = openai.files.content(file_id="file_id") + +print(file_content) +``` + + + + + + + The following endpoints are **NOT** supported for Bedrock for security reasons: + - `GET /v1/files` + - `DELETE /v1/files/{file_id}` + + diff --git a/virtual_key_old/integrations/llms/bedrock/fine-tuning.mdx b/virtual_key_old/integrations/llms/bedrock/fine-tuning.mdx new file mode 100644 index 00000000..5a33db16 --- /dev/null +++ b/virtual_key_old/integrations/llms/bedrock/fine-tuning.mdx @@ -0,0 +1,465 @@ +--- +title: "Fine-tune" +description: "Fine-tune your models with Bedrock" +--- + +### Upload a file + +Please follow to the bedrock file upload [guide](/integrations/llms/bedrock/files) for more details. + +### Create a fine-tuning job + + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client + +portkey = Portkey( +api_key="PORTKEY_API_KEY", # Replace with your Portkey API key +virtual_key="VIRTUAL_KEY" # Add your provider's virtual key +) + +fine_tune_job = portkey.fine_tuning.jobs.create( + training_file="file_id", # encoded s3 file URI of the training data. + model="model_id", # ex: modelId from bedrock for fine-tuning + hyperparameters={ + "n_epochs": 1 + }, + role_arn="role_arn", # service role arn for bedrock job to assume when running. + job_name="job_name", # name for the job, optional will created random if not provided. + validation_file="file_id", # optional, must be encoded s3 file URI. + suffix="finetuned_model_name", + model_type="text" # optional, chat or text. + ) + +print(fine_tune_job) + +```` + + +```typescript +import { Portkey } from "portkey-ai"; +# Initialize the Portkey client +const portkey = Portkey( + apiKey="PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey="VIRTUAL_KEY" // Add your provider's virtual key +) + +(async () => { + const fine_tune_job = await portkey.fineTuning.jobs.create( + training_file:"file_id", // encoded s3 file URI of the training data. + model:"model_id", // ex: modelId from bedrock for fine-tuning + hyperparameters: { + "n_epochs": 1 + }, + role_arn: "role_arn", // service role arn for bedrock job to assume when running. + job_name: "job_name", // name for the job, optional will created random if not provided. + validation_file: "file_id", // optional, must be encoded s3 file URI. + suffix: "finetuned_model_name", + model_type: "text" // optional, chat or text. + ) + + console.log(fine_tune_job) +})(); +```` + + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + virtual_key="VIRTUAL_KEY", + api_key="PORTKEY_API_KEY" + ) +) + +fine_tune_job = openai.fine_tuning.jobs.create( + training_file="file_id", # encoded s3 file URI of the training data. + model="model_id", # bedrock modelId for fine-tuning + hyperparameters={ + "n_epochs": 1 + }, + role_arn="role_arn", # service role arn for bedrock job to assume when running. + job_name="job_name", # name for the job, optional will created random if not provided. + validation_file="file_id", # optional, must be encoded s3 file URI. + suffix="finetuned_model_name", + model_type="text" # optional, chat or text. + ) + +print(fine_tune_job) +``` + + +```typescript +import OpenAI from 'openai'; // We're using the v4 SDK +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + virtualKey: "VIRTUAL_KEY", + apiKey: "PORTKEY_API_KEY" // defaults to process.env["PORTKEY_API_KEY"] + }) +}); + +(async () => { + const fine_tune_job = await openai.fineTuning.jobs.create({ + training_file: "file_id", // encoded s3 file URI of the training data. + model: "model_id", // ex: `modelId` from bedrock for fine-tuning + hyperparameters: { + "n_epochs": 1 + }, + role_arn: "role_arn", // service role arn for bedrock job to assume when running. + job_name: "job_name", // name for the job, optional will created random if not provided. + validation_file: "file_id", // optional, must be encoded s3 file URI. + suffix: "finetuned_model_name", + model_type: "text" // optional, chat or text. + }); + + console.log(fine_tune_job) +})(); +``` + + + +```sh +curl \ +--header 'Content-Type: application/json' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-aws-s3-bucket: ' \ +--data '{ + "model": "", + "model_type": "text", #chat or text + "suffix": "", + "training_file": "", + "role_arn": "", + "job_name": "", + "hyperparameters": { + "n_epochs": 1 + } +}' \ +'https://api.portkey.ai/v1/fine_tuning/jobs' +``` + + + + +**Notes:** + +- Bedrock fine-tuning dataset format is a little bit different from OpenAI's fine-tuning dataset format. +- `model_type` field is required for the dataset transformation, currently gateway does the following dataset transformation: + - `chat` -> `text-to-text` + - `chat` -> `chat`. +- `model` param should be the `ModelID` that is required for fine-tuning not for the inference. `ModelID` is different for inference and fine-tuning. + +> List of supported finetune models and their IDs are available at [Bedrock documentation](https://docs.aws.amazon.com/bedrock/latest/userguide/model-customization.html) + +## List Fine-tuning Jobs + + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Add your provider's virtual key +) + +# List all fine-tuning jobs +jobs = portkey.fine_tuning.jobs.list( + limit=10 # Optional: Number of jobs to retrieve (default: 20) +) + +print(jobs) +``` + + +```typescript +import { Portkey } from "portkey-ai"; + +// Initialize the Portkey client +const portkey = Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key +}); + +(async () => { + // List all fine-tuning jobs + const jobs = await portkey.fineTuning.jobs.list({ + limit: 10 // Optional: Number of jobs to retrieve (default: 20) + }); + + console.log(jobs); +})(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + virtual_key="VIRTUAL_KEY", + api_key="PORTKEY_API_KEY" + ) +) + +# List all fine-tuning jobs +jobs = openai.fine_tuning.jobs.list( + limit=10 # Optional: Number of jobs to retrieve (default: 20) +) + +print(jobs) +``` + + +```typescript +import OpenAI from 'openai'; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'; + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + virtualKey: "VIRTUAL_KEY", + apiKey: "PORTKEY_API_KEY" + }) +}); + +(async () => { + // List all fine-tuning jobs + const jobs = await openai.fineTuning.jobs.list({ + limit: 10 // Optional: Number of jobs to retrieve (default: 20) + }); + + console.log(jobs); +})(); +``` + + +```sh +curl \ +--header 'Content-Type: application/json' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' \ +'https://api.portkey.ai/v1/fine_tuning/jobs?limit=10' +``` + + + +## Retrieve Fine-tuning Job + + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Add your provider's virtual key +) + +# Retrieve a specific fine-tuning job +job = portkey.fine_tuning.jobs.retrieve( + job_id="job_id" # The ID of the fine-tuning job to retrieve +) + +print(job) +``` + + +```typescript +import { Portkey } from "portkey-ai"; + +// Initialize the Portkey client +const portkey = Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key +}); + +(async () => { + // Retrieve a specific fine-tuning job + const job = await portkey.fineTuning.jobs.retrieve({ + job_id: "job_id" // The ID of the fine-tuning job to retrieve + }); + + console.log(job); +})(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + virtual_key="VIRTUAL_KEY", + api_key="PORTKEY_API_KEY" + ) +) + +# Retrieve a specific fine-tuning job +job = openai.fine_tuning.jobs.retrieve( + fine_tuning_job_id="job_id" # The ID of the fine-tuning job to retrieve +) + +print(job) +``` + + +```typescript +import OpenAI from 'openai'; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'; + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + virtualKey: "VIRTUAL_KEY", + apiKey: "PORTKEY_API_KEY" + }) +}); + +(async () => { + // Retrieve a specific fine-tuning job + const job = await openai.fineTuning.jobs.retrieve( + "job_id" // The ID of the fine-tuning job to retrieve + ); + + console.log(job); +})(); +``` + + +```sh +curl \ +--header 'Content-Type: application/json' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' \ +'https://api.portkey.ai/v1/fine_tuning/jobs/' +``` + + + +## Cancel Fine-tuning Job + + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Add your provider's virtual key +) + +# Cancel a fine-tuning job +cancelled_job = portkey.fine_tuning.jobs.cancel( + job_id="job_id" # The ID of the fine-tuning job to cancel +) + +print(cancelled_job) +``` + + +```typescript +import { Portkey } from "portkey-ai"; + +// Initialize the Portkey client +const portkey = Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key +}); + +(async () => { + // Cancel a fine-tuning job + const cancelledJob = await portkey.fineTuning.jobs.cancel({ + job_id: "job_id" // The ID of the fine-tuning job to cancel + }); + + console.log(cancelledJob); +})(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + virtual_key="VIRTUAL_KEY", + api_key="PORTKEY_API_KEY" + ) +) + +# Cancel a fine-tuning job +cancelled_job = openai.fine_tuning.jobs.cancel( + fine_tuning_job_id="job_id" # The ID of the fine-tuning job to cancel +) + +print(cancelled_job) +``` + + +```typescript +import OpenAI from 'openai'; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'; + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + virtualKey: "VIRTUAL_KEY", + apiKey: "PORTKEY_API_KEY" + }) +}); + +(async () => { + // Cancel a fine-tuning job + const cancelledJob = await openai.fineTuning.jobs.cancel( + "job_id" // The ID of the fine-tuning job to cancel + ); + + console.log(cancelledJob); +})(); +``` + + +```sh +curl \ +--request POST \ +--header 'Content-Type: application/json' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' \ +'https://api.portkey.ai/v1/fine_tuning/jobs//cancel' +``` + + + + +## References + +- Fine-tune Support types for models: [Link](https://docs.aws.amazon.com/bedrock/latest/userguide/model-customization-prepare.html#model-customization-data-support) +- Fine-tuning Documentation: [Link](https://docs.aws.amazon.com/bedrock/latest/userguide/custom-models.html) diff --git a/virtual_key_old/integrations/llms/bedrock/prompt-caching.mdx b/virtual_key_old/integrations/llms/bedrock/prompt-caching.mdx new file mode 100644 index 00000000..f67a0a3f --- /dev/null +++ b/virtual_key_old/integrations/llms/bedrock/prompt-caching.mdx @@ -0,0 +1,213 @@ +--- +title: 'Prompt Caching on Bedrock' +--- + +Prompt caching on Amazon Bedrock lets you cache specific portions of your requests for repeated use. This feature significantly reduces inference response latency and input token costs by allowing the model to skip recomputation of previously processed content. + +With Portkey, you can easily implement Amazon Bedrock's prompt caching through our OpenAI-compliant unified API and prompt templates. + +## Model Support + +Amazon Bedrock prompt caching is generally available with the following models: + + +**Currently Supported Models:** +- Claude 3.7 Sonnet +- Claude 3.5 Haiku +- Amazon Nova Micro +- Amazon Nova Lite +- Amazon Nova Pro + +Customers who were given access to Claude 3.5 Sonnet v2 during the prompt caching preview will retain their access, but no additional customers will be granted access to prompt caching on the Claude 3.5 Sonnet v2 model. + + +## How Bedrock Prompt Caching Works + +When using prompt caching, you define **cache checkpoints** - markers that indicate parts of your prompt to cache. These cached sections must be static between requests; any alterations will result in a cache miss. + + + You can also use Bedrock Prompt Caching Feature with Portkey's Prompt Templates. + + + + +## Implementation Examples + +Here's how to implement prompt caching with Portkey: + + + +```javascript NodeJS +import Portkey from 'portkey-ai' + +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your Bedrock Virtual Key +}) + +const chatCompletion = await portkey.chat.completions.create({ + messages: [ + { "role": 'system', "content": [ + { + "type":"text","text":"You are a helpful assistant" + }, + { + "type":"text","text":"This is a large document I want to cache...", + "cache_control": {"type": "ephemeral"} + } + ]}, + { "role": 'user', "content": 'Summarize the above document for me in 20 words' } + ], + model: 'anthropic.claude-3-7-sonnet-20250219-v1:0' +}); + +console.log(chatCompletion.choices[0].message.content); +``` + +```python Python +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY", + virtual_key="BEDROCK_VIRTUAL_KEY", +) + +chat_completion = portkey.chat.completions.create( + messages= [ + { "role": 'system', "content": [ + { + "type":"text","text":"You are a helpful assistant" + }, + { + "type":"text","text":"This is a large document I want to cache...", + "cache_control": {"type": "ephemeral"} + } + ]}, + { "role": 'user', "content": 'Summarize the above document in 20 words' } + ], + model= 'anthropic.claude-3-7-sonnet-20250219-v1:0', +) + +print(chat_completion.choices[0].message.content) +``` + +```javascript OpenAI NodeJS +import OpenAI from "openai"; +import { PORTKEY_GATEWAY_URL, createHeaders } from "portkey-ai"; + +const openai = new OpenAI({ + apiKey: "BEDROCK_API_KEY", + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "bedrock", + apiKey: "PORTKEY_API_KEY", + }), +}); + +const chatCompletion = await openai.chat.completions.create({ + messages: [ + { "role": 'system', "content": [ + { + "type":"text","text":"You are a helpful assistant" + }, + { + "type":"text","text":"This is a large document I want to cache...", + "cache_control": {"type": "ephemeral"} + } + ]}, + { "role": 'user', "content": 'Summarize the above document for me in 20 words' } + ], + model: 'anthropic.claude-3-7-sonnet-20250219-v1:0', +}); + +console.log(chatCompletion.choices[0].message.content); +``` + +```python OpenAI Python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +client = OpenAI( + api_key="BEDROCK_API_KEY", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key="PORTKEY_API_KEY", + provider="bedrock", + ) +) + +chat_completion = client.chat.completions.create( + messages= [ + { "role": 'system', "content": [ + { + "type":"text","text":"You are a helpful assistant" + }, + { + "type":"text","text":"This is a large document I want to cache...", + "cache_control": {"type": "ephemeral"} + } + ]}, + { "role": 'user', "content": 'Summarize the above document in 20 words' } + ], + model= 'anthropic.claude-3-7-sonnet-20250219-v1:0', +) + +print(chat_completion.choices[0].message.content) +``` + +```sh REST API +curl https://api.portkey.ai/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "x-portkey-virtual-key: $BEDROCK_VIRTUAL_KEY" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -d '{ + "model": "anthropic.claude-3-7-sonnet-20250219-v1:0", + "messages": [ + { "role": "system", "content": [ + { + "type":"text","text":"You are a helpful assistant" + }, + { + "type":"text","text":"This is a large document I want to cache...", + "cache_control": {"type": "ephemeral"} + } + ]}, + { "role": "user", "content": "Summarize the above document for me in 20 words" } + ] + }' +``` + + + +## Supported Features and Limitations + + +**Supported Features** +- Text prompts and images embedded within text prompts +- Multiple cache checkpoints per request +- Caching in system prompts, messages, and tools fields (model-dependent) + + +### Supported Models and Limits + +Below is a detailed table of supported models, their minimum token requirements, maximum cache checkpoints, and fields that support caching: + +| Model | Model ID | Min tokens per checkpoint | Max checkpoints per request | Cacheable fields | +|-------|----------|---------------------------|--------------------------|-----------------| +| Claude 3.7 Sonnet | anthropic.claude-3-7-sonnet-20250219-v1:0 | 1,024 | 4 | system, messages, tools | +| Claude 3.5 Haiku | anthropic.claude-3-5-haiku-20241022-v1:0 | 2,048 | 4 | system, messages, tools | +| Amazon Nova Micro | amazon.nova-micro-v1:0 | 1,000 | 4 | system, messages | +| Amazon Nova Lite | amazon.nova-lite-v1:0 | 1,000 | 4 | system, messages | +| Amazon Nova Pro | amazon.nova-pro-v1:0 | 1,000 | 4 | system, messages | + + +- The Amazon Nova models support a maximum of 32k tokens for prompt caching. +- For Claude models, tools caching is fully supported. +- Tools caching is not supported for Amazon Nova models. + + + +## Related Resources + +For more detailed information on Bedrock prompt caching, refer to: + diff --git a/virtual_key_old/integrations/llms/byollm.mdx b/virtual_key_old/integrations/llms/byollm.mdx new file mode 100644 index 00000000..095efb4b --- /dev/null +++ b/virtual_key_old/integrations/llms/byollm.mdx @@ -0,0 +1,346 @@ +--- +title: "Bring Your Own LLM" +description: "Integrate your privately hosted LLMs with Portkey for unified management, observability, and reliability." +--- + +Portkey's Bring Your Own LLM feature allows you to seamlessly integrate privately hosted language models into your AI infrastructure. This powerful capability enables unified management of both private and commercial LLMs through a consistent interface while leveraging Portkey's comprehensive suite of observability and reliability features. + +## Key Benefits + +- **Unified API Access**: Manage private and commercial LLMs through a single, consistent interface +- **Enhanced Reliability**: Leverage Portkey's fallbacks, retries, and load balancing for your private deployments +- **Comprehensive Monitoring**: Track performance, usage, and costs alongside your commercial LLM usage +- **Simplified Access Control**: Manage team-specific permissions and usage limits +- **Secure Credential Management**: Protect sensitive authentication details through Portkey's secure vault + +## Integration Options + + +**Prerequisites** + +Your private LLM must implement an API specification compatible with one of Portkey's [supported providers](/integrations/llms) (e.g., OpenAI's `/chat/completions`, Anthropic's `/messages`, etc.). + + +Portkey offers two primary methods to integrate your private LLMs: + +1. **Using Virtual Keys**: Store your deployment details securely in Portkey's vault +2. **Direct Integration**: Pass deployment details in your requests without storing them + +### Option 1: Using Virtual Keys + +#### Step 1: Add Your Deployment Details + +Navigate to the [Virtual Keys](https://app.portkey.ai/virtual-keys) section in your Portkey dashboard and create a new Virtual Key. + + + Virtual Key setup for private LLM + + +1. Click **"Add Key"** and enable the **"Local/Privately hosted provider"** toggle +2. Configure your deployment: + - Select the matching provider API specification (typically `OpenAI`) + - Enter your model's base URL in the `Custom Host` field + - Add required authentication headers and their values +3. Click **"Create"** to generate your virtual key + +#### Step 2: Use Your Virtual Key in Requests + +After creating your virtual key, you can use it in your applications: + + +```js NodeJS +import Portkey from 'portkey-ai' + +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "YOUR_PRIVATE_LLM_VIRTUAL_KEY" +}) + +async function main() { + const response = await portkey.chat.completions.create({ + messages: [{ role: "user", content: "Explain quantum computing in simple terms" }], + model: "YOUR_MODEL_NAME", // The model name your private deployment expects + }); + + console.log(response.choices[0].message.content); +} + +main(); +``` + +```py Python +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY", + virtual_key="YOUR_PRIVATE_LLM_VIRTUAL_KEY" +) + +response = portkey.chat.completions.create( + model="YOUR_MODEL_NAME", # The model name your private deployment expects + messages=[ + {"role": "user", "content": "Explain quantum computing in simple terms"} + ] +) + +print(response.choices[0].message.content) +``` + +```sh cURL +curl https://api.portkey.ai/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-virtual-key: $YOUR_PRIVATE_LLM_VIRTUAL_KEY" \ + -d '{ + "model": "YOUR_MODEL_NAME", + "messages": [ + { "role": "user", "content": "Explain quantum computing in simple terms" } + ] + }' +``` + + +### Option 2: Direct Integration Without Virtual Keys + +If you prefer not to store your private LLM details in Portkey's vault, you can pass them directly in your API requests: + + +```js NodeJS +import Portkey from 'portkey-ai' + +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + provider: "openai", // The API spec your LLM implements + customHost: "https://your-llm-server.com/v1/", // Include the API version + Authorization: "Bearer YOUR_AUTH_TOKEN", // Optional: Any auth headers needed + forwardHeaders: ["Authorization"] // Headers to forward without processing +}) + +async function main() { + const response = await portkey.chat.completions.create({ + messages: [{ role: "user", content: "Explain quantum computing in simple terms" }], + model: "YOUR_MODEL_NAME", + }); + + console.log(response.choices[0].message.content); +} + +main(); +``` + +```py Python +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY", + provider="openai", # The API spec your LLM implements + custom_host="https://your-llm-server.com/v1/", # Include the API version + Authorization="Bearer YOUR_AUTH_TOKEN", # Optional: Any auth headers needed + forward_headers=["Authorization"] # Headers to forward without processing +) + +response = portkey.chat.completions.create( + model="YOUR_MODEL_NAME", + messages=[{"role": "user", "content": "Explain quantum computing in simple terms"}] +) + +print(response.choices[0].message.content) +``` + +```sh cURL +curl https://api.portkey.ai/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-provider: openai" \ + -H "x-portkey-custom-host: https://your-llm-server.com/v1" \ + -H "Authorization: Bearer YOUR_AUTH_TOKEN" \ + -H "x-portkey-forward-headers: Authorization" \ + -d '{ + "model": "YOUR_MODEL_NAME", + "messages": [ + { "role": "user", "content": "Explain quantum computing in simple terms" } + ] + }' +``` + + + +The `custom_host` must include the API version path (e.g., `/v1/`). Portkey will automatically append the endpoint path (`/chat/completions`, `/completions`, or `/embeddings`). + + +## Securely Forwarding Sensitive Headers + +For headers containing sensitive information that shouldn't be logged or processed by Portkey, use the `forward_headers` parameter to pass them directly to your private LLM: + + + +```js +import Portkey from 'portkey-ai' + +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + provider: "openai", + customHost: "https://your-llm-server.com/v1/", + // Headers to include with requests + Authorization: "Bearer sk_live_xxxxx", + xApiKey: "sensitive-key-value", + xOrgId: "org-12345", + // Headers to forward without processing + forwardHeaders: ["Authorization", "xApiKey", "xOrgId"] +}) +``` + +In the JavaScript SDK, convert header names to **camelCase**. For example, `X-My-Custom-Header` becomes `xMyCustomHeader`. + + + +```python +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY", + provider="openai", + custom_host="https://your-llm-server.com/v1/", + # Headers to include with requests + Authorization="Bearer sk_live_xxxxx", + x_api_key="sensitive-key-value", + x_org_id="org-12345", + # Headers to forward without processing + forward_headers=["Authorization", "x_api_key", "x_org_id"] +) +``` + +In the Python SDK, convert header names to **snake_case**. For example, `X-My-Custom-Header` becomes `x_my_custom_header`. + + + +```sh +curl https://api.portkey.ai/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-provider: openai" \ + -H "x-portkey-custom-host: https://your-llm-server.com/v1" \ + -H "Authorization: Bearer sk_live_xxxxx" \ + -H "x-api-key: sensitive-key-value" \ + -H "x-org-id: org-12345" \ + -H "x-portkey-forward-headers: Authorization, x-api-key, x-org-id" \ + -d '{ + "model": "YOUR_MODEL_NAME", + "messages": [{ "role": "user", "content": "Hello!" }] + }' +``` + + + +### Using Forward Headers in Gateway Configs + +You can also specify `forward_headers` in your Gateway Config for consistent header forwarding: + +```json +{ + "strategy": { "mode": "fallback" }, + "targets": [ + { + "provider": "openai", + "custom_host": "https://your-private-llm.com/v1", + "forward_headers": ["Authorization", "x-api-key", "x-custom-token"] + }, + { + "provider": "openai", + "api_key": "sk-xxxxx" // Fallback to commercial provider + } + ] +} +``` + +## Advanced Features + +### Using Private LLMs with Gateway Configs + +Private LLMs work seamlessly with all Portkey Gateway features. Some common use cases: + +- **Load Balancing**: Distribute traffic across multiple private LLM instances +- **Fallbacks**: Set up automatic failover between private and commercial LLMs +- **Conditional Routing**: Route requests to different LLMs based on metadata + +```json +{ + "strategy": { + "mode": "fallback" + }, + "targets": [ + { + "provider": "openai", + "custom_host": "http://PRIVATE_LLM/v1", + "forward_headers": ["Authorization"] + }, + { + "virtual_key": "openai-key" + } + ] +} +``` + +Learn more about [Gateway Configs](/product/ai-gateway/configs). + + +## Monitoring and Analytics + +Portkey provides comprehensive observability for your private LLM deployments, just like it does for commercial providers: + +- **Log Analysis**: View detailed request and response logs +- **Performance Metrics**: Track latency, token usage, and error rates +- **User Attribution**: Associate requests with specific users via metadata + + + Analytics dashboard showing private LLM usage + + + +## Troubleshooting + +| Issue | Possible Causes | Solutions | +|-------|-----------------|-----------| +| Connection Errors | Incorrect URL, network issues, firewall rules | Verify URL format, check network connectivity, confirm firewall allows traffic | +| Authentication Failures | Invalid credentials, incorrect header format | Check credentials, ensure headers are correctly formatted and forwarded | +| Timeout Errors | LLM server overloaded, request too complex | Adjust timeout settings, implement load balancing, simplify requests | +| Inconsistent Responses | Different model versions, configuration differences | Standardize model versions, document expected behavior differences | + +## FAQs + + + + Yes, as long as it implements an API specification compatible with one of Portkey's supported providers (OpenAI, Anthropic, etc.). The model should accept requests and return responses in the format expected by that provider. + + + + You have two options: + 1. Create separate virtual keys for each endpoint + 2. Use Gateway Configs with load balancing to distribute traffic across multiple endpoints + + + + Portkey itself doesn't impose specific request volume limitations for private LLMs. Your throughput will be limited only by your private LLM deployment's capabilities and any rate limits you configure in Portkey. + + + + Yes, you can specify different model names in your requests as long as your private LLM deployment supports them. The model name is passed through to your deployment. + + + + Absolutely! One of Portkey's key benefits is the ability to manage both private and commercial LLMs through a unified interface. You can even set up fallbacks between them or route requests conditionally. + + + +## Next Steps + +Explore these related resources to get the most out of your private LLM integration: + + + + + + + + + diff --git a/virtual_key_old/integrations/llms/cerebras.mdx b/virtual_key_old/integrations/llms/cerebras.mdx new file mode 100644 index 00000000..73092951 --- /dev/null +++ b/virtual_key_old/integrations/llms/cerebras.mdx @@ -0,0 +1,107 @@ +--- +title: "Cerebras" +--- + +Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including the models hosted on [Cerebras Inference API](https://cerebras.ai/inference). + + +Provider Slug: `cerebras` + + +## Portkey SDK Integration with Cerebras + +Portkey provides a consistent API to interact with models from various providers. To integrate Cerebras with Portkey: + +### 1\. Install the Portkey SDK + + + ```sh + npm install --save portkey-ai + ``` + + + ```sh + pip install portkey-ai + ``` + + + + + + +### 2\. Initialize Portkey with Cerebras + +To use Cerebras with Portkey, get your API key from [here](https://cerebras.ai/inference), then add it to Portkey to create the virtual key. + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "CEREBRAS_VIRTUAL_KEY" // Your Cerebras Inference virtual key + }) + ``` + + + ```py + from portkey_ai import Portkey + + portkey = Portkey( + api_key ="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="CEREBRAS_VIRTUAL_KEY" # Your Cerebras Inference virtual key + ) + ``` + + + + + + + +### 3\. Invoke Chat Completions + + + ```js + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'llama3.1-8b', + }); + + console.log(chatCompletion.choices); + ``` + + + ```python + completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model= 'llama3.1-8b' + ) + + print(completion) + ``` + + + + +--- + +## Supported Models + +Cerebras currently supports `Llama-3.1-8B` and `Llama-3.1-70B`. You can find more info here: + +[![Logo](/images/llms/apple-touch-icon.png)Overview - Starter KitStarter Kit](https://inference-docs.cerebras.ai/introduction) + +## Next Steps + +The complete list of features supported in the SDK are available on the link below. + + + + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your Cerebras](/product/ai-gateway/configs)[ requests](/product/ai-gateway/configs) +3. [Tracing Cerebras requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to Cerebras](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/cohere.mdx b/virtual_key_old/integrations/llms/cohere.mdx new file mode 100644 index 00000000..1c7d0c7e --- /dev/null +++ b/virtual_key_old/integrations/llms/cohere.mdx @@ -0,0 +1,165 @@ +--- +title: "Cohere" +--- + +Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including Cohere's generation, embedding, and other endpoints. + +With Portkey, you can take advantage of features like fast AI gateway access, observability, prompt management, and more, all while ensuring the secure management of your LLM API keys through a [virtual key](/product/ai-gateway/virtual-keys) system. + + +Provider Slug. `cohere` + +## Portkey SDK Integration with Cohere + +Portkey provides a consistent API to interact with models from Cohere. To integrate Cohere with Portkey: + +### 1\. Install the Portkey SDK + +Add the Portkey SDK to your application to interact with Cohere's models through Portkey's gateway. + + + ```sh + npm install --save portkey-ai + ``` + + + ```sh + pip install portkey-ai + ``` + + + + + + +### 2\. Initialize Portkey with the Virtual Key + +To use Cohere with Portkey, [get your API key from here](https://dashboard.cohere.com/api-keys), then add it to Portkey to create the virtual key. + + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your Cohere Virtual Key + }) + ``` + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Cohere + ) + ``` + + + + +### **3\. Invoke Chat Completions with Cohere** + +Use the Portkey instance to send requests to Cohere's models. You can also override the virtual key directly in the API call if needed. + + + + ```js + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'command', + }); + + console.log(chatCompletion.choices); + ``` + + + ``` + chat_completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model= 'command' + ) + ``` + + + + +## Managing Cohere Prompts + +You can manage all prompts to Cohere in the [Prompt Library](/product/prompt-library). All the current models of Cohere are supported and you can easily start testing different prompts. + +Once you're ready with your prompt, you can use the `portkey.prompts.completions.create` interface to use the prompt in your application. + +## Other Cohere Endpoints + +### Embeddings + +Embedding endpoints are natively supported within Portkey like this: + +```js +const embedding = await portkey.embeddings.create({ + input: 'Name the tallest buildings in Hawaii' +}); + +console.log(embedding); +``` + +### Re-ranking + +You can use cohere reranking the `portkey.post` method with the body expected by [Cohere's reranking API](https://docs.cohere.com/reference/rerank-1). + + + + ```js + const response = await portkey.post( + "/rerank", + { + "return_documents": false, + "max_chunks_per_doc": 10, + "model": "rerank-english-v2.0", + "query": "What is the capital of the United States?", + "documents": [ + "Carson City is the capital city of the American state of Nevada.", + "The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean. Its capital is Saipan.", + "Washington, D.C. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district.", + "Capital punishment (the death penalty) has existed in the United States since beforethe United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states." + ] + } + ) + ``` + + + ```python + response = portkey.post( + "/rerank", + return_documents=False, + max_chunks_per_doc=10, + model="rerank-english-v2.0", + query="What is the capital of the United States?", + documents=[ + "Carson City is the capital city of the American state of Nevada.", + "The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean. Its capital is Saipan.", + "Washington, D.C. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district.", + "Capital punishment (the death penalty) has existed in the United States since beforethe United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states." + ] + ) + ``` + + + + + +## Next Steps + +The complete list of features supported in the SDK are available on the link below. + + + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your Cohere requests](/product/ai-gateway/configs) +3. [Tracing Cohere requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to Cohere APIs](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/dashscope.mdx b/virtual_key_old/integrations/llms/dashscope.mdx new file mode 100644 index 00000000..6d78f570 --- /dev/null +++ b/virtual_key_old/integrations/llms/dashscope.mdx @@ -0,0 +1,351 @@ +--- +title: 'Dashscope' +description: 'Integrate Dashscope with Portkey for seamless completions, prompt management, and advanced features like streaming, function calling, and fine-tuning.' +--- + + +**Portkey Provider Slug:** `dashscope` + + +## Overview + +Portkey offers native integrations with [dashscope](https://dashscope.aliyun.com/) for Node.js, Python, and REST APIs. By combining Portkey with Dashscope, you can create production-grade AI applications with enhanced reliability, observability, and advanced features. + + + + Explore the official Dashscope documentation for comprehensive details on their APIs and models. + + + +## Getting Started + + + + Visit the [Dashscope dashboard](https://help.aliyun.com/zh/model-studio/developer-reference/get-api-key) to generate your API key. + + + + Portkey's virtual key vault simplifies your interaction with Dashscope. Virtual keys act as secure aliases for your actual API keys, offering enhanced security and easier management through [budget limits](/product/ai-gateway/usage-limits) to control your API usage. + + Use the Portkey app to create a [virtual key](/product/ai-gateway/virtual-keys) associated with your Dashscope API key. + + + + Now that you have your virtual key, set up the Portkey client: + + ### Portkey Hosted App + Use the Portkey API key and the Dashscope virtual key to initialize the client in your preferred programming language. + + + ```python Python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Dashscope + ) + ``` + + ```javascript Node.js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your Dashscope Virtual Key + }) + ``` + + + ### Open Source Use + Alternatively, use Portkey's Open Source AI Gateway to enhance your app's reliability with minimal code: + + + ```python Python + from portkey_ai import Portkey, PORTKEY_GATEWAY_URL + + portkey = Portkey( + api_key="dummy", # Replace with your Portkey API key + base_url=PORTKEY_GATEWAY_URL, + Authorization="DASHSCOPE_API_KEY", # Replace with your Dashscope API Key + provider="dashscope" + ) + ``` + + ```javascript Node.js + import Portkey, { PORTKEY_GATEWAY_URL } from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "dummy", // Replace with your Portkey API key + baseUrl: PORTKEY_GATEWAY_URL, + Authorization: "DASHSCOPE_API_KEY", // Replace with your Dashscope API Key + provider: "dashscope" + }) + ``` + + + + +๐Ÿ”ฅ That's it! You've integrated Portkey into your application with just a few lines of code. Now let's explore making requests using the Portkey client. + +## Supported Models + + + +`Chat` - qwen-long, qwen-max, qwen-max-0428, qwen-max-0403, qwen-max-0107, qwen-plus, qwen-plus-0806, qwen-plus-0723, qwen-plus-0624, qwen-plus-0206, qwen-turbo, qwen-turbo-0624, qwen-turbo-0206, qwen2-57b-a14b-instruct, qwen2-72b-instruct, qwen2-7b-instruct, qwen2-1.5b-instruct, qwen2-0.5b-instruct, qwen1.5-110b-chat, qwen1.5-72b-chat, qwen1.5-32b-chat, qwen1.5-14b-chat, qwen1.5-7b-chat, qwen1.5-1.8b-chat, qwen1.5-0.5b-chat, codeqwen1.5-7b-chat, qwen-72b-chat, qwen-14b-chat, qwen-7b-chat, qwen-1.8b-longcontext-chat, qwen-1.8b-chat, qwen2-math-72b-instruct, qwen2-math-7b-instruct, qwen2-math-1.5b-instruct + + +`Embedding`- text-embedding-v1, text-embedding-v2, text-embedding-v3 + + + + +## Supported Endpoints and Parameters + +| Endpoint | Supported Parameters | +| ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `chatComplete` | messages, max_tokens, temperature, top_p, stream, presence_penalty, frequency_penalty | +| `embed` | model, input, encoding_format, dimensions, user | + + + +## Dashscope Supported Features + +### Chat Completions + +Generate chat completions using Dashscope models through Portkey: + + +```python Python +completion = portkey.chat.completions.create( + messages=[{"role": "user", "content": "Say this is a test"}], + model="qwen-turbo" +) + +print(completion.choices[0].message.content) +``` + +```javascript Node.js +const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'qwen-turbo', +}); + +console.log(chatCompletion.choices[0].message.content); +``` + +```curl REST +curl -X POST "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -d '{ + "messages": [{"role": "user", "content": "Say this is a test"}], + "model": "qwen-turbo" + }' +``` + + +### Embeddings + +Generate embeddings for text using Dashscope embedding models: + + +```python Python +response = portkey.embeddings.create( + input="Your text string goes here", + model="text-embedding-v1" +) + +print(response.data[0].embedding) +``` + +```javascript Node.js +const response = await portkey.embeddings.create({ + input: "Your text string goes here", + model: "text-embedding-v1" +}); + +console.log(response.data[0].embedding); +``` + +```curl REST +curl -X POST "https://api.portkey.ai/v1/embeddings" \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -d '{ + "input": "Your text string goes here", + "model": "text-embedding-v1" + }' +``` + + + + + + +# Portkey's Advanced Features + +## Track End-User IDs + +Portkey allows you to track user IDs passed with the user parameter in Dashscope requests, enabling you to monitor user-level costs, requests, and more: + + +```python Python +response = portkey.chat.completions.create( + model="qwen-turbo", + messages=[{"role": "user", "content": "Say this is a test"}], + user="user_123456" +) +``` + +```javascript Node.js +const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: "user", content: "Say this is a test" }], + model: "qwen-turbo", + user: "user_12345", +}); +``` + +```curl REST +curl -X POST "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -d '{ + "model": "qwen-turbo", + "messages": [{"role": "user", "content": "Say this is a test"}], + "user": "user_123456" + }' +``` + + +When you include the user parameter in your requests, Portkey logs will display the associated user ID, as shown in the image below: + +Portkey Logs with User ID + +In addition to the `user` parameter, Portkey allows you to send arbitrary custom metadata with your requests. This powerful feature enables you to associate additional context or information with each request, which can be useful for analysis, debugging, or other custom use cases. + + + + Explore how to use custom metadata to enhance your request tracking and analysis. + + + +## Using The Gateway Config + +Here's a simplified version of how to use Portkey's Gateway Configuration: + + + + You can create a Gateway configuration using the Portkey Config Dashboard or by writing a JSON configuration in your code. In this example, requests are routed based on the user's subscription plan (paid or free). + + ```json + config = { + "strategy": { + "mode": "conditional", + "conditions": [ + { + "query": { "metadata.user_plan": { "$eq": "paid" } }, + "then": "qwen-turbo" + }, + { + "query": { "metadata.user_plan": { "$eq": "free" } }, + "then": "gpt-3.5" + } + ], + "default": "base-gpt4" + }, + "targets": [ + { + "name": "qwen-turbo", + "virtual_key": "xx" + }, + { + "name": "gpt-3.5", + "virtual_key": "yy" + } + ] + } + ``` + + + + When a user makes a request, it will pass through Portkey's AI Gateway. Based on the configuration, the Gateway routes the request according to the user's metadata. + Conditional Routing Diagram + + + + Pass the Gateway configuration to your Portkey client. You can either use the config object or the Config ID from Portkey's hosted version. + + + ```python Python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", + virtual_key="VIRTUAL_KEY", + config=portkey_config + ) + ``` + + ```javascript Node.js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "VIRTUAL_KEY", + config: portkeyConfig + }) + ``` + + + + +That's it! Portkey seamlessly allows you to make your AI app more robust using built-in gateway features. Learn more about advanced gateway features: + + + + Distribute requests across multiple targets based on defined weights. + + + Automatically switch to backup targets if the primary target fails. + + + Route requests to different targets based on specified conditions. + + + Enable caching of responses to improve performance and reduce costs. + + + +## Guardrails + +Portkey's AI gateway enables you to enforce input/output checks on requests by applying custom hooks before and after processing. Protect your user's/company's data by using PII guardrails and many more available on Portkey Guardrails: + +```json +{ + "virtual_key":"dashscope-xxx", + "before_request_hooks": [{ + "id": "input-guardrail-id-xx" + }], + "after_request_hooks": [{ + "id": "output-guardrail-id-xx" + }] +} +``` + + + Explore Portkey's guardrail features to enhance the security and reliability of your AI applications. + + +## Next Steps + +The complete list of features supported in the SDK are available in our comprehensive documentation: + + + Explore the full capabilities of the Portkey SDK and how to leverage them in your projects. + + +--- + + + +For the most up-to-date information on supported features and endpoints, please refer to our [API Reference](/docs/api-reference/introduction). diff --git a/virtual_key_old/integrations/llms/deepbricks.mdx b/virtual_key_old/integrations/llms/deepbricks.mdx new file mode 100644 index 00000000..19918067 --- /dev/null +++ b/virtual_key_old/integrations/llms/deepbricks.mdx @@ -0,0 +1,103 @@ +--- +title: "Deepbricks" +description: "Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including [Deepbricks](https://deepbricks.ai/). +" +--- + + +With Portkey, you can take advantage of features like fast AI gateway access, observability, prompt management, and more, all while ensuring the secure management of your LLM API keys through a [virtual key](/product/ai-gateway/virtual-keys) system. + + +Provider Slug: `deepbricks` + + +## Portkey SDK Integration with Deepbricks Models + +Portkey provides a consistent API to interact with models from various providers. To integrate Deepbricks with Portkey: + +### 1. Install the Portkey SDK + +Add the Portkey SDK to your application to interact with Deepbricks API through Portkey's gateway. + + + + ```sh + npm install --save portkey-ai + ``` + + + ```sh + pip install portkey-ai + ``` + + + +### 2. Initialize Portkey with the Virtual Key + +To use Deepbricks with Portkey, [get your API key from here](https://deepbricks.ai/pricing), then add it to Portkey to create the virtual key. + + + + ```javascript + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your Deepbricks + }) + ``` + + + ```python + from portkey_ai import Portkey + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Deepbricks + ) + ``` + + + +### 3. Invoke Chat Completions with Deepbricks + +Use the Portkey instance to send requests to Deepbricks. You can also override the virtual key directly in the API call if needed. + + + + ```javascript + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'deepseek-ai/DeepSeek-V2-Chat', + }); + console.log(chatCompletion.choices); + ``` + + + ```python + completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model= 'deepseek-ai/DeepSeek-V2-Chat' + ) + print(completion) + ``` + + + +## Managing Deepbricks Prompts + +You can manage all prompts to Deepbricks in the [Prompt Library](/product/prompt-library). All the current models of Deepbricks are supported and you can easily start testing different prompts. + +Once you're ready with your prompt, you can use the `portkey.prompts.completions.create` interface to use the prompt in your application. + +The complete list of features supported in the SDK are available on the link below. + + + Explore the Portkey SDK Client documentation + + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your Deepbricks requests](/product/ai-gateway/configs) +3. [Tracing Deepbricks requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to Deepbricks APIs](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/deepgram.mdx b/virtual_key_old/integrations/llms/deepgram.mdx new file mode 100644 index 00000000..ffa2d9b7 --- /dev/null +++ b/virtual_key_old/integrations/llms/deepgram.mdx @@ -0,0 +1,40 @@ +--- +title: "Deepgram" +description: Portkey provides a robust and secure gateway to use and observe Deepgrm's Speech-to-Text API. +--- + +Deepgram API is currently supported on Portkey's REST API, with support for Python & Node SDKs coming soon. + +## Speech to Text API + +* We set the target Deepgram API URL with the `x-portkey-custom-host` header +* We set the target provider as `openai` to let Portkey know that this request should be handled similarly to OpenAI + + + +```sh +curl 'https://api.portkey.ai/v1/listen' \ + -H 'Authorization: Token $DEEPGRAM_API_KEY' \ + -H 'Content-Type: audio/mp3' \ + -H 'x-portkey-custom-host: https://api.deepgram.com/v1' \ + -H 'x-portkey-provider: openai' \ + -H 'x-portkey-api-key: $PORTKEY_API_KEY' \ + --data-binary '@audio.mp3' +``` + + +```sh +curl 'https://api.portkey.ai/v1/listen' \ + -H 'Authorization: Token $DEEPGRAM_API_KEY' \ + -H 'Accept: application/json' \ + -H 'Content-Type: application/json' \ + -H 'x-portkey-custom-host: https://api.deepgram.com/v1' \ + -H 'x-portkey-provider: openai' \ + -H 'x-portkey-api-key: $PORTKEY_API_KEY' \ + -d '{"url": "https://dpgr.am/spacewalk.wav"}' +``` + + + + + diff --git a/virtual_key_old/integrations/llms/deepinfra.mdx b/virtual_key_old/integrations/llms/deepinfra.mdx new file mode 100644 index 00000000..f0f286dc --- /dev/null +++ b/virtual_key_old/integrations/llms/deepinfra.mdx @@ -0,0 +1,112 @@ +--- +title: "Deepinfra" +--- + +Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including the models hosted on [Deepinfra API](https://deepinfra.com/models/text-generation). + + +Provider Slug. `deepinfra` + + +## Portkey SDK Integration with Deepinfra Models + +Portkey provides a consistent API to interact with models from various providers. To integrate Deepinfra with Portkey: + +### 1\. Install the Portkey SDK + +Add the Portkey SDK to your application to interact with Mistral AI's API through Portkey's gateway. + + + ```sh + npm install --save portkey-ai + ``` + + + ```sh + pip install portkey-ai + ``` + + + + + + +### 2\. Initialize Portkey with the Virtual Key + +To use Deepinfra with Virtual Key, [get your API key from here](https://deepinfra.com/dash/api%5Fkeys). Then add it to Portkey to create the virtual key + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your Deepinfra Virtual Key + }) + ``` + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="DEEPINFRA_VIRTUAL_KEY" + ) + ``` + + + + + + + +### 3\. Invoke Chat Completions + + + ```js + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'nvidia/Nemotron-4-340B-Instruct', + }); + + console.log(chatCompletion.choices); + ``` + + + ```python + completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model= 'nvidia/Nemotron-4-340B-Instruct' + ) + + print(completion) + ``` + + + + + + + +--- + +## Supported Models + +Here's the list of all the Deepinfra models you can route to using Portkey - + + +[![Logo](/images/llms/deepinfra-logo-64.webp)Models | Machine Learning Inference | Deep InfraDeepInfra](https://deepinfra.com/models/text-generation) + +## Next Steps + +The complete list of features supported in the SDK are available on the link below. + + + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your Deepinfra](/product/ai-gateway/configs)[ requests](/product/ai-gateway/configs) +3. [Tracing Deepinfra requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to Deepinfra](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/deepseek.mdx b/virtual_key_old/integrations/llms/deepseek.mdx new file mode 100644 index 00000000..4d162b9f --- /dev/null +++ b/virtual_key_old/integrations/llms/deepseek.mdx @@ -0,0 +1,268 @@ +--- +title: "DeepSeek" +description: "Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including DeepSeek models. +" +--- + + +With Portkey, you can take advantage of features like fast AI gateway access, observability, prompt management, and more, all while ensuring the secure management of your LLM API keys through a [virtual key](/product/ai-gateway/virtual-keys) system. + + +Provider Slug: **deepseek** + + +## Portkey SDK Integration with DeepSeek Models + +Portkey provides a consistent API to interact with models from various providers. To integrate DeepSeek with Portkey: + +### 1. Install the Portkey SDK + +Add the Portkey SDK to your application to interact with DeepSeek AI's API through Portkey's gateway. + + + + ```sh + npm install --save portkey-ai + ``` + + + ```sh + pip install portkey-ai + ``` + + + +### 2. Initialize Portkey with the Virtual Key + +To use DeepSeek with Portkey, [get your API key from here](https://platform.deepseek.com/api_keys), then add it to Portkey to create the virtual key. + + + + ```javascript + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your DeepSeek Virtual Key + }) + ``` + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for DeepSeek + ) + ``` + + + +### 3. Invoke Chat Completions with DeepSeek + +Use the Portkey instance to send requests to DeepSeek. You can also override the virtual key directly in the API call if needed. + + + + ```javascript + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'deepseek-chat', + }); + + console.log(chatCompletion.choices); + ``` + + + ```python + completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model= 'deepseek-chat' + ) + + print(completion) + ``` + + + +### 4. Invoke Multi-round Conversation with DeepSeek + + + + ```javascript + const client = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your DeepSeek Virtual Key + }) + + // Function to send chat messages and get a response + async function sendChatMessages(messages) { + try { + const response = await axios.post(baseURL, { + model: 'deepseek-chat', + messages: messages + }, { headers: headers }); + return response.data; + } catch (error) { + console.error('Error during the API request:', error.response ? error.response.data : error.message); + return null; + } + } + + // Round 1 + (async () => { + let messages = [{ role: 'user', content: "What's the highest mountain in the world?" }]; + + let response = await sendChatMessages(messages); + if (response) { + messages.push(response.choices[0].message); + console.log(`Messages Round 1: ${JSON.stringify(messages, null, 2)}`); + } + + // Round 2 + messages.push({ role: 'user', content: 'What is the second?' }); + response = await sendChatMessages(messages); + if (response) { + messages.push(response.choices[0].message); + console.log(`Messages Round 2: ${JSON.stringify(messages, null, 2)}`); + } + })(); + ``` + + + ```python + client = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for DeepSeek + ) + + # Round 1 + messages = [{"role": "user", "content": "What's the highest mountain in the world?"}] + response = client.chat.completions.create( + model="deepseek-chat", + messages=messages + ) + + messages.append(response.choices[0].message) + print(f"Messages Round 1: {messages}") + + # Round 2 + messages.append({"role": "user", "content": "What is the second?"}) + response = client.chat.completions.create( + model="deepseek-chat", + messages=messages + ) + + messages.append(response.choices[0].message) + print(f"Messages Round 2: {messages}") + ``` + + + +### 5. JSON Output with DeepSeek + + + + ```javascript + const client = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your DeepSeek Virtual Key + }) + + const systemPrompt = ` + The user will provide some exam text. Please parse the "question" and "answer" and output them in JSON format. + + EXAMPLE INPUT: + Which is the highest mountain in the world? Mount Everest. + + EXAMPLE JSON OUTPUT: + { + "question": "Which is the highest mountain in the world?", + "answer": "Mount Everest" + } + `; + + const userPrompt = "Which is the longest river in the world? The Nile River."; + + const messages = [ + { role: "system", content: systemPrompt }, + { role: "user", content: userPrompt } + ]; + + client.chat.completions.create({ + model: "deepseek-chat", + messages: messages, + responseFormat: { + type: 'json_object' + } + }).then(response => { + console.log(JSON.parse(response.choices[0].message.content)); + }).catch(error => { + console.error('Error:', error); + }); + ``` + + + ```python + import json + + client = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for DeepSeek + ) + + system_prompt = """ + The user will provide some exam text. Please parse the "question" and "answer" and output them in JSON format. + + EXAMPLE INPUT: + Which is the highest mountain in the world? Mount Everest. + + EXAMPLE JSON OUTPUT: + { + "question": "Which is the highest mountain in the world?", + "answer": "Mount Everest" + } + """ + + user_prompt = "Which is the longest river in the world? The Nile River." + + messages = [{"role": "system", "content": system_prompt}, + {"role": "user", "content": user_prompt}] + + response = client.chat.completions.create( + model="deepseek-chat", + messages=messages, + response_format={ + 'type': 'json_object' + } + ) + + print(json.loads(response.choices[0].message.content)) + ``` + + + +## Managing DeepSeek Prompts + +You can manage all prompts to DeepSeek in the [Prompt Library](/product/prompt-library). All the current models of DeepSeek are supported and you can easily start testing different prompts. + +Once you're ready with your prompt, you can use the `portkey.prompts.completions.create` interface to use the prompt in your application. + +### Supported Endpoints + +1. `CHAT_COMPLETIONS` +2. `STREAM_CHAT_COMPLETIONS` + +The complete list of features supported in the SDK is available on the link below. + + + Learn more about the Portkey SDK Client + + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your DeepSeek requests](/product/ai-gateway/configs) +3. [Tracing DeepSeek requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to DeepSeek APIs](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/fireworks.mdx b/virtual_key_old/integrations/llms/fireworks.mdx new file mode 100644 index 00000000..c6b8d81f --- /dev/null +++ b/virtual_key_old/integrations/llms/fireworks.mdx @@ -0,0 +1,375 @@ +--- +title: "Fireworks" +--- + +Portkey provides a robust and secure gateway to facilitate the integration of various models into your apps, including [chat](/integrations/llms/fireworks#id-3.-invoke-chat-completions-with-fireworks), [vision](/integrations/llms/fireworks#using-vision-models), [image generation](/integrations/llms/fireworks#using-image-generation-models), and [embedding](/integrations/llms/fireworks#using-embeddings-models) models hosted on the [Fireworks platform](https://fireworks.ai/). + +With Portkey, you can take advantage of features like fast AI gateway access, observability, prompt management, and more, all while ensuring the secure management of your LLM API keys through a [virtual key](/product/ai-gateway/virtual-keys) system. + +Provider Slug. `fireworks-ai` + +## Portkey SDK Integration with Fireworks Models + +Portkey provides a consistent API to interact with models from various providers. To integrate Fireworks with Portkey: + +### 1\. Install the Portkey SDK + + + ```sh + npm install --save portkey-ai + ``` + + + + ```sh + pip install portkey-ai + ``` + + + + + + +### 2\. Initialize Portkey with the Virtual Key + +To use Fireworks with Portkey, [get your API key from here](https://fireworks.ai/api-keys), then add it to Portkey to create the virtual key. + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "FIREWORKS_VIRTUAL_KEY" // Your Virtual Key + }) + ``` + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Defaults to os.env("PORTKEY_API_KEY") + virtual_key="FIREWORKS_VIRTUAL_KEY" # Your Virtual Key + ) + ``` + + + + + + + +### **3\. Invoke Chat Completions with** Fireworks + +You can use the Portkey instance now to send requests to Fireworks API. + + + ```js + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'accounts/fireworks/models/llama-v3-70b-instruct', + }); + + console.log(chatCompletion.choices); + ``` + + + ```python + completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model= 'accounts/fireworks/models/llama-v3-70b-instruct' + ) + + print(completion) + ``` + + + + + +Now, let's explore how you can use Portkey to call other models (vision, embedding, image) on the Fireworks API: + +### Using Embeddings Models + +Call any [embedding model hosted on Fireworks](https://readme.fireworks.ai/docs/querying-embeddings-models#list-of-available-models) with the familiar OpenAI embeddings signature: + + + ```js + const embeddings = await portkey.embeddings.create({ + input: "create vector representation on this sentence", + model: "thenlper/gte-large", + }); + + console.log(embeddings); + ``` + + + ```python + embeddings = portkey.embeddings.create( + input='create vector representation on this sentence', + model='thenlper/gte-large' + ) + + print(embeddings) + ``` + + + + + +### Using Vision Models + +Portkey natively supports [vision models hosted on Fireworks](https://readme.fireworks.ai/docs/querying-vision-language-models): + + + ```js + const completion = await portkey.chat.completions.create( + messages: [ + { "role": "user", "content": [ + { "type": "text","text": "Can you describe this image?" }, + { "type": "image_url", "image_url": + { "url": "https://images.unsplash.com/photo-1582538885592-e70a5d7ab3d3?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1770&q=80" } + } + ] + } + ], + model: 'accounts/fireworks/models/firellava-13b' + ) + + console.log(completion); + ``` + + + ```python + completion = portkey.chat.completions.create( + messages= [ + { "role": "user", "content": [ + { "type": "text","text": "Can you describe this image?" }, + { "type": "image_url", "image_url": + { "url": "https://images.unsplash.com/photo-1582538885592-e70a5d7ab3d3?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1770&q=80" } + } + ] + } + ], + model= 'accounts/fireworks/models/firellava-13b' + ) + + print(completion) + ``` + + + + + + +### Using Image Generation Models + +Portkey also supports calling [image generation models hosted on Fireworks](https://readme.fireworks.ai/reference/image%5Fgenerationaccountsfireworksmodelsstable-diffusion-xl-1024-v1-0) in the familiar OpenAI signature: + + + ```js + import Portkey from 'portkey-ai'; + import fs from 'fs'; + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "FIREWORKS_VIRTUAL_KEY" + }); + + async function main(){ + const image = await portkey.images.generate({ + model: "accounts/fireworks/models/stable-diffusion-xl-1024-v1-0", + prompt: "An orange elephant in a purple pond" + }); + + const imageData = image.data[0].b64_json as string; + + fs.writeFileSync("fireworks-image-gen.png", Buffer.from(imageData, 'base64')); + } + + main() + ``` + + + ```python + from portkey_ai import Portkey + import base64 + from io import BytesIO + from PIL import Image + + portkey = Portkey( + api_key="PORTKEY_API_KEY", + virtual_key="FIREWORKS_VIRTUAL_KEY" + ) + + image = portkey.images.generate( + model="accounts/fireworks/models/stable-diffusion-xl-1024-v1-0", + prompt="An orange elephant in a purple pond" + ) + + Image.open(BytesIO(base64.b64decode(image.data[0].b64_json))).save("fireworks-image-gen.png") + ``` + + + + +--- + +## Fireworks Grammar Mode + +Fireworks lets you define [formal grammars](https://en.wikipedia.org/wiki/Formal%5Fgrammar) to constrain model outputs. You can use it to force the model to generate valid JSON, speak only in emojis, or anything else. ([Originally created by GGML](https://github.com/ggerganov/llama.cpp/tree/master/grammars)) + +Grammar mode is set with the `response_format` param. Just pass your grammar definition with `{"type": "grammar", "grammar": grammar_definition}` + +Let's say you want to classify patient requests into 3 pre-defined classes: + + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Defaults to os.env("PORTKEY_API_KEY") + virtual_key="FIREWORKS_VIRTUAL_KEY" # Your Virtual Key + ) + + patient_classification = """ + root ::= diagnosis + diagnosis ::= "flu" | "dengue" | "malaria" + """ + + completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + response_format={"type": "grammar", "grammar": patient_classification}, + model= 'accounts/fireworks/models/llama-v3-70b-instruct' + ) + + print(completion) + ``` + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "FIREWORKS_VIRTUAL_KEY" // Your Virtual Key + }) + + const patient_classification = ` + root ::= diagnosis + diagnosis ::= "flu" | "dengue" | "malaria" + `; + + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + response_format: {"type": "grammar", "grammar": patient_classification}, + model: 'accounts/fireworks/models/llama-v3-70b-instruct', + }); + + console.log(chatCompletion.choices); + ``` + + + + + +NOTE: Fireworks Grammer Mode is not supported on Portkey prompts playground + +[Explore the Fireworks guide for more examples and a deeper dive on Grammer node](https://readme.fireworks.ai/docs/structured-output-grammar-based). + +## Fireworks JSON Mode + +You can force the model to return (1) **An arbitrary JSON**, or (2) **JSON with given schema** with Fireworks' JSON mode. + + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Defaults to os.env("PORTKEY_API_KEY") + virtual_key="FIREWORKS_VIRTUAL_KEY" # Your Virtual Key + ) + + class Recipe(BaseModel): + title: str + description: str + steps: List[str] + + json_response = portkey.chat.completions.create( + messages = [{ "role": 'user', "content": 'Give me a recipe for making Ramen, in JSON format' }], + model = 'accounts/fireworks/models/llama-v3-70b-instruct', + response_format = { + "type":"json_object", + "schema": Recipe.schema_json() + } + ) + + print(json_response.choices[0].message.content) + ``` + + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "FIREWORKS_VIRTUAL_KEY" // Your Virtual Key + }) + + asyn function main(){ + const json_response = await portkey.chat.completions.create({ + messages: [{role: "user",content: `Give me a recipe for making Ramen, in JSON format`}], + model: "accounts/fireworks/models/llama-v3-70b-instruct", + response_format: { + type: "json_object", + schema: { + type: "object", + properties: { + title: { type: "string" }, + description: { type: "string" }, + steps: { type: "array" } + } + } + } + }); + } + + console.log(json_response.choices[0].message.content); + + main() + ``` + + + + + + +[Explore Fireworks docs for JSON mode for more examples](https://readme.fireworks.ai/docs/structured-response-formatting). + +## Fireworks Function Calling + +Portkey also supports function calling mode on Fireworks. [Explore this cookbook for a deep dive and examples](/guides/getting-started/function-calling). + +## Managing Fireworks Prompts + +You can manage all Fireworks prompts in the [Prompt Library](/product/prompt-library). All the current 49+ language models available on Fireworks are supported and you can easily start testing different prompts. + +Once you're ready with your prompt, you can use the `portkey.prompts.completions.create` interface to use the prompt in your application. + +## Next Steps + +The complete list of features supported in the SDK are available on the link below. + + + + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your ](/product/ai-gateway/configs)[requests](/product/ai-gateway/configs) +3. [Tracing requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to Firework APIs](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/fireworks/files.mdx b/virtual_key_old/integrations/llms/fireworks/files.mdx new file mode 100644 index 00000000..bc0555ec --- /dev/null +++ b/virtual_key_old/integrations/llms/fireworks/files.mdx @@ -0,0 +1,310 @@ +--- +title: Files +description: Upload files to Fireworks +--- + + +## Uploading Files + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + provider="fireworks-ai", + fireworks_account_id="FIREWORKS_ACCOUNT_ID" +) + +upload_file_response = portkey.files.create( + purpose="batch", + file=open("file.pdf", "rb") +) + +print(upload_file_response) +``` + + +```js +import { Portkey } from 'portkey-ai'; + +// Initialize the Portkey client +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY", // Add your provider's virtual key + provider: "fireworks-ai", + fireworksAccountId: "FIREWORKS_ACCOUNT_ID" +}); + +const uploadFile = async () => { + const file = await portkey.files.create({ + purpose: "batch", + file: fs.createReadStream("file.pdf") + }); + + console.log(file); +} + +await uploadFile(); + +``` + + +```sh +# you can also use a virtual key here +curl --location 'https://api.portkey.ai/v1/files' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-provider: fireworks-ai' \ +--header 'Content-Type: application/json' \ +--header 'x-portkey-fireworks-account-id: {YOUR_FIREWORKS_ACCOUNT_ID}' \ +--form 'file=@"{YOUR_FILE_PATH}"', +--form 'purpose="batch"' +``` + + +```js +import OpenAI from 'openai'; // We're using the v4 SDK +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "fireworks-ai", + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + fireworksAccountId: "FIREWORKS_ACCOUNT_ID" + }) +}); + +const uploadFile = async () => { + const file = await openai.files.create({ + purpose: "batch", + file: fs.createReadStream("file.pdf") + }); + + console.log(file); +} + +await uploadFile(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="fireworks-ai", + api_key="PORTKEY_API_KEY", + fireworks_account_id="FIREWORKS_ACCOUNT_ID" + ) +) + +upload_file_response = openai.files.create( + purpose="batch", + file=open("file.pdf", "rb") +) + +print(upload_file_response) +``` + + + + + +## Get File + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + fireworks_account_id="FIREWORKS_ACCOUNT_ID" +) + +file = portkey.files.retrieve(file_id="file_id") + +print(file) +``` + + +```js +import { Portkey } from 'portkey-ai'; + +// Initialize the Portkey client +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY", // Add your provider's virtual key + fireworksAccountId="FIREWORKS_ACCOUNT_ID", +}); + +const getFile = async () => { + const file = await portkey.files.retrieve(file_id="file_id"); + + console.log(file); +} + +await getFile(); + +``` + + +```sh +curl --location 'https://api.portkey.ai/v1/files/' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-fireworks-account-id: {YOUR_FIREWORKS_ACCOUNT_ID}' +``` + + +```js +import OpenAI from 'openai'; // We're using the v4 SDK +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "fireworks-ai", + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + fireworksAccountId="FIREWORKS_ACCOUNT_ID", + }) +}); + +const getFile = async () => { + const file = await openai.files.retrieve(file_id="file_id"); + + console.log(file); +} + +await getFile(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="fireworks-ai", + api_key="PORTKEY_API_KEY", + fireworks_account_id="FIREWORKS_ACCOUNT_ID", + ) +) + +file = openai.files.retrieve(file_id="file_id") + +print(file) +``` + + + + + +## Get File Content + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + fireworks_account_id="FIREWORKS_ACCOUNT_ID", +) + +file_content = portkey.files.content(file_id="file_id") + +print(file_content) +``` + + +```js +import { Portkey } from 'portkey-ai'; + +// Initialize the Portkey client +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY", // Add your provider's virtual key + fireworksAccountId="FIREWORKS_ACCOUNT_ID", +}); + +const getFileContent = async () => { + const file_content = await portkey.files.content(file_id="file_id"); + + console.log(file_content); +} + +await getFileContent(); + +``` + + +```sh +curl --location 'https://api.portkey.ai/v1/files//content' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-fireworks-account-id: {YOUR_FIREWORKS_ACCOUNT_ID}' +``` + + +```js +import OpenAI from 'openai'; // We're using the v4 SDK +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "fireworks-ai", + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + fireworksAccountId="FIREWORKS_ACCOUNT_ID", + }) +}); + +const getFileContent = async () => { + const file_content = await openai.files.content(file_id="file_id"); + + console.log(file_content); +} + +await getFileContent(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="fireworks-ai", + api_key="PORTKEY_API_KEY", + fireworks_account_id="FIREWORKS_ACCOUNT_ID", + ) +) + +file_content = openai.files.content(file_id="file_id") + +print(file_content) +``` + + + + + +- [Fireworks Datasets API](https://docs.fireworks.ai/api-reference/list-datasets) diff --git a/virtual_key_old/integrations/llms/fireworks/fine-tuning.mdx b/virtual_key_old/integrations/llms/fireworks/fine-tuning.mdx new file mode 100644 index 00000000..c92cd7da --- /dev/null +++ b/virtual_key_old/integrations/llms/fireworks/fine-tuning.mdx @@ -0,0 +1,459 @@ +--- +title: "Fine-tune" +description: "Fine-tune your models with Bedrock" +--- + +### Upload a file + +Please follow to the fireworks file upload [guide](/integrations/llms/fireworks/files) for more details. + +### Create a fine-tuning job + + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client + +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + fireworks_account_id="FIREWORKS_ACCOUNT_ID" # Add your fireworks's account id +) + +fine_tune_job = portkey.fine_tuning.jobs.create( + training_file="file_id", + model="model_id", + hyperparameters={ + "n_epochs": 1 + }, + validation_file="file_id", + suffix="finetuned_model_name", + ) + +print(fine_tune_job) + +```` + + +```typescript +import { Portkey } from "portkey-ai"; +// Initialize the Portkey client +const portkey = Portkey( + apiKey="PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey="VIRTUAL_KEY", // Add your provider's virtual key + fireworksAccountId="FIREWORKS_ACCOUNT_ID" // Add your fireworks's account id +) + +(async () => { + const fine_tune_job = await portkey.fineTuning.jobs.create( + training_file:"file_id", + model:"model_id", + hyperparameters: { + "n_epochs": 1 + }, + validation_file: "file_id", + suffix: "finetuned_model_name", + ) + + console.log(fine_tune_job) +})(); +```` + + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + virtual_key="VIRTUAL_KEY", + api_key="PORTKEY_API_KEY", + fireworks_account_id="FIREWORKS_ACCOUNT_ID" + ) +) + +fine_tune_job = openai.fine_tuning.jobs.create( + training_file="file_id", + model="model_id", + hyperparameters={ + "n_epochs": 1 + }, + validation_file="file_id", + suffix="finetuned_model_name", + ) + +print(fine_tune_job) +``` + + +```typescript +import OpenAI from 'openai'; // We're using the v4 SDK +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + virtualKey: "VIRTUAL_KEY", + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + fireworksAccountId: "FIREWORKS_ACCOUNT_ID" + }) +}); + +(async () => { + const fine_tune_job = await openai.fineTuning.jobs.create({ + training_file: "file_id", + model: "model_id", + hyperparameters: { + "n_epochs": 1 + }, + validation_file: "file_id", + suffix: "finetuned_model_name", + }); + + console.log(fine_tune_job) +})(); +``` + + + +```sh +curl \ +--header 'Content-Type: application/json' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-fireworks-account-id: ' \ +--data '{ + "model": "", + "suffix": "", + "training_file": "", + "hyperparameters": { + "n_epochs": 1 + } +}' \ +'https://api.portkey.ai/v1/fine_tuning/jobs' +``` + + + + +## List Fine-tuning Jobs + + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + fireworks_account_id="FIREWORKS_ACCOUNT_ID" # Add your fireworks's account id +) + +# List all fine-tuning jobs +jobs = portkey.fine_tuning.jobs.list( + limit=10 # Optional: Number of jobs to retrieve (default: 20) +) + +print(jobs) +``` + + +```typescript +import { Portkey } from "portkey-ai"; + +// Initialize the Portkey client +const portkey = Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY", // Add your provider's virtual key + fireworksAccountId: "FIREWORKS_ACCOUNT_ID" // Add your fireworks's account id +}); + +(async () => { + // List all fine-tuning jobs + const jobs = await portkey.fineTuning.jobs.list({ + limit: 10 // Optional: Number of jobs to retrieve (default: 20) + }); + + console.log(jobs); +})(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + virtual_key="VIRTUAL_KEY", + api_key="PORTKEY_API_KEY", + fireworks_account_id="FIREWORKS_ACCOUNT_ID" + ) +) + +# List all fine-tuning jobs +jobs = openai.fine_tuning.jobs.list( + limit=10 # Optional: Number of jobs to retrieve (default: 20) +) + +print(jobs) +``` + + +```typescript +import OpenAI from 'openai'; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'; + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + virtualKey: "VIRTUAL_KEY", + apiKey: "PORTKEY_API_KEY", + fireworksAccountId: "FIREWORKS_ACCOUNT_ID" + }) +}); + +(async () => { + // List all fine-tuning jobs + const jobs = await openai.fineTuning.jobs.list({ + limit: 10 // Optional: Number of jobs to retrieve (default: 20) + }); + + console.log(jobs); +})(); +``` + + +```sh +curl \ +--header 'Content-Type: application/json' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-fireworks-account-id: ' \ +'https://api.portkey.ai/v1/fine_tuning/jobs?limit=10' +``` + + + +## Retrieve Fine-tuning Job + + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + fireworks_account_id="FIREWORKS_ACCOUNT_ID" # Add your fireworks's account id +) + +# Retrieve a specific fine-tuning job +job = portkey.fine_tuning.jobs.retrieve( + job_id="job_id" # The ID of the fine-tuning job to retrieve +) + +print(job) +``` + + +```typescript +import { Portkey } from "portkey-ai"; + +// Initialize the Portkey client +const portkey = Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY", // Add your provider's virtual key + fireworksAccountId: "FIREWORKS_ACCOUNT_ID" // Add your fireworks's account id +}); + +(async () => { + // Retrieve a specific fine-tuning job + const job = await portkey.fineTuning.jobs.retrieve({ + job_id: "job_id" // The ID of the fine-tuning job to retrieve + }); + + console.log(job); +})(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + virtual_key="VIRTUAL_KEY", + api_key="PORTKEY_API_KEY", + fireworks_account_id="FIREWORKS_ACCOUNT_ID" + ) +) + +# Retrieve a specific fine-tuning job +job = openai.fine_tuning.jobs.retrieve( + fine_tuning_job_id="job_id" # The ID of the fine-tuning job to retrieve +) + +print(job) +``` + + +```typescript +import OpenAI from 'openai'; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'; + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + virtualKey: "VIRTUAL_KEY", + apiKey: "PORTKEY_API_KEY", + fireworksAccountId: "FIREWORKS_ACCOUNT_ID" + }) +}); + +(async () => { + // Retrieve a specific fine-tuning job + const job = await openai.fineTuning.jobs.retrieve( + "job_id" // The ID of the fine-tuning job to retrieve + ); + + console.log(job); +})(); +``` + + +```sh +curl \ +--header 'Content-Type: application/json' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-fireworks-account-id: ' \ +'https://api.portkey.ai/v1/fine_tuning/jobs/' +``` + + + +## Cancel Fine-tuning Job + + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + fireworks_account_id="FIREWORKS_ACCOUNT_ID" # Add your fireworks's account id +) + +# Cancel a fine-tuning job +cancelled_job = portkey.fine_tuning.jobs.cancel( + job_id="job_id" # The ID of the fine-tuning job to cancel +) + +print(cancelled_job) +``` + + +```typescript +import { Portkey } from "portkey-ai"; + +// Initialize the Portkey client +const portkey = Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY", // Add your provider's virtual key + fireworksAccountId: "FIREWORKS_ACCOUNT_ID" // Add your fireworks's account id +}); + +(async () => { + // Cancel a fine-tuning job + const cancelledJob = await portkey.fineTuning.jobs.cancel({ + job_id: "job_id" // The ID of the fine-tuning job to cancel + }); + + console.log(cancelledJob); +})(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + virtual_key="VIRTUAL_KEY", + api_key="PORTKEY_API_KEY", + fireworks_account_id="FIREWORKS_ACCOUNT_ID" + ) +) + +# Cancel a fine-tuning job +cancelled_job = openai.fine_tuning.jobs.cancel( + fine_tuning_job_id="job_id" # The ID of the fine-tuning job to cancel +) + +print(cancelled_job) +``` + + +```typescript +import OpenAI from 'openai'; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'; + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + virtualKey: "VIRTUAL_KEY", + apiKey: "PORTKEY_API_KEY", + fireworksAccountId: "FIREWORKS_ACCOUNT_ID" + }) +}); + +(async () => { + // Cancel a fine-tuning job + const cancelledJob = await openai.fineTuning.jobs.cancel( + "job_id" // The ID of the fine-tuning job to cancel + ); + + console.log(cancelledJob); +})(); +``` + + +```sh +curl \ +--request POST \ +--header 'Content-Type: application/json' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' \ +--header 'x-portkey-fireworks-account-id: ' \ +'https://api.portkey.ai/v1/fine_tuning/jobs//cancel' +``` + + + + +## References + +- [Fireworks Fine-tuning](https://docs.fireworks.ai/fine-tuning/fine-tuning-models) +- [Fireworks Fine-tuning API](https://docs.fireworks.ai/api-reference/list-supervised-fine-tuning-jobs) \ No newline at end of file diff --git a/virtual_key_old/integrations/llms/gemini.mdx b/virtual_key_old/integrations/llms/gemini.mdx new file mode 100644 index 00000000..ca5c17d0 --- /dev/null +++ b/virtual_key_old/integrations/llms/gemini.mdx @@ -0,0 +1,491 @@ +--- +title: "Google Gemini" +--- + +Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including [Google Gemini APIs](https://cloud.google.com/vertex-ai/docs/generative-ai/model-reference/gemini). + +With Portkey, you can take advantage of features like fast AI gateway access, observability, prompt management, and more, all while ensuring the secure management of your LLM API keys through a [virtual key](/product/ai-gateway/virtual-keys) system. +Provider Slug. `google` + + +## Portkey SDK Integration with Google Gemini Models + +Portkey provides a consistent API to interact with models from various providers. To integrate Google Gemini with Portkey: + +### 1\. Install the Portkey SDK + +Add the Portkey SDK to your application to interact with Google Gemini's API through Portkey's gateway. + + + +```sh +npm install --save portkey-ai +``` + + +```sh +pip install portkey-ai +``` + + + + +### 2\. Initialize Portkey with the Virtual Key + +To use Gemini with Portkey, [get your API key from here](https://aistudio.google.com/app/apikey), then add it to Portkey to create the virtual key. + + +```js +import Portkey from 'portkey-ai' + +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your Google Virtual Key +}) +``` + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Google + ) + ``` + + + + + + +### **3\. Invoke Chat Completions with** Google Gemini + +Use the Portkey instance to send requests to Google Gemini. You can also override the virtual key directly in the API call if needed. + + + ```js + const chatCompletion = await portkey.chat.completions.create({ + messages: [ + { role: 'system', content: 'You are not a helpful assistant' }, + { role: 'user', content: 'Say this is a test' } + ], + model: 'gemini-1.5-pro', + }); + + console.log(chatCompletion.choices); + ``` + + + +```python +completion = portkey.chat.completions.create( + messages= [ + { "role": 'system', "content": 'You are not a helpful assistant' }, + { "role": 'user', "content": 'Say this is a test' } + ], + model= 'gemini-1.5-pro' +) + +print(completion) +``` + + + + + + +Portkey supports the `system_instructions` parameter for Google Gemini 1.5 - allowing you to control the behavior and output of your Gemini-powered applications with ease. + +Simply include your Gemini system prompt as part of the `{"role":"system"}` message within the `messages` array of your request body. Portkey Gateway will automatically transform your message to ensure seamless compatibility with the Google Gemini API. + + +## Function Calling + +Portkey supports function calling mode on Google's Gemini Models. Explore this Cookbook for a deep dive and examples: + +[Function Calling](/guides/getting-started/function-calling) + +## Document, Video, Audio Processing with Gemini +Gemini supports attaching `mp4`, `pdf`, `jpg`, `mp3`, `wav`, etc. file types to your messages. + + +Gemini Docs: +* [Document Processing](https://ai.google.dev/gemini-api/docs/document-processing?lang=python) +* [Video & Image Processing](https://ai.google.dev/gemini-api/docs/vision?lang=python) +* [Audio Processing](https://ai.google.dev/gemini-api/docs/audio?lang=python) + + +Using Portkey, here's how you can send these media files: + + +```javascript JavaScript +const chatCompletion = await portkey.chat.completions.create({ + messages: [ + { role: 'system', content: 'You are a helpful assistant' }, + { role: 'user', content: [ + { + type: 'image_url', + image_url: { + url: 'gs://cloud-samples-data/generative-ai/image/scones.jpg' + } + }, + { + type: 'text', + text: 'Describe the image' + } + ]} + ], + model: 'gemini-1.5-pro', + max_tokens: 200 +}); +``` + +```python Python +completion = portkey.chat.completions.create( + messages=[ + { + "role": "system", + "content": "You are a helpful assistant" + }, + { + "role": "user", + "content": [ + { + "type": "image_url", + "image_url": { + "url": "gs://cloud-samples-data/generative-ai/image/scones.jpg" + } + }, + { + "type": "text", + "text": "Describe the image" + } + ] + } + ], + model='gemini-1.5-pro', + max_tokens=200 +) +print(completion) +``` + +```sh cURL +curl --location 'https://api.portkey.ai/v1/chat/completions' \ +--header 'x-portkey-provider: vertex-ai' \ +--header 'x-portkey-vertex-region: us-central1' \ +--header 'Content-Type: application/json' \ +--header 'x-portkey-api-key: PORTKEY_API_KEY' \ +--header 'Authorization: GEMINI_API_KEY' \ +--data '{ + "model": "gemini-1.5-pro", + "max_tokens": 200, + "stream": false, + "messages": [ + { + "role": "system", + "content": "You are a helpful assistant" + }, + { + "role": "user", + "content": [ + { + "type": "image_url", + "image_url": { + "url": "gs://cloud-samples-data/generative-ai/image/scones.jpg" + } + }, + { + "type": "text", + "text": "describe this image" + } + ] + } + ] +}' +``` + + +This same message format also works for all other media types โ€” just send your media file in the `url` field, like `"url": "gs://cloud-samples-data/video/animals.mp4"`. + + +Your URL should have the file extension, this is used for inferring `MIME_TYPE` which is a required parameter for prompting Gemini models with files. + + +### Sending base64 Image +Here, you can send the `base64` image data along with the `url` field too: + +```json +"url": "data:image/png;base64,UklGRkacAABXRUJQVlA4IDqcAAC....." +``` + +## Grounding with Google Search + +Vertex AI supports grounding with Google Search. This is a feature that allows you to ground your LLM responses with real-time search results. +Grounding is invoked by passing the `google_search` tool (for newer models like gemini-2.0-flash-001), and `google_search_retrieval` (for older models like gemini-1.5-flash) in the `tools` array. + +```json +"tools": [ + { + "type": "function", + "function": { + "name": "google_search" // or google_search_retrieval for older models + } + }] +``` + + +If you mix regular tools with grounding tools, vertex might throw an error saying only one tool can be used at a time. + + + +## Extended Thinking (Reasoning Models) (Beta) + + +The assistants thinking response is returned in the `response_chunk.choices[0].delta.content_blocks` array, not the `response.choices[0].message.content` string. + + +Models like `gemini-2.5-flash-preview-04-17` `gemini-2.5-flash-preview-04-17` support [extended thinking](https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/use-claude#claude-3-7-sonnet). +This is similar to openai thinking, but you get the model's reasoning as it processes the request as well. + +Note that you will have to set [`strict_open_ai_compliance=False`](/product/ai-gateway/strict-open-ai-compliance) in the headers to use this feature. + +### Single turn conversation + + ```py Python + from portkey_ai import Portkey + + # Initialize the Portkey client + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + strict_open_ai_compliance=False + ) + + # Create the request + response = portkey.chat.completions.create( + model="gemini-2.5-flash-preview-04-17", + max_tokens=3000, + thinking={ + "type": "enabled", + "budget_tokens": 2030 + }, + stream=True, + messages=[ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "when does the flight from new york to bengaluru land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + } + ] + ) + print(response) + # in case of streaming responses you'd have to parse the response_chunk.choices[0].delta.content_blocks array + # response = portkey.chat.completions.create( + # ...same config as above but with stream: true + # ) + # for chunk in response: + # if chunk.choices[0].delta: + # content_blocks = chunk.choices[0].delta.get("content_blocks") + # if content_blocks is not None: + # for content_block in content_blocks: + # print(content_block) + ``` + ```ts NodeJS + import Portkey from 'portkey-ai'; + + // Initialize the Portkey client + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY", // your vertex-ai virtual key + strictOpenAiCompliance: false + }); + + // Generate a chat completion + async function getChatCompletionFunctions() { + const response = await portkey.chat.completions.create({ + model: "gemini-2.5-flash-preview-04-17", + max_tokens: 3000, + thinking: { + type: "enabled", + budget_tokens: 2030 + }, + stream: true, + messages: [ + { + role: "user", + content: [ + { + type: "text", + text: "when does the flight from new york to bengaluru land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + } + ] + }); + console.log(response); + // in case of streaming responses you'd have to parse the response_chunk.choices[0].delta.content_blocks array + // const response = await portkey.chat.completions.create({ + // ...same config as above but with stream: true + // }); + // for await (const chunk of response) { + // if (chunk.choices[0].delta?.content_blocks) { + // for (const contentBlock of chunk.choices[0].delta.content_blocks) { + // console.log(contentBlock); + // } + // } + // } + } + // Call the function + getChatCompletionFunctions(); + ``` + ```js OpenAI NodeJS + import OpenAI from 'openai'; // We're using the v4 SDK + import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + + const openai = new OpenAI({ + apiKey: 'VERTEX_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "vertex-ai", + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + strictOpenAiCompliance: false + }) + }); + + // Generate a chat completion with streaming + async function getChatCompletionFunctions(){ + const response = await openai.chat.completions.create({ + model: "gemini-2.5-flash-preview-04-17", + max_tokens: 3000, + thinking: { + type: "enabled", + budget_tokens: 2030 + }, + stream: true, + messages: [ + { + role: "user", + content: [ + { + type: "text", + text: "when does the flight from new york to bengaluru land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + } + ], + }); + + console.log(response) + // in case of streaming responses you'd have to parse the response_chunk.choices[0].delta.content_blocks array + // const response = await openai.chat.completions.create({ + // ...same config as above but with stream: true + // }); + // for await (const chunk of response) { + // if (chunk.choices[0].delta?.content_blocks) { + // for (const contentBlock of chunk.choices[0].delta.content_blocks) { + // console.log(contentBlock); + // } + // } + // } + } + await getChatCompletionFunctions(); + ``` + ```py OpenAI Python + from openai import OpenAI + from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + + openai = OpenAI( + api_key='VERTEX_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="vertex-ai", + api_key="PORTKEY_API_KEY", + strict_open_ai_compliance=False + ) + ) + + + response = openai.chat.completions.create( + model="gemini-2.5-flash-preview-04-17", + max_tokens=3000, + thinking={ + "type": "enabled", + "budget_tokens": 2030 + }, + stream=True, + messages=[ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "when does the flight from new york to bengaluru land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + } + ] + ) + + print(response) + ``` + ```sh cURL + curl "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-provider: vertex-ai" \ + -H "x-api-key: $VERTEX_API_KEY" \ + -H "x-portkey-strict-open-ai-compliance: false" \ + -d '{ + "model": "gemini-2.5-flash-preview-04-17", + "max_tokens": 3000, + "thinking": { + "type": "enabled", + "budget_tokens": 2030 + }, + "stream": true, + "messages": [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "when does the flight from new york to bengaluru land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + } + ] + }' + ``` + + + + To disable thinking for gemini models like `gemini-2.5-flash-preview-04-17`, you are required to explicitly set `budget_tokens` to `0`. + ```json + "thinking": { + "type": "enabled", + "budget_tokens": 0 + } + ``` + + + +Gemini grounding mode may not work via Portkey SDK. Contact support@portkey.ai for assistance. + + +## Next Steps + +The complete list of features supported in the SDK are available on the link below. + + + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your Gemini requests](/product/ai-gateway/configs) +3. [Tracing Google Gemini requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to Gemini APIs](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/github.mdx b/virtual_key_old/integrations/llms/github.mdx new file mode 100644 index 00000000..666ca35f --- /dev/null +++ b/virtual_key_old/integrations/llms/github.mdx @@ -0,0 +1,105 @@ +--- +title: "Github" +--- + +Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including the models hosted on [Github Models Marketplace](https://github.com/marketplace/models). + + +Provider Slug: `github` + + +## Portkey SDK Integration with Github Models + +Portkey provides a consistent API to interact with models from various providers. To integrate Github Models with Portkey: + +### 1\. Install the Portkey SDK + + + ```sh + npm install --save portkey-ai + ``` + + + ```sh + pip install -U portkey-ai + ``` + + + + + + +### 2\. Initialize Portkey with Github Models + +To use Github with Portkey, get your API key [from here](https://github.com/settings/tokens), then add it to Portkey to create the virtual key. + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "GITHUB_VIRTUAL_KEY" // Your Github Models virtual key + }) + ``` + + + ```py + from portkey_ai import Portkey + + portkey = Portkey( + api_key ="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="GITHUB_VIRTUAL_KEY" # Your Github Models virtual key + ) + ``` + + + + + + + +### 3\. Invoke Chat Completions + + + ```js + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'Phi-3-small-128k-instruct', + }); + + console.log(chatCompletion.choices); + ``` + + + ```python + completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model= 'Phi-3-small-128k-instruct' + ) + + print(completion) + ``` + + + + +--- + +## Supported Models + +Portkey supports *all* the models (both `Chat/completion` and `Embeddings` capabilities) on the Github Models marketplace. + +## Next Steps + +The complete list of features supported in the SDK are available on the link below. + + + + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your requests](/product/ai-gateway/configs)[ requests](/product/ai-gateway/configs) +3. [Tracing Github requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to Github](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/google-palm.mdx b/virtual_key_old/integrations/llms/google-palm.mdx new file mode 100644 index 00000000..5f3cff17 --- /dev/null +++ b/virtual_key_old/integrations/llms/google-palm.mdx @@ -0,0 +1,123 @@ +--- +title: "Google Palm" +--- + +Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including [Google Palm APIs](https://developers.generativeai.google/guide/palm%5Fapi%5Foverview). + +With Portkey, you can take advantage of features like fast AI gateway access, observability, prompt management, and more, all while ensuring the secure management of your LLM API keys through a [virtual key](/product/ai-gateway/virtual-keys) system. + + +Provider Slug. `palm` + +## Portkey SDK Integration with Google Palm + +Portkey provides a consistent API to interact with models from various providers. To integrate Google Palm with Portkey: + +### 1\. Install the Portkey SDK + +Add the Portkey SDK to your application to interact with Google Palm's API through Portkey's gateway. + + + ``` + npm install --save portkey-ai + ``` + + + ``` + pip install portkey-ai + ``` + + + + + + +### 2\. Initialize Portkey with the Virtual Key + +Set up Portkey with your virtual key as part of the initialization configuration. You can create a [virtual key](/product/ai-gateway/virtual-keys) for Google Palm in the UI. + + + ```js + import Portkey from 'portkey-ai' + + + const portkey = new Portkey({ + + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + + virtualKey: "VIRTUAL_KEY" // Your Google Palm Virtual Key + + }) + ``` + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Google Palm + + ) + ``` + + + + + + + +### **3\. Invoke Chat Completions with** Google Palm + +Use the Portkey instance to send requests to Google Palm. You can also override the virtual key directly in the API call if needed. + + + ```js + const chatCompletion = await portkey.chat.completions.create({ + + messages: [{ role: 'user', content: 'Say this is a test' }], + + model: 'chat-bison-001', + + }); + + console.log(chatCompletion.choices); + ``` + + + ```python + completion = portkey.chat.completions.create( + + messages= [{ "role": 'user', "content": 'Say this is a test' }], + + model= 'chat-bison-001' + + ) + + print(completion) + ``` + + + + + +## Managing Google Palm Prompts + +You can manage all prompts to Google Palm in the [Prompt Library](/product/prompt-library). All the current models of Google Palm are supported and you can easily start testing different prompts. + +Once you're ready with your prompt, you can use the `portkey.prompts.completions.create` interface to use the prompt in your application. + +## Next Steps + +The complete list of features supported in the SDK are available on the link below. + + + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your Palm requests](/product/ai-gateway/configs) +3. [Tracing Google Palm requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to Palm APIs](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/groq.mdx b/virtual_key_old/integrations/llms/groq.mdx new file mode 100644 index 00000000..3ff1d459 --- /dev/null +++ b/virtual_key_old/integrations/llms/groq.mdx @@ -0,0 +1,349 @@ +--- +title: "Groq" +--- + +Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including [Groq APIs](https://console.groq.com/docs/quickstart). + +With Portkey, you can take advantage of features like fast AI gateway access, observability, prompt management, and more, all while ensuring the secure management of your LLM API keys through a [virtual key](/product/ai-gateway/virtual-keys) system. + +Provider Slug. `groq` + +## Portkey SDK Integration with Groq Models + +Portkey provides a consistent API to interact with models from various providers. To integrate Groq with Portkey: + +### 1\. Install the Portkey SDK + +Add the Portkey SDK to your application to interact with Groq AI's API through Portkey's gateway. + + + + ```sh + npm install --save portkey-ai + ``` + + + ```sh + pip install portkey-ai + ``` + + + + + + + +### 2\. Initialize Portkey with the Virtual Key + +To use Groq with Portkey, [get your API key from here](https://console.groq.com/keys), then add it to Portkey to create the virtual key. + + + + +```js +import Portkey from 'portkey-ai' + +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your Groq Virtual Key +}) +``` + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Groq + ) + ``` + + + + + + + +### **3\. Invoke Chat Completions with** Groq + +Use the Portkey instance to send requests to Groq. You can also override the virtual key directly in the API call if needed. + + + + + ```js + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'mixtral-8x7b-32768', + }); + + console.log(chatCompletion.choices); + ``` + + + +```python +completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model= 'mistral-medium' +) + +print(completion) +``` + + + + + + +## Managing Groq Prompts + +You can manage all prompts to Groq in the [Prompt Library](/product/prompt-library). All the current models of Groq are supported and you can easily start testing different prompts. + +Once you're ready with your prompt, you can use the `portkey.prompts.completions.create` interface to use the prompt in your application. + + + +### Groq Tool Calling +Tool calling feature lets models trigger external tools based on conversation context. You define available functions, the model chooses when to use them, and your application executes them and returns results. + +Portkey supports Groq Tool Calling and makes it interoperable across multiple providers. With Portkey Prompts, you can templatize various your prompts & tool schemas as well. + + + + + + + +```javascript Get Weather Tool +let tools = [{ + type: "function", + function: { + name: "getWeather", + description: "Get the current weather", + parameters: { + type: "object", + properties: { + location: { type: "string", description: "City and state" }, + unit: { type: "string", enum: ["celsius", "fahrenheit"] } + }, + required: ["location"] + } + } +}]; + +let response = await portkey.chat.completions.create({ + model: "llama-3.3-70b-versatile", + messages: [ + { role: "system", content: "You are a helpful assistant." }, + { role: "user", content: "What's the weather like in Delhi - respond in JSON" } + ], + tools, + tool_choice: "auto", +}); + +console.log(response.choices[0].finish_reason); +``` + + +```python Get Weather Tool +tools = [{ + "type": "function", + "function": { + "name": "getWeather", + "description": "Get the current weather", + "parameters": { + "type": "object", + "properties": { + "location": {"type": "string", "description": "City and state"}, + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]} + }, + "required": ["location"] + } + } +}] + +response = portkey.chat.completions.create( + model="llama-3.3-70b-versatile", + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "What's the weather like in Delhi - respond in JSON"} + ], + tools=tools, + tool_choice="auto" +) + +print(response.choices[0].finish_reason) +``` + + +```curl Get Weather Tool +curl -X POST "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -d '{ + "model": "llama-3.3-70b-versatile", + "messages": [ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "What'\''s the weather like in Delhi - respond in JSON"} + ], + "tools": [{ + "type": "function", + "function": { + "name": "getWeather", + "description": "Get the current weather", + "parameters": { + "type": "object", + "properties": { + "location": {"type": "string", "description": "City and state"}, + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]} + }, + "required": ["location"] + } + } + }], + "tool_choice": "auto" + }' +``` + + + + + + + + + + + + +### Groq Speech to Text (Whisper) + +OpenAI's Audio API converts speech to text using the Whisper model. It offers transcription in the original language and translation to English, supporting multiple file formats and languages with high accuracy. + + +```python Python +audio_file= open("/path/to/file.mp3", "rb") + +# Transcription +transcription = portkey.audio.transcriptions.create( + model="whisper-large-v3", + file=audio_file +) +print(transcription.text) + +# Translation +translation = portkey.audio.translations.create( + model="whisper-large-v3", + file=audio_file +) +print(translation.text) +``` + +```javascript Node.js +import fs from "fs"; + +// Transcription +async function transcribe() { + const transcription = await portkey.audio.transcriptions.create({ + file: fs.createReadStream("/path/to/file.mp3"), + model: "whisper-large-v3", + }); + console.log(transcription.text); +} +transcribe(); + +// Translation +async function translate() { + const translation = await portkey.audio.translations.create({ + file: fs.createReadStream("/path/to/file.mp3"), + model: "whisper-large-v3", + }); + console.log(translation.text); +} +translate(); +``` + +```curl REST +# Transcription +curl -X POST "https://api.portkey.ai/v1/audio/transcriptions" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -H "Content-Type: multipart/form-data" \ + -F "file=@/path/to/file.mp3" \ + -F "model=whisper-large-v3" + +# Translation +curl -X POST "https://api.portkey.ai/v1/audio/translations" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -H "Content-Type: multipart/form-data" \ + -F "file=@/path/to/file.mp3" \ + -F "model=whisper-large-v3" +``` + + + +--- + +### Groq Text to Speech + +Groq's Text to Speech (TTS) API converts written text into natural-sounding audio using six distinct voices. It supports multiple languages, streaming capabilities, and various audio formats for different use cases. + + +```python Python +from pathlib import Path + +speech_file_path = Path(__file__).parent / "speech.mp3" +response = portkey.audio.speech.create( + model="playai-tts", + voice="Fritz-PlayAI", + input="Today is a wonderful day to build something people love!" +) + +with open(speech_file_path, "wb") as f: + f.write(response.content) +``` + +```javascript Node.js +import path from 'path'; +import fs from 'fs'; + +const speechFile = path.resolve("./speech.mp3"); + +async function main() { + const mp3 = await portkey.audio.speech.createCertainly! I'll continue with the Text to Speech section and then move on to the additional features and sections: + +```javascript Node.js +({ + model: "playai-tts", + voice: "Fritz-PlayAI", + input: "Today is a wonderful day to build something people love!", + }); + const buffer = Buffer.from(await mp3.arrayBuffer()); + await fs.promises.writeFile(speechFile, buffer); +} + +main(); +``` + +```curl REST +curl -X POST "https://api.portkey.ai/v1/audio/speech" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -H "Content-Type: application/json" \ + -d '{ + "model": "playai-tts", + "voice": "Fritz-PlayAI", + "input": "Today is a wonderful day to build something people love!" + }' \ + --output speech.mp3 +``` + + +--- + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your Groq](/product/ai-gateway/configs)[ requests](/product/ai-gateway/configs) +3. [Tracing Groq requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to Groq APIs](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/huggingface.mdx b/virtual_key_old/integrations/llms/huggingface.mdx new file mode 100644 index 00000000..797aa952 --- /dev/null +++ b/virtual_key_old/integrations/llms/huggingface.mdx @@ -0,0 +1,241 @@ +--- +title: "Hugging Face" +--- + +Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including all the text generation models supported by [Huggingface's Inference endpoints](https://huggingface.co/docs/api-inference/index). + +With Portkey, you can take advantage of features like fast AI gateway access, observability, prompt management, and more, all while ensuring the secure management of your LLM API keys through a [virtual key](/product/ai-gateway/virtual-keys) system. + + +Provider Slug. `huggingface` + +## Portkey SDK Integration with Huggingface + +Portkey provides a consistent API to interact with models from various providers. To integrate Huggingface with Portkey: + +### 1\. Install the Portkey SDK + +Add the Portkey SDK to your application to interact with Huggingface's API through Portkey's gateway. + + + + ```sh + npm install --save portkey-ai + ``` + + + ```sh + pip install portkey-ai + ``` + + + + + + + +### 2\. Initialize Portkey with the Virtual Key + +To use Huggingface with Portkey, [get your Huggingface Access token from here](https://huggingface.co/settings/tokens), then add it to Portkey to create the virtual key. + + + + +```js +import Portkey from 'portkey-ai' + +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY", // Your Huggingface Virtual Key + huggingfaceBaseUrl: "HUGGINGFACE_DEDICATED_URL" // Optional: Use this if you have a dedicated server hosted on Huggingface +}) +``` + + + +```py +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY", # Replace with your virtual key for Huggingface + huggingface_base_url="HUGGINGFACE_DEDICATED_URL" # Optional: Use this if you have a dedicated server hosted on Huggingface +) +``` + + + +```py +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +client = OpenAI( + api_key="HUGGINGFACE_ACCESS_TOKEN", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key="PORTKEY_API_KEY", + provider="huggingface", + huggingface_base_url="HUGGINGFACE_DEDICATED_URL" + ) +) +``` + + + + + ```js + import OpenAI from "openai"; + import { PORTKEY_GATEWAY_URL, createHeaders } from "portkey-ai"; + + const client = new OpenAI({ + apiKey: "HUGGINGFACE_ACCESS_TOKEN", + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "huggingface", + apiKey: "PORTKEY_API_KEY", + huggingfaceBaseUrl: "HUGGINGFACE_DEDICATED_URL" + }), + }); + ``` + + + + + +### **3\. Invoke Chat Completions with** Huggingface + +Use the Portkey instance to send requests to Huggingface. You can also override the virtual key directly in the API call if needed. + + + + +```js +const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'meta-llama/Meta-Llama-3.1-8B-Instruct', // make sure your model is hot +}); + +console.log(chatCompletion.choices[0].message.content); +``` + + +```py +chat_completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model= 'meta-llama/meta-llama-3.1-8b-instruct', # make sure your model is hot +) + +print(chat_completion.choices[0].message.content) +``` + + + +```py +chat_completion = client.chat.completions.create( + messages = [{ "role": 'user', "content": 'Say this is a test' }], + model = 'meta-llama/meta-llama-3.1-8b-instruct', # make sure your model is hot +) + +print(chat_completion.choices[0].message.content) +``` + + + + +```js +async function main() { + const chatCompletion = await client.chat.completions.create({ + model: "meta-llama/meta-llama-3.1-8b-instruct", // make sure your model is hot + messages: [{ role: "user", content: "How many points to Gryffindor?" }], + }); + console.log(chatCompletion.choices[0].message.content); +} + +main(); +``` + + + +```py +chat_completion = client.chat.completions.create( + messages = [{ "role": 'user', "content": 'Say this is a test' }], + model = 'meta-llama/meta-llama-3.1-8b-instruct', # make sure your model is hot +) + +print(chat_completion.choices[0].message.content) +``` + + + + +## [Using Virtual Keys](https://app.portkey.ai/virtual-keys) + +Virtual Keys serve as Portkey's unified authentication system for all LLM interactions, simplifying the use of multiple providers and Portkey features within your application. For self-hosted LLMs, you can configure custom authentication requirements including authorization keys, bearer tokens, or any other headers needed to access your model: + + + + + +1. Navigate to [Virtual Keys](https://app.portkey.ai/virtual-keys) in your Portkey dashboard +2. Click **"Add Key"** and enable the **"Local/Privately hosted provider"** toggle +3. Configure your deployment: + - Select the matching provider API specification (typically `OpenAI`) + - Enter your model's base URL in the `Custom Host` field + - Add required authentication headers and their values +4. Click **"Create"** to generate your virtual key + +You can now use this virtual key in your requests: + + + + ```js + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "YOUR_SELF_HOSTED_LLM_VIRTUAL_KEY" + + async function main() { + const response = await client.chat.completions.create({ + messages: [{ role: "user", content: "Bob the builder.." }], + model: "your-self-hosted-model-name", + }); + + console.log(response.choices[0].message.content); + }) + ``` + + + ```python + portkey = Portkey( + api_key="PORTKEY_API_KEY", + virtual_key="YOUR_SELF_HOSTED_LLM_VIRTUAL_KEY" + ) + + response = portkey.chat.completions.create( + model="your-self-hosted-model-name", + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "Hello!"} + ] + + print(response) + ) + ``` + + + +For more information about managing self-hosted LLMs with Portkey, see [Bring Your Own LLM](/integrations/llms/byollm). + + + +# Next Steps + +The complete list of features supported in the SDK are available on the link below. + + + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your Huggingface requests](/product/ai-gateway/configs)[ requests](/product/ai-gateway/configs) +3. [Tracing Huggingface requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to Huggingface APIs](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/inference.net.mdx b/virtual_key_old/integrations/llms/inference.net.mdx new file mode 100644 index 00000000..9ecabdf9 --- /dev/null +++ b/virtual_key_old/integrations/llms/inference.net.mdx @@ -0,0 +1,101 @@ +--- +title: "Inference.net" +description: "Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including the models hosted on [Inference.net](https://www.inference.net/). +" +--- + + + +Provider slug: `inference-net` + + +## Portkey SDK Integration with Inference.net + +Portkey provides a consistent API to interact with models from various providers. To integrate Inference.net with Portkey: + +### 1. Install the Portkey SDK + + + + ```sh + npm install --save portkey-ai + ``` + + + ```sh + pip install portkey-ai + ``` + + + +### 2. Initialize Portkey with Inference.net Authorization + +* Set `provider` name as `inference-net` +* Pass your API key with `Authorization` header + + + + ```javascript + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + provider: "inference-net", + Authorization: "Bearer INFERENCE-NET API KEY" + }) + ``` + + + ```python + from portkey_ai import Portkey + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + provider="inference-net", + Authorization="Bearer INFERENCE-NET API KEY" + ) + ``` + + + +### 3. Invoke Chat Completions + + + + ```javascript + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'llama3', + }); + console.log(chatCompletion.choices); + ``` + + + ```python + completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model= 'llama3' + ) + print(completion) + ``` + + + +## Supported Models + +Find more info about models supported by Inference.net here: + +[Inference.net](https://www.inference.net/) + +## Next Steps + +The complete list of features supported in the SDK are available on the link below. + + + + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your Inference.net requests](/product/ai-gateway/configs) +3. [Tracing Inference.net requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to Inference.net](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/jina-ai.mdx b/virtual_key_old/integrations/llms/jina-ai.mdx new file mode 100644 index 00000000..b5d1a84d --- /dev/null +++ b/virtual_key_old/integrations/llms/jina-ai.mdx @@ -0,0 +1,133 @@ +--- +title: "Jina AI" +--- + +Portkey provides a robust and secure gateway to facilitate the integration of various models into your applications, including [Jina AI embedding & reranker models](https://jina.ai/). + +With Portkey, you can take advantage of features like fast AI gateway access, observability, and more, all while ensuring the secure management of your API keys through a [virtual key](/product/ai-gateway/virtual-keys) system. + +Provider Slug. `jina` + +## Portkey SDK Integration with Jina AI Models + +Portkey provides a consistent API to interact with models from various providers. To integrate Jina AI with Portkey: + +### 1\. Install the Portkey SDK + +Add the Portkey SDK to your application to interact with Jina AI's API through Portkey's gateway. + + + + + ```sh + npm install --save portkey-ai + ``` + + + +```sh +pip install portkey-ai +``` + + + + + + +### 2\. Initialize Portkey with the Virtual Key + +To use JinaAI with Portkey, [get your API key from here](https://jina.ai/), then add it to Portkey to create the virtual key. + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "JINA_AI_VIRTUAL_KEY" // Your Jina AI Virtual Key + }) + ``` + + +```python +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="JINA_AI_VIRTUAL_KEY" # Replace with your virtual key for Jina AI +) +``` + + + + + + + +### **3\. Invoke Embeddings with** Jina AI + +Use the Portkey instance to send your embeddings requests to Jina AI. You can also override the virtual key directly in the API call if needed. + + + ```js + const embeddings = await portkey.embeddings.create({ + input: "embed this", + model: "jina-embeddings-v2-base-es", + }); + ``` + + +```py +embeddings = portkey.embeddings.create( + input = "embed this", + model = "jina-embeddings-v2-base-de" +) +``` + + + + +### Using Jina AI Reranker Models + +Portkey also supports the Reranker models by Jina AI through the REST API. + + + ```sh + curl https://api.portkey.ai/v1/rerank \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $JINA_AI_API_KEY" \ + -H "x-portkey-provider: jina" \ + -d '{ + "model": "jina-reranker-v1-base-en", + "query": "Organic skincare products for sensitive skin", + "documents": [ + "Eco-friendly kitchenware for modern homes", + "Biodegradable cleaning supplies for eco-conscious consumers", + "Organic cotton baby clothes for sensitive skin" + ], + "top_n": 2 + }' + ``` + + + + + + + +## Supported Models + +Portkey works with all the embedding & reranker models offered by Jina AI. You can browse the full list of Jina AI models [here](https://jina.ai/embeddings#apiform). + +## Next Steps + +The complete list of features supported in the SDK are available on the link below. + + + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your J](/product/ai-gateway/configs)ina AI[ requests](/product/ai-gateway/configs) +3. [Tracing Jina AI requests](/product/observability/traces) +4. [Setup a fallback from OpenAI Embeddings to Jina AI](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/lambda.mdx b/virtual_key_old/integrations/llms/lambda.mdx new file mode 100644 index 00000000..ead52503 --- /dev/null +++ b/virtual_key_old/integrations/llms/lambda.mdx @@ -0,0 +1,461 @@ +--- +title: 'Lambda Labs' +description: 'Integrate Lambda with Portkey AI for seamless completions, prompt management, and advanced features like streaming and function calling.' +--- + + +**Portkey Provider Slug:** `lambda` + + +## Overview + +Portkey offers native integrations with [Lambda](https://lambdalabs.com/) for Node.js, Python, and REST APIs. By combining Portkey with Lambda, you can create production-grade AI applications with enhanced reliability, observability, and advanced features. + + + + Explore the official Lambda documentation for comprehensive details on their APIs and models. + + + +## Getting Started + + + + Visit the [Lambda dashboard](https://cloud.lambdalabs.com/api-keys) to generate your API key. + + + + Portkey's virtual key vault simplifies your interaction with Lambda. Virtual keys act as secure aliases for your actual API keys, offering enhanced security and easier management through [budget limits](/product/ai-gateway/usage-limits) to control your API usage. + + Use the Portkey app to create a [virtual key](/product/ai-gateway/virtual-keys) associated with your Lambda API key. + + + + Now that you have your virtual key, set up the Portkey client: + + ### Portkey Hosted App + Use the Portkey API key and the Lambda virtual key to initialize the client in your preferred programming language. + + + ```python Python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Lambda + ) + ``` + + ```javascript Node.js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your Lambda Virtual Key + }) + ``` + + + ### Open Source Use + Alternatively, use Portkey's Open Source AI Gateway to enhance your app's reliability with minimal code: + + + ```python Python + from portkey_ai import Portkey, PORTKEY_GATEWAY_URL + + portkey = Portkey( + api_key="dummy", # Replace with your Portkey API key + base_url=PORTKEY_GATEWAY_URL, + Authorization="LAMBDA_API_KEY", # Replace with your Lambda API Key + provider="lambda" + ) + ``` + + ```javascript Node.js + import Portkey, { PORTKEY_GATEWAY_URL } from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "dummy", // Replace with your Portkey API key + baseUrl: PORTKEY_GATEWAY_URL, + Authorization: "LAMBDA_API_KEY", // Replace with your Lambda API Key + provider: "lambda" + }) + ``` + + + + +๐Ÿ”ฅ That's it! You've integrated Portkey into your application with just a few lines of code. Now let's explore making requests using the Portkey client. + +## Supported Models + + + +- deepseek-coder-v2-lite-instruct +- dracarys2-72b-instruct +- hermes3-405b +- hermes3-405b-fp8-128k +- hermes3-70b +- hermes3-8b +- lfm-40b +- llama3.1-405b-instruct-fp8 +- llama3.1-70b-instruct-fp8 +- llama3.1-8b-instruct +- llama3.2-3b-instruct +- llama3.1-nemotron-70b-instruct + + + + + + +## Supported Endpoints and Parameters + +| Endpoint | Supported Parameters | +| ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `chatComplete` | messages, max_tokens, temperature, top_p, stream, presence_penalty, frequency_penalty | +| `complete` | model, prompt, max_tokens, temperature, top_p, n, stream, logprobs, echo, stop, presence_penalty, frequency_penalty, best_of, logit_bias, user, seed, suffix | + + +## Lambda Supported Features + +### Chat Completions + +Generate chat completions using Lambda models through Portkey: + + +```python Python +completion = portkey.chat.completions.create( + messages=[{"role": "user", "content": "Say this is a test"}], + model="llama3.1-8b-instruct" +) + +print(completion.choices[0].message.content) +``` + +```javascript Node.js +const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'llama3.1-8b-instruct', +}); + +console.log(chatCompletion.choices[0].message.content); +``` + +```curl REST +curl -X POST "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -d '{ + "messages": [{"role": "user", "content": "Say this is a test"}], + "model": "llama3.1-8b-instruct" + }' +``` + + +### Streaming + +Stream responses for real-time output in your applications: + + +```python Python +chat_complete = portkey.chat.completions.create( + model="llama3.1-8b-instruct", + messages=[{"role": "user", "content": "Say this is a test"}], + stream=True +) + +for chunk in chat_complete: + print(chunk.choices[0].delta.content or "", end="", flush=True) +``` + +```javascript Node.js +const stream = await portkey.chat.completions.create({ + model: 'gpt-4', + messages: [{ role: 'user', content: 'Say this is a test' }], + stream: true, +}); + +for await (const chunk of stream) { + process.stdout.write(chunk.choices[0]?.delta?.content || ''); +} +``` + +```curl REST +curl -X POST "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -d '{ + "model": "llama3.1-8b-instruct", + "messages": [{"role": "user", "content": "Say this is a test"}], + "stream": true + }' +``` + + + +### Function Calling + +Leverage Lambda's function calling capabilities through Portkey: + + +```javascript Node.js +let tools = [{ + type: "function", + function: { + name: "getWeather", + description: "Get the current weather", + parameters: { + type: "object", + properties: { + location: { type: "string", description: "City and state" }, + unit: { type: "string", enum: ["celsius", "fahrenheit"] } + }, + required: ["location"] + } + } +}]; + +let response = await portkey.chat.completions.create({ + model: "llama3.1-8b-instruct", + messages: [ + { role: "system", content: "You are a helpful assistant." }, + { role: "user", content: "What's the weather like in Delhi - respond in JSON" } + ], + tools, + tool_choice: "auto", +}); + +console.log(response.choices[0].finish_reason); +``` + +```python Python +tools = [{ + "type": "function", + "function": { + "name": "getWeather", + "description": "Get the current weather", + "parameters": { + "type": "object", + "properties": { + "location": {"type": "string", "description": "City and state"}, + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]} + }, + "required": ["location"] + } + } +}] + +response = portkey.chat.completions.create( + model="llama3.1-8b-instruct", + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "What's the weather like in Delhi - respond in JSON"} + ], + tools=tools, + tool_choice="auto" +) + +print(response.choices[0].finish_reason) +``` + +```curl REST +curl -X POST "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -d '{ + "model": "llama3.1-8b-instruct", + "messages": [ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "What'\''s the weather like in Delhi - respond in JSON"} + ], + "tools": [{ + "type": "function", + "function": { + "name": "getWeather", + "description": "Get the current weather", + "parameters": { + "type": "object", + "properties": { + "location": {"type": "string", "description": "City and state"}, + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]} + }, + "required": ["location"] + } + } + }], + "tool_choice": "auto" + }' +``` + + + + +# Portkey's Advanced Features + +## Track End-User IDs + +Portkey allows you to track user IDs passed with the user parameter in Lambda requests, enabling you to monitor user-level costs, requests, and more: + + +```python Python +response = portkey.chat.completions.create( + model="llama3.1-8b-instruct", + messages=[{"role": "user", "content": "Say this is a test"}], + user="user_123456" +) +``` + +```javascript Node.js +const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: "user", content: "Say this is a test" }], + model: "llama3.1-8b-instruct", + user: "user_12345", +}); +``` + +```curl REST +curl -X POST "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -d '{ + "model": "llama3.1-8b-instruct", + "messages": [{"role": "user", "content": "Say this is a test"}], + "user": "user_123456" + }' +``` + + +When you include the user parameter in your requests, Portkey logs will display the associated user ID, as shown in the image below: + +Portkey Logs with User ID + +In addition to the `user` parameter, Portkey allows you to send arbitrary custom metadata with your requests. This powerful feature enables you to associate additional context or information with each request, which can be useful for analysis, debugging, or other custom use cases. + + + + Explore how to use custom metadata to enhance your request tracking and analysis. + + + +## Using The Gateway Config + +Here's a simplified version of how to use Portkey's Gateway Configuration: + + + + You can create a Gateway configuration using the Portkey Config Dashboard or by writing a JSON configuration in your code. In this example, requests are routed based on the user's subscription plan (paid or free). + + ```json + config = { + "strategy": { + "mode": "conditional", + "conditions": [ + { + "query": { "metadata.user_plan": { "$eq": "paid" } }, + "then": "llama3.1" + }, + { + "query": { "metadata.user_plan": { "$eq": "free" } }, + "then": "gpt-3.5" + } + ], + "default": "gpt-3.5" + }, + "targets": [ + { + "name": "llama3.1", + "virtual_key": "xx" + }, + { + "name": "gpt-3.5", + "virtual_key": "yy" + } + ] + } + ``` + + + + When a user makes a request, it will pass through Portkey's AI Gateway. Based on the configuration, the Gateway routes the request according to the user's metadata. + Conditional Routing Diagram + + + + Pass the Gateway configuration to your Portkey client. You can either use the config object or the Config ID from Portkey's hosted version. + + + ```python Python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", + virtual_key="VIRTUAL_KEY", + config=portkey_config + ) + ``` + + ```javascript Node.js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "VIRTUAL_KEY", + config: portkeyConfig + }) + ``` + + + + +That's it! Portkey seamlessly allows you to make your AI app more robust using built-in gateway features. Learn more about advanced gateway features: + + + + Distribute requests across multiple targets based on defined weights. + + + Automatically switch to backup targets if the primary target fails. + + + Route requests to different targets based on specified conditions. + + + Enable caching of responses to improve performance and reduce costs. + + + +## Guardrails + +Portkey's AI gateway enables you to enforce input/output checks on requests by applying custom hooks before and after processing. Protect your user's/company's data by using PII guardrails and many more available on Portkey Guardrails: + +```json +{ + "virtual_key":"lambda-xxx", + "before_request_hooks": [{ + "id": "input-guardrail-id-xx" + }], + "after_request_hooks": [{ + "id": "output-guardrail-id-xx" + }] +} +``` + + + Explore Portkey's guardrail features to enhance the security and reliability of your AI applications. + + +## Next Steps + +The complete list of features supported in the SDK are available in our comprehensive documentation: + + + Explore the full capabilities of the Portkey SDK and how to leverage them in your projects. + + +--- + + + + +For the most up-to-date information on supported features and endpoints, please refer to our [API Reference](/docs/api-reference/introduction). diff --git a/virtual_key_old/integrations/llms/lemon-fox.mdx b/virtual_key_old/integrations/llms/lemon-fox.mdx new file mode 100644 index 00000000..ccda305b --- /dev/null +++ b/virtual_key_old/integrations/llms/lemon-fox.mdx @@ -0,0 +1,458 @@ +--- +title: 'Lemonfox-AI' +description: 'Integrate LemonFox with Portkey for seamless completions, prompt management, and advanced features like streaming, function calling, and fine-tuning.' +--- + + +**Portkey Provider Slug:** `lemonfox-ai` + + +## Overview + +Portkey offers native integrations with [LemonFox-AI](https://www.lemonfox.ai/) for Node.js, Python, and REST APIs. By combining Portkey with Lemonfox AI, you can create production-grade AI applications with enhanced reliability, observability, and advanced features. + + + + Explore the official Lemonfox AI documentation for comprehensive details on their APIs and models. + + + +## Getting Started + + + + Visit the [LemonFox dashboard](https://www.lemonfox.ai/apis/keys) to generate your API key. + + + + Portkey's virtual key vault simplifies your interaction with LemonFox AI. Virtual keys act as secure aliases for your actual API keys, offering enhanced security and easier management through [budget limits](/product/ai-gateway/usage-limits) to control your API usage. + + Use the Portkey app to create a [virtual key](/product/ai-gateway/virtual-keys) associated with your Lemonfox AI API key. + + + + Now that you have your virtual key, set up the Portkey client: + + ### Portkey Hosted App + Use the Portkey API key and the LemonFox AI virtual key to initialize the client in your preferred programming language. + + + ```python Python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for LemonFox AI + ) + ``` + + ```javascript Node.js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your LemonFox AI Virtual Key + }) + ``` + + + ### Open Source Use + Alternatively, use Portkey's Open Source AI Gateway to enhance your app's reliability with minimal code: + + + ```python Python + from portkey_ai import Portkey, PORTKEY_GATEWAY_URL + + portkey = Portkey( + api_key="dummy", # Replace with your Portkey API key + base_url=PORTKEY_GATEWAY_URL, + Authorization="LEMONFOX_AI_API_KEY", # Replace with your Lemonfox AI API Key + provider="lemonfox-ai" + ) + ``` + + ```javascript Node.js + import Portkey, { PORTKEY_GATEWAY_URL } from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "dummy", // Replace with your Portkey API key + baseUrl: PORTKEY_GATEWAY_URL, + Authorization: "LEMONFOX_AI_API_KEY", // Replace with your Lemonfox AI API Key + provider: "lemonfox-ai" + }) + ``` + + + + +๐Ÿ”ฅ That's it! You've integrated Portkey into your application with just a few lines of code. Now let's explore making requests using the Portkey client. + +## Supported Models + + + +`Chat` - Mixtral AI, Llama 3.1 8B and Llama 3.1 70B + + +`Speech-To-Text`- Whisper large-v3 + + +`Vision`- Stable Diffusion XL (SDXL) + + + + + +## Supported Endpoints and Parameters + +| Endpoint | Supported Parameters | +| ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `chatComplete` | messages, max_tokens, temperature, top_p, stream, presence_penalty, frequency_penalty | +| `imageGenerate` | prompt, response_format, negative_prompt, size, n | +| `createTranscription` | translate, language, prompt, response_format, file | | + + +## Lemonfox AI Supported Features + +### Chat Completions + +Generate chat completions using Lemonfox AI models through Portkey: + + +```python Python +completion = portkey.chat.completions.create( + messages=[{"role": "user", "content": "Say this is a test"}], + model="llama-8b-chat" +) + +print(completion.choices[0].message.content) +``` + +```javascript Node.js +const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'llama-8b-chat', +}); + +console.log(chatCompletion.choices[0].message.content); +``` + +```curl REST +curl -X POST "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -d '{ + "messages": [{"role": "user", "content": "Say this is a test"}], + "model": "llama-8b-chat" + }' +``` + + +### Streaming + +Stream responses for real-time output in your applications: + + +```python Python +chat_complete = portkey.chat.completions.create( + model="llama-8b-chat", + messages=[{"role": "user", "content": "Say this is a test"}], + stream=True +) + +for chunk in chat_complete: + print(chunk.choices[0].delta.content or "", end="", flush=True) +``` + +```javascript Node.js +const stream = await portkey.chat.completions.create({ + model: 'gpt-4', + messages: [{ role: 'user', content: 'Say this is a test' }], + stream: true, +}); + +for await (const chunk of stream) { + process.stdout.write(chunk.choices[0]?.delta?.content || ''); +} +``` + +```curl REST +curl -X POST "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -d '{ + "model": "llama-8b-chat", + "messages": [{"role": "user", "content": "Say this is a test"}], + "stream": true + }' +``` + + +### Image Generate + +Here's how you can generate images using Lemonfox AI + + + + + +```python Python +from portkey_ai import Portkey + +client = Portkey( + api_key = "PORTKEY_API_KEY", + virtual_key = "PROVIDER_VIRTUAL_KEY" +) + +client.images.generate( + prompt="A cute baby sea otter", + n=1, + size="1024x1024" +) + +``` + +```javascript Node.js +import Portkey from 'portkey-ai'; + +const client = new Portkey({ + apiKey: 'PORTKEY_API_KEY', + virtualKey: 'PROVIDER_VIRTUAL_KEY' +}); + +async function main() { + const image = await client.images.generate({prompt: "A cute baby sea otter" }); + + console.log(image.data); +} +main(); + +``` + +```curl REST +curl https://api.portkey.ai/v1/images/generations \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-virtual-key: $PORTKEY_PROVIDER_VIRTUAL_KEY" \ + -d '{ + "prompt": "A cute baby sea otter", + "n": 1, + "size": "1024x1024" + }' + +``` + + +### Transcription + +Portkey supports both `Transcription` methods for STT models: + + +```python Python +audio_file= open("/path/to/file.mp3", "rb") + +# Transcription +transcription = portkey.audio.transcriptions.create( + model="whisper-1", + file=audio_file +) +print(transcription.text) +``` + +```javascript Node.js +import fs from "fs"; + +// Transcription +async function transcribe() { + const transcription = await portkey.audio.transcriptions.create({ + file: fs.createReadStream("/path/to/file.mp3"), + model: "whisper-1", + }); + console.log(transcription.text); +} +transcribe(); + + +``` + +```curl REST +# Transcription +curl -X POST "https://api.portkey.ai/v1/audio/transcriptions" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -H "Content-Type: multipart/form-data" \ + -F "file=@/path/to/file.mp3" \ + -F "model=whisper-1" + +``` + + + + + +# Portkey's Advanced Features + +## Track End-User IDs + +Portkey allows you to track user IDs passed with the user parameter in Lemonfox AI requests, enabling you to monitor user-level costs, requests, and more: + + +```python Python +response = portkey.chat.completions.create( + model="llama-8b-chat", + messages=[{"role": "user", "content": "Say this is a test"}], + user="user_123456" +) +``` + +```javascript Node.js +const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: "user", content: "Say this is a test" }], + model: "llama-8b-chat", + user: "user_12345", +}); +``` + +```curl REST +curl -X POST "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -d '{ + "model": "llama-8b-chat", + "messages": [{"role": "user", "content": "Say this is a test"}], + "user": "user_123456" + }' +``` + + +When you include the user parameter in your requests, Portkey logs will display the associated user ID, as shown in the image below: + +Portkey Logs with User ID + +In addition to the `user` parameter, Portkey allows you to send arbitrary custom metadata with your requests. This powerful feature enables you to associate additional context or information with each request, which can be useful for analysis, debugging, or other custom use cases. + + + + Explore how to use custom metadata to enhance your request tracking and analysis. + + + +## Using The Gateway Config + +Here's a simplified version of how to use Portkey's Gateway Configuration: + + + + You can create a Gateway configuration using the Portkey Config Dashboard or by writing a JSON configuration in your code. In this example, requests are routed based on the user's subscription plan (paid or free). + + ```json + config = { + "strategy": { + "mode": "conditional", + "conditions": [ + { + "query": { "metadata.user_plan": { "$eq": "paid" } }, + "then": "llama-8b-chat" + }, + { + "query": { "metadata.user_plan": { "$eq": "free" } }, + "then": "gpt-3.5" + } + ], + "default": "base-gpt4" + }, + "targets": [ + { + "name": "llama-8b-chat", + "virtual_key": "xx" + }, + { + "name": "gpt-3.5", + "virtual_key": "yy" + } + ] + } + ``` + + + + When a user makes a request, it will pass through Portkey's AI Gateway. Based on the configuration, the Gateway routes the request according to the user's metadata. + Conditional Routing Diagram + + + + Pass the Gateway configuration to your Portkey client. You can either use the config object or the Config ID from Portkey's hosted version. + + + ```python Python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", + virtual_key="VIRTUAL_KEY", + config=portkey_config + ) + ``` + + ```javascript Node.js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "VIRTUAL_KEY", + config: portkeyConfig + }) + ``` + + + + +That's it! Portkey seamlessly allows you to make your AI app more robust using built-in gateway features. Learn more about advanced gateway features: + + + + Distribute requests across multiple targets based on defined weights. + + + Automatically switch to backup targets if the primary target fails. + + + Route requests to different targets based on specified conditions. + + + Enable caching of responses to improve performance and reduce costs. + + + +## Guardrails + +Portkey's AI gateway enables you to enforce input/output checks on requests by applying custom hooks before and after processing. Protect your user's/company's data by using PII guardrails and many more available on Portkey Guardrails: + +```json +{ + "virtual_key":"lemonfox-ai-xxx", + "before_request_hooks": [{ + "id": "input-guardrail-id-xx" + }], + "after_request_hooks": [{ + "id": "output-guardrail-id-xx" + }] +} +``` + + + Explore Portkey's guardrail features to enhance the security and reliability of your AI applications. + + +## Next Steps + +The complete list of features supported in the SDK are available in our comprehensive documentation: + + + Explore the full capabilities of the Portkey SDK and how to leverage them in your projects. + + +--- + + + +For the most up-to-date information on supported features and endpoints, please refer to our [API Reference](/docs/api-reference/introduction). diff --git a/virtual_key_old/integrations/llms/lepton.mdx b/virtual_key_old/integrations/llms/lepton.mdx new file mode 100644 index 00000000..67f69344 --- /dev/null +++ b/virtual_key_old/integrations/llms/lepton.mdx @@ -0,0 +1,188 @@ +--- +title: "Lepton AI" +--- + +Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including [Lepton AI APIs](https://www.lepton.ai/docs/guides). + +With Portkey, you can take advantage of features like fast AI gateway access, observability, prompt management, and more, all while ensuring the secure management of your LLM API keys through a [virtual key](/product/ai-gateway/virtual-keys) system. + +Provider Slug. `lepton` + + +## Portkey SDK Integration with Lepton AI Models + +Portkey provides a consistent API to interact with models from various providers. To integrate Lepton AI with Portkey: + +### 1\. Install the Portkey SDK + +Add the Portkey SDK to your application to interact with Lepton AI's API through Portkey's gateway. + + + + ```sh + npm install --save portkey-ai + ``` + + + ```sh + pip install portkey-ai + ``` + + + +### 2\. Initialize Portkey with the Virtual Key + +To use Lepton AI with Portkey, [get your API key from Lepton AI](https://console.lepton.ai/), then add it to Portkey to create the virtual key. + + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your Lepton AI Virtual Key + }) + ``` + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Lepton + ) + ``` + + + +### 3\. Invoke Chat Completions with Lepton AI + +Use the Portkey instance to send requests to Lepton AI. You can also override the virtual key directly in the API call if needed. + + + + ```js + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'llama-3-8b-sft-v1', + }); + + console.log(chatCompletion.choices); + ``` + + + ```python + completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model= 'llama-3-8b-sft-v1' + ) + + print(completion) + ``` + + + ```sh + curl --location 'https://api.portkey.ai/v1/chat/completions' \ + -H 'Content-Type: application/json' \ + -H 'x-portkey-api-key: PORTKEY_API_KEY' \ + -H 'x-portkey-virtual-key: VIRTUAL_KEY' \ + --data '{ + "model": "llama-3-8b-sft-v1", + "messages": [ + { + "role": "user", + "content": "Say this is a test" + } + ] + }' + ``` + + + +## Speech-to-Text (Transcription) + +Lepton AI provides speech-to-text capabilities through Portkey's unified API: + + + + ```js + import fs from 'fs'; + + const transcription = await portkey.audio.transcriptions.create({ + file: fs.createReadStream('audio.mp3'), + model: 'whisper-large-v3', + }); + + console.log(transcription.text); + ``` + + + ```python + with open("audio.mp3", "rb") as audio_file: + transcription = portkey.audio.transcriptions.create( + file=audio_file, + model="whisper-large-v3" + ) + + print(transcription.text) + ``` + + + +## Advanced Features + +### Streaming Responses + +Lepton AI supports streaming responses to provide real-time generation: + + + + ```js + const stream = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Write a story about a robot' }], + model: 'llama-3-8b-sft-v1', + stream: true, + }); + + for await (const chunk of stream) { + process.stdout.write(chunk.choices[0]?.delta?.content || ''); + } + ``` + + + ```python + stream = portkey.chat.completions.create( + messages=[{"role": "user", "content": "Write a story about a robot"}], + model="llama-3-8b-sft-v1", + stream=True + ) + + for chunk in stream: + if chunk.choices[0].delta.content: + print(chunk.choices[0].delta.content, end="") + ``` + + + + +## Managing Lepton AI Prompts + +You can manage all prompts to Lepton AI in the [Prompt Library](/product/prompt-library). All the current models of Lepton AI are supported and you can easily start testing different prompts. + +Once you're ready with your prompt, you can use the `portkey.prompts.completions.create` interface to use the prompt in your application. + +## Next Steps + +The complete list of features supported in the SDK are available on the link below. + + + + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your Lepton AI requests](/product/ai-gateway/configs) +3. [Tracing Lepton AI requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to Lepton AI APIs](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/lingyi-01.ai.mdx b/virtual_key_old/integrations/llms/lingyi-01.ai.mdx new file mode 100644 index 00000000..800dcb6c --- /dev/null +++ b/virtual_key_old/integrations/llms/lingyi-01.ai.mdx @@ -0,0 +1,109 @@ +--- +title: "Lingyi (01.ai)" +--- + +Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including [Lingyi (01.ai).](https://01.ai) + +With Portkey, you can take advantage of features like fast AI gateway access, observability, prompt management, and more, all while ensuring the secure management of your LLM API keys through a [virtual key](/product/ai-gateway/virtual-keys) system. + +Provider Slug: `lingyi` + +## Portkey SDK Integration with Lingyi Models + +Portkey provides a consistent API to interact with models from various providers. To integrate Lingyi with Portkey: + +### 1\. Install the Portkey SDK + +Add the Portkey SDK to your application to interact with Lingyi AI's API through Portkey's gateway. + + + ```sh + npm install --save portkey-ai + ``` + + + ```sh + pip install portkey-ai + ``` + + + + + + +### 2\. Initialize Portkey with the Virtual Key + +To use Lingyi with Portkey, [get your API key from here](https://platform.lingyiwanwu.com/apikeys), then add it to Portkey to create the virtual key. + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your Lingyi Virtual Key + }) + ``` + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Groq + ) + ``` + + + + + + + +### 3\. Invoke Chat Completions with Lingyi + +Use the Portkey instance to send requests to Lingyi. You can also override the virtual key directly in the API call if needed. + + + ```js + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'Yi-Large-Preview', + }); + + console.log(chatCompletion.choices); + ``` + + + ```python + completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model= 'mistral-medium' + ) + + print(completion) + ``` + + + + + + + +## Managing Lingyi Prompts + +You can manage all prompts to Lingyi in the [Prompt Library](/product/prompt-library). All the current models of Lingyi are supported and you can easily start testing different prompts. + +Once you're ready with your prompt, you can use the `portkey.prompts.completions.create` interface to use the prompt in your application. + +The complete list of features supported in the SDK are available on the link below. + + + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your Lingyi requests](/product/ai-gateway/configs) +3. [Tracing Lingyi requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to Lingyi APIs](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/local-ai.mdx b/virtual_key_old/integrations/llms/local-ai.mdx new file mode 100644 index 00000000..745c5add --- /dev/null +++ b/virtual_key_old/integrations/llms/local-ai.mdx @@ -0,0 +1,172 @@ +--- +title: "LocalAI" +--- + +Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including your **locally hosted models through** [**LocalAI**](https://localai.io/). + +## Portkey SDK Integration with LocalAI + +### 1\. Install the Portkey SDK + + + ```sh + npm install --save portkey-ai + ``` + + + +```sh +pip install portkey-ai +``` + + + + + + +### 2\. Initialize Portkey with LocalAI URL + +First, ensure that your API is externally accessible. If you're running the API on `http://localhost`, consider using a tool like `ngrok` to create a public URL. Then, instantiate the Portkey client by adding your LocalAI URL (along with the version identifier) to the `customHost` property, and add the provider name as `openai`. + +**Note:** Don't forget to include the version identifier (e.g., `/v1`) in the `customHost` URL + + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + provider: "openai", + customHost: "https://7cc4-3-235-157-146.ngrok-free.app/v1" // Your LocalAI ngrok URL + }) + ``` + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + provider="openai", + custom_host="https://7cc4-3-235-157-146.ngrok-free.app/v1" # Your LocalAI ngrok URL + ) + ``` + + + + + + +Portkey currently supports all endpoints that adhere to the OpenAI specification. This means, you can access and observe any of your LocalAI models that are exposed through OpenAI-compliant routes. + +[List of supported endpoints here](/integrations/llms/local-ai#localai-endpoints-supported). + +### 3\. Invoke Chat Completions + +Use the Portkey SDK to invoke chat completions from your LocalAI model, just as you would with any other provider. + + + ```js + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'ggml-koala-7b-model-q4_0-r2.bin', + }); + + console.log(chatCompletion.choices); + ``` + + + ```python + completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model= 'ggml-koala-7b-model-q4_0-r2.bin' + ) + + print(completion) + ``` + + + + + +## [Using Virtual Keys](https://app.portkey.ai/virtual-keys) + +Virtual Keys serve as Portkey's unified authentication system for all LLM interactions, simplifying the use of multiple providers and Portkey features within your application. For self-hosted LLMs, you can configure custom authentication requirements including authorization keys, bearer tokens, or any other headers needed to access your model: + + + + + +1. Navigate to [Virtual Keys](https://app.portkey.ai/virtual-keys) in your Portkey dashboard +2. Click **"Add Key"** and enable the **"Local/Privately hosted provider"** toggle +3. Configure your deployment: + - Select the matching provider API specification (typically `OpenAI`) + - Enter your model's base URL in the `Custom Host` field + - Add required authentication headers and their values +4. Click **"Create"** to generate your virtual key + +You can now use this virtual key in your requests: + + + + ```js + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "YOUR_SELF_HOSTED_LLM_VIRTUAL_KEY" + + async function main() { + const response = await client.chat.completions.create({ + messages: [{ role: "user", content: "Bob the builder.." }], + model: "your-self-hosted-model-name", + }); + + console.log(response.choices[0].message.content); + }) + ``` + + + ```python + portkey = Portkey( + api_key="PORTKEY_API_KEY", + virtual_key="YOUR_SELF_HOSTED_LLM_VIRTUAL_KEY" + ) + + response = portkey.chat.completions.create( + model="your-self-hosted-model-name", + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "Hello!"} + ] + + print(response) + ) + ``` + + + +For more information about managing self-hosted LLMs with Portkey, see [Bring Your Own LLM](/integrations/llms/byollm). + + +## LocalAI Endpoints Supported +| Endpoint | Resource | +| :----------------------------------------------- | :---------------------------------------------------------------- | +| /chat/completions (Chat, Vision, Tools support) | [Doc](/provider-endpoints/chat) | +| /images/generations | [Doc](/provider-endpoints/images/create-image) | +| /embeddings | [Doc](/provider-endpoints/embeddings) | +| /audio/transcriptions | [Doc](/product/ai-gateway/multimodal-capabilities/speech-to-text) | + +## Next Steps + +Explore the complete list of features supported in the SDK: + + + +--- + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your Ollama requests](/product/ai-gateway/universal-api#ollama-in-configs) +3. [Tracing Ollama requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to Ollama APIs](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/mistral-ai.mdx b/virtual_key_old/integrations/llms/mistral-ai.mdx new file mode 100644 index 00000000..bbf445b4 --- /dev/null +++ b/virtual_key_old/integrations/llms/mistral-ai.mdx @@ -0,0 +1,280 @@ +--- +title: "Mistral AI" +--- + +Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including [Mistral AI APIs](https://docs.mistral.ai/api/). + +With Portkey, you can take advantage of features like fast AI gateway access, observability, prompt management, and more, all while ensuring the secure management of your LLM API keys through a [virtual key](/product/ai-gateway/virtual-keys) system. + + +Provider Slug. `mistral-ai` + +## Portkey SDK Integration with Mistral AI Models + +Portkey provides a consistent API to interact with models from various providers. To integrate Mistral AI with Portkey: + +### 1\. Install the Portkey SDK + +Add the Portkey SDK to your application to interact with Mistral AI's API through Portkey's gateway. + + + ```sh + npm install --save portkey-ai + ``` + + + ```sh + pip install portkey-ai + ``` + + + + + + + +### 2\. Initialize Portkey with the Virtual Key + +To use Mistral AI with Portkey, [get your API key from here](https://console.mistral.ai/api-keys/ ), then add it to Portkey to create the virtual key. + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your Mistral AI Virtual Key + }) + ``` + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Mistral AI + ) + ``` + + + + + +### **3.1\. Invoke Chat Completions with** Mistral AI + +Use the Portkey instance to send requests to Mistral AI. You can also override the virtual key directly in the API call if needed. + +You can also call the new Codestral model here! + + + ```js + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'codestral-latest', + }); + + console.log(chatCompletion.choices); + ``` + + + ```python + completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model= 'codestral-latest' + ) + + print(completion) + ``` + + + + + + +--- + +## Invoke Codestral Endpoint + +Using Portkey, you can also call Mistral API's new Codestral endpoint. Just pass the Codestral URL `https://codestral.mistral.ai/v1` with the `customHost` property. + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "MISTRAL_VIRTUAL_KEY", + customHost: "https://codestral.mistral.ai/v1" + }) + + const codeCompletion = await portkey.chat.completions.create({ + model: "codestral-latest", + messages: [{"role": "user", "content": "Write a minimalist Python code to validate the proof for the special number 1729"}] + }); + + console.log(codeCompletion.choices[0].message.content); + ``` + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", + virtual_key="MISTRAL_VIRTUAL_KEY", + custom_host="https://codestral.mistral.ai/v1" + ) + + code_completion = portkey.chat.completions.create( + model="codestral-latest", + messages=[{"role": "user", "content": "Write a minimalist Python code to validate the proof for the special number 1729"}] + ) + + print(code_completion.choices[0].message.content) + ``` + + + + + + + +#### Your Codestral requests will show up on Portkey logs with the code snippets rendered beautifully! + + + + + +## Codestral v/s Mistral API Endpoint + +Here's a handy guide for when you might want to make your requests to the Codestral endpoint v/s the original Mistral API endpoint: + + + + +[For more, check out Mistral's Code Generation guide here](https://docs.mistral.ai/capabilities/code%5Fgeneration/#operation/listModels). + +--- + +## Managing Mistral AI Prompts + +You can manage all prompts to Mistral AI in the [Prompt Library](/product/prompt-library). All the current models of Mistral AI are supported and you can easily start testing different prompts. + +Once you're ready with your prompt, you can use the `portkey.prompts.completions.create` interface to use the prompt in your application. + + +### Mistral Tool Calling +Tool calling feature lets models trigger external tools based on conversation context. You define available functions, the model chooses when to use them, and your application executes them and returns results. + +Portkey supports Mistral Tool Calling and makes it interoperable across multiple providers. With Portkey Prompts, you can templatize various your prompts & tool schemas as well. + + + + +```javascript Get Weather Tool +let tools = [{ + type: "function", + function: { + name: "getWeather", + description: "Get the current weather", + parameters: { + type: "object", + properties: { + location: { type: "string", description: "City and state" }, + unit: { type: "string", enum: ["celsius", "fahrenheit"] } + }, + required: ["location"] + } + } +}]; + +let response = await portkey.chat.completions.create({ + model: "your_mistral_model_name", + messages: [ + { role: "system", content: "You are a helpful assistant." }, + { role: "user", content: "What's the weather like in Delhi - respond in JSON" } + ], + tools, + tool_choice: "auto", +}); + +console.log(response.choices[0].finish_reason); +``` + + +```python Get Weather Tool +tools = [{ + "type": "function", + "function": { + "name": "getWeather", + "description": "Get the current weather", + "parameters": { + "type": "object", + "properties": { + "location": {"type": "string", "description": "City and state"}, + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]} + }, + "required": ["location"] + } + } +}] + +response = portkey.chat.completions.create( + model="your_mistral_model_name", + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "What's the weather like in Delhi - respond in JSON"} + ], + tools=tools, + tool_choice="auto" +) + +print(response.choices[0].finish_reason) +``` + + +```curl Get Weather Tool +curl -X POST "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -d '{ + "model": "your_mistral_model_name", + "messages": [ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "What'\''s the weather like in Delhi - respond in JSON"} + ], + "tools": [{ + "type": "function", + "function": { + "name": "getWeather", + "description": "Get the current weather", + "parameters": { + "type": "object", + "properties": { + "location": {"type": "string", "description": "City and state"}, + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]} + }, + "required": ["location"] + } + } + }], + "tool_choice": "auto" + }' +``` + + + + +## Next Steps + +The complete list of features supported in the SDK are available on the link below. + + + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your Mistral AI](/product/ai-gateway/configs)[ requests](/product/ai-gateway/configs) +3. [Tracing Mistral AI requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to Mistral AI APIs](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/monster-api.mdx b/virtual_key_old/integrations/llms/monster-api.mdx new file mode 100644 index 00000000..cf03283d --- /dev/null +++ b/virtual_key_old/integrations/llms/monster-api.mdx @@ -0,0 +1,105 @@ +--- +title: "Monster API" +description: "MonsterAPIs provides access to generative AI model APIs at 80% lower costs. Connect to MonsterAPI LLM APIs seamlessly through Portkey's AI gateway." +--- + +Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including [MonsterAPI APIs](https://developer.monsterapi.ai/docs/getting-started). + +With Portkey, you can take advantage of features like fast AI gateway access, observability, prompt management, and more, all while ensuring the secure management of your LLM API keys through a [virtual key](/product/ai-gateway/virtual-keys) system. + +Provider Slug. `monsterapi` + +## Portkey SDK Integration with MonsterAPI Models + +Portkey provides a consistent API to interact with models from various providers. To integrate MonsterAPI with Portkey: + +### 1\. Install the Portkey SDK + +Add the Portkey SDK to your application to interact with MonsterAPI's API through Portkey's gateway. + + + ```sh + npm install --save portkey-ai + ``` + + + ```sh + pip install portkey-ai + ``` + + + + + + + +### 2\. Initialize Portkey with the Virtual Key + +To use Monster API with Portkey, [get your API key from here,](https://monsterapi.ai/user/dashboard) then add it to Portkey to create the virtual key. + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your MonsterAPI Virtual Key + }) + ``` + + + + + + + +### **3\. Invoke Chat Completions with** MonsterAPI + +Use the Portkey instance to send requests to MonsterAPI. You can also override the virtual key directly in the API call if needed. + + + ```js + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'TinyLlama/TinyLlama-1.1B-Chat-v1.0', + }); + + console.log(chatCompletion.choices); + ``` + + + ```python + completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model= 'mistral-medium' + ) + + print(completion) + ``` + + + + + +## Managing MonsterAPI Prompts + +You can manage all prompts to MonsterAPI in the [Prompt Library](/product/prompt-library). All the current models of MonsterAPI are supported and you can easily start testing different prompts. + +Once you're ready with your prompt, you can use the `portkey.prompts.completions.create` interface to use the prompt in your application. + +## Supported Models + +[Find the latest list of supported models here.](https://llm.monsterapi.ai/docs) + +## Next Steps + +The complete list of features supported in the SDK are available on the link below. + +[SDK](/api-reference/portkey-sdk-client) + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your MonsterAPI requests](/product/ai-gateway/configs) +3. [Tracing MonsterAPI requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to MonsterAPI APIs](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/moonshot.mdx b/virtual_key_old/integrations/llms/moonshot.mdx new file mode 100644 index 00000000..6e87d00d --- /dev/null +++ b/virtual_key_old/integrations/llms/moonshot.mdx @@ -0,0 +1,109 @@ +--- +title: "Moonshot" +--- + +Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including [Moonshot. ](https://moonshot.cn) + +With Portkey, you can take advantage of features like fast AI gateway access, observability, prompt management, and more, all while ensuring the secure management of your LLM API keys through a [virtual key](/product/ai-gateway/virtual-keys) system. + + +Provider Slug. `moonshot` + + +## Portkey SDK Integration with Moonshot Models + +Portkey provides a consistent API to interact with models from various providers. To integrate Moonshot with Portkey: + +### 1\. Install the Portkey SDK + +Add the Portkey SDK to your application to interact with Moonshot's API through Portkey's gateway. + + + ```sh + npm install --save portkey-ai + ``` + + + +```sh +pip install portkey-ai +``` + + + + + + +### 2\. Initialize Portkey with the Virtual Key + +To use Moonshot with Portkey, [get your API key from here,](https://moonshot.cn) then add it to Portkey to create the virtual key. + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your Moonshot Virtual Key + }) + ``` + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Groq + ) + ``` + + + + + + +### 3\. Invoke Chat Completions with Moonshot + +Use the Portkey instance to send requests to Moonshot. You can also override the virtual key directly in the API call if needed. + + + ```js + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'moonshot-v1-8k', + }); + + console.log(chatCompletion.choices);d + ``` + + + ```python + completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model= 'moonshot-v1-8k' + ) + + print(completion) + ``` + + + + + +## Managing Moonshot Prompts + +You can manage all prompts to Moonshot in the [Prompt Library](/product/prompt-library). All the current models of Moonshot are supported and you can easily start testing different prompts. + +Once you're ready with your prompt, you can use the `portkey.prompts.completions.create` interface to use the prompt in your application. + +The complete list of features supported in the SDK are available on the link below. + + + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your Moonshot requests](/product/ai-gateway/configs) +3. [Tracing Moonshot requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to Moonshot APIs](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/ncompass.mdx b/virtual_key_old/integrations/llms/ncompass.mdx new file mode 100644 index 00000000..b572d889 --- /dev/null +++ b/virtual_key_old/integrations/llms/ncompass.mdx @@ -0,0 +1,118 @@ +--- +title: "Ncompass" +--- + +Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including [nCompass APIs](https://www.ncompass.tech/). + +With Portkey, you can take advantage of features like fast AI gateway access, observability, prompt management, and more, all while ensuring the secure management of your LLM API keys through a [virtual key](/product/ai-gateway/virtual-keys) system. + +Provider Slug. `ncompass` + +## Portkey SDK Integration with nCompass Models + +Portkey provides a consistent API to interact with models from various providers. To integrate nCompass with Portkey: + +### 1\. Install the Portkey SDK + +Add the Portkey SDK to your application to interact with nCompass AI's API through Portkey's gateway. + + + + ```sh + npm install --save portkey-ai + ``` + + + ```sh + pip install portkey-ai + ``` + + + + + + + +### 2\. Initialize Portkey with the Virtual Key + +To use nCompass with Portkey, get your API key/JWT Token from the Snowflake Platform, then add it to Portkey to create the virtual key. + + + + +```js +import Portkey from 'portkey-ai' + +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your nCompass Virtual Key +}) +``` + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for nCompass + ) + ``` + + + + + + + +### **3\. Invoke Chat Completions with** nCompass + +Use the Portkey instance to send requests to nCompass. You can also override the virtual key directly in the API call if needed. + + + + + ```js + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'meta-llama/Llama-3.1-8B-Instruct', + }); + + console.log(chatCompletion.choices); + ``` + + + +```python +completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model= 'meta-llama/Llama-3.1-8B-Instruct' +) + +print(completion) +``` + + + + + + +## Managing nCompass Prompts + +You can manage all prompts to nCompass in the [Prompt Library](/product/prompt-library). All the current models of nCompass are supported and you can easily start testing different prompts. + +Once you're ready with your prompt, you can use the `portkey.prompts.completions.create` interface to use the prompt in your application. + + + + + + +--- + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your nCompass](/product/ai-gateway/configs)[ requests](/product/ai-gateway/configs) +3. [Tracing nCompass requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to nCompass APIs](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/nebius.mdx b/virtual_key_old/integrations/llms/nebius.mdx new file mode 100644 index 00000000..302ca0b8 --- /dev/null +++ b/virtual_key_old/integrations/llms/nebius.mdx @@ -0,0 +1,111 @@ +--- +title: 'Nebius' +--- +Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including [Nebius AI](https://nebius.ai/). + +With Portkey, you can take advantage of features like fast AI gateway access, observability, prompt management, and more, all while ensuring the secure management of your LLM API keys through a [virtual key](/product/ai-gateway/virtual-keys) system. + + +Provider Slug. `nebius` + +## Portkey SDK Integration with Nebius AI Models + +Portkey provides a consistent API to interact with models from various providers. To integrate Nebius AI with Portkey: + +### 1\. Install the Portkey SDK + +Add the Portkey SDK to your application to interact with Nebius AI's API through Portkey's gateway. + + + ```sh + npm install --save portkey-ai + ``` + + + ```sh + pip install portkey-ai + ``` + + + + + + + +### 2\. Initialize Portkey with the Virtual Key + +To use Nebius AI with Portkey, [get your API key from here](https://studio.nebius.com/settings/api-keys), then add it to Portkey to create the virtual key. + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your Nebius Virtual Key + }) + ``` + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Nebius + ) + ``` + + + + + + +### 3\. Invoke Chat Completions with Nebius AI + +Use the Portkey instance to send requests to Nebius AI. You can also override the virtual key directly in the API call if needed. + + + ```js + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'deepseek-ai/DeepSeek-V3', + }); + + console.log(chatCompletion.choices);d + ``` + + + ```python + completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model= 'deepseek-ai/DeepSeek-V3' + ) + + print(completion) + ``` + + + + + + + +## Managing Nebius AI Prompts + +You can manage all prompts to Nebius AI in the [Prompt Studio](/product/prompt-library). All the current models of Nebius AI are supported and you can easily start testing different prompts. + +Once you're ready with your prompt, you can use the `portkey.prompts.completions.create` interface to use the prompt in your application. + +## Supported Models + +The complete list of features supported in the SDK are available on the link below. + + + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your Nebius](/product/ai-gateway/configs) +3. [Tracing Nebius requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to Nebius APIs](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/nomic.mdx b/virtual_key_old/integrations/llms/nomic.mdx new file mode 100644 index 00000000..1e1463ca --- /dev/null +++ b/virtual_key_old/integrations/llms/nomic.mdx @@ -0,0 +1,95 @@ +--- +title: "Nomic" +--- + +Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including [Nomic](https://docs.nomic.ai/reference/getting-started/). + +Nomic has especially become popular due to it's superior embeddings and is now available through Portkey's AI gateway as well. + +With Portkey, you can take advantage of features like fast AI gateway access, observability, prompt management, and more, all while ensuring the secure management of your LLM API keys through a [virtual key](/product/ai-gateway/virtual-keys) system. + +Provider Slug. `nomic` + +## Portkey SDK Integration with Nomic + +Portkey provides a consistent API to interact with embedding models from various providers. To integrate Nomic with Portkey: + +### 1\. Create a Virtual Key for Nomic in your Portkey account + +You can head over to the virtual keys tab and create one for Nomic. This will be then used to make API requests to Nomic without needing the protected API key. [Grab your Nomic API key from here](https://atlas.nomic.ai/data/randomesid/org/keys). + +![Logo](/images/llms/nomic.png) + + + +### 2\. Install the Portkey SDK and Initialize with this Virtual Key + +Add the Portkey SDK to your application to interact with Nomic's API through Portkey's gateway. + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your Nomic Virtual Key + }) + ``` + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Nomic + ) + ``` + + + + + + +### 3\. Invoke the Embeddings API with Nomic + +Use the Portkey instance to send requests to your Nomic API. You can also override the virtual key directly in the API call if needed. + + + ```js + const embeddings = await portkey.embeddings.create({ + input: "create vector representation on this sentence", + model: "nomic-embed-text-v1.5", + }); + + console.log(embeddings); + ``` + + + ```python + embeddings = portkey.embeddings.create( + input='create vector representation on this sentence', + model='nomic-embed-text-v1.5' + ) + + print(embeddings) + ``` + + + + + + +## Next Steps + +The complete list of features supported in the SDK are available on the link below. + + + + +You'll find more information in the relevant sections: + +1. [API Reference for Embeddings](/provider-endpoints/embeddings) +2. [Add metadata to your requests](/product/observability/metadata) +3. [Add gateway configs to your Nomic requests](/product/ai-gateway/configs) +4. [Tracing Nomic requests](/product/observability/traces) diff --git a/virtual_key_old/integrations/llms/novita-ai.mdx b/virtual_key_old/integrations/llms/novita-ai.mdx new file mode 100644 index 00000000..d1087887 --- /dev/null +++ b/virtual_key_old/integrations/llms/novita-ai.mdx @@ -0,0 +1,112 @@ +--- +title: "Novita AI" +--- + +Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including [Novita AI](https://novita.ai/). + +With Portkey, you can take advantage of features like fast AI gateway access, observability, prompt management, and more, all while ensuring the secure management of your LLM API keys through a [virtual key](/product/ai-gateway/virtual-keys) system. + + +Provider Slug. `novita-ai` + +## Portkey SDK Integration with Novita AI Models + +Portkey provides a consistent API to interact with models from various providers. To integrate Novita AI with Portkey: + +### 1\. Install the Portkey SDK + +Add the Portkey SDK to your application to interact with Novita AI's API through Portkey's gateway. + + + ```sh + npm install --save portkey-ai + ``` + + + ```sh + pip install portkey-ai + ``` + + + + + + + +### 2\. Initialize Portkey with the Virtual Key + +To use Novita AI with Portkey, [get your API key from here](https://novita.ai/settings), then add it to Portkey to create the virtual key. + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your Novita Virtual Key + }) + ``` + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Groq + ) + ``` + + + + + + +### 3\. Invoke Chat Completions with Novita AI + +Use the Portkey instance to send requests to Novita AI. You can also override the virtual key directly in the API call if needed. + + + ```js + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'Nous-Hermes-2-Mixtral-8x7B-DPO', + }); + + console.log(chatCompletion.choices);d + ``` + + + ```python + completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model= 'reka-core' + ) + + print(completion) + ``` + + + + + + + +## Managing Novita AI Prompts + +You can manage all prompts to Novita AI in the [Prompt Library](/product/prompt-library). All the current models of Novita AI are supported and you can easily start testing different prompts. + +Once you're ready with your prompt, you can use the `portkey.prompts.completions.create` interface to use the prompt in your application. + +## Supported Models + +The complete list of features supported in the SDK are available on the link below. + + + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. A[dd gateway configs to your Novita](/product/ai-gateway/configs) +3. [Tracing Novita requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to Novita APIs](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/nscale.mdx b/virtual_key_old/integrations/llms/nscale.mdx new file mode 100644 index 00000000..99b6608f --- /dev/null +++ b/virtual_key_old/integrations/llms/nscale.mdx @@ -0,0 +1,129 @@ +--- +title: "Nscale (EU Sovereign)" +--- + +Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including the models hosted on [Nscale](https://docs.nscale.com/docs/inference/serverless-models/current). + + +Provider Slug. `nscale` + + +## Portkey SDK Integration with Nscale + +Portkey provides a consistent API to interact with models from various providers. To integrate Nscale with Portkey: + +### 1. Install the Portkey SDK + + + ```sh + npm install --save portkey-ai + ``` + + + ```sh + pip install portkey-ai + ``` + + + +### 2. Initialize Portkey with the Virtual Key +To use Nscale with Virtual Key, [get your API key from here](https://console.nscale.com). Then add it to Portkey to create the virtual key. + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your Nscale Virtual Key + }) + ``` + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="NSCALE_VIRTUAL_KEY" + ) + ``` + + + +### 3. Invoke Chat Completions + + + ```js + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'meta-llama/Llama-4-Scout-17B-16E-Instruct', + }); + + console.log(chatCompletion.choices); + ``` + + + ```python + completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model= 'meta-llama/Llama-4-Scout-17B-16E-Instruct' + ) + + print(completion) + ``` + + + +### 4. Invoke Image Generation + + + ```js + const response = await portkey.images.generations.create({ + prompt: "A beautiful sunset over mountains", + model: "stabilityai/stable-diffusion-xl-base-1.0", + n: 1, + size: "1024x1024" + }); + + console.log(response.data[0].url); + ``` + + + ```python + response = portkey.images.generate( + prompt="A beautiful sunset over mountains", + model="stabilityai/stable-diffusion-xl-base-1.0", + n=1, + size="1024x1024" + ) + + print(response.data[0].url) + ``` + + + +--- + +## Supported Models + + + + Explore the complete list of available models on Nscale's documentation, including chat models, image generation models, and their pricing details. + + + +--- + +## Next Steps + +The complete list of features supported in the SDK are available on the link below. + + + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your Nscale requests](/product/ai-gateway/configs) +3. [Tracing Nscale requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to Nscale](/product/ai-gateway/fallbacks) \ No newline at end of file diff --git a/virtual_key_old/integrations/llms/ollama.mdx b/virtual_key_old/integrations/llms/ollama.mdx new file mode 100644 index 00000000..5b5a16fe --- /dev/null +++ b/virtual_key_old/integrations/llms/ollama.mdx @@ -0,0 +1,470 @@ +--- +title: "Ollama" +--- + +Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including your **locally hosted models through Ollama**. + +Provider Slug. `ollama` + +## Portkey SDK Integration with Ollama Models + +Portkey provides a consistent API to interact with models from various providers. + + +If you are running the open source Portkey Gateway, refer to this guide on how to connect Portkey with Ollama. + + +### 1\. Expose your Ollama API + +Expose your Ollama API by using a tunneling service like [ngrok](https://ngrok.com/) or any other way you prefer. + +You can skip this step if you're self-hosting the Gateway. + +For using Ollama with ngrok, here's a [useful guide](https://github.com/ollama/ollama/blob/main/docs/faq.md#how-can-i-use-ollama-with-ngrok) +```sh +ngrok http 11434 --host-header="localhost:11434" +``` + +### 2\. Install the Portkey SDK + +Install the Portkey SDK in your application to interact with your Ollama API through Portkey. + + + +```sh +npm install --save portkey-ai +``` + + + ```sh + pip install portkey-ai + ``` + + + + + + +### 3\. Initialize Portkey with Ollama URL + +Instantiate the Portkey client by adding your Ollama publicly-exposed URL to the `customHost` property. + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + provider: "ollama", + customHost: "https://7cc4-3-235-157-146.ngrok-free.app" // Your Ollama ngrok URL + }) + ``` + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + provider="ollama", + custom_host="https://7cc4-3-235-157-146.ngrok-free.app" # Your Ollama ngrok URL + ) + ``` + + + + + + +For the Ollama integration, you only need to pass the base URL to `customHost` without the version identifier (such as `/v1`) - Portkey takes care of the rest! + +### **4\. Invoke Chat Completions with** Ollama + +Use the Portkey SDK to invoke chat completions from your Ollama model, just as you would with any other provider. + + + ```js + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'llama3', + }); + + console.log(chatCompletion.choices); + ``` + + + + ```python + completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model= 'llama3' + ) + + print(completion) + ``` + + + ```sh + curl --location 'https://api.portkey.ai/v1/chat/completions' \ + --header 'Content-Type: application/json' \ + --header 'x-portkey-custom-host: https://1eb6-103-180-45-236.ngrok-free.app' \ + --header 'x-portkey-provider: ollama' \ + --header 'x-portkey-api-key: PORTKEY_API_KEY' \ + --data '{ + "model": "tinyllama", + "max_tokens": 200, + "stream": false, + "messages": [ + { + "role": "system", + "content": [ + { + "type": "text", + "text": "You are Batman" + } + ] + }, + { + "role": "user", + "content": [ + { + "type": "text", + "text": "Who is the greatest detective" + } + ] + }, + { + "role": "assistant", + "content": [ + { + "type": "text", + "text": "is it me?" + } + ] + } + ] + }' + ``` + + + + + +## [Using Virtual Keys](https://app.portkey.ai/virtual-keys) + +Virtual Keys serve as Portkey's unified authentication system for all LLM interactions, simplifying the use of multiple providers and Portkey features within your application. For self-hosted LLMs, you can configure custom authentication requirements including authorization keys, bearer tokens, or any other headers needed to access your model: + + + + + +1. Navigate to [Virtual Keys](https://app.portkey.ai/virtual-keys) in your Portkey dashboard +2. Click **"Add Key"** and enable the **"Local/Privately hosted provider"** toggle +3. Configure your deployment: + - Select the matching provider API specification (typically `OpenAI`) + - Enter your model's base URL in the `Custom Host` field + - Add required authentication headers and their values +4. Click **"Create"** to generate your virtual key + +You can now use this virtual key in your requests: + + + + ```js + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "YOUR_SELF_HOSTED_LLM_VIRTUAL_KEY" + + async function main() { + const response = await client.chat.completions.create({ + messages: [{ role: "user", content: "Bob the builder.." }], + model: "your-self-hosted-model-name", + }); + + console.log(response.choices[0].message.content); + }) + ``` + + + ```python + portkey = Portkey( + api_key="PORTKEY_API_KEY", + virtual_key="YOUR_SELF_HOSTED_LLM_VIRTUAL_KEY" + ) + + response = portkey.chat.completions.create( + model="your-self-hosted-model-name", + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "Hello!"} + ] + + print(response) + ) + ``` + + + +For more information about managing self-hosted LLMs with Portkey, see [Bring Your Own LLM](/integrations/llms/byollm). + + +## Local Setup (npm or docker) + +First, install the Gateway locally: + + + +```sh +npx @portkey-ai/gateway +``` +| Your Gateway is running on http://localhost:8080/v1 ๐Ÿš€ | | +| - | - | + + +```sh +docker pull portkeyai/gateway +``` +| Your Gateway is running on http://host.docker.internal:8080/v1 ๐Ÿš€ | | +| - | - | + + + + +Then, just change the `baseURL` to the Gateway URL, `customHost` to the Ollam URL, and make requests. + + +If you are running Portkey inside a `Docker container`, but Ollama is running natively on your machine (i.e. not in Docker), you will have to refer to Ollama using `http://host.docker.internal:11434` for the Gateway to be able to call it. + + + +```ts NodeJS +import Portkey from 'portkey-ai'; + +const client = new Portkey({ + baseUrl: 'http://localhost:8080/v1', + apiKey: 'PORTKEY_API_KEY', + virtualKey: 'PROVIDER_VIRTUAL_KEY', + customHost: "http://host.docker.internal:11434" // Your Ollama Docker URL +}); + +async function main() { + const response = await client.chat.completions.create({ + messages: [{ role: "user", content: "Bob the builder.." }], + model: "gpt-4o", + }); + + console.log(response.choices[0].message.content); +} + +main(); +``` +```py Python +from portkey_ai import Portkey + +client = Portkey( + base_url = 'http://localhost:8080/v1', + api_key = "PORTKEY_API_KEY", + virtual_key = "PROVIDER_VIRTUAL_KEY", + custom_host="http://localhost:11434" # Your Ollama URL +) + +response = client.chat.completions.create( + model="gpt-4o", + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "Hello!"} + ] +) + +print(response.choices[0].message) +``` +```sh cURL +curl http://localhost:8080/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-custom-host: http://localhost:11434" \ + -H "x-portkey-virtual-key: $PORTKEY_PROVIDER_VIRTUAL_KEY" \ + -d '{ + "model": "gpt-4o", + "messages": [ + { "role": "user", "content": "Hello!" } + ] + }' +``` +```py OpenAI Python SDK +from openai import OpenAI +from portkey_ai import createHeaders, PORTKEY_GATEWAY_URL + +client = OpenAI( + api_key="xx", + base_url="https://localhost:8080/v1", + default_headers=createHeaders( + api_key="PORTKEY_API_KEY", + virtual_key="OPENAI_VIRTUAL_KEY", + custom_host="http://localhost:11434" + ) +) + +completion = client.chat.completions.create( + model="gpt-4o", + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "Hello!"} + ] +) + +print(completion.choices[0].message) +``` +```ts OpenAI NodeJS SDK +import OpenAI from 'openai'; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'xx', + baseURL: 'https://localhost:8080/v1', + defaultHeaders: createHeaders({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "OPENAI_VIRTUAL_KEY", + customHost: "http://localhost:11434" + }) +}); + +async function main() { + const completion = await openai.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'gpt-4o', + }); + + console.log(chatCompletion.choices); +} + +main(); +``` + + + + +### Ollama Tool Calling +Tool calling feature lets models trigger external tools based on conversation context. You define available functions, the model chooses when to use them, and your application executes them and returns results. + +Portkey supports Ollama Tool Calling and makes it interoperable across multiple providers. With Portkey Prompts, you can templatize various your prompts & tool schemas as well. + + + + + + + +```javascript Get Weather Tool +let tools = [{ + type: "function", + function: { + name: "getWeather", + description: "Get the current weather", + parameters: { + type: "object", + properties: { + location: { type: "string", description: "City and state" }, + unit: { type: "string", enum: ["celsius", "fahrenheit"] } + }, + required: ["location"] + } + } +}]; + +let response = await portkey.chat.completions.create({ + model: "llama-3.3-70b-versatile", + messages: [ + { role: "system", content: "You are a helpful assistant." }, + { role: "user", content: "What's the weather like in Delhi - respond in JSON" } + ], + tools, + tool_choice: "auto", +}); + +console.log(response.choices[0].finish_reason); +``` + + +```python Get Weather Tool +tools = [{ + "type": "function", + "function": { + "name": "getWeather", + "description": "Get the current weather", + "parameters": { + "type": "object", + "properties": { + "location": {"type": "string", "description": "City and state"}, + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]} + }, + "required": ["location"] + } + } +}] + +response = portkey.chat.completions.create( + model="llama-3.3-70b-versatile", + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "What's the weather like in Delhi - respond in JSON"} + ], + tools=tools, + tool_choice="auto" +) + +print(response.choices[0].finish_reason) +``` + + +```sh Get Weather Tool +curl -X POST "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -d '{ + "model": "llama-3.3-70b-versatile", + "messages": [ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "What'\''s the weather like in Delhi - respond in JSON"} + ], + "tools": [{ + "type": "function", + "function": { + "name": "getWeather", + "description": "Get the current weather", + "parameters": { + "type": "object", + "properties": { + "location": {"type": "string", "description": "City and state"}, + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]} + }, + "required": ["location"] + } + } + }], + "tool_choice": "auto" + }' +``` + + + Checkout [Prompt Engineering Studio](/product/prompt-engineering-studio/prompt-playground) + + + + + + +## Next Steps + +Explore the complete list of features supported in the SDK: + + + +--- + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your Ollama requests](/product/ai-gateway/universal-api#ollama-in-configs) +3. [Tracing Ollama requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to Ollama APIs](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/openai.mdx b/virtual_key_old/integrations/llms/openai.mdx new file mode 100644 index 00000000..ff7bd8f8 --- /dev/null +++ b/virtual_key_old/integrations/llms/openai.mdx @@ -0,0 +1,1174 @@ +--- +title: "OpenAI" +description: "Learn to integrate OpenAI with Portkey, enabling seamless completions, prompt management, and advanced functionalities like streaming, function calling and fine-tuning." +--- + +Portkey has native integrations with OpenAI SDKs for Node.js, Python, and its REST APIs. For OpenAI integration using other frameworks, explore our partnerships, including [Langchain](/integrations/libraries/langchain-python), [LlamaIndex](/integrations/libraries/llama-index-python), among [others](/integrations/llms). + +Provider Slug. `openai` + +## Using the Portkey Gateway + +To integrate the Portkey gateway with OpenAI, + +* Set the `baseURL` to the Portkey Gateway URL +* Include Portkey-specific headers such as `provider`, `apiKey`, 'virtualKey' and others. + +Here's how to apply it to a **chat completion** request: + + + + +Install the Portkey SDK with npm +```sh +npm install portkey-ai +``` + +```ts Chat Completions +import Portkey from 'portkey-ai'; + +const client = new Portkey({ + apiKey: 'PORTKEY_API_KEY', + virtualKey: 'PROVIDER_VIRTUAL_KEY' +}); + +async function main() { + const response = await client.chat.completions.create({ + messages: [{ role: "user", content: "Bob the builder.." }], + model: "gpt-4o", + }); + + console.log(response.choices[0].message.content); +} + +main(); +``` + + + + +Install the Portkey SDK with pip +```sh +pip install portkey-ai +``` + +```py Chat Completions +from portkey_ai import Portkey + +client = Portkey( + api_key = "PORTKEY_API_KEY", + virtual_key = "PROVIDER_VIRTUAL_KEY" +) + +response = client.chat.completions.create( + model="gpt-4o", + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "Hello!"} + ] +) + +print(response.choices[0].message) +``` + + + + + +```sh Chat Completions +curl https://api.portkey.ai/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-virtual-key: $PORTKEY_PROVIDER_VIRTUAL_KEY" \ + -d '{ + "model": "gpt-4o", + "messages": [ + { "role": "user", "content": "Hello!" } + ] + }' +``` + + + + +Install the OpenAI & Portkey SDKs with pip +```sh +pip install openai portkey-ai +``` + +```py Chat Completions +from openai import OpenAI +from portkey_ai import createHeaders, PORTKEY_GATEWAY_URL + +client = OpenAI( + api_key="xx", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key="PORTKEY_API_KEY", + virtual_key="OPENAI_VIRTUAL_KEY" + ) +) + +completion = client.chat.completions.create( + model="gpt-4o", + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "Hello!"} + ] +) + +print(completion.choices[0].message) +``` + + + +Install the OpenAI & Portkey SDKs with npm +```sh +npm install openai portkey-ai +``` + +```ts Chat Completions +import OpenAI from 'openai'; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'xx', + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "OPENAI_VIRTUAL_KEY" + }) +}); + +async function main() { + const completion = await openai.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'gpt-4o', + }); + + console.log(chatCompletion.choices); +} + +main(); +``` + + + + + + +This request will be automatically logged by Portkey. You can view this in your logs dashboard. Portkey logs the tokens utilized, execution time, and cost for each request. Additionally, you can delve into the details to review the precise request and response data. + + +Portkey supports [OpenAI's new "developer" role](https://platform.openai.com/docs/api-reference/chat/create#chat-create-messages) in chat completions. With o1 models and newer, the `developer` role replaces the previous `system` role. + + + +### Using the Responses API + +OpenAI has released a new Responses API that combines the best of both Chat Completions and Assistants APIs. Portkey fully supports this new API, enabling you to use it with both the Portkey SDK and OpenAI SDK. + + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", + virtual_key="OPENAI_VIRTUAL_KEY" + ) + + response = portkey.responses.create( + model="gpt-4.1", + input="Tell me a three sentence bedtime story about a unicorn." + ) + + print(response) + ``` + + + ```js + import Portkey from 'portkey-ai'; + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "OPENAI_VIRTUAL_KEY" + }); + + async function main() { + const response = await portkey.responses.create({ + model: "gpt-4.1", + input: "Tell me a three sentence bedtime story about a unicorn." + }); + + console.log(response); + } + + main(); + ``` + + + ```python + from openai import OpenAI + from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + + client = OpenAI( + api_key="OPENAI_API_KEY", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY", + virtual_key="OPENAI_VIRTUAL_KEY" + ) + ) + + response = client.responses.create( + model="gpt-4.1", + input="Tell me a three sentence bedtime story about a unicorn." + ) + + print(response) + ``` + + + ```js + import OpenAI from 'openai'; + import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + + const openai = new OpenAI({ + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY", + virtualKey: "OPENAI_VIRTUAL_KEY" + }) + }); + + async function main() { + const response = await openai.responses.create({ + model: "gpt-4.1", + input: "Tell me a three sentence bedtime story about a unicorn." + }); + + console.log(response); + } + + main(); + ``` + + + + +The Responses API provides a more flexible foundation for building agentic applications with built-in tools that execute automatically. + + + + + Portkey supports Remote MCP support by OpenAI on it's Responses API. Learn More + + + + +## Track End-User IDs + +Portkey allows you to track user IDs passed with the `user` parameter in OpenAI requests, enabling you to monitor user-level costs, requests, and more. + + + + +```js +const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: "user", content: "Say this is a test" }], + model: "gpt-4o", + user: "user_12345", +}); +``` + + + + +```py +response = portkey.chat.completions.create( + model="gpt-4o", + messages=[{ role: "user", content: "Say this is a test" }] + user="user_123456" +) +``` + + +When you include the `user` parameter in your requests, Portkey logs will display the associated user ID, as shown in the image below: + + +logs + + +In addition to the `user` parameter, Portkey allows you to send arbitrary custom metadata with your requests. This powerful feature enables you to associate additional context or information with each request, which can be useful for analysis, debugging, or other custom use cases. + + + +* The same integration approach applies to APIs for [completions](https://platform.openai.com/docs/guides/text-generation/completions-api), [embeddings](https://platform.openai.com/docs/api-reference/embeddings/create), [vision](https://platform.openai.com/docs/guides/vision/quick-start), [moderation](https://platform.openai.com/docs/api-reference/moderations/create), [transcription](https://platform.openai.com/docs/api-reference/audio/createTranscription), [translation](https://platform.openai.com/docs/api-reference/audio/createTranslation), [speech](https://platform.openai.com/docs/api-reference/audio/createSpeech) and [files](https://platform.openai.com/docs/api-reference/files/create). +* If you are looking for a way to add your **Org ID** & **Project ID** to the requests, head over to [this section](/integrations/llms/openai#managing-openai-projects-and-organizations-in-portkey). + + + +## Using the Prompts API + +Portkey also supports creating and managing prompt templates in the [prompt library](/product/prompt-library). This enables the collaborative development of prompts directly through the user interface. + +1. Create a prompt template with variables and set the hyperparameters. + + +prompt + + + +2. Use this prompt in your codebase using the Portkey SDK. + + + + +```js +import Portkey from 'portkey-ai' + +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", +}) + +// Make the prompt creation call with the variables + +const promptCompletion = await portkey.prompts.completions.create({ + promptID: "Your Prompt ID", + variables: { + // The variables specified in the prompt + } +}) +``` + + +```js +// We can also override the hyperparameters + +const promptCompletion = await portkey.prompts.completions.create({ + promptID: "Your Prompt ID", + variables: { + // The variables specified in the prompt + }, + max_tokens: 250, + presence_penalty: 0.2 +}) +``` + + + + +```python +from portkey_ai import Portkey + +client = Portkey( + api_key="PORTKEY_API_KEY", # defaults to os.environ.get("PORTKEY_API_KEY") +) + +prompt_completion = client.prompts.completions.create( + prompt_id="Your Prompt ID", + variables={ + # The variables specified in the prompt + } +) + +print(prompt_completion) + +# We can also override the hyperparameters + +prompt_completion = client.prompts.completions.create( + prompt_id="Your Prompt ID", + variables={ + # The variables specified in the prompt + }, + max_tokens=250, + presence_penalty=0.2 +) + +print(prompt_completion) +``` + + + +```sh +curl -X POST "https://api.portkey.ai/v1/prompts/:PROMPT_ID/completions" \ +-H "Content-Type: application/json" \ +-H "x-portkey-api-key: $PORTKEY_API_KEY" \ +-d '{ + "variables": { + # The variables to use + }, + "max_tokens": 250, # Optional + "presence_penalty": 0.2 # Optional +}' +``` + + + +Observe how this streamlines your code readability and simplifies prompt updates via the UI without altering the codebase. + +## Advanced Use Cases + +### Realtime API + +Portkey supports OpenAI's Realtime API with a seamless integration. This allows you to use Portkey's logging, cost tracking, and guardrail features while using the Realtime API. + + + +### Streaming Responses + +Portkey supports streaming responses using Server Sent Events (SSE). + + + + ```js + import OpenAI from 'openai'; + + import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + const openai = new OpenAI({ + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY" // defaults to process.env["PORTKEY_API_KEY"] + }) + }); + + async function main() { + const stream = await openai.chat.completions.create({ + model: 'gpt-4', + messages: [{ role: 'user', content: 'Say this is a test' }], + stream: true, + }); + + for await (const chunk of stream) { + process.stdout.write(chunk.choices[0]?.delta?.content || ''); + } + } + + main(); + ``` + + + ```python + from openai import OpenAI + from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + + client = OpenAI( + api_key="OPENAI_API_KEY", # defaults to os.environ.get("OPENAI_API_KEY") + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY" # defaults to os.environ.get("PORTKEY_API_KEY") + ) + ) + + chat_complete = client.chat.completions.create( + model="gpt-4", + messages=[{"role": "user", "content": "Say this is a test"}], + stream=True + ) + + for chunk in chat_complete: + print(chunk.choices[0].delta.content, end="", flush=True) + ``` + + + + + + +#### Streaming with the Responses API + +You can also stream responses from the Responses API: + + + + ```python + response = portkey.responses.create( + model="gpt-4.1", + instructions="You are a helpful assistant.", + input="Hello!", + stream=True + ) + + for event in response: + print(event) + ``` + + + ```js + const response = await portkey.responses.create({ + model: "gpt-4.1", + instructions: "You are a helpful assistant.", + input: "Hello!", + stream: true + }); + + for await (const event of response) { + console.log(event); + } + ``` + + + ```python + response = client.responses.create( + model="gpt-4.1", + instructions="You are a helpful assistant.", + input="Hello!", + stream=True + ) + + for event in response: + print(event) + ``` + + + ```js + const response = await openai.responses.create({ + model: "gpt-4.1", + instructions: "You are a helpful assistant.", + input: "Hello!", + stream: true + }); + + for await (const event of response) { + console.log(event); + } + ``` + + + + + +### Using Vision Models + +Portkey's multimodal Gateway fully supports OpenAI vision models as well. See this guide for more info: + + +[Vision](/product/ai-gateway/multimodal-capabilities/vision) + + +#### Vision with the Responses API + +You can also use the Responses API to process images alongside text: + + + + ```python + response = portkey.responses.create( + model="gpt-4.1", + input=[ + { + "role": "user", + "content": [ + { "type": "input_text", "text": "What is in this image?" }, + { + "type": "input_image", + "image_url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg" + } + ] + } + ] + ) + + print(response) + ``` + + + ```js + const response = await portkey.responses.create({ + model: "gpt-4.1", + input: [ + { + role: "user", + content: [ + { type: "input_text", text: "What is in this image?" }, + { + type: "input_image", + image_url: "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg" + } + ] + } + ] + }); + + console.log(response); + ``` + + + ```python + response = client.responses.create( + model="gpt-4.1", + input=[ + { + "role": "user", + "content": [ + { "type": "input_text", "text": "What is in this image?" }, + { + "type": "input_image", + "image_url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg" + } + ] + } + ] + ) + + print(response) + ``` + + + ```js + const response = await openai.responses.create({ + model: "gpt-4.1", + input: [ + { + role: "user", + content: [ + { type: "input_text", text: "What is in this image?" }, + { + type: "input_image", + image_url: "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg" + } + ] + } + ] + }); + + console.log(response); + ``` + + + + + +### Function Calling + +Function calls within your OpenAI or Portkey SDK operations remain standard. These logs will appear in Portkey, highlighting the utilized functions and their outputs. + +Additionally, you can define functions within your prompts and invoke the `portkey.prompts.completions.create` method as above. + + +#### Function Calling with the Responses API + +The Responses API also supports function calling with the same powerful capabilities: + + + + ```python + tools = [ + { + "type": "function", + "name": "get_current_weather", + "description": "Get the current weather in a given location", + "parameters": { + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "The city and state, e.g. San Francisco, CA" + }, + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]} + }, + "required": ["location", "unit"] + } + } + ] + + response = portkey.responses.create( + model="gpt-4.1", + tools=tools, + input="What is the weather like in Boston today?", + tool_choice="auto" + ) + + print(response) + ``` + + + ```js + const tools = [ + { + type: "function", + name: "get_current_weather", + description: "Get the current weather in a given location", + parameters: { + type: "object", + properties: { + location: { + type: "string", + description: "The city and state, e.g. San Francisco, CA" + }, + unit: { type: "string", enum: ["celsius", "fahrenheit"] } + }, + required: ["location", "unit"] + } + } + ]; + + const response = await portkey.responses.create({ + model: "gpt-4.1", + tools: tools, + input: "What is the weather like in Boston today?", + tool_choice: "auto" + }); + + console.log(response); + ``` + + + ```python + tools = [ + { + "type": "function", + "name": "get_current_weather", + "description": "Get the current weather in a given location", + "parameters": { + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "The city and state, e.g. San Francisco, CA" + }, + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]} + }, + "required": ["location", "unit"] + } + } + ] + + response = client.responses.create( + model="gpt-4.1", + tools=tools, + input="What is the weather like in Boston today?", + tool_choice="auto" + ) + + print(response) + ``` + + + ```js + const tools = [ + { + type: "function", + name: "get_current_weather", + description: "Get the current weather in a given location", + parameters: { + type: "object", + properties: { + location: { + type: "string", + description: "The city and state, e.g. San Francisco, CA" + }, + unit: { type: "string", enum: ["celsius", "fahrenheit"] } + }, + required: ["location", "unit"] + } + } + ]; + + const response = await openai.responses.create({ + model: "gpt-4.1", + tools: tools, + input: "What is the weather like in Boston today?", + tool_choice: "auto" + }); + + console.log(response); + ``` + + + + + +### Fine-Tuning + +Please refer to our fine-tuning guides to take advantage of Portkey's advanced [continuous fine-tuning](/product/autonomous-fine-tuning) capabilities. + +### Image Generation + +Portkey supports multiple modalities for OpenAI and you can make image generation requests through Portkey's AI Gateway the same way as making completion calls. + + + + ```js + // Define the OpenAI client as shown above + + const image = await openai.images.generate({ + model:"dall-e-3", + prompt:"Lucy in the sky with diamonds", + size:"1024x1024" + }) + ``` + + + + ```python + # Define the OpenAI client as shown above + + image = openai.images.generate( + model="dall-e-3", + prompt="Lucy in the sky with diamonds", + size="1024x1024" + ) + ``` + + + +Portkey's fast AI gateway captures the information about the request on your Portkey Dashboard. On your logs screen, you'd be able to see this request with the request and response. + +querying-vision-language-models + + +More information on image generation is available in the [API Reference](/provider-endpoints/images/create-image#create-image). + +### Audio - Transcription, Translation, and Text-to-Speech + +Portkey's multimodal Gateway also supports the `audio` methods on OpenAI API. Check out the below guides for more info: + +Check out the below guides for more info: + +[Text-to-Speech](/product/ai-gateway/multimodal-capabilities/text-to-speech) + + +[Speech-to-Text](/product/ai-gateway/multimodal-capabilities/speech-to-text) + +--- + +## Intergrated Tools with Repsponses API + +### Web Search Tool + +Web search delivers accurate and clearly-cited answers from the web, using the same tool as search in ChatGPT: + + + + ```python + response = portkey.responses.create( + model="gpt-4.1", + tools=[{ + "type": "web_search_preview", + "search_context_size": "medium", # Options: "high", "medium" (default), or "low" + "user_location": { # Optional - for localized results + "type": "approximate", + "country": "US", + "city": "San Francisco", + "region": "California" + } + }], + input="What was a positive news story from today?" + ) + + print(response) + ``` + + + ```js + const response = await portkey.responses.create({ + model: "gpt-4.1", + tools: [{ + type: "web_search_preview", + search_context_size: "medium", // Options: "high", "medium" (default), or "low" + user_location: { // Optional - for localized results + type: "approximate", + country: "US", + city: "San Francisco", + region: "California" + } + }], + input: "What was a positive news story from today?" + }); + + console.log(response); + ``` + + + + + **Options for `search_context_size`:** + - `high`: Most comprehensive context, higher cost, slower response + - `medium`: Balanced context, cost, and latency (default) + - `low`: Minimal context, lowest cost, fastest response + + Responses include citations for URLs found in search results, with clickable references. + + +### File Search Tool + +File search enables quick retrieval from your knowledge base across multiple file types: + + + + ```python + response = portkey.responses.create( + model="gpt-4.1", + tools=[{ + "type": "file_search", + "vector_store_ids": ["vs_1234567890"], + "max_num_results": 20, + "filters": { # Optional - filter by metadata + "type": "eq", + "key": "document_type", + "value": "report" + } + }], + input="What are the attributes of an ancient brown dragon?" + ) + + print(response) + ``` + + + ```js + const response = await portkey.responses.create({ + model: "gpt-4.1", + tools: [{ + type: "file_search", + vector_store_ids: ["vs_1234567890"], + max_num_results: 20, + filters: { // Optional - filter by metadata + type: "eq", + key: "document_type", + value: "report" + } + }], + input: "What are the attributes of an ancient brown dragon?" + }); + + console.log(response); + ``` + + + + + This tool requires you to first create a vector store and upload files to it. Supports various file formats including PDFs, DOCXs, TXT, and more. Results include file citations in the response. + + +### Enhanced Reasoning + +Control the depth of model reasoning for more comprehensive analysis: + + + + ```python + response = portkey.responses.create( + model="o3-mini", + input="How much wood would a woodchuck chuck?", + reasoning={ + "effort": "high" # Options: "high", "medium", or "low" + } + ) + + print(response) + ``` + + + ```js + const response = await portkey.responses.create({ + model: "o3-mini", + input: "How much wood would a woodchuck chuck?", + reasoning: { + effort: "high" // Options: "high", "medium", or "low" + } + }); + + console.log(response); + ``` + + + +### Computer Use Assistant + +Portkey also supports the Computer Use Assistant (CUA) tool, which helps agents control computers or virtual machines through screenshots and actions. This feature is available for select developers as a research preview on premium tiers. + + + + Learn More about Computer use tool here + + + + +## Managing OpenAI Projects & Organizations in Portkey + +When integrating OpenAI with Portkey, you can specify your OpenAI organization and project IDs along with your API key. This is particularly useful if you belong to multiple organizations or are accessing projects through a legacy user API key. + +Specifying the organization and project IDs helps you maintain better control over your access rules, usage, and costs. + +In Portkey, you can add your Org & Project details by, + +1. Creating your Virtual Key +2. Defining a Gateway Config +3. Passing Details in a Request + +Let's explore each method in more detail. + +### Using Virtual Keys + +When selecting OpenAI from the dropdown menu while creating a virtual key, Portkey automatically displays optional fields for the organization ID and project ID alongside the API key field. + +[Get your OpenAI API key from here](https://platform.openai.com/api-keys), then add it to Portkey to create the virtual key that can be used throughout Portkey. + +![LOGO](/images/llms/virtual.png) + + +[Virtual Keys](/product/ai-gateway/virtual-keys) + +Portkey takes budget management a step further than OpenAI. While OpenAI allows setting budget limits per project, Portkey enables you to set budget limits for each virtual key you create. For more information on budget limits, refer to this documentation: + + +[Budget Limits](/product/ai-gateway/virtual-keys/budget-limits) + +### Using The Gateway Config + +You can also specify the organization and project details in the gateway config, either at the root level or within a specific target. + +```json +{ + "provider": "openai", + "api_key": "OPENAI_API_KEY", + "openai_organization": "org-xxxxxx", + "openai_project": "proj_xxxxxxxx" +} +``` + +### While Making a Request + +You can also pass your organization and project details directly when making a request using curl, the OpenAI SDK, or the Portkey SDK. + + + ```python + from openai import OpenAI + from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + + client = OpenAI( + api_key="OPENAI_API_KEY", + organization="org-xxxxxxxxxx", + project="proj_xxxxxxxxx", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY" + ) + ) + + chat_complete = client.chat.completions.create( + model="gpt-4o", + messages=[{"role": "user", "content": "Say this is a test"}], + ) + + print(chat_complete.choices[0].message.content) + ``` + + + + +```js +import OpenAI from "openai"; +import { PORTKEY_GATEWAY_URL, createHeaders } from "portkey-ai"; + +const openai = new OpenAI({ + apiKey: "OPENAI_API_KEY", + organization: "org-xxxxxx", + project: "proj_xxxxxxx", + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY", + }), +}); + +async function main() { + const chatCompletion = await openai.chat.completions.create({ + messages: [{ role: "user", content: "Say this is a test" }], + model: "gpt-4o", + }); + + console.log(chatCompletion.choices); +} + +main(); +``` + + + + +```sh +curl https://api.portkey.ai/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "x-portkey-openai-organization: org-xxxxxxx" \ + -H "x-portkey-openai-project: proj_xxxxxxx" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-provider: openai" \ + -d '{ + "model": "gpt-4o", + "messages": [{"role": "user","content": "Hello!"}] + }' +``` + + + +```python +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY", + provider="openai", + Authorization="Bearer OPENAI_API_KEY", + openai_organization="org-xxxxxxxxx", + openai_project="proj_xxxxxxxxx", +) + +chat_complete = portkey.chat.completions.create( + model="gpt-4o", + messages=[{"role": "user", "content": "Say this is a test"}], +) + +print(chat_complete.choices[0].message.content) +``` + + + +```js +import Portkey from "portkey-ai"; + +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + provider: "openai", + Authorization: "Bearer OPENAI_API_KEY", + openaiOrganization: "org-xxxxxxxxxxx", + openaiProject: "proj_xxxxxxxxxxxxx", +}); + +async function main() { + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: "user", content: "Say this is a test" }], + model: "gpt-4o", + }); + + console.log(chatCompletion.choices); +} + +main(); +``` + + + +--- + +### Portkey Features + +Portkey supports the complete host of it's functionality via the OpenAI SDK so you don't need to migrate away from it. + +Please find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to the OpenAI client or a single request](/product/ai-gateway/configs) +3. [Tracing OpenAI requests](/product/observability/traces) +4. [Setup a fallback to Azure OpenAI](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/openai/batches.mdx b/virtual_key_old/integrations/llms/openai/batches.mdx new file mode 100644 index 00000000..cdef8db4 --- /dev/null +++ b/virtual_key_old/integrations/llms/openai/batches.mdx @@ -0,0 +1,497 @@ +--- +title: Batches +description: Perform batch inference with OpenAI +--- + + +With Portkey, you can perform [OpenAI Batch Inference](https://platform.openai.com/docs/guides/batch) operations. +This is the most efficient way to +- Test your data with different foundation models +- Perform A/B testing with different foundation models +- Perform batch inference with different foundation models + +## Create Batch Job + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Add your provider's virtual key +) + +start_batch_response = portkey.batches.create( + input_file_id="file_id", # file id of the input file + endpoint="endpoint", # ex: /v1/chat/completions + completion_window="completion_window", # ex: 24h + metadata={} # metadata for the batch +) + +print(start_batch_response) +``` + + +```js +import { Portkey } from 'portkey-ai'; + +// Initialize the Portkey client +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key +}); + +const startBatch = async () => { + const startBatchResponse = await portkey.batches.create({ + input_file_id: "file_id", // file id of the input file + endpoint: "endpoint", // ex: /v1/chat/completions + completion_window: "completion_window", // ex: 24h + metadata: {} // metadata for the batch + }); + + console.log(startBatchResponse); +} + +await startBatch(); + +``` + + +```sh +curl --location 'https://api.portkey.ai/v1/batches' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' \ +--header 'Content-Type: application/json' \ +--data '{ + "input_file_id": "", + "endpoint": "", + "completion_window": "", + "metadata": {}, +}' +``` + + +```js +import OpenAI from 'openai'; // We're using the v4 SDK +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY" // defaults to process.env["PORTKEY_API_KEY"] + }) +}); + +const startBatch = async () => { + const startBatchResponse = await openai.batches.create({ + input_file_id: "file_id", // file id of the input file + endpoint: "endpoint", // ex: /v1/chat/completions + completion_window: "completion_window", // ex: 24h + metadata: {} // metadata for the batch + }); + + console.log(startBatchResponse); +} + +await startBatch(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY" + ) +) + +start_batch_response = openai.batches.create( + input_file_id="file_id", # file id of the input file + endpoint="endpoint", # ex: /v1/chat/completions + completion_window="completion_window", # ex: 24h + metadata={} # metadata for the batch +) + +print(start_batch_response) +``` + + + + +## List Batch Jobs + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Add your provider's virtual key +) + +batches = portkey.batches.list() + +print(batches) +``` + + +```js +import { Portkey } from 'portkey-ai'; + +// Initialize the Portkey client +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key +}); + +const listBatches = async () => { + const batches = await portkey.batches.list(); + + console.log(batches); +} + +await listBatches(); + +``` + + +```sh +curl --location 'https://api.portkey.ai/v1/batches' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' +``` + + +```js +import OpenAI from 'openai'; // We're using the v4 SDK +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY" // defaults to process.env["PORTKEY_API_KEY"] + }) +}); + +const listBatches = async () => { + const batches = await openai.batches.list(); + + console.log(batches); +} + +await listBatches(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY" + ) +) + +batches = openai.batches.list() + +print(batches) +``` + + + + + +## Get Batch Job Details + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Add your provider's virtual key +) + +batch = portkey.batches.retrieve(batch_id="batch_id") + +print(batch) +``` + + +```js +import { Portkey } from 'portkey-ai'; + +// Initialize the Portkey client +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key +}); + +const getBatch = async () => { + const batch = await portkey.batches.retrieve(batch_id="batch_id"); + + console.log(batch); +} + +await getBatch(); + +``` + + +```sh +curl --location 'https://api.portkey.ai/v1/batches/' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' +``` + + +```js +import OpenAI from 'openai'; // We're using the v4 SDK +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY" // defaults to process.env["PORTKEY_API_KEY"] + }) +}); + +const getBatch = async () => { + const batch = await openai.batches.retrieve(batch_id="batch_id"); + + console.log(batch); +} + +await getBatch(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY" + ) +) + +batch = openai.batches.retrieve(batch_id="batch_id") + +print(batch) +``` + + + + + +## Get Batch Output + + +```sh +curl --location 'https://api.portkey.ai/v1/batches//output' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' +``` + + + + +## List Batch Jobs + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Add your provider's virtual key +) + +batches = portkey.batches.list() + +print(batches) +``` + + +```js +import { Portkey } from 'portkey-ai'; + +// Initialize the Portkey client +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key +}); + +const listBatchingJobs = async () => { + const batching_jobs = await portkey.batches.list(); + + console.log(batching_jobs); +} + +await listBatchingJobs(); + +``` + + +```sh +curl --location 'https://api.portkey.ai/v1/batches' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' +``` + + +```js +import OpenAI from 'openai'; // We're using the v4 SDK +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY" // defaults to process.env["PORTKEY_API_KEY"] + }) +}); + +const listBatchingJobs = async () => { + const batching_jobs = await openai.batches.list(); + + console.log(batching_jobs); +} + +await listBatchingJobs(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY" + ) +) + +batching_jobs = openai.batches.list() + +print(batching_jobs) +``` + + + + +## Cancel Batch Job + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Add your provider's virtual key +) + +cancel_batch_response = portkey.batches.cancel(batch_id="batch_id") + +print(cancel_batch_response) +``` + + +```js +import { Portkey } from 'portkey-ai'; + +// Initialize the Portkey client +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key +}); + +const cancelBatch = async () => { + const cancel_batch_response = await portkey.batches.cancel(batch_id="batch_id"); + + console.log(cancel_batch_response); +} + +await cancelBatch(); + +``` + + +```sh +curl --request POST --location 'https://api.portkey.ai/v1/batches//cancel' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' +``` + + +```js +import OpenAI from 'openai'; // We're using the v4 SDK +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY" // defaults to process.env["PORTKEY_API_KEY"] + }) +}); + +const cancelBatch = async () => { + const cancel_batch_response = await openai.batches.cancel(batch_id="batch_id"); + + console.log(cancel_batch_response); +} + +await cancelBatch(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY" + ) +) + +cancel_batch_response = openai.batches.cancel(batch_id="batch_id") + +print(cancel_batch_response) +``` + + + diff --git a/virtual_key_old/integrations/llms/openai/files.mdx b/virtual_key_old/integrations/llms/openai/files.mdx new file mode 100644 index 00000000..874a1f8b --- /dev/null +++ b/virtual_key_old/integrations/llms/openai/files.mdx @@ -0,0 +1,466 @@ +--- +title: Files +description: Upload files to OpenAI +--- + +## Uploading Files + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Add your provider's virtual key +) + +upload_file_response = portkey.files.create( + purpose="batch", + file=open("file.pdf", "rb") +) + +print(upload_file_response) +``` + + +```js +import { Portkey } from 'portkey-ai'; + +// Initialize the Portkey client +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key +}); + +const uploadFile = async () => { + const file = await portkey.files.create({ + purpose: "batch", + file: fs.createReadStream("file.pdf") + }); + + console.log(file); +} + +await uploadFile(); + +``` + + +```sh +curl --location --request POST 'https://api.portkey.ai/v1/files' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' \ +--form 'purpose=""' \ +--form 'file=@""' +``` + + +```js +import OpenAI from 'openai'; // We're using the v4 SDK +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY" // defaults to process.env["PORTKEY_API_KEY"] + }) +}); + +const uploadFile = async () => { + const file = await openai.files.create({ + purpose: "batch", + file: fs.createReadStream("file.pdf") + }); + + console.log(file); +} + +await uploadFile(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY" + ) +) + +upload_file_response = openai.files.create( + purpose="batch", + file=open("file.pdf", "rb") +) + +print(upload_file_response) +``` + + + + +## List Files + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Add your provider's virtual key +) + +files = portkey.files.list() + +print(files) +``` + + +```js +import { Portkey } from 'portkey-ai'; + +// Initialize the Portkey client +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key +}); + +const listFiles = async () => { + const files = await portkey.files.list(); + + console.log(files); +} + +await listFiles(); + +``` + + +```sh +curl --location 'https://api.portkey.ai/v1/files' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' +``` + + +```js +import OpenAI from 'openai'; // We're using the v4 SDK +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY" // defaults to process.env["PORTKEY_API_KEY"] + }) +}); + +const listFiles = async () => { + const files = await openai.files.list(); + + console.log(file); +} + +await listFiles(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY" + ) +) + +files = openai.files.list() + +print(files) +``` + + + + + +## Get File + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Add your provider's virtual key +) + +file = portkey.files.retrieve(file_id="file_id") + +print(file) +``` + + +```js +import { Portkey } from 'portkey-ai'; + +// Initialize the Portkey client +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key +}); + +const getFile = async () => { + const file = await portkey.files.retrieve(file_id="file_id"); + + console.log(file); +} + +await getFile(); + +``` + + +```sh +curl --location 'https://api.portkey.ai/v1/files/' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' +``` + + +```js +import OpenAI from 'openai'; // We're using the v4 SDK +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY" // defaults to process.env["PORTKEY_API_KEY"] + }) +}); + +const getFile = async () => { + const file = await openai.files.retrieve(file_id="file_id"); + + console.log(file); +} + +await getFile(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY" + ) +) + +file = openai.files.retrieve(file_id="file_id") + +print(file) +``` + + + + + +## Get File Content + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Add your provider's virtual key +) + +file_content = portkey.files.content(file_id="file_id") + +print(file_content) +``` + + +```js +import { Portkey } from 'portkey-ai'; + +// Initialize the Portkey client +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key +}); + +const getFileContent = async () => { + const file_content = await portkey.files.content(file_id="file_id"); + + console.log(file_content); +} + +await getFileContent(); + +``` + + +```sh +curl --location 'https://api.portkey.ai/v1/files//content' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' +``` + + +```js +import OpenAI from 'openai'; // We're using the v4 SDK +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY" // defaults to process.env["PORTKEY_API_KEY"] + }) +}); + +const getFileContent = async () => { + const file_content = await openai.files.content(file_id="file_id"); + + console.log(file_content); +} + +await getFileContent(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY" + ) +) + +file_content = openai.files.content(file_id="file_id") + +print(file_content) +``` + + + + + +## Delete File + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Add your provider's virtual key +) + +delete_file_response = portkey.files.delete(file_id="file_id") + +print(delete_file_response) +``` + + +```js +import { Portkey } from 'portkey-ai'; + +// Initialize the Portkey client +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key +}); + +const deleteFile = async () => { + const delete_file_response = await portkey.files.delete(file_id="file_id"); + + console.log(delete_file_response); +} + +await deleteFile(); + +``` + + +```sh +curl --location --request DELETE 'https://api.portkey.ai/v1/files/' \ +--header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' +``` + + +```js +import OpenAI from 'openai'; // We're using the v4 SDK +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY" // defaults to process.env["PORTKEY_API_KEY"] + }) +}); + +const deleteFile = async () => { + const delete_file_response = await openai.files.delete(file_id="file_id"); + + console.log(delete_file_response); +} + +await deleteFile(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY" + ) +) + +delete_file_response = openai.files.delete(file_id="file_id") + +print(delete_file_response) +``` + + + diff --git a/virtual_key_old/integrations/llms/openai/fine-tuning.mdx b/virtual_key_old/integrations/llms/openai/fine-tuning.mdx new file mode 100644 index 00000000..6347142c --- /dev/null +++ b/virtual_key_old/integrations/llms/openai/fine-tuning.mdx @@ -0,0 +1,521 @@ +--- +title: "Fine-tune" +description: "Fine-tune your models with OpenAI" +--- + +### Upload a file + + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Add your provider's virtual key +) + +# Upload a file for fine-tuning +file = portkey.files.create( + file=open("dataset.jsonl", "rb"), + purpose="fine-tune" +) + +print(file) +``` + + +```typescript +import { Portkey } from "portkey-ai"; +import * as fs from 'fs'; + +// Initialize the Portkey client +const portkey = Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key +}); + +(async () => { + // Upload a file for fine-tuning + const file = await portkey.files.create({ + file: fs.createReadStream("dataset.jsonl"), + purpose: "fine-tune" + }); + + console.log(file); +})(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + virtual_key="VIRTUAL_KEY", + api_key="PORTKEY_API_KEY" + ) +) + +# Upload a file for fine-tuning +file = openai.files.create( + file=open("dataset.jsonl", "rb"), + purpose="fine-tune" +) + +print(file) +``` + + +```typescript +import OpenAI from 'openai'; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'; +import * as fs from 'fs'; + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + virtualKey: "VIRTUAL_KEY", + apiKey: "PORTKEY_API_KEY" + }) +}); + +(async () => { + // Upload a file for fine-tuning + const file = await openai.files.create({ + file: fs.createReadStream("dataset.jsonl"), + purpose: "fine-tune" + }); + + console.log(file); +})(); +``` + + +```sh +curl -X POST --header 'x-portkey-api-key: ' \ + --header 'x-portkey-virtual-key: ' \ + --form 'file=@dataset.jsonl' \ + --form 'purpose=fine-tune' \ + 'https://api.portkey.ai/v1/files' +``` + + + +### Create a fine-tuning job + + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Add your provider's virtual key +) + +# Create a fine-tuning job +fine_tune_job = portkey.fine_tuning.jobs.create( + model="gpt-3.5-turbo", # Base model to fine-tune + training_file="file_id", # ID of the uploaded training file + validation_file="file_id", # Optional: ID of the uploaded validation file + suffix="finetune_name", # Custom suffix for the fine-tuned model name + hyperparameters={ + "n_epochs": 1 + } +) + +print(fine_tune_job) +``` + + +```typescript +import { Portkey } from "portkey-ai"; + +// Initialize the Portkey client +const portkey = Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key +}); + +(async () => { + // Create a fine-tuning job + const fineTuneJob = await portkey.fineTuning.jobs.create({ + model: "gpt-3.5-turbo", // Base model to fine-tune + training_file: "file_id", // ID of the uploaded training file + validation_file: "file_id", // Optional: ID of the uploaded validation file + suffix: "finetune_name", // Custom suffix for the fine-tuned model name + hyperparameters: { + n_epochs: 1 + } + }); + + console.log(fineTuneJob); +})(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + virtual_key="VIRTUAL_KEY", + api_key="PORTKEY_API_KEY" + ) +) + +# Create a fine-tuning job +fine_tune_job = openai.fine_tuning.jobs.create( + model="gpt-3.5-turbo", # Base model to fine-tune + training_file="file_id", # ID of the uploaded training file + validation_file="file_id", # Optional: ID of the uploaded validation file + suffix="finetune_name", # Custom suffix for the fine-tuned model name + hyperparameters={ + "n_epochs": 1 + } +) + +print(fine_tune_job) +``` + + +```typescript +import OpenAI from 'openai'; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'; + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + virtualKey: "VIRTUAL_KEY", + apiKey: "PORTKEY_API_KEY" + }) +}); + +(async () => { + // Create a fine-tuning job + const fineTuneJob = await openai.fineTuning.jobs.create({ + model: "gpt-3.5-turbo", // Base model to fine-tune + training_file: "file_id", // ID of the uploaded training file + validation_file: "file_id", // Optional: ID of the uploaded validation file + suffix: "finetune_name", // Custom suffix for the fine-tuned model name + hyperparameters: { + n_epochs: 1 + } + }); + + console.log(fineTuneJob); +})(); +``` + + +```sh +curl -X POST --header 'Content-Type: application/json' \ + --header 'x-portkey-api-key: ' \ + --header 'x-portkey-virtual-key: ' \ + --data \ + $'{"model": "", "suffix": "", "training_file": "", "validation_file": "", "method": {"type": "supervised", "supervised": {"hyperparameters": {"n_epochs": 1}}}}\n' \ +'https://api.portkey.ai/v1/fine_tuning/jobs' +``` + + + +### List fine-tuning jobs + + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Add your provider's virtual key +) + +# List all fine-tuning jobs +jobs = portkey.fine_tuning.jobs.list( + limit=10 # Optional: Number of jobs to retrieve (default: 20) +) + +print(jobs) +``` + + +```typescript +import { Portkey } from "portkey-ai"; + +// Initialize the Portkey client +const portkey = Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key +}); + +(async () => { + // List all fine-tuning jobs + const jobs = await portkey.fineTuning.jobs.list({ + limit: 10 // Optional: Number of jobs to retrieve (default: 20) + }); + + console.log(jobs); +})(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + virtual_key="VIRTUAL_KEY", + api_key="PORTKEY_API_KEY" + ) +) + +# List all fine-tuning jobs +jobs = openai.fine_tuning.jobs.list( + limit=10 # Optional: Number of jobs to retrieve (default: 20) +) + +print(jobs) +``` + + +```typescript +import OpenAI from 'openai'; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'; + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + virtualKey: "VIRTUAL_KEY", + apiKey: "PORTKEY_API_KEY" + }) +}); + +(async () => { + // List all fine-tuning jobs + const jobs = await openai.fineTuning.jobs.list({ + limit: 10 // Optional: Number of jobs to retrieve (default: 20) + }); + + console.log(jobs); +})(); +``` + + +```sh +curl -X GET --header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' \ +'https://api.portkey.ai/v1/fine_tuning/jobs' +``` + + + +### Get a fine-tuning job + + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Add your provider's virtual key +) + +# Retrieve a specific fine-tuning job +job = portkey.fine_tuning.jobs.retrieve( + "job_id" # The ID of the fine-tuning job to retrieve +) + +print(job) +``` + + +```typescript +import { Portkey } from "portkey-ai"; + +// Initialize the Portkey client +const portkey = Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key +}); + +(async () => { + // Retrieve a specific fine-tuning job + const job = await portkey.fineTuning.jobs.retrieve( + "job_id" // The ID of the fine-tuning job to retrieve + ); + + console.log(job); +})(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + virtual_key="VIRTUAL_KEY", + api_key="PORTKEY_API_KEY" + ) +) + +# Retrieve a specific fine-tuning job +job = openai.fine_tuning.jobs.retrieve( + fine_tuning_job_id="job_id" # The ID of the fine-tuning job to retrieve +) + +print(job) +``` + + +```typescript +import OpenAI from 'openai'; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'; + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + virtualKey: "VIRTUAL_KEY", + apiKey: "PORTKEY_API_KEY" + }) +}); + +(async () => { + // Retrieve a specific fine-tuning job + const job = await openai.fineTuning.jobs.retrieve( + "job_id" // The ID of the fine-tuning job to retrieve + ); + + console.log(job); +})(); +``` + + +```sh +curl -X GET --header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' \ +'https://api.portkey.ai/v1/fine_tuning/jobs/' +``` + + + +### Cancel a fine-tuning job + + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Add your provider's virtual key +) + +# Cancel a fine-tuning job +cancelled_job = portkey.fine_tuning.jobs.cancel( + "job_id" # The ID of the fine-tuning job to cancel +) + +print(cancelled_job) +``` + + +```typescript +import { Portkey } from "portkey-ai"; + +// Initialize the Portkey client +const portkey = Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key +}); + +(async () => { + // Cancel a fine-tuning job + const cancelledJob = await portkey.fineTuning.jobs.cancel( + "job_id" // The ID of the fine-tuning job to cancel + ); + + console.log(cancelledJob); +})(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + virtual_key="VIRTUAL_KEY", + api_key="PORTKEY_API_KEY" + ) +) + +# Cancel a fine-tuning job +cancelled_job = openai.fine_tuning.jobs.cancel( + "job_id" # The ID of the fine-tuning job to cancel +) + +print(cancelled_job) +``` + + +```typescript +import OpenAI from 'openai'; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'; + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + virtualKey: "VIRTUAL_KEY", + apiKey: "PORTKEY_API_KEY" + }) +}); + +(async () => { + // Cancel a fine-tuning job + const cancelledJob = await openai.fineTuning.jobs.cancel( + "job_id" // The ID of the fine-tuning job to cancel + ); + + console.log(cancelledJob); +})(); +``` + + +```sh +curl -X POST --header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' \ +'https://api.portkey.ai/v1/fine_tuning/jobs//cancel' +``` + + + +Refer to [OpenAI's fine-tuning documentation](https://platform.openai.com/docs/api-reference/fine-tuning) for more information on the parameters and options available. diff --git a/virtual_key_old/integrations/llms/openai/prompt-caching-openai.mdx b/virtual_key_old/integrations/llms/openai/prompt-caching-openai.mdx new file mode 100644 index 00000000..c6b8fad9 --- /dev/null +++ b/virtual_key_old/integrations/llms/openai/prompt-caching-openai.mdx @@ -0,0 +1,253 @@ +--- +title: "Prompt Caching" +--- + +OpenAI now offers prompt caching, a feature that can significantly reduce both latency and costs for your API requests. This feature is particularly beneficial for prompts exceeding 1024 tokens, offering up to an 80% reduction in latency for longer prompts over 10,000 tokens. + +**Prompt Caching is enabled for following models** + +* `gpt-4o (excludes gpt-4o-2024-05-13)` +* `gpt-4o-mini` +* `o1-preview` +* `o1-mini` + +Portkey supports OpenAI's prompt caching feature out of the box. Here is an examples on of how to use it: + + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", + virtual_key="OPENAI_VIRTUAL_KEY", + ) + + # Define tools (for function calling example) + tools = [ + { + "type": "function", + "function": { + "name": "get_weather", + "description": "Get the current weather in a given location", + "parameters": { + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "The city and state, e.g. San Francisco, CA", + }, + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}, + }, + "required": ["location"], + }, + } + } + ] + + + # Example: Function calling with caching + response = portkey.chat.completions.create( + model="gpt-4", + messages=[ + {"role": "system", "content": "You are a helpful assistant that can check the weather."}, + {"role": "user", "content": "What's the weather like in San Francisco?"} + ], + tools=tools, + tool_choice="auto" + ) + print(json.dumps(response.model_dump(), indent=2)) + ``` + + + ```javascript + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your OpenAI Virtual Key + }) + + // Define tools (for function calling example) + const tools = [ + { + "type": "function", + "function": { + "name": "get_weather", + "description": "Get the current weather in a given location", + "parameters": { + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "The city and state, e.g. San Francisco, CA", + }, + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}, + }, + "required": ["location"], + }, + } + } + ]; + + async function Examples() { + + // Example : Function calling with caching + completion = await openai.chat.completions.create({ + messages: [ + {"role": "system", "content": "You are a helpful assistant that can check the weather."}, + {"role": "user", "content": "What's the weather like in San Francisco?"} + ], + model: "gpt-4", + tools: tools, + tool_choice: "auto" + }); + console.log(JSON.stringify(completion, null, 2)); + } + + Examples(); + ``` + + + ```python + from openai import OpenAI + from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + import os + import json + + client = OpenAI( + api_key=os.environ.get("OPENAI_API_KEY"), + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key=os.environ.get("PORTKEY_API_KEY") + ) + ) + + # Define tools (for function calling example) + tools = [ + { + "type": "function", + "function": { + "name": "get_weather", + "description": "Get the current weather in a given location", + "parameters": { + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "The city and state, e.g. San Francisco, CA", + }, + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}, + }, + "required": ["location"], + }, + } + } + ] + + + # Example: Function calling with caching + response = client.chat.completions.create( + model="gpt-4", + messages=[ + {"role": "system", "content": "You are a helpful assistant that can check the weather."}, + {"role": "user", "content": "What's the weather like in San Francisco?"} + ], + tools=tools, + tool_choice="auto" + ) + print(json.dumps(response.model_dump(), indent=2)) + ``` + + + ```javascript + import OpenAI from 'openai'; + import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + + const openai = new OpenAI({ + apiKey: "OPENAI_API_KEY", + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY" + }) + }); + + // Define tools (for function calling example) + const tools = [ + { + "type": "function", + "function": { + "name": "get_weather", + "description": "Get the current weather in a given location", + "parameters": { + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "The city and state, e.g. San Francisco, CA", + }, + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}, + }, + "required": ["location"], + }, + } + } + ]; + + async function Examples() { + + // Example : Function calling with caching + completion = await openai.chat.completions.create({ + messages: [ + {"role": "system", "content": "You are a helpful assistant that can check the weather."}, + {"role": "user", "content": "What's the weather like in San Francisco?"} + ], + model: "gpt-4", + tools: tools, + tool_choice: "auto" + }); + console.log(JSON.stringify(completion, null, 2)); + } + + Examples(); + ``` + + + +### What can be cached + +* **Messages:** The complete messages array, encompassing system, user, and assistant interactions. +* **Images:** Images included in user messages, either as links or as base64-encoded data, as well as multiple images can be sent. Ensure the detail parameter is set identically, as it impacts image tokenization. +* **Tool use:** Both the messages array and the list of available `tools` can be cached, contributing to the minimum 1024 token requirement. +* **Structured outputs:** The structured output schema serves as a prefix to the system message and can be cached. + +### What's Not Supported + +* Completions API (only Chat Completions API is supported) +* Streaming responses (caching works, but streaming itself is not affected) + +### Monitoring Cache Performance + +Prompt caching requests & responses based on OpenAI's calculations here: + + + + + +All requests, including those with fewer than 1024 tokens, will display a `cached_tokens` field of the `usage.prompt_tokens_details` [chat completions object](https://platform.openai.com/docs/api-reference/chat/object) indicating how many of the prompt tokens were a cache hit. + +For requests under 1024 tokens, `cached_tokens` will be zero. + + + +

cached_tokens field of the usage.prompt_tokens_details

+ + +**Key Features:** + +* Reduced Latency: Especially significant for longer prompts. +* Lower Costs: Cached portions of prompts are billed at a discounted rate. +* Improved Efficiency: Allows for more context in prompts without increasing costs proportionally. +* Zero Data Retention: No data is stored during the caching process, making it eligible for zero data retention policies. diff --git a/virtual_key_old/integrations/llms/openai/structured-outputs.mdx b/virtual_key_old/integrations/llms/openai/structured-outputs.mdx new file mode 100644 index 00000000..ed2c65e4 --- /dev/null +++ b/virtual_key_old/integrations/llms/openai/structured-outputs.mdx @@ -0,0 +1,245 @@ +--- +title: 'Structured Outputs' +description: "Structured Outputs ensure that the model always follows your supplied [JSON schema](https://json-schema.org/overview/what-is-jsonschema). Portkey supports OpenAI's Structured Outputs feature out of the box with our SDKs & APIs. +" +--- + + + +Structured Outputs is different from OpenAI's `JSON Mode` as well as `Function Calling`. [Check out this table](#difference-between-structured-outputs-json-mode-and-function-calling) for a quick comparison. + + +Portkey SDKs for [Python](https://github.com/openai/openai-python/blob/main/helpers.md#structured-outputs-parsing-helpers) and [JavaScript](https://github.com/openai/openai-node/blob/master/helpers.md#structured-outputs-parsing-helpers) also make it easy to define object schemas using [Pydantic](https://docs.pydantic.dev/latest/) and [Zod](https://zod.dev/) respectively. Below, you can see how to extract information from unstructured text that conforms to a schema defined in code. + + + + ```python + from portkey_ai import Portkey + from pydantic import BaseModel + + class Step(BaseModel): + explanation: str + output: str + + class MathReasoning(BaseModel): + steps: list[Step] + final_answer: str + + portkey = Portkey( + api_key="PORTKEY_API_KEY", + virtual_key="OPENAI_VIRTUAL_KEY" + ) + + completion = portkey.beta.chat.completions.parse( + model="gpt-4o-2024-08-06", + messages=[ + {"role": "system", "content": "You are a helpful math tutor. Guide the user through the solution step by step."}, + {"role": "user", "content": "how can I solve 8x + 7 = -23"} + ], + response_format=MathReasoning, + ) + + print(completion.choices[0].message) + print(completion.choices[0].message.parsed) + ``` + + + ```typescript + import { Portkey } from 'portkey-ai'; + import { z } from 'zod'; + import { zodResponseFormat } from "openai/helpers/zod"; + + const MathReasoning = z.object({ + steps: z.array(z.object({ explanation: z.string(), output: z.string() })), + final_answer: z.string() + }); + + const portkey = new Portkey({ + apiKey: "YOUR_API_KEY", + virtualKey: "YOUR_VIRTUAL_KEY" + }); + + async function runMathTutor() { + try { + const completion = await portkey.chat.completions.create({ + model: "gpt-4o-2024-08-06", + messages: [ + { role: "system", content: "You are a helpful math tutor." }, + { role: "user", content: "Solve 8x + 7 = -23" } + ], + response_format: zodResponseFormat(MathReasoning, "MathReasoning") + }); + + console.log(completion.choices[0].message.content); + } catch (error) { + console.error("Error:", error); + } + } + + runMathTutor(); + ``` + + + +The second approach, shown in the subsequent examples, uses a JSON schema directly in the API call. This method is more portable across different languages and doesn't require additional libraries, but lacks the integrated type checking of the Pydantic/Zod approach. Choose the method that best fits your project's needs and language ecosystem. + + + + ```typescript + import Portkey from "portkey-ai"; + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "OPENAI_VIRTUAL_KEY", + }); + + async function main() { + const completion = await portkey.chat.completions.create({ + model: "gpt-4o-2024-08-06", + messages: [ + { role: "system", content: "Extract the event information." }, + { + role: "user", + content: "Alice and Bob are going to a science fair on Friday.", + }, + ], + response_format: { + type: "json_schema", + json_schema: { + name: "math_reasoning", + schema: { + type: "object", + properties: { + steps: { + type: "array", + items: { + type: "object", + properties: { + explanation: { type: "string" }, + output: { type: "string" }, + }, + required: ["explanation", "output"], + additionalProperties: false, + }, + }, + final_answer: { type: "string" }, + }, + required: ["steps", "final_answer"], + additionalProperties: false, + }, + strict: true, + }, + }, + }); + const event = completion.choices[0].message?.content; + console.log(event); + } + + main(); + ``` + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", + virtual_key="OPENAI_VIRTUAL_KEY" + ) + + completion = portkey.chat.completions.create( + model="gpt-4o-2024-08-06", + messages=[ + {"role": "system", "content": "Extract the event information."}, + {"role": "user", "content": "A meteor the size of 1000 football stadiums will hit earth this Sunday"}, + ], + response_format={ + "type": "json_schema", + "json_schema": { + "name": "math_reasoning", + "schema": { + "type": "object", + "properties": { + "steps": { + "type": "array", + "items": { + "type": "object", + "properties": { + "explanation": { "type": "string" }, + "output": { "type": "string" } + }, + "required": ["explanation", "output"], + "additionalProperties": False + } + }, + "final_answer": { "type": "string" } + }, + "required": ["steps", "final_answer"], + "additionalProperties": False + }, + "strict": True + } + }, + ) + + print(completion.choices[0].message.content) + ``` + + + ```sh + curl https://api.portkey.ai/v1/chat/completions \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-virtual-key: $OPENAI_VIRTUAL_KEY" \ + -H "Content-Type: application/json" \ + -d '{ + "model": "gpt-4o-2024-08-06", + "messages": [ + { + "role": "system", + "content": "You are a helpful math tutor. Guide the user through the solution step by step." + }, + { + "role": "user", + "content": "how can I solve 8x + 7 = -23" + } + ], + "response_format": { + "type": "json_schema", + "json_schema": { + "name": "math_reasoning", + "schema": { + "type": "object", + "properties": { + "steps": { + "type": "array", + "items": { + "type": "object", + "properties": { + "explanation": { "type": "string" }, + "output": { "type": "string" } + }, + "required": ["explanation", "output"], + "additionalProperties": false + } + }, + "final_answer": { "type": "string" } + }, + "required": ["steps", "final_answer"], + "additionalProperties": false + }, + "strict": true + } + } + }' + ``` + + + +## Difference Between Structured Outputs, JSON Mode, and Function Calling + +* If you are connecting the model to tools, functions, data, etc. in your system, then you should use **function calling.** +* And if you want to structure the model's output when it responds to the user, then you should use a structured `response_format`. + * In `response_format`, you can set it as `{ "type": "json_object" }` to enable the [JSON Mode](https://platform.openai.com/docs/guides/structured-outputs/json-mode). + * And you can set it as `{ "type": "json_schema" }` to use the [Structured Outputs Mode described above](https://platform.openai.com/docs/guides/structured-outputs). + +For more, refer to OpenAI's [detailed documentation on Structured Outputs here](https://platform.openai.com/docs/guides/structured-outputs/supported-schemas). diff --git a/virtual_key_old/integrations/llms/openai2.mdx b/virtual_key_old/integrations/llms/openai2.mdx new file mode 100644 index 00000000..145f340a --- /dev/null +++ b/virtual_key_old/integrations/llms/openai2.mdx @@ -0,0 +1,698 @@ +--- +title: 'OpenAI' +description: Integrate OpenAI with Portkey to get production metrics for your requests and make chat completions, audio, image generation, structured outputs, function calling, fine-tuning, batch, and more requests. +--- + + +Provider Slug: `openai` + + +## Overview + +Portkey integrates with [OpenAI](https://platform.openai.com/docs/api-reference/introduction)'s APIs to help you create production-grade AI sppd with enhanced reliability, observability, and governance features. + +## Getting Started + + + + Visit the [OpenAI dashboard](https://platform.openai.com/account/api-keys) to generate your API key. + + + + Portkey's virtual key vault simplifies your interaction with OpenAI. Virtual keys act as secure aliases for your actual API keys, offering enhanced security and easier management through [budget limits](/docs/product/observability/budget-limits) to control your API usage. + + Use the Portkey app to create a [virtual key](/docs/product/ai-gateway/virtual-keys) associated with your OpenAI API key. + + + + Now that you have your virtual key, set up the Portkey client: + + ### Portkey Hosted App + Use the Portkey API key and the OpenAI virtual key to initialize the client in your preferred programming language. + + + ```python Python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for OpenAI + ) + ``` + + ```javascript Node.js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your OpenAI Virtual Key + }) + ``` + + + ### Open Source Use + Alternatively, use Portkey's Open Source AI Gateway to enhance your app's reliability with minimal code: + + + ```python Python + from portkey_ai import Portkey, PORTKEY_GATEWAY_URL + + portkey = Portkey( + api_key="dummy", # Replace with your Portkey API key + base_url=PORTKEY_GATEWAY_URL, + Authorization="OPENAI_API_KEY", # Replace with your OpenAI API Key + provider="openai" + ) + ``` + + ```javascript Node.js + import Portkey, { PORTKEY_GATEWAY_URL } from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "dummy", // Replace with your Portkey API key + baseUrl: PORTKEY_GATEWAY_URL, + Authorization: "OPENAI_API_KEY", // Replace with your OpenAI API Key + provider: "openai" + }) + ``` + + + + +๐Ÿ”ฅ That's it! You've integrated Portkey into your application with just a few lines of code. Now let's explore making requests using the Portkey client. + +## Supported Models + + +- GPT-4o +- GPT-4o mini +- o1-preview +- o1-mini +- GPT-4 Turbo +- GPT-4 +- GPT-3.5 Turbo +- DALLยทE +- TTS (Text-to-Speech) +- Whisper +- Embeddings +- Moderation +- GPT base + + +## OpenAI Supported Features + +### Chat Completions + +Generate chat completions using OpenAI models through Portkey: + + +```python Python +completion = portkey.chat.completions.create( + messages=[{"role": "user", "content": "Say this is a test"}], + model="gpt-4o" +) + +print(completion.choices[0].message.content) +``` + +```javascript Node.js +const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'gpt-4o', +}); + +console.log(chatCompletion.choices[0].message.content); +``` + +```curl REST +curl -X POST "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -d '{ + "messages": [{"role": "user", "content": "Say this is a test"}], + "model": "gpt-4o" + }' +``` + + +### Streaming + +Stream responses for real-time output in your applications: + + +```python Python +chat_complete = portkey.chat.completions.create( + model="gpt-4o", + messages=[{"role": "user", "content": "Say this is a test"}], + stream=True +) + +for chunk in chat_complete: + print(chunk.choices[0].delta.content or "", end="", flush=True) +``` + +```javascript Node.js +const stream = await portkey.chat.completions.create({ + model: 'gpt-4', + messages: [{ role: 'user', content: 'Say this is a test' }], + stream: true, +}); + +for await (const chunk of stream) { + process.stdout.write(chunk.choices[0]?.delta?.content || ''); +} +``` + +```curl REST +curl -X POST "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -d '{ + "model": "gpt-4o", + "messages": [{"role": "user", "content": "Say this is a test"}], + "stream": true + }' +``` + + +### Function Calling + +Leverage OpenAI's function calling capabilities through Portkey: + + +```javascript Node.js +let tools = [{ + type: "function", + function: { + name: "getWeather", + description: "Get the current weather", + parameters: { + type: "object", + properties: { + location: { type: "string", description: "City and state" }, + unit: { type: "string", enum: ["celsius", "fahrenheit"] } + }, + required: ["location"] + } + } +}]; + +let response = await portkey.chat.completions.create({ + model: "gpt-4o", + messages: [ + { role: "system", content: "You are a helpful assistant." }, + { role: "user", content: "What's the weather like in Delhi - respond in JSON" } + ], + tools, + tool_choice: "auto", +}); + +console.log(response.choices[0].finish_reason); +``` + +```python Python +tools = [{ + "type": "function", + "function": { + "name": "getWeather", + "description": "Get the current weather", + "parameters": { + "type": "object", + "properties": { + "location": {"type": "string", "description": "City and state"}, + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]} + }, + "required": ["location"] + } + } +}] + +response = portkey.chat.completions.create( + model="gpt-4o", + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "What's the weather like in Delhi - respond in JSON"} + ], + tools=tools, + tool_choice="auto" +) + +print(response.choices[0].finish_reason) +``` + +```curl REST +curl -X POST "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -d '{ + "model": "gpt-4o", + "messages": [ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "What'\''s the weather like in Delhi - respond in JSON"} + ], + "tools": [{ + "type": "function", + "function": { + "name": "getWeather", + "description": "Get the current weather", + "parameters": { + "type": "object", + "properties": { + "location": {"type": "string", "description": "City and state"}, + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]} + }, + "required": ["location"] + } + } + }], + "tool_choice": "auto" + }' +``` + + +### Vision + +Process images alongside text using OpenAI's vision capabilities: + + +```python Python +response = portkey.chat.completions.create( + model="gpt-4-vision-preview", + messages=[ + { + "role": "user", + "content": [ + {"type": "text", "text": "What's in this image?"}, + { + "type": "image_url", + "image_url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg", + }, + ], + } + ], + max_tokens=300, +) + +print(response) +``` + +```javascript Node.js +const response = await portkey.chat.completions.create({ + model: "gpt-4-vision-preview", + messages: [ + { + role: "user", + content: [ + { type: "text", text: "What's in this image?" }, + { + type: "image_url", + image_url: "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg", + }, + ], + }, + ], + max_tokens: 300, +}); + +console.log(response); +``` + +```curl REST +curl -X POST "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -d '{ + "model": "gpt-4-vision-preview", + "messages": [ + { + "role": "user", + "content": [ + {"type": "text", "text": "What'\''s in this image?"}, + { + "type": "image_url", + "image_url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg" + } + ] + } + ], + "max_tokens": 300 + }' +``` + + +### Embeddings + +Generate embeddings for text using OpenAI's embedding models: + + +```python Python +response = portkey.embeddings.create( + input="Your text string goes here", + model="text-embedding-3-small" +) + +print(response.data[0].embedding) +``` + +```javascript Node.js +const response = await portkey.embeddings.create({ + input: "Your text string goes here", + model: "text-embedding-3-small" +}); + +console.log(response.data[0].embedding); +``` + +```curl REST +curl -X POST "https://api.portkey.ai/v1/embeddings" \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -d '{ + "input": "Your text string goes here", + "model": "text-embedding-3-small" + }' +``` + + +### Transcription and Translation + +Portkey supports both `Transcription` and `Translation` methods for STT models: + + +```python Python +audio_file= open("/path/to/file.mp3", "rb") + +# Transcription +transcription = portkey.audio.transcriptions.create( + model="whisper-1", + file=audio_file +) +print(transcription.text) + +# Translation +translation = portkey.audio.translations.create( + model="whisper-1", + file=audio_file +) +print(translation.text) +``` + +```javascript Node.js +import fs from "fs"; + +// Transcription +async function transcribe() { + const transcription = await portkey.audio.transcriptions.create({ + file: fs.createReadStream("/path/to/file.mp3"), + model: "whisper-1", + }); + console.log(transcription.text); +} +transcribe(); + +// Translation +async function translate() { + const translation = await portkey.audio.translations.create({ + file: fs.createReadStream("/path/to/file.mp3"), + model: "whisper-1", + }); + console.log(translation.text); +} +translate(); +``` + +```curl REST +# Transcription +curl -X POST "https://api.portkey.ai/v1/audio/transcriptions" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -H "Content-Type: multipart/form-data" \ + -F "file=@/path/to/file.mp3" \ + -F "model=whisper-1" + +# Translation +curl -X POST "https://api.portkey.ai/v1/audio/translations" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -H "Content-Type: multipart/form-data" \ + -F "file=@/path/to/file.mp3" \ + -F "model=whisper-1" +``` + + +### Text to Speech + +Convert text to speech using OpenAI's TTS models: + + +```python Python +from pathlib import Path + +speech_file_path = Path(__file__).parent / "speech.mp3" +response = portkey.audio.speech.create( + model="tts-1", + voice="alloy", + input="Today is a wonderful day to build something people love!" +) + +with open(speech_file_path, "wb") as f: + f.write(response.content) +``` + +```javascript Node.js +import path from 'path'; +import fs from 'fs'; + +const speechFile = path.resolve("./speech.mp3"); + +async function main() { + const mp3 = await portkey.audio.speech.createCertainly! I'll continue with the Text to Speech section and then move on to the additional features and sections: + +```javascript Node.js +({ + model: "tts-1", + voice: "alloy", + input: "Today is a wonderful day to build something people love!", + }); + const buffer = Buffer.from(await mp3.arrayBuffer()); + await fs.promises.writeFile(speechFile, buffer); +} + +main(); +``` + +```curl REST +curl -X POST "https://api.portkey.ai/v1/audio/speech" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -H "Content-Type: application/json" \ + -d '{ + "model": "tts-1", + "voice": "alloy", + "input": "Today is a wonderful day to build something people love!" + }' \ + --output speech.mp3 +``` + + +### Prompt Caching + +Implement prompt caching to improve performance and reduce costs: + + + Learn how to implement prompt caching for OpenAI models with Portkey. + + +### Structured Output + +Use structured outputs for more consistent and parseable responses: + + + Discover how to use structured outputs with OpenAI models in Portkey. + + +## Supported Endpoints and Parameters + +| Endpoint | Supported Parameters | +| ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `complete` | model, prompt, max_tokens, temperature, top_p, n, stream, logprobs, echo, stop, presence_penalty, frequency_penalty, best_of, logit_bias, user, seed, suffix | +| `embed` | model, input, encoding_format, dimensions, user | +| `chatComplete` | model, messages, functions, function_call, max_tokens, temperature, top_p, n, stream, stop, presence_penalty, frequency_penalty, logit_bias, user, seed, tools, tool_choice, response_format, logprobs, top_logprobs, stream_options, service_tier, parallel_tool_calls, max_completion_tokens | +| `imageGenerate` | prompt, model, n, quality, response_format, size, style, user | +| `createSpeech` | model, input, voice, response_format, speed | +| `createTranscription` | All parameters supported | +| `createTranslation` | All parameters supported | + +--- + +# Portkey's Advanced Features + +## Track End-User IDs + +Portkey allows you to track user IDs passed with the user parameter in OpenAI requests, enabling you to monitor user-level costs, requests, and more: + + +```python Python +response = portkey.chat.completions.create( + model="gpt-4o", + messages=[{"role": "user", "content": "Say this is a test"}], + user="user_123456" +) +``` + +```javascript Node.js +const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: "user", content: "Say this is a test" }], + model: "gpt-4o", + user: "user_12345", +}); +``` + +```curl REST +curl -X POST "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -d '{ + "model": "gpt-4o", + "messages": [{"role": "user", "content": "Say this is a test"}], + "user": "user_123456" + }' +``` + + +When you include the user parameter in your requests, Portkey logs will display the associated user ID, as shown in the image below: + +Portkey Logs with User ID + +In addition to the `user` parameter, Portkey allows you to send arbitrary custom metadata with your requests. This powerful feature enables you to associate additional context or information with each request, which can be useful for analysis, debugging, or other custom use cases. + + + + Explore how to use custom metadata to enhance your request tracking and analysis. + + + +## Using The Gateway Config + +Here's a simplified version of how to use Portkey's Gateway Configuration: + + + + You can create a Gateway configuration using the Portkey Config Dashboard or by writing a JSON configuration in your code. In this example, requests are routed based on the user's subscription plan (paid or free). + + ```json + config = { + "strategy": { + "mode": "conditional", + "conditions": [ + { + "query": { "metadata.user_plan": { "$eq": "paid" } }, + "then": "gpt4o" + }, + { + "query": { "metadata.user_plan": { "$eq": "free" } }, + "then": "gpt-3.5" + } + ], + "default": "base-gpt4" + }, + "targets": [ + { + "name": "gpt4o", + "virtual_key": "xx" + }, + { + "name": "gpt-3.5", + "virtual_key": "yy" + } + ] + } + ``` + + + + When a user makes a request, it will pass through Portkey's AI Gateway. Based on the configuration, the Gateway routes the request according to the user's metadata. + Conditional Routing Diagram + + + + Pass the Gateway configuration to your Portkey client. You can either use the config object or the Config ID from Portkey's hosted version. + + + ```python Python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", + virtual_key="VIRTUAL_KEY", + config=portkey_config + ) + ``` + + ```javascript Node.js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "VIRTUAL_KEY", + config: portkeyConfig + }) + ``` + + + + +That's it! Portkey seamlessly allows you to make your AI app more robust using built-in gateway features. Learn more about advanced gateway features: + + + + Distribute requests across multiple targets based on defined weights. + + + Automatically switch to backup targets if the primary target fails. + + + Route requests to different targets based on specified conditions. + + + Enable caching of responses to improve performance and reduce costs. + + + +## Guardrails + +Portkey's AI gateway enables you to enforce input/output checks on requests by applying custom hooks before and after processing. Protect your user's/company's data by using PII guardrails and many more available on Portkey Guardrails: + +```json +{ + "virtual_key":"openai-xxx", + "before_request_hooks": [{ + "id": "input-guardrail-id-xx" + }], + "after_request_hooks": [{ + "id": "output-guardrail-id-xx" + }] +} +``` + + + Explore Portkey's guardrail features to enhance the security and reliability of your AI applications. + + +## Next Steps + +The complete list of features supported in the SDK are available in our comprehensive documentation: + + + Explore the full capabilities of the Portkey SDK and how to leverage them in your projects. + + +--- + +## Limitations + + +Portkey does not support the following OpenAI features: +- Streaming for audio endpoints +- Chat completions feedback API +- File management endpoints + + +For the most up-to-date information on supported features and endpoints, please refer to our [API Reference](/docs/api-reference/introduction). diff --git a/virtual_key_old/integrations/llms/openai3.mdx b/virtual_key_old/integrations/llms/openai3.mdx new file mode 100644 index 00000000..cdb894b3 --- /dev/null +++ b/virtual_key_old/integrations/llms/openai3.mdx @@ -0,0 +1,1492 @@ +--- +title: 'OpenAI' +description: Complete guide to integrate OpenAI API with Portkey. Support for gpt-4o, o1, chat completions, vision, and audio APIs with built-in reliability and monitoring features. +--- + + +[Latest Pricing](https://models.portkey.ai/providers/openai) | [API Status](https://status.portkey.ai/) | [Supported Endpoints](/api-reference/inference-api/supported-providers) + + +OpenAI's API offers powerful language, embedding, and multimodal models (gpt-4o, o1, whisper, dall-e, etc.). Portkey makes your OpenAI requests production-ready with its observability, fallbacks, guardrails, and more features. Portkey also lets you use OpenAI API's other capabilities like + + +## Integrate +Just paste your OpenAI API Key from [here](https://platform.openai.com/account/api-keys) to [Portkey](https://app.portkey.ai/virtual-keys) to create your Virtual Key. + + + +Your OpenAI personal or service account API keys can be saved to Portkey. Additionally, your **[OpenAI Admin API Keys](https://platform.openai.com/settings/organization/admin-keys)** can also be saved to Portkey so that you can route to OpenAI Admin routes through Portkey API. + + +Optional +- Add your OpenAI organization and project ID details: ([Docs](#openai-projects-and-organizations)) +- Directly use OpenAI API key without the Virtual Key: ([Docs](/api-reference/inference-api/headers#1-provider-slug-auth)) +- Create a short-lived virtual key OR one with usage/rate limits: ([Docs](/product/ai-gateway/virtual-keys)) + + +Note: While OpenAI supports setting budget & rate limits at Project level, on Portkey, along with that, you can set granular budget & rate limits per each key. + + +## Sample Request +Portkey is a drop-in replacement for OpenAI. You can make request using the official OpenAI or Portkey SDKs. + +Popular libraries & agent frameworks like LangChain, CrewAI, AutoGen, etc. are [also supported](#popular-libraries). +All Azure OpenAI models & endpoints are [also supported](/integrations/llms/azure-openai) + + + +Install the Portkey SDK with npm +```sh +npm install portkey-ai +``` + +```ts Chat Completions +import Portkey from 'portkey-ai'; + +const client = new Portkey({ + apiKey: 'PORTKEY_API_KEY', + virtualKey: 'PROVIDER_VIRTUAL_KEY' +}); + +async function main() { + const response = await client.chat.completions.create({ + messages: [{ role: "user", content: "Bob the builder.." }], + model: "gpt-4o", + }); + + console.log(response.choices[0].message.content); +} + +main(); +``` +```ts Image Generations +``` +```ts Create Embeddings +``` + + + +Install the Portkey SDK with pip +```sh +pip install portkey-ai +``` + +```py Chat Completions +from portkey_ai import Portkey + +client = Portkey( + api_key = "PORTKEY_API_KEY", + virtual_key = "PROVIDER_VIRTUAL_KEY" +) + +response = client.chat.completions.create( + model="gpt-4o", + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "Hello!"} + ] +) + +print(response.choices[0].message) +``` +```py Image Generations +``` +```py Create Embeddings +``` + + + + +```sh Chat Completions +curl https://api.portkey.ai/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-virtual-key: $PORTKEY_PROVIDER_VIRTUAL_KEY" \ + -d '{ + "model": "gpt-4o", + "messages": [ + { "role": "user", "content": "Hello!" } + ] + }' +``` +```sh Image Generations +``` +```sh Create Embeddings +``` + + + +Install the OpenAI & Portkey SDKs with pip +```sh +pip install openai portkey-ai +``` + +```py Chat Completions +from openai import OpenAI +from portkey_ai import createHeaders, PORTKEY_GATEWAY_URL + +client = OpenAI( + api_key="xx", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key="PORTKEY_API_KEY", + virtual_key="OPENAI_VIRTUAL_KEY" + ) +) + +completion = client.chat.completions.create( + model="gpt-4o", + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "Hello!"} + ] +) + +print(completion.choices[0].message) +``` +```py Image Generations +``` +```py Create Embeddings +``` + + + +Install the OpenAI & Portkey SDKs with npm +```sh +npm install openai portkey-ai +``` + +```ts Chat Completions +import OpenAI from 'openai'; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'xx', + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "OPENAI_VIRTUAL_KEY" + }) +}); + +async function main() { + const completion = await openai.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'gpt-4o', + }); + + console.log(chatCompletion.choices); +} + +main(); +``` +```ts Image Generations +``` +```ts Create Embeddings +``` + + + + ... + ```ts + ``` + ... + + + +**Viewing the Log** + +Portkey will log your request and give you useful data such as timestamp, request type, LLM used, tokens generated, and cost. For multimodal models, Portkey will also show the image sent with vision/image models, as well as the image generated. + + + + + +## Local Setup +If you do not want to use Portkey's hosted API, you can also run Portkey locally: + + + +Portkey runs on our popular [open source Gateway](https://git.new/portkey). You can spin it up locally to make requests without sending them to the Portkey API. + +```sh Install the Gateway +npx @portkey-ai/gateway +``` + +```sh Docker Image +npx @portkey-ai/gateway +``` + +| Your Gateway is running on http://localhost:8080/v1 ๐Ÿš€ | | +| - | - | + +Then, just change the `baseURL` to the local Gateway URL, and make requests: + +```ts NodeJS +import Portkey from 'portkey-ai'; + +const client = new Portkey({ + baseUrl: 'http://localhost:8080/v1', + apiKey: 'PORTKEY_API_KEY', + virtualKey: 'PROVIDER_VIRTUAL_KEY' +}); + +async function main() { + const response = await client.chat.completions.create({ + messages: [{ role: "user", content: "Bob the builder.." }], + model: "gpt-4o", + }); + + console.log(response.choices[0].message.content); +} + +main(); +``` +```py Python +from portkey_ai import Portkey + +client = Portkey( + base_url = 'http://localhost:8080/v1', + api_key = "PORTKEY_API_KEY", + virtual_key = "PROVIDER_VIRTUAL_KEY" +) + +response = client.chat.completions.create( + model="gpt-4o", + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "Hello!"} + ] +) + +print(response.choices[0].message) +``` +```sh cURL +curl http://localhost:8080/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-virtual-key: $PORTKEY_PROVIDER_VIRTUAL_KEY" \ + -d '{ + "model": "gpt-4o", + "messages": [ + { "role": "user", "content": "Hello!" } + ] + }' +``` +```py OpenAI Python SDK +from openai import OpenAI +from portkey_ai import createHeaders, PORTKEY_GATEWAY_URL + +client = OpenAI( + api_key="xx", + base_url="https://localhost:8080/v1", + default_headers=createHeaders( + api_key="PORTKEY_API_KEY", + virtual_key="OPENAI_VIRTUAL_KEY" + ) +) + +completion = client.chat.completions.create( + model="gpt-4o", + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "Hello!"} + ] +) + +print(completion.choices[0].message) +``` +```ts OpenAI NodeJS SDK +import OpenAI from 'openai'; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'xx', + baseURL: 'https://localhost:8080/v1', + defaultHeaders: createHeaders({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "OPENAI_VIRTUAL_KEY" + }) +}); + +async function main() { + const completion = await openai.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'gpt-4o', + }); + + console.log(chatCompletion.choices); +} + +main(); +``` + + +**On-Prem Deployment (AWS, GCP, Azure)** +Portkey's data & control planes can be fully deployed on-prem with the Enterprise license. + + + +--- + +## Support for OpenAI Capabilities + +Portkey works with *all* of OpenAI's endpoints and supports all OpenAI capabilities like prompt caching, structured outputs, and more. + + + Enables models to interact with external tools by declaring functions that the model can invoke based on conversation context. + Returns model responses in predefined formats (JSON/XML) for consistent, parseable application integration. + Analyzes images alongside text, enabling visual understanding and question-answering through URL or base64 inputs. + Transforms text into numerical vectors for semantic search, clustering, and recommendations. + Automatically reuses results from similar API requests to reduce latency and costs, with no setup required. + Creates and modifies images using DALLยทE models, with DALLยทE 3 for generation and DALLยทE 2 for editing. + Converts audio to text using Whisper model, supporting multiple languages and formats. + Transforms text into natural speech using six voices, with streaming support and multiple audio formats. + Powers low-latency, multi-modal conversations through WebRTC and WebSocket connections. + Screens text content for harmful or inappropriate material. + Provides step-by-step problem-solving through structured logical analysis. + Shows probability distributions of possible responses with confidence levels. + Customizes models on specific datasets for improved domain performance. + Offers managed, stateful AI agents with tool use and conversation memory. + Processes large volumes of requests efficiently in batch mode. + + +Find examples for each below: + +--- + +### OpenAI Tool Calling +Tool calling feature lets models trigger external tools based on conversation context. You define available functions, the model chooses when to use them, and your application executes them and returns results. + +Portkey supports OpenAI Tool Calling and makes it interoperable across multiple providers. With Portkey Prompts, you can templatize various your prompts & tool schemas as well. + + +```javascript Get Weather Tool +let tools = [{ + type: "function", + function: { + name: "getWeather", + description: "Get the current weather", + parameters: { + type: "object", + properties: { + location: { type: "string", description: "City and state" }, + unit: { type: "string", enum: ["celsius", "fahrenheit"] } + }, + required: ["location"] + } + } +}]; + +let response = await portkey.chat.completions.create({ + model: "gpt-4o", + messages: [ + { role: "system", content: "You are a helpful assistant." }, + { role: "user", content: "What's the weather like in Delhi - respond in JSON" } + ], + tools, + tool_choice: "auto", +}); + +console.log(response.choices[0].finish_reason); +``` + + +```python Get Weather Tool +tools = [{ + "type": "function", + "function": { + "name": "getWeather", + "description": "Get the current weather", + "parameters": { + "type": "object", + "properties": { + "location": {"type": "string", "description": "City and state"}, + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]} + }, + "required": ["location"] + } + } +}] + +response = portkey.chat.completions.create( + model="gpt-4o", + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "What's the weather like in Delhi - respond in JSON"} + ], + tools=tools, + tool_choice="auto" +) + +print(response.choices[0].finish_reason) +``` + + +```curl Get Weather Tool +curl -X POST "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -d '{ + "model": "gpt-4o", + "messages": [ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "What'\''s the weather like in Delhi - respond in JSON"} + ], + "tools": [{ + "type": "function", + "function": { + "name": "getWeather", + "description": "Get the current weather", + "parameters": { + "type": "object", + "properties": { + "location": {"type": "string", "description": "City and state"}, + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]} + }, + "required": ["location"] + } + } + }], + "tool_choice": "auto" + }' +``` + + + + + +**Tracing the Request** + +On Portkey you can easily trace the whole tool call - from defining tool schemas to getting the final LLM output: + + + + + +--- + +### OpenAI Structured Outputs + +Use structured outputs for more consistent and parseable responses: + + + Discover how to use structured outputs with OpenAI models in Portkey. + + +--- + +### OpenAI Vision + +OpenAI's vision models can analyze images alongside text, enabling visual question-answering capabilities. Images can be provided via URLs or base64 encoding in user messages. + + +```py Python +response = portkey.chat.completions.create( + model="gpt-4-vision-preview", + messages=[ + { + "role": "user", + "content": [ + {"type": "text", "text": "What's in this image?"}, + { + "type": "image_url", + "image_url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg", + }, + ], + } + ], + max_tokens=300, +) + +print(response) +``` + +```ts Node.js +const response = await portkey.chat.completions.create({ + model: "gpt-4-vision-preview", + messages: [ + { + role: "user", + content: [ + { type: "text", text: "What's in this image?" }, + { + type: "image_url", + image_url: "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg", + }, + ], + }, + ], + max_tokens: 300, +}); + +console.log(response); +``` + +```sh cURL +curl -X POST "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -d '{ + "model": "gpt-4-vision-preview", + "messages": [ + { + "role": "user", + "content": [ + {"type": "text", "text": "What'\''s in this image?"}, + { + "type": "image_url", + "image_url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg" + } + ] + } + ], + "max_tokens": 300 + }' +``` + + +**Tracing Vision Requests** + +You can see the image(s) sent on your Portkey log: + + + + + +**Uploading Base64 encoded images** + +If you have an image or set of images locally, you can pass those to the model in base 64 encoded format. [Check out this example from OpenAI on how to do this](https://platform.openai.com/docs/guides/vision#uploading-base64-encoded-images). + + +[Vision Model Limitation](#limitations-for-vision-requests) | [Vision FAQs](#vision-faqs) + + +--- + +### OpenAI Embeddings + +OpenAI's embedding models (like `text-embedding-3-small`) transform text inputs into lists of floating point numbers - smaller distances between vectors indicate higher text similarity. They power use cases like semantic search, content clustering, recommendations, and anomaly detection. + +Simply send text to the embeddings API endpoint to generate these vectors for your applications. + + +```python Python +response = portkey.embeddings.create( + input="Your text string goes here", + model="text-embedding-3-small" +) + +print(response.data[0].embedding) +``` + +```javascript Node.js +const response = await portkey.embeddings.create({ + input: "Your text string goes here", + model: "text-embedding-3-small" +}); + +console.log(response.data[0].embedding); +``` + +```curl REST +curl -X POST "https://api.portkey.ai/v1/embeddings" \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -d '{ + "input": "Your text string goes here", + "model": "text-embedding-3-small" + }' +``` + + + + [Embedding FAQs](#embedding-faqs) + + +--- + +### OpenAI Prompt Caching + +Prompt caching automatically reuses results from similar API requests, reducing latency by up to 80% and costs by 50%. This feature works by default for all OpenAI API calls, requires no setup, and has no additional fees. + +Portkey accurately logs the usage statistics and costs for your cached requests. + + + Read more about OpenAI Prompt Caching here. + + + + [Prompt Caching Limitations](/integrations/llms/openai/prompt-caching-openai#what-can-be-cached) | [Prompt Caching FAQs](#prompt-caching-faqs) + +--- + +### OpenAI Image Generations (DALL-E) + +OpenAI's Images API enables AI-powered image generation, manipulation, and variation creation for creative and commercial applications. Whether you're building image generation features, editing tools, or creative applications, the API provides powerful visual AI capabilities through DALLยทE models. + +The API offers three core capabilities: +- Generate new images from text prompts (DALLยทE 3, DALLยทE 2) +- Edit existing images with text-guided replacements (DALLยทE 2) +- Create variations of existing images (DALLยทE 2) + + + ```ts Node.js + import Portkey from 'portkey-ai'; + + const client = new Portkey({ + apiKey: 'PORTKEY_API_KEY', + virtualKey: 'PROVIDER_VIRTUAL_KEY' + }); + + async function main() { + const image = await client.images.generate({ + model: "dall-e-3", + prompt: "A cute baby sea otter" + }); + + console.log(image.data); + } + main(); + ``` + ```py Python + from portkey_ai import Portkey + + client = Portkey( + api_key = "PORTKEY_API_KEY", + virtual_key = "PROVIDER_VIRTUAL_KEY" + ) + + client.images.generate( + model="dall-e-3", + prompt="A cute baby sea otter", + n=1, + size="1024x1024" + ) + ``` + ```sh cURL + curl https://api.portkey.ai/v1/images/generations \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-virtual-key: $PORTKEY_PROVIDER_VIRTUAL_KEY" \ + -d '{ + "model": "dall-e-3", + "prompt": "A cute baby sea otter", + "n": 1, + "size": "1024x1024" + }' + ``` + + +**Tracing Image Generation Requests** + +Portkey logs the generated image along with your whole request: + + + + + + [Image Generations Limitations](#image-generations-limitations) | [Image Generations FAQs](#image-generations-faqs) + + +--- + +### OpenAI Transcription & Translation (Whisper) + +OpenAI's Audio API converts speech to text using the Whisper model. It offers transcription in the original language and translation to English, supporting multiple file formats and languages with high accuracy. + + +```python Python +audio_file= open("/path/to/file.mp3", "rb") + +# Transcription +transcription = portkey.audio.transcriptions.create( + model="whisper-1", + file=audio_file +) +print(transcription.text) + +# Translation +translation = portkey.audio.translations.create( + model="whisper-1", + file=audio_file +) +print(translation.text) +``` + +```javascript Node.js +import fs from "fs"; + +// Transcription +async function transcribe() { + const transcription = await portkey.audio.transcriptions.create({ + file: fs.createReadStream("/path/to/file.mp3"), + model: "whisper-1", + }); + console.log(transcription.text); +} +transcribe(); + +// Translation +async function translate() { + const translation = await portkey.audio.translations.create({ + file: fs.createReadStream("/path/to/file.mp3"), + model: "whisper-1", + }); + console.log(translation.text); +} +translate(); +``` + +```curl REST +# Transcription +curl -X POST "https://api.portkey.ai/v1/audio/transcriptions" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -H "Content-Type: multipart/form-data" \ + -F "file=@/path/to/file.mp3" \ + -F "model=whisper-1" + +# Translation +curl -X POST "https://api.portkey.ai/v1/audio/translations" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -H "Content-Type: multipart/form-data" \ + -F "file=@/path/to/file.mp3" \ + -F "model=whisper-1" +``` + + + + [Speech-to-Text Limitations](#speech-to-text-limitations) | [Speech-to-text FAQs](#speech-to-text-faqs) + + +--- + +### OpenAI Text to Speech + +OpenAI's Text to Speech (TTS) API converts written text into natural-sounding audio using six distinct voices. It supports multiple languages, streaming capabilities, and various audio formats for different use cases. + + +```python Python +from pathlib import Path + +speech_file_path = Path(__file__).parent / "speech.mp3" +response = portkey.audio.speech.create( + model="tts-1", + voice="alloy", + input="Today is a wonderful day to build something people love!" +) + +with open(speech_file_path, "wb") as f: + f.write(response.content) +``` + +```javascript Node.js +import path from 'path'; +import fs from 'fs'; + +const speechFile = path.resolve("./speech.mp3"); + +async function main() { + const mp3 = await portkey.audio.speech.createCertainly! I'll continue with the Text to Speech section and then move on to the additional features and sections: + +```javascript Node.js +({ + model: "tts-1", + voice: "alloy", + input: "Today is a wonderful day to build something people love!", + }); + const buffer = Buffer.from(await mp3.arrayBuffer()); + await fs.promises.writeFile(speechFile, buffer); +} + +main(); +``` + +```curl REST +curl -X POST "https://api.portkey.ai/v1/audio/speech" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -H "Content-Type: application/json" \ + -d '{ + "model": "tts-1", + "voice": "alloy", + "input": "Today is a wonderful day to build something people love!" + }' \ + --output speech.mp3 +``` + + + + [Text-to-Speech Limitations](#text-to-speech-limitations) | [Text-to-Speech FAQs](#text-to-speech-faqs) + + +--- + +### OpenAI Realtime API + +OpenAI's Realtime API enables dynamic, low-latency conversations combining text, voice, and function calling capabilities. Built on GPT-4o models optimized for realtime interactions, it supports both WebRTC for client-side applications and WebSockets for server-side implementations. + +Portkey enhances OpenAI's Realtime API with production-ready features: +- Complete request/response logging for realtime streams +- Cost tracking and budget management for streaming sessions +- Multi-modal conversation monitoring +- Session-based analytics and debugging + +The API bridges the gap between traditional request-response patterns and interactive, real-time AI experiences, with Portkey adding the reliability and observability needed for production deployments. Developers can access this functionality through two model variants: +- `gpt-4o-realtime` for full capabilities +- `gpt-4o-mini-realtime` for lighter applications + + + +--- + +### More Capabilities + + + + + + + + + + + + + + + + + + +--- + +## Portkey Features + + + + + +Portkey allows you to track user IDs passed with the user parameter in OpenAI requests, enabling you to monitor user-level costs, requests, and more: + + +```python Python +response = portkey.chat.completions.create( + model="gpt-4o", + messages=[{"role": "user", "content": "Say this is a test"}], + user="user_123456" +) +``` + +```javascript Node.js +const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: "user", content: "Say this is a test" }], + model: "gpt-4o", + user: "user_12345", +}); +``` + +```curl REST +curl -X POST "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -d '{ + "model": "gpt-4o", + "messages": [{"role": "user", "content": "Say this is a test"}], + "user": "user_123456" + }' +``` + + +When you include the user parameter in your requests, Portkey logs will display the associated user ID, as shown in the image below: + +Portkey Logs with User ID + +In addition to the `user` parameter, Portkey allows you to send arbitrary custom metadata with your requests. This powerful feature enables you to associate additional context or information with each request, which can be useful for analysis, debugging, or other custom use cases. + + + + Explore how to use custom metadata to enhance your request tracking and analysis. + + + + + + + +Here's a simplified version of how to use Portkey's Gateway Configuration: + + + + You can create a Gateway configuration using the Portkey Config Dashboard or by writing a JSON configuration in your code. In this example, requests are routed based on the user's subscription plan (paid or free). + + ```json + config = { + "strategy": { + "mode": "conditional", + "conditions": [ + { + "query": { "metadata.user_plan": { "$eq": "paid" } }, + "then": "gpt4o" + }, + { + "query": { "metadata.user_plan": { "$eq": "free" } }, + "then": "gpt-3.5" + } + ], + "default": "base-gpt4" + }, + "targets": [ + { + "name": "gpt4o", + "virtual_key": "xx" + }, + { + "name": "gpt-3.5", + "virtual_key": "yy" + } + ] + } + ``` + + + + When a user makes a request, it will pass through Portkey's AI Gateway. Based on the configuration, the Gateway routes the request according to the user's metadata. + Conditional Routing Diagram + + + + Pass the Gateway configuration to your Portkey client. You can either use the config object or the Config ID from Portkey's hosted version. + + + ```python Python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", + virtual_key="VIRTUAL_KEY", + config=portkey_config + ) + ``` + + ```javascript Node.js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "VIRTUAL_KEY", + config: portkeyConfig + }) + ``` + + + + +That's it! Portkey seamlessly allows you to make your AI app more robust using built-in gateway features. Learn more about advanced gateway features: + + + + Distribute requests across multiple targets based on defined weights. + + + Automatically switch to backup targets if the primary target fails. + + + Route requests to different targets based on specified conditions. + + + Enable caching of responses to improve performance and reduce costs. + + + + + + + +Portkey's AI gateway enables you to enforce input/output checks on requests by applying custom hooks before and after processing. Protect your user's/company's data by using PII guardrails and many more available on Portkey Guardrails: + +```json +{ + "virtual_key":"openai-xxx", + "before_request_hooks": [{ + "id": "input-guardrail-id-xx" + }], + "after_request_hooks": [{ + "id": "output-guardrail-id-xx" + }] +} +``` + + + Explore Portkey's guardrail features to enhance the security and reliability of your AI applications. + + + + + + + + + + + + + + + + + + + + + + +## Popular Libraries + +You can make your OpenAI integrations with popular libraries also production-ready and reliable with native integrations. + +### OpenAI with Langchain + + + ``` + ``` + + +### OpenAI with LangGraph + + + ``` + ``` + + +### OpenAI with LibreChat + +### OpenAI with CrewAI + +### OpenAI with Llamaindex + +### OpenAI with Vercel + +--- + +### More Libraries + + + + + + +--- + +## Cookbooks + + + + + + +--- + +## Appendix + +### OpenAI Projects & Organizations + +Organization management is particularly useful if you belong to multiple organizations or are accessing projects through a legacy OpenAI user API key. Specifying the organization and project IDs also helps you maintain better control over your access rules, usage, and costs. + +In Portkey, you can add your OpenAI Org & Project details by **Using Virtual Keys**, **Using Configs**, or **While Making a Request**. + + + + + +When selecting OpenAI from the Virtual Key dropdown menu while creating a virtual key, Portkey displays optional fields for the organization ID and project ID alongside the API key field. + + + + +Portkey takes budget management a step further than OpenAI. While OpenAI allows setting budget limits per project, Portkey enables you to set budget limits for each virtual key you create. For more information on budget limits, [refer to this documentation](/product/ai-gateway/virtual-keys/budget-limits) + + + + + + +You can also specify the organization and project details in your request config, either at the root level or within a specific target. + +```json {3,4} +{ + "provider": "openai", + "api_key": "OPENAI_API_KEY", + "openai_organization": "org-xxxxxx", + "openai_project": "proj_xxxxxxxx" +} +``` + + + + + +Pass OpenAI organization and project details directly when making a request: + + +```python OpenAI Python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +client = OpenAI( + api_key="OPENAI_API_KEY", + organization="org-xxxxxxxxxx", + project="proj_xxxxxxxxx", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY" + ) +) + +chat_complete = client.chat.completions.create( + model="gpt-4o", + messages=[{"role": "user", "content": "Say this is a test"}], +) + +print(chat_complete.choices[0].message.content) +``` + +```js OpenAI NodeJS +import OpenAI from "openai"; +import { PORTKEY_GATEWAY_URL, createHeaders } from "portkey-ai"; + +const openai = new OpenAI({ + apiKey: "OPENAI_API_KEY", + organization: "org-xxxxxx", + project: "proj_xxxxxxx", + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY", + }), +}); + +async function main() { + const chatCompletion = await openai.chat.completions.create({ + messages: [{ role: "user", content: "Say this is a test" }], + model: "gpt-4o", + }); + + console.log(chatCompletion.choices); +} + +main(); +``` +```sh cURL +curl https://api.portkey.ai/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "x-portkey-openai-organization: org-xxxxxxx" \ + -H "x-portkey-openai-project: proj_xxxxxxx" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-provider: openai" \ + -d '{ + "model": "gpt-4o", + "messages": [{"role": "user","content": "Hello!"}] + }' +``` +```python Portkey Python +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY", + provider="openai", + Authorization="Bearer OPENAI_API_KEY", + openai_organization="org-xxxxxxxxx", + openai_project="proj_xxxxxxxxx", +) + +chat_complete = portkey.chat.completions.create( + model="gpt-4o", + messages=[{"role": "user", "content": "Say this is a test"}], +) + +print(chat_complete.choices[0].message.content) +``` +```js Portkey NodeJS +import Portkey from "portkey-ai"; + +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + provider: "openai", + Authorization: "Bearer OPENAI_API_KEY", + openaiOrganization: "org-xxxxxxxxxxx", + openaiProject: "proj_xxxxxxxxxxxxx", +}); + +async function main() { + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: "user", content: "Say this is a test" }], + model: "gpt-4o", + }); + + console.log(chatCompletion.choices); +} + +main(); +``` + + + + + + +### Supported Parameters + + + +| Method / Endpoint | Supported Parameters | +| ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `completions` | model, prompt, max_tokens, temperature, top_p, n, stream, logprobs, echo, stop, presence_penalty, frequency_penalty, best_of, logit_bias, user, seed, suffix | +| `embeddings` | model, input, encoding_format, dimensions, user | +| `chat.completions` | model, messages, functions, function_call, max_tokens, temperature, top_p, n, stream, stop, presence_penalty, frequency_penalty, logit_bias, user, seed, tools, tool_choice, response_format, logprobs, top_logprobs, stream_options, service_tier, parallel_tool_calls, max_completion_tokens | +| `image.generations` | prompt, model, n, quality, response_format, size, style, user | +| `create.speech` | model, input, voice, response_format, speed | +| `create.transcription` | All parameters supported | +| `create.translation` | All parameters supported | + + + +### Supported Models + + + + +### Limitations + + +Portkey does not support the following OpenAI features: +- Streaming for audio endpoints + + +#### Limitations for Vision Requests +- Medical images: Vision models are not suitable for interpreting specialized medical images like CT scans and shouldn't be used for medical advice. +- Non-English: The models may not perform optimally when handling images with text of non-Latin alphabets, such as Japanese or Korean. +- Small text: Enlarge text within the image to improve readability, but avoid cropping important details. +- Rotation: The models may misinterpret rotated / upside-down text or images. +- Visual elements: The models may struggle to understand graphs or text where colors or styles like solid, dashed, or dotted lines vary. +- Spatial reasoning: The models struggle with tasks requiring precise spatial localization, such as identifying chess positions. +- Accuracy: The models may generate incorrect descriptions or captions in certain scenarios. +- Image shape: The models struggle with panoramic and fisheye images. +- Metadata and resizing: The models do not process original file names or metadata, and images are resized before analysis, affecting their original dimensions. +- Counting: May give approximate counts for objects in images. +- CAPTCHAS: For safety reasons, CAPTCHA submissions are blocked by OpenAI. + +#### Image Generations Limitations +- **DALLยทE 3 Restrictions:** + - Only supports image generation (no editing or variations) + - Limited to one image per request + - Fixed size options: 1024x1024, 1024x1792, or 1792x1024 pixels + - Automatic prompt enhancement cannot be disabled +- **Image Requirements:** + - Must be PNG format + - Maximum file size: 4MB + - Must be square dimensions + - For edits/variations: input images must meet same requirements +- **Content Restrictions:** + - All prompts and images are filtered based on OpenAI's content policy + - Violating content will return an error + - Edited areas must be described in full context, not just the edited portion +- **Technical Limitations:** + - Image URLs expire after 1 hour + - Image editing (inpainting) and variations only available in DALLยทE 2 + - Response format limited to URL or Base64 data + +#### Speech-to-text Limitations +- **File Restrictions:** + - Maximum file size: 25 MB + - Supported formats: mp3, mp4, mpeg, mpga, m4a, wav, webm + - No streaming support +- **Language Limitations:** + - Translation output available only in English + - Variable accuracy for non-listed languages + - Limited control over generated audio compared to other language models +- **Technical Constraints:** + - Prompt limited to first 244 tokens + - Restricted processing for longer audio files + - No real-time transcription support + +#### Text-to-Speech Limitations +- **Voice Restrictions:** + - Limited to 6 pre-built voices (alloy, echo, fable, onyx, nova, shimmer) + - Voices optimized primarily for English + - No custom voice creation support + - No direct control over emotional range or tone +- **Audio Quality Trade-offs:** + - tts-1: Lower latency but potentially more static + - tts-1-hd: Higher quality but increased latency + - Quality differences may vary by listening device +- **Usage Requirements:** + - Must disclose AI-generated nature to end users + - Cannot create custom voice clones + - Performance varies for non-English languages + + +### FAQs + +#### General + + +You can sign up to OpenAI [here](https://platform.openai.com/docs/overview) and grab your scoped API key [here](https://platform.openai.com/api-keys). + + +The OpenAI API can be used by signing up to the OpenAI platform. You can find the pricing info [here](https://openai.com/api/pricing/) + + +You can find your current rate limits imposed by OpenAI [here](https://platform.openai.com/settings/organization/limits). For more tips, check out [this guide](/guides/getting-started/tackling-rate-limiting#tackling-rate-limiting). + + + +#### Vision FAQs + + +Vision fine-tuning is available for [some OpenAI models](https://platform.openai.com/docs/guides/fine-tuning#vision). + + +No, you can use dall-e-3 to generate images and gpt-4o and other chat models to understand images. + + +OpenAI currently supports PNG (.png), JPEG (.jpeg and .jpg), WEBP (.webp), and non-animated GIF (.gif). + + +OpenAI currently restricts image uploads to 20MB per image. + + +OpenAI processes images at the token level, so each image that's processed counts towards your tokens per minute (TPM) limit. See how OpenAI [calculates costs here](https://platform.openai.com/docs/guides/vision#calculating-costs) for details on the formula used to determine token count per image. + + +No, the models do not receive image metadata. + + + +#### Embedding FAQs + + +[This cookbook by OpenAI](https://cookbook.openai.com/examples/how_to_count_tokens_with_tiktoken) illustrates how to leverage their Tiktoken library to count tokens for various embedding requests. + + +Using a specialized vector database helps here. [Check out this cookbook by OpenAI](https://cookbook.openai.com/examples/vector_databases/readme) for a deep dive. + + +The cutoff date for V3 embedding models (`text-embedding-3-large` & `text-embedding-3-small`) is **September 2021** - so they do not know about the most recent events. + + + +#### Prompt Caching FAQs + + +OpenAI Prompt caches are not shared between organizations. Only members of the same organization can access caches of identical prompts. + + + Prompt Caching does not influence the generation of output tokens or the final response provided by the API. Regardless of whether caching is used, the output generated will be identical. This is because only the prompt itself is cached, while the actual response is computed anew each time based on the cached prompt. + + +Manual cache clearing is not currently available. Prompts that have not been encountered recently are automatically cleared from the cache. Typical cache evictions occur after 5-10 minutes of inactivity, though sometimes lasting up to a maximum of one hour during off-peak periods. + + +No. Caching happens automatically, with no explicit action needed or extra cost paid to use the caching feature. + + +Yes, as caching does not affect rate limits. + + +Discounting for Prompt Caching is not available on the Batch API but is available on Scale Tier. With Scale Tier, any tokens that are spilled over to the shared API will also be eligible for caching. + + +Yes, Prompt Caching is compliant with existing Zero Data Retention policies. + + + +#### Image Generations FAQs + + + +DALLยทE 3 offers higher quality images and enhanced capabilities, but only supports image generation. DALLยทE 2 supports all three capabilities: generation, editing, and variations. + + +Generated image URLs expire after one hour. Download or process the images before expiration. + + +Images must be square PNG files under 4MB. For editing features, both the image and mask must have identical dimensions. + + +While you can't completely disable it, you can add "I NEED to test how the tool works with extremely simple prompts. DO NOT add any detail, just use it AS-IS:" to your prompt. + + +DALLยทE 3 supports 1 image per request (use parallel requests for more), while DALLยทE 2 supports up to 10 images per request. + + +The API requires PNG format for all image uploads and manipulations. Generated images can be returned as either a URL or Base64 data. + + +Available only in DALLยทE 2, inpainting requires both an original image and a mask. The transparent areas of the mask indicate where the image should be edited, and your prompt should describe the complete new image, not just the edited area. + + + +#### Speech-to-text FAQs + + + +The API supports mp3, mp4, mpeg, mpga, m4a, wav, and webm formats, with a maximum file size of 25 MB. + + + +No, currently the translation API only supports output in English, regardless of the input language. + + + +You'll need to either compress the audio file or split it into smaller chunks. Tools like PyDub can help split audio files while avoiding mid-sentence breaks. + + + +While the model was trained on 98 languages, only languages with less than 50% word error rate are officially supported. Other languages may work but with lower accuracy. + + + +Yes, using the `timestamp_granularities` parameter, you can get timestamps at the segment level, word level, or both. + + + +You can use the prompt parameter to provide context or correct spellings of specific terms, or use post-processing with GPT-4 for more extensive corrections. + + + +Transcription provides output in the original language, while translation always converts the audio to English text. + + + +#### Text-to-Speech FAQs + + +TTS-1 offers lower latency for real-time applications but may include more static. TTS-1-HD provides higher quality audio but with increased generation time. + + + +The API supports multiple formats: MP3 (default), Opus (for streaming), AAC (for mobile), FLAC (lossless), WAV (uncompressed), and PCM (raw 24kHz samples). + + + +No, the API only supports the six built-in voices (alloy, echo, fable, onyx, nova, and shimmer). Custom voice creation is not available. + + + +While the voices are optimized for English, the API supports multiple languages with varying effectiveness. Performance quality may vary by language. + + + +There's no direct mechanism to control emotional output. While capitalization and grammar might influence the output, results are inconsistent. + + + +Yes, the API supports real-time audio streaming using chunk transfer encoding, allowing audio playback before complete file generation. + + + +Yes, OpenAI's usage policies require clear disclosure to end users that they are hearing AI-generated voices, not human ones. + + diff --git a/virtual_key_old/integrations/llms/openrouter.mdx b/virtual_key_old/integrations/llms/openrouter.mdx new file mode 100644 index 00000000..77631dc8 --- /dev/null +++ b/virtual_key_old/integrations/llms/openrouter.mdx @@ -0,0 +1,204 @@ +--- +title: "OpenRouter" +--- + +Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including [OpenRouter](https://openrouter.ai). + +With Portkey, you can take advantage of features like fast AI gateway access, observability, prompt management, and more, all while ensuring the secure management of your LLM API keys through a [virtual key](/product/ai-gateway/virtual-keys) system. + +Provider Slug. `openrouter` + +## Portkey SDK Integration with OpenRouter Models + +Portkey provides a consistent API to interact with models from various providers. To integrate OpenRouter with Portkey: + +### 1\. Install the Portkey SDK + +Add the Portkey SDK to your application to interact with OpenRouter AI's API through Portkey's gateway. + + + ```sh + npm install --save portkey-ai + ``` + + + ```sh + pip install portkey-ai + ``` + + + + + + + +### 2\. Initialize Portkey with the Virtual Key + +To use OpenRouter with Portkey, [get your API key from here](https://openrouter.ai/settings/keys), then add it to Portkey to create the virtual key. + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your OpenRouter Virtual Key + }) + ``` + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Open Router + ) + ``` + + + + + + +### **3\. Invoke Chat Completions with** OpenRouter + +Use the Portkey instance to send requests to OpenRouter. You can also override the virtual key directly in the API call if needed. + + + +```js +const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'openai/gpt-4o-2024-08-06', +}); + +console.log(chatCompletion.choices); +``` + + + ```python + completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model= 'mistral-medium' + ) + + print(completion) + ``` + + + + + + +### Open Router Tool Calling +Tool calling feature lets models trigger external tools based on conversation context. You define available functions, the model chooses when to use them, and your application executes them and returns results. + +Portkey supports Open Router Tool Calling and makes it interoperable across multiple providers. With Portkey Prompts, you can templatize various your prompts & tool schemas as well. + + + + + + + +```javascript Get Weather Tool +let tools = [{ + type: "function", + function: { + name: "getWeather", + description: "Get the current weather", + parameters: { + type: "object", + properties: { + location: { type: "string", description: "City and state" }, + unit: { type: "string", enum: ["celsius", "fahrenheit"] } + }, + required: ["location"] + } + } +}]; + +let response = await portkey.chat.completions.create({ + model: "openai/gpt-4o", + messages: [ + { role: "system", content: "You are a helpful assistant." }, + { role: "user", content: "What's the weather like in Delhi - respond in JSON" } + ], + tools, + tool_choice: "auto", +}); + +console.log(response.choices[0].finish_reason); +``` + + +```python Get Weather Tool +tools = [{ + "type": "function", + "function": { + "name": "getWeather", + "description": "Get the current weather", + "parameters": { + "type": "object", + "properties": { + "location": {"type": "string", "description": "City and state"}, + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]} + }, + "required": ["location"] + } + } +}] + +response = portkey.chat.completions.create( + model="openai/gpt-4o", + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "What's the weather like in Delhi - respond in JSON"} + ], + tools=tools, + tool_choice="auto" +) + +print(response.choices[0].finish_reason) +``` + + +```curl Get Weather Tool +curl -X POST "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -d '{ + "model": "openai/gpt-4o", + "messages": [ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "What'\''s the weather like in Delhi - respond in JSON"} + ], + "tools": [{ + "type": "function", + "function": { + "name": "getWeather", + "description": "Get the current weather", + "parameters": { + "type": "object", + "properties": { + "location": {"type": "string", "description": "City and state"}, + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]} + }, + "required": ["location"] + } + } + }], + "tool_choice": "auto" + }' +``` + + + + + + +The complete list of features supported in the SDK are available on the link below. + + + diff --git a/virtual_key_old/integrations/llms/perplexity-ai.mdx b/virtual_key_old/integrations/llms/perplexity-ai.mdx new file mode 100644 index 00000000..3085c0c3 --- /dev/null +++ b/virtual_key_old/integrations/llms/perplexity-ai.mdx @@ -0,0 +1,272 @@ +--- +title: "Perplexity AI" +--- + +Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including [Perplexity AI APIs](https://docs.perplexity.ai/reference/post%5Fchat%5Fcompletions). + +With Portkey, you can take advantage of features like fast AI gateway access, observability, prompt management, and more, all while ensuring the secure management of your LLM API keys through a [virtual key](/product/ai-gateway/virtual-keys) system. + +Provider Slug. `perplexity-ai` + +## Portkey SDK Integration with Perplexity AI Models + +Portkey provides a consistent API to interact with models from various providers. To integrate Perplexity AI with Portkey: + +### 1\. Install the Portkey SDK + +Add the Portkey SDK to your application to interact with Perplexity AI's API through Portkey's gateway. + + + ```sh + npm install --save portkey-ai + ``` + + + ```sh + pip install portkey-ai + ``` + + + + + + +### 2\. Initialize Portkey with the Virtual Key + +To use Perplexity AI with Portkey, [get your API key from here,](https://www.perplexity.ai/settings/api) then add it to Portkey to create the virtual key. + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your Perplexity AI Virtual Key + }) + ``` + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Perplexity AI + ) + ``` + + + + + +### **3\. Invoke Chat Completions with** Perplexity AI + +Use the Portkey instance to send requests to Perplexity AI. You can also override the virtual key directly in the API call if needed. + + + ```js + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'pplx-70b-chat', + }); + + console.log(chatCompletion.choices); + ``` + + + ```python + completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model= 'pplx-70b-chat' + ) + + print(completion) + ``` + + + + +## Fetching citations + +If you need to obtain citations in the response, you can disable [strict open ai compliance](/product/ai-gateway/strict-open-ai-compliance) + +## Perplexity-Specific Features + +Perplexity AI offers several unique features that can be accessed through additional parameters in your requests: + +### Search Domain Filter (Beta) +You can limit citations to specific domains using the `search_domain_filter` parameter. This feature is currently in closed beta and limited to 3 domains for whitelisting or blacklisting. + + + + ```python + completion = portkey.chat.completions.create( + messages=[{"role": "user", "content": "Tell me about electric cars"}], + model="pplx-70b-chat", + search_domain_filter=["tesla.com", "ford.com", "-competitors.com"] # Use '-' prefix for blacklisting + ) + ``` + + + ```js + const completion = await portkey.chat.completions.create({ + messages: [{ role: "user", content: "Tell me about electric cars" }], + model: "pplx-70b-chat", + search_domain_filter: ["tesla.com", "ford.com", "-competitors.com"] // Use '-' prefix for blacklisting + }); + ``` + + + +### Image Results (Beta) +Enable image results in responses from online models using the `return_images` parameter: + + + + ```python + completion = portkey.chat.completions.create( + messages=[{"role": "user", "content": "Show me pictures of electric cars"}], + model="pplx-70b-chat", + return_images=True # Feature in closed beta + ) + ``` + + + ```js + const completion = await portkey.chat.completions.create({ + messages: [{ role: "user", content: "Show me pictures of electric cars" }], + model: "pplx-70b-chat", + return_images: true // Feature in closed beta + }); + ``` + + + +### Related Questions (Beta) +Get related questions in the response using the `return_related_questions` parameter: + + + + ```python + completion = portkey.chat.completions.create( + messages=[{"role": "user", "content": "Tell me about electric cars"}], + model="pplx-70b-chat", + return_related_questions=True # Feature in closed beta + ) + ``` + + + ```js + const completion = await portkey.chat.completions.create({ + messages: [{ role: "user", content: "Tell me about electric cars" }], + model: "pplx-70b-chat", + return_related_questions: true // Feature in closed beta + }); + ``` + + + +### Search Recency Filter +Filter search results based on time intervals using the `search_recency_filter` parameter: + + + + ```python + completion = portkey.chat.completions.create( + messages=[{"role": "user", "content": "What are the latest developments in electric cars?"}], + model="pplx-70b-chat", + search_recency_filter="week" # Options: month, week, day, hour + ) + ``` + + + ```js + const completion = await portkey.chat.completions.create({ + messages: [{ role: "user", content: "What are the latest developments in electric cars?" }], + model: "pplx-70b-chat", + search_recency_filter: "week" // Options: month, week, day, hour + }); + ``` + + + + +### Web Search Options +Determines how much search context is retrieved for the model. +Options are: +- `low`: minimizes context for cost savings but less comprehensive answers. +- `medium`: balanced approach suitable for most queries. +- `high`: maximizes context for comprehensive answers but at higher cost. + + + + ```python + completion = portkey.chat.completions.create( + messages=[{"role": "user", "content": "What are the latest developments in electric cars?"}], + model="sonar", + web_search_options={ + "search_context_size": "high" + } + + ) + ``` + + + ```js + const completion = await portkey.chat.completions.create({ + messages: [{ role: "user", content: "What are the latest developments in electric cars?" }], + model: "sonar", + web_search_options: { + "search_context_size": "high" + } + }); + ``` + + + + + +### Search Recency Filter +Filters search results based on time (e.g., 'week', 'day'). + + + + ```python + completion = portkey.chat.completions.create( + messages=[{"role": "user", "content": "What are the latest developments in electric cars?"}], + model="sonar", + "search_recency_filter": "", + ) + ``` + + + ```js + const completion = await portkey.chat.completions.create({ + messages: [{ role: "user", content: "What are the latest developments in electric cars?" }], + model: "sonar", + search_recency_filter: "", + }); + ``` + + + + + +## Managing Perplexity AI Prompts + +You can manage all prompts to Perplexity AI in the [Prompt Library](/product/prompt-library). All the current models of Perplexity AI are supported and you can easily start testing different prompts. + +Once you're ready with your prompt, you can use the `portkey.prompts.completions.create` interface to use the prompt in your application. + +## Next Steps + +The complete list of features supported in the SDK are available on the link below. + + + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your Perplexity AI](/product/ai-gateway/configs)[ requests](/product/ai-gateway/configs) +3. [Tracing Perplexity AI requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to Perplexity AI APIs](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/predibase.mdx b/virtual_key_old/integrations/llms/predibase.mdx new file mode 100644 index 00000000..1ca586d1 --- /dev/null +++ b/virtual_key_old/integrations/llms/predibase.mdx @@ -0,0 +1,294 @@ +--- +title: "Predibase" +--- + +Portkey provides a robust and secure gateway to seamlessly integrate **open-source** and **fine-tuned** LLMs from Predibase into your applications. With Portkey, you can leverage powerful features like fast AI gateway, caching, observability, prompt management, and more, while securely managing your LLM API keys through a virtual key system. + +Provider Slug. `predibase` + +## Portkey SDK Integration with Predibase + +Using Portkey, you can call your Predibase models in the familar **OpenAI-spec** and try out your existing pipelines on Predibase fine-tuned models with 2 LOC change. + +### 1\. Install the Portkey SDK + +Install the Portkey SDK in your project using npm or pip: + + + ```sh + npm install --save portkey-ai + ``` + + +```sh +pip install portkey-ai +``` + + + + + + + +### 2\. Initialize Portkey with the Virtual Key + +To use Predibase with Portkey, [get your API key from here](https://app.predibase.com/settings), then add it to Portkey to create the virtual key. + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your Predibase Virtual Key + }) + ``` + + +```python +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Predibase +) +``` + + + +```js +import OpenAI from "openai"; +import { PORTKEY_GATEWAY_URL, createHeaders } from "portkey-ai"; + +const portkey = new OpenAI({ + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "PREDIBASE_VIRTUAL_KEY", + }), +}); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +portkey = OpenAI( + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key="PORTKEY_API_KEY", + virtual_key="PREDIBASE_VIRTUAL_KEY" + ) +) +``` + + + + + + +### 3\. Invoke Chat Completions on Predibase Serverless Endpoints + +Predibase offers LLMs like **Llama 3**, **Mistral**, **Gemma**, etc. on its [serverless infra](https://docs.predibase.com/user-guide/inference/models#serverless-endpoints) that you can query instantly. + +#### Sending Predibase Tenand ID + +Predibase expects your **account tenant ID** along with the API key in each request. With Portkey, you can send [**your Tenand ID**](https://app.predibase.com/settings) with the `user` param while making your request. + + + + + ```js + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'llama-3-8b ', + user: 'PREDIBASE_TENANT_ID' + }); + + console.log(chatCompletion.choices); + ``` + + +```python +completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model= 'llama-3-8b', + user= "PREDIBASE_TENANT_ID" +) + +print(completion) +``` + + + ```sh + curl https://api.portkey.ai/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-virtual-key: $PREDIBASE_VIRTUAL_KEY" \ + -d '{ + "messages": [{"role": "user","content": "Hello!"}], + "model": "llama-3-8b", + "user": "PREDIBASE_TENANT_ID" + }' + ``` + + + + + + +### 4\. Invoke Predibase Fine-Tuned Models + +With Portkey, you can send your fine-tune model & adapter details directly with the `model` param while making a request. + +The format is: + +`model = :` + + +For example, if your base model is `llama-3-8b` and the adapter repo name is `sentiment-analysis`, you can make a request like this: + + + + ```js + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'llama-3-8b:sentiment-analysis/1', + user: 'PREDIBASE_TENANT_ID' + }); + + console.log(chatCompletion.choices); + ``` + + +```python +completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model= 'llama-3-8b:sentiment-analysis/1', + user= "PREDIBASE_TENANT_ID" +) + +print(completion) +``` + + + ```sh + curl https://api.portkey.ai/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-virtual-key: $PREDIBASE_VIRTUAL_KEY" \ + -d '{ + "messages": [{"role": "user","content": "Hello!"}], + "model": "llama-3-8b:sentiment-analysis/1", + "user": "PREDIBASE_TENANT_ID" + }' + ``` + + + + + + +--- + +### Routing to Dedicated Deployments + +Using Portkey, you can easily route to your dedicatedly deployed models as well. Just pass the dedicated deployment name in the `model` param: + + +`model = "my-dedicated-mistral-deployment-name"` + +### JSON Schema Mode + +You can enforce JSON schema for all Predibase models - just set the `response_format` to `json_object` and pass the relevant schema while making your request. Portkey logs will show your JSON output separately + + + + ```js + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'llama-3-8b ', + user: 'PREDIBASE_TENANT_ID', + response_format: { + "type": "json_object", + "schema": {"properties": { + "name": {"maxLength": 10, "title": "Name", "type": "string"}, + "age": {"title": "Age", "type": "integer"}, + "required": ["name", "age", "strength"], + "title": "Character", + "type": "object" + } + } + }); + + console.log(chatCompletion.choices); + ``` + + +```python +# Using Pydantic to define the schema +from pydantic import BaseModel, constr + +# Define JSON Schema +class Character(BaseModel): + name: constr(max_length=10) + age: int + strength: int + +completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model= 'llama-3-8b', + user= "PREDIBASE_TENANT_ID", + response_format={ + "type": "json_object", + "schema": Character.schema(), + }, +) + +print(completion) +``` + + + ```sh + curl https://api.portkey.ai/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-virtual-key: $PREDIBASE_VIRTUAL_KEY" \ + -d '{ + "messages": [{"role": "user","content": "Hello!"}], + "model": "llama-3-8b", + "user": "PREDIBASE_TENANT_ID", + "response_format": { + "type": "json_object", + "schema": {"properties": { + "name": {"maxLength": 10, "title": "Name", "type": "string"}, + "age": {"title": "Age", "type": "integer"}, + "required": ["name", "age", "strength"], + "title": "Character", + "type": "object" + } + } + }' + ``` + + + + + + +--- + +## Next Steps + +The complete list of features supported in the SDK are available on the link below. + + + + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your Predibase requests](/product/ai-gateway/configs) +3. [Tracing Predibase requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to Predibase](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/recraft-ai.mdx b/virtual_key_old/integrations/llms/recraft-ai.mdx new file mode 100644 index 00000000..cbc0ca84 --- /dev/null +++ b/virtual_key_old/integrations/llms/recraft-ai.mdx @@ -0,0 +1,147 @@ +--- +title: "Recraft AI" +--- + +Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including [Recraft AI APIs](https://www.recraft.ai/docs). + +With Portkey, you can take advantage of features like fast AI gateway access, observability, prompt management, and more, all while ensuring the secure management of your LLM API keys through a [virtual key](/product/ai-gateway/virtual-keys) system. + +Provider Slug. `recraft-ai` + +## Portkey SDK Integration with Recraft AI Models + +Portkey provides a consistent API to interact with models from various providers. Recraft AI currently has + the following models available for integration: +- `recraftv3` + - `recraftv2` + +## Image Generation on Recraft AI using Portkey + +Portkey supports the OpenAI signature to make text-to-image requests. + + +```js +import Portkey from 'portkey-ai'; + +// Initialize the Portkey client +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + provider: "recraft-ai", + Authorization: "RECRAFT_API_KEY" +}); + +async function main() { + const image = await portkey.images.generate({ + model: "recraftv3", + prompt: "Lucy in the sky with diamonds", + style: 'digital_illustration', + }); + + console.log(image.data); +} + +main(); +``` + + + +```py +from portkey_ai import Portkey +from IPython.display import display, Image + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + provider= "recraft-ai", + Authorization= "RECRAFT_API_KEY" +) + +response = client.images.generate( + model="recraftv3", + prompt='race car on a track', + style='digital_illustration', +) +print(response.data[0].url) + +``` + + + +```js +import OpenAI from 'openai'; // We're using the v4 SDK +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const client = new OpenAI({ + apiKey: 'RECRAFT_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "recraft-ai", + apiKey: "PORTKEY_API_KEY" // defaults to process.env["PORTKEY_API_KEY"] + }) +}); + +async function main() { + const image = await openai.images.generate({ + model: "recraftv3", + prompt: 'race car on a track', + style: 'digital_illustration', + }); + + console.log(image.data); +} + +main(); +``` + + + +```py +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders +from IPython.display import display, Image + +client = OpenAI( + api_key='RECRAFT_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="recraft-ai", + api_key="PORTKEY_API_KEY" + ) +) + +response = client.images.generate( + model="recraftv3", + prompt='race car on a track', + style='digital_illustration', +) +print(response.data[0].url) + +) + +# Display the image +display(Image(url=image.data[0].url)) +``` + + + +```sh +curl "https://api.portkey.ai/v1/images/generations" \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-provider: recraft-ai" \ + -H "Authorization: Bearer $RECRAFT_API_KEY" \ + -d '{ + "prompt": "Lucy in the sky with diamonds", + "style": "digital_illustration" + }' +``` + + + + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your Recraft AI](/product/ai-gateway/configs)[ requests](/product/ai-gateway/configs) +3. [Tracing Recraft AI requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to Recraft AI APIs](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/reka-ai.mdx b/virtual_key_old/integrations/llms/reka-ai.mdx new file mode 100644 index 00000000..322440e0 --- /dev/null +++ b/virtual_key_old/integrations/llms/reka-ai.mdx @@ -0,0 +1,119 @@ +--- +title: "Reka AI" +--- + +Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including [Reka AI](https://www.reka.ai/). + +With Portkey, you can take advantage of features like fast AI gateway access, observability, prompt management, and more, all while ensuring the secure management of your LLM API keys through a [virtual key](/product/ai-gateway/virtual-keys) system. + +Provider Slug. `reka` + +## Portkey SDK Integration with Reka Models + +Portkey provides a consistent API to interact with models from various providers. To integrate Reka with Portkey: + +### 1\. Install the Portkey SDK + +Add the Portkey SDK to your application to interact with Reka AI's API through Portkey's gateway. + + + ```sh + npm install --save portkey-ai + ``` + + + ```sh + pip install portkey-ai + ``` + + + + + + + +### 2\. Initialize Portkey with the Virtual Key + +To use Reka AI with Portkey, [get your API key from here,](https://platform.reka.ai/apikeys) then add it to Portkey to create the virtual key. + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your Reka AI Virtual Key + }) + ``` + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Groq + ) + ``` + + + + + + +### 3\. Invoke Chat Completions with Reka AI + +Use the Portkey instance to send requests to Reka AI. You can also override the virtual key directly in the API call if needed. + + + ```js + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'reka-core', + }); + + console.log(chatCompletion.choices);d + ``` + + + + +```python +completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model= 'reka-core' +) + +print(completion) +``` + + + + + + + +## Managing Reka Prompts + +You can manage all prompts to Reka in the [Prompt Library](/product/prompt-library). All the current models of Reka are supported and you can easily start testing different prompts. + +Once you're ready with your prompt, you can use the `portkey.prompts.completions.create` interface to use the prompt in your application. + +## Supported Models + +| Model Name | Model String to Use in API calls | +| ---------- | -------------------------------- | +| Core | reka-core, reka-core-20240415 | +| Edge | reka-edge, reka-edge-20240208 | +| Flash | reka-flash, reka-flash-20240226 | + +The complete list of features supported in the SDK are available on the link below. + + + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your Reka](/product/ai-gateway/configs)[ requests](/product/ai-gateway/configs) +3. [Tracing Reka requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to Reka APIs](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/replicate.mdx b/virtual_key_old/integrations/llms/replicate.mdx new file mode 100644 index 00000000..20300f7f --- /dev/null +++ b/virtual_key_old/integrations/llms/replicate.mdx @@ -0,0 +1,181 @@ +--- +title: "Replicate" +--- + +[Replicate](https://replicate.com/) is a platform for building and running machine learning models. + +Replicate does not have a standarized JSON body format for their inference API, hence it is not possible to use unified API to interact with Replicate. +Portkey instead provides a proxy to Replicate, allowing you to use virtual keys and observability features. + +## Portkey SDK Integration with Replicate + +To integrate Replicate with Portkey: + +### 1\. Install the Portkey SDK + +Add the Portkey SDK to your application to interact with Replicate through Portkey's gateway. + + + + ```sh + npm install --save portkey-ai + ``` + + + ```sh + pip install portkey-ai + ``` + + + +### 2\. Initialize Portkey with a Virtual Key + +To use Replicate with Portkey, get your Replicate API key from [here](https://replicate.com/account/api-tokens), then add it to Portkey to create your [Replicate virtual key](/product/ai-gateway/virtual-keys#using-virtual-keys). + + + + +```js +import Portkey from 'portkey-ai' + +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your Replicate Virtual Key +}) +``` + + + + +```python +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Replicate +) +``` + + + +```python +from openai import OpenAI + +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +client = OpenAI( + api_key="REPLICATE_API_KEY", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key="PORTKEY_API_KEY", + provider="replicate" + ) +) +``` + + + + +```js +import OpenAI from "openai"; + +import { PORTKEY_GATEWAY_URL, createHeaders } from "portkey-ai"; + +const client = new OpenAI({ + apiKey: "REPLICATE_API_KEY", + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "replicate", + apiKey: "PORTKEY_API_KEY", + }), +}); +``` + + + +### 3\. Use the Portkey SDK to interact with Replicate + + + + ```python +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="REPLICATE_VIRTUAL_KEY", +) + +response = portkey.post( + url="predictions", # Replace with the endpoint you want to call +) + +print(response) + ``` + + + ```javascript + import Portkey from 'portkey-ai'; + + // Initialize the Portkey client + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "REPLICATE_VIRTUAL_KEY", // Add your Replicate's virtual key + }); + + response = portkey.post( + url="predictions", # Replace with the endpoint you want to call + ) + + print(response) + ``` + + + ```javascript + import OpenAI from 'openai'; // We're using the v4 SDK + import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + + const openai = new OpenAI({ + apiKey: 'REPLICATE_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + virtualKey: "REPLICATE_VIRTUAL_KEY", + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + }) + }); + + response = openai.post( + url="predictions", # Replace with the endpoint you want to call + ) + + print(response) + ``` + + + ```python + from openai import OpenAI + from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + + openai = OpenAI( + api_key='REPLICATE_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="replicate", + api_key="PORTKEY_API_KEY" + ) + ) + + response = openai.post( + url="predictions", # Replace with the endpoint you want to call + ) + + print(response) + ``` + + + ```sh +curl --location --request POST 'https://api.portkey.ai/v1/predictions' \ +--header 'x-portkey-virtual-key: REPLICATE_VIRTUAL_KEY' \ +--header 'x-portkey-api-key: PORTKEY_API_KEY' + ``` + + diff --git a/virtual_key_old/integrations/llms/sambanova.mdx b/virtual_key_old/integrations/llms/sambanova.mdx new file mode 100644 index 00000000..2a3ed47c --- /dev/null +++ b/virtual_key_old/integrations/llms/sambanova.mdx @@ -0,0 +1,102 @@ +--- +title: "SambaNova" +description: "Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including [SambaNova AI](https://sambanova.ai/)." +--- + + +With Portkey, you can take advantage of features like fast AI gateway access, observability, prompt management, and more, all while ensuring the secure management of your LLM API keys through a [virtual key](/product/ai-gateway/virtual-keys) system. + + +Provider Slug: `sambanova` + + +## Portkey SDK Integration with SambaNova Models + +### **1. Install the Portkey SDK** + +Add the Portkey SDK to your application to interact with SambaNova's API through Portkey's gateway. + + + + ```sh + npm install --save portkey-ai + ``` + + + ```sh + pip install portkey-ai + ``` + + + +### **2. Initialize Portkey with the Virtual Key** + + + + ```javascript + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your SambaNova Virtual Key + }) + ``` + + + ```python + from portkey_ai import Portkey + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for SambaNova AI + ) + ``` + + + +### **3. Invoke Chat Completions** + +Use the Portkey instance to send requests to the SambaNova API. You can also override the virtual key directly in the API call if needed. + + + + ```javascript + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'Meta-Llama-3.1-405B-Instruct', + }); + console.log(chatCompletion.choices); + ``` + + + ```python + completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model= 'Meta-Llama-3.1-405B-Instruct' + ) + print(completion) + ``` + + + +## Managing SambaNova Prompts + +You can manage all prompts to SambaNova models in the [Prompt Library](/product/prompt-library). All the current models of SambaNova are supported and you can easily start testing different prompts. + +Once you're ready with your prompt, you can use the `portkey.prompts.completions.create` interface to use the prompt in your application. + +The complete list of supported models are available here: + + + View the list of supported SambaNova models + + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your SambaNova requests](/product/ai-gateway/configs) +3. [Tracing SambaNova requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to SambaNova APIs](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/segmind.mdx b/virtual_key_old/integrations/llms/segmind.mdx new file mode 100644 index 00000000..0f3570e2 --- /dev/null +++ b/virtual_key_old/integrations/llms/segmind.mdx @@ -0,0 +1,144 @@ +--- +title: "Segmind" +--- + +Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including [Segmind APIs](https://docs.segmind.com/). + +With Portkey, you can take advantage of features like fast AI gateway access, observability, prompt management, and more, all while ensuring the secure management of your LLM API keys through a [virtual key](/product/ai-gateway/virtual-keys) system. + +Provider Slug. `segmind` + +## Portkey SDK Integration with Segmind + +Portkey provides a consistent API to interact with image generation models from various providers. To integrate Segmind with Portkey: + +### 1\. Install the Portkey SDK + +Add the Portkey SDK to your application to interact with the Segmind API through Portkey's gateway. + + + ```sh + npm install --save portkey-ai + ``` + + +```sh +pip install portkey-ai +``` + + + + + + + +### 2\. Initialize Portkey with the Virtual Key + +To use Segmind with Portkey, [get your API key from here](https://cloud.segmind.com/console/api-keys), then add it to Portkey to create the virtual key. + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your Segmind Virtual Key + }) + ``` + + +```python +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Segmind +) +``` + + + + + + +### **3\. Invoke Image Generation with** Segmind + +Use the Portkey instance to send requests to Stability AI. You can also override the virtual key directly in the API call if needed. + + + ```js + const image = await portkey.images.generate({ + model:"sdxl1.0-txt2img", + prompt:"Lucy in the sky with diamonds", + size:"1024x1024" + }) + ``` + + +```py +image = portkey.images.generate( + model="sdxl1.0-txt2img", + prompt="Lucy in the sky with diamonds", + size="1024x1024" +) +``` + + + + + + +Notice how we're using the OpenAI's image generation signature to prompt Segmind's hosted serverless endpoints allowing greater flexibility to change models and providers later if necessary. + +### Supported Models + +The following models are supported, newer models added to Segmind should also be automatically supported. + +| Model String | Model Name | Extra Keys (if any) | +| ------------------------ | --------------------- | ---------------------------------------------------------------------- | +| sdxl1.0-txt2img | SDXL | | +| sd1.5-526mix | 526 Mix | | +| sd1.5-allinonepixel | All In One Pixel | | +| sd1.5-disneyB | Cartoon | | +| sd1.5-colorful | Colorful | | +| sd1.5-cuterichstyle | Cute Rich Style | | +| sd1.5-cyberrealistic | Cyber Realistic | | +| sd1.5-deepspacediffusion | Deep Spaced Diffusion | | +| sd1.5-dreamshaper | Dream Shaper | | +| sd1.5-dvarch | Dv Arch | | +| sd1.5-edgeofrealism | Edge of Realism | | +| sd1.5-epicrealism | Epic Realism | | +| sd1.5-fantassifiedicons | Fantassified Icons | | +| sd1.5-flat2d | Flat 2D | | +| sd1.5-fruitfusion | Fruit Fusion | | +| sd1.5-icbinp | Icbinp | | +| sd1.5-juggernaut | Juggernaut | | +| kandinsky2.2-txt2img | Kandinsky | | +| sd1.5-majicmix | Majicmix | | +| sd1.5-manmarumix | Manmarumix | | +| sd1.5-paragon | Paragon | | +| potraitsd1.5-txt2img | Potrait SD | | +| qrsd1.5-txt2img | QR Generator | control\_scale, control\_scale, control\_scale, qr\_text, invert, size | +| sd1.5-rcnz | RCNZ | | +| sd1.5-rpg | RPG | | +| sd1.5-realisticvision | Realistic Vision | | +| sd1.5-reliberate | Reliberate | | +| sd1.5-revanimated | Revanimated | | +| sd1.5-samaritan-3d | Samaritan | | +| sd1.5-scifi | SciFi | | +| smallsd1.5-txt2img | Small SD | | +| tinysd1.5-txt2img | Tiny SD | | + +## Next Steps + +The complete list of features supported in the SDK are available on the link below. + + + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your Segmind requests](/product/ai-gateway/configs) +3. [Tracing Segmind's requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to Segmind](/product/ai-gateway/fallbacks) +5. [Image generation API Reference](/provider-endpoints/images/create-image) diff --git a/virtual_key_old/integrations/llms/siliconflow.mdx b/virtual_key_old/integrations/llms/siliconflow.mdx new file mode 100644 index 00000000..cc12e228 --- /dev/null +++ b/virtual_key_old/integrations/llms/siliconflow.mdx @@ -0,0 +1,103 @@ +--- +title: "SiliconFlow" +Discroption: "Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including [SiliconFlow](https://siliconflow.cn/). +" +--- + +With Portkey, you can take advantage of features like fast AI gateway access, observability, prompt management, and more, all while ensuring the secure management of your LLM API keys through a [virtual key](/product/ai-gateway/virtual-keys) system. + + +Provider Slug: `siliconflow` + + +## Portkey SDK Integration with SiliconFlow Models + +Portkey provides a consistent API to interact with models from various providers. To integrate SiliconFlow with Portkey: + +### 1. Install the Portkey SDK + +Add the Portkey SDK to your application to interact with SiliconFlow's API through Portkey's gateway. + + + + ```sh + npm install --save portkey-ai + ``` + + + ```sh + pip install portkey-ai + ``` + + + +### 2. Initialize Portkey with the Virtual Key + +To use SiliconFlow with Portkey, [get your API key from here](https://siliconflow.cn/), then add it to Portkey to create the virtual key. + + + + ```javascript + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your Silicon Flow + }) + ``` + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for SiliconFlow + ) + ``` + + + +### 3. Invoke Chat Completions with SiliconFlow + +Use the Portkey instance to send requests to SiliconFlow. You can also override the virtual key directly in the API call if needed. + + + + ```javascript + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'deepseek-ai/DeepSeek-V2-Chat', + }); + console.log(chatCompletion.choices); + ``` + + + ```python + completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model= 'deepseek-ai/DeepSeek-V2-Chat' + ) + print(completion) + ``` + + + +## Managing SiliconFlow Prompts + +You can manage all prompts to SiliconFlow in the [Prompt Library](/product/prompt-library). All the current models of SiliconFlow are supported and you can easily start testing different prompts. + +Once you're ready with your prompt, you can use the `portkey.prompts.completions.create` interface to use the prompt in your application. + +The complete list of features supported in the SDK are available on the link below. + + + Explore the Portkey SDK Client documentation + + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your SiliconFlow requests](/product/ai-gateway/configs) +3. [Tracing SiliconFlow requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to SiliconFlow APIs](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/snowflake-cortex.mdx b/virtual_key_old/integrations/llms/snowflake-cortex.mdx new file mode 100644 index 00000000..c99813b5 --- /dev/null +++ b/virtual_key_old/integrations/llms/snowflake-cortex.mdx @@ -0,0 +1,118 @@ +--- +title: "Snowflake Cortex" +--- + +Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including [Snowlfake Cortex APIs](https://docs.snowflake.com/). + +With Portkey, you can take advantage of features like fast AI gateway access, observability, prompt management, and more, all while ensuring the secure management of your LLM API keys through a [virtual key](/product/ai-gateway/virtual-keys) system. + +Provider Slug. `cortex` + +## Portkey SDK Integration with Snowflake Cortex Models + +Portkey provides a consistent API to interact with models from various providers. To integrate Snowflake Cortex with Portkey: + +### 1\. Install the Portkey SDK + +Add the Portkey SDK to your application to interact with Snowflake Cortex AI's API through Portkey's gateway. + + + + ```sh + npm install --save portkey-ai + ``` + + + ```sh + pip install portkey-ai + ``` + + + + + + + +### 2\. Initialize Portkey with the Virtual Key + +To use Snowflake Cortex with Portkey, get your API key/JWT Token from the Snowflake Platform, then add it to Portkey to create the virtual key. + + + + +```js +import Portkey from 'portkey-ai' + +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your Snowflake Cortex Virtual Key +}) +``` + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Snowflake Cortex + ) + ``` + + + + + + + +### **3\. Invoke Chat Completions with** Snowflake Cortex + +Use the Portkey instance to send requests to Snowflake Cortex. You can also override the virtual key directly in the API call if needed. + + + + + ```js + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'claude-3-5-sonnet', + }); + + console.log(chatCompletion.choices); + ``` + + + +```python +completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model= 'claude-3-5-sonnet' +) + +print(completion) +``` + + + + + + +## Managing Snowflake Cortex Prompts + +You can manage all prompts to Snowflake Cortex in the [Prompt Library](/product/prompt-library). All the current models of Snowflake Cortex are supported and you can easily start testing different prompts. + +Once you're ready with your prompt, you can use the `portkey.prompts.completions.create` interface to use the prompt in your application. + + + + + + +--- + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your Snowflake Cortex](/product/ai-gateway/configs)[ requests](/product/ai-gateway/configs) +3. [Tracing Snowflake Cortex requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to Snowflake Cortex APIs](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/stability-ai.mdx b/virtual_key_old/integrations/llms/stability-ai.mdx new file mode 100644 index 00000000..4e46130a --- /dev/null +++ b/virtual_key_old/integrations/llms/stability-ai.mdx @@ -0,0 +1,102 @@ +--- +title: "Stability AI" +--- + +Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including [Stability AI APIs](https://platform.stability.ai/docs/api-reference). + +With Portkey, you can take advantage of features like fast AI gateway access, observability, prompt management, and more, all while ensuring the secure management of your LLM API keys through a [virtual key](/product/ai-gateway/virtual-keys) system. + +Provider Slug. `stability-ai` + +## Portkey SDK Integration with Stability AI + +Portkey provides a consistent API to interact with image generation models from various providers. To integrate Stability AI with Portkey: + +### 1\. Install the Portkey SDK + +Add the Portkey SDK to your application to interact with the Stability API through Portkey's gateway. + + + ```sh + npm install --save portkey-ai + ``` + + +```sh +pip install portkey-ai +``` + + + + +### 2\. Initialize Portkey with the Virtual Key + +To use Stability AI with Portkey, [get your API key from here](https://platform.stability.ai/account/keys). Then add it to Portkey to create the virtual key + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your Stability AI Virtual Key + }) + ``` + + +```python +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Stability AI +) +``` + + + + + + +### **3\. Invoke Image Generation with** Stability AI + +Use the Portkey instance to send requests to Stability AI. You can also override the virtual key directly in the API call if needed. + + + ```js + const image = await portkey.images.generate({ + model:"stable-diffusion-v1-6", + prompt:"Lucy in the sky with diamonds", + size:"1024x1024" + }) + ``` + + +```py +image = portkey.images.generate( + model="stable-diffusion-v1-6", + prompt="Lucy in the sky with diamonds", + size="1024x1024" +) +``` + + + + + + +Notice how we're using the OpenAI's image generation signature to prompt Stability allowing greater flexibility to change models and providers later if necessary. + +## Next Steps + +The complete list of features supported in the SDK are available on the link below. + + + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your Stability AI requests](/product/ai-gateway/configs) +3. [Tracing Stability AI's requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to Stability](/product/ai-gateway/fallbacks) +5. [Image generation API Reference](/provider-endpoints/images/create-image) diff --git a/virtual_key_old/integrations/llms/suggest-a-new-integration.mdx b/virtual_key_old/integrations/llms/suggest-a-new-integration.mdx new file mode 100644 index 00000000..5d1050bb --- /dev/null +++ b/virtual_key_old/integrations/llms/suggest-a-new-integration.mdx @@ -0,0 +1,5 @@ +--- +title: "Suggest a new integration!" +--- + +Have a suggestion for an integration with Portkey? Tell us on [Discord](https://discord.gg/DD7vgKK299), or drop a message on support@portkey.ai. \ No newline at end of file diff --git a/virtual_key_old/integrations/llms/together-ai.mdx b/virtual_key_old/integrations/llms/together-ai.mdx new file mode 100644 index 00000000..e4fd4aa2 --- /dev/null +++ b/virtual_key_old/integrations/llms/together-ai.mdx @@ -0,0 +1,110 @@ +--- +title: "Together AI" +--- + +Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including [Together AI APIs](https://docs.together.ai/reference/inference). + +With Portkey, you can take advantage of features like fast AI gateway access, observability, prompt management, and more, all while ensuring the secure management of your LLM API keys through a [virtual key](/product/ai-gateway/virtual-keys) system. + +Provider Slug. `together-ai` + +## Portkey SDK Integration with Together AI Models + +Portkey provides a consistent API to interact with models from various providers. To integrate Together AI with Portkey: + +### 1\. Install the Portkey SDK + +Add the Portkey SDK to your application to interact with Together AI's API through Portkey's gateway. + + + +```sh +npm install --save portkey-ai +``` + + + ```sh + pip install portkey-ai + ``` + + + + + + + +### 2\. Initialize Portkey with the Virtual Key + +To use Together AI with Portkey, [get your API key from here](https://api.together.ai/settings/api-keys). Then add it to Portkey to create the virtual key + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your Together AI Virtual Key + }) + ``` + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Together AI + ) + ``` + + + + + +### **3\. Invoke Chat Completions with** Together AI + +Use the Portkey instance to send requests to Together AI. You can also override the virtual key directly in the API call if needed. + + + ```js + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'togethercomputer/llama-2-70b-chat', + }); + + console.log(chatCompletion.choices); + ``` + + + ```python + completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model='togethercomputer/llama-2-70b-chat' + ) + + print(completion) + ``` + + + + + + +## Managing Together AI Prompts + +You can manage all prompts to Together AI in the [Prompt Library](/product/prompt-library). All the current models of Together AI are supported and you can easily start testing different prompts. + +Once you're ready with your prompt, you can use the `portkey.prompts.completions.create` interface to use the prompt in your application. + +## Next Steps + +The complete list of features supported in the SDK are available on the link below. + + + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your Together AI](/product/ai-gateway/configs)[ requests](/product/ai-gateway/configs) +3. [Tracing Together AI requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to Together AI APIs](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/triton.mdx b/virtual_key_old/integrations/llms/triton.mdx new file mode 100644 index 00000000..5031bba4 --- /dev/null +++ b/virtual_key_old/integrations/llms/triton.mdx @@ -0,0 +1,111 @@ +--- +title: "Triton" +description: "Integrate Trtiton-hosted custom models with Portkey and take them to production" +--- + +Portkey provides a robust and secure platform to observe, govern, and manage your **locally** or **privately** hosted custom models using Triton. + + +Here's the official [Triton Inference Server documentation](https://docs.nvidia.com/deeplearning/triton-inference-server/user-guide/docs/getting_started/quickstart.html) for more details. + + +## Integrating Custom Models with Portkey SDK + + +Expose your Triton server by using a tunneling service like [ngrok](https://ngrok.com/) or any other way you prefer. You can skip this step if youโ€™re self-hosting the Gateway. + +```sh +ngrok http 11434 --host-header="localhost:8080" +``` + + + + + + + ```sh + npm install --save portkey-ai + ``` + + + ```sh + pip install portkey-ai + ``` + + + + + + +1. Pass your publicly-exposed Triton server URL to Portkey with `customHost` +2. Set target `provider` as `triton`. + + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + provider: "triton", + customHost: "http://localhost:8000/v2/models/mymodel" // Your Triton Hosted URL + Authorization: "AUTH_KEY", // If you need to pass auth + }) + ``` + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", + provider="triton", + custom_host="http://localhost:8000/v2/models/mymodel" # Your Triton Hosted URL + Authorization="AUTH_KEY", # If you need to pass auth + ) + ``` + + + + +More on `custom_host` [here](/product/ai-gateway/universal-api#integrating-local-or-private-models). + + + +Use the Portkey SDK to invoke chat completions (generate) from your model, just as you would with any other provider: + + + + ```js + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }] + }); + + console.log(chatCompletion.choices); + ``` + + +```python +completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }] +) + +print(completion) +``` + + + + +## Next Steps + +Explore the complete list of features supported in the SDK: + + +--- + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your requests](/product/ai-gateway/universal-api#ollama-in-configs) +3. [Tracing requests](/product/observability/traces) +4. [Setup a fallback from triton to your local LLM](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/upstage.mdx b/virtual_key_old/integrations/llms/upstage.mdx new file mode 100644 index 00000000..07ce2e45 --- /dev/null +++ b/virtual_key_old/integrations/llms/upstage.mdx @@ -0,0 +1,500 @@ +--- +title: 'Upstage AI' +description: 'Integrate Upstage with Portkey AI for seamless completions, prompt management, and advanced features like streaming and embedding.' +--- + + +**Portkey Provider Slug:** `upstage` + + +## Overview + +Portkey offers native integrations with [Upstage](https://www.upstage.ai/) for Node.js, Python, and REST APIs. By combining Portkey with Upstage, you can create production-grade AI applications with enhanced reliability, observability, and advanced features. + + + + Explore the official Upstage documentation for comprehensive details on their APIs and models. + + + +## Getting Started + + + + Visit the [Upstage dashboard](https://console.upstage.ai/api-keys) to generate your API key. + + + + Portkey's virtual key vault simplifies your interaction with Upstage. Virtual keys act as secure aliases for your actual API keys, offering enhanced security and easier management through [budget limits](/product/ai-gateway/usage-limits) to control your API usage. + + Use the Portkey app to create a [virtual key](/product/ai-gateway/virtual-keys) associated with your Upstage API key. + + + + Now that you have your virtual key, set up the Portkey client: + + ### Portkey Hosted App + Use the Portkey API key and the Upstage virtual key to initialize the client in your preferred programming language. + + + ```python Python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Upstage + ) + ``` + + ```javascript Node.js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your Upstage Virtual Key + }) + ``` + + + ### Open Source Use + Alternatively, use Portkey's Open Source AI Gateway to enhance your app's reliability with minimal code: + + + ```python Python + from portkey_ai import Portkey, PORTKEY_GATEWAY_URL + + portkey = Portkey( + api_key="dummy", # Replace with your Portkey API key + base_url=PORTKEY_GATEWAY_URL, + Authorization="UPSTAGE_API_KEY", # Replace with your Upstage API Key + provider="upstage" + ) + ``` + + ```javascript Node.js + import Portkey, { PORTKEY_GATEWAY_URL } from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "dummy", // Replace with your Portkey API key + baseUrl: PORTKEY_GATEWAY_URL, + Authorization: "UPSTAGE_API_KEY", // Replace with your Upstage API Key + provider: "upstage" + }) + ``` + + + + +๐Ÿ”ฅ That's it! You've integrated Portkey into your application with just a few lines of code. Now let's explore making requests using the Portkey client. + +## Supported Models + + + +`Chat` - solar-pro, solar-mini and solar-mini-ja + + +`Embedding`- embedding-passage, embedding-query + + + + + + +## Supported Endpoints and Parameters + +| Endpoint | Supported Parameters | +| ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `chatComplete` | messages, max_tokens, temperature, top_p, stream, presence_penalty, frequency_penalty | +| `embed` | model, input, encoding_format, dimensions, user | + + +## Upstage Supported Features + +### Chat Completions + +Generate chat completions using Upstage models through Portkey: + + +```python Python +completion = portkey.chat.completions.create( + messages=[{"role": "user", "content": "Say this is a test"}], + model="solar-pro" +) + +print(completion.choices[0].message.content) +``` + +```javascript Node.js +const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'solar-pro', +}); + +console.log(chatCompletion.choices[0].message.content); +``` + +```curl REST +curl -X POST "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -d '{ + "messages": [{"role": "user", "content": "Say this is a test"}], + "model": "solar-pro" + }' +``` + + +### Streaming + +Stream responses for real-time output in your applications: + + +```python Python +chat_complete = portkey.chat.completions.create( + model="solar-pro", + messages=[{"role": "user", "content": "Say this is a test"}], + stream=True +) + +for chunk in chat_complete: + print(chunk.choices[0].delta.content or "", end="", flush=True) +``` + +```javascript Node.js +const stream = await portkey.chat.completions.create({ + model: 'gpt-4', + messages: [{ role: 'user', content: 'Say this is a test' }], + stream: true, +}); + +for await (const chunk of stream) { + process.stdout.write(chunk.choices[0]?.delta?.content || ''); +} +``` + +```curl REST +curl -X POST "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -d '{ + "model": "solar-pro", + "messages": [{"role": "user", "content": "Say this is a test"}], + "stream": true + }' +``` + + + +### Function Calling + +Leverage Upstage's function calling capabilities through Portkey: + + +```javascript Node.js +let tools = [{ + type: "function", + function: { + name: "getWeather", + description: "Get the current weather", + parameters: { + type: "object", + properties: { + location: { type: "string", description: "City and state" }, + unit: { type: "string", enum: ["celsius", "fahrenheit"] } + }, + required: ["location"] + } + } +}]; + +let response = await portkey.chat.completions.create({ + model: "solar-pro", + messages: [ + { role: "system", content: "You are a helpful assistant." }, + { role: "user", content: "What's the weather like in Delhi - respond in JSON" } + ], + tools, + tool_choice: "auto", +}); + +console.log(response.choices[0].finish_reason); +``` + +```python Python +tools = [{ + "type": "function", + "function": { + "name": "getWeather", + "description": "Get the current weather", + "parameters": { + "type": "object", + "properties": { + "location": {"type": "string", "description": "City and state"}, + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]} + }, + "required": ["location"] + } + } +}] + +response = portkey.chat.completions.create( + model="solar-pro", + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "What's the weather like in Delhi - respond in JSON"} + ], + tools=tools, + tool_choice="auto" +) + +print(response.choices[0].finish_reason) +``` + +```curl REST +curl -X POST "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -d '{ + "model": "solar-pro", + "messages": [ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "What'\''s the weather like in Delhi - respond in JSON"} + ], + "tools": [{ + "type": "function", + "function": { + "name": "getWeather", + "description": "Get the current weather", + "parameters": { + "type": "object", + "properties": { + "location": {"type": "string", "description": "City and state"}, + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]} + }, + "required": ["location"] + } + } + }], + "tool_choice": "auto" + }' +``` + + + +### Embeddings + +Generate embeddings for text using Upstage embedding models: + + +```python Python +response = portkey.embeddings.create( + input="Your text string goes here", + model="embedding-query" +) + +print(response.data[0].embedding) +``` + +```javascript Node.js +const response = await portkey.embeddings.create({ + input: "Your text string goes here", + model: "embedding-query" +}); + +console.log(response.data[0].embedding); +``` + +```curl REST +curl -X POST "https://api.portkey.ai/v1/embeddings" \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -d '{ + "input": "Your text string goes here", + "model": "embedding-query" + }' +``` + + + +# Portkey's Advanced Features + +## Track End-User IDs + +Portkey allows you to track user IDs passed with the user parameter in Upstage requests, enabling you to monitor user-level costs, requests, and more: + + +```python Python +response = portkey.chat.completions.create( + model="solar-pro", + messages=[{"role": "user", "content": "Say this is a test"}], + user="user_123456" +) +``` + +```javascript Node.js +const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: "user", content: "Say this is a test" }], + model: "solar-pro", + user: "user_12345", +}); +``` + +```curl REST +curl -X POST "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -d '{ + "model": "solar-pro", + "messages": [{"role": "user", "content": "Say this is a test"}], + "user": "user_123456" + }' +``` + + +When you include the user parameter in your requests, Portkey logs will display the associated user ID, as shown in the image below: + +Portkey Logs with User ID + +In addition to the `user` parameter, Portkey allows you to send arbitrary custom metadata with your requests. This powerful feature enables you to associate additional context or information with each request, which can be useful for analysis, debugging, or other custom use cases. + + + + Explore how to use custom metadata to enhance your request tracking and analysis. + + + +## Using The Gateway Config + +Here's a simplified version of how to use Portkey's Gateway Configuration: + + + + You can create a Gateway configuration using the Portkey Config Dashboard or by writing a JSON configuration in your code. In this example, requests are routed based on the user's subscription plan (paid or free). + + ```json + config = { + "strategy": { + "mode": "conditional", + "conditions": [ + { + "query": { "metadata.user_plan": { "$eq": "paid" } }, + "then": "solar-pro" + }, + { + "query": { "metadata.user_plan": { "$eq": "free" } }, + "then": "gpt-3.5" + } + ], + "default": "base-gpt4" + }, + "targets": [ + { + "name": "solar-pro", + "virtual_key": "xx" + }, + { + "name": "gpt-3.5", + "virtual_key": "yy" + } + ] + } + ``` + + + + When a user makes a request, it will pass through Portkey's AI Gateway. Based on the configuration, the Gateway routes the request according to the user's metadata. + Conditional Routing Diagram + + + + Pass the Gateway configuration to your Portkey client. You can either use the config object or the Config ID from Portkey's hosted version. + + + ```python Python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", + virtual_key="VIRTUAL_KEY", + config=portkey_config + ) + ``` + + ```javascript Node.js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "VIRTUAL_KEY", + config: portkeyConfig + }) + ``` + + + + +That's it! Portkey seamlessly allows you to make your AI app more robust using built-in gateway features. Learn more about advanced gateway features: + + + + Distribute requests across multiple targets based on defined weights. + + + Automatically switch to backup targets if the primary target fails. + + + Route requests to different targets based on specified conditions. + + + Enable caching of responses to improve performance and reduce costs. + + + +## Guardrails + +Portkey's AI gateway enables you to enforce input/output checks on requests by applying custom hooks before and after processing. Protect your user's/company's data by using PII guardrails and many more available on Portkey Guardrails: + +```json +{ + "virtual_key":"upstage-xxx", + "before_request_hooks": [{ + "id": "input-guardrail-id-xx" + }], + "after_request_hooks": [{ + "id": "output-guardrail-id-xx" + }] +} +``` + + + Explore Portkey's guardrail features to enhance the security and reliability of your AI applications. + + +## Next Steps + +The complete list of features supported in the SDK are available in our comprehensive documentation: + + + Explore the full capabilities of the Portkey SDK and how to leverage them in your projects. + + +--- + + +## Limitations + + +Portkey does not support the following Upstage features: +- Document Parse +- Document QA +- Document OCR +- Embeddings +- Translation +- Groundedness Check +- Key Information Extraction + + + + +For the most up-to-date information on supported features and endpoints, please refer to our [API Reference](/docs/api-reference/introduction). diff --git a/virtual_key_old/integrations/llms/vertex-ai.mdx b/virtual_key_old/integrations/llms/vertex-ai.mdx new file mode 100644 index 00000000..b9052bf8 --- /dev/null +++ b/virtual_key_old/integrations/llms/vertex-ai.mdx @@ -0,0 +1,1105 @@ +--- +title: "Google Vertex AI" +--- + +Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs), and embedding models into your apps, including [Google Vertex AI](https://cloud.google.com/vertex-ai?hl=en). + +With Portkey, you can take advantage of features like fast AI gateway access, observability, prompt management, and more, all while ensuring the secure management of your Vertex auth through a [virtual key](/product/ai-gateway/virtual-keys/) system + +Provider Slug. `vertex-ai` + + +## Portkey SDK Integration with Google Vertex AI + +Portkey provides a consistent API to interact with models from various providers. To integrate Google Vertex AI with Portkey: + +### 1\. Install the Portkey SDK + +Add the Portkey SDK to your application to interact with Google Vertex AI API through Portkey's gateway. + + + + ```sh + npm install --save portkey-ai + ``` + + + ```sh +pip install portkey-ai +``` + + + + + + + +### 2\. Initialize Portkey with the Virtual Key + +To integrate Vertex AI with Portkey, you'll need your `Vertex Project Id` Or `Service Account JSON` & `Vertex Region`, with which you can set up the Virtual key. + +[Here's a guide on how to find your Vertex Project details](/integrations/llms/vertex-ai#how-to-find-your-google-vertex-project-details) + +If you are integrating through Service Account File, [refer to this guide](/integrations/llms/vertex-ai#get-your-service-account-json). + + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VERTEX_VIRTUAL_KEY", // Your Vertex AI Virtual Key + }) + ``` + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VERTEX_VIRTUAL_KEY" # Replace with your virtual key for Google + ) + ``` + + + + +If you do not want to add your Vertex AI details to Portkey vault, you can directly pass them while instantiating the Portkey client. [More on that here](/integrations/llms/vertex-ai#making-requests-without-virtual-keys). + +### **3\. Invoke Chat Completions with** Vertex AI and Gemini + +Use the Portkey instance to send requests to Gemini models hosted on Vertex AI. You can also override the virtual key directly in the API call if needed. + +Vertex AI uses OAuth2 to authenticate its requests, so you need to send the **access token** additionally along with the request. + + + + +```js +const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'gemini-1.5-pro-latest', +}, {Authorization: "Bearer $YOUR_VERTEX_ACCESS_TOKEN"}); + +console.log(chatCompletion.choices); +``` + + + +```python +completion = portkey.with_options(Authorization="Bearer $YOUR_VERTEX_ACCESS_TOKEN").chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model= 'gemini-1.5-pro-latest' +) + +print(completion) +``` + + + + +To use Anthopic models on Vertex AI, prepend `anthropic.` to the model name.
+Example: `anthropic.claude-3-5-sonnet@20240620` + +Similarly, for Meta models, prepend `meta.` to the model name.
+Example: `meta.llama-3-8b-8192` +
+ + + + + +## Using Self-Deployed Models on Vertex AI (Hugging Face, Custom Models) + +Portkey supports connecting to self-deployed models on Vertex AI, including models from Hugging Face or any custom models you've deployed to a Vertex AI endpoint. + +**Requirements for Self-Deployed Models** + +To use self-deployed models on Vertex AI through Portkey: + +1. **Model Naming Convention**: When making requests to your self-deployed model, you must prefix the model name with `endpoints.` + ``` + endpoints.my_endpoint_name + ``` + +2. **Required Permissions**: The Google Cloud service account used in your Portkey virtual key must have the `aiplatform.endpoints.predict` permission. + + + +```js +const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'endpoints.my_custom_llm', // Notice the 'endpoints.' prefix +}, {Authorization: "Bearer $YOUR_VERTEX_ACCESS_TOKEN"}); + +console.log(chatCompletion.choices); +``` + + +```python +completion = portkey.with_options(Authorization="Bearer $YOUR_VERTEX_ACCESS_TOKEN").chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model= 'endpoints.my_huggingface_model' # Notice the 'endpoints.' prefix +) + +print(completion) +``` + + + + +**Why the prefix?** Vertex AI's product offering for self-deployed models is called "Endpoints." This naming convention indicates to Portkey that it should route requests to your custom endpoint rather than a standard Vertex AI model. + +This approach works for all models you can self-deploy on Vertex AI Model Garden, including Hugging Face models and your own custom models. + + + + + +## Document, Video, Audio Processing + +Vertex AI supports attaching `webm`, `mp4`, `pdf`, `jpg`, `mp3`, `wav`, etc. file types to your Gemini messages. + + +Gemini Docs: +* [Document Processing](https://ai.google.dev/gemini-api/docs/document-processing?lang=python) +* [Video & Image Processing](https://ai.google.dev/gemini-api/docs/vision?lang=python) +* [Audio Processing](https://ai.google.dev/gemini-api/docs/audio?lang=python) + + +Using Portkey, here's how you can send these media files: + + +```javascript JavaScript +const chatCompletion = await portkey.chat.completions.create({ + messages: [ + { role: 'system', content: 'You are a helpful assistant' }, + { role: 'user', content: [ + { + type: 'image_url', + image_url: { + url: 'gs://cloud-samples-data/generative-ai/image/scones.jpg' + } + }, + { + type: 'text', + text: 'Describe the image' + } + ]} + ], + model: 'gemini-1.5-pro-001', + max_tokens: 200 +}); +``` + +```python Python +completion = portkey.chat.completions.create( + messages=[ + { + "role": "system", + "content": "You are a helpful assistant" + }, + { + "role": "user", + "content": [ + { + "type": "image_url", + "image_url": { + "url": "gs://cloud-samples-data/generative-ai/image/scones.jpg" + } + }, + { + "type": "text", + "text": "Describe the image" + } + ] + } + ], + model='gemini-1.5-pro-001', + max_tokens=200 +) + +print(completion) +``` + +```sh cURL +curl --location 'https://api.portkey.ai/v1/chat/completions' \ +--header 'x-portkey-provider: vertex-ai' \ +--header 'x-portkey-vertex-region: us-central1' \ +--header 'Content-Type: application/json' \ +--header 'x-portkey-api-key: PORTKEY_API_KEY' \ +--header 'Authorization: GEMINI_API_KEY' \ +--data '{ + "model": "gemini-1.5-pro-001", + "max_tokens": 200, + "stream": false, + "messages": [ + { + "role": "system", + "content": "You are a helpful assistant" + }, + { + "role": "user", + "content": [ + { + "type": "image_url", + "image_url": { + "url": "gs://cloud-samples-data/generative-ai/image/scones.jpg" + } + }, + { + "type": "text", + "text": "describe this image" + } + ] + } + ] +}' +``` + + +## Extended Thinking (Reasoning Models) (Beta) + + +The assistants thinking response is returned in the `response_chunk.choices[0].delta.content_blocks` array, not the `response.choices[0].message.content` string. + +Gemini models do not support plugging back the reasoning into multi turn conversations, so you don't need to send the thinking message back to the model. + + +Models like `google.gemini-2.5-flash-preview-04-17` `anthropic.claude-3-7-sonnet@20250219` support [extended thinking](https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/use-claude#claude-3-7-sonnet). +This is similar to openai thinking, but you get the model's reasoning as it processes the request as well. + +Note that you will have to set [`strict_open_ai_compliance=False`](/product/ai-gateway/strict-open-ai-compliance) in the headers to use this feature. + +### Single turn conversation + + ```py Python + from portkey_ai import Portkey + + # Initialize the Portkey client + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + strict_open_ai_compliance=False + ) + + # Create the request + response = portkey.chat.completions.create( + model="anthropic.claude-3-7-sonnet@20250219", + max_tokens=3000, + thinking={ + "type": "enabled", + "budget_tokens": 2030 + }, + stream=True, + messages=[ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "when does the flight from new york to bengaluru land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + } + ] + ) + print(response) + # in case of streaming responses you'd have to parse the response_chunk.choices[0].delta.content_blocks array + # response = portkey.chat.completions.create( + # ...same config as above but with stream: true + # ) + # for chunk in response: + # if chunk.choices[0].delta: + # content_blocks = chunk.choices[0].delta.get("content_blocks") + # if content_blocks is not None: + # for content_block in content_blocks: + # print(content_block) + ``` + ```ts NodeJS + import Portkey from 'portkey-ai'; + + // Initialize the Portkey client + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY", // your vertex-ai virtual key + strictOpenAiCompliance: false + }); + + // Generate a chat completion + async function getChatCompletionFunctions() { + const response = await portkey.chat.completions.create({ + model: "anthropic.claude-3-7-sonnet@20250219", + max_tokens: 3000, + thinking: { + type: "enabled", + budget_tokens: 2030 + }, + stream: true, + messages: [ + { + role: "user", + content: [ + { + type: "text", + text: "when does the flight from new york to bengaluru land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + } + ] + }); + console.log(response); + // in case of streaming responses you'd have to parse the response_chunk.choices[0].delta.content_blocks array + // const response = await portkey.chat.completions.create({ + // ...same config as above but with stream: true + // }); + // for await (const chunk of response) { + // if (chunk.choices[0].delta?.content_blocks) { + // for (const contentBlock of chunk.choices[0].delta.content_blocks) { + // console.log(contentBlock); + // } + // } + // } + } + // Call the function + getChatCompletionFunctions(); + ``` + ```js OpenAI NodeJS + import OpenAI from 'openai'; // We're using the v4 SDK + import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + + const openai = new OpenAI({ + apiKey: 'VERTEX_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "vertex-ai", + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + strictOpenAiCompliance: false + }) + }); + + // Generate a chat completion with streaming + async function getChatCompletionFunctions(){ + const response = await openai.chat.completions.create({ + model: "anthropic.claude-3-7-sonnet@20250219", + max_tokens: 3000, + thinking: { + type: "enabled", + budget_tokens: 2030 + }, + stream: true, + messages: [ + { + role: "user", + content: [ + { + type: "text", + text: "when does the flight from new york to bengaluru land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + } + ], + }); + + console.log(response) + // in case of streaming responses you'd have to parse the response_chunk.choices[0].delta.content_blocks array + // const response = await openai.chat.completions.create({ + // ...same config as above but with stream: true + // }); + // for await (const chunk of response) { + // if (chunk.choices[0].delta?.content_blocks) { + // for (const contentBlock of chunk.choices[0].delta.content_blocks) { + // console.log(contentBlock); + // } + // } + // } + } + await getChatCompletionFunctions(); + ``` + ```py OpenAI Python + from openai import OpenAI + from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + + openai = OpenAI( + api_key='VERTEX_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="vertex-ai", + api_key="PORTKEY_API_KEY", + strict_open_ai_compliance=False + ) + ) + + + response = openai.chat.completions.create( + model="anthropic.claude-3-7-sonnet@20250219", + max_tokens=3000, + thinking={ + "type": "enabled", + "budget_tokens": 2030 + }, + stream=True, + messages=[ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "when does the flight from new york to bengaluru land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + } + ] + ) + + print(response) + ``` + ```sh cURL + curl "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-provider: vertex-ai" \ + -H "x-api-key: $VERTEX_API_KEY" \ + -H "x-portkey-strict-open-ai-compliance: false" \ + -d '{ + "model": "anthropic.claude-3-7-sonnet@20250219", + "max_tokens": 3000, + "thinking": { + "type": "enabled", + "budget_tokens": 2030 + }, + "stream": true, + "messages": [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "when does the flight from new york to bengaluru land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + } + ] + }' + ``` + + + + To disable thinking for gemini models like `google.gemini-2.5-flash-preview-04-17`, you are required to explicitly set `budget_tokens` to `0`. + ```json + "thinking": { + "type": "enabled", + "budget_tokens": 0 + } + ``` + + +### Multi turn conversation + + + ```py Python + from portkey_ai import Portkey + + # Initialize the Portkey client + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + strict_open_ai_compliance=False + ) + + # Create the request + response = portkey.chat.completions.create( + model="anthropic.claude-3-7-sonnet@20250219", + max_tokens=3000, + thinking={ + "type": "enabled", + "budget_tokens": 2030 + }, + stream=True, + messages=[ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "when does the flight from baroda to bangalore land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + }, + { + "role": "assistant", + "content": [ + { + "type": "thinking", + "thinking": "The user is asking several questions about a flight from Baroda (also known as Vadodara) to Bangalore:\n1. When does the flight land tomorrow\n2. What time does it land\n3. What is the flight number\n4. What is the baggage belt number at the arrival airport\n\nTo properly answer these questions, I would need access to airline flight schedules and airport information systems. However, I don't have:\n- Real-time or scheduled flight information\n- Access to airport baggage claim allocation systems\n- Information about specific flights between these cities\n- The ability to look up tomorrow's specific flight schedules\n\nThis question requires current, specific flight information that I don't have access to. Instead of guessing or providing potentially incorrect information, I should explain this limitation and suggest ways the user could find this information.", + "signature": "EqoBCkgIARABGAIiQBVA7FBNLRtWarDSy9TAjwtOpcTSYHJ+2GYEoaorq3V+d3eapde04bvEfykD/66xZXjJ5yyqogJ8DEkNMotspRsSDKzuUJ9FKhSNt/3PdxoMaFZuH+1z1aLF8OeQIjCrA1+T2lsErrbgrve6eDWeMvP+1sqVqv/JcIn1jOmuzrPi2tNz5M0oqkOO9txJf7QqEPPw6RG3JLO2h7nV1BMN6wE=" + } + ] + }, + { + "role": "user", + "content": "thanks that's good to know, how about to chennai?" + } + ] + ) + print(response) + ``` + ```ts NodeJS + import Portkey from 'portkey-ai'; + + // Initialize the Portkey client + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY", // your vertex-ai virtual key + strictOpenAiCompliance: false + }); + + // Generate a chat completion + async function getChatCompletionFunctions() { + const response = await portkey.chat.completions.create({ + model: "anthropic.claude-3-7-sonnet@20250219", + max_tokens: 3000, + thinking: { + type: "enabled", + budget_tokens: 2030 + }, + stream: true, + messages: [ + { + role: "user", + content: [ + { + type: "text", + text: "when does the flight from baroda to bangalore land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + }, + { + role: "assistant", + content: [ + { + type: "thinking", + thinking: "The user is asking several questions about a flight from Baroda (also known as Vadodara) to Bangalore:\n1. When does the flight land tomorrow\n2. What time does it land\n3. What is the flight number\n4. What is the baggage belt number at the arrival airport\n\nTo properly answer these questions, I would need access to airline flight schedules and airport information systems. However, I don't have:\n- Real-time or scheduled flight information\n- Access to airport baggage claim allocation systems\n- Information about specific flights between these cities\n- The ability to look up tomorrow's specific flight schedules\n\nThis question requires current, specific flight information that I don't have access to. Instead of guessing or providing potentially incorrect information, I should explain this limitation and suggest ways the user could find this information.", + signature: "EqoBCkgIARABGAIiQBVA7FBNLRtWarDSy9TAjwtOpcTSYHJ+2GYEoaorq3V+d3eapde04bvEfykD/66xZXjJ5yyqogJ8DEkNMotspRsSDKzuUJ9FKhSNt/3PdxoMaFZuH+1z1aLF8OeQIjCrA1+T2lsErrbgrve6eDWeMvP+1sqVqv/JcIn1jOmuzrPi2tNz5M0oqkOO9txJf7QqEPPw6RG3JLO2h7nV1BMN6wE=" + } + ] + }, + { + role: "user", + content: "thanks that's good to know, how about to chennai?" + } + ] + }); + console.log(response); + } + // Call the function + getChatCompletionFunctions(); + ``` + ```js OpenAI NodeJS + import OpenAI from 'openai'; // We're using the v4 SDK + import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + + const openai = new OpenAI({ + apiKey: 'ANTHROPIC_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "vertex-ai", + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + strictOpenAiCompliance: false + }) + }); + + // Generate a chat completion with streaming + async function getChatCompletionFunctions(){ + const response = await openai.chat.completions.create({ + model: "anthropic.claude-3-7-sonnet@20250219", + max_tokens: 3000, + thinking: { + type: "enabled", + budget_tokens: 2030 + }, + stream: true, + messages: [ + { + role: "user", + content: [ + { + type: "text", + text: "when does the flight from baroda to bangalore land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + }, + { + role: "assistant", + content: [ + { + type: "thinking", + thinking: "The user is asking several questions about a flight from Baroda (also known as Vadodara) to Bangalore:\n1. When does the flight land tomorrow\n2. What time does it land\n3. What is the flight number\n4. What is the baggage belt number at the arrival airport\n\nTo properly answer these questions, I would need access to airline flight schedules and airport information systems. However, I don't have:\n- Real-time or scheduled flight information\n- Access to airport baggage claim allocation systems\n- Information about specific flights between these cities\n- The ability to look up tomorrow's specific flight schedules\n\nThis question requires current, specific flight information that I don't have access to. Instead of guessing or providing potentially incorrect information, I should explain this limitation and suggest ways the user could find this information.", + signature: "EqoBCkgIARABGAIiQBVA7FBNLRtWarDSy9TAjwtOpcTSYHJ+2GYEoaorq3V+d3eapde04bvEfykD/66xZXjJ5yyqogJ8DEkNMotspRsSDKzuUJ9FKhSNt/3PdxoMaFZuH+1z1aLF8OeQIjCrA1+T2lsErrbgrve6eDWeMvP+1sqVqv/JcIn1jOmuzrPi2tNz5M0oqkOO9txJf7QqEPPw6RG3JLO2h7nV1BMN6wE=" + } + ] + }, + { + role: "user", + content: "thanks that's good to know, how about to chennai?" + } + ], + }); + + console.log(response) + + } + await getChatCompletionFunctions(); + ``` + ```py OpenAI Python + from openai import OpenAI + from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + + openai = OpenAI( + api_key='Anthropic_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="vertex-ai", + api_key="PORTKEY_API_KEY", + strict_open_ai_compliance=False + ) + ) + + + response = openai.chat.completions.create( + model="anthropic.claude-3-7-sonnet@20250219", + max_tokens=3000, + thinking={ + "type": "enabled", + "budget_tokens": 2030 + }, + stream=True, + messages=[ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "when does the flight from baroda to bangalore land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + }, + { + "role": "assistant", + "content": [ + { + "type": "thinking", + "thinking": "The user is asking several questions about a flight from Baroda (also known as Vadodara) to Bangalore:\n1. When does the flight land tomorrow\n2. What time does it land\n3. What is the flight number\n4. What is the baggage belt number at the arrival airport\n\nTo properly answer these questions, I would need access to airline flight schedules and airport information systems. However, I don't have:\n- Real-time or scheduled flight information\n- Access to airport baggage claim allocation systems\n- Information about specific flights between these cities\n- The ability to look up tomorrow's specific flight schedules\n\nThis question requires current, specific flight information that I don't have access to. Instead of guessing or providing potentially incorrect information, I should explain this limitation and suggest ways the user could find this information.", + signature: "EqoBCkgIARABGAIiQBVA7FBNLRtWarDSy9TAjwtOpcTSYHJ+2GYEoaorq3V+d3eapde04bvEfykD/66xZXjJ5yyqogJ8DEkNMotspRsSDKzuUJ9FKhSNt/3PdxoMaFZuH+1z1aLF8OeQIjCrA1+T2lsErrbgrve6eDWeMvP+1sqVqv/JcIn1jOmuzrPi2tNz5M0oqkOO9txJf7QqEPPw6RG3JLO2h7nV1BMN6wE=" + } + ] + }, + { + "role": "user", + "content": "thanks that's good to know, how about to chennai?" + } + ] + ) + + print(response) + ``` + ```sh cURL + curl "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-provider: vertex-ai" \ + -H "x-api-key: $VERTEX_API_KEY" \ + -H "x-portkey-strict-open-ai-compliance: false" \ + -d '{ + "model": "anthropic.claude-3-7-sonnet@20250219", + "max_tokens": 3000, + "thinking": { + "type": "enabled", + "budget_tokens": 2030 + }, + "stream": true, + "messages": [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "when does the flight from baroda to bangalore land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + }, + { + "role": "assistant", + "content": [ + { + "type": "thinking", + "thinking": "The user is asking several questions about a flight from Baroda (also known as Vadodara) to Bangalore:\n1. When does the flight land tomorrow\n2. What time does it land\n3. What is the flight number\n4. What is the baggage belt number at the arrival airport\n\nTo properly answer these questions, I would need access to airline flight schedules and airport information systems. However, I don't have:\n- Real-time or scheduled flight information\n- Access to airport baggage claim allocation systems\n- Information about specific flights between these cities\n- The ability to look up tomorrow's specific flight schedules\n\nThis question requires current, specific flight information that I don't have access to. Instead of guessing or providing potentially incorrect information, I should explain this limitation and suggest ways the user could find this information.", + "signature": "EqoBCkgIARABGAIiQBVA7FBNLRtWarDSy9TAjwtOpcTSYHJ+2GYEoaorq3V+d3eapde04bvEfykD/66xZXjJ5yyqogJ8DEkNMotspRsSDKzuUJ9FKhSNt/3PdxoMaFZuH+1z1aLF8OeQIjCrA1+T2lsErrbgrve6eDWeMvP+1sqVqv/JcIn1jOmuzrPi2tNz5M0oqkOO9txJf7QqEPPw6RG3JLO2h7nV1BMN6wE=" + } + ] + }, + { + "role": "user", + "content": "thanks that's good to know, how about to chennai?" + } + ] + }' + ``` + + +### Sending `base64` Image + +Here, you can send the `base64` image data along with the `url` field too: + +```json +"url": "data:image/png;base64,UklGRkacAABXRUJQVlA4IDqcAAC....." +``` + +This same message format also works for all other media types โ€” just send your media file in the `url` field, like `"url": "gs://cloud-samples-data/video/animals.mp4"` for google cloud urls and `"url":"https://download.samplelib.com/mp3/sample-3s.mp3"` for public urls + +Your URL should have the file extension, this is used for inferring `MIME_TYPE` which is a required parameter for prompting Gemini models with files + + +## Text Embedding Models + +You can use any of Vertex AI's `English` and `Multilingual` models through Portkey, in the familar OpenAI-schema. + + +The Gemini-specific parameter `task_type` is also supported on Portkey. + + + +```javascript +import Portkey from 'portkey-ai'; + +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "VERTEX_VIRTUAL_KEY" +}); + +// Generate embeddings +async function getEmbeddings() { + const embeddings = await portkey.embeddings.create({ + input: "embed this", + model: "text-multilingual-embedding-002", + // @ts-ignore (if using typescript) + task_type: "CLASSIFICATION", // Optional + }, {Authorization: "Bearer $YOUR_VERTEX_ACCESS_TOKEN"}); + + console.log(embeddings); +} +await getEmbeddings(); +``` + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VERTEX_VIRTUAL_KEY" +) + +# Generate embeddings +def get_embeddings(): + embeddings = portkey.with_options(Authorization="Bearer $YOUR_VERTEX_ACCESS_TOKEN").embeddings.create( + input='The vector representation for this text', + model='text-embedding-004', + task_type="CLASSIFICATION" # Optional + ) + print(embeddings) + +get_embeddings() +``` + + +```sh + curl 'https://api.portkey.ai/v1/embeddings' \ + -H 'Content-Type: application/json' \ + -H 'x-portkey-api-key: PORTKEY_API_KEY' \ + -H 'x-portkey-provider: vertex-ai' \ + -H 'Authorization: Bearer VERTEX_AI_ACCESS_TOKEN' \ + -H 'x-portkey-virtual-key: $VERTEX_VIRTUAL_KEY' \ + --data-raw '{ + "model": "textembedding-004", + "input": "A HTTP 246 code is used to signify an AI response containing hallucinations or other inaccuracies", + "task_type": "CLASSIFICATION" + }' +``` + + + + +## Function Calling + +Portkey supports function calling mode on Google's Gemini Models. Explore this Cookbook for a deep dive and examples: + + +[Function Calling](/guides/getting-started/function-calling) + +## Managing Vertex AI Prompts + +You can manage all prompts to Google Gemini in the [Prompt Library](/product/prompt-library). All the models in the model garden are supported and you can easily start testing different prompts. + +Once you're ready with your prompt, you can use the `portkey.prompts.completions.create` interface to use the prompt in your application. + +## Image Generation Models + +Portkey supports the `Imagen API` on Vertex AI for image generations, letting you easily make requests in the familar OpenAI-compliant schema. + + +```sh cURL +curl https://api.portkey.ai/v1/images/generations \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-virtual-key: $PORTKEY_PROVIDER_VIRTUAL_KEY" \ + -d '{ + "prompt": "Cat flying to mars from moon", + "model":"imagen-3.0-generate-001" + }' +``` +```py Python +from portkey_ai import Portkey + +client = Portkey( + api_key = "PORTKEY_API_KEY", + virtual_key = "PROVIDER_VIRTUAL_KEY" +) + +client.images.generate( + prompt = "Cat flying to mars from moon", + model = "imagen-3.0-generate-001" +) +``` +```ts JavaScript +import Portkey from 'portkey-ai'; + +const client = new Portkey({ + apiKey: 'PORTKEY_API_KEY', + virtualKey: 'PROVIDER_VIRTUAL_KEY' +}); + +async function main() { + const image = await client.images.generate({ + prompt: "Cat flying to mars from moon", + model: "imagen-3.0-generate-001" + }); + + console.log(image.data); +} +main(); +``` + + +[Image Generation API Reference](/api-reference/inference-api/images/create-image) + +### List of Supported Imagen Models +- `imagen-3.0-generate-001` +- `imagen-3.0-fast-generate-001` +- `imagegeneration@006` +- `imagegeneration@005` +- `imagegeneration@002` + +## Grounding with Google Search + +Vertex AI supports grounding with Google Search. This is a feature that allows you to ground your LLM responses with real-time search results. +Grounding is invoked by passing the `google_search` tool (for newer models like gemini-2.0-flash-001), and `google_search_retrieval` (for older models like gemini-1.5-flash) in the `tools` array. + +```json +"tools": [ + { + "type": "function", + "function": { + "name": "google_search" // or google_search_retrieval for older models + } + }] +``` + + +If you mix regular tools with grounding tools, vertex might throw an error saying only one tool can be used at a time. + + +## gemini-2.0-flash-thinking-exp and other thinking/reasoning models + +`gemini-2.0-flash-thinking-exp` models return a Chain of Thought response along with the actual inference text, +this is not openai compatible, however, Portkey supports this by adding a `\r\n\r\n` and appending the two responses together. +You can split the response along this pattern to get the Chain of Thought response and the actual inference text. + +If you require the Chain of Thought response along with the actual inference text, pass the [strict open ai compliance flag](/product/ai-gateway/strict-open-ai-compliance) as `false` in the request. + +If you want to get the inference text only, pass the [strict open ai compliance flag](/product/ai-gateway/strict-open-ai-compliance) as `true` in the request. + +--- + +## Making Requests Without Virtual Keys + +You can also pass your Vertex AI details & secrets directly without using the Virtual Keys in Portkey. + +Vertex AI expects a `region`, a `project ID` and the `access token` in the request for a successful completion request. This is how you can specify these fields directly in your requests: + + + + + +### Example Request + + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + vertexProjectId: "sample-55646", + vertexRegion: "us-central1", + provider:"vertex_ai", + Authorization: "$GCLOUD AUTH PRINT-ACCESS-TOKEN" + }) + + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'gemini-pro', + }); + + console.log(chatCompletion.choices); + ``` + + +```python +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY", + vertex_project_id="sample-55646", + vertex_region="us-central1", + provider="vertex_ai", + Authorization="$GCLOUD AUTH PRINT-ACCESS-TOKEN" +) + +completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model= 'gemini-1.5-pro-latest' +) + +print(completion) +``` + + +```js +import OpenAI from "openai"; +import { PORTKEY_GATEWAY_URL, createHeaders } from "portkey-ai"; + +const portkey = new OpenAI({ + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + apiKey: "PORTKEY_API_KEY", + provider: "vertex-ai", + vertexRegion: "us-central1", + vertexProjectId: "xxx" + Authorization: "Bearer $GCLOUD AUTH PRINT-ACCESS-TOKEN", + // forwardHeaders: ["Authorization"] // You can also directly forward the auth token to Google + }), +}); + +async function main() { + const response = await portkey.chat.completions.create({ + messages: [{ role: "user", content: "1729" }], + model: "gemini-1.5-flash-001", + max_tokens: 32, + }); + + console.log(response.choices[0].message.content); +} + +main(); +``` + + +```sh +curl 'https://api.portkey.ai/v1/chat/completions' \ +-H 'Content-Type: application/json' \ +-H 'x-portkey-api-key: PORTKEY_API_KEY' \ +-H 'x-portkey-provider: vertex-ai' \ +-H 'Authorization: Bearer VERTEX_AI_ACCESS_TOKEN' \ +-H 'x-portkey-vertex-project-id: sample-94994' \ +-H 'x-portkey-vertex-region: us-central1' \ +--data '{ + "model": "gemini-1.5-pro", + "messages": [ + { + "role": "system", + "content": "You are a helpful assistant" + }, + { + "role": "user", + "content": "what is a portkey?" + } + ] +}' +``` + + + +For further questions on custom Vertex AI deployments or fine-grained access tokens, reach out to us on support@portkey.ai + + + + +### How to Find Your Google Vertex Project Details + +To obtain your **Vertex Project ID and Region,** [navigate to Google Vertex Dashboard]( https://console.cloud.google.com/vertex-ai). + +* You can copy the **Project ID** located at the top left corner of your screen. +* Find the **Region dropdown** on the same page to get your Vertex Region. + + +![Logo](/images/llms/vertex.png) + + +### Get Your Service Account JSON +* [Follow this process](https://cloud.google.com/iam/docs/keys-create-delete) to get your Service Account JSON. + +When selecting Service Account File as your authentication method, you'll need to: + +1. Upload your Google Cloud service account JSON file +2. Specify the Vertex Region + +This method is particularly important for using self-deployed models, as your service account must have the `aiplatform.endpoints.predict` permission to access custom endpoints. + +Learn more about permission on your Vertex IAM key [here](https://cloud.google.com/vertex-ai/docs/general/iam-permissions). + + +**For Self-Deployed Models**: Your service account **must** have the `aiplatform.endpoints.predict` permission in Google Cloud IAM. Without this specific permission, requests to custom endpoints will fail. + + +### Using Project ID and Region Authentication + +For standard Vertex AI models, you can simply provide: + +1. Your Vertex Project ID (found in your Google Cloud console) +2. The Vertex Region where your models are deployed + +This method is simpler but may not have all the permissions needed for custom endpoints. + + + + + +--- + +## Next Steps + +The complete list of features supported in the SDK are available on the link below. + + + + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your Vertex AI requests](/product/ai-gateway/configs) +3. [Tracing Vertex AI requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to Vertex AI APIs](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/vertex-ai/batches.mdx b/virtual_key_old/integrations/llms/vertex-ai/batches.mdx new file mode 100644 index 00000000..0521a0bf --- /dev/null +++ b/virtual_key_old/integrations/llms/vertex-ai/batches.mdx @@ -0,0 +1,444 @@ +--- +title: "Batches" +description: "Perform batch inference with Vertex AI" +--- + +With Portkey, you can perform batch inference operations with Vertex AI models. This is the most efficient way to: +- Process large volumes of data with Vertex AI models +- Test your data with different foundation models +- Perform A/B testing with different foundation models + +### Upload a file for batch inference + + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VERTEX_VIRTUAL_KEY", # Add your Vertex virtual key + vertex_storage_bucket_name="your_bucket_name", # Specify the GCS bucket name + provider_file_name="your_file_name.jsonl", # Specify the file name in GCS + provider_model="gemini-1.5-flash-001" # Specify the model to use +) + +# Upload a file for batch inference +file = portkey.files.create( + file=open("dataset.jsonl", "rb"), + purpose="batch" +) + +print(file) +``` + + +```typescript +import { Portkey } from "portkey-ai"; +import * as fs from 'fs'; + +// Initialize the Portkey client +const portkey = Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VERTEX_VIRTUAL_KEY", // Add your Vertex virtual key + vertexStorageBucketName: "your_bucket_name", // Specify the GCS bucket name + providerFileName: "your_file_name.jsonl", // Specify the file name in GCS + providerModel: "gemini-1.5-flash-001" // Specify the model to use +}); + +(async () => { + // Upload a file for batch inference + const file = await portkey.files.create({ + file: fs.createReadStream("dataset.jsonl"), + purpose: "batch" + }); + + console.log(file); +})(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + virtual_key="VERTEX_VIRTUAL_KEY", + api_key="PORTKEY_API_KEY", + vertex_storage_bucket_name="your_bucket_name", + provider_file_name="your_file_name.jsonl", + provider_model="gemini-1.5-flash-001" + ) +) + +# Upload a file for batch inference +file = openai.files.create( + file=open("dataset.jsonl", "rb"), + purpose="batch" +) + +print(file) +``` + + +```typescript +import OpenAI from 'openai'; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'; +import * as fs from 'fs'; + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + virtualKey: "VERTEX_VIRTUAL_KEY", + apiKey: "PORTKEY_API_KEY", + vertexStorageBucketName: "your_bucket_name", + providerFileName: "your_file_name.jsonl", + providerModel: "gemini-1.5-flash-001" + }) +}); + +(async () => { + // Upload a file for batch inference + const file = await openai.files.create({ + file: fs.createReadStream("dataset.jsonl"), + purpose: "batch" + }); + + console.log(file); +})(); +``` + + +```sh +curl -X POST --header 'x-portkey-api-key: ' \ + --header 'x-portkey-virtual-key: ' \ + --header 'x-portkey-vertex-storage-bucket-name: ' \ + --header 'x-portkey-provider-file-name: .jsonl' \ + --header 'x-portkey-provider-model: ' \ + --form 'purpose="batch"' \ + --form 'file=@dataset.jsonl' \ + 'https://api.portkey.ai/v1/files' +``` + + + +### Create a batch job + + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VERTEX_VIRTUAL_KEY" # Add your Vertex virtual key +) + +# Create a batch inference job +batch_job = portkey.batches.create( + input_file_id="", # File ID from the upload step + endpoint="/v1/chat/completions", # API endpoint to use + completion_window="24h", # Time window for completion + model="gemini-1.5-flash-001" +) + +print(batch_job) +``` + + +```typescript +import { Portkey } from "portkey-ai"; + +// Initialize the Portkey client +const portkey = Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VERTEX_VIRTUAL_KEY" // Add your Vertex virtual key +}); + +(async () => { + // Create a batch inference job + const batchJob = await portkey.batches.create({ + input_file_id: "", // File ID from the upload step + endpoint: "/v1/chat/completions", // API endpoint to use + completion_window: "24h", // Time window for completion + model:"gemini-1.5-flash-001" + }); + + console.log(batchJob); +})(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + virtual_key="VERTEX_VIRTUAL_KEY", + api_key="PORTKEY_API_KEY" + ) +) + +# Create a batch inference job +batch_job = openai.batches.create( + input_file_id="", # File ID from the upload step + endpoint="/v1/chat/completions", # API endpoint to use + completion_window="24h", # Time window for completion + model="gemini-1.5-flash-001" +) + +print(batch_job) +``` + + +```typescript +import OpenAI from 'openai'; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'; + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + virtualKey: "VERTEX_VIRTUAL_KEY", + apiKey: "PORTKEY_API_KEY" + }) +}); + +(async () => { + // Create a batch inference job + const batchJob = await openai.batches.create({ + input_file_id: "", // File ID from the upload step + endpoint: "/v1/chat/completions", // API endpoint to use + completion_window: "24h", // Time window for completion + model:"gemini-1.5-flash-001" + }); + + console.log(batchJob); +})(); +``` + + +```sh +curl -X POST --header 'Content-Type: application/json' \ + --header 'x-portkey-api-key: ' \ + --header 'x-portkey-virtual-key: ' \ + --data \ + $'{"input_file_id": "", "endpoint": "/v1/chat/completions", "completion_window": "24h", "model":"gemini-1.5-flash-001"}' \ +'https://api.portkey.ai/v1/batches' +``` + + + +### List batch jobs + + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VERTEX_VIRTUAL_KEY" # Add your Vertex virtual key +) + +# List all batch jobs +jobs = portkey.batches.list( + limit=10 # Optional: Number of jobs to retrieve (default: 20) +) + +print(jobs) +``` + + +```typescript +import { Portkey } from "portkey-ai"; + +// Initialize the Portkey client +const portkey = Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VERTEX_VIRTUAL_KEY" // Add your Vertex virtual key +}); + +(async () => { + // List all batch jobs + const jobs = await portkey.batches.list({ + limit: 10 // Optional: Number of jobs to retrieve (default: 20) + }); + + console.log(jobs); +})(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + virtual_key="VERTEX_VIRTUAL_KEY", + api_key="PORTKEY_API_KEY" + ) +) + +# List all batch jobs +jobs = openai.batches.list( + limit=10 # Optional: Number of jobs to retrieve (default: 20) +) + +print(jobs) +``` + + + +```typescript +import OpenAI from 'openai'; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'; + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + virtualKey: "VERTEX_VIRTUAL_KEY", + apiKey: "PORTKEY_API_KEY" + }) +}); + +(async () => { + // List all batch jobs + const jobs = await openai.batches.list({ + limit: 10 // Optional: + }); + + console.log(jobs); +})(); +``` + + + +```sh +curl -X GET --header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' \ +'https://api.portkey.ai/v1/batches' +``` + + + +### Get a batch job + + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VERTEX_VIRTUAL_KEY" # Add your Vertex virtual key +) + +# Retrieve a specific batch job +job = portkey.batches.retrieve( + "job_id" # The ID of the batch job to retrieve +) + +print(job) +``` + + +```typescript +import { Portkey } from "portkey-ai"; + +// Initialize the Portkey client +const portkey = Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VERTEX_VIRTUAL_KEY" // Add your Vertex virtual key +}); + +(async () => { + // Retrieve a specific batch job + const job = await portkey.batches.retrieve( + "job_id" // The ID of the batch job to retrieve + ); + + console.log(job); +})(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + virtual_key="VERTEX_VIRTUAL_KEY", + api_key="PORTKEY_API_KEY" + ) +) + +# Retrieve a specific batch job +job = openai.batches.retrieve( + "job_id" # The ID of the batch job to retrieve +) + +print(job) +``` + + +```typescript +import OpenAI from 'openai'; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'; + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + virtualKey: "VERTEX_VIRTUAL_KEY", + apiKey: "PORTKEY_API_KEY" + }) +}); + +(async () => { + // Retrieve a specific batch job + const job = await openai.batches.retrieve( + "job_id" // The ID of the batch job to retrieve + ); + + console.log(job); +})(); +``` + + +```sh +curl -X GET --header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' \ +'https://api.portkey.ai/v1/batches/' +``` + + + +### Get batch job output + + +```sh +curl -X GET --header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' \ +'https://api.portkey.ai/v1/batches//output' +``` + + \ No newline at end of file diff --git a/virtual_key_old/integrations/llms/vertex-ai/controlled-generations.mdx b/virtual_key_old/integrations/llms/vertex-ai/controlled-generations.mdx new file mode 100644 index 00000000..4099d8e4 --- /dev/null +++ b/virtual_key_old/integrations/llms/vertex-ai/controlled-generations.mdx @@ -0,0 +1,290 @@ +--- +title: 'Controlled Generations' +description: "Controlled Generations ensure that the model always follows your supplied [JSON schema](https://json-schema.org/overview/what-is-jsonschema). Portkey supports Vertex AI's Controlled Generations feature out of the box with our SDKs & APIs. +" +--- + + +Controlled Generations allows you to constrain model responses to predefined sets of values. This is particularly useful for classification tasks, multiple choice responses, and structured data extraction. + + +This feature is available for `Gemini 1.5 Pro` & `Gemini 1.5 Flash` models. + + +## With Pydantic & Zod + +Portkey SDKs for [Python and JavaScript](/api-reference/portkey-sdk-client) also make it easy to define object schemas using [Pydantic](https://docs.pydantic.dev/latest/) and [Zod](https://zod.dev/) respectively. Below, you can see how to extract information from unstructured text that conforms to a schema defined in code. + + + +```python +from portkey_ai import Portkey +from pydantic import BaseModel + +class Step(BaseModel): + explanation: str + output: str + +class MathReasoning(BaseModel): + steps: list[Step] + final_answer: str + +portkey = Portkey( + apiKey= "PORTKEY_API_KEY", + virtual_key="VERTEX_VIRTUAL_KEY" +) + +completion = portkey.beta.chat.completions.parse( + model="gemini-1.5-pro-002", + messages=[ + {"role": "system", "content": "You are a helpful math tutor. Guide the user through the solution step by step."}, + {"role": "user", "content": "how can I solve 8x + 7 = -23"} + ], + response_format=MathReasoning, +) + +print(completion.choices[0].message) +print(completion.choices[0].message.parsed) + +``` + + +To use Zod with VerteX AI you will also need to import `{ zodResponseFormat }` from `openai/helpers/zod` + + ```typescript + import { Portkey } from 'portkey-ai'; + import { z } from 'zod'; + import { zodResponseFormat } from "openai/helpers/zod"; + + const MathReasoning = z.object({ + steps: z.array(z.object({ explanation: z.string(), output: z.string() })), + final_answer: z.string() + }); + + const portkey = new Portkey({ + apiKey: "YOUR_API_KEY", + virtualKey: "YOUR_GEMINI_VIRTUAL_KEY" + }); + + async function runMathTutor() { + try { + const completion = await portkey.chat.completions.create({ + model: "gemini-1.5-pro-002", + messages: [ + { role: "system", content: "You are a helpful math tutor." }, + { role: "user", content: "Solve 8x + 7 = -23" } + ], + response_format: zodResponseFormat(MathReasoning, "MathReasoning") + }); + + console.log(completion.choices[0].message.content); + } catch (error) { + console.error("Error:", error); + } + } + + runMathTutor(); + ``` + + + +## Using Enums +You can also use enums to constrain the model's output to a predefined set of values. This is particularly useful for classification tasks and multiple choice responses. + + + ```python + from portkey_ai import Portkey + from enum import Enum + from pydantic import BaseModel + + class InstrumentClass(Enum): + PERCUSSION = "Percussion" + STRING = "String" + WOODWIND = "Woodwind" + BRASS = "Brass" + KEYBOARD = "Keyboard" + + # Initialize Portkey with API details + portkey = Portkey( + api_key="PORTKEY_API_KEY", + virtual_key="VERTEX_VIRTUAL_KEY" + ) + + # Simple enum classification + completion = portkey.chat.completions.create( + model="gemini-1.5-pro-002", + messages=[ + {"role": "system", "content": "Classify the musical instrument."}, + {"role": "user", "content": "What type of instrument is a piano?"} + ], + response_format={ + "type": "json_schema", + "json_schema": { + "type": "string", + "enum": [e.value for e in InstrumentClass], + "title": "instrument_classification" + } + } + ) + + print(completion.choices[0].message.content) + ``` + + + +## Using JSON schema Directly +This method is more portable across different languages and doesn't require additional libraries, but lacks the integrated type checking of the Pydantic/Zod approach. Choose the method that best fits your project's needs. + + + + ```typescript + import Portkey from "portkey-ai"; + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "VERTEX_VIRTUAL_KEY", + }); + + async function main() { + const completion = await portkey.chat.completions.create({ + model: "gemini-1.5-pro-002", + messages: [ + { role: "system", content: "Extract the event information." }, + { + role: "user", + content: "Alice and Bob are going to a science fair on Friday.", + }, + ], + response_format: { + type: "json_schema", + json_schema: { + name: "math_reasoning", + schema: { + type: "object", + properties: { + steps: { + type: "array", + items: { + type: "object", + properties: { + explanation: { type: "string" }, + output: { type: "string" }, + }, + required: ["explanation", "output"], + additionalProperties: false, + }, + }, + final_answer: { type: "string" }, + }, + required: ["steps", "final_answer"], + additionalProperties: false, + }, + strict: true, + }, + }, + }); + const event = completion.choices[0].message?.content; + console.log(event); + } + + main(); + ``` + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", + virtual_key="VERTEX_VIRTUAL_KEY" + ) + + completion = portkey.chat.completions.create( + model="gemini-1.5-pro-002", + messages=[ + {"role": "system", "content": "Extract the event information."}, + {"role": "user", "content": "A meteor the size of 1000 football stadiums will hit earth this Sunday"}, + ], + response_format={ + "type": "json_schema", + "json_schema": { + "name": "math_reasoning", + "schema": { + "type": "object", + "properties": { + "steps": { + "type": "array", + "items": { + "type": "object", + "properties": { + "explanation": { "type": "string" }, + "output": { "type": "string" } + }, + "required": ["explanation", "output"], + "additionalProperties": False + } + }, + "final_answer": { "type": "string" } + }, + "required": ["steps", "final_answer"], + "additionalProperties": False + }, + "strict": True + } + }, + ) + + print(completion.choices[0].message.content) + ``` + + + ```sh + curl https://api.portkey.ai/v1/chat/completions \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-virtual-key: $VERTEX_VIRTUAL_KEY" \ + -H "Content-Type: application/json" \ + -d '{ + "model": "gemini-1.5-pro-002", + "messages": [ + { + "role": "system", + "content": "You are a helpful math tutor. Guide the user through the solution step by step." + }, + { + "role": "user", + "content": "how can I solve 8x + 7 = -23" + } + ], + "response_format": { + "type": "json_schema", + "json_schema": { + "name": "math_reasoning", + "schema": { + "type": "object", + "properties": { + "steps": { + "type": "array", + "items": { + "type": "object", + "properties": { + "explanation": { "type": "string" }, + "output": { "type": "string" } + }, + "required": ["explanation", "output"], + "additionalProperties": false + } + }, + "final_answer": { "type": "string" } + }, + "required": ["steps", "final_answer"], + "additionalProperties": false + }, + "strict": true + } + } + }' + ``` + + + +For more, refer to Google Vertex AI's [detailed documentation on Controlled Generations here](https://cloud.google.com/docs/authentication/provide-credentials-adc#local-dev). diff --git a/virtual_key_old/integrations/llms/vertex-ai/embeddings.mdx b/virtual_key_old/integrations/llms/vertex-ai/embeddings.mdx new file mode 100644 index 00000000..2c654a2f --- /dev/null +++ b/virtual_key_old/integrations/llms/vertex-ai/embeddings.mdx @@ -0,0 +1,371 @@ +--- +title: "Embeddings" +description: "Get embeddings from Vertex AI" +--- + +Vertex AI offers wide ranging support for embedding text, images and videos. +Portkey provides a standardized interface for embedding multiple modalities. + +## Embedding Text + + ```python Python + from portkey_ai import Portkey + + client = Portkey( + api_key="YOUR_PORTKEY_API_KEY", # defaults to os.environ.get("PORTKEY_API_KEY") + virtual_key="VIRTUAL_KEY", + ) + + embeddings = client.embeddings.create( + model="textembedding-gecko@003", + input_type="classification", + input="The food was delicious and the waiter...", + # input=["text to embed", "more text to embed"], # if you would like to embed multiple texts + ) + ``` + + ```javascript NodeJS + import { Portkey } from 'portkey-ai'; + + const portkey = new Portkey({ + apiKey: "YOUR_API_KEY", + virtualKey: "YOUR_VIRTUAL_KEY" + }); + + const embedding = await portkey.embeddings.create({ + input: 'Name the tallest buildings in Hawaii', + // input: ['text to embed', 'more text to embed'], // if you would like to embed multiple texts + model: 'textembedding-gecko@003' + }); + + console.log(embedding); + ``` + + ```sh cURL + curl --location 'https://api.portkey.ai/v1/embeddings' \ + --header 'Content-Type: application/json' \ + --header 'x-portkey-api-key: PORTKEY_API_KEY' \ + --header 'x-portkey-virtual-key: PORTKEY_VIRTUAL_KEY' \ + --data-raw '{ + "model": "textembedding-gecko@003", + "input": [ + "A HTTP 246 code is used to signify an AI response containing hallucinations or other inaccuracies", + "246: Partially incorrect response" + ], + # "input": "Name the tallest buildings in Hawaii", + "input_type": "classification" + }' + ``` + + ```python OpenAI Python + from openai import OpenAI + from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + + portkey_client = OpenAI( + api_key='NOT_REQUIRED', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY" + ) + ) + + embeddings = portkey_client.embeddings.create( + model="textembedding-gecko@003", + input_type="classification", + input="The food was delicious and the waiter...", + # input=["text to embed", "more text to embed"], # if you would like to embed multiple texts + ) + ``` + + ```js OpenAI NodeJS + import OpenAI from 'openai'; // We're using the v4 SDK + import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + + const portkeyClient = new OpenAI({ + apiKey: 'NOT_REQUIRED', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "vertex-ai", + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "PORTKEY_VIRTUAL_KEY" + }) + }); + + const embedding = await portkeyClient.embeddings.create({ + input: 'Name the tallest buildings in Hawaii', + // input: ['text to embed', 'more text to embed'], // if you would like to embed multiple texts + model: 'textembedding-gecko@003' + }); + + console.log(embedding); + ``` + + + +## Embeddings Images + + + ```python Python + from portkey_ai import Portkey + + client = Portkey( + api_key="YOUR_PORTKEY_API_KEY", # defaults to os.environ.get("PORTKEY_API_KEY") + virtual_key="VIRTUAL_KEY", + ) + + embeddings = client.embeddings.create( + model="multimodalembedding@001", + input=[ + { + "text": "this is the caption of the image", + "image": { + "base64": "UklGRkacAABXRUJQVlA4IDqcAACQggKdASqpAn8B.....", + # "url": "gcs://..." # if you want to use a url + } + } + ] + ) + ``` + + ```javascript NodeJS + import { Portkey } from 'portkey-ai'; + + const portkey = new Portkey({ + apiKey: "YOUR_API_KEY", + virtualKey: "YOUR_VIRTUAL_KEY" + }); + + const embedding = await portkey.embeddings.create({ + input: [ + { + "text": "this is the caption of the image", + "image": { + "base64": "UklGRkacAABXRUJQVlA4IDqcAACQggKdASqpAn8B.....", + // "url": "gcs://..." // if you want to use a url + } + } + ], + model: 'multimodalembedding@001' + }); + + console.log(embedding); + ``` + + ```sh cURL + curl --location 'https://api.portkey.ai/v1/embeddings' \ + --header 'Content-Type: application/json' \ + --header 'x-portkey-api-key: PORTKEY_API_KEY' \ + --header 'x-portkey-virtual-key: PORTKEY_VIRTUAL_KEY' \ + --data-raw '{ + "model": "multimodalembedding@001", + "input": [ + { + "text": "this is the caption of the image", + "image": { + "base64": "UklGRkacAABXRUJQVlA4IDqcAACQggKdASqpAn8B....." + # "url": "gcs://..." # if you want to use a url + } + } + ] + }' + ``` + + ```python OpenAI Python + from openai import OpenAI + from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + + portkey_client = OpenAI( + api_key='NOT_REQUIRED', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY" + ) + ) + + embeddings = portkey_client.embeddings.create( + model="multimodalembedding@001", + input=[ + { + "text": "this is the caption of the image", + "image": { + "base64": "UklGRkacAABXRUJQVlA4IDqcAACQggKdASqpAn8B.....", + # "url": "gcs://..." # if you want to use a url + } + } + ] + ) + ``` + + ```js OpenAI NodeJS + import OpenAI from 'openai'; // We're using the v4 SDK + import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + + const portkeyClient = new OpenAI({ + apiKey: 'NOT_REQUIRED', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "PORTKEY_VIRTUAL_KEY" + }) + }); + + const embedding = await portkeyClient.embeddings.create({ + input: [ + { + "text": "this is the caption of the image", + "image": { + "base64": "UklGRkacAABXRUJQVlA4IDqcAACQggKdASqpAn8B.....", + // "url": "gcs://..." // if you want to use a url + } + } + ], + model: 'multimodalembedding@001' + }); + + console.log(embedding); + ``` + + + + +## Embeddings Videos + + + ```python Python + from portkey_ai import Portkey + + client = Portkey( + api_key="YOUR_PORTKEY_API_KEY", # defaults to os.environ.get("PORTKEY_API_KEY") + virtual_key="VIRTUAL_KEY", + ) + + embeddings = client.embeddings.create( + model="multimodalembedding@001", + input=[ + { + "text": "this is the caption of the video", + "video": { + "base64": "UklGRkacAABXRUJQVlA4IDqcAACQggKdASqpAn8B.....", + "start_offset": 0, + "end_offset": 10, + "interval": 5, + # "url": "gcs://..." # if you want to use a url + } + } + ] + ) + ``` + ```javascript NodeJS + import { Portkey } from 'portkey-ai'; + + const portkey = new Portkey({ + apiKey: "YOUR_API_KEY", + virtualKey: "YOUR_VIRTUAL_KEY" + }); + + const embedding = await portkey.embeddings.create({ + input: [ + { + "text": "this is the caption of the video", + "video": { + "base64": "UklGRkacAABXRUJQVlA4IDqcAACQggKdASqpAn8B.....", + "start_offset": 0, + "end_offset": 10, + "interval": 5, + // "url": "gcs://..." // if you want to use a url + } + } + ], + model: 'multimodalembedding@001' + }); + + console.log(embedding); + ``` + + ```sh cURL + curl --location 'https://api.portkey.ai/v1/embeddings' \ + --header 'Content-Type: application/json' \ + --header 'x-portkey-api-key: PORTKEY_API_KEY' \ + --header 'x-portkey-virtual-key: PORTKEY_VIRTUAL_KEY' \ + --data-raw '{ + "model": "multimodalembedding@001", + "input": [ + { + "text": "this is the caption of the video", + "video": { + "base64": "UklGRkacAABXRUJQVlA4IDqcAACQggKdASqpAn8B.....", + "start_offset": 0, + "end_offset": 10, + "interval": 5 + # "url": "gcs://..." # if you want to use a url + } + } + ] + }' + ``` + + ```python OpenAI Python + from openai import OpenAI + from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + + portkey_client = OpenAI( + api_key='NOT_REQUIRED', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY" + ) + ) + + embeddings = portkey_client.embeddings.create( + model="multimodalembedding@001", + input=[ + { + "text": "this is the caption of the video", + "video": { + "base64": "UklGRkacAABXRUJQVlA4IDqcAACQggKdASqpAn8B.....", + "start_offset": 0, + "end_offset": 10, + "interval": 5, + # "url": "gcs://..." # if you want to use a url + } + } + ] + ) + ``` + + ```js OpenAI NodeJS + import OpenAI from 'openai'; // We're using the v4 SDK + import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + + const portkeyClient = new OpenAI({ + apiKey: 'NOT_REQUIRED', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "PORTKEY_VIRTUAL_KEY" + }) + }); + + const embedding = await portkeyClient.embeddings.create({ + input: [ + { + "text": "this is the caption of the video", + "video": { + "base64": "UklGRkacAABXRUJQVlA4IDqcAACQggKdASqpAn8B.....", + "start_offset": 0, + "end_offset": 10, + "interval": 5, + // "url": "gcs://..." // if you want to use a url + } + } + ], + model: 'multimodalembedding@001' + }); + + console.log(embedding); + ``` + + diff --git a/virtual_key_old/integrations/llms/vertex-ai/files.mdx b/virtual_key_old/integrations/llms/vertex-ai/files.mdx new file mode 100644 index 00000000..d2e18b87 --- /dev/null +++ b/virtual_key_old/integrations/llms/vertex-ai/files.mdx @@ -0,0 +1,307 @@ +--- +title: Files +description: Upload files to Google Cloud Storage for Vertex AI fine-tuning and batch inference +--- + +To perform fine-tuning or batch inference with Vertex AI, you need to upload files to Google Cloud Storage. +With Portkey, you can easily upload files to GCS and use them for fine-tuning or batch inference with Vertex AI models. + +## Uploading Files + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VERTEX_VIRTUAL_KEY", # Add your Vertex virtual key + vertex_storage_bucket_name="your_bucket_name", # Specify the GCS bucket name + provider_file_name="your_file_name.jsonl", # Specify the file name in GCS + provider_model="gemini-1.5-flash-001" # Specify the model to use +) + +upload_file_response = portkey.files.create( + purpose="fine-tune", # Can be "fine-tune" or "batch" + file=open("dataset.jsonl", "rb") +) + +print(upload_file_response) +``` + + +```js +import { Portkey } from 'portkey-ai'; +import * as fs from 'fs'; + +// Initialize the Portkey client +const portkey = Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VERTEX_VIRTUAL_KEY", // Add your Vertex virtual key + vertexStorageBucketName: "your_bucket_name", // Specify the GCS bucket name + providerFileName: "your_file_name.jsonl", // Specify the file name in GCS + providerModel: "gemini-1.5-flash-001" // Specify the model to use +}); + +const uploadFile = async () => { + const file = await portkey.files.create({ + purpose: "fine-tune", // Can be "fine-tune" or "batch" + file: fs.createReadStream("dataset.jsonl") + }); + + console.log(file); +} + +uploadFile(); +``` + + +```sh +curl -X POST --header 'x-portkey-api-key: ' \ + --header 'x-portkey-virtual-key: ' \ + --header 'x-portkey-vertex-storage-bucket-name: ' \ + --header 'x-portkey-provider-file-name: .jsonl' \ + --header 'x-portkey-provider-model: ' \ + --form 'purpose="fine-tune"' \ + --form 'file=@dataset.jsonl' \ + 'https://api.portkey.ai/v1/files' +``` + + +```js +import OpenAI from 'openai'; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'; +import * as fs from 'fs'; + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + virtualKey: "VERTEX_VIRTUAL_KEY", + apiKey: "PORTKEY_API_KEY", + vertexStorageBucketName: "your_bucket_name", + providerFileName: "your_file_name.jsonl", + providerModel: "gemini-1.5-flash-001" + }) +}); + +const uploadFile = async () => { + const file = await openai.files.create({ + purpose: "fine-tune", // Can be "fine-tune" or "batch" + file: fs.createReadStream("dataset.jsonl") + }); + + console.log(file); +} + +uploadFile(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + virtual_key="VERTEX_VIRTUAL_KEY", + api_key="PORTKEY_API_KEY", + vertex_storage_bucket_name="your_bucket_name", + provider_file_name="your_file_name.jsonl", + provider_model="gemini-1.5-flash-001" + ) +) + +upload_file_response = openai.files.create( + purpose="fine-tune", # Can be "fine-tune" or "batch" + file=open("dataset.jsonl", "rb") +) + +print(upload_file_response) +``` + + + + + +## Get File + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VERTEX_VIRTUAL_KEY" # Add your Vertex virtual key +) + +file = portkey.files.retrieve(file_id="file_id") + +print(file) +``` + + +```js +import { Portkey } from 'portkey-ai'; + +// Initialize the Portkey client +const portkey = Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VERTEX_VIRTUAL_KEY" // Add your Vertex virtual key +}); + +const getFile = async () => { + const file = await portkey.files.retrieve("file_id"); + + console.log(file); +} + +getFile(); +``` + + +```sh +curl -X GET --header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' \ +'https://api.portkey.ai/v1/files/' +``` + + +```js +import OpenAI from 'openai'; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'; + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + virtualKey: "VERTEX_VIRTUAL_KEY", + apiKey: "PORTKEY_API_KEY" + }) +}); + +const getFile = async () => { + const file = await openai.files.retrieve("file_id"); + + console.log(file); +} + +getFile(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + virtual_key="VERTEX_VIRTUAL_KEY", + api_key="PORTKEY_API_KEY" + ) +) + +file = openai.files.retrieve(file_id="file_id") + +print(file) +``` + + + + + +## Get File Content + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VERTEX_VIRTUAL_KEY" # Add your Vertex virtual key +) + +file_content = portkey.files.content(file_id="file_id") + +print(file_content) +``` + + +```js +import { Portkey } from 'portkey-ai'; + +// Initialize the Portkey client +const portkey = Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VERTEX_VIRTUAL_KEY" // Add your Vertex virtual key +}); + +const getFileContent = async () => { + const fileContent = await portkey.files.content("file_id"); + + console.log(fileContent); +} + +getFileContent(); +``` + + +```sh +curl -X GET --header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' \ +'https://api.portkey.ai/v1/files//content' +``` + + +```js +import OpenAI from 'openai'; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'; + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + virtualKey: "VERTEX_VIRTUAL_KEY", + apiKey: "PORTKEY_API_KEY" + }) +}); + +const getFileContent = async () => { + const fileContent = await openai.files.content("file_id"); + + console.log(fileContent); +} + +getFileContent(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + virtual_key="VERTEX_VIRTUAL_KEY", + api_key="PORTKEY_API_KEY" + ) +) + +file_content = openai.files.content(file_id="file_id") + +print(file_content) +``` + + + + + +Note: The `ListFiles` endpoint is not supported for Vertex AI. \ No newline at end of file diff --git a/virtual_key_old/integrations/llms/vertex-ai/fine-tuning.mdx b/virtual_key_old/integrations/llms/vertex-ai/fine-tuning.mdx new file mode 100644 index 00000000..2e952f52 --- /dev/null +++ b/virtual_key_old/integrations/llms/vertex-ai/fine-tuning.mdx @@ -0,0 +1,532 @@ +--- +title: "Fine-tune" +description: "Fine-tune your models with Vertex AI" +--- + +### Upload a file + + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VERTEX_VIRTUAL_KEY", # Add your Vertex virtual key + vertex_storage_bucket_name="your_bucket_name", # Specify the GCS bucket name + provider_file_name="your_file_name.jsonl", # Specify the file name in GCS + provider_model="gemini-1.5-flash-001" # Specify the model to fine-tune +) + +# Upload a file for fine-tuning +file = portkey.files.create( + file=open("dataset.jsonl", "rb"), + purpose="fine-tune" +) + +print(file) +``` + + +```typescript +import { Portkey } from "portkey-ai"; +import * as fs from 'fs'; + +// Initialize the Portkey client +const portkey = Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VERTEX_VIRTUAL_KEY", // Add your Vertex virtual key + vertexStorageBucketName: "your_bucket_name", // Specify the GCS bucket name + providerFileName: "your_file_name.jsonl", // Specify the file name in GCS + providerModel: "gemini-1.5-flash-001" // Specify the model to fine-tune +}); + +(async () => { + // Upload a file for fine-tuning + const file = await portkey.files.create({ + file: fs.createReadStream("dataset.jsonl"), + purpose: "fine-tune" + }); + + console.log(file); +})(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + virtual_key="VERTEX_VIRTUAL_KEY", + api_key="PORTKEY_API_KEY", + vertex_storage_bucket_name="your_bucket_name", + provider_file_name="your_file_name.jsonl", + provider_model="gemini-1.5-flash-001" + ) +) + +# Upload a file for fine-tuning +file = openai.files.create( + file=open("dataset.jsonl", "rb"), + purpose="fine-tune" +) + +print(file) +``` + + +```typescript +import OpenAI from 'openai'; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'; +import * as fs from 'fs'; + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + virtualKey: "VERTEX_VIRTUAL_KEY", + apiKey: "PORTKEY_API_KEY", + vertexStorageBucketName: "your_bucket_name", + providerFileName: "your_file_name.jsonl", + providerModel: "gemini-1.5-flash-001" + }) +}); + +(async () => { + // Upload a file for fine-tuning + const file = await openai.files.create({ + file: fs.createReadStream("dataset.jsonl"), + purpose: "fine-tune" + }); + + console.log(file); +})(); +``` + + +```sh +curl -X POST --header 'x-portkey-api-key: ' \ + --header 'x-portkey-virtual-key: ' \ + --header 'x-portkey-vertex-storage-bucket-name: ' \ + --header 'x-portkey-provider-file-name: .jsonl' \ + --header 'x-portkey-provider-model: ' \ + --form 'purpose="fine-tune"' \ + --form 'file=@dataset.jsonl' \ + 'https://api.portkey.ai/v1/files' +``` + + + +### Create a fine-tuning job + + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VERTEX_VIRTUAL_KEY" # Add your Vertex virtual key +) + +# Create a fine-tuning job +fine_tune_job = portkey.fine_tuning.jobs.create( + model="gemini-1.5-pro-002", # Base model to fine-tune + training_file="", # Encoded GCS path to the training file + suffix="finetune_name", # Custom suffix for the fine-tuned model name + hyperparameters={ + "n_epochs": 2 + } +) + +print(fine_tune_job) +``` + + +```typescript +import { Portkey } from "portkey-ai"; + +// Initialize the Portkey client +const portkey = Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VERTEX_VIRTUAL_KEY" // Add your Vertex virtual key +}); + +(async () => { + // Create a fine-tuning job + const fineTuneJob = await portkey.fineTuning.jobs.create({ + model: "gemini-1.5-pro-002", // Base model to fine-tune + training_file: "", // Encoded GCS path to the training file + suffix: "finetune_name", // Custom suffix for the fine-tuned model name + hyperparameters: { + n_epochs: 2 + } + }); + + console.log(fineTuneJob); +})(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + virtual_key="VERTEX_VIRTUAL_KEY", + api_key="PORTKEY_API_KEY" + ) +) + +# Create a fine-tuning job +fine_tune_job = openai.fine_tuning.jobs.create( + model="gemini-1.5-pro-002", # Base model to fine-tune + training_file="", # Encoded GCS path to the training file + suffix="finetune_name", # Custom suffix for the fine-tuned model name + hyperparameters={ + "n_epochs": 2 + } +) + +print(fine_tune_job) +``` + + +```typescript +import OpenAI from 'openai'; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'; + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + virtualKey: "VERTEX_VIRTUAL_KEY", + apiKey: "PORTKEY_API_KEY" + }) +}); + +(async () => { + // Create a fine-tuning job + const fineTuneJob = await openai.fineTuning.jobs.create({ + model: "gemini-1.5-pro-002", // Base model to fine-tune + training_file: "", // Encoded GCS path to the training file + suffix: "finetune_name", // Custom suffix for the fine-tuned model name + hyperparameters: { + n_epochs: 2 + } + }); + + console.log(fineTuneJob); +})(); +``` + + +```sh +curl -X POST --header 'Content-Type: application/json' \ + --header 'x-portkey-api-key: ' \ + --header 'x-portkey-virtual-key: ' \ + --data \ + $'{"model": "", "suffix": "", "training_file": "gs:///.jsonl", "hyperparameters": {"n_epochs": 2}}\n' \ +'https://api.portkey.ai/v1/fine_tuning/jobs' +``` + + + +### List fine-tuning jobs + + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VERTEX_VIRTUAL_KEY" # Add your Vertex virtual key +) + +# List all fine-tuning jobs +jobs = portkey.fine_tuning.jobs.list( + limit=10 # Optional: Number of jobs to retrieve (default: 20) +) + +print(jobs) +``` + + +```typescript +import { Portkey } from "portkey-ai"; + +// Initialize the Portkey client +const portkey = Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VERTEX_VIRTUAL_KEY" // Add your Vertex virtual key +}); + +(async () => { + // List all fine-tuning jobs + const jobs = await portkey.fineTuning.jobs.list({ + limit: 10 // Optional: Number of jobs to retrieve (default: 20) + }); + + console.log(jobs); +})(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + virtual_key="VERTEX_VIRTUAL_KEY", + api_key="PORTKEY_API_KEY" + ) +) + +# List all fine-tuning jobs +jobs = openai.fine_tuning.jobs.list( + limit=10 # Optional: Number of jobs to retrieve (default: 20) +) + +print(jobs) +``` + + +```typescript +import OpenAI from 'openai'; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'; + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + virtualKey: "VERTEX_VIRTUAL_KEY", + apiKey: "PORTKEY_API_KEY" + }) +}); + +(async () => { + // List all fine-tuning jobs + const jobs = await openai.fineTuning.jobs.list({ + limit: 10 // Optional: Number of jobs to retrieve (default: 20) + }); + + console.log(jobs); +})(); +``` + + +```sh +curl -X GET --header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' \ +'https://api.portkey.ai/v1/fine_tuning/jobs' +``` + + + +### Get a fine-tuning job + + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VERTEX_VIRTUAL_KEY" # Add your Vertex virtual key +) + +# Retrieve a specific fine-tuning job +job = portkey.fine_tuning.jobs.retrieve( + "job_id" # The ID of the fine-tuning job to retrieve +) + +print(job) +``` + + +```typescript +import { Portkey } from "portkey-ai"; + +// Initialize the Portkey client +const portkey = Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VERTEX_VIRTUAL_KEY" // Add your Vertex virtual key +}); + +(async () => { + // Retrieve a specific fine-tuning job + const job = await portkey.fineTuning.jobs.retrieve( + "job_id" // The ID of the fine-tuning job to retrieve + ); + + console.log(job); +})(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + virtual_key="VERTEX_VIRTUAL_KEY", + api_key="PORTKEY_API_KEY" + ) +) + +# Retrieve a specific fine-tuning job +job = openai.fine_tuning.jobs.retrieve( + "job_id" // The ID of the fine-tuning job to retrieve +) + +print(job) +``` + + +```typescript +import OpenAI from 'openai'; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'; + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + virtualKey: "VERTEX_VIRTUAL_KEY", + apiKey: "PORTKEY_API_KEY" + }) +}); + +(async () => { + // Retrieve a specific fine-tuning job + const job = await openai.fineTuning.jobs.retrieve( + "job_id" // The ID of the fine-tuning job to retrieve + ); + + console.log(job); +})(); +``` + + +```sh +curl -X GET --header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' \ +'https://api.portkey.ai/v1/fine_tuning/jobs/' +``` + + + +### Cancel a fine-tuning job + + + +```python +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VERTEX_VIRTUAL_KEY" # Add your Vertex virtual key +) + +# Cancel a fine-tuning job +cancelled_job = portkey.fine_tuning.jobs.cancel( + "job_id" # The ID of the fine-tuning job to cancel +) + +print(cancelled_job) +``` + + +```typescript +import { Portkey } from "portkey-ai"; + +// Initialize the Portkey client +const portkey = Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VERTEX_VIRTUAL_KEY" // Add your Vertex virtual key +}); + +(async () => { + // Cancel a fine-tuning job + const cancelledJob = await portkey.fineTuning.jobs.cancel( + "job_id" // The ID of the fine-tuning job to cancel + ); + + console.log(cancelledJob); +})(); +``` + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + virtual_key="VERTEX_VIRTUAL_KEY", + api_key="PORTKEY_API_KEY" + ) +) + +# Cancel a fine-tuning job +cancelled_job = openai.fine_tuning.jobs.cancel( + "job_id" // The ID of the fine-tuning job to cancel +) + +print(cancelled_job) +``` + + +```typescript +import OpenAI from 'openai'; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'; + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + virtualKey: "VERTEX_VIRTUAL_KEY", + apiKey: "PORTKEY_API_KEY" + }) +}); + +(async () => { + // Cancel a fine-tuning job + const cancelledJob = await openai.fineTuning.jobs.cancel( + "job_id" // The ID of the fine-tuning job to cancel + ); + + console.log(cancelledJob); +})(); +``` + + +```sh +curl -X POST --header 'x-portkey-api-key: ' \ +--header 'x-portkey-virtual-key: ' \ +'https://api.portkey.ai/v1/fine_tuning/jobs//cancel' +``` + + + +Refer to [Google Vertex AI's fine-tuning documentation](https://cloud.google.com/vertex-ai/docs/generative-ai/models/tune-models) for more information on the parameters and options available. diff --git a/virtual_key_old/integrations/llms/vllm.mdx b/virtual_key_old/integrations/llms/vllm.mdx new file mode 100644 index 00000000..0481191c --- /dev/null +++ b/virtual_key_old/integrations/llms/vllm.mdx @@ -0,0 +1,171 @@ +--- +title: "vLLM" +description: "Integrate vLLM-hosted custom models with Portkey and take them to production" +--- + +Portkey provides a robust and secure platform to observe, govern, and manage your **locally** or **privately** hosted custom models using vLLM. + + +Here's a [list](https://docs.vllm.ai/en/latest/models/supported_models.html) of all model architectures supported on vLLM. + + +## Integrating Custom Models with Portkey SDK + + +Expose your vLLM server by using a tunneling service like [ngrok](https://ngrok.com/) or any other way you prefer. You can skip this step if youโ€™re self-hosting the Gateway. + +```sh +ngrok http 11434 --host-header="localhost:8080" +``` + + + + + + + ```sh + npm install --save portkey-ai + ``` + + + ```sh + pip install portkey-ai + ``` + + + + + + +1. Pass your publicly-exposed vLLM server URL to Portkey with `customHost` (by default, vLLM is on `http://localhost:8000/v1`) +2. Set target `provider` as `openai` since the server follows OpenAI API schema. + + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + provider: "openai", + customHost: "https://7cc4-3-235-157-146.ngrok-free.app" // Your vLLM ngrok URL + Authorization: "AUTH_KEY", // If you need to pass auth + }) + ``` + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", + provider="openai", + custom_host="https://7cc4-3-235-157-146.ngrok-free.app" # Your vLLM ngrok URL + Authorization="AUTH_KEY", # If you need to pass auth + ) + ``` + + + + +More on `custom_host` [here](/product/ai-gateway/universal-api#integrating-local-or-private-models). + + + +Use the Portkey SDK to invoke chat completions from your model, just as you would with any other provider: + + + + ```js + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }] + }); + + console.log(chatCompletion.choices); + ``` + + +```python +completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }] +) + +print(completion) +``` + + + + + +## [Using Virtual Keys](https://app.portkey.ai/virtual-keys) + +Virtual Keys serve as Portkey's unified authentication system for all LLM interactions, simplifying the use of multiple providers and Portkey features within your application. For self-hosted LLMs, you can configure custom authentication requirements including authorization keys, bearer tokens, or any other headers needed to access your model: + + + + + +1. Navigate to [Virtual Keys](https://app.portkey.ai/virtual-keys) in your Portkey dashboard +2. Click **"Add Key"** and enable the **"Local/Privately hosted provider"** toggle +3. Configure your deployment: + - Select the matching provider API specification (typically `OpenAI`) + - Enter your model's base URL in the `Custom Host` field + - Add required authentication headers and their values +4. Click **"Create"** to generate your virtual key + +You can now use this virtual key in your requests: + + + + ```js + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "YOUR_SELF_HOSTED_LLM_VIRTUAL_KEY" + + async function main() { + const response = await client.chat.completions.create({ + messages: [{ role: "user", content: "Bob the builder.." }], + model: "your-self-hosted-model-name", + }); + + console.log(response.choices[0].message.content); + }) + ``` + + + ```python + portkey = Portkey( + api_key="PORTKEY_API_KEY", + virtual_key="YOUR_SELF_HOSTED_LLM_VIRTUAL_KEY" + ) + + response = portkey.chat.completions.create( + model="your-self-hosted-model-name", + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "Hello!"} + ] + + print(response) + ) + ``` + + + +For more information about managing self-hosted LLMs with Portkey, see [Bring Your Own LLM](/integrations/llms/byollm). + + + +## Next Steps + +Explore the complete list of features supported in the SDK: + + +--- + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your requests](/product/ai-gateway/universal-api#ollama-in-configs) +3. [Tracing requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to your local LLM](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/voyage-ai.mdx b/virtual_key_old/integrations/llms/voyage-ai.mdx new file mode 100644 index 00000000..ac5c32e9 --- /dev/null +++ b/virtual_key_old/integrations/llms/voyage-ai.mdx @@ -0,0 +1,143 @@ +--- +title: "Voyage AI" +description: "Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including Voyage AI's embedding and Re-rank endpoints. +" +--- + +With Portkey, you can take advantage of features like fast AI gateway access, observability, prompt management, and more, all while ensuring the secure management of your LLM API keys through a [virtual key](/product/ai-gateway/virtual-keys) system. + + +Provider Slug: **voyage** + + +## Portkey SDK Integration with Voyage + +Portkey provides a consistent API to interact with models from Voyage. To integrate Voyage with Portkey: + +### 1. Install the Portkey SDK + +Add the Portkey SDK to your application to interact with Voyage AI's models through Portkey's gateway. + + + + ```sh + npm install --save portkey-ai + ``` + + + ```sh + pip install portkey-ai + ``` + + + +### 2. Initialize Portkey with the API Key + +To use Voyage with Portkey, [get your API key from here](https://dash.voyageai.com/), then add it to Portkey to create the virtual key. + + + + ```javascript + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + Authorization: "VOYAGE_API_KEY", // Replace with your Voyage API key + provider: "voyage" + }) + ``` + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + Authorization="VOYAGE_API_KEY", # Replace with your Voyage API key + provider="voyage" + ) + ``` + + + +### Embeddings + +Embedding endpoints are natively supported within Portkey like this: + + + + ```javascript + const embedding = await portkey.embeddings.create({ + input: 'Name the tallest buildings in Hawaii', + model: 'voyage-3' + }); + + console.log(embedding); + ``` + + + ```python + embedding = portkey.embeddings.create( + input= 'Name the tallest buildings in Hawaii', + model= 'voyage-3' + ) + + print(embedding) + ``` + + + +### Re-ranking + +You can use Voyage reranking the `portkey.post` method with the body expected by Voyage + + + + ```javascript + const response = await portkey.post( + "/rerank", + { + "model": "rerank-2-lite", + "query": "What is the capital of the United States?", + "documents": [ + "Carson City is the capital city of the American state of Nevada.", + "The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean. Its capital is Saipan.", + "Washington, D.C. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district.", + "Capital punishment (the death penalty) has existed in the United States since beforethe United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states." + ] + }) + ``` + + + ```python + response = portkey.post( + "/rerank", + model="rerank-2-lite", + query="What is the capital of the United States?", + documents=[ + "Carson City is the capital city of the American state of Nevada.", + "The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean. Its capital is Saipan.", + "Washington, D.C. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district.", + "Capital punishment (the death penalty) has existed in the United States since beforethe United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states." + ] + ) + + print(response) + ``` + + + +## Next Steps + +The complete list of features supported in the SDK is available on the link below. + + + Explore the SDK documentation + + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your Voyage requests](/product/ai-gateway/configs) +3. [Tracing Voyage requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to Voyage APIs](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/workers-ai.mdx b/virtual_key_old/integrations/llms/workers-ai.mdx new file mode 100644 index 00000000..83a04228 --- /dev/null +++ b/virtual_key_old/integrations/llms/workers-ai.mdx @@ -0,0 +1,230 @@ +--- +title: "Workers AI" +--- + +Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including [Workers AI.](https://developers.cloudflare.com/workers-ai/) + +With Portkey, you can take advantage of features like fast AI gateway access, observability, prompt management, and more, all while ensuring the secure management of your LLM API keys through a [virtual key](/product/ai-gateway/virtual-keys) system. + +Provider Slug. **workers-ai** + +## Portkey SDK Integration with Workers AI Models + +Portkey provides a consistent API to interact with models from various providers. To integrate Workers AI with Portkey: + +### 1\. Install the Portkey SDK + +Add the Portkey SDK to your application to interact with Workers AI's API through Portkey's gateway. + + + ```sh + npm install --save portkey-ai + ``` + + + ```sh + pip install portkey-ai + ``` + + + + + + +### 2\. Initialize Portkey with the Virtual Key + +To use Workers AI with Portkey, [get your API key from here](https://console.groq.com/keys), then add it to Portkey to create the virtual key. + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your Workers AI Virtual Key + }) + ``` + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Groq + ) + ``` + + + + + + + +### **3\. Invoke Chat Completions with** Workers AI + +Use the Portkey instance to send requests to Workers AI. You can also override the virtual key directly in the API call if needed. + + + ```js + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say "this is a test"' }], + model: '@cf/meta/llama-3.2-3b-instruct', + }); + + console.log(chatCompletion.choices); + ``` + + + ```python + completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say "this is a test"' }], + model= '@cf/meta/llama-3.2-3b-instruct' + ) + + print(completion) + ``` + + + + + + + +### **4\. Invoke Image Generation with** Workers AI + +Use the Portkey instance to send requests to Workers AI. You can also override the virtual key directly in the API call if needed. + +Portkey supports the OpenAI signature to make text-to-image requests. + + + +```js +import Portkey from 'portkey-ai'; + +// Initialize the Portkey client +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key +}); + +async function main() { + const image = await portkey.images.generate({ + model: "image_model_name", + prompt: "Lucy in the sky with diamonds" + }); + + console.log(image.data); +} + +main(); +``` + + + +```py +from portkey_ai import Portkey +from IPython.display import display, Image + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Add your provider's virtual key +) + +image = portkey.images.generate( + model="image_model_name", + prompt="Lucy in the sky with diamonds" +) + +# Display the image +display(Image(url=image.data[0].url)) +``` + + + +```js +import OpenAI from 'openai'; // We're using the v4 SDK +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'WORKERS_AI_API_KEY', // defaults to process.env["WORKERS_AI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY" // defaults to process.env["PORTKEY_API_KEY"] + }) +}); + +async function main() { + const image = await openai.images.generate({ + model: "image_model_name", + prompt: "Lucy in the sky with diamonds" + }); + + console.log(image.data); +} + +main(); +``` + + + +```py +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders +from IPython.display import display, Image + +client = OpenAI( + api_key='WORKERS_AI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY" + ) +) + +image = client.images.generate( + model="image_model_name", + prompt="Lucy in the sky with diamonds", + n=1, + size="1024x1024" +) + +# Display the image +display(Image(url=image.data[0].url)) +``` + + + +```sh +curl "https://api.portkey.ai/v1/images/generations" \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-virtual-key: $WORKERS_AI_VIRTUAL_KEY" \ + -d '{ + "model": "image_model_name", + "prompt": "Lucy in the sky with diamonds" + }' +``` + + + + + + +## Managing Workers AI Prompts + +You can manage all prompts to Workers AI in the [Prompt Library](/product/prompt-library). All the current models of Workers AI are supported and you can easily start testing different prompts. + +Once you're ready with your prompt, you can use the `portkey.prompts.completions.create` interface to use the prompt in your application. + +The complete list of features supported in the SDK are available on the link below. + + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your Workers requests](/product/ai-gateway/configs) +3. [Tracing Workers AI requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to Workers AI APIs](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/llms/x-ai.mdx b/virtual_key_old/integrations/llms/x-ai.mdx new file mode 100644 index 00000000..307762b7 --- /dev/null +++ b/virtual_key_old/integrations/llms/x-ai.mdx @@ -0,0 +1,555 @@ +--- +title: 'xAI' +description: Portkey supports xAI's chat completions, completions, and embeddings APIs. +--- + + +[Supported Endpoints](/api-reference/inference-api/supported-providers) + + +## Integrate +Just paste your xAI API Key to [Portkey](https://app.portkey.ai/virtual-keys) to create your Virtual Key. + + +## Sample Request +Portkey is a drop-in replacement for xAI. You can make request using the official Portkey SDK. + +Popular libraries & agent frameworks like LangChain, CrewAI, AutoGen, etc. are [also supported](#popular-libraries). + + + +```ts NodeJS +import Portkey from 'portkey-ai'; + +const client = new Portkey({ + apiKey: 'PORTKEY_API_KEY', + virtualKey: 'PROVIDER_VIRTUAL_KEY' +}); + +async function main() { + const response = await client.chat.completions.create({ + messages: [{ role: "user", content: "Bob the builder.." }], + model: "grok-beta", + }); + + console.log(response.choices[0].message.content); +} + +main(); +``` +```py Python +from portkey_ai import Portkey + +client = Portkey( + api_key = "PORTKEY_API_KEY", + virtual_key = "PROVIDER_VIRTUAL_KEY" +) + +response = client.chat.completions.create( + model="grok-beta", + messages=[{"role": "user", "content": "Hello!"}] +) + +print(response.choices[0].message) +``` +```sh cURL +curl https://api.portkey.ai/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-virtual-key: $PORTKEY_PROVIDER_VIRTUAL_KEY" \ + -d '{ + "model": "grok-beta", + "messages": [ + { "role": "user", "content": "Hello!" } + ] + }' +``` +```py OpenAI Python SDK +from openai import OpenAI +from portkey_ai import createHeaders, PORTKEY_GATEWAY_URL + +client = OpenAI( + api_key="xx", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key="PORTKEY_API_KEY", + virtual_key="OPENAI_VIRTUAL_KEY" + ) +) + +completion = client.chat.completions.create( + model="grok-beta", + messages=[ + {"role": "user", "content": "Hello!"} + ] +) + +print(completion.choices[0].message) +``` +```ts OpenAI NodeJS SDK +import OpenAI from 'openai'; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'xx', + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "OPENAI_VIRTUAL_KEY" + }) +}); + +async function main() { + const completion = await openai.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'grok-beta', + }); + + console.log(chatCompletion.choices); +} + +main(); +``` + + +## Local Setup +If you do not want to use Portkey's hosted API, you can also run Portkey locally: + + +Portkey runs on our popular [open source Gateway](https://git.new/portkey). You can spin it up locally to make requests without sending them to the Portkey API. + +```sh Install the Gateway +npx @portkey-ai/gateway +``` + +```sh Docker Image +npx @portkey-ai/gateway +``` + +| Your Gateway is running on http://localhost:8080/v1 ๐Ÿš€ | | +| - | - | + +Then, just change the `baseURL` to the local Gateway URL, and make requests: + +```ts NodeJS +import Portkey from 'portkey-ai'; + +const client = new Portkey({ + baseUrl: 'http://localhost:8080/v1', + apiKey: 'PORTKEY_API_KEY', + virtualKey: 'PROVIDER_VIRTUAL_KEY' +}); + +async function main() { + const response = await client.chat.completions.create({ + messages: [{ role: "user", content: "Bob the builder.." }], + model: "grok-beta", + }); + + console.log(response.choices[0].message.content); +} + +main(); +``` +```py Python +from portkey_ai import Portkey + +client = Portkey( + base_url = 'http://localhost:8080/v1', + api_key = "PORTKEY_API_KEY", + virtual_key = "PROVIDER_VIRTUAL_KEY" +) + +response = client.chat.completions.create( + model="grok-beta", + messages=[ + {"role": "user", "content": "Hello!"} + ] +) + +print(response.choices[0].message) +``` +```sh cURL +curl http://localhost:8080/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-virtual-key: $PORTKEY_PROVIDER_VIRTUAL_KEY" \ + -d '{ + "model": "grok-beta", + "messages": [ + { "role": "user", "content": "Hello!" } + ] + }' +``` +```py OpenAI Python SDK +from openai import OpenAI +from portkey_ai import createHeaders, PORTKEY_GATEWAY_URL + +client = OpenAI( + api_key="xx", + base_url="https://localhost:8080/v1", + default_headers=createHeaders( + api_key="PORTKEY_API_KEY", + virtual_key="OPENAI_VIRTUAL_KEY" + ) +) + +completion = client.chat.completions.create( + model="grok-beta", + messages=[ + {"role": "user", "content": "Hello!"} + ] +) + +print(completion.choices[0].message) +``` +```ts OpenAI NodeJS SDK +import OpenAI from 'openai'; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'xx', + baseURL: 'https://localhost:8080/v1', + defaultHeaders: createHeaders({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "OPENAI_VIRTUAL_KEY" + }) +}); + +async function main() { + const completion = await openai.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'grok-beta', + }); + + console.log(chatCompletion.choices); +} + +main(); +``` + + + + + Portkey's data & control planes can be fully deployed on-prem with the Enterprise license. + + [More details here](/product/enterprise-offering/private-cloud-deployments) + + + +--- + +## Integration Overview + +### xAI Endpoints & Capabilities + +Portkey works with *all* of xAI's endpoints and supports all xAI capabilities like function calling and image understanding. Find examples for each below: + + + +```javascript Node.js +let tools = [{ + type: "function", + function: { + name: "getWeather", + description: "Get the current weather", + parameters: { + type: "object", + properties: { + location: { type: "string", description: "City and state" }, + unit: { type: "string", enum: ["celsius", "fahrenheit"] } + }, + required: ["location"] + } + } +}]; + +let response = await portkey.chat.completions.create({ + model: "grok-beta", + messages: [ + { role: "system", content: "You are a helpful assistant." }, + { role: "user", content: "What's the weather like in Delhi - respond in JSON" } + ], + tools, + tool_choice: "auto", +}); + +console.log(response.choices[0].finish_reason); +``` + +```python Python +tools = [{ + "type": "function", + "function": { + "name": "getWeather", + "description": "Get the current weather", + "parameters": { + "type": "object", + "properties": { + "location": {"type": "string", "description": "City and state"}, + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]} + }, + "required": ["location"] + } + } +}] + +response = portkey.chat.completions.create( + model="grok-beta", + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "What's the weather like in Delhi - respond in JSON"} + ], + tools=tools, + tool_choice="auto" +) + +print(response.choices[0].finish_reason) +``` + +```curl REST +curl -X POST "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -d '{ + "model": "grok-beta", + "messages": [ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "What'\''s the weather like in Delhi - respond in JSON"} + ], + "tools": [{ + "type": "function", + "function": { + "name": "getWeather", + "description": "Get the current weather", + "parameters": { + "type": "object", + "properties": { + "location": {"type": "string", "description": "City and state"}, + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]} + }, + "required": ["location"] + } + } + }], + "tool_choice": "auto" + }' +``` + + + + + +Process images alongside text using xAI's vision capabilities: + + +```python Python +response = portkey.chat.completions.create( + model="grok-beta", + messages=[ + { + "role": "user", + "content": [ + {"type": "text", "text": "What's in this image?"}, + { + "type": "image_url", + "image_url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg", + }, + ], + } + ], + max_tokens=300, +) + +print(response) +``` + +```javascript Node.js +const response = await portkey.chat.completions.create({ + model: "grok-beta", + messages: [ + { + role: "user", + content: [ + { type: "text", text: "What's in this image?" }, + { + type: "image_url", + image_url: "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg", + }, + ], + }, + ], + max_tokens: 300, +}); + +console.log(response); +``` + +```curl REST +curl -X POST "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer YOUR_PORTKEY_API_KEY" \ + -d '{ + "model": "grok-beta", + "messages": [ + { + "role": "user", + "content": [ + {"type": "text", "text": "What'\''s in this image?"}, + { + "type": "image_url", + "image_url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg" + } + ] + } + ], + "max_tokens": 300 + }' +``` + + + + + + +Generate embeddings for text using xAI's embedding models: + +`Coming Soon!` + + + + +### Portkey Features + + + + +Here's a simplified version of how to use Portkey's Gateway Configuration: + + + + You can create a Gateway configuration using the Portkey Config Dashboard or by writing a JSON configuration in your code. In this example, requests are routed based on the user's subscription plan (paid or free). + + ```json + config = { + "strategy": { + "mode": "conditional", + "conditions": [ + { + "query": { "metadata.user_plan": { "$eq": "paid" } }, + "then": "grok-beta" + }, + { + "query": { "metadata.user_plan": { "$eq": "free" } }, + "then": "grok-2-1212" + } + ], + "default": "grok-beta" + }, + "targets": [ + { + "name": "grok-beta", + "virtual_key": "xx" + }, + { + "name": "grok-2-1212", + "virtual_key": "yy" + } + ] + } + ``` + + + + When a user makes a request, it will pass through Portkey's AI Gateway. Based on the configuration, the Gateway routes the request according to the user's metadata. + Conditional Routing Diagram + + + + Pass the Gateway configuration to your Portkey client. You can either use the config object or the Config ID from Portkey's hosted version. + + + ```python Python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", + virtual_key="VIRTUAL_KEY", + config=portkey_config + ) + ``` + + ```javascript Node.js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "VIRTUAL_KEY", + config: portkeyConfig + }) + ``` + + + + +That's it! Portkey seamlessly allows you to make your AI app more robust using built-in gateway features. Learn more about advanced gateway features: + + + + Distribute requests across multiple targets based on defined weights. + + + Automatically switch to backup targets if the primary target fails. + + + Route requests to different targets based on specified conditions. + + + Enable caching of responses to improve performance and reduce costs. + + + + + + + +Portkey's AI gateway enables you to enforce input/output checks on requests by applying custom hooks before and after processing. Protect your user's/company's data by using PII guardrails and many more available on Portkey Guardrails: + +```json +{ + "virtual_key":"xai-xxx", + "before_request_hooks": [{ + "id": "input-guardrail-id-xx" + }], + "after_request_hooks": [{ + "id": "output-guardrail-id-xx" + }] +} +``` + + + Explore Portkey's guardrail features to enhance the security and reliability of your AI applications. + + + + + + + +--- + +## Appendix + +### FAQs + + + +You can sign up to xAI [here](https://accounts.x.ai/) and grab your API key. + + +xAI typically gives some amount of free credits without you having to add your credit card. Reach out to their support team if you'd like additional free credits. + + +You can find your current rate limits imposed by xAI on the console. Use Portkey's loadbalancer to tackle rate limiting by xAI. + + diff --git a/virtual_key_old/integrations/llms/zhipu.mdx b/virtual_key_old/integrations/llms/zhipu.mdx new file mode 100644 index 00000000..794e3505 --- /dev/null +++ b/virtual_key_old/integrations/llms/zhipu.mdx @@ -0,0 +1,150 @@ +--- +title: "ZhipuAI / ChatGLM / BigModel" +--- + +[ZhipuAI](https://open.bigmodel.cn/) has developed the GLM series of open source LLMs that are some of the world's best performing and capable models today. Portkey provides a robust and secure gateway to seamlessly integrate these LLMs into your applications in the familiar OpenAI spec with just 2 LOC change! + +With Portkey, you can leverage powerful features like fast AI gateway, caching, observability, prompt management, and more, while securely managing your LLM API keys through a virtual key system. + +Provider Slug. `zhipu` + +## Portkey SDK Integration with ZhipuAI + +### 1\. Install the Portkey SDK + +Install the Portkey SDK in your project using npm or pip: + + + + ```sh + npm install --save portkey-ai + ``` + + +```sh +pip install portkey-ai +``` + + + + + + +### 2\. Initialize Portkey with the Virtual Key + +To use ZhipuAI / ChatGLM / BigModel with Portkey, [get your API key from here](https://bigmodel.cn/usercenter/apikeys), then add it to Portkey to create the virtual key. + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your ZhipuAI Virtual Key + }) + ``` + + +```python +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for ZhipuAI +) +``` + + + +```js +import OpenAI from "openai"; +import { PORTKEY_GATEWAY_URL, createHeaders } from "portkey-ai"; + +const portkey = new OpenAI({ + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "ZHIPUAI_VIRTUAL_KEY", + }), +}); +``` + + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +portkey = OpenAI( + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key="PORTKEY_API_KEY", + virtual_key="ZHIPUAI_VIRTUAL_KEY" + ) +) +``` + + + + + +### 3\. Invoke Chat Completions + + + ```js + const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Who are you?' }], + model: 'glm-4' + }); + + console.log(chatCompletion.choices); + ``` + + > I am an AI assistant named ZhiPuQingYan๏ผˆๆ™บ่ฐฑๆธ…่จ€๏ผ‰, you can call me Xiaozhi๐Ÿค– + + + +```python +completion = portkey.chat.completions.create( + messages= [{ "role": 'user', "content": 'Say this is a test' }], + model= 'glm-4' +) + +print(completion) +``` + +> I am an AI assistant named ZhiPuQingYan๏ผˆๆ™บ่ฐฑๆธ…่จ€๏ผ‰, you can call me Xiaozhi๐Ÿค– + + + + ```sh + curl https://api.portkey.ai/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-virtual-key: $ZHIPUAI_VIRTUAL_KEY" \ + -d '{ + "messages": [{"role": "user","content": "Hello!"}], + "model": "glm-4", + }' + ``` + + > I am an AI assistant named ZhiPuQingYan๏ผˆๆ™บ่ฐฑๆธ…่จ€๏ผ‰, you can call me Xiaozhi๐Ÿค– + + + + + +--- + +## Next Steps + +The complete list of features supported in the SDK are available on the link below. + + + +You'll find more information in the relevant sections: + +1. [Add metadata to your requests](/product/observability/metadata) +2. [Add gateway configs to your ZhipuAI requests](/product/ai-gateway/configs) +3. [Tracing ZhipuAI requests](/product/observability/traces) +4. [Setup a fallback from OpenAI to ZhipuAI](/product/ai-gateway/fallbacks) diff --git a/virtual_key_old/integrations/partner.mdx b/virtual_key_old/integrations/partner.mdx new file mode 100644 index 00000000..c2813924 --- /dev/null +++ b/virtual_key_old/integrations/partner.mdx @@ -0,0 +1,4 @@ +--- +title: Submit an Integration +url: https://github.com/Portkey-AI/gateway/blob/main/.github/CONTRIBUTING.md +--- diff --git a/virtual_key_old/integrations/plugins/exa.mdx b/virtual_key_old/integrations/plugins/exa.mdx new file mode 100644 index 00000000..3b1ee934 --- /dev/null +++ b/virtual_key_old/integrations/plugins/exa.mdx @@ -0,0 +1,186 @@ +--- +title: "Exa Online Search" +description: "Transform offline LLMs into online models with real-time internet search capabilities." +--- + +[Exa](https://exa.ai) provides a powerful search API that seamlessly integrates with your LLM applications, enabling real-time internet access for any model. When integrated with Portkey, Exa transforms requests by adding relevant search results before they reach the model, effectively turning offline models into online models. + + + Learn more about Exa and their offerings. + + +## How Exa Online Search Works + +The Exa Online Search plugin automatically adds a context window with relevant search results from the internet before your prompt reaches the LLM: + +1. **User sends a prompt** requiring up-to-date information +2. **Portkey intercepts the request** and sends a search query to Exa +3. **Exa returns relevant search results** from across the web +4. **Search results are added as context** to the original prompt +5. **Enhanced prompt is sent to the LLM** which now has access to current information + +This process allows any LLM to respond with up-to-date knowledge without retraining or fine-tuning. + +## Setting Up Exa Online Search + +### 1. Enable Exa Plugin in Portkey + +* Navigate to `Settings` โ†’ `Plugins` in the sidebar +* Find Exa in the list of available plugins and click "Enable" +* Enter your Exa API key (obtain this from your [Exa dashboard](https://exa.ai)) +* Save your settings + +### 2. Create an Exa Guardrail in Portkey + +* Navigate to the `Guardrails` page and click `Create` +* Search for "Exa Online Search" and click `Add` +* Configure your search parameters: + * **Context Prefix**: Add Text that appears before the search results in the LLM query(default: ``) + * **Context Suffix**: Add Text that appears after the search results in the LLM query (default: ``) + * **Number of Results**: How many search results to include (recommended: 1-5) + * **Include Domains**: Optional list of specific domains to limit search results to + * **Exclude Domains**: Optional list of domains to exclude from search results + * **Timeout**: Maximum wait time for search results in milliseconds (default: 10000) + +* Set any `actions` you want on your check, and create the Guardrail! + + + Guardrail Actions allow you to orchestrate your guardrails logic. You can learn more about them [here](/product/guardrails#there-are-6-types-of-guardrail-actions) + + +* Save your guardrail + + + + + +### 3. Add Guardrail ID to a Config + +* When you save a guardrail, you'll get an associated Guardrail ID +* Add this ID to the `input_guardrails` parameter in your Portkey Config. +* Create these Configs in Portkey UI, save them, and get an associated Config ID to attach to your requests. [More here](/product/ai-gateway/configs). + +```json +{ + "input_guardrails": [ + "your_exa_guardrail_id" + ] +} +``` + +* Save this config and note its Config ID for use in your requests + + + + The Exa Plugin is supported only as an `input guardrail`. It adds web search results to your request before it reaches the LLM, which is why only `before_request_hooks` are supported and not `after_request_hooks` on Portkey's gateway. [Learn more about guardrails here](/product/guardrails) + + +### 4. Use the Config in Your Requests + + + +```js +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + config: "pc-***" // Your config ID with Exa enabled +}); + +// Any request using this client will now have Exa search capabilities +const response = await portkey.chat.completions.create({ + model: "gpt-4", + messages: [ + { role: "user", content: "What are the latest developments in quantum computing?" } + ] +}); +``` + + +```py +portkey = Portkey( + api_key="PORTKEY_API_KEY", + config="pc-***" # Your config ID with Exa enabled +) + +# Any request using this client will now have Exa search capabilities +response = portkey.chat.completions.create( + model="gpt-4", + messages=[ + {"role": "user", "content": "What are the latest developments in quantum computing?"} + ] +) +``` + + +```js +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + apiKey: "PORTKEY_API_KEY", + config: "CONFIG_ID_WITH_EXA" // Your config with Exa enabled + }) +}); + +// Any request using this client will now have Exa search capabilities +const response = await openai.chat.completions.create({ + model: "gpt-4", + messages: [ + { role: "user", content: "What happened in the world today?" } + ] +}); +``` + + + +## Best Practices + +1. **Combine with Output Guardrails**: Pair Exa with output validation guardrails to ensure not just up-to-date information, but also accurate formatting and content safety. + +2. **Manage Token Usage**: Be mindful that adding search context increases token consumption. Monitor usage patterns and adjust the number of search results accordingly. + +3. **Use Domain Filtering**: For specialized applications, use domain filtering to ensure information comes from authoritative sources relevant to your use case. + +4. **Test Thoroughly**: Different models may respond differently to the added context. Test your configuration across various query types and models. + +## Monitoring and Logs + +All Exa-enhanced requests are logged in the Portkey dashboard. You can review: + +* Which requests used Exa search +* How many tokens were used for the search context +* How the search results affected the model's response + + + + + +## Frequently Asked Questions + + + + Yes, the Exa Online Search plugin works with any LLM provider supported by Portkey, including OpenAI, Anthropic, Cohere, and more. The plugin adds context before your prompt reaches the model, so it's model-agnostic. + + + + Adding search results as context will increase token usage since the prompt becomes longer. The exact increase depends on the number of search results and their length. You can control this by adjusting the "Number of Results" parameter. + + + + + Exa provides real-time search results from across the web, with freshness depending on how quickly content is indexed. For most major news and information sources, content is available within minutes to hours of publication. + + + + Unlike OpenAI's browsing capability which is limited to specific models and the OpenAI ecosystem, Exa Online Search works with any LLM provider and model available through Portkey, giving you more flexibility and control. + + + +## Get Support + +If you face any issues with the Exa integration, reach out to the Portkey team on the [community forum](https://discord.gg/portkey-llms-in-prod-1143393887742861333). + +## Learn More + +- [Exa API Documentation](https://exa.ai/docs) +- [Portkey Guardrails Overview](/product/guardrails) +``` diff --git a/virtual_key_old/integrations/tracing-providers.mdx b/virtual_key_old/integrations/tracing-providers.mdx new file mode 100644 index 00000000..abfcfbed --- /dev/null +++ b/virtual_key_old/integrations/tracing-providers.mdx @@ -0,0 +1,9 @@ +--- +title: "Overview" +--- + + + + + + diff --git a/virtual_key_old/integrations/tracing-providers/arize.mdx b/virtual_key_old/integrations/tracing-providers/arize.mdx new file mode 100644 index 00000000..61c6d7e2 --- /dev/null +++ b/virtual_key_old/integrations/tracing-providers/arize.mdx @@ -0,0 +1,363 @@ +--- +title: 'Arize Phoenix' +description: 'Extend Portkeyโ€™s powerful AI Gateway with Arize Phoenix for unified LLM observability, tracing, and analytics across your ML stack.' +--- + +Portkey is a **production-grade AI Gateway and Observability platform** for AI applications. It offers built-in observability, reliability features and over 40+ key LLM metrics. For teams standardizing observability in Arize Phoenix, Portkey also supports seamless integration. + + +Portkey provides comprehensive observability out-of-the-box. This integration is for teams who want to consolidate their ML observability in Arize Phoenix alongside Portkey's AI Gateway capabilities. + + +## Why Portkey + Arize Phoenix? + +[Arize Phoenix](https://github.com/Arize-ai/openinference/tree/main/python/instrumentation/openinference-instrumentation-portkey) brings observability to LLM workflows with tracing, prompt debugging, and performance monitoring. + +Thanks to Phoenix's OpenInference instrumentation, Portkey can emit structured traces automatically โ€” no extra setup needed. This gives you clear visibility into every LLM call, making it easier to debug and improve your app. + + + + +- **1600+ LLM Providers**: Single API for OpenAI, Anthropic, AWS Bedrock, and more +- **Advanced Routing**: Fallbacks, load balancing, conditional routing +- **Cost Optimization**: Semantic caching, request +- **Security**: PII detection, content filtering, compliance controls + + + +- **40+ Key Metrics**: Cost, latency, tokens, error rates +- **Detailed Logs & Traces**: Request/response bodies and custom tracing +- **Custom Metadata**: Attach custom metadata to your requests +- **Custom Alerts**: Real-time monitoring and notifications + + + +With this integration, you can route LLM traffic through Portkey and gain deep observability in Arize Phoenixโ€”bringing together the best of gateway orchestration and ML observability. + + +## Getting Started + +### Installation + +Install the required packages to enable Arize Phoenix integration with your Portkey deployment: + + + +```bash arize +pip install portkey-ai openinference-instrumentation-portkey arize-otel +``` + +```bash phoenix +pip install portkey-ai openinference-instrumentation-portkey arize-phoenix-otel +``` + + +### Setting up the Integration + + + +First, set up the Arize OpenTelemetry configuration: + + +```python arize +from arize.otel import register + +# Configure Arize as your telemetry backend +tracer_provider = register( + space_id="your-space-id", # Found in Arize app settings + api_key="your-api-key", # Your Arize API key + project_name="portkey-gateway" # Name your project +) +``` + +```python phoenix +from phoenix.otel import register + +# Configure Arize as your telemetry backend +tracer_provider = register( + space_id="your-space-id", # Found in Arize app settings + api_key="your-api-key", # Your Arize API key + project_name="portkey-gateway" # Name your project +) +``` + + + + + + +Initialize the Portkey instrumentor to format traces for Arize: + +```python +from openinference.instrumentation.portkey import PortkeyInstrumentor + +# Enable instrumentation +PortkeyInstrumentor().instrument(tracer_provider=tracer_provider) +``` + + + +Set up Portkey with all its powerful features: + +```python +from portkey_ai import Portkey + +# Initialize Portkey client +portkey = Portkey( + api_key="your-portkey-api-key", # Optional for self-hosted + virtual_key="your-openai-virtual-key" # Or use provider-specific virtual keys +) + +response = portkey.chat.completions.create( + model="gpt-4o-mini", + messages=[{"role": "user", "content": "Explain machine learning"}] +) + +print(response.choices[0].message.content) +``` + + + +## Complete Integration Example + +Here's a complete working example that connects Portkey's AI Gateway with Arize Phoenix for centralized monitoring: + + +```python [expandable] +import os +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders +from arize.otel import register # OR from phoenix.otel import register + +# configure the Phoenix tracer +from openinference.instrumentation.portkey import PortkeyInstrumentor + +# Step 1: Configure Arize Phoenix +tracer_provider = register( + space_id="your-space-id", + api_key="your-arize-api-key", + project_name="portkey-production" +) + +# Step 2: Enable Portkey instrumentation +PortkeyInstrumentor().instrument(tracer_provider=tracer_provider) + +# Step 3: Configure Portkey's Advanced AI Gateway +advanced_config = { + "strategy": { + "mode": "loadbalance" # Distribute load across providers + }, + "targets": [ + { + "virtual_key": "openai-vk", + "weight": 0.7, + "override_params": {"model": "gpt-4o"} + }, + { + "virtual_key": "anthropic-vk", + "weight": 0.3, + "override_params": {"model": "claude-3-opus-20240229"} + } + ], + "cache": { + "mode": "semantic", # Intelligent caching + "max_age": 3600 + }, + "retry": { + "attempts": 3, + "on_status_codes": [429, 500, 502, 503, 504] + }, + "request_timeout": 30000 +} + +# Initialize Portkey-powered client +client = OpenAI( + api_key="not-needed", # Virtual keys handle auth + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key=os.environ.get("PORTKEY_API_KEY"), + config=advanced_config, + metadata={ + "user_id": "user-123", + "session_id": "session-456", + "feature": "chat-assistant" + } + ) +) + +# Make requests through Portkey's AI Gateway +response = client.chat.completions.create( + model="gpt-4o", # Portkey handles provider routing + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "Explain quantum computing in simple terms."} + ], + temperature=0.7 +) + +print(response.choices[0].message.content) +``` + +## Portkey AI Gateway Features + +While Arize Phoenix provides observability, Portkey delivers a complete AI infrastructure platform. Here's everything you get with Portkey: + +### ๐Ÿš€ Core Gateway Capabilities + + + +Access OpenAI, Anthropic, Google, Cohere, Mistral, Llama, and 1600+ models through a single unified API. No more managing different SDKs or endpoints. + + + +Use the same code to call any LLM provider. Switch between models and providers without changing your application code. + + + +Secure vault for API keys with budget limits, rate limiting, and access controls. Never expose raw API keys in your code. + + + +Define routing strategies, model parameters, and reliability settings in reusable configurations. Version control your AI infrastructure. + + + +### ๐Ÿ›ก๏ธ Reliability & Performance + + + +Automatically switch to backup providers when primary fails. Define fallback chains across multiple providers. + + + +Distribute requests across multiple API keys or providers based on custom weights and strategies. + + + +Configurable retry logic with exponential backoff for transient failures and rate limits. + + + +Set custom timeouts to prevent hanging requests and improve application responsiveness. + + + +Route requests to different models based on content, metadata, or custom conditions. + + + +Gradually roll out new models or providers with percentage-based traffic splitting. + + + +### ๐Ÿ’ฐ Cost Optimization + + + +Intelligent caching that understands semantic similarity. Reduce costs by up to 90% on repeated queries. + + + +Set spending limits per API key, team, or project. Get alerts before hitting limits. + + + +Set spending limits per API key, team, or project. Get alerts before hitting limits. + + + +Real-time cost tracking across all providers with detailed breakdowns by model, user, and feature. + + + +### ๐Ÿ“Š Built-in Observability + + + +Track 40+ metrics including latency, tokens, costs, cache hits, error rates, and more in real-time. + + + +Full request/response logging with advanced filtering, search, and export capabilities. + + + +Trace requests across your entire AI pipeline with correlation IDs and custom metadata. + + + +Set up alerts on any metric with webhook, email, or Slack notifications. + + + +### ๐Ÿ”’ Security & Compliance + + + +Automatically detect and redact sensitive information like SSN, credit cards, and personal data. + + + +Block harmful, toxic, or inappropriate content in real-time based on custom policies. + + + +Fine-grained RBAC with team management, user permissions, and audit logs. + + + +Enterprise-grade security with SOC2 Type II certification and GDPR compliance. + + + +Complete audit trail of all API usage, configuration changes, and user actions. + + + +Zero data retention options and deployment in your own VPC for maximum privacy. + + + +### ๐Ÿข Enterprise Features + + + +SAML 2.0 support for Okta, Azure AD, Google Workspace, and custom IdPs. + + + +Multi-workspace support with hierarchical teams and department-level controls. + + + +99.9% uptime SLA with dedicated support and custom deployment options. + + + +Deploy Portkey in your own AWS, Azure, or GCP environment with full control. + + + + +## Next Steps + + + +Discover all AI Gateway capabilities beyond observability + + + +Secure your API keys and set budgets + + + +Configure fallbacks, load balancing, and more + + + +Use Portkey's native observability features + + + + +**Need help?** Join our [Discord community](https://portkey.sh/discord) diff --git a/virtual_key_old/integrations/tracing-providers/langfuse.mdx b/virtual_key_old/integrations/tracing-providers/langfuse.mdx new file mode 100644 index 00000000..cadf471e --- /dev/null +++ b/virtual_key_old/integrations/tracing-providers/langfuse.mdx @@ -0,0 +1,302 @@ +--- +title: "Langfuse" +description: "Integrate Langfuse observability with Portkey's AI gateway for comprehensive LLM monitoring and advanced routing capabilities" +--- + +Langfuse is an open-source LLM observability platform that helps you monitor, debug, and analyze your LLM applications. When combined with Portkey, you get the best of both worlds: Langfuse's detailed observability and Portkey's advanced AI gateway features. + +This integration allows you to: +- Track all LLM requests in Langfuse while routing through Portkey +- Use Portkey's 250+ LLM providers with Langfuse observability +- Implement advanced features like caching, fallbacks, and load balancing +- Maintain detailed traces and analytics in both platforms + +## Quick Start Integration + +Since Portkey provides an OpenAI-compatible API, integrating with Langfuse is straightforward using Langfuse's OpenAI wrapper. + +### Installation + +```bash +pip install portkey-ai langfuse openai +``` + +### Basic Setup + +```python +import os +from portkey_ai import createHeaders, PORTKEY_GATEWAY_URL + +# Set your Langfuse credentials +os.environ["LANGFUSE_PUBLIC_KEY"] = "YOUR_LANGFUSE_PUBLIC_KEY" +os.environ["LANGFUSE_SECRET_KEY"] = "YOUR_LANGFUSE_SECRET_KEY" + +# Import OpenAI from langfuse +from langfuse.openai import OpenAI + +# Initialize the client +client = OpenAI( + api_key="YOUR_OPENAI_API_KEY", # Your LLM provider API key + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_VIRTUAL_KEY", # Optional: Use virtual keys + # config="YOUR_CONFIG_ID", # Optional: Use saved configs + # trace_id="YOUR_TRACE_ID", # Optional: Custom trace ID + ) +) + +# Make a request +response = client.chat.completions.create( + model="gpt-4o-mini", + messages=[{"role": "user", "content": "Hello, world!"}], +) + +print(response.choices[0].message.content) +``` + + +This integration automatically logs requests to both Langfuse and Portkey, giving you observability data in both platforms. + + +## Using Portkey Features with Langfuse + +### 1. Virtual Keys + +Virtual Keys in Portkey allow you to securely manage API keys and set usage limits. Use them with Langfuse for better security: + +```python +from langfuse.openai import OpenAI +from portkey_ai import createHeaders, PORTKEY_GATEWAY_URL + +client = OpenAI( + api_key="dummy_key", # Not used when virtual key is provided + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_VIRTUAL_KEY" + ) +) + +response = client.chat.completions.create( + model="gpt-4o", + messages=[{"role": "user", "content": "Explain quantum computing"}] +) +``` + +### 2. Multiple Providers + +Switch between 250+ LLM providers while maintaining Langfuse observability: + + + +```python +client = OpenAI( + api_key="YOUR_OPENAI_KEY", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + provider="openai" + ) +) +``` + + +```python +client = OpenAI( + api_key="YOUR_ANTHROPIC_KEY", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + provider="anthropic", + metadata={"model_override": "claude-3-opus-20240229"} + ) +) +``` + + +```python +client = OpenAI( + api_key="dummy_key", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_AZURE_VIRTUAL_KEY" + ) +) +``` + + + +### 3. Advanced Routing with Configs + +Use Portkey's config system for advanced features while tracking in Langfuse: + +```python +# Create a config in Portkey dashboard first, then reference it +client = OpenAI( + api_key="dummy_key", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + config="pc-langfuse-prod" # Your saved config ID + ) +) +``` + +Example config for fallback between providers: +```json +{ + "strategy": { + "mode": "fallback" + }, + "targets": [ + { + "virtual_key": "openai-key", + "override_params": {"model": "gpt-4o"} + }, + { + "virtual_key": "anthropic-key", + "override_params": {"model": "claude-3-opus-20240229"} + } + ] +} +``` + +### 4. Caching for Cost Optimization + +Enable caching to reduce costs while maintaining full observability: + +```python +config = { + "cache": { + "mode": "semantic", + "max_age": 3600 + }, + "virtual_key": "YOUR_VIRTUAL_KEY" +} + +client = OpenAI( + api_key="dummy_key", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + config=config + ) +) +``` + +### 5. Custom Metadata and Tracing + +Add custom metadata visible in both Langfuse and Portkey: + +```python +client = OpenAI( + api_key="YOUR_API_KEY", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + provider="openai", + metadata={ + "user_id": "user_123", + "session_id": "session_456", + "environment": "production" + }, + trace_id="langfuse-trace-001" + ) +) +``` + + + + + Automatically switch to backup targets if the primary target fails. + + + Route requests to different targets based on specified conditions. + + + Distribute requests across multiple targets based on defined weights. + + + Enable caching of responses to improve performance and reduce costs. + + +Automatic retry handling with exponential backoff for failed requests + + + Set and manage budget limits across teams and departments. Control costs with granular budget limits and usage tracking. + + + + +## Observability Features + +With this integration, you get: + +### In Langfuse: +- request/response logging +- Latency tracking +- Token usage analytics +- Cost calculation +- Trace visualization + +### In Portkey: +- Request logs with provider details +- Advanced analytics across providers +- Cost tracking and budgets +- Performance metrics +- Custom dashboards +- Token usage analytics + + + + + + + + +## Migration Guide + +If you're already using Langfuse with OpenAI, migrating to use Portkey is simple: + + +```python Before +from langfuse.openai import OpenAI + +client = OpenAI( + api_key="YOUR_OPENAI_KEY" +) +``` + +```python After +from langfuse.openai import OpenAI +from portkey_ai import createHeaders, PORTKEY_GATEWAY_URL + +client = OpenAI( + api_key="YOUR_OPENAI_KEY", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key="YOUR_PORTKEY_API_KEY", + provider="openai" + ) +) +``` + + +## Next Steps + +- [Create Virtual Keys](/product/ai-gateway/virtual-keys) for secure API key management +- [Build Configs](/product/ai-gateway/configs) for advanced routing +- [Set up Guardrails](/product/guardrails) for content filtering +- [Implement Caching](/product/ai-gateway/cache-simple-and-semantic) for cost optimization + +## Resources + +- [Langfuse Documentation](https://langfuse.com/docs) +- [Portkey AI Gateway Guide](/product/ai-gateway) +- [Portkey Python SDK Reference](/api-reference/portkey-sdk-client) + + +For enterprise support and custom features, contact our [enterprise team](https://calendly.com/portkey-ai). + diff --git a/virtual_key_old/integrations/tracing-providers/logfire.mdx b/virtual_key_old/integrations/tracing-providers/logfire.mdx new file mode 100644 index 00000000..cd24e861 --- /dev/null +++ b/virtual_key_old/integrations/tracing-providers/logfire.mdx @@ -0,0 +1,17 @@ +--- +title: "Pydantic Logfire" +description: "Logfire is a tool for comprehensive observability of your LLM applications with OpenTelemetry." +--- + + +[Logfire](https://pydantic.dev/logfire) and any opentelemetry compatible tracing library works out of the box with Portkey. + +All you need to do is set the following environment variables in your application: + +```sh +OTEL_EXPORTER_OTLP_ENDPOINT = "https://api.portkey.com/v1/otel" +OTEL_EXPORTER_OTLP_HEADERS = "x-portkey-api-key={YOUR_PORTKEY_API_KEY}" +``` + + + diff --git a/virtual_key_old/integrations/vector-databases/milvus.mdx b/virtual_key_old/integrations/vector-databases/milvus.mdx new file mode 100644 index 00000000..36f03bf0 --- /dev/null +++ b/virtual_key_old/integrations/vector-databases/milvus.mdx @@ -0,0 +1,188 @@ +--- +title: "Milvus" +--- + +[Milvus](https://milvus.io/) is an open-source vector database built for GenAI applications. +It is built to be performant and scale to tens of billions of vectors with minimal performance loss. + +Portkey provides a proxy to Milvus, allowing you to use virtual keys and observability features. + +## Portkey SDK Integration with Milvus + +Portkey provides a consistent API to interact with models from various providers. To integrate Milvus with Portkey: + +### 1\. Install the Portkey SDK + +Add the Portkey SDK to your application to interact with Milvus through Portkey's gateway. + + + + ```sh + npm install --save portkey-ai + ``` + + + ```sh + pip install portkey-ai + ``` + + + +### 2\. Initialize Portkey with a Virtual Key + +To use Milvus with Portkey, get your Milvus API key from here, then add it to Portkey to create your [Milvus virtual key](/product/ai-gateway/virtual-keys#using-virtual-keys). + + + + +```js +import Portkey from 'portkey-ai' + +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your Milvus Virtual Key +}) +``` + + + + +```python +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Milvus +) +``` + + + +```python +from openai import OpenAI + +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +client = OpenAI( + api_key="MILVUS_API_KEY", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key="PORTKEY_API_KEY", + provider="milvus", + custom_host="MILVUS_HOST" # Replace with your Milvus host example: https://in03-34d7b37f7ee12c7.serverless.gcp-us-west1.cloud.zilliz.com + ) +) +``` + + + + +```js +import OpenAI from "openai"; + +import { PORTKEY_GATEWAY_URL, createHeaders } from "portkey-ai"; + +const client = new OpenAI({ + apiKey: "MILVUS_API_KEY", + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "milvus", + apiKey: "PORTKEY_API_KEY", + customHost: "MILVUS_HOST" // Replace with your Milvus host example: https://in03-34d7b37f7ee12c7.serverless.gcp-us-west1.cloud.zilliz.com + }), +}); +``` + + + +### 3\. Use the Portkey SDK to interact with Milvus + + + + ```python +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="MILVUS_VIRTUAL_KEY", + custom_host="MILVUS_HOST" # Replace with your Milvus host example: https://in03-34d7b37f7ee12c7.serverless.gcp-us-west1.cloud.zilliz.com +) + +response = portkey.post( + url="v2/vectordb/collections/list", # Replace with the endpoint you want to call +) + +print(response) + ``` + + + ```javascript + import Portkey from 'portkey-ai'; + + // Initialize the Portkey client + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "MILVUS_VIRTUAL_KEY", // Add your Milvus's virtual key + customHost="MILVUS_HOST" // Replace with your Milvus host example: https://in03-34d7b37f7ee12c7.serverless.gcp-us-west1.cloud.zilliz.com + }); + + response = portkey.post( + url="v2/vectordb/collections/list", # Replace with the endpoint you want to call + ) + + print(response) + ``` + + + ```javascript + import OpenAI from 'openai'; // We're using the v4 SDK + import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + + const openai = new OpenAI({ + apiKey: 'MILVUS_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + virtualKey: "MILVUS_VIRTUAL_KEY", + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + customHost: "MILVUS_HOST" // Replace with your Milvus host example: https://in03-34d7b37f7ee12c7.serverless.gcp-us-west1.cloud.zilliz.com + }) + }); + + response = openai.post( + url="v2/vectordb/collections/list", # Replace with the endpoint you want to call + ) + + print(response) + ``` + + + ```python + from openai import OpenAI + from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + + openai = OpenAI( + api_key='MILVUS_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="milvus", + api_key="PORTKEY_API_KEY", + custom_host="MILVUS_HOST" # Replace with your Milvus host example: https://in03-34d7b37f7ee12c7.serverless.gcp-us-west1.cloud.zilliz.com + ) + ) + + response = openai.post( + url="v2/vectordb/collections/list", # Replace with the endpoint you want to call + ) + + print(response) + ``` + + + ```sh +curl --location --request POST 'https://api.portkey.ai/v1/v2/vectordb/collections/list' \ +--header 'x-portkey-custom-host: https://in03-34d7b37f7de12c7.serverless.gcp-us-west1.cloud.zilliz.com' \ +--header 'x-portkey-virtual-key: MILVUS_VIRTUAL_KEY' \ +--header 'x-portkey-api-key: PORTKEY_API_KEY' + ``` + + diff --git a/virtual_key_old/integrations/vector-databases/qdrant.mdx b/virtual_key_old/integrations/vector-databases/qdrant.mdx new file mode 100644 index 00000000..bd9f0d6e --- /dev/null +++ b/virtual_key_old/integrations/vector-databases/qdrant.mdx @@ -0,0 +1,197 @@ +--- +title: "Qdrant" +--- + +[Qdrant](https://qdrant.tech/) is an open-source vector similarity search engine built for production-ready vector search applications. +It provides a convenient API to store, search, and manage vectors with additional payload data. + +Portkey provides a proxy to Qdrant, allowing you to use virtual keys and observability features. + +## Portkey SDK Integration with Qdrant + +Portkey provides a consistent API to interact with models from various providers. To integrate Qdrant with Portkey: + +### 1\. Install the Portkey SDK + +Add the Portkey SDK to your application to interact with Qdrant through Portkey's gateway. + + + + ```sh + npm install --save portkey-ai + ``` + + + ```sh + pip install portkey-ai + ``` + + + +### 2\. Initialize Portkey with a Virtual Key + +To use Qdrant with Portkey, get your API key from [Qdrant App](https://cloud.qdrant.io/), then add it to Portkey to create your [Qdrant virtual key](/product/ai-gateway/virtual-keys#using-virtual-keys). + +You will also need your Portkey API Key from [Portkey's Dashboard](https://app.portkey.ai). + + + + +```js +import Portkey from 'portkey-ai' + +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Your Qdrant Virtual Key +}) +``` + + +```python +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Qdrant +) +``` + + +```python +from openai import OpenAI + +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +client = OpenAI( + api_key="QDRANT_API_KEY", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key="PORTKEY_API_KEY", + provider="qdrant", + custom_host="QDRANT_HOST" # Replace with your Qdrant host + ) +) +``` + + +```js +import OpenAI from "openai"; + +import { PORTKEY_GATEWAY_URL, createHeaders } from "portkey-ai"; + +const client = new OpenAI({ + apiKey: "QDRANT_API_KEY", + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "qdrant", + apiKey: "PORTKEY_API_KEY", + customHost: "QDRANT_HOST" // Replace with your Qdrant host + }), +}); +``` + + + +### 3\. Use the Portkey SDK to interact with Qdrant + + + + +```python +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="QDRANT_VIRTUAL_KEY", + custom_host="QDRANT_HOST" # Replace with your Qdrant host +) + +response = portkey.post( + url="https://xxxx-xxx-xxx-xx-xxxxxx.us-west-2-0.aws.cloud.qdrant.io", # Qdrant search endpoint, you can use any Qdrant endpoint +) + +print(response) +``` + + + +```javascript +import Portkey from 'portkey-ai'; + +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "QDRANT_VIRTUAL_KEY", // Add your Qdrant's virtual key + customHost: "QDRANT_HOST" // Replace with your Qdrant host +}); + +async function makeRequest() { + const response = await portkey.post( + "https://xxxx-xxx-xxx-xx-xxxxxx.us-west-2-0.aws.cloud.qdrant.io", + { /* Your request body here */ } + ); + console.log(response); +} + +makeRequest(); +``` + + + +```javascript +import OpenAI from 'openai'; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const portkey = new OpenAI({ + apiKey: 'QDRANT_API_KEY', + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + virtualKey: "QDRANT_VIRTUAL_KEY", + apiKey: "PORTKEY_API_KEY", + customHost: "QDRANT_HOST" // Replace with your Qdrant host + }) +}); + +async function makeRequest() { + const response = await portkey.post( + "https://xxxx-xxx-xxx-xx-xxxxxx.us-west-2-0.aws.cloud.qdrant.io", + { /* Your request body here */ } + ); + console.log(response); +} + +makeRequest(); +``` + + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='QDRANT_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="qdrant", + api_key="PORTKEY_API_KEY", + custom_host="QDRANT_HOST" # Replace with your Qdrant host + ) +) + +response = openai.post( + url="https://xxxx-xxx-xxx-xx-xxxxxx.us-west-2-0.aws.cloud.qdrant.io", # Qdrant search endpoint, You can use any Qdrant endpoint +) + +print(response) +``` + + + +```sh +curl --location --request POST 'https://xxxx-xxx-xxx-xx-xxxxxx.us-west-2-0.aws.cloud.qdrant.io' \ +--header 'x-portkey-custom-host: QDRANT_HOST' \ +--header 'x-portkey-virtual-key: QDRANT_VIRTUAL_KEY' \ +--header 'x-portkey-api-key: PORTKEY_API_KEY' +``` + + diff --git a/virtual_key_old/introduction/feature-overview.mdx b/virtual_key_old/introduction/feature-overview.mdx new file mode 100644 index 00000000..dac7d4ff --- /dev/null +++ b/virtual_key_old/introduction/feature-overview.mdx @@ -0,0 +1,145 @@ +--- +title: 'Portkey Features' +description: 'Explore the powerful features of Portkey' +--- + +## AI Gateway + +Connect to 250+ AI models using a single consistent API. Set up load balancers, automated fallbacks, caching, conditional routing, and more, seamlessly. + + + + Integrate with multiple AI models through a single API + + + Implement simple and semantic caching for improved performance + + + Set up automated fallbacks for enhanced reliability + + + Handle various data types with multimodal AI capabilities + + + Implement automatic retries for improved resilience + + + Distribute workload efficiently across multiple models + + + Manage access with virtual API keys + + + Set and manage request timeouts + + + Implement canary testing for safe deployments + + + Route requests based on specific conditions + + + Set and manage budget limits + + + +## Observability & Logs + +Gain real-time insights, track key metrics, and streamline debugging with our OpenTelemetry-compliant system. + + + + Access and analyze detailed logs + + + Implement distributed tracing for request flows + + + Gain insights through comprehensive analytics + + + Apply filters for targeted analysis + + + Manage and utilize metadata effectively + + + Collect and analyze user feedback + + + +## Prompt Library + +Collaborate with team members to create, templatize, and version prompt templates easily. Experiment across 250+ LLMs with a strong Publish/Release flow to deploy the prompts. + + + + Create and manage reusable prompt templates + + + Utilize modular prompt components + + + Advanced prompting with JSON mode + + + +## Guardrails + +Enforce Real-Time LLM Behavior with 50+ state-of-the-art AI guardrails, so that you can synchronously run Guardrails on your requests and route them with precision. + + + + Implement rule-based safety checks + + + Leverage AI for advanced content filtering + + + Integrate third-party safety solutions + + + Customize guardrails to your needs + + + +## Agents + +Natively integrate Portkey's gateway, guardrails, and observability suite with leading agent frameworks and take them to production. + + + + + + + + + + + + + + + + + + +## More Resources + + + + Compare different Portkey subscription plans + + + Join our community of developers + + + Explore our comprehensive API documentation + + + Learn about our enterprise solutions + + + Contribute to our open-source projects + + diff --git a/virtual_key_old/introduction/make-your-first-request.mdx b/virtual_key_old/introduction/make-your-first-request.mdx new file mode 100644 index 00000000..c21e4169 --- /dev/null +++ b/virtual_key_old/introduction/make-your-first-request.mdx @@ -0,0 +1,73 @@ +--- +title: "Make Your First Request" +description: "Integrate Portkey and analyze your first LLM call in 2 minutes!" +--- + +## 1\. Get your Portkey API Key + +[Create](https://app.portkey.ai/signup) or [log in](https://app.portkey.ai/login) to your Portkey account. Grab your account's API key from the "Settings" page. + + + Copy your Portkey account API key + + +Based on your access level, you might see the relevant permissions on the API key modal - tick the ones you'd like, name your API key, and save it. + +## 2\. Integrate Portkey + +Portkey offers a variety of integration options, including SDKs, REST APIs, and native connections with platforms like OpenAI, Langchain, and LlamaIndex, among others. + +### Through the OpenAI SDK + +If you're using the **OpenAI SDK**, import the Portkey SDK and configure it within your OpenAI client object: + + + + +### Portkey SDK + +You can also use the **Portkey SDK / REST APIs** directly to make the chat completion calls. This is a more versatile way to make LLM calls across any provider: + + + + +Once, the integration is ready, you can view the requests reflect on your Portkey dashboard. + + + + +### Other Integration Guides + + + + + + + + + + +## 3\. Next Steps + +Now that you're up and running with Portkey, you can dive into the various Portkey features to learn about all of the supported functionalities: + + + + + + + + + + + + +While you're here, why not [give us a star](https://git.new/ai-gateway-docs)? It helps us a lot! + diff --git a/virtual_key_old/introduction/what-is-portkey.mdx b/virtual_key_old/introduction/what-is-portkey.mdx new file mode 100644 index 00000000..af8c8276 --- /dev/null +++ b/virtual_key_old/introduction/what-is-portkey.mdx @@ -0,0 +1,168 @@ +--- +title: "What is Portkey?" +description: Portkey AI is a comprehensive platform designed to streamline and enhance AI integration for developers and organizations. It serves as a unified interface for interacting with over 250 AI models, offering advanced tools for control, visibility, and security in your Generative AI apps. +mode: "wide" +--- + +It takes 2 mins to integrate and with that, it starts monitoring all of your LLM requests and makes your app resilient, secure, performant, and more accurate at the same time. + +Here's a product walkthrough (3 mins): + + + +### Integrate in 3 Lines of Code + + + +```Python Python +from portkey_ai import Portkey + +portkey = Portkey( + api_key="YOUR_PORTKEY_API_KEY", + virtual_key="YOUR_VIRTUAL_KEY" +) + +chat_complete = portkey.chat.completions.create( + model="gpt-3.5-turbo", + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "Hello!"} + ] +) +print(chat_complete.choices[0].message.content) +``` + +```js NodeJS +import Portkey from 'portkey-ai'; + +const portkey = new Portkey({ + apiKey: "YOUR_PORTKEY_API_KEY", + virtualKey: "YOUR_VIRTUAL_KEY" +}); + +async function createChatCompletion() { + const chat_complete = await portkey.chat.completions.create({ + model: "gpt-3.5-turbo", + messages: [ + { role: "system", content: "You are a helpful assistant." }, + { role: "user", content: "Hello!" } + ] + }); + console.log(chat_complete.choices[0].message.content); +} + +createChatCompletion(); +``` + +```sh REST API +curl https://api.portkey.ai/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: YOUR_PORTKEY_API_KEY" \ + -H "x-portkey-virtual-key: YOUR_VIRTUAL_KEY" \ + -d '{ + "model": "gpt-3.5-turbo", + "messages": [ + { + "role": "system", + "content": "You are a helpful assistant." + }, + { + "role": "user", + "content": "Hello!" + } + ] + }' +``` + +```py OpenAI Python SDK +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +client = OpenAI( + api_key="YOUR_OPENAI_API_KEY", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="YOUR_PORTKEY_API_KEY" + ) +) + +chat_complete = client.chat.completions.create( + model="gpt-3.5-turbo", + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "Hello!"} + ] +) +print(chat_complete.choices[0].message.content) +``` + +```js OpenAI NodeJS SDK +import OpenAI from 'openai'; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'; + +const openai = new OpenAI({ + apiKey: 'YOUR_OPENAI_API_KEY', + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "YOUR_PORTKEY_API_KEY" + }) +}); + +async function createChatCompletion() { + const chat_complete = await openai.chat.completions.create({ + model: "gpt-3.5-turbo", + messages: [ + { role: "system", content: "You are a helpful assistant." }, + { role: "user", content: "Hello!" } + ] + }); + console.log(chat_complete.choices[0].message.content); +} + +createChatCompletion(); +``` + + + + While you're here, why not [give us a star](https://git.new/ai-gateway-docs)? It helps us a lot! + + +### FAQs + + + + Portkey is hosted on edge workers throughout the world, ensuring minimal latency. Our benchmarks estimate a total latency addition between 20-40ms compared to direct API calls. This slight increase is often offset by the benefits of our caching and routing optimizations. + + Our edge worker locations: + + + + + + Portkey AI is ISO:27001 and SOC 2 certified, and GDPR & HIPAA compliant. We maintain best practices for service security, data storage, and retrieval. All data is encrypted in transit and at rest using industry-standard AES-256 encryption. For enhanced security, we offer: + 1. On request, we can enable a feature that does NOT store any of your request and response body objects in Portkey datastores or our logs. + 2. For enterprises, we offer managed hosting to deploy Portkey inside private clouds. + + For more information on these options, contact us at support@portkey.ai. + + + Portkey is built on scalable infrastructure and can handle millions of requests per minute with very high concurrency. We currently serve over 25M requests daily with a 99.99% uptime. Our edge architecture & scaling capabilities ensure we can accommodate sudden spikes in traffic without performance degradation. + + View our Status Page + + + We *DO NOT* impose any explicit timeout for our free OR paid plans currently. While we don't time out requests on our end, we recommend implementing client-side timeouts appropriate for your use case to handle potential network issues or upstream API delays. + + + Yes! We support SSO with any custom OIDC provider. + + + Portkey's Gateway is open source and free to use. + On managed version, Portkey offers a free plan with 10k requests per month. We also offer paid plans with more requests and additional features. + + + We're available all the time on Discord, or on our support email - support@portkey.ai + + diff --git a/virtual_key_old/product/administration/configure-api-key-access-permissions.mdx b/virtual_key_old/product/administration/configure-api-key-access-permissions.mdx new file mode 100644 index 00000000..5bfb1dd9 --- /dev/null +++ b/virtual_key_old/product/administration/configure-api-key-access-permissions.mdx @@ -0,0 +1,50 @@ +--- +title: "Configure API Key Access Permissions for Workspaces" +--- + + + This is a Portkey Enterprise plan feature. + + +## Overview + +API Key Management in Portkey enables Organization administrators to control how workspace managers and members interact with API keys within their workspaces. This feature offers granular control over who can view and manage different types of API keys, providing enhanced security and appropriate access levels across your organization. + +## Accessing API Key Management Permissions + +1. Navigate to **Admin Settings** in the Portkey dashboard +2. Select the **Security** tab from the left sidebar +3. Locate the **API Key Management** section + +## Permission Settings + +The API Key Management section provides four distinct permission options: + +| Permission | Description | +|------------|-------------| +| **View Keys (Workspace Managers)** | Enable workspace managers to view both user API keys and service API keys within their workspace. Keys are limited to those created by workspace administrators. | +| **Manage Keys (Workspace Managers)** | Allow workspace managers to create, update, and delete both user API keys and service API keys within their workspace. Keys are limited to those created by workspace administrators. | +| **View Keys (Workspace Members)** | Enable workspace members to view user API keys only. Members can only see keys created for them by workspace managers or administrators. | +| **Manage Keys (Workspace Members)** | Allow workspace members to create, update, and delete user API keys only. Members can only manage keys created for them by workspace managers or administrators. | + + + + + +## Understanding API Key Types + +**Note**: Service API keys provide system-level access and are distinct from user API keys which grant individual user access. + +- **Service API Keys**: Used for automated processes, integrations, and system-level operations. These keys typically have broader permissions and are not tied to individual users. +- **User API Keys**: Associated with specific users and provide individualized access to Portkey resources. These keys are generally more limited in scope and tied to the permissions of the specific user. + + +## Related Features + + + Learn about Portkey's comprehensive access control features including user roles and organization hierarchy + + + + Understand the difference between Admin and Workspace API Keys in Portkey + diff --git a/virtual_key_old/product/administration/configure-logs-access-permissions-in-workspace.mdx b/virtual_key_old/product/administration/configure-logs-access-permissions-in-workspace.mdx new file mode 100644 index 00000000..958af434 --- /dev/null +++ b/virtual_key_old/product/administration/configure-logs-access-permissions-in-workspace.mdx @@ -0,0 +1,45 @@ +--- +title: "Configure Logs Access Permissions for Workspace" +--- + + + This is a Portkey Enterprise plan feature. + + +## Overview +Logs Management in Portkey enables Organization and Workspace adminis to control who can access logs and log metadata within workspaces. This feature provides granular permissions to protect sensitive information while enabling appropriate visibility for team members. + +## Accessing Logs Management +1. Navigate to **Admin Settings** in the Portkey dashboard +2. Select the **Security** tab from the left sidebar +3. Locate the **Logs Management** section + +## Permission Settings + +The Logs Management section provides four distinct permission options: + +| Permission | Description | +|------------|-------------| +| **Managers View Logs** | Allow workspace managers to view logs within their workspace | +| **Managers View Logs Metadata** | Allow workspace managers to view logs metadata within their workspace | +| **Members View Logs** | Allow workspace members to view logs within their workspace | +| **Members View Logs Metadata** | Allow workspace members to view logs metadata within their workspace | + + + + + + +## Logs vs. Logs Metadata +- **Logs**: Complete log entries including request and response payloads +- **Logs Metadata**: Information such as timestamps, model used, tokens consumed, and other metrics without the actual content + +## Related Features + + + Learn about Portkey's access control features including user roles and organization hierarchy + + + + Export logs for longer-term storage or analysis + diff --git a/virtual_key_old/product/administration/configure-virtual-key-access-permissions.mdx b/virtual_key_old/product/administration/configure-virtual-key-access-permissions.mdx new file mode 100644 index 00000000..a8bb709e --- /dev/null +++ b/virtual_key_old/product/administration/configure-virtual-key-access-permissions.mdx @@ -0,0 +1,57 @@ +--- +title: "Configure Virtual Key Access Permission for Workspaces" +--- + + + This is a Portkey Enterprise plan feature. + + +## Overview + +Virtual Key Management in Portkey allows Organization administrators to define who can view and manage virtual keys within workspaces. This feature provides granular control over access to virtual keys, which are critical for managing connections to external AI providers and services. + +## Accessing Virtual Key Management Permissions + +1. Navigate to **Admin Settings** in the Portkey dashboard +2. Select the **Security** tab from the left sidebar +3. Locate the **Virtual Key Management** section + +## Permission Settings + +The Virtual Key Management section provides three distinct permission options: + +| Permission | Description | +|------------|-------------| +| **View Virtual Keys (Workspace Managers)** | Enable workspace managers to view all virtual keys within their workspace. | +| **Manage Virtual Keys (Workspace Managers)** | Allow workspace managers to create, update, and delete virtual keys within their workspace. | +| **View Virtual Keys (Workspace Members)** | Enable workspace members to view virtual keys within their workspace. Note: Members cannot create, modify or delete virtual keys by default. | + + + + + +## Understanding Virtual Keys + +Virtual keys in Portkey securely store provider credentials and enable: + +- Centralized management of AI provider keys +- Abstraction of actual provider keys from end users +- Definition of routing rules, fallbacks, and other advanced features +- Application of usage limits and tracking across providers + +By controlling who can view and manage these virtual keys, organizations can maintain security while enabling appropriate access for different team roles. + + +## Related Features + + + Learn about Portkey's access control features including user roles and organization hierarchy + + + + Understand how virtual keys work and how to configure them effectively + + + + Learn how to set budget limits on virtual keys to control spending + diff --git a/virtual_key_old/product/administration/enforce-budget-and-rate-limit.mdx b/virtual_key_old/product/administration/enforce-budget-and-rate-limit.mdx new file mode 100644 index 00000000..6cf99fd6 --- /dev/null +++ b/virtual_key_old/product/administration/enforce-budget-and-rate-limit.mdx @@ -0,0 +1,124 @@ +--- +title: "Enforce Budget Limits and Rate Limits for Your API Keys" +description: "Configure budget and rate limits on API keys to effectively manage AI spending and usage across your organization" +--- + + +Available on **Enterprise** plan and select **Pro** customers. + + +## Overview +For enterprises deploying AI at scale, maintaining financial oversight and operational control is crucial. Portkey's governance features for API keys provide finance teams, IT departments, and executives with the transparency and guardrails needed to confidently scale AI adoption across the organization. + +By implementing budget and rate limits on API keys at both organization and workspace levels, you can: + +- Prevent unexpected cost overruns through automated spending caps +- Maintain performance and availability through usage rate controls +- Receive timely notifications when thresholds are approached +- Enforce consistent governance policies across teams and departments + +These capabilities ensure your organization can innovate with AI while maintaining predictable costs and usage patterns. + + +## Budget Limits + +Budget limits allow you to set maximum LLM spending or token usage thresholds on your API keys, automatically preventing further usage when limits are reached. + +When creating or editing an API key, you can establish spending parameters that align with your financial planning: + + + + +### Setting Up Budget Limits + +When creating a new API key or editing an existing one: + +1. Toggle on **Add Budget Limit** +2. Choose between two limit types: + - **Cost**: Set a maximum spend in USD (minimum $1) + - **Tokens**: Set a maximum token usage + + + + + +### Alert Thresholds + +You can configure alert thresholds to receive notifications before reaching your full budget: + +1. Enter a value in the **Alert Threshold** field +2. When usage reaches this threshold, notifications will be sent to configured recipients +3. The API key continues to function until the full budget limit is reached + +### Periodic Reset Options + +Budget limits can be set to automatically reset at regular intervals: + + + + + +- **No Periodic Reset**: The budget limit applies until exhausted +- **Reset Weekly**: Budget limits reset every Sunday at 12 AM UTC +- **Reset Monthly**: Budget limits reset on the 1st of each month at 12 AM UTC + +## Rate Limits + +Rate limits control how frequently an API key can be used, helping you maintain application performance and prevent unexpected usage spikes. + +### Setting Up Rate Limits + +When creating a new API key or editing an existing one: + +1. Toggle on **Add Rate Limit** +2. Choose your limit type: + - **Requests**: Limit based on number of API calls + - **Tokens**: Limit based on token consumption +3. Specify the limit value and time interval + + + + + +### Time Intervals + +Rate limits can be applied using three different time intervals: + +- **Per Minute**: For granular control of high-frequency applications +- **Per Hour**: For balanced control of moderate usage +- **Per Day**: For broader usage management + +When a rate limit is reached, subsequent requests are rejected until the time interval resets. + +## Email Notifications + +Email notifications keep relevant stakeholders informed about API key usage and when limits are approached or reached. + +### Configuring Notifications + +To set up email notifications for an API key with budget limits: + +1. Toggle on **Email Notifications** when creating/editing an API key +2. Add recipient email addresses: + - Type an email address and click **New** or press Enter + - Add multiple recipients as needed + + + + + +### Default Recipients + +When limits are reached or thresholds are crossed, Portkey automatically sends notifications to: + +- Organization administrators +- Organization owners +- The API key creator/owner + +You can add additional recipients such as finance team members, department heads, or project managers who need visibility into AI usage. + +## Availability + +These features are available to Portkey Enterprise customers and select Pro users. To enable these features for your account, please contact [support@portkey.ai](mailto:support@portkey.ai) or join the [Portkey Discord](https://portkey.ai/community) community. + +To learn more about the Portkey Enterprise plan, [schedule a consultation](https://portkey.sh/demo-16). diff --git a/virtual_key_old/product/administration/enforce-default-config.mdx b/virtual_key_old/product/administration/enforce-default-config.mdx new file mode 100644 index 00000000..f3632bef --- /dev/null +++ b/virtual_key_old/product/administration/enforce-default-config.mdx @@ -0,0 +1,199 @@ +--- +title: "Enforcing Default Configs on API Keys" +description: "Learn how to attach default configs to API keys for enforcing governance controls across your organization" +--- + +## Overview + +Portkey allows you to attach default configs to API keys, enabling you to enforce specific routing rules, security controls, and other governance measures across all API calls made with those keys. This feature provides a powerful way to implement organization-wide policies without requiring changes to individual application code. + + + This feature is available on all Portkey plans. + + +## How It Works + +When you attach a default config to an API key: + +1. All API calls made using that key will automatically apply the config settings +2. Users don't need to specify config IDs in their code +3. Administrators can update governance controls by simply updating the config, without requiring code changes + +This creates a clean separation between application development and governance controls. + + + Create and manage configs to define routing rules, fallbacks, caching, and more + + +## Benefits of Default Configs + +Attaching default configs to API keys provides several governance benefits: + +- **Centralized Control**: Update policies for multiple applications by changing a single config +- **Access Management**: Control which models and providers users can access +- **Cost Control**: Implement budget limits and control spending across teams +- **Reliability**: Enforce fallbacks, retries, and timeout settings organization-wide +- **Security**: Apply guardrails and content moderation across all applications +- **Consistent Settings**: Ensure all applications use the same routing logic + +## Setting Up Default Configs + +### Prerequisites + +Before attaching a config to an API key, you need to: + +1. Create a [config](/product/ai-gateway/configs) on Portkey app with your desired settings +2. Note the config ID that want to attach to your API keys + +### Attaching Configs via the UI + +Navigate to **API Keys** in the sidebar, then: +- **New Keys**: Click **Create** and select your desired config from the **Config** dropdown +- **Existing Keys**: Click the edit icon on any key and update the config selection +- **Config Override**: Enable or Disable config override flag. + - **ON** (default): Users can specify their own configs in requests, which will override the default config attached to the API key + - **OFF**: Any attempt to override the default config in a request will result in a 400 error, enforcing the use of only the pre-approved config + + + + Admin API Key Config Selection + + + + You can attach only one config to an API key. This applies to both workspace API keys and admin API keys. + + +### Attaching Config via the API + +You can also programmatically attach config when creating or updating API keys using the Portkey API. + +#### Creating a New API Key with Default Config + + + +```python +from portkey_ai import Portkey + +portkey = Portkey(api_key="YOUR_ADMIN_API_KEY") + +api_key = portkey.api_keys.create( + name="engineering-team", + type="organisation", + workspace_id="YOUR_WORKSPACE_ID", + defaults={ + "config_id": "pc-your-config-id", + "metadata": { + "environment": "production", + "department": "engineering" + } + }, + scopes=["logs.view", "configs.read"] +) + +print(f"API Key created: {api_key.key}") +``` + + +```javascript +import { Portkey } from 'portkey-ai'; + +const portkey = new Portkey({ + apiKey: 'YOUR_ADMIN_API_KEY' +}); + +async function createApiKey() { + const apiKey = await portkey.apiKeys.create({ + name: 'engineering-team', + type: 'organisation', + workspace_id: 'YOUR_WORKSPACE_ID', + defaults: { + config_id: 'pc-your-config-id', + metadata: { + environment: 'production', + department: 'engineering' + } + }, + scopes: ['logs.view', 'configs.read'] + }); + + console.log(`API Key created: ${apiKey.key}`); +} + +createApiKey(); +``` + + +```bash +curl -X POST https://api.portkey.ai/v1/admin/api-keys \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer YOUR_ADMIN_API_KEY" \ + -d '{ + "name": "engineering-team", + "type": "organisation", + "workspace_id": "YOUR_WORKSPACE_ID", + "defaults": { + "config_id": "pc-your-config-id", + "metadata": { + "environment": "production", + "department": "engineering" + } + }, + "scopes": ["logs.view", "configs.read"] + }' +``` + + + +#### Updating an Existing API Key + +For detailed information on API key management, refer to our API documentation: + + + Learn how to create API keys with default configs + + + + Learn how to update existing API keys with new default configs + + +## Config Precedence + +When using API keys with default configs, Portkey provides flexible options for controlling config behavior: + +- The default config attached to the API key will be automatically applied to all requests made with that key +- By default, if a user explicitly specifies a config ID in their request, that config will override the default config attached to the API key +- You can now disable config overrides by toggling off "Allow Config Override" when creating or editing API keys + + +This flexibility allows for centralized governance while still enabling exceptions when needed. + +## Use Cases + +### Enterprise AI Governance + +For large organizations with multiple AI applications, attaching default configs to API keys enables centralized governance: + +1. Create department-specific API keys with appropriate configs +2. Apply budget limits and define model usage per department +3. Track usage and spending per team by filtering logs by config_id +4. Update policies centrally without requiring code changes + +### Security and Compliance + +Ensure all API interactions follow security protocols by enforcing: + +- Specific models that have been approved for use +- Content moderation with input/output guardrails +- PII detection and redaction + +### Cost Management + +Control AI spending by: + +- Routing to cost-effective models by default +- Implementing caching for frequently used prompts +- Applying rate limits to prevent unexpected usage spikes + +## Support + +For questions about configuring default settings for API keys or troubleshooting issues, contact [Portkey support](mailto:support@portkey.ai) or reach out on [Discord](https://portkey.sh/reddit-discord). diff --git a/virtual_key_old/product/administration/enforce-orgnization-level-guardrails.mdx b/virtual_key_old/product/administration/enforce-orgnization-level-guardrails.mdx new file mode 100644 index 00000000..79289fc8 --- /dev/null +++ b/virtual_key_old/product/administration/enforce-orgnization-level-guardrails.mdx @@ -0,0 +1,38 @@ +--- +title: "Enforcing Org Level Guardrails" +--- +## Overview +Portkey enables organization owners to enforce request guardrails at the organization level. This feature ensures that all API requests made within the organization comply with predefined policies, enhancing security, compliance, and governance. + +## How It Works +Organization owners can define input and output guardrails in the Organization Guardrails section. These guardrails are enforced on all API requests made within the organization, ensuring uniform policy enforcement across all users and applications. + +- **Input Guardrails**: Define checks and constraints for incoming LLM requests. +- **Output Guardrails**: Ensure LLM responses align with organizational policies. + +The guardrails available here are the same as those found in the Guardrails section of the Portkey platform. Multiple providers are supported for setting up guardrails. For a detailed list of supported providers and configurations + + + Learn about the different Guardrails you can set up in Portkey + + + +## Configuration +### Setting Up Guardrails Requirements +1. Head to `Admin Settings` on the Portkey dashboard +2. Navigate to the `Organisation Guardrails` section +3. Add your `Input` and/or `Output` Guardrails +4. Save your changes + +Once configured, these guardrails will be enforced on all API requests across the organization. + + + + +Best Practices +- Clearly communicate guardrail requirements to all developers in your organization. +- Maintain internal documentation on your guardrail policies to ensure consistency. + + +## Support +For questions about configuring metadata schemas or troubleshooting issues, contact [Portkey support](mailto:support@portkey.ai) or reach out on [Discord](https://portkey.sh/reddit-discord). diff --git a/virtual_key_old/product/administration/enforce-workspace-budget-limts-and-rate-limits.mdx b/virtual_key_old/product/administration/enforce-workspace-budget-limts-and-rate-limits.mdx new file mode 100644 index 00000000..14632bec --- /dev/null +++ b/virtual_key_old/product/administration/enforce-workspace-budget-limts-and-rate-limits.mdx @@ -0,0 +1,110 @@ +--- +title: "Enforce Workspace Budget and Rate Limits" +description: "Configure budget and rate limits at the workspace level to effectively manage AI spending and resource allocation" +--- + + +Available on **Enterprise** plan and select **Pro** customers. + + +## Overview + +Workspace Budget Limits provide granular financial and usage control at the workspace level, allowing organization admins/owners and workspace admins to allocate resources effectively across teams and projects. This feature complements API key-level budget controls, creating a comprehensive governance framework for AI spending across your organization. + +By implementing budget and rate limits at the workspace level, you can: + +- Allocate specific budgets to different departments, teams, or projects +- Prevent individual workspaces from consuming disproportionate resources +- Establish rate limits to ensure equitable API access across workspaces +- Maintain greater visibility and control over organization-wide AI expenditure + +## Accessing Workspace Budget Controls + +To configure budget limits for a workspace: + +1. Navigate to the sidebar and select **Workspace Control** +2. In the Workspace Control panel, click on the **Budget Allocation** tab +3. Configure your desired rate and budget limits for the workspace + + +Only workspace administrators and organization owners/admins have access to these controls. Permissions can be managed in **Admin Settings** โ†’ **Security** tab. + + +## Setting Rate Limits + +Rate limits control how frequently requests can be made from a workspace, helping you maintain application performance and prevent unexpected usage spikes. + +To set up rate limits for a workspace: + +1. Toggle on **Add Rate Limit** +2. Choose your limit type: + - **Requests**: Limit based on number of API calls + - **Tokens**: Limit based on token consumption +3. Specify the limit value in the **Set Rate Limit** field +4. Select a time interval from the dropdown (Requests/Minute, Requests/Hour, Requests/Day) + + + + + +When a workspace reaches its rate limit, all subsequent requests from that workspace will be rejected until the time interval resets, regardless of which API key is used. + +## Setting Budget Limits + +Budget limits allow you to set maximum spending or token usage thresholds for an entire workspace, automatically preventing further usage when limits are reached. + +To set up budget limits for a workspace: + +1. Toggle on **Add Budget** +2. Choose your limit type: + - **Cost**: Set a maximum spend in USD + - **Tokens**: Set a maximum token usage +3. Enter the budget amount in the **Budget Limit ($)** field +4. Optionally, set an **Alert Threshold ($)** to receive notifications before reaching the full budget +5. Select a **Periodic Reset** option to determine when the budget refreshes + + + + + +### Alert Thresholds + +Alert thresholds trigger notifications when a percentage of the workspace budget has been consumed: + +1. Enter a value in the **Alert Threshold ($)** field +2. When usage reaches this threshold, notifications will be sent to configured recipients +3. The workspace continues to function until the full budget limit is reached + +### Periodic Reset Options + +Workspace budgets can be set to automatically reset at regular intervals: + +- **No Periodic Reset**: The budget limit applies until exhausted +- **Reset Weekly**: Budget limits reset every Sunday at 12 AM UTC +- **Reset Monthly**: Budget limits reset on the 1st of each month at 12 AM UTC + + +## Notification System + +When workspace limits are approached or reached, Portkey automatically sends notifications to: + +- Organization administrators +- Organization owners +- Workspace administrators + +These notifications help teams stay informed about their resource usage and take appropriate action before limits are reached. + +## Use Cases + +Workspace budget limits are particularly useful for: + +- **Departmental Allocations**: Assign specific AI budgets to different departments (Marketing, Customer Support, R&D) +- **Project Management**: Allocate resources based on project priority and requirements +- **Cost Center Tracking**: Monitor and control spending across different cost centers +- **Phased Rollouts**: Gradually increase limits as teams demonstrate value and mature their AI use cases + + + +## Availability + +Workspace Budget Limits are available to Portkey Enterprise customers and select Pro users. To enable these features for your account, please contact [support@portkey.ai](mailto:support@portkey.ai) or join the [Portkey Discord](https://portkey.ai/community) community. diff --git a/virtual_key_old/product/administration/enforce-workspace-level-guardials.mdx b/virtual_key_old/product/administration/enforce-workspace-level-guardials.mdx new file mode 100644 index 00000000..e5a8cf5d --- /dev/null +++ b/virtual_key_old/product/administration/enforce-workspace-level-guardials.mdx @@ -0,0 +1,38 @@ +--- +title: "Enforcing Workspace Level Guardrails" +--- + +## Overview +Portkey allows workspace owners to enforce request guardrails at the workspace level. This feature ensures that all API requests made within a workspace comply with predefined policies, enhancing security, compliance, and governance at a more granular level. + +## How It Works +Workspace owners can define input and output guardrails in the workspace settings. These guardrails are enforced on all API requests made within the workspace, ensuring uniform policy enforcement across all users and applications in that workspace. + +- **Input Guardrails**: Define checks and constraints for incoming LLM requests within the workspace. +- **Output Guardrails**: Ensure LLM responses align with the defined guardrail policies. + +The guardrails available here are the ones created within the user's workspace. Users can select any of these as the default input/output guardrails for their workspace. Multiple providers are supported for setting up guardrails. For a detailed list of supported providers and configurations: + + + + Learn about the different Guardrails you can set up in Portkey + + +## Configuration +### Setting Up Workspace-Level Guardrails +1. Head to the Portkey dashboard and navigate to the sidebar. +2. Click on your workspace and select the **Edit (๐Ÿ–Š๏ธ) option**. +3. In the **Edit Workspace** menu: + - Choose an **Input Guardrail** and **Output Guardrail** from the options (created in the workspace) . +4. Save your changes. + +Once configured, these guardrails will be enforced on all API requests within the workspace. + + +**Best Practices** +- Clearly communicate guardrail requirements to all members in your workspace. +- Maintain internal documentation on your workspace guardrail policies to ensure consistency. + + +## Support +For questions about configuring workspace-level guardrails or troubleshooting issues, contact [Portkey support](mailto:support@portkey.ai) or reach out on [Discord](https://portkey.sh/reddit-discord). diff --git a/virtual_key_old/product/administration/enforcing-request-metadata.mdx b/virtual_key_old/product/administration/enforcing-request-metadata.mdx new file mode 100644 index 00000000..22daef8d --- /dev/null +++ b/virtual_key_old/product/administration/enforcing-request-metadata.mdx @@ -0,0 +1,178 @@ +--- +title: "Enforcing Request Metadata" +--- +## Overview +Portkey allows organisation owners to define mandatory [metadata](/product/observability/metadata) fields that must be included with every API request. This feature enables granular observability across your organisation's LLM usage. + +## How It Works +Organisation owners can define JSON schemas specifying required metadata properties for: +- API keys +- Workspaces + +These metadata requirements are strictly enforced whenever: +- A new API key is created +- A new workspace is created + + + Existing API keys and Workspaces will not be affected by the new metadata schema. The new schema will only be enforced on NEW API keys, Workspaces or when existing API keys are updated. + + +## Configuration +### Setting Up Metadata Requirements +1. Head to `Admin Settings` on the Portkey dashboard +2. Navigate to the `Organisation Properties` section +3. Select either `API Key Metadata Schema` or `Workspace Metadata Schema` +4. Create a [JSON schema](#json-schema-requirements) specifying your required metadata fields +5. Add or modify the metadata schem. Save your changes + +## JSON Schema Requirements +The metadata schema follows [JSON Schema draft-07](https://json-schema.org/draft-07) with certain modifications: + +### Required Schema Properties +- `"type": "object"` at the top level must always be present +- `"additionalProperties": true` must be present and always set to `true` +- Properties inside the schema must always have `"type": "string"` + +| Requirement | Description | +| ----------- | ----------- | +| `"type": "object"` | The top-level schema must be declared as an object | +| `"properties": {...}` | Define the metadata fields and their constraints | +| `"additionalProperties": true` | Additional properties must be allowed | + + + + + +```json +{ + "type": "object", + "properties": { + "team_id": { + "type": "string" + }, + "environment": { + "type": "string" + } + }, + "additionalProperties": true +} +``` + + + + +```json +{ + "type": "object", + "required": ["team_id", "service_id"], + "properties": { + "team_id": { + "type": "string" + }, + "service_id": { + "type": "string" + }, + "environment": { + "type": "string" + } + }, + "additionalProperties": true +} +``` + + + + +```json +{ + "type": "object", + "properties": { + "usertype": { + "type": "string" + }, + "environment": { + "type": "string", + "default": "dev" + } + }, + "additionalProperties": true +} +``` + + + + +```json +{ + "type": "object", + "properties": { + "use_case": { + "type": "string", + "enum": [ + "research", + "instruction", + "administrative" + ] + } + }, + "additionalProperties": true +} +``` + + + + + + + + + +## Using this Metadata in Portkey +Metadata validation on the schema is enforced on API Key/Workspace creation time. + +### When Creating API Keys +When creating a new API key (either Service or User type), you must provide metadata that conforms to the defined schema: +1. Navigate to the API Keys section +2. Click `Create` +3. Fill in the key details +4. In the Metadata field, provide a [JSON schema](#json-schema-requirements) with all required fields +5. If any required fields are missing, you'll receive an error + +### When Creating Workspaces +When creating a new workspace, you must provide metadata that conforms to the defined schema: +1. Navigate to Workspaces on the left sidebar +2. Click `+Add Workspace` +3. Fill in the workspace details +4. In the Metadata field, provide a [JSON schema](#json-schema-requirements) with all required fields +5. If any required fields are missing, you'll receive an error + +### Default Values +- Properties with default values will be automatically added if not specified +- User-provided values take precedence over default values + + + +## Metadata Precedence Order + +When the same key appears at multiple levels, the **precedence order** is: + +1. Workspace metadata (highest priority) +2. API key metadata +3. Request metadata (lowest priority) + + + Please note that before 1.10.20, the precedence order was: + 1. Request metadata (highest priority) + 2. API key metadata + 3. Workspace metadata (lowest priority) + + + + +Best Practices +- Organisation owners should clearly communicate metadata requirements to all developers +- Maintain internal documentation of your metadata schema + + +## Support +For questions about configuring metadata schemas or troubleshooting issues, contact [Portkey support](mailto:support@portkey.ai) or reach out on [Discord](https://portkey.sh/reddit-discord). diff --git a/virtual_key_old/product/ai-gateway.mdx b/virtual_key_old/product/ai-gateway.mdx new file mode 100644 index 00000000..9b08876e --- /dev/null +++ b/virtual_key_old/product/ai-gateway.mdx @@ -0,0 +1,86 @@ +--- +title: AI Gateway +description: The world's fastest AI Gateway with advanced routing & integrated Guardrails. +--- + +## Features + + + + + Use any of the supported models with a universal API (REST and SDKs) + + + + Save costs and decrease latencies by using a cache + + + + Connect to Remote MCP severs, allowing you to connect external tools and data sources. + + + + Fallback between providers and models for resilience + + + + Route to different targets based on custom conditional checks + + + + Use vision, audio, image generation, and more models + + + + Setup automatic retry strategies + + + + Load balance between various API Keys to counter rate-limits + + + + Canary test new models in production + + + + Manage AI provider keys and auth in a secure vault + + + + Easily handle unresponsive LLM requests + + + + Set usage limits based on costs incurred or tokens used + + + + Set hourly, daily, or per minute rate limits on requests or tokens sent + + + + +## Using the Gateway + +The various gateway strategies are implemented using Gateway configs. You can read more about configs below. + + + + +## Open Source + +We've open sourced our battle-tested AI gateway to the community. You can run it locally with a single command: + +```sh +npx @portkey-ai/gateway +``` +[**Contribute here**](https://github.com/portkey-ai/gateway). + +While you're here, why not [give us a star](https://git.new/ai-gateway-docs)? It helps us a lot! + + + + + +You can also [self-host](https://github.com/Portkey-AI/gateway/blob/main/docs/installation-deployments.md) the gateway and then connect it to Portkey. Please reach out on hello@portkey.ai and we'll help you set this up! diff --git a/virtual_key_old/product/ai-gateway/automatic-retries.mdx b/virtual_key_old/product/ai-gateway/automatic-retries.mdx new file mode 100644 index 00000000..d16d29da --- /dev/null +++ b/virtual_key_old/product/ai-gateway/automatic-retries.mdx @@ -0,0 +1,160 @@ +--- +title: "Automatic Retries" +description: "LLM APIs often have inexplicable failures. With Portkey, you can rescue a substantial number of your requests with our in-built automatic retries feature. " +--- + + + This feature is available on all Portkey [plans](https://portkey.ai/pricing). + + +* Automatic retries are triggered **up to 5 times** +* Retries can also be triggered only on **specific error codes** +* Each subsequent retry attempt follows **exponential backoff strategy** to prevent network overload +* Optionally respects provider's `Retry-After` headers for rate-limited requests + +## Enabling Retries + +To enable retry, just add the `retry` param to your [config object](/api-reference/config-object). + +### Retry with 5 attempts + +```JSON +{ + "retry": { + "attempts": 5 + }, + "virtual_key": "virtual-key-xxx" +} +``` + +### Retry only on specific error codes + +By default, Portkey triggers retries on the following error codes: **\[429, 500, 502, 503, 504\]** + +You can change this behaviour by setting the optional `on_status_codes` param in your retry config and manually inputting the error codes on which retry will be triggered. + + +```JSON +{ + "retry": { + "attempts": 3, + "on_status_codes": [ 408, 429, 401 ] + }, + "virtual_key": "virtual-key-xxx" +} +``` + + + If the `on_status_codes` param is present, retries will be triggered **only** on the error codes specified in that Config and not on Portkey's default error codes for retries (i.e. \[429, 500, 502, 503, 504\]) + + +### Respecting provider's retry headers + +Portkey can respect the provider's `retry-after-ms`, `x-ms-retry-after-ms` and `retry-after`response headers when encountering rate limits. This enables more intelligent retry timing based on the provider's response headers rather than using the default exponential backoff strategy. + +To enable this feature, add the `use_retry_after_headers` parameter to your retry config. By default this behaviour is disabled, and `use_retry_after_headers` is set to `false`. + +```JSON +{ + "retry": { + "attempts": 3, + "on_status_codes": [ 429 ], + "use_retry_after_headers": true + }, + "virtual_key": "virtual-key-xxx" +} +``` + + +When `use_retry_after_headers` is set to `true` and the provider includes `Retry-After` or `Retry-After-ms` headers in their response, Portkey will use these values to determine the wait time before the next retry attempt, overriding the exponential backoff strategy. + +If the provider doesn't include these headers in the response, Portkey will fall back to the standard exponential backoff strategy. + +The cumulative retry wait time for a single request is capped at 60 seconds. For example, if the first retry has a wait time of 20 seconds, and the second retry response includes a Retry-After value of 50 seconds, the request will fail since the total wait time (20+50=70) exceeds the 60-second cap. Similarly, if any single Retry-After value exceeds 60 seconds, the request will fail immediately. + + + +### Exponential backoff strategy + +When not using provider retry headers (or when they're not available), Portkey triggers retries following this exponential backoff strategy: + +| Attempt | Time out between requests | +| ----------------- | ------------------------- | +| Initial Call | Immediately | +| Retry 1st attempt | 1 second | +| Retry 2nd attempt | 2 seconds | +| Retry 3rd attempt | 4 seconds | +| Retry 4th attempt | 8 seconds | +| Retry 5th attempt | 16 seconds | + + + + + + This feature is available on all Portkey [plans](https://portkey.ai/pricing). + + +* Automatic retries are triggered **up to 5 times** +* Retries can also be triggered only on **specific error codes** +* And each subsequent retry attempt follows **exponential backoff strategy** to prevent network overload + +## Enabling Retries + +To enable retry, just add the `retry` param to your [config object](/api-reference/config-object). + +### Retry with 5 attempts + +```JSON +{ + "retry": { + "attempts": 5 + }, + "virtual_key": "virtual-key-xxx" +} +``` + +### Retry only on specific error codes + +By default, Portkey triggers retries on the following error codes: **\[429, 500, 502, 503, 504\]** + +You can change this behaviour by setting the optional `on_status_codes` param in your retry config and manually inputting the error codes on which rety will be triggered. + + +```JSON +{ + "retry": { + "attempts": 3, + "on_status_codes": [ 408, 429, 401 ] + }, + "virtual_key": "virtual-key-xxx" +} +``` + + + If the `on_status_codes` param is present, retries will be triggered **only** on the error codes specified in that Config and not on Portkey's default error codes for retries (i.e. \[429, 500, 502, 503, 504\]) + + +### Exponential backoff strategy + +Here's how Portkey triggers retries following exponential backoff: + +| Attempt | Time out between requests | +| ----------------- | ------------------------- | +| Initial Call | Immediately | +| Retry 1st attempt | 1 second | +| Retry 2nd attempt | 2 seconds | +| Retry 3rd attempt | 4 seconds | +| Retry 4th attempt | 8 seconds | +| Retry 5th attempt | 16 seconds | + +### Understanding the Retry Attempt Header + +In the response from Portkey, you can find the `x-portkey-retry-attempt-count` header which provides information about retry attempts: + +- If the value is `-1`: This means that Portkey exhausted all the retry attempts and the request was unsuccessful +- If the value is `0`: This means that there were no retries configured +- If the value is `>0`: This means that Portkey attempted retries and this is the exact number at which the request was successful + + +Currently, Portkey does not log all the retry attempts individually in the logs dashboard. Instead, the response times from all retry attempts are summed up in the single log entry. + diff --git a/virtual_key_old/product/ai-gateway/batches.mdx b/virtual_key_old/product/ai-gateway/batches.mdx new file mode 100644 index 00000000..ba602eaa --- /dev/null +++ b/virtual_key_old/product/ai-gateway/batches.mdx @@ -0,0 +1,63 @@ +--- +title: "Batches" +description: "Run batch inference with Portkey" +--- + +Portkey supports batching requests in two ways: + +1. Batching requests with Provider's batch API using unified api and provider file +2. Custom batching with Portkey's batch API + +## 1. Batching requests directly with Provider's batch API using unified api + +Portkey supports batching requests directly with Provider's batch API by using a unified api structure. +The unified api structure is similar to [OpenAI's batch API](https://platform.openai.com/docs/guides/batch). +Please refer to individual provider's documentation for more details if additional headers or parameters are required. + +| Provider | Supported Endpoints | +|---------------|-------------------------------------------| +| [OpenAI](/integrations/llms/openai/batches) | `completions`, `chat completions`, `embedding` | +| [Bedrock](/integrations/llms/bedrock/batches) | `chat completions` | +| [Azure OpenAI](/integrations/llms/azure-openai/batches) | `completions`, `chat completions`, `embedding` | +| [Vertex](/integrations/llms/vertex-ai) | `embedding`, `chat completions` | + + + +## 2. Custom batching with Portkey's batch API + +Portkey supports custom batching with Portkey's batch API in two ways. +1. Batching requests with Provider's batch API using Portkey's file. +2. Batching requests directly with Portkey's gateway using Portkey's file. + +This is controlled by `completion_window` parameter in the request. +- When `completion_window` is set to `24h`, Portkey will batch requests with Provider's batch API using Portkey's file. +- When `completion_window` is set to `immediate`, Portkey will batch requests directly with Portkey's gateway. + +Along with this, you have to set `portkey_options` which helps Portkey to batch requests to Provider's batch API or Gateway. +- This is achieved by using provider specific headers in the portkey_options + - For example, if you want to use OpenAI's batch API, you can set `portkey_options` to `{"x-portkey-virtual-key": "openai-virtual_key"}` + +### 2.1 Batching requests with Provider's batch API using Portkey's file + +`completion_window` needs to be set to `24h` and `input_file_id` needs to be Portkey's file id. Please refer to [Portkey's files](/product/ai-gateway/files) for more details. + + +- Using Portkey's file, you can upload your data to Portkey and reuse the content in your requests. +- Portkey will automatically upload the file to the Provider and reuse the content in your batch requests. +- Portkey will also automatically check the batch progress and does post batch request analysis including token and cost calculations. +- You can also get the batch output in the response using `GET /batches//output` endpoint. + +### 2.2 Batching requests directly with Portkey's gateway + +`completion_window` needs to be set to `immediate` and `input_file_id` needs to be Portkey's file id. Please refer to [Portkey's files](/product/ai-gateway/files) for more details. + + +Using this method, you can batch requests to any provider, whether they support batching or not. + +- Portkey will batch requests directly with Portkey's gateway. +- Please note the following: + - Currently, Batch Size is set to 25 by default. + - Batch Interval / Reset is set to 5 seconds (Next batch runs after 5 seconds) + - We retry requests 3 times by default (can be extended retry using `x-portkey-config` header with retry) + - We have plans to support custom batch size, batch interval and retry count in the future. +- Portkey will also automatically check the batch progress and does post batch request analysis including token and cost calculations. diff --git a/virtual_key_old/product/ai-gateway/cache-simple-and-semantic.mdx b/virtual_key_old/product/ai-gateway/cache-simple-and-semantic.mdx new file mode 100644 index 00000000..5d46c697 --- /dev/null +++ b/virtual_key_old/product/ai-gateway/cache-simple-and-semantic.mdx @@ -0,0 +1,355 @@ +--- +title: "Cache (Simple & Semantic)" +--- + + +**Simple** caching is available for all plans.
+**Semantic** caching is available for [**Production**](https://portkey.ai/pricing) and [**Enterprise**](https://portkey.ai/docs/product/enterprise-offering) users. +
+ +Speed up and save money on your LLM requests by storing past responses in the Portkey cache. There are 2 cache modes: + +* **Simple:** Matches requests verbatim. Perfect for repeated, identical prompts. Works on **all models** including image generation models. +* **Semantic:** Matches responses for requests that are semantically similar. Ideal for denoising requests with extra prepositions, pronouns, etc. Works on any model available on `/chat/completions`or `/completions` routes. + +Portkey cache serves requests upto **20x times faster** and **cheaper**. + +## Enable Cache in the Config + +To enable Portkey cache, just add the `cache` params to your [config object](/api-reference/config-object#cache-object-details). + + + Caching will not work if the `x-portkey-debug: "false"` header is included in the request + + +## Simple Cache + + +```sh +"cache": { "mode": "simple" } +``` + +### How it Works + +Simple cache performs an exact match on the input prompts. If the exact same request is received again, Portkey retrieves the response directly from the cache, bypassing the model execution. + +--- + +## Semantic Cache + + +```sh +"cache": { "mode": "semantic" } +``` + +### How it Works + +Semantic cache considers the contextual similarity between input requests. It uses cosine similarity to ascertain if the similarity between the input and a cached request exceeds a specific threshold. If the similarity threshold is met, Portkey retrieves the response from the cache, saving model execution time. Check out this [blog](https://portkey.ai/blog/reducing-llm-costs-and-latency-semantic-cache/) for more details on how we do this. + + + Semantic cache is a "superset" of both caches. Setting cache mode to "semantic" will work for when there are simple cache hits as well. + + + + To optimise for accurate cache hit rates, Semantic cache only works with requests with less than 8,191 input tokens, and with number of messages (human, assistant, system combined) less than or equal to 4. + + +### Ignoring the First Message in Semantic Cache + +When using the `/chat/completions` endpoint, Portkey requires at least **two** message objects in the `messages` array. The first message object, typically used for the `system` message, is not considered when determining semantic similarity for caching purposes. + +For example: + + +```JSON +messages = [ + { "role": "system", "content": "You are a helpful assistant" }, + { "role": "user", "content": "Who is the president of the US?" } +] +``` + +In this case, only the content of the `user` message ("Who is the president of the US?") is used for finding semantic matches in the cache. The `system` message ("You are a helpful assistant") is ignored. + +This means that even if you change the `system` message while keeping the `user` message semantically similar, Portkey will still return a semantic cache hit. + +This allows you to modify the behavior or context of the assistant without affecting the cache hits for similar user queries. + +### [Read more how to set cache in Configs](/product/ai-gateway/cache-simple-and-semantic#how-cache-works-with-configs). + +--- + +## Setting Cache Age + +You can set the age (or "ttl") of your cached response with this setting. Cache age is also set in your Config object: + +```json +"cache": { + "mode": "semantic", + "max_age": 60 +} +``` + +In this example, your cache will automatically expire after 60 seconds. Cache age is set in **seconds**. + + +* **Minimum** cache age is **60 seconds** +* **Maximum** cache age is **90 days** (i.e. **7776000** seconds) +* **Default** cache age is **7 days** (i.e. **604800** seconds) + + +--- + + +## Organization-Level Cache TTL Settings + +Organization administrators can now define the default cache TTL (Time to Live) for all API keys and workspaces within the organization. This provides centralized control over cache expiration to align with data retention policies. + +**How to Configure Organization Cache TTL** +1. Navigate to Admin Settings +2. Select Organization Properties +3. Scroll to Cache Settings +4. Enter your desired default cache TTL value (in seconds) +5. Click Save + +### How Organization-Level Cache TTL Works + +1. **Default Value**: The value set at the organization level serves as both the maximum and default TTL value. +2. **Precedence Rules**: + - If no `max_age` is specified in the request, the organization-level default value is used. + - If the `max_age` value in a request is greater than the organization-level default, the organization-level value takes precedence. + - If the `max_age` in a request is less than the organization-level default, the lower request value is honored. +3. The max value of Organisation Level Cache TTL is 25923000 seconds. + +This feature helps organizations implement consistent cache retention policies while still allowing individual requests to use shorter TTL values when needed. + + + + +## Force Refresh Cache + +Ensure that a new response is fetched and stored in the cache even when there is an existing cached response for your request. Cache force refresh can only be done **at the time of making a request**, and it is **not a part of your Config**. + +You can enable cache force refresh with this header: + +```sh +"x-portkey-cache-force-refresh": "True" +``` + + + +```sh +curl https://api.portkey.ai/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-virtual-key: open-ai-xxx" \ + -H "x-portkey-config: cache-config-xxx" \ + -H "x-portkey-cache-force-refresh: true" \ + -d '{ + "messages": [{"role": "user","content": "Hello!"}] + }' +``` + + + +```py +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY", + virtual_key="open-ai-xxx", + config="pp-cache-xxx" +) + +response = portkey.with_options( + cache_force_refresh = True +).chat.completions.create( + messages = [{ "role": 'user', "content": 'Hello!' }], + model = 'gpt-4' +) +``` + + + +```JS +import Portkey from 'portkey-ai'; + +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + config: "pc-cache-xxx", + virtualKey: "open-ai-xxx" +}) + +async function main(){ + const response = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Hello' }], + model: 'gpt-4', + }, { + cacheForceRefresh: true + }); +} + +main() +``` + + + + + + +* Cache force refresh is only activated if a cache config is **also passed** along with your request. (setting `cacheForceRefresh` as `true` without passing the relevant cache config will not have any effect) +* For requests that have previous semantic hits, force refresh is performed on ALL the semantic matches of your request. + + +--- + +## Cache Namespace: Simplified Cache Partitioning + +Portkey generally partitions the cache along all the values passed in your request header. With a custom cache namespace, you can now ignore metadata and other headers, and only partition the cache based on the custom strings that you send. + +This allows you to have finer control over your cached data and optimize your cache hit ratio. + +### How It Works + +To use Cache Namespaces, simply include the `x-portkey-cache-namespace` header in your API requests, followed by any custom string value. Portkey will then use this namespace string as the sole basis for partitioning the cache, disregarding all other headers, including metadata. + +For example, if you send the following header: + +```sh +"x-portkey-cache-namespace: user-123" +``` + +Portkey will cache the response under the namespace `user-123`, ignoring any other headers or metadata associated with the request. + + + + +```JS +import Portkey from 'portkey-ai'; + +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + config: "pc-cache-xxx", + virtualKey: "open-ai-xxx" +}) + +async function main(){ + const response = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Hello' }], + model: 'gpt-4', + }, { + cacheNamespace: 'user-123' + }); +} + +main() +``` + + + +```Python +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY", + virtual_key="open-ai-xxx", + config="pp-cache-xxx" +) + +response = portkey.with_options( + cache_namespace = "user-123" +).chat.completions.create( + messages = [{ "role": 'user', "content": 'Hello!' }], + model = 'gpt-4' +) +``` + + + +```sh +curl https://api.portkey.ai/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-virtual-key: open-ai-xxx" \ + -H "x-portkey-config: cache-config-xxx" \ + -H "x-portkey-cache-namespace: user-123" \ + -d '{ + "messages": [{"role": "user","content": "Hello!"}] + }' +``` + + +In this example, the response will be cached under the namespace `user-123`, ignoring any other headers or metadata. + +--- + +## Cache in Analytics + +Portkey shows you powerful stats on cache usage on the Analytics page. Just head over to the Cache tab, and you will see: + +* Your raw number of cache hits as well as daily cache hit rate +* Your average latency for delivering results from cache and how much time it saves you +* How much money the cache saves you + +## Cache in Logs + +On the Logs page, the cache status is updated on the Status column. You will see `Cache Disabled` when you are not using the cache, and any of `Cache Miss`, `Cache Refreshed`, `Cache Hit`, `Cache Semantic Hit` based on the cache hit status. Read more [here](/product/observability/logs). + + + + +For each request we also calculate and show the cache response time and how much money you saved with each hit. + +--- + +## How Cache works with Configs + +You can set cache at two levels: + +* **Top-level** that works across all the targets. +* **Target-level** that works when that specific target is triggered. + + + + + + +```json +{ + "cache": {"mode": "semantic", "max_age": 60}, + "strategy": {"mode": "fallback"}, + "targets": [ + {"virtual_key": "openai-key-1"}, + {"virtual_key": "openai-key-2"} + ] +} +``` + + + +```json +{ + "strategy": {"mode": "fallback"}, + "targets": [ + { + "virtual_key": "openai-key-1", + "cache": {"mode": "simple", "max_age": 200} + }, + { + "virtual_key": "openai-key-2", + "cache": {"mode": "semantic", "max_age": 100} + } + ] +} +``` + + + + + You can also set cache at **both levels (top & target).** + +In this case, the **target-level cache** setting will be **given preference** over the **top-level cache** setting. You should start getting cache hits from the second request onwards for that specific target. + + + + If any of your targets have `override_params` then cache on that target will not work until that particular combination of params is also stored with the cache. If there are **no** `override_params`for that target, then **cache will be active** on that target even if it hasn't been triggered even once. + diff --git a/virtual_key_old/product/ai-gateway/canary-testing.mdx b/virtual_key_old/product/ai-gateway/canary-testing.mdx new file mode 100644 index 00000000..15b642e6 --- /dev/null +++ b/virtual_key_old/product/ai-gateway/canary-testing.mdx @@ -0,0 +1,41 @@ +--- +title: "Canary Testing" +description: "You can use Portkey's AI gateway to also canary test new models or prompts in different environments. " +--- + +This feature is available on all Portkey [plans](https://portkey.ai/pricing). + +This uses the same techniques as [load balancing](/product/ai-gateway/load-balancing) but to achieve a different outcome. + +### Example: Test Llama2 on 5% of the traffic + +Let's take an example where we want to introduce llama2 in our systems (through Anyscale) but we're not sure of the impact. We can create a config specifically for this use case to test llama2 in production. + +The config object would look like this + + +```JSON +{ + "strategy": { + "mode": "loadbalance" + }, + "targets": [ + { + "virtual_key": "openai-virtual-key", + "weight": 0.95 + }, + { + "virtual_key": "anyscale-virtual-key", + "weight": 0.05, + "override_params": { + "model": "meta-llama/Llama-2-70b-chat-hf" + } + ] +} +``` + +Here we are telling the gateway to send 5% of the traffic to anyscale's hosted llama2-70b model. Portkey handles all the request transforms to make sure you don't have to change your code. + +You can now [use this config like this](/product/ai-gateway/configs#using-configs) in your requests. + +Once data starts flowing in, we can use Portkey's [analytics dashboards](/product/observability/analytics) to see the impact of the new model on cost, latency, errors and feedback. diff --git a/virtual_key_old/product/ai-gateway/conditional-routing.mdx b/virtual_key_old/product/ai-gateway/conditional-routing.mdx new file mode 100644 index 00000000..437c8448 --- /dev/null +++ b/virtual_key_old/product/ai-gateway/conditional-routing.mdx @@ -0,0 +1,956 @@ +--- +title: 'Conditional Routing' +--- + + +This feature is available on all Portkey [plans](https://portkey.ai/pricing). + + +Using Portkey Gateway, you can route your requests to different provider targets based on custom conditions you define. These can be conditions like: + +* If this user is on the `paid plan`, route their request to a `custom fine-tuned model` +* If the model parameter is set to `fastest`, route to `gpt-4o-mini`, if `smartest`, route to `openai o1` +* If this user is an `EU resident`, call an `EU hosted model` +* If the `temperature` parameter is above `0.7`, route to a more creative model +* If the request is coming from `testing environment` with a `llm-pass-through` flag, route it to the `cheapest model` +* ..and more! + +Using this strategy, you can set up sophisticated routing logic based on: +1. **Metadata** - Custom key-value pairs you pass with your requests +2. **Request Parameters** - Any parameter that you send in your original LLM request (like model, temperature, max_tokens) + +All routing happens very fast on the _gateway_, on _edge_. + +## Enabling Conditional Routing + +Conditional routing is one of the _strategies_ in Portkey's [Gateway Configs](/product/ai-gateway/configs). (others being `fallback` and `loadbalance`). To use it in your app, + +1. You need to create a `conditional` config in Portkey UI. +2. Save the Config and get an associated Config ID. +3. And just pass the Config ID along with your requests using the `config` param. + +## 1. Creating the `conditional` Config + +Here's how a sample `conditional` config looks (along with its simpler, tree view). + + + + ```json + { + "strategy": { + "mode": "conditional", + "conditions": [ + ...conditions + ], + "default": "target_1" + }, + "targets": [ + { + "name": "target_1", + "virtual_key":"xx" + }, + { + "name": "target_2", + "virtual_key":"yy" + } + ] + } +``` + + + ``` + โ”‚โ”€ config + โ”‚ โ”‚โ”€ strategy mode:conditional + โ”‚ โ”‚ โ”‚โ”€ conditions + โ”‚ โ”‚ โ”‚ โ”‚โ”€ array of conditions + โ”‚ โ”‚ โ”‚โ”€ default + โ”‚ โ”‚ โ”‚ โ”‚โ”€ target name + โ”‚ โ”‚โ”€ targets + โ”‚ โ”‚ โ”‚โ”€ target 1 + โ”‚ โ”‚ โ”‚ โ”‚โ”€ name + โ”‚ โ”‚ โ”‚ โ”‚โ”€ provider details + โ”‚ โ”‚ โ”‚โ”€ target 2 + โ”‚ โ”‚ โ”‚ โ”‚โ”€ name + โ”‚ โ”‚ โ”‚ โ”‚โ”€ provider details + ``` + + + +* `strategy.mode`: Set to `conditional` +* `strategy.conditions`: Query conditions with rules applied on metadata values or request parameters along with which target to call when the condition passes +* `strategy.default`: The default target name to call when none of the conditions pass +* `targets`: Array of target objects with unique `names` and provider details. These target names are referenced in the `conditions` objects above. + + +`conditions` and `default` are **required params** for the `conditional` strategy. + + +### Structure of `conditions` Object + +`conditions` are where you will actually write the routing rules. Here's a sample `condition` object: + + + + +```json + { + "query": { "metadata.user_plan": { "$eq": "paid" } }, + "then": "finetuned-gpt4" + } +``` + + + + +```json +{ + "query": { "params.model": { "$eq": "smartest" } }, + "then": "smartest-model-target" +} +``` + + + + + + + +`query`: Write the exact rule for checking metadata values or request parameters + +`then`: Define which target to call if the query `PASSES` + +### List of Condition Query Operators + +| Operator | Description | +| -------- | ------------------------ | +| `$eq` | Equals | +| `$ne` | Not equals | +| `$in` | In array | +| `$nin` | Not in array | +| `$regex` | Match the regex | +| `$gt` | Greater than | +| `$gte` | Greater than or equal to | +| `$lt` | Less than | +| `$lte` | Less than or equal to | + +### Logical Query Operators + +* `$and`: All conditions must be true +* `$or`: At least one condition must be true + +** Example Condition objects with Logical Operators** + + + + ```json + { + "$and": [ + { "metadata.user_type": { "$eq": "pro" } }, + { "metadata.model": { "$eq": "gpt-4" } } + ] + } + ``` + + + ```json + { + "$or": [ + { "params.temperature": { "$gt": 0.7 } }, + { "params.top_p": { "$lt": 0.8 } } + ] + } + ``` + + + ```json + { + "$or": [ + { + "$and": [ + { "metadata.user_type": { "$eq": "pro" } }, + { "params.model": { "$eq": "gpt-4o" } } + ] + }, + { "params.max_tokens": { "$gt": 1000 } } + ] + } + ``` + + + + +1. You can write nested queries (with `$and`, `$or` operators) +2. When a condition is incorrect or it fails, Portkey moves on to the next condition until it finds a successful condition. +3. If no conditions pass, then the `default` target name is called +4. Since Portkey iterates through the queries sequentially, the order of your conditions is important +5. When using parameter-based routing, the user must send the exact parameters expected in the conditions, or the request will give an error + + +## 2. Getting Config ID + +Based on the `conditions` and the Config structure described above, you can create your [Config in Portkey UI](https://app.portkey.ai/configs), and save it to get Config ID. The UI also helps you autofill and autoformat your Config. + + + + +## 3. Implementing Conditional Routing + +Conditional routing is enabled through Portkey's [Gateway Configs](/product/ai-gateway/configs) by following these steps: + +1. Create a conditional config in Portkey UI ([app.portkey.ai/configs](https://app.portkey.ai/configs)) +2. Save the config to get a Config ID +3. Use this Config ID in your requests + + + + + + Route requests based on the values of parameters like `model`, `temperature`, or `max_tokens` + + + Route requests based on custom metadata fields you include with your request + + +You can also combine both approaches for [more sophisticated routing logic](#combined-routing-with-multiple-conditions) + + +### Routing Based on Request Parameters + + +With conditional routing, you can route based on any parameter in your LLM request (`model`, `temperature`, `max_tokens`, etc.). + + + Portkey only supports params routing for primitive types (string, number, boolean). Complex types like arrays and objects are not supported. + + +**Example: Model Parameter Based Routing** + +This example routes based on the `model` parameter, allowing you to use aliases instead of specific model names: + +```json +{ + "strategy": { + "mode": "conditional", + "conditions": [ + { + "query": { + "params.model": { + "$eq": "fastest" + } + }, + "then": "fastest-model-target" + }, + { + "query": { + "params.model": { + "$eq": "smartest" + } + }, + "then": "smartest-model-target" + } + ], + "default": "fastest-model-target" + }, + "targets": [ + { + "name": "smartest-model-target", + "virtual_key": "anthropic-vk", + "override_params": { + "model": "claude-3.5-sonnet" + } + }, + { + "name": "fastest-model-target", + "virtual_key": "oai-vk", + "override_params": { + "model": "gpt-4o-mini" + } + } + ] +} +``` + +Using the config: + +```python +from portkey_ai import Portkey +client = Portkey( + api_key="PORTKEY_API_KEY", + config="model-routing-config-id" +) + +# This will use the "smartest" model (claude-3.5-sonnet) +completion = client.chat.completions.create( + model="smartest", # This value matches our conditional routing condition + messages=[ + {"role": "developer", "content": "You are a helpful assistant."}, + {"role": "user", "content": "Explain quantum computing to a 5-year-old"} + ] +) + +# This will use the "fastest" model (gpt-4o-mini) +completion = client.chat.completions.create( + model="fastest", # This value matches our conditional routing condition + messages=[ + {"role": "developer", "content": "You are a helpful assistant."}, + {"role": "user", "content": "What's 2+2?"} + ] +) +``` + + +When using parameter-based routing, make sure to include the parameter specified in your routing condition in your request. In the example above, you need to include `model` in your request for routing to work properly. + + + + + + + +### Routing Based on Metadata + + + + + + +Portkey support [metadata](/product/observability/metadata)-based routing, which requires sending custom metadata with your request. + +**Applying Conditional Routing Based on Metadata** + + + + + + + In this example we are routing our request based on `user_plan` metadata sent along request. If the user is on a `paid` plan, we route to a `finetuned-gpt4` model, otherwise we route to a `base-gpt4` model. + + ```json + { + "strategy": { + "mode": "conditional", + "conditions": [ + { + "query": { "metadata.user_plan": { "$eq": "paid" } }, + "then": "finetuned-gpt4" + }, + { + "query": { "metadata.user_plan": { "$eq": "free" } }, + "then": "base-gpt4" + } + ], + "default": "base-gpt4" + }, + "targets": [ + { + "name": "finetuned-gpt4", + "virtual_key":"xx", + "override_params": { + "model": "ft://gpt4-xxxxx" + } + }, + { + "name": "base-gpt4", + "virtual_key":"yy", + "override_params": { + "model": "gpt-4" + } + } + } + ``` + + + ```json + { + "strategy": { + "mode": "conditional", + "conditions": [ + { + "query": { + "$and": [ + { "metadata.user_type": { "$eq": "pro" } }, + { "metadata.user_tier": { "$eq": "tier-1" } } + ] + }, + "then": "gpt4_v2_target" + }, + { + "query": { + "$or": [ + { "metadata.client": { "$eq": "UI" } }, + { "metadata.app_name": { "$regex": "my_app" } } + ] + }, + "then": "app_target" + } + ], + "default": "default_target" + }, + "targets": [ + { "name": "gpt4_v2_target", "virtual_key": "openai-xx"}, + { "name": "app_target", "virtual_key": "openai-yy" }, + { "name": "default_target", "virtual_key": "openai-zz" } + ] + } + ``` + + + + + +Using the config: + +```python +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY", + config="my-conditional-router-config" +) + +response = portkey.with_options( + metadata = { + "user_plan": "free", + "environment": "production", + "session_id": "1729" +}).chat.completions.create( + messages = [{ "role": 'user', "content": 'What is 1729' }] +) +``` + + + + + +You can combine both metadata and request parameters in your conditions. For example, you can route based on a combination of `metadata.user_plan` and `params.model`. + + + + + +### Combined Routing with Multiple Conditions + +You can combine metadata and parameter conditions for more sophisticated routing: + +```python +from portkey_ai import Portkey + +client = Portkey( + api_key="PORTKEY_API_KEY", + config="combined-routing-config-id" +) + +# Route based on both metadata and temperature parameter +completion = client.with_options( + metadata={"user_tier": "enterprise"} +).chat.completions.create( + temperature=0.9, # High temperature will route to creative model + messages=[{"role": "user", "content": "Write a story about dragons"}] +) +``` + +Config example for combined conditions: + +```json +{ + "strategy": { + "mode": "conditional", + "conditions": [ + { + "query": { + "$and": [ + { "metadata.user_tier": { "$eq": "enterprise" } }, + { "params.temperature": { "$gte": 0.7 } } + ] + }, + "then": "creative-premium-target" + } + ], + "default": "standard-target" + }, + "targets": [ + { + "name": "creative-premium-target", + "virtual_key": "anthropic-vk", + "override_params": { "model": "claude-3-opus" } + }, + { + "name": "standard-target", + "virtual_key": "openai-vk", + "override_params": { "model": "gpt-3.5-turbo" } + } + ] +} +``` + + + + + + +## More Examples Using Conditional Routing + +Here are practical examples of how you can leverage conditional routing to handle real-world challenges in your LLM applications. These examples showcase common patterns that help optimize cost, performance, compliance, and feature deployment without changing your application code. + + + + + +Route premium users to advanced models and free users to cost-effective ones. + +```json +{ + "strategy": { + "mode": "conditional", + "conditions": [ + { + "query": { "metadata.user_tier": { "$eq": "premium" } }, + "then": "premium-model" + }, + { + "query": { "metadata.user_tier": { "$eq": "free" } }, + "then": "basic-model" + } + ], + "default": "basic-model" + }, + "targets": [ + { + "name": "premium-model", "virtual_key": "openai-vk", + "override_params": { "model": "gpt-4o" } + }, + { + "name": "basic-model", "virtual_key": "openai-vk", + "override_params": { "model": "gpt-3.5-turbo" } + } + ] +} +``` + + + +Ensure GDPR compliance by routing EU users to EU-compliant deployments. + +```json +{ + "strategy": { + "mode": "conditional", + "conditions": [ + { + "query": { "metadata.user_region": { "$eq": "EU" } }, + "then": "eu-compliant" + } + ], + "default": "standard-model" + }, + "targets": [ + { + "name": "eu-compliant", "virtual_key": "azure-eu-vk", + "override_params": { "model": "gpt-4" } + }, + { + "name": "standard-model", "virtual_key": "openai-vk", + "override_params": { "model": "gpt-4o" } + } + ] +} +``` + + + +Route requests to different models based on data sensitivity levels. + +```json +{ + "strategy": { + "mode": "conditional", + "conditions": [ + { + "query": { "metadata.data_sensitivity": { "$eq": "high" } }, + "then": "on-premises-model" + }, + { + "query": { "metadata.data_sensitivity": { "$in": ["medium", "low"] } }, + "then": "cloud-model" + } + ], + "default": "public-model" + }, + "targets": [ + { + "name": "public-model", "virtual_key": "openai-vk" }, + { + "name": "on-premises-model", "virtual_key": "private-vk" }, + { + "name": "cloud-model", "virtual_key": "azure-vk" } + ] +} +``` + + + + + + + +Use intuitive aliases instead of remembering specific model names. + +```json +{ + "strategy": { + "mode": "conditional", + "conditions": [ + { + "query": { "params.model": { "$eq": "fastest" } }, + "then": "fast-model" + }, + { + "query": { "params.model": { "$eq": "smartest" } }, + "then": "smart-model" + } + ], + "default": "balanced-model" + }, + "targets": [ + { + "name": "smart-model", "virtual_key": "anthropic-vk", + "override_params": { "model": "claude-3.5-sonnet" } + }, + { + "name": "fast-model", "virtual_key": "openai-vk", + "override_params": { "model": "gpt-4o-mini" } + }, + { + "name": "balanced-model", "virtual_key": "openai-vk", + "override_params": { "model": "gpt-4o" } + } + ] +} +``` + + + +Route creative vs factual requests to different optimized models. + +```json +{ + "strategy": { + "mode": "conditional", + "conditions": [ + { + "query": { "params.temperature": { "$gte": 0.7 } }, + "then": "creative-model" + }, + { + "query": { "params.temperature": { "$lt": 0.3 } }, + "then": "factual-model" + } + ], + "default": "balanced-model" + }, + "targets": [ + { + "name": "creative-model", "virtual_key": "anthropic-vk", + "override_params": { "model": "claude-3-sonnet" } + }, + { + "name": "factual-model", "virtual_key": "openai-vk", + "override_params": { "model": "gpt-4o" } + }, + { + "name": "balanced-model", "virtual_key": "openai-vk", + "override_params": { "model": "gpt-3.5-turbo" } + } + ] +} +``` + + + +Route high-token requests to models with larger context windows. + +```json +{ + "strategy": { + "mode": "conditional", + "conditions": [ + { + "query": { "params.max_tokens": { "$gt": 4000 } }, + "then": "high-capacity" + } + ], + "default": "standard-capacity" + }, + "targets": [ + { + "name": "high-capacity", "virtual_key": "anthropic-vk", + "override_params": { "model": "claude-3-opus" } + }, + { + "name": "standard-capacity", "virtual_key": "openai-vk", + "override_params": { "model": "gpt-4o-mini" } + } + ] +} +``` + + + + + + + +Route different tasks to specialized models for optimal results. + +```json +{ + "strategy": { + "mode": "conditional", + "conditions": [ + { + "query": { "metadata.task": { "$eq": "coding" } }, + "then": "coding-model" + }, + { + "query": { "metadata.task": { "$eq": "writing" } }, + "then": "writing-model" + } + ], + "default": "general-model" + }, + "targets": [ + { + "name": "coding-model", "virtual_key": "openai-vk", + "override_params": { "model": "gpt-4o" } + }, + { + "name": "writing-model", "virtual_key": "anthropic-vk", + "override_params": { "model": "claude-3-opus" } + }, + { + "name": "general-model", "virtual_key": "openai-vk", + "override_params": { "model": "gpt-3.5-turbo" } + } + ] +} +``` + + + +Test new models with specific user groups before full rollout. + +```json +{ + "strategy": { + "mode": "conditional", + "conditions": [ + { + "query": { "metadata.user_group": { "$eq": "beta" } }, + "then": "new-model" + } + ], + "default": "current-model" + }, + "targets": [ + { + "name": "new-model", "virtual_key": "anthropic-vk", + "override_params": { "model": "claude-3.5-sonnet" } + }, + { + "name": "current-model", "virtual_key": "openai-vk", + "override_params": { "model": "gpt-3.5-turbo" } + } + ] +} +``` + + + +Control feature rollout with feature flags in metadata. + +```json +{ + "strategy": { + "mode": "conditional", + "conditions": [ + { + "query": { "metadata.features.new_model_enabled": { "$eq": true } }, + "then": "new-model-target" + } + ], + "default": "standard-model" + }, + "targets": [ + { + "name": "new-model-target", "virtual_key": "openai-vk", + "override_params": { "model": "gpt-4o" } + }, + { + "name": "standard-model", "virtual_key": "openai-vk", + "override_params": { "model": "gpt-3.5-turbo" } + } + ] +} +``` + + + + + + +You can apply input guardrails to specific targets in your conditional routing configuration. This allows you to validate or transform the input before it's sent to the LLM: + +```json +{ + "strategy": { + "mode": "conditional", + "conditions": [ + { + "query": { + "metadata.user_plan": { "$eq": "paid" } + }, + "then": "premium-model" + }, + { + "query": { + "metadata.user_plan": { "$eq": "free" } + }, + "then": "standard-model" + } + ], + "default": "standard-model" + }, + "targets": [ + { + "name": "premium-model", + "virtual_key": "openai-vk-xxx", + "input_guardrails": ["pg-pii-detector-xxx", "pg-toxic-content-xxx"] + }, + { + "name": "standard-model", + "virtual_key": "openai-vk-yyy" + } + ] +} +``` + +In this example, when a user with the "paid" plan sends a request, it's routed to the premium model but first runs through two input guardrails: one for PII detection and another for toxic content filtering. + + + +You can also apply output guardrails to validate or transform the LLM's response: + +```json +{ + "strategy": { + "mode": "conditional", + "conditions": [ + { + "query": { + "params.temperature": { "$gte": 0.7 } + }, + "then": "creative-model" + } + ], + "default": "standard-model" + }, + "targets": [ + { + "name": "creative-model", + "virtual_key": "anthropic-vk-xxx", + "output_guardrails": [ + "pg-fact-checker-xxx", + "pg-content-moderator-xxx" + ] + }, + { + "name": "standard-model", + "virtual_key": "openai-vk-xxx" + } + ] +} +``` + +This configuration applies output guardrails to the "creative-model" target, which is used when the temperature parameter is high. The response from this model will be checked for factual accuracy and moderated for inappropriate content. + + + +For comprehensive protection, you can apply both input and output guardrails to your targets: + +```json +{ + "strategy": { + "mode": "conditional", + "conditions": [ + { + "query": { + "metadata.user_plan": { "$eq": "paid" } + }, + "then": "premium-model" + }, + { + "query": { + "metadata.user_plan": { "$eq": "free" } + }, + "then": "standard-model" + } + ], + "default": "standard-model" + }, + "targets": [ + { + "name": "premium-model", + "virtual_key": "openai-vk-xxx", + "input_guardrails": ["pg-pii-detector-xxx", "pg-toxic-content-xxx"], + "output_guardrails": ["pg-fact-checker-xxx", "pg-compliance-xxx"] + }, + { + "name": "standard-model", + "virtual_key": "openai-vk-yyy", + "input_guardrails": ["pg-basic-filter-xxx"], + "output_guardrails": ["pg-basic-moderator-xxx"] + } + ] +} +``` + +This configuration provides end-to-end protection by: +1. First checking inputs with guardrails before sending to the LLM +2. Then validating outputs with guardrails before returning to the user + +Each target can have its own set of guardrails, allowing you to apply different levels of protection based on your routing conditions. + + + + +Guardrails are referenced by their IDs in the configuration. You can create guardrails in the Portkey UI and then reference them in your conditional routing config. + +Learn more about [Portkey Guardrails](/product/guardrails) to understand how to create and manage different types of guardrails for your LLM applications. + + + + + + + + +## Important Note + +1. **Parameter Presence**: When routing based on request parameters, make sure the parameters specified in your conditions are included in your requests. + +2. **Primitive Types Only**: Parameter-based routing works with primitive data types (strings, numbers, booleans) but not with arrays or nested objects. + +3. **Order Matters**: Conditions are evaluated in the order they're defined, so place more specific conditions before more general ones. + +4. **Error Handling**: If your condition references a parameter that doesn't exist in the request, the request will fail with an error. + +5. **Multiple Parameter Types**: Portkey supports routing based on any parameter that can be sent in LLM requests including `model`, `temperature`, `top_p`, `frequency_penalty`, `presence_penalty`, `max_tokens`, and many others. + + + + + + + +[Join us on Discord](https://portkey.wiki/chat) to share your thoughts on this feature or to get help with setting up advanced routing scenarios. + diff --git a/virtual_key_old/product/ai-gateway/configs.mdx b/virtual_key_old/product/ai-gateway/configs.mdx new file mode 100644 index 00000000..3a53729c --- /dev/null +++ b/virtual_key_old/product/ai-gateway/configs.mdx @@ -0,0 +1,154 @@ +--- +title: "Configs" +description: This feature is available on all Portkey plans. +--- + +Available on all Portkey plans. + +Configs streamline your Gateway management, enabling you to programmatically control various aspects like fallbacks, load balancing, retries, caching, and more. + +A configuration is a JSON object that can be used to define routing rules for all the requests coming to your gateway. You can configure multiple configs and use them in your requests. + +## Creating Configs + +Navigate to the โ€˜Configsโ€™ page in the Portkey app and click 'Create' to start writing a new config. + +## Using Configs + +Configs are supported across all integrations. + +* Through the config parameter of the Portkey SDK client(Directly or via [frameworks](/integrations/llms)) +* Through the config headers in the OpenAI SDK +* Via the REST API through the `x-portkey-config` header + +### Applying Gateway Configs + +Gateway [configs](/product/ai-gateway/configs) allow you to unlock the gateway superpowers of Portkey. You can create a config in the UI and attach it's config id in the OpenAI client. + + + + +```js +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + config: "pc-***" // Supports a string config id or a config object +}); +``` + + + +```py +portkey = Portkey( + api_key="PORTKEY_API_KEY", + config="pc-***" # Supports a string config id or a config object +) +``` + + + +```js +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + config: "CONFIG_ID" // Fetched from the UI + }) +}); +``` + + + +```python +client = OpenAI( + api_key="OPENAI_API_KEY", # defaults to os.environ.get("OPENAI_API_KEY") + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY", # defaults to os.environ.get("PORTKEY_API_KEY") + config="CONFIG_ID" + ) +) +``` + + + +```sh +curl https://api.portkey.ai/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-provider: openai" \ + -H "x-portkey-config: $CONFIG_ID" \ + -d '{ + "model": "gpt-3.5-turbo", + "messages": [{ + "role": "user", + "content": "Hello!" + }] + }' +``` + + + +If you want to attach the configuration to only a few requests instead of modifying the client, you can send it in the request headers for OpenAI or in the config parameter while using the Portkey SDK. + +> Note: If you have a default configuration set in the client, but also include a configuration in a specific request, the request-specific configuration will take precedence and replace the default config for that particular request. + + + + +```js +portkey.chat.completions.create({ + messages: [{role: "user", content: "Say this is a test"}], + model: "gpt-3.5-turbo" +}, {config: "pc-***"}) +``` + + + +```py +portkey.with_options(config="pc-***").chat.completions.create( + messages = [{ "role": 'user', "content": 'Say this is a test' }], + model = 'gpt-3.5-turbo' +}) +``` + + + +```js +let reqHeaders = createHeaders({config: "CONFIG_ID}); +openai.chat.completions.create({ + messages: [{role: "user", content: "Say this is a test"}], + model: "gpt-3.5-turbo" +}, {headers: reqHeaders}) +``` + + + +```py +reqHeaders = createHeaders(config="CONFIG_ID") +client.with_options(headers=reqHeaders).chat.completions.create( + messages = [{ "role": 'user', "content": 'Say this is a test' }], + model = 'gpt-3.5-turbo' +}) +``` + + + + + +You can also add the config JSON as a string instead of the slug. + + +## Configs in Logs + +Portkey shows your Config usage smartly on the logs page with the **Status column** and gives you a snapshot of the Gateway activity for every request. [Read more about the status column here](https://portkey.ai/docs/product/observability/logs#request-status-guide). + +You can also see the ID of the specific Config used for a request separately in the log details, and jump into viewing/editing it directly from the log details page. + +## Config Object Documentation +Find detailed info about the Config object schema, and more examples: + + diff --git a/virtual_key_old/product/ai-gateway/fallbacks.mdx b/virtual_key_old/product/ai-gateway/fallbacks.mdx new file mode 100644 index 00000000..72fb9ab0 --- /dev/null +++ b/virtual_key_old/product/ai-gateway/fallbacks.mdx @@ -0,0 +1,90 @@ +--- +title: "Fallbacks" +--- + +This feature is available on all Portkey [plans](https://portkey.ai/pricing). + +With an array of Language Model APIs available on the market, each with its own strengths and specialties, wouldn't it be great if you could seamlessly switch between them based on their performance or availability? Portkey's Fallback capability is designed to do exactly that. The Fallback feature allows you to specify a list of providers/models in a prioritized order. If the primary LLM fails to respond or encounters an error, Portkey will automatically fallback to the next LLM in the list, ensuring your application's robustness and reliability. + + + + + +## Enabling Fallback on LLMs + +To enable fallbacks, you can modify the [config object](/api-reference/config-object) to include the `fallback` mode. + +Here's a quick example of a config to **fallback** to Anthropic's `claude-3.5-sonnet` if OpenAI's `gpt-4o` fails. + +```JSON +{ + "strategy": { + "mode": "fallback" + }, + "targets": [ + { + "virtual_key": "openai-virtual-key", + "override_params": { + "model": "gpt-4o" + } + }, + { + "virtual_key": "anthropic-virtual-key", + "override_params": { + "model": "claude-3.5-sonnet-20240620" + } + } + ] +} +``` + +In this scenario, if the OpenAI model encounters an error or fails to respond, Portkey will automatically retry the request with Anthropic. + +[Using Configs in your Requests](/product/ai-gateway/configs#using-configs) + +## Triggering fallback on specific error codes + +By default, fallback is triggered on any request that returns a **non-2xx** status code. + +You can change this behaviour by setting the optional `on_status_codes` param in your fallback config and manually inputting the status codes on which fallback will be triggered. + + +```sh +{ + "strategy": { + "mode": "fallback", + "on_status_codes": [ 429 ] + }, + "targets": [ + { + "virtual_key": "openai-virtual-key" + }, + { + "virtual_key": "azure-openai-virtual-key" + } + ] +} +``` + +Here, fallback from OpenAI to Azure OpenAI will only be triggered when there is a `429` error code from the OpenAI request (i.e. rate limiting error) + +## Tracing Fallback Requests on Portkey + +Portkey logs all the requests that are sent as a part of your fallback config. This allows you to easily trace and see which targets failed and see which ones were eventually successful. + +To see your fallback trace, + +1. On the Logs page, first filter the logs with the specific `Config ID` where you've setup the fallback - this will show all the requests that have been sent with that config. +2. Now, trace an individual request and all the failed + successful logs for it by filtering further on `Trace ID` \- this will show all the logs originating from a single request. + + + + + +## Caveats and Considerations + +While the Fallback on LLMs feature greatly enhances the reliability and resilience of your application, there are a few things to consider: + +1. Ensure the LLMs in your fallback list are compatible with your use case. Not all LLMs offer the same capabilities. +2. Keep an eye on your usage with each LLM. Depending on your fallback list, a single request could result in multiple LLM invocations. +3. Understand that each LLM has its own latency and pricing. Falling back to a different LLM could have implications on the cost and response time. diff --git a/virtual_key_old/product/ai-gateway/files.mdx b/virtual_key_old/product/ai-gateway/files.mdx new file mode 100644 index 00000000..dfa90f30 --- /dev/null +++ b/virtual_key_old/product/ai-gateway/files.mdx @@ -0,0 +1,55 @@ +--- +title: "Files" +description: "Upload files to Portkey and reuse the content in your requests" +--- + +Portkey supports managing files in two ways: + +1. Uploading and managing files to any provider using the unified signature +2. [Enterprise Only] Uploading files to Portkey and using them for batching/fine-tuning requests with any provider + +## 1. Uploading and managing files to any provider using the unified signature + +Please refer to the [Provider Specific Files](/integrations/llms/openai/files) documentation for more details. + +1. [OpenAI](/integrations/llms/openai/files) +2. [Bedrock](/integrations/llms/bedrock/files) +3. [Azure OpenAI](/integrations/llms/azure-openai/files) +4. [Fireworks](/integrations/llms/fireworks/files) +5. [Vertex](/integrations/llms/vertex-ai/files) + +## 2. [Enterprise Only] Uploading files to Portkey and using them for batching/fine-tuning requests with any provider + +With Portkey, you can upload files to Portkey and reuse them for [batching inference](/product/ai-gateway/batches) with any provider and [fine-tuning](/product/ai-gateway/fine-tuning) with supported providers. + +In this way, you can test your data with different foundation models, perform A/B testing with different foundation models, and perform batch inference with different foundation models. + +### Uploading Files + +```sh + curl --location --request POST 'https://api.portkey.ai/v1/files' \ +--header 'x-portkey-api-key: ' \ +--form 'purpose=""' \ +--form 'file=@""' +``` + +### Listing Files + +```sh +curl -X GET https://api.portkey.ai/v1/files \ + -H "Authorization: Bearer $PORTKEY_API_KEY" +``` + +### Get File + +```sh +curl -X GET https://api.portkey.ai/v1/files/{file_id} \ + -H "Authorization: Bearer $PORTKEY_API_KEY" +``` + +### Get File Content + +```sh +curl -X GET https://api.portkey.ai/v1/files/{file_id}/content \ + -H "Authorization: Bearer $PORTKEY_API_KEY" +``` \ No newline at end of file diff --git a/virtual_key_old/product/ai-gateway/fine-tuning.mdx b/virtual_key_old/product/ai-gateway/fine-tuning.mdx new file mode 100644 index 00000000..02949900 --- /dev/null +++ b/virtual_key_old/product/ai-gateway/fine-tuning.mdx @@ -0,0 +1,118 @@ +--- +title: "Fine-tuning" +description: "Run your fine-tuning jobs with Portkey Gateway" +--- + +Portkey Gateway supports fine-tuning in two ways: + +1. Fine-tuning with portkey as a provider, supports multiple providers with a single unified API. +2. Fine-tuning with Portkey as a client, supports following providers: + - [OpenAI](/integrations/llms/openai/fine-tuning) + - [Bedrock](/integrations/llms/bedrock/fine-tuning) + - [Azure OpenAI](/integrations/llms/azure-openai/fine-tuning) + - [Fireworks](/integrations/llms/fireworks/fine-tuning) + - [Vertex](/integrations/llms/vertex-ai/fine-tuning) + +## 1. Fine-tuning with Portkey as a client + +With Portkey acting as a client, gives you the following benefits: + +1. Provider specific fine-tuning. +2. More control over the fine-tuning process. +3. Unified API for all providers. + +## 2. Fine-tuning with Portkey as a provider + +With Portkey acting as provider, gives you the following benefits: + +1. Unified API for all providers +2. No need to manage multiple endpoints and keys +3. Easy to switch between providers with limited changes +4. Easier integration with Portkey's other features like batching, etc. + +## How to use fine-tuning + +Portkey supports a Unified Fine-tuning API for all providers which is based on OpenAI's Fine-tuning API. + +While the signature of API is same for all providers, there are places where a specific provider require some more information which can be passed via headers if used as a client, or by `portkey_options` params if used as a provider. + +### Fine-tuning with Portkey as a client + +**Upload a File** + +```sh +curl -X POST --header 'x-portkey-api-key: ' \ + --form 'file=@dataset.jsonl' \ + --form 'purpose=fine-tune' \ + 'https://api.portkey.ai/v1/files' +``` + +Learn more about [files](/product/ai-gateway/files) + +**Create a Fine-tuning Job** + +```sh +curl -X POST --header 'Content-Type: application/json' \ + --header 'x-portkey-api-key: ' \ + --data \ + $'{"model": "", "suffix": "", "training_file": "", "portkey_options": {"x-portkey-virtual-key": ""}, "method": {"type": "supervised", "supervised": {"hyperparameters": {"n_epochs": 1}}}}\n' \ +'https://api.portkey.ai/v1/fine_tuning/jobs' +``` + + + `portkey_options` supports all the options that are supported by each provider + in the gateway. Values can be provider specific, refer to provider's + documentation for each provider's specific options required for fine-tuning. + + +**List Fine-tuning Jobs** + +```sh +curl -X GET --header 'x-portkey-api-key: ' \ +'https://api.portkey.ai/v1/fine_tuning/jobs' +``` + +**Get Fine-tuning Job** + +```sh +curl -X GET --header 'x-portkey-api-key: ' \ +'https://api.portkey.ai/v1/fine_tuning/jobs/' +``` + +**Cancel Fine-tuning Job** + +```sh +curl -X POST --header 'x-portkey-api-key: ' \ +'https://api.portkey.ai/v1/fine_tuning/jobs//cancel' +``` + +When you use Portkey as a provider for fine-tuning, everything is managed by Portkey hosted solution, this includes job submission with provider, monitoring job status and able to use the fine-tuned model with Portkey's Prompt Playground. + + + Providers like OpenAI, Azure OpenAPI does provider a straight forward approach + to use the fine-tuned model for inference whereas providers like `Bedrock` abd + `Vertex` does require you to manually provide compute which is hard for + portkey to manage. + + +**Notes**: + +- For `Bedrock`, `model` param should point to the `modelId` from `portkey` and the `modelId` to submit to the provider should be passed via `provider_options`. Example is available below. + +```sh + curl -X POST --header 'Content-Type: application/json' \ + --header 'x-portkey-api-key: ' \ + --data \ + $'{"model": "amazon.titan-text-lite-v1:0", "suffix": "", "training_file": "", "provider_options": {"model": "amazon.titan-text-lite-v1:0:4k"}, "method": {"type": "supervised", "supervised": {"hyperparameters": {"n_epochs": 1}}}}\n' \ + 'https://api.portkey.ai/v1/fine_tuning/jobs' +``` + +> For more info about `modelId`, please refer to the provider's documentation. + +Please refer to the provider's documentation for fine-tuning with Portkey as a gateway. + +1. [OpenAI](/integrations/llms/openai/fine-tuning) +2. [Bedrock](/integrations/llms/bedrock/fine-tuning) +3. [Azure OpenAI](/integrations/llms/azure-openai/fine-tuning) +4. [Fireworks](/integrations/llms/fireworks/fine-tuning) +5. [Vertex](/integrations/llms/vertex-ai/fine-tuning) diff --git a/virtual_key_old/product/ai-gateway/load-balancing.mdx b/virtual_key_old/product/ai-gateway/load-balancing.mdx new file mode 100644 index 00000000..c6b3c6ef --- /dev/null +++ b/virtual_key_old/product/ai-gateway/load-balancing.mdx @@ -0,0 +1,61 @@ +--- +title: "Load Balancing" +description: Load Balancing feature efficiently distributes network traffic across multiple LLMs. +--- + + + This feature is available on all Portkey [plans](https://portkey.ai/pricing). + + + This ensures high availability and optimal performance of your generative AI apps, preventing any single LLM from becoming a performance bottleneck. + +## Enable Load Balancing + +To enable Load Balancing, you can modify the `config` object to include a `strategy` with `loadbalance` mode. + +Here's a quick example to **load balance 75-25** between an OpenAI and an Azure OpenAI account + +```JSON +{ + "strategy": { + "mode": "loadbalance" + }, + "targets": [ + { + "virtual_key": "openai-virtual-key", + "weight": 0.75 + }, + { + "virtual_key": "azure-virtual-key", + "weight": 0.25 + } + ] +} +``` + +### You can [create](/product/ai-gateway/configs#creating-configs) and then [use](/product/ai-gateway/configs#using-configs) the config in your requests. + +## How Load Balancing Works + +1. **Defining the Loadbalance Targets & their Weights**: You provide a list of `virtual keys` (or `provider` \+ `api_key` pairs), and assign a `weight` value to each target. The weights represent the relative share of requests that should be routed to each target. +2. **Weight Normalization**: Portkey first sums up all the weights you provided for the targets. It then divides each target's weight by the total sum to calculate the normalized weight for that target. This ensures the weights add up to 1 (or 100%), allowing Portkey to distribute the load proportionally. +For example, let's say you have three targets with weights 5, 3, and 1\. The total sum of weights is 9 (5 + 3 + 1). Portkey will then normalize the weights as follows: + * Target 1: 5 / 9 = 0.55 (55% of the traffic) + * Target 2: 3 / 9 = 0.33 (33% of the traffic) + * Target 3: 1 / 9 = 0.11 (11% of the traffic) +3. **Request Distribution**: When a request comes in, Portkey routes it to a target LLM based on the normalized weight probabilities. This ensures the traffic is distributed across the LLMs according to the specified weights. + + +* Default`weight`value is`1` +* Minimum`weight`value is`0` +* If `weight` is not set for a target, the default `weight` value (i.e. `1`) is applied. +* You can set `"weight":0` for a specific target to stop routing traffic to it without removing it from your Config + + +## Caveats and Considerations + +While the Load Balancing feature offers numerous benefits, there are a few things to consider: + +1. Ensure the LLMs in your list are compatible with your use case. Not all LLMs offer the same capabilities or respond in the same format. +2. Be aware of your usage with each LLM. Depending on your weight distribution, your usage with each LLM could vary significantly. +3. Keep in mind that each LLM has its own latency and pricing. Diversifying your traffic could have implications on the cost and response time. diff --git a/virtual_key_old/product/ai-gateway/multimodal-capabilities.mdx b/virtual_key_old/product/ai-gateway/multimodal-capabilities.mdx new file mode 100644 index 00000000..3b96e924 --- /dev/null +++ b/virtual_key_old/product/ai-gateway/multimodal-capabilities.mdx @@ -0,0 +1,26 @@ +--- +title: "Multimodal Capabilities" +--- + +This feature is available on all Portkey [plans](https://portkey.ai/pricing). + + +The Gateway is your unified interface for **multimodal models**, along with chat, text, and embedding models. + +Using the Gateway, you can call `vision`, `audio (text-to-speech & speech-to-text)`, `image generation` and other multimodal models from multiple providers (like `OpenAI`, `Anthropic`, `Stability AI`, etc.) โ€” all using the familiar OpenAI signature. + + + + + +#### Explore the AI Gateway's Multimodal capabilities below: + + + + + + + + + + diff --git a/virtual_key_old/product/ai-gateway/multimodal-capabilities/function-calling.mdx b/virtual_key_old/product/ai-gateway/multimodal-capabilities/function-calling.mdx new file mode 100644 index 00000000..a9cd1476 --- /dev/null +++ b/virtual_key_old/product/ai-gateway/multimodal-capabilities/function-calling.mdx @@ -0,0 +1,278 @@ +--- +title: "Function Calling" +description: "Portkey's AI Gateway supports function calling capabilities that many foundational model providers offer. In the API call you can describe functions and the model can choose to output text or this function name with parameters." +--- + +## Functions Usage + +Portkey supports the OpenAI signature to define functions as part of the API request. The `tools` parameter accepts functions which can be sent specifically for models that support function/tool calling. + + + + +```js +import Portkey from 'portkey-ai'; + +// Initialize the Portkey client +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key +}); + +// Generate a chat completion with streaming +async function getChatCompletionFunctions(){ + const messages = [{"role": "user", "content": "What's the weather like in Boston today?"}]; + const tools = [ + { + "type": "function", + "function": { + "name": "get_current_weather", + "description": "Get the current weather in a given location", + "parameters": { + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "The city and state, e.g. San Francisco, CA", + }, + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}, + }, + "required": ["location"], + }, + } + } + ]; + + const response = await portkey.chat.completions.create({ + model: "gpt-3.5-turbo", + messages: messages, + tools: tools, + tool_choice: "auto", + }); + + console.log(response) + +} +await getChatCompletionFunctions(); +``` + + + +```py +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Add your provider's virtual key +) + +tools = [ + { + "type": "function", + "function": { + "name": "get_current_weather", + "description": "Get the current weather in a given location", + "parameters": { + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "The city and state, e.g. San Francisco, CA", + }, + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}, + }, + "required": ["location"], + }, + } + } +] + +messages = [{"role": "user", "content": "What's the weather like in Boston today?"}] + +completion = portkey.chat.completions.create( + model="gpt-3.5-turbo", + messages=messages, + tools=tools, + tool_choice="auto" +) + +print(completion) +``` + + + +```js +import OpenAI from 'openai'; // We're using the v4 SDK +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY" // defaults to process.env["PORTKEY_API_KEY"] + }) +}); + +// Generate a chat completion with streaming +async function getChatCompletionFunctions(){ + const messages = [{"role": "user", "content": "What's the weather like in Boston today?"}]; + const tools = [ + { + "type": "function", + "function": { + "name": "get_current_weather", + "description": "Get the current weather in a given location", + "parameters": { + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "The city and state, e.g. San Francisco, CA", + }, + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}, + }, + "required": ["location"], + }, + } + } + ]; + + const response = await openai.chat.completions.create({ + model: "gpt-3.5-turbo", + messages: messages, + tools: tools, + tool_choice: "auto", + }); + + console.log(response) + +} +await getChatCompletionFunctions(); +``` + + + +```py +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY" + ) +) + +tools = [ + { + "type": "function", + "function": { + "name": "get_current_weather", + "description": "Get the current weather in a given location", + "parameters": { + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "The city and state, e.g. San Francisco, CA", + }, + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}, + }, + "required": ["location"], + }, + } + } +] + +messages = [{"role": "user", "content": "What's the weather like in Boston today?"}] + +completion = openai.chat.completions.create( + model="gpt-3.5-turbo", + messages=messages, + tools=tools, + tool_choice="auto" +) + +print(completion) +``` + + + +```sh +curl "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-provider: openai" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -d '{ + "model": "gpt-3.5-turbo", + "messages": [ + { + "role": "user", + "content": "What is the weather like in Boston?" + } + ], + "tools": [ + { + "type": "function", + "function": { + "name": "get_current_weather", + "description": "Get the current weather in a given location", + "parameters": { + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "The city and state, e.g. San Francisco, CA" + }, + "unit": { + "type": "string", + "enum": ["celsius", "fahrenheit"] + } + }, + "required": ["location"] + } + } + } + ], + "tool_choice": "auto" +}' +``` + + + +### [API Reference](/provider-endpoints/chat) + +On completion, the request will get logged in the logs UI where the tools and functions can be viewed. Portkey will automatically format the JSON blocks in the input and output which makes a great debugging experience. + + + + +## Managing Functions and Tools in Prompts + +Portkey's Prompt Library supports creating prompt templates with function/tool definitions, as well as letting you set the `tool choice` param. Portkey will also validate your tool definition on the fly, eliminating syntax errors. + + + + + +## Supported Providers and Models + +The following providers are supported for function calling with more providers getting added soon. Please raise a [request](/integrations/llms/suggest-a-new-integration) or a [PR](https://github.com/Portkey-AI/gateway/pulls) to add model or provider to the AI gateway. + +| Provider | Models | +| ------------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------- | +| [OpenAI](/integrations/llms/openai) | gpt-4 series of modelsgpt-3.5-turbo series of models | +| [Azure OpenAI](/integrations/llms/azure-openai) | gpt-4 series of modelsgpt-3.5-turbo series of models | +| [Anyscale](/integrations/llms/anyscale-llama2-mistral-zephyr) | mistralai/Mistral-7B-Instruct-v0.1 mistralai/Mixtral-8x7B-Instruct-v0.1 | +| [Together AI](/integrations/llms/together-ai) | mistralai/Mixtral-8x7B-Instruct-v0.1 mistralai/Mistral-7B-Instruct-v0.1 togethercomputer/CodeLlama-34b-Instruct | +| [Fireworks AI](/integrations/llms/fireworks) | firefunction-v1fw-function-call-34b-v0 | +| [Google Gemini](/integrations/llms/gemini) / [Vertex AI](/integrations/llms/vertex-ai) | gemini-1.0-progemini-1.0-pro-001gemini-1.5-pro-latest | + +## Cookbook + +[**Here's a detailed cookbook on function calling using Portkey.**](/guides/getting-started/function-calling) diff --git a/virtual_key_old/product/ai-gateway/multimodal-capabilities/image-generation.mdx b/virtual_key_old/product/ai-gateway/multimodal-capabilities/image-generation.mdx new file mode 100644 index 00000000..c6ea2286 --- /dev/null +++ b/virtual_key_old/product/ai-gateway/multimodal-capabilities/image-generation.mdx @@ -0,0 +1,142 @@ +--- +title: "Image Generation" +description: Portkey's AI gateway supports image generation capabilities that many foundational model providers offer. +--- + +The most common use case is that of **text-to-image** where the user sends a prompt which the image model processes and returns an image. + + + The guide for vision models is [available here](/product/ai-gateway/multimodal-capabilities/vision). + + +## Text-to-Image Usage + +Portkey supports the OpenAI signature to make text-to-image requests. + + +```js +import Portkey from 'portkey-ai'; + +// Initialize the Portkey client +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key +}); + +async function main() { + const image = await portkey.images.generate({ + model: "dall-e-3", + prompt: "Lucy in the sky with diamonds" + }); + + console.log(image.data); +} + +main(); +``` + + + +```py +from portkey_ai import Portkey +from IPython.display import display, Image + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Add your provider's virtual key +) + +image = portkey.images.generate( + model="dall-e-3", + prompt="Lucy in the sky with diamonds" +) + +# Display the image +display(Image(url=image.data[0].url)) +``` + + + +```js +import OpenAI from 'openai'; // We're using the v4 SDK +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY" // defaults to process.env["PORTKEY_API_KEY"] + }) +}); + +async function main() { + const image = await openai.images.generate({ + model: "dall-e-3", + prompt: "Lucy in the sky with diamonds" + }); + + console.log(image.data); +} + +main(); +``` + + + +```py +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders +from IPython.display import display, Image + +client = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY" + ) +) + +image = client.images.generate( + model="dall-e-3", + prompt="Lucy in the sky with diamonds", + n=1, + size="1024x1024" +) + +# Display the image +display(Image(url=image.data[0].url)) +``` + + + +```sh +curl "https://api.portkey.ai/v1/images/generations" \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-virtual-key: $OPENAI_VIRTUAL_KEY" \ + -d '{ + "model": "dall-e-3", + "prompt": "Lucy in the sky with diamonds" + }' +``` + + + +### API Reference + +[Create Image](/provider-endpoints/images/create-image) + +On completion, the request will get logged in the logs UI where the image can be viewed. + +(_Note that providers may remove the hosted image after a period of time, so some logs might only contain the url_) + + + + + +## Cookbook + +[**Here's a detailed cookbook on image generation using Portkey**](https://github.com/Portkey-AI/portkey-cookbook/blob/main/examples/image-generation.ipynb) which demonstrates the use of multiple providers and routing between them through Configs. diff --git a/virtual_key_old/product/ai-gateway/multimodal-capabilities/speech-to-text.mdx b/virtual_key_old/product/ai-gateway/multimodal-capabilities/speech-to-text.mdx new file mode 100644 index 00000000..cdc86c11 --- /dev/null +++ b/virtual_key_old/product/ai-gateway/multimodal-capabilities/speech-to-text.mdx @@ -0,0 +1,147 @@ +--- +title: "Speech-to-Text" +description: "Portkey's AI gateway supports STT models like Whisper by OpenAI." +--- + +## Transcription & Translation Usage + +Portkey supports both `Transcription` and `Translation` methods for STT models and follows the OpenAI signature where you can send the file (in `flac`, `mp3`, `mp4`, `mpeg`, `mpga`, `m4a`, `ogg`, `wav`, or `webm` formats) as part of the API request. + +Here's an example: + +OpenAI NodeJSOpenAI PythonREST + + +```js +import fs from "fs"; +import OpenAI from "openai"; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: "dummy", // We are using Virtual Key from Portkey + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "OPENAI_VIRTUAL_KEY" + }) +}); + +// Transcription + +async function transcribe() { + const transcription = await openai.audio.transcriptions.create({ + file: fs.createReadStream("/path/to/file.mp3"), + model: "whisper-1", + }); + + console.log(transcription.text); +} +transcribe(); + +// Translation + +async function translate() { + const translation = await openai.audio.translations.create({ + file: fs.createReadStream("/path/to/file.mp3"), + model: "whisper-1", + }); + console.log(translation.text); +} +translate(); +``` + + + +```py +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +client = OpenAI( + api_key="dummy" #We are using Virtual Key from Portkey + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key="PORTKEY_API_KEY", + virtual_key="OPENAI_VIRTUAL_KEY" + ) +) + +audio_file= open("/path/to/file.mp3", "rb") + +# Transcription + +transcription = client.audio.transcriptions.create( + model="whisper-1", + file=audio_file +) +print(transcription.text) + +# Translation + +translation = client.audio.translations.create( + model="whisper-1", + file=audio_file +) +print(translation.text) +``` + + + +```python +from pathlib import Path +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Add your provider's virtual key +) +audio_file= open("/path/to/file.mp3", "rb") + +# Transcription +transcription = portkey.audio.transcriptions.create( + model="whisper-1", + file=audio_file +) + +print(transcription.text) +# Translation +translation = portkey.audio.translations.create( + model="whisper-1", + file=audio_file +) +print(translation.text) +``` + + + +For Transcriptions: +```sh +curl "https://api.portkey.ai/v1/audio/transcriptions" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-provider: openai" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H 'Content-Type: multipart/form-data' \ + --form file=@/path/to/file/audio.mp3 \ + --form model=whisper-1 +``` +For Translations: +```sh +curl "https://api.portkey.ai/v1/audio/translations" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-provider: openai" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H 'Content-Type: multipart/form-data' \ + --form file=@/path/to/file/audio.mp3 \ + --form model=whisper-1 +``` + + +On completion, the request will get logged in the logs UI where you can see trasncribed or translated text, along with the cost and latency incurred. + +## Supported Providers and Models + +The following providers are supported for speech-to-text with more providers getting added soon. Please raise a [request](/integrations/llms/suggest-a-new-integration) or a [PR](https://github.com/Portkey-AI/gateway/pulls) to add model or provider to the AI gateway. + +| Provider | Models | Functions | +| ---------------------------------------- | --------- | ------------------------- | +| [OpenAI](/integrations/llms/openai) | whisper-1 | Transcription Translation | diff --git a/virtual_key_old/product/ai-gateway/multimodal-capabilities/text-to-speech.mdx b/virtual_key_old/product/ai-gateway/multimodal-capabilities/text-to-speech.mdx new file mode 100644 index 00000000..18daa51c --- /dev/null +++ b/virtual_key_old/product/ai-gateway/multimodal-capabilities/text-to-speech.mdx @@ -0,0 +1,130 @@ +--- +title: "Text-to-Speech" +description: "Portkey's AI gateway currently supports text-to-speech models on `OpenAI` and `Azure OpenAI`." +--- + +## Usage + +We follow the OpenAI signature where you can send the input text and the voice option as a part of the API request. All the output formats `mp3`, `opus`, `aac`, `flac`, and `pcm` are supported. Portkey also supports real time audio streaming for TTS models. + +Here's an example: + + + + +```js +import fs from "fs"; +import path from "path"; +import OpenAI from "openai"; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: "dummy", // We are using Virtual Key from Portkey + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "OPENAI_VIRTUAL_KEY" + }) +}); + +const speechFile = path.resolve("./speech.mp3"); + +async function main() { + const mp3 = await openai.audio.speech.create({ + model: "tts-1", + voice: "alloy", + input: "Today is a wonderful day to build something people love!", + }); + const buffer = Buffer.from(await mp3.arrayBuffer()); + await fs.promises.writeFile(speechFile, buffer); +} + +main(); +``` + + + +```py +from pathlib import Path +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +client = OpenAI( + api_key="dummy" #We are using Virtual Key from Portkey + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key="PORTKEY_API_KEY", + virtual_key="OPENAI_VIRTUAL_KEY" + ) +) + +speech_file_path = Path(__file__).parent / "speech.mp3" + +response = client.audio.speech.create( + model="tts-1", + voice="alloy", + input="Today is a wonderful day to build something people love!" +) + +f = open(speech_file_path, "wb") +f.write(response.content) +f.close() +``` + + + + +```python +from pathlib import Path +from portkey_ai import Portkey + +# Initialize the Portkey client + +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Add your provider's virtual key +) + +speech_file_path = Path(__file__).parent / "speech.mp3" + +response = portkey.audio.speech.create( + model="tts-1", + voice="alloy", + input="Today is a wonderful day to build something people love!" +) + +f = open(speech_file_path, "wb") +f.write(response.content) +f.close() +``` + + + +```sh +curl "https://api.portkey.ai/v1/audio/speech" \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-provider: openai" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -d '{ + "model": "tts-1", + "input": "Today is a wonderful day to build something people love!", + "voice": "alloy" + }' \ + --output speech.mp3 +``` + + + +On completion, the request will get logged in the logs UI and show the cost and latency incurred. + +## Supported Providers and Models + +The following providers are supported for text-to-speech with more providers getting added soon. Please raise a [request](/integrations/llms/suggest-a-new-integration) or a [PR](https://github.com/Portkey-AI/gateway/pulls) to add model or provider to the AI gateway. + +| Provider | Models | +| ---------------------------------------- | -------------- | +| [OpenAI](/integrations/llms/openai) | tts-1 tts-1-hd | +| [Azure OpenAI](/integrations/llms/azure-openai) | tts-1 tts-1-hd | +| Deepgram (Coming Soon) | | +| ElevanLabs (Coming Soon) | | diff --git a/virtual_key_old/product/ai-gateway/multimodal-capabilities/thinking-mode.mdx b/virtual_key_old/product/ai-gateway/multimodal-capabilities/thinking-mode.mdx new file mode 100644 index 00000000..afc843c3 --- /dev/null +++ b/virtual_key_old/product/ai-gateway/multimodal-capabilities/thinking-mode.mdx @@ -0,0 +1,538 @@ +--- +title: "Thinking Mode" +--- + +Thinking/Reasoning models represent a new generation of LLMs specifically trained to expose their internal reasoning process. Unlike traditional LLMs that only show final outputs, thinking models like Claude 3.7 Sonnet, OpenAI o1/o3, and Deepseek R1 are designed to "think out loud" - producing a detailed chain of thought before delivering their final response. + +These reasoning-optimized models are built to excel in tasks requiring complex analysis, multi-step problem solving, and structured logical thinking. Portkey makes these advanced models accessible through a unified API specification that works consistently across providers. + +## Supported Thinking Models + +Portkey currently supports the following thinking-enabled models: + +- **Anthropic**: `claude-3-7-sonnet-latest` +- **Google Vertex AI**: `anthropic.claude-3-7-sonnet@20250219` +- **Amazon Bedrock**: `claude-3-7-sonnet` + + +More thinking models will be supported as they become available. + + +## Using Thinking Mode + +1. You must set `strict_open_ai_compliance=False` in your headers or client configuration +2. The thinking response is returned in a different format than standard completions +3. For streaming responses, the thinking content is in `response_chunk.choices[0].delta.content_blocks` + + +Extended thinking API through Portkey is currently in beta. + + + +### Basic Example + + + + ```python + from portkey_ai import Portkey + + # Initialize the Portkey client + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + strict_open_ai_compliance=False # Required for thinking mode + ) + + # Create the request + response = portkey.chat.completions.create( + model="claude-3-7-sonnet-latest", + max_tokens=3000, + thinking={ + "type": "enabled", + "budget_tokens": 2030 # Maximum tokens to use for thinking + }, + stream=False, + messages=[ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "when does the flight from new york to bengaluru land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + } + ] + ) + print(response) + + # For streaming responses, handle content_blocks differently + # response = portkey.chat.completions.create( + # ...same config as above but with stream=True + # ) + # for chunk in response: + # if chunk.choices[0].delta: + # content_blocks = chunk.choices[0].delta.get("content_blocks") + # if content_blocks is not None: + # for content_block in content_blocks: + # print(content_block) + ``` + + + ```javascript + import Portkey from 'portkey-ai'; + + // Initialize the Portkey client + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY", // Add your provider's virtual key + strictOpenAiCompliance: false // Required for thinking mode + }); + + // Generate a chat completion + async function getChatCompletionWithThinking() { + const response = await portkey.chat.completions.create({ + model: "claude-3-7-sonnet-latest", + max_tokens: 3000, + thinking: { + type: "enabled", + budget_tokens: 2030 + }, + stream: false, + messages: [ + { + role: "user", + content: [ + { + type: "text", + text: "when does the flight from new york to bengaluru land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + } + ] + }); + console.log(response); + + // For streaming responses: + // const response = await portkey.chat.completions.create({ + // ...same config as above but with stream: true + // }); + // for await (const chunk of response) { + // if (chunk.choices[0].delta?.content_blocks) { + // for (const contentBlock of chunk.choices[0].delta.content_blocks) { + // console.log(contentBlock); + // } + // } + // } + } + + // Call the function + getChatCompletionWithThinking(); + ``` + + + ```javascript + import OpenAI from 'openai'; // We're using the v4 SDK + import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + + const openai = new OpenAI({ + apiKey: 'ANTHROPIC_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "anthropic", + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + strictOpenAiCompliance: false + }) + }); + + // Generate a chat completion with thinking mode + async function getChatCompletionFunctions(){ + const response = await openai.chat.completions.create({ + model: "claude-3-7-sonnet-latest", + max_tokens: 3000, + thinking: { + type: "enabled", + budget_tokens: 2030 + }, + stream: false, + messages: [ + { + role: "user", + content: [ + { + type: "text", + text: "when does the flight from new york to bengaluru land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + } + ], + }); + + console.log(response) + } + await getChatCompletionFunctions(); + ``` + + + ```python + from openai import OpenAI + from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + + openai = OpenAI( + api_key='ANTHROPIC_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="anthropic", + api_key="PORTKEY_API_KEY", + strict_open_ai_compliance=False + ) + ) + + response = openai.chat.completions.create( + model="claude-3-7-sonnet-latest", + max_tokens=3000, + thinking={ + "type": "enabled", + "budget_tokens": 2030 + }, + stream=False, + messages=[ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "when does the flight from new york to bengaluru land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + } + ] + ) + + print(response) + ``` + + + ```sh + curl "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-provider: anthropic" \ + -H "x-api-key: $ANTHROPIC_API_KEY" \ + -H "x-portkey-strict-open-ai-compliance: false" \ + -d '{ + "model": "claude-3-7-sonnet-latest", + "max_tokens": 3000, + "thinking": { + "type": "enabled", + "budget_tokens": 2030 + }, + "stream": false, + "messages": [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "when does the flight from new york to bengaluru land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + } + ] + }' + ``` + + + +## Multi-Turn Conversations + +For multi-turn conversations, include the previous thinking content in the conversation history: + + + ```py Python + from portkey_ai import Portkey + + # Initialize the Portkey client + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY", # Add your provider's virtual key + strict_open_ai_compliance=False + ) + + # Create the request + response = portkey.chat.completions.create( + model="claude-3-7-sonnet-latest", + max_tokens=3000, + thinking={ + "type": "enabled", + "budget_tokens": 2030 + }, + stream=False, + messages=[ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "when does the flight from baroda to bangalore land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + }, + { + "role": "assistant", + "content": [ + { + "type": "thinking", + "thinking": "The user is asking several questions about a flight from Baroda (also known as Vadodara) to Bangalore:\n1. When does the flight land tomorrow\n2. What time does it land\n3. What is the flight number\n4. What is the baggage belt number at the arrival airport\n\nTo properly answer these questions, I would need access to airline flight schedules and airport information systems. However, I don't have:\n- Real-time or scheduled flight information\n- Access to airport baggage claim allocation systems\n- Information about specific flights between these cities\n- The ability to look up tomorrow's specific flight schedules\n\nThis question requires current, specific flight information that I don't have access to. Instead of guessing or providing potentially incorrect information, I should explain this limitation and suggest ways the user could find this information.", + "signature": "EqoBCkgIARABGAIiQBVA7FBNLRtWarDSy9TAjwtOpcTSYHJ+2GYEoaorq3V+d3eapde04bvEfykD/66xZXjJ5yyqogJ8DEkNMotspRsSDKzuUJ9FKhSNt/3PdxoMaFZuH+1z1aLF8OeQIjCrA1+T2lsErrbgrve6eDWeMvP+1sqVqv/JcIn1jOmuzrPi2tNz5M0oqkOO9txJf7QqEPPw6RG3JLO2h7nV1BMN6wE=" + } + ] + }, + { + "role": "user", + "content": "thanks that's good to know, how about to chennai?" + } + ] + ) + print(response) + ``` + ```ts NodeJS + import Portkey from 'portkey-ai'; + + // Initialize the Portkey client + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY", // Add your anthropic's virtual key + strictOpenAiCompliance: false + }); + + // Generate a chat completion + async function getChatCompletionFunctions() { + const response = await portkey.chat.completions.create({ + model: "claude-3-7-sonnet-latest", + max_tokens: 3000, + thinking: { + type: "enabled", + budget_tokens: 2030 + }, + stream: false, + messages: [ + { + role: "user", + content: [ + { + type: "text", + text: "when does the flight from baroda to bangalore land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + }, + { + role: "assistant", + content: [ + { + type: "thinking", + thinking: "The user is asking several questions about a flight from Baroda (also known as Vadodara) to Bangalore:\n1. When does the flight land tomorrow\n2. What time does it land\n3. What is the flight number\n4. What is the baggage belt number at the arrival airport\n\nTo properly answer these questions, I would need access to airline flight schedules and airport information systems. However, I don't have:\n- Real-time or scheduled flight information\n- Access to airport baggage claim allocation systems\n- Information about specific flights between these cities\n- The ability to look up tomorrow's specific flight schedules\n\nThis question requires current, specific flight information that I don't have access to. Instead of guessing or providing potentially incorrect information, I should explain this limitation and suggest ways the user could find this information.", + signature: "EqoBCkgIARABGAIiQBVA7FBNLRtWarDSy9TAjwtOpcTSYHJ+2GYEoaorq3V+d3eapde04bvEfykD/66xZXjJ5yyqogJ8DEkNMotspRsSDKzuUJ9FKhSNt/3PdxoMaFZuH+1z1aLF8OeQIjCrA1+T2lsErrbgrve6eDWeMvP+1sqVqv/JcIn1jOmuzrPi2tNz5M0oqkOO9txJf7QqEPPw6RG3JLO2h7nV1BMN6wE=" + } + ] + }, + { + role: "user", + content: "thanks that's good to know, how about to chennai?" + } + ] + }); + console.log(response); + } + // Call the function + getChatCompletionFunctions(); + ``` + ```js OpenAI NodeJS + import OpenAI from 'openai'; // We're using the v4 SDK + import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + + const openai = new OpenAI({ + apiKey: 'ANTHROPIC_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "anthropic", + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + strict_open_ai_compliance: false + }) + }); + + // Generate a chat completion with streaming + async function getChatCompletionFunctions(){ + const response = await openai.chat.completions.create({ + model: "claude-3-7-sonnet-latest", + max_tokens: 3000, + thinking: { + type: "enabled", + budget_tokens: 2030 + }, + stream: false, + messages: [ + { + role: "user", + content: [ + { + type: "text", + text: "when does the flight from baroda to bangalore land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + }, + { + role: "assistant", + content: [ + { + type: "thinking", + thinking: "The user is asking several questions about a flight from Baroda (also known as Vadodara) to Bangalore:\n1. When does the flight land tomorrow\n2. What time does it land\n3. What is the flight number\n4. What is the baggage belt number at the arrival airport\n\nTo properly answer these questions, I would need access to airline flight schedules and airport information systems. However, I don't have:\n- Real-time or scheduled flight information\n- Access to airport baggage claim allocation systems\n- Information about specific flights between these cities\n- The ability to look up tomorrow's specific flight schedules\n\nThis question requires current, specific flight information that I don't have access to. Instead of guessing or providing potentially incorrect information, I should explain this limitation and suggest ways the user could find this information.", + signature: "EqoBCkgIARABGAIiQBVA7FBNLRtWarDSy9TAjwtOpcTSYHJ+2GYEoaorq3V+d3eapde04bvEfykD/66xZXjJ5yyqogJ8DEkNMotspRsSDKzuUJ9FKhSNt/3PdxoMaFZuH+1z1aLF8OeQIjCrA1+T2lsErrbgrve6eDWeMvP+1sqVqv/JcIn1jOmuzrPi2tNz5M0oqkOO9txJf7QqEPPw6RG3JLO2h7nV1BMN6wE=" + } + ] + }, + { + role: "user", + content: "thanks that's good to know, how about to chennai?" + } + ], + }); + + console.log(response) + + } + await getChatCompletionFunctions(); + ``` + ```py OpenAI Python + from openai import OpenAI + from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + + openai = OpenAI( + api_key='Anthropic_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="anthropic", + api_key="PORTKEY_API_KEY", + strict_open_ai_compliance=False + ) + ) + + + response = openai.chat.completions.create( + model="claude-3-7-sonnet-latest", + max_tokens=3000, + thinking={ + "type": "enabled", + "budget_tokens": 2030 + }, + stream=False, + messages=[ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "when does the flight from baroda to bangalore land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + }, + { + "role": "assistant", + "content": [ + { + "type": "thinking", + "thinking": "The user is asking several questions about a flight from Baroda (also known as Vadodara) to Bangalore:\n1. When does the flight land tomorrow\n2. What time does it land\n3. What is the flight number\n4. What is the baggage belt number at the arrival airport\n\nTo properly answer these questions, I would need access to airline flight schedules and airport information systems. However, I don't have:\n- Real-time or scheduled flight information\n- Access to airport baggage claim allocation systems\n- Information about specific flights between these cities\n- The ability to look up tomorrow's specific flight schedules\n\nThis question requires current, specific flight information that I don't have access to. Instead of guessing or providing potentially incorrect information, I should explain this limitation and suggest ways the user could find this information.", + signature: "EqoBCkgIARABGAIiQBVA7FBNLRtWarDSy9TAjwtOpcTSYHJ+2GYEoaorq3V+d3eapde04bvEfykD/66xZXjJ5yyqogJ8DEkNMotspRsSDKzuUJ9FKhSNt/3PdxoMaFZuH+1z1aLF8OeQIjCrA1+T2lsErrbgrve6eDWeMvP+1sqVqv/JcIn1jOmuzrPi2tNz5M0oqkOO9txJf7QqEPPw6RG3JLO2h7nV1BMN6wE=" + } + ] + }, + { + "role": "user", + "content": "thanks that's good to know, how about to chennai?" + } + ] + ) + + print(response) + ``` + ```sh cURL + curl "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-provider: anthropic" \ + -H "x-api-key: $ANTHROPIC_API_KEY" \ + -H "x-portkey-strict-open-ai-compliance: false" \ + -d '{ + "model": "claude-3-7-sonnet-latest", + "max_tokens": 3000, + "thinking": { + "type": "enabled", + "budget_tokens": 2030 + }, + "stream": false, + "messages": [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "when does the flight from baroda to bangalore land tomorrow, what time, what is its flight number, and what is its baggage belt?" + } + ] + }, + { + "role": "assistant", + "content": [ + { + "type": "thinking", + "thinking": "The user is asking several questions about a flight from Baroda (also known as Vadodara) to Bangalore:\n1. When does the flight land tomorrow\n2. What time does it land\n3. What is the flight number\n4. What is the baggage belt number at the arrival airport\n\nTo properly answer these questions, I would need access to airline flight schedules and airport information systems. However, I don't have:\n- Real-time or scheduled flight information\n- Access to airport baggage claim allocation systems\n- Information about specific flights between these cities\n- The ability to look up tomorrow's specific flight schedules\n\nThis question requires current, specific flight information that I don't have access to. Instead of guessing or providing potentially incorrect information, I should explain this limitation and suggest ways the user could find this information.", + "signature": "EqoBCkgIARABGAIiQBVA7FBNLRtWarDSy9TAjwtOpcTSYHJ+2GYEoaorq3V+d3eapde04bvEfykD/66xZXjJ5yyqogJ8DEkNMotspRsSDKzuUJ9FKhSNt/3PdxoMaFZuH+1z1aLF8OeQIjCrA1+T2lsErrbgrve6eDWeMvP+1sqVqv/JcIn1jOmuzrPi2tNz5M0oqkOO9txJf7QqEPPw6RG3JLO2h7nV1BMN6wE=" + } + ] + }, + { + "role": "user", + "content": "thanks that's good to know, how about to chennai?" + } + ] + }' + ``` + + + +## Understanding Response Format + +When using thinking-enabled models, be aware of the special response format: + + +The assistant's thinking response is returned in the `response_chunk.choices[0].delta.content_blocks` array, not the `response.choices[0].message.content` string. + + +This is especially important for streaming responses, where you'll need to specifically parse and extract the thinking content from the content blocks. + +## When to Use Thinking Models + +Thinking models are particularly valuable in specific use cases: + + + +## FAQs + + + + No, thinking mode is only available on specific reasoning-optimized models. Currently, this includes Claude 3.7 Sonnet and will expand to other models as they become available. + + + + Yes, enabling thinking mode will increase your token usage since the model is generating additional content for its reasoning process. The `budget_tokens` parameter lets you control the maximum tokens allocated to thinking. + + + + Yes, particularly for streaming responses. The thinking content is returned in the `content_blocks` array rather than the standard content field, so you'll need to adapt your response parsing logic. + + + + The thinking mode response format extends beyond the standard OpenAI completion schema. Setting `strict_open_ai_compliance` to false allows Portkey to return this extended format with the thinking content. + + + diff --git a/virtual_key_old/product/ai-gateway/multimodal-capabilities/vision.mdx b/virtual_key_old/product/ai-gateway/multimodal-capabilities/vision.mdx new file mode 100644 index 00000000..4a6a45c7 --- /dev/null +++ b/virtual_key_old/product/ai-gateway/multimodal-capabilities/vision.mdx @@ -0,0 +1,218 @@ +--- +title: "Vision" +description: "Portkey's AI gateway supports vision models like GPT-4V by OpenAI, Gemini by Google and more." +--- + + +**What are vision models?** + +Vision models are artificial intelligence systems that combine both vision and language modalities to process images and natural language text. These models are typically trained on large image and text datasets with different structures based on the pre-training objective. + + +## Vision Chat Completion Usage + +Portkey supports the OpenAI signature to define messages with images as part of the API request. Images are made available to the model in two main ways: by passing a link to the image or by passing the base64 encoded image directly in the request. + +Here's an example using OpenAI's `gpt-4o` model + + + + +```js +import Portkey from 'portkey-ai'; + +// Initialize the Portkey client +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key + virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key +}); + +// Generate a chat completion with streaming +async function getChatCompletionFunctions(){ + const response = await portkey.chat.completions.create({ + model: "gpt-4o-mini", + messages: [{ + role: "user", + content: [ + { type: "text", text: "What is in this image?" }, + { + type: "image_url", + image_url: { + url: "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg", + }, + }, + ], + }], + }); + + console.log(response) + +} +await getChatCompletionFunctions(); +``` + + + +```py +from portkey_ai import Portkey + +# Initialize the Portkey client +portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY" # Add your provider's virtual key +) + +response = client.chat.completions.create( + model="gpt-4o", + messages=[{ + "role": "user", + "content": [ + {"type": "text", "text": "What's in this image?"}, + { + "type": "image_url", + "image_url": { + "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg", + }, + }, + ], + }], + ) + +print(response) +``` + + + +```js +import OpenAI from 'openai'; // We're using the v4 SDK +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY" // defaults to process.env["PORTKEY_API_KEY"] + }) +}); + +// Generate a chat completion with streaming +async function getChatCompletionFunctions(){ + const response = await openai.chat.completions.create({ + model: "gpt-4o-mini", + messages: [{ + role: "user", + content: [ + { type: "text", text: "What is in this image?" }, + { + type: "image_url", + image_url: { + url: "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg", + }, + }, + ], + }], + }); + + console.log(response) + +} +await getChatCompletionFunctions(); +``` + + + +```py +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY" + ) +) + +response = openai.chat.completions.create( + model="gpt-4o-mini", + messages=[{ + "role": "user", + "content": [ + {"type": "text", "text": "What's in this image?"}, + { + "type": "image_url", + "image_url": { + "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg", + }, + }, + ], + }], +) + + +print(resonse) +``` + + + +```sh +curl "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-provider: openai" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -d '{ + "model": "gpt-4o-mini", + "messages": [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "What is in this image?" + }, + { + "type": "image_url", + "image_url": { + "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg" + } + } + ] + } + ], + "max_tokens": 300 + }' +``` + + + +### [API Reference](/product/ai-gateway/multimodal-capabilities/vision#vision-chat-completion-usage) + +On completion, the request will get logged in the logs UI where any image inputs or outputs can be viewed. Portkey will automatically load the image URLs or the base64 images making for a great debugging experience with vision models. + + + + + +## Creating prompt templates for vision models + +Portkey's prompt library supports creating templates with image inputs. If the same image will be used in all prompt calls, you can save it as part of the template's image URL itself. Or, if the image will be sent via the API as a variable, add a variable to the image link. + + + + +## Supported Providers and Models + +Portkey supports all vision models from its integrated providers as they become available. The table below shows some examples of supported vision models. Please raise a [request](/integrations/llms/suggest-a-new-integration) or a [PR](https://github.com/Portkey-AI/gateway/pulls) to add a provider to the AI gateway. + +| Provider | Models | Functions | +| ---------------------------------------------------- | -------------------------------------------------------------------------------------------- | ---------------------- | +| [OpenAI](/integrations/llms/openai) | `gpt-4-vision-preview`, `gpt-4o`, `gpt-4o-mini ` | Create Chat Completion | +| [Azure OpenAI](/integrations/llms/azure-openai) | `gpt-4-vision-preview`, `gpt-4o`, `gpt-4o-mini ` | Create Chat Completion | +| [Gemini](/integrations/llms/gemini) | `gemini-1.0-pro-vision `, `gemini-1.5-flash`, `gemini-1.5-flash-8b`, `gemini-1.5-pro` | Create Chat Completion | +| [Anthropic](/integrations/llms/anthropic) | `claude-3-sonnet`, `claude-3-haiku`, `claude-3-opus`, `claude-3.5-sonnet`, `claude-3.5-haiku` | Create Chat Completion | +| [AWS Bedrock](/integrations/llms/aws-bedrock) | `anthropic.claude-3-5-sonnet anthropic.claude-3-5-haiku anthropic.claude-3-5-sonnet-20240620-v1:0` | Create Chat Completion | + +For a complete list of all supported provider (including non-vision LLMs), check out our [providers documentation](/integrations/llms). diff --git a/virtual_key_old/product/ai-gateway/realtime-api.mdx b/virtual_key_old/product/ai-gateway/realtime-api.mdx new file mode 100644 index 00000000..9889dff1 --- /dev/null +++ b/virtual_key_old/product/ai-gateway/realtime-api.mdx @@ -0,0 +1,187 @@ +--- +title: "Realtime API" +description: "Use OpenAI's Realtime API with logs, cost tracking, and more!" +--- + + + This feature is available on all Portkey [plans](https://portkey.ai/pricing). + + +[OpenAI's Realtime API](https://platform.openai.com/docs/guides/realtime) while the fastest way to use multi-modal generation, presents its own set of problems around logging, cost tracking and guardrails. + +Portkey's AI Gateway provides a solution to these problems with a seamless integration. Portkeys logging is unique in that it captures the entire request and response, including the model's response, cost, and guardrail violations. + +### Here's how to get started: + + + + ```python +from portkey_ai import AsyncPortkey as Portkey, PORTKEY_GATEWAY_URL +import asyncio + +async def main(): + client = Portkey( + api_key="PORTKEY_API_KEY", + virtual_key="VIRTUAL_KEY", + base_url=PORTKEY_GATEWAY_URL, + ) + + async with client.beta.realtime.connect(model="gpt-4o-realtime-preview-2024-10-01") as connection: #replace with the model you want to use + await connection.session.update(session={'modalities': ['text']}) + + await connection.conversation.item.create( + item={ + "type": "message", + "role": "user", + "content": [{"type": "input_text", "text": "Say hello!"}], + } + ) + await connection.response.create() + + async for event in connection: + if event.type == 'response.text.delta': + print(event.delta, flush=True, end="") + + elif event.type == 'response.text.done': + print() + + elif event.type == "response.done": + break + +asyncio.run(main()) + ``` + + + ```javascript + // coming soon + ``` + + + ```javascript +// requires `yarn add ws @types/ws` +import OpenAI from 'openai'; +import { OpenAIRealtimeWS } from 'openai/beta/realtime/ws'; +import { createHeaders, PORTKEY_GATEWAY_URL } from 'portkey-ai'; + +const headers = createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY", + virtualKey: 'VIRTUAL_KEY' + }) +const openai = new OpenAI({ + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: headers + }); +const rt = new OpenAIRealtimeWS({ model: 'gpt-4o-realtime-preview-2024-12-17', options: {headers: headers} }, openai); + +// access the underlying `ws.WebSocket` instance +rt.socket.on('open', () => { + console.log('Connection opened!'); + rt.send({ + type: 'session.update', + session: { + modalities: ['text'], + model: 'gpt-4o-realtime-preview', + }, + }); + + rt.send({ + type: 'conversation.item.create', + item: { + type: 'message', + role: 'user', + content: [{ type: 'input_text', text: 'Say a couple paragraphs!' }], + }, + }); + + rt.send({ type: 'response.create' }); +}); + +rt.on('error', (err) => { + // in a real world scenario this should be logged somewhere as you + // likely want to continue procesing events regardless of any errors + throw err; +}); + +rt.on('session.created', (event) => { + console.log('session created!', event.session); + console.log(); +}); + +rt.on('response.text.delta', (event) => process.stdout.write(event.delta)); +rt.on('response.text.done', () => console.log()); + +rt.on('response.done', () => rt.close()); + +rt.socket.on('close', () => console.log('\nConnection closed!')); + ``` + + + ```python +import asyncio +from openai import AsyncOpenAI +from portkey_ai import createHeaders, PORTKEY_GATEWAY_URL + +async def main(): + headers = createHeaders(provider="openai", api_key="PORTKEY_API_KEY", virtual_key="VIRTUAL_KEY") + client = AsyncOpenAI( + base_url=PORTKEY_GATEWAY_URL, + ) + + async with client.beta.realtime.connect(model="gpt-4o-realtime-preview", extra_headers=headers) as connection: #replace with the model you want to use + await connection.session.update(session={'modalities': ['text']}) + + await connection.conversation.item.create( + item={ + "type": "message", + "role": "user", + "content": [{"type": "input_text", "text": "Say hello!"}], + } + ) + await connection.response.create() + + async for event in connection: + if event.type == 'response.text.delta': + print(event.delta, flush=True, end="") + + elif event.type == 'response.text.done': + print() + + elif event.type == "response.done": + break + +asyncio.run(main()) + ``` + + + ```sh +# we're using websocat for this example, but you can use any websocket client +websocat "wss://api.portkey.ai/v1/realtime?model=gpt-4o-realtime-preview-2024-10-01" \ + -H "x-portkey-provider: openai" \ + -H "x-portkey-virtual-key: VIRTUAL_KEY" \ + -H "x-portkey-OpenAI-Beta: realtime=v1" \ + -H "x-portkey-api-key: PORTKEY_API_KEY" +# once connected, you can send your messages as you would with OpenAI's Realtime API + ``` + + + +For advanced use cases, you can use configs (https://portkey.ai/docs/product/ai-gateway/configs#configs) + +If you would not like to store your API Keys with Portkey, you can pass your openai key in the `Authorization` header. + +## Fire Away! + +You can see your logs in realtime with neatly visualized traces and cost tracking. + + + Realtime API + + +## Next Steps + +- [For more info on realtime API, refer here](https://platform.openai.com/docs/guides/realtime) +- [Portkeys OpenAI Integration](/integrations/llms/openai) +- [Logs](/product/observability/logs) +- [Traces](/product/observability/traces) +- [Guardrails](/product/ai-gateway/guardrails) diff --git a/virtual_key_old/product/ai-gateway/remote-mcp.mdx b/virtual_key_old/product/ai-gateway/remote-mcp.mdx new file mode 100644 index 00000000..928691ac --- /dev/null +++ b/virtual_key_old/product/ai-gateway/remote-mcp.mdx @@ -0,0 +1,298 @@ +--- +title: "Remote MCP" +description: Portkey's AI gateway has MCP server support that many foundational model providers offer. +--- + +[Model Context Protocol](https://modelcontextprotocol.io/introduction) (MCP) is an open protocol that standardizes how applications provide tools and context to LLMs. The MCP tool in the Responses API allows developers to give the model access to tools hosted on **Remote MCP servers**. These are MCP servers maintained by developers and organizations across the internet that expose these tools to MCP clients, like the Responses API. + + +Portkey Supports using MCP server via the Response API. Calling a remote MCP server with the Responses API is straightforward. For example, here's how you can use the [DeepWiki](https://deepwiki.com/) MCP server to ask questions about nearly any public GitHub repository. + + + +Example MCP request +-------------- + + +A Responses API request to OpenAI with MCP tools enabled. + + +```bash cURL +curl https://api.portkey.ai/v1/responses \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-virtual-key: $OPENAI_VIRTUAL_KEY" \ + -d '{ + "model": "gpt-4.1", + "tools": [ + { + "type": "mcp", + "server_label": "deepwiki", + "server_url": "https://mcp.deepwiki.com/mcp", + "require_approval": "never" + } + ], + "input": "What transport protocols are supported in the 2025-03-26 version of the MCP spec?" + }' +``` + +```javascript OpenAI Node SDK +import OpenAI from "openai"; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const client = new OpenAI({ + apiKey: "xx", // Can be left blank when using virtual keys + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "OPENAI_VIRTUAL_KEY" + }) +}); + +const resp = await client.responses.create({ + model: "gpt-4.1", + tools: [ + { + type: "mcp", + server_label: "deepwiki", + server_url: "https://mcp.deepwiki.com/mcp", + require_approval: "never", + }, + ], + input: "What transport protocols are supported in the 2025-03-26 version of the MCP spec?", +}); + +console.log(resp.output_text); +``` + +```python OpenAI Python SDK +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +client = OpenAI( + api_key="xx", # Can be left blank when using virtual keys + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key="PORTKEY_API_KEY", + virtual_key="OPENAI_VIRTUAL_KEY" + ) +) + +resp = client.responses.create( + model="gpt-4.1", + tools=[ + { + "type": "mcp", + "server_label": "deepwiki", + "server_url": "https://mcp.deepwiki.com/mcp", + "require_approval": "never", + }, + ], + input="What transport protocols are supported in the 2025-03-26 version of the MCP spec?", +) + +print(resp.output_text) +``` + +```typescript Portkey Node SDK +import Portkey from 'portkey-ai'; + +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "OPENAI_VIRTUAL_KEY" +}); + +const resp = await portkey.responses.create({ + model: "gpt-4.1", + tools: [ + { + type: "mcp", + server_label: "deepwiki", + server_url: "https://mcp.deepwiki.com/mcp", + require_approval: "never", + }, + ], + input: "What transport protocols are supported in the 2025-03-26 version of the MCP spec?", +}); + +console.log(resp.output_text); +``` + +```python Portkey Python SDK +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY", + virtual_key="OPENAI_VIRTUAL_KEY" +) + +resp = portkey.responses.create( + model="gpt-4.1", + tools=[ + { + "type": "mcp", + "server_label": "deepwiki", + "server_url": "https://mcp.deepwiki.com/mcp", + "require_approval": "never", + }, + ], + input="What transport protocols are supported in the 2025-03-26 version of the MCP spec?", +) + +print(resp.output_text) +``` + + + + + + + + +MCP Server Authentication +-------------- + +Unlike the DeepWiki MCP server, most other MCP servers require authentication. The MCP tool in the Responses API gives you the ability to flexibly specify headers that should be included in any request made to a remote MCP server. These headers can be used to share API keys, oAuth access tokens, or any other authentication scheme the remote MCP server implements. + +The most common header used by remote MCP servers is the `Authorization` header. This is what passing this header looks like: + +Use Stripe MCP tool + + +```bash cURL +curl https://api.portkey.ai/v1/responses \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-virtual-key: $OPENAI_VIRTUAL_KEY" \ + -d '{ + "model": "gpt-4.1", + "input": "Create a payment link for $20", + "tools": [ + { + "type": "mcp", + "server_label": "stripe", + "server_url": "https://mcp.stripe.com", + "headers": { + "Authorization": "Bearer $STRIPE_API_KEY" + } + } + ] + }' +``` + +```javascript OpenAI Node SDK +import OpenAI from "openai"; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const client = new OpenAI({ + apiKey: "xx", // Can be left blank when using virtual keys + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "OPENAI_VIRTUAL_KEY" + }) +}); + +const resp = await client.responses.create({ + model: "gpt-4.1", + input: "Create a payment link for $20", + tools: [ + { + type: "mcp", + server_label: "stripe", + server_url: "https://mcp.stripe.com", + headers: { + Authorization: "Bearer $STRIPE_API_KEY" + } + } + ] +}); + +console.log(resp.output_text); +``` + +```python OpenAI Python SDK +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +client = OpenAI( + api_key="xx", # Can be left blank when using virtual keys + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key="PORTKEY_API_KEY", + virtual_key="OPENAI_VIRTUAL_KEY" + ) +) + +resp = client.responses.create( + model="gpt-4.1", + input="Create a payment link for $20", + tools=[ + { + "type": "mcp", + "server_label": "stripe", + "server_url": "https://mcp.stripe.com", + "headers": { + "Authorization": "Bearer $STRIPE_API_KEY" + } + } + ] +) + +print(resp.output_text) +``` + +```typescript Portkey Node SDK +import Portkey from 'portkey-ai'; + +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "OPENAI_VIRTUAL_KEY" +}); + +const resp = await portkey.responses.create({ + model: "gpt-4.1", + input: "Create a payment link for $20", + tools: [ + { + type: "mcp", + server_label: "stripe", + server_url: "https://mcp.stripe.com", + headers: { + Authorization: "Bearer $STRIPE_API_KEY" + } + } + ] +}); + +console.log(resp.output_text); +``` + +```python Portkey Python SDK +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY", + virtual_key="OPENAI_VIRTUAL_KEY" +) + +resp = portkey.responses.create( + model="gpt-4.1", + input="Create a payment link for $20", + tools=[ + { + "type": "mcp", + "server_label": "stripe", + "server_url": "https://mcp.stripe.com", + "headers": { + "Authorization": "Bearer $STRIPE_API_KEY" + } + } + ] +) + +print(resp.output_text) +``` + + +To prevent the leakage of sensitive keys, the Responses API does not store the values of **any** string you provide in the `headers` object. These values will also not be visible in the Response object created. Additionally, because some remote MCP servers generate authenticated URLs, we also discard the _path_ portion of the `server_url` in our responses (i.e. `example.com/mcp` becomes `example.com`). Because of this, you must send the full path of the MCP `server_url` and any relevant `headers` in every Responses API creation request you make. diff --git a/virtual_key_old/product/ai-gateway/request-timeouts.mdx b/virtual_key_old/product/ai-gateway/request-timeouts.mdx new file mode 100644 index 00000000..013393c5 --- /dev/null +++ b/virtual_key_old/product/ai-gateway/request-timeouts.mdx @@ -0,0 +1,199 @@ +--- +title: "Request Timeouts" +description: "Manage unpredictable LLM latencies effectively with Portkey's **Request Timeouts**." +--- + + + This feature is available on all Portkey [plans](https://portkey.ai/pricing). + + + This feature allows automatic termination of requests that exceed a specified duration, letting you gracefully handle errors or make another, faster request. + +## Enabling Request Timeouts + +You can enable request timeouts while **making your request** or you can **set them in Configs**. + +Request timeouts are specified in **milliseconds** (`integer)` + +### While Making Request + +Set request timeout while instantiating your Portkey client or if you're using the REST API, send the `x-portkey-request-timeout` header. + + + + + +```js +import Portkey from 'portkey-ai'; + +// Construct a client with a virtual key +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "VIRTUAL_KEY", + requestTimeout: 3000 +}) + +const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'gpt-4o-mini', +}); + +console.log(chatCompletion.choices); +``` + + + +```py +from portkey_ai import Portkey + +# Construct a client with a virtual key +portkey = Portkey( + api_key="PORTKEY_API_KEY", + virtual_key="VIRTUAL_KEY", + request_timeout=3000 +) + +completion = portkey.chat.completions.create( + messages = [{ "role": 'user', "content": 'Say this is a test' }], + model = 'gpt-4o-mini' +) +``` + + + +```sh +curl "https://api.portkey.ai/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-virtual-key: openai-virtual-key" \ + -H "x-portkey-request-timeout:5000" \ + -d '{ + "model": "gpt-4o-mini", + "messages": [{"role": "user", "content": "Hello!"}] + }' +``` + + + +### With Configs + +In Configs, request timeouts are set at either (1) strategy level, or (2) target level. + +For a 10-second timeout, it will be: + +```json +"request_timeout": 10000 +``` + +### Setting Request Timeout at Strategy Level + + +```JSON +{ + "strategy": { "mode": "fallback" }, + "request_timeout": 10000, + "targets": [ + { "virtual_key": "open-ai-xxx" }, + { "virtual_key": "azure-open-ai-xxx" } + ] +} +``` + +Here, the request timeout of 10 seconds will be applied to \* **all**\* the targets in this Config. + +### Setting Request Timeout at Target Level + +```JSON +{ + "strategy": { "mode": "fallback" }, + "targets": [ + { "virtual_key": "open-ai-xxx", "request_timeout": 10000, }, + { "virtual_key": "azure-open-ai-xxx", "request_timeout": 2000,} + ] +} +``` + +Here, for the first target, a request timeout of 10s will be set, while for the second target, a request timeout of 2s will be set. + + +Nested target objects inherit the top-level timeout, with the option to override it at any level for customized control. + + +### How timeouts work in nested Configs + + +```JSON +{ + "strategy": { "mode": "loadbalance" }, + "request_timeout": 2000, + "targets": [ + { + "strategy": { "mode":"fallback" }, + "request_timeout": 5000, + "targets": [ + { + "virtual_key":"open-ai-1-1" + }, + { + "virtual_key": "open-ai-1-2", + "request_timeout": 10000 + } + ], + "weight": 1 + }, + { + "virtual_key": "azure-open-ai-1", + "weight": 1 + } + ] +} +``` + +1. We've set a global timeout of **2s** at line #3 +2. The first target has a nested fallback strategy, with a top level request timeout of **5s** at line #7 +3. The first virtual key (at line #10), the **target-level** timeout of **5s** will be applied +4. For the second virtual key (i.e. `open-ai-1-2`), there is a timeout override, set at **10s**, which will be applied only to this target +5. For the last target (i.e. virtual key `azure-open-ai-1`), the top strategy-level timeout of **2s** will be applied + +## Handling Request Timeouts + +Portkey issues a standard **408 error** for timed-out requests. You can leverage this by setting up fallback or retry strategies through the `on_status_codes` parameter, ensuring robust handling of these scenarios. + +### Triggering Fallbacks with Request Timeouts + +```JSON +{ + "strategy": { + "mode": "fallback", + "on_status_codes": [408] + }, + "targets": [ + { "virtual_key": "open-ai-xxx", "request_timeout": 2000, }, + { "virtual_key": "azure-open-ai-xxx"} + ] +} +``` + +Here, fallback from OpenAI to Azure OpenAI will only be triggered if the first request times out after 2 seconds, otherwise the request will fail with a 408 error code. + +### Triggering Retries with Request Timeouts + +```JSON +{ + "request_timeout": 1000, + "retry": { "attempts": 3, "on_status_codes": [ 408 ] }, + "virtual_key": "open-ai-xxx" +} +``` + +Here, retry is triggered upto 3 times whenever the request takes more than 1s to return a response. After 3 unsuccessful retries, it will fail with a 408 code. + +[Here's a general guide on how to use Configs in your requests.](/product/ai-gateway/configs) + +### Caveats and Considerations + +While the request timeout is a powerful feature to help you gracefully handle unruly models & their latencies, there are a few things to consider: + +1. Ensure that you are setting reasonable timeouts - for example, models like `gpt-4` often have sub-10-second response times +2. Ensure that you gracefully handle 408 errors for whenever a request does get timed out - you can inform the user to rerun their query and setup some neat interactions on your app +3. For streaming requests, the timeout will not be triggered if it gets **atleast a chunk** before the specified duration. diff --git a/virtual_key_old/product/ai-gateway/strict-open-ai-compliance.mdx b/virtual_key_old/product/ai-gateway/strict-open-ai-compliance.mdx new file mode 100644 index 00000000..682495ee --- /dev/null +++ b/virtual_key_old/product/ai-gateway/strict-open-ai-compliance.mdx @@ -0,0 +1,77 @@ +--- +title: "Strict OpenAI Compliance" +--- + +By default, all the responses sent back from Portkey are compliant with the [OpenAI specification](https://platform.openai.com/docs/api-reference/chat/create). + +In some cases, a response from a provider like Perplexity may contain useful fields which do not have a corresponding 1:1 mapping to OpenAI fields. +To get those fields in the response, you can do one of the following: +- Python SDK: Pass this parameter `strict_open_ai_compliance=false` when initializing the portkey client +- Node SDK: Pass this parameter `strictOpenAiCompliance: false` when initializing the portkey client +- HTTP requests: Pass this header `x-portkey-strict-open-ai-compliance: false` with your request + + + + By default strict_open_ai_compliance=false in Portkey Python and Node SDK + + + + + ```python + from portkey_ai import Portkey + + portkey = Portkey( + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key + virtual_key="VIRTUAL_KEY", # Replace with your virtual key + strict_open_ai_compliance=False + ) + ``` + + + ```js + import Portkey from 'portkey-ai' + + const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY", // Your Virtual Key + strictOpenAiCompliance: false + }) + ``` + + + Add the following header to your request + `x-portkey-strict-open-ai-compliance: false` + + + ```python + from openai import OpenAI + from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + + openai = OpenAI( + api_key='OPENAI_API_KEY', + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY" + strict_open_ai_compliance=False + ) + ) + ``` + + + ```js + import OpenAI from 'openai'; // We're using the v4 SDK + import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + + const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', // defaults to process.env["OPENAI_API_KEY"], + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + provider: "openai", + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"], + strictOpenAiCompliance: false + }) + }); + ``` + + diff --git a/virtual_key_old/product/ai-gateway/universal-api.mdx b/virtual_key_old/product/ai-gateway/universal-api.mdx new file mode 100644 index 00000000..442721ff --- /dev/null +++ b/virtual_key_old/product/ai-gateway/universal-api.mdx @@ -0,0 +1,185 @@ +--- +title: "Universal API" +description: Portkey's Universal API provides a consistent interface to integrate a wide range of modalities (text, vision, audio) and LLMs (hosted OR local) into your apps. +--- + + + This feature is available on all Portkey plans. + + + So, instead of maintaining separate integrations for different multimodal LLMs, you can interact with models from OpenAI, Anthropic, Meta, Cohere, Mistral, and many more (100+ models, 15+ providers) - all using a common, unified API signature. + +## Portkey Follows OpenAI Spec + +Portkey API is powered by its [battle-tested open-source AI Gateway](https://github.com/portkey-ai/gateway), which converts all incoming requests to the OpenAI signature and returns OpenAI-compliant responses. + +## Switching Providers is a Breeze + + + + + ```JS +import Portkey from 'portkey-ai'; + +// Calling OpenAI +const portkey = new Portkey({ + provider: "openai", + Authorization: "Bearer sk-xxxxx" +}) + +const response = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Hello' }], + model: 'gpt-4', +}); + +// Swithing to Anthropic +const portkey = new Portkey({ + provider: "anthropic", + Authorization: "Bearer sk-ant-xxxxx" +}) + +const response = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Hello' }], + model: 'claude-3-opus-20240229', +}); +``` + + + + ```py +from portkey_ai import Portkey + +# Calling OpenAI +portkey = Portkey( + provider = "openai", + Authorization = "sk-xxxxx" +) + +response = portkey.chat.completions.create( + messages = [{ "role": 'user', "content": 'Hello' }], + model = 'gpt-4' +) + +# Switching to Anthropic +portkey = Portkey( + provider = "anthropic", + Authorization = "sk-ant-xxxxx" +) + +response = portkey.chat.completions.create( + messages = [{ "role": 'user', "content": 'Hello' }], + model = 'claude-3-opus-20240229' +) +``` + + + + +## Integrating Local or Private Models + +Portkey can also route to and observe your locally or privately hosted LLMs, as long as the model is compliant with one of the 15+ providers supported by Portkey and the URL is exposed publicly. + +Simply specify the `custom_host` parameter along with the `provider` name, and Portkey will handle the communication with your local model. + + + + +```js +import Portkey from 'portkey-ai'; + +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + provider: "mistral-ai", + customHost: "http://MODEL_URL/v1/" // Point Portkey to where the model is hosted +}) + +async function main(){ + const response = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: '1729' }], + model: 'mixtral-8x22b', + }); + console.log(response) +} + +main() +``` + + + +```py +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY", + provider="mistral-ai", + custom_host="http://MODEL_URL/v1/" # Point Portkey to where the model is hosted +) + +chat = portkey.chat.completions.create( + messages = [{ "role": 'user', "content": 'Say this is a test' }], + model="mixtral-8x22b" +) + +print(chat) +``` + + + +```sh +curl https://api.portkey.ai/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-provider: mistral-ai" \ + -H "x-portkey-custom-host: http://MODEL_URL/v1/" \ + -d '{ + "model": "mixtral-8x22b", + "messages": [{ "role": "user", "content": "Say this is a test" }] + }' +``` + + + + +**Note:** + +When using `custom_host`, include the version identifier (e.g., `/v1`) in the URL. Portkey will append the actual endpoint path (`/chat/completions`, `/completions`, or `/embeddings`) automatically. (For Ollama models, this works differently. [Check here](/integrations/llms/ollama)) + + +## Powerful Routing and Fallback Strategies + +With Portkey you can implement sophisticated routing and fallback strategies. Route requests to different providers based on various criteria, loadbalance them, set up retries or fallbacks to alternative models in case of failures or resource constraints. + +Here's an example config where we set up a fallback from OpenAI to a locally hosted Llama3 on Ollama: + +```py +config = { + "strategy": { "mode": "loadbalance" }, + "targets": [ + { + "provider": "openai", + "api_key": "xxx", + "weight": 1, + "override_params": { "model": "gpt-3.5-turbo" } + }, + { + "provider": "mistral-ai", + "custom_host": "http://MODEL_URL/v1/", + "weight": 1, + "override_params": { "model": "mixtral-8x22b" } + } + ] +} + +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY", + config=config +) +``` + +## Multimodality + +Portkey integrates with multimodal models through the same unified API and supports vision, audio, image generation, and more capabilities across providers. + +[Multimodal Capabilities](/product/ai-gateway/multimodal-capabilities) diff --git a/virtual_key_old/product/ai-gateway/virtual-keys.mdx b/virtual_key_old/product/ai-gateway/virtual-keys.mdx new file mode 100644 index 00000000..4f3ab853 --- /dev/null +++ b/virtual_key_old/product/ai-gateway/virtual-keys.mdx @@ -0,0 +1,220 @@ +--- +title: "Virtual Keys" +description: "Portkey's virtual key system allows you to securely store your LLM API keys in our vault, utilizing a unique virtual identifier to streamline API key management." +tag: "Deprecated" +--- + + + MIGRATION NOTICE + +We are upgrading the Virtual Key experience with the [Model Catalog](/support/upgrade-to-model-catalog) feature. + +With Model Catalog, you can now: +- Set model level budget & rate limits +- Inherit budget & rate limits from parent AI provider integrations +- Set granular, workspace-level access controls +- Pass the provider slug (previosuly known as `virtual key`) with the model param in your LLM requests + + + +Learn how to replace your virtual keys with Model Catalog + + +--- + +This feature also provides the following benefits: + +* Easier key rotation +* The ability to generate multiple virtual keys for a single API key +* Imposition of restrictions [based on cost](/product/ai-gateway/virtual-keys/budget-limits), request volume, and user access + +These can be managed within your account under the "Virtual Keys" tab. + +## Creating Virtual Keys: + +1. Navigate to the "Virtual Keys" page and click the "Add Key" button in the top right corner. +2. Select your AI provider, name your key uniquely, and note any usage specifics if needed. + + + **Tip:** You can register multiple keys for one provider or use different names for the same key for easy identification. + + +### Azure Virtual Keys +Azure Virtual Keys allow you to manage multiple Azure deployments under a single virtual key. This feature simplifies API key management and enables flexible usage of different Azure OpenAI models. +You can create multiple deployments under the same resource group and manage them using a single virtual key. + + + + + +To use the required deployment, simply pass the `alias` of the deployment as the `model` in LLM request body. In case the models is left empty or the specified alias does not exist, the default deployment is used. + + +## How are the provider API keys stored? + +Your API keys are encrypted and stored in secure vaults, accessible only at the moment of a request. Decryption is performed exclusively in isolated workers and only when necessary, ensuring the highest level of data security. + +## How are the provider keys linked to the virtual key? + +We randomly generate virtual keys and link them separately to the securely stored keys. This means, your raw API keys can not be reverse engineered from the virtual keys. + +## Using Virtual Keys + +### Using the Portkey SDK + +Add the virtual key directly to the initialization configuration for Portkey. + + + +```js +import Portkey from 'portkey-ai' + +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Portkey supports a vault for your LLM Keys +}) +``` + + + +```py +# Construct a client with a virtual key +portkey = Portkey( + api_key="PORTKEY_API_KEY", + virtual_key="VIRTUAL_KEY" +) +``` + + + +Alternatively, you can override the virtual key during the completions call as follows: + + + + +```js +const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'gpt-3.5-turbo', +}, {virtualKey: "OVERRIDING_VIRTUAL_KEY"}); +``` + + + +```py +completion = portkey.with_options(virtual_key="...").chat.completions.create( + messages = [{ "role": 'user', "content": 'Say this is a test' }], + model = 'gpt-3.5-turbo' +) +``` + + + +### Using the OpenAI SDK + +Add the virtual key directly to the initialization configuration for the OpenAI client. + + + +```js +import OpenAI from "openai"; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + + +const openai = new OpenAI({ + apiKey: '', // can be left blank + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] + virtualKey: "VIRTUAL_KEY" // Portkey supports a vault for your LLM Keys + }) +}); +``` + + + +```py +# Construct a client with a virtual key +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +client = OpenAI( + api_key="", # can be left blank + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key="PORTKEY_API_KEY" # defaults to os.environ.get("PORTKEY_API_KEY") + virtual_key="VIRTUAL_KEY" # Portkey supports a vault for your LLM Keys + ) +) +``` + + + +Alternatively, you can override the virtual key during the completions call as follows: + + + + +```js +const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'gpt-3.5-turbo', +}, {virtualKey: "OVERRIDING_VIRTUAL_KEY"}); +``` + + + +```py +completion = portkey.with_options(virtual_key="...").chat.completions.create( + messages = [{ "role": 'user', "content": 'Say this is a test' }], + model = 'gpt-3.5-turbo' +) +``` + + + +### Using alias with Azure virtual keys: + +```js +const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'gpt-4o', // This will be the alias of the deployment +}, {virtualKey: "VIRTUAL_KEY"}); +``` + + +### Self-Hosted LLM Virtual Keys + +Portkey supports creating virtual keys for your privately hosted LLMs, allowing you to manage them alongside commercial providers. + +1. When creating a new virtual key, enable the "Local/Privately hosted provider" toggle +2. Select the provider API specification your LLM implements (typically OpenAI) +3. Enter your model's base URL in the "Custom Host" field +4. Add any required authentication headers and their values + +This allows you to use your self-hosted models with all Portkey features including observability, reliability, and access control. + + + + + +For more details, see [Bring Your Own LLM](/product/ai-gateway/byollm). + +## Setting Budget Limits + +Portkey provides a simple way to set budget limits for any of your virtual keys and helps you manage your spending on AI providers (and LLMs) - giving you confidence and control over your application's costs. + +[Budget Limits](/product/ai-gateway/virtual-keys/budget-limits) + +## Prompt Templates + +Choose your Virtual Key within Portkeyโ€™s prompt templates, and it will be automatically retrieved and ready for use. + +## Langchain / LlamaIndex + +Set the virtual key when utilizing Portkey's custom LLM as shown below: + +```py +# Example in Langchain +llm = PortkeyLLM(api_key="PORTKEY_API_KEY",virtual_key="VIRTUAL_KEY") +``` diff --git a/virtual_key_old/product/ai-gateway/virtual-keys/bedrock-amazon-assumed-role.mdx b/virtual_key_old/product/ai-gateway/virtual-keys/bedrock-amazon-assumed-role.mdx new file mode 100644 index 00000000..b9f19803 --- /dev/null +++ b/virtual_key_old/product/ai-gateway/virtual-keys/bedrock-amazon-assumed-role.mdx @@ -0,0 +1,126 @@ +--- +title: "Connect Bedrock with Amazon Assumed Role" +description: "How to create a virtual key for Bedrock using Amazon Assumed Role Authentication" +--- + +Available on all plans. + + + + On the Enterprise plan and need to connect to Bedrock using an AWS assumed role? Check out the documentation here. + + + +## Select AWS Assumed Role Authentication + +Create a new virtual key on Portkey, select **Bedrock** as the provider and **AWS Assumed Role** as the authentication method. + + + + + + +## Create an AWS Role for Portkey to Assume +This role you create will be used by Porktey to execute InvokeModel commands on Bedrock models in your AWS account. The setup process will establish a minimal-permission ("least privilege") role and set it up to allow Porktey to assume this role. + +### Create a permission policy in your AWS account using the following JSON + +```json +{ + "Version": "2012-10-17", + "Statement": [ + { + "Sid": "BedrockConsole", + "Effect": "Allow", + "Action": [ + "bedrock:InvokeModel", + "bedrock:InvokeModelWithResponseStream" + ], + "Resource": "*" + } + ] +} +``` + + + + + +### Create a new IAM role +Choose *AWS account* as the trusted entity type. If you set an external ID be sure to copy it, we will need it later. + + + + + +### Add the above policy to the role +Search for the policy you created above and add it to the role. + + + + + +### Configure Trust Relationship for the role +Once the role is created, open the role and navigate to the *Trust relationships* tab and click *Edit trust policy*. +This is where you will add the Portkey AWS account as a trusted entity. + +```sh Portkey Account ARN +arn:aws:iam::299329113195:role/portkey-app +``` + + +The above ARN only works for our [hosted app](https://app.portkey.ai/).
+ +To enable **Assumed Role for AWS in your Portkey Enterprise deployment**, you can refer to [this guide](https://github.com/Portkey-AI/helm/blob/main/charts/portkey-gateway/docs/Bedrock.md). If you face any issue, please reach out to us at support@portkey.ai. +
+ +Paste the following JSON into the trust policy editor and click *Update Trust Policy*. + +```json +{ + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Principal": { + "AWS": "arn:aws:iam::299329113195:role/portkey-app" + }, + "Action": "sts:AssumeRole", + "Condition": {} +} +] +} +``` + +If you set an external ID, add it to the condition as shown below. + +```json + { + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Principal": { + "AWS": "" + }, + "Action": "sts:AssumeRole", + "Condition": { + "StringEquals": { + "sts:ExternalId": "" + } + } + } + ] +} +``` + + + +## Configure the virtual key with the role ARN +Once the role is created, copy the role ARN and paste it into the Bedrock integrations modal in Portkey along with the external ID if you set one and the AWS region you are using. + + + + + + You're all set! You can now use the virtual key to invoke Bedrock models. diff --git a/virtual_key_old/product/ai-gateway/virtual-keys/budget-limits.mdx b/virtual_key_old/product/ai-gateway/virtual-keys/budget-limits.mdx new file mode 100644 index 00000000..459913e7 --- /dev/null +++ b/virtual_key_old/product/ai-gateway/virtual-keys/budget-limits.mdx @@ -0,0 +1,88 @@ +--- +title: "Budget Limits" +description: "Budget Limits lets you set cost limits on virtual keys" +--- + +Budget Limits lets you set cost or token limits on virtual keys + + +Available on **Enterprise** plan and select **Pro** customers. + + +**Budget Limits on Virtual Keys** provide a simple way to manage your spending on AI providers (and LLMs) - giving you confidence and control over your application's costs. + +Budget Limit is currently only available to **Portkey** [**Enterprise Plan**](https://portkey.ai/docs/product/enterprise-offering) customers. Email us at `support@portkey.ai` if you would like to enable it for your org. + +## Setting Budget Limits on Virtual Keys + +When creating a new virtual key on Portkey, you can set limits in two ways: + +### Cost-Based Limits +Set a budget limit in USD that, once reached, will automatically expire the key to prevent further usage and overspending. + +### Token-Based Limits +Set a maximum number of tokens that can be consumed, allowing you to control usage independent of cost fluctuations. + + + + + +> #### Key Considerations +> +> * Budget limits can be set as either cost-based (USD) or token-based +> * The minimum cost limit you can set is **$1** +> * The minimum token limit you can set is **100 tokens** +> * Budget limits apply until exhausted or reset +> * Budget limits are applied only to requests made after the limit is set; they do not apply retroactively +> * Once set, budget limits **cannot be edited** by any organization member +> * Budget limits work for **all providers** available on Portkey and apply to **all organization members** who use the virtual key + +## Alert Thresholds + +You can now set alert thresholds to receive notifications before your budget limit is reached: + +* For cost-based budgets, set thresholds in USD +* For token-based budgets, set thresholds in tokens +* Receive email notifications when usage reaches the threshold +* Continue using the key until the full budget limit is reached + +## Periodic Reset Options + +You can configure budget limits to automatically reset at regular intervals: + + + + + +### Reset Period Options +- **No Periodic Reset**: The budget limit applies until exhausted with no automatic renewal +- **Reset Weekly**: Budget limits automatically reset every week +- **Reset Monthly**: Budget limits automatically reset every month + +### Reset Timing +- Weekly resets occur at the beginning of each week (Sunday at 12 AM UTC) +- Monthly resets occur on the **1st** calendar day of the month, at **12 AM UTC**, irrespective of when the budget limit was set prior + +## Editing Budget Limits + +If you need to change or update a budget limit, you can **duplicate** the existing virtual key and create a new one with the desired limit. + +## Monitoring Your Spending and Usage + +You can track your spending and token usage for any specific virtual key by navigating to the Analytics tab and filtering by the **desired key** and **timeframe**. + +## Pricing Support and Limitations + +Budget limits currently apply to all providers and models for which Portkey has pricing support. If a specific request log shows `0 cents` in the COST column, it means that Portkey does not currently track pricing for that model, and it will not count towards the virtual key's budget limit. + +For token-based budgets, Portkey tracks both input and output tokens across all supported models. + +It's important to note that budget limits cannot be applied retrospectively. The spend counter starts from zero only after you've set a budget limit for a key. + +## Availability + +Budget Limits is currently available **exclusively to Portkey Enterprise** customers and select Pro users. If you're interested in enabling this feature for your account, please reach out to us at [support@portkey.ai](mailto:support@portkey.ai) or join the [Portkey Discord](https://portkey.ai/community) community. + +## Enterprise Plan + +To discuss Portkey Enterprise plan details and pricing, [you can schedule a quick call here](https://portkey.sh/demo-16). diff --git a/virtual_key_old/product/ai-gateway/virtual-keys/rate-limits.mdx b/virtual_key_old/product/ai-gateway/virtual-keys/rate-limits.mdx new file mode 100644 index 00000000..1e5b6083 --- /dev/null +++ b/virtual_key_old/product/ai-gateway/virtual-keys/rate-limits.mdx @@ -0,0 +1,77 @@ +--- +title: "Rate Limits" +description: "Set Rate Limts to your virtual keys" +--- + +Rate Limits lets you set request or token consumption limits on virtual keys + + +Available on **Enterprise** plan and select **Pro** customers. + + +**Rate Limits on Virtual Keys** provide a powerful way to control the frequency of API requests or token consumption for your AI providers - giving you confidence and control over your application's usage patterns and performance. + +Rate Limit is currently only available to **Portkey** [**Enterprise Plan**](https://portkey.ai/docs/product/enterprise-offering) customers and select Pro users. Email us at `support@portkey.ai` if you would like to enable it for your org. + +## Setting Rate Limits on Virtual Keys + +When creating a new virtual key on Portkey, you can set rate limits in two ways: + +### Request-Based Limits +Set a maximum number of requests that can be made within a specified time period (per minute, hour, or day). + +### Token-Based Limits +Set a maximum number of tokens that can be consumed within a specified time period (per minute, hour, or day). + + + + + +> #### Key Considerations +> +> * Rate limits can be set as either request-based or token-based +> * Time intervals can be configured as per minute, per hour, or per day +> * Setting the limit to 0 disables the virtual key +> * Rate limits apply immediately after being set +> * Once set, budget limits **cannot be edited** by any organization member +> * Rate limits work for **all providers** available on Portkey and apply to **all organization members** who use the virtual key +> * After a rate limit is reached, requests will be rejected until the time period resets + +## Rate Limit Intervals + +You can choose from three different time intervals for your rate limits: + +* **Per Minute**: Limits reset every minute, ideal for fine-grained control +* **Per Hour**: Limits reset hourly, providing balanced usage control +* **Per Day**: Limits reset daily, suitable for broader usage patterns + +## Exceeding Rate Limits + +When a rate limit is reached: +* Subsequent requests are rejected with a specific error code +* Error messages clearly indicate that the rate limit has been exceeded +* The limit automatically resets after the specified time period has elapsed + +## Editing Rate Limits + +If you need to change or update a rate limit, you can **duplicate** the existing virtual key and create a new one with the desired limit. + +## Monitoring Your Usage + +You can track your request and token usage for any specific virtual key by navigating to the Analytics tab and filtering by the **desired key** and **timeframe**. + +## Use Cases for Rate Limits + +* **Cost Control**: Prevent unexpected usage spikes that could lead to high costs +* **Performance Management**: Ensure your application maintains consistent performance +* **Fairness**: Distribute API access fairly across teams or users +* **Security**: Mitigate potential abuse or DoS attacks +* **Provider Compliance**: Stay within the rate limits imposed by underlying AI providers + +## Availability + +Rate Limits is currently available **exclusively to Portkey Enterprise** customers and select Pro users. If you're interested in enabling this feature for your account, please reach out to us at [support@portkey.ai](mailto:support@portkey.ai) or join the [Portkey Discord](https://portkey.ai/community) community. + +## Enterprise Plan + +To discuss Portkey Enterprise plan details and pricing, [you can schedule a quick call here](https://portkey.sh/demo-16). diff --git a/virtual_key_old/product/autonomous-fine-tuning.mdx b/virtual_key_old/product/autonomous-fine-tuning.mdx new file mode 100644 index 00000000..29275245 --- /dev/null +++ b/virtual_key_old/product/autonomous-fine-tuning.mdx @@ -0,0 +1,98 @@ +--- +title: 'Autonomous Fine-tuning' +description: 'Automatically create, manage, and execute fine-tuning jobs for Large Language Models (LLMs) across multiple providers.' +--- + + +**This feature is in private beta.** +Please drop us a message on support@portkey.ai or on our [Discord](https://discord.gg/DD7vgKK299) if you're interested. + + +## What is Autonomous LLM Fine-tuning? + +Autonomous Fine-tuning is a powerful feature offered by Portkey AI that enables organizations to automatically create, manage, and execute fine-tuning jobs for Large Language Models (LLMs) across multiple providers. + +This feature leverages your existing API usage data to continuously improve and customize LLM performance for your specific use cases. + +## Benefits + +- **Automated Workflow**: Streamline the entire fine-tuning process from data preparation to model deployment. +- **Multi-Provider Support**: Fine-tune models across 10+ providers, including OpenAI, Azure, AWS Bedrock, and Anyscale. +- **Data-Driven Improvements**: Utilize your actual API usage data to create relevant and effective fine-tuning datasets. +- **Continuous Learning**: Set up periodic fine-tuning jobs to keep your models up-to-date with the latest data. +- **Enhanced Performance**: Improve model accuracy and relevance for your specific use cases. +- **Cost-Effective**: Optimize your LLM usage by fine-tuning models to better suit your needs, potentially reducing the number of API calls required. +- **Centralized Management**: Manage all your fine-tuning jobs across different providers from a single interface. + +## Data Preparation + +1. **Log Collection**: Portkey's AI gateway automatically collects and stores logs from your LLM API requests. +2. **Data Enrichment**: + - Filter logs based on various criteria. + - Annotate logs with additional information. + - Use Portkey's Guardrails feature for automatic log annotation. +3. **Dataset Creation**: Utilize filters to select the most relevant logs for your fine-tuning dataset. +4. **Data Export**: Export the enriched logs as a dataset suitable for fine-tuning. + +## Fine-tuning Process + +1. **Model Selection**: Choose from a wide range of supported LLM providers and models. +2. **Job Configuration**: Set up fine-tuning parameters through an intuitive UI. +3. **Execution**: Portkey triggers the fine-tuning job on the selected provider's platform. +4. **Monitoring**: Track the progress of your fine-tuning jobs through Portkey's dashboard. +5. **Deployment**: Once complete, the fine-tuned model becomes available for use through Portkey's API gateway. + +## How It Works: Step-by-Step + +1. **Data Collection**: As you use Portkey's AI gateway for LLM requests, logs are automatically collected and stored in your Portkey account. +2. **Data Enrichment**: + - Apply filters to your log data. + - Add annotations and additional context to logs. + - Utilize Portkey's Guardrails feature for automatic input/output annotations. +3. **Dataset Creation**: + - Use the enriched log data to create a curated dataset for fine-tuning. + - Apply additional filters to select the most relevant data points. +4. **Fine-tuning Job Setup**: + - Access the Fine-tuning feature in Portkey's UI. + - Select your desired LLM provider and model. + - Choose your prepared dataset. + - Configure fine-tuning parameters. +5. **Job Execution**: + - Portkey initiates the fine-tuning job on the chosen provider's platform. + - Monitor the progress through Portkey's dashboard. +6. **Model Deployment**: + - Once fine-tuning is complete, the new model becomes available through Portkey's API gateway. +7. **Continuous Improvement** (Optional): + - Set up periodic fine-tuning jobs (daily, weekly, or monthly). + - Portkey automatically creates and executes these jobs using the latest data. + +## Partnerships + +Portkey AI has established partnerships to extend the capabilities of its Autonomous Fine-tuning feature: + +- **OpenPipe**: Integration allows Portkey's enriched data to be used on OpenPipe's fine-tuning platform. +- **Pipeshift**: Portkey's datasets can be seamlessly utilized in Pipeshift's inference platform. + +These partnerships enable users to leverage Portkey's data preparation capabilities with specialized fine-tuning and inference services. + +## Getting Started + +To begin using Autonomous Fine-tuning: + +1. Ensure you have an active Portkey AI account with the AI gateway set up. +2. Navigate to the Fine-tuning section in your Portkey dashboard. +3. Follow the step-by-step wizard to create your first fine-tuning job. +4. For assistance, consult our detailed documentation or contact Portkey support. + +## Best Practices + +- Regularly review and update your data filtering criteria to ensure the quality of your fine-tuning datasets. +- Start with smaller, focused datasets before scaling up to larger fine-tuning jobs. +- Monitor the performance of your fine-tuned models and iterate as needed. +- Leverage Portkey's analytics to gain insights into your model's performance improvements. + +By utilizing Portkey AI's Autonomous Fine-tuning feature, you can harness the power of your own data to create customized, high-performing LLMs tailored to your specific needs, all while streamlining the management of multiple AI providers. + +## API Support + +For more information on API support for fine-tuning, please refer to our [fine-tuning documentation](/api-reference/inference-api/fine-tuning/create-fine-tuning-job). diff --git a/virtual_key_old/product/enterprise-offering.mdx b/virtual_key_old/product/enterprise-offering.mdx new file mode 100644 index 00000000..b2ef4aaa --- /dev/null +++ b/virtual_key_old/product/enterprise-offering.mdx @@ -0,0 +1,78 @@ +--- +title: "Enterprise Offering" +mode: "wide" +--- + + + + Deploy Portkey in your own private cloud for enhanced security and control. + + + Set custom retention periods for different users to meet your data storage requirements. + + + Set monthly or custom budget limits on LLM usage, at provider or Portkey key level. + + + Organize your teams and entities with more access control in Portkey. + + + Programmatically set rate limits at API key level and prevent abuse. + + + Route requests based on your custom-defined criteria. + + + Ensure the highest level of data privacy & security with isolated storage infrastructure. + + + Portkey complies with SOC 2, GDPR, ISO27001, HIPAA to meet the most demanding industry standards. + + + Protect sensitive healthcare data with BAAs tailored to your organization's requirements. + + + Portkey has seamless integrations with your preferred Single Sign-On (SSO) providers like Okta and Microsoft. + + + Easily export all your data to your preferred data lake for long-term storage. + + + Create & manage unlimited prompt templates on Portkey. + + + Leverage unlimited caching with customizable Time-to-Live (TTL) settings. + + + Team & individual level RBAC for AI services, LLMs, logs, and more. + + + Integrate multiple LLM providers and their auth mechanissms (JWT, IAM etc) over a common, unified API. + + + Deploy org-wide guardrails like PII redaction, company policy adherence, and more across *all* AI calls. + + + Purpose-built telemetry & analytics to monitor AI service performance & ensure SLA adherence. + + + Executive-only dashboards to track AI adoption, ROI, and strategic impact across your organization. + + + System-wide audit trails for all AI services, and all the Portkey features described above. + + + Bring your own encryption keys to Portkey AI to encrypt data at rest. + + + +## Interested? Schedule a Call Below + + diff --git a/virtual_key_old/product/enterprise-offering/access-control-management.mdx b/virtual_key_old/product/enterprise-offering/access-control-management.mdx new file mode 100644 index 00000000..2983fec3 --- /dev/null +++ b/virtual_key_old/product/enterprise-offering/access-control-management.mdx @@ -0,0 +1,83 @@ +--- +title: "Access Control Management" +description: "With customizable user roles, API key management, and comprehensive audit logs, Portkey provides the flexibility and control needed to ensure secure collaboration & maintain a strong security posture" +--- + + + + This is a Portkey [**Enterprise**](https://portkey.ai/docs/product/enterprise-offering) plan feature. + + +At Portkey, we understand the critical importance of access control and data security for enterprise customers. Our platform provides a robust and flexible access control management system that enables you to safeguard your sensitive information while empowering your teams to collaborate effectively. + +## 1\. Isolated and Customizable Organizations + +Portkey's enterprise version allows you to create multiple `organizations`, each serving as a secure and isolated environment for your teams or projects. This multi-tenant architecture ensures that your data, logs, analytics, prompts, virtual keys, configs, guardrails, and API keys are strictly confined within each `organization`, preventing unauthorized access and maintaining data confidentiality. + + + + + +With the ability to create and manage multiple organizations, you can tailor access control to match your company's structure and project requirements. Users can be assigned to specific organizations, and they can seamlessly switch between them using Portkey's intuitive user interface. + + + + + +## 2\. Fine-Grained User Roles and Permissions + +Portkey offers a comprehensive Role-Based Access Control (RBAC) system that allows you to define and assign user roles with granular permissions. By default, Portkey provides three roles: `Owner`, `Admin`, and `Member`, each with a predefined set of permissions across various features. + +* `Owners` have complete control over the organization, including user management, billing, and all platform features. +* `Admins` have elevated privileges, allowing them to manage users, prompts, configs, guardrails, virtual keys, and API keys. +* `Members` have access to essential features like logs, analytics, prompts, configs, and virtual keys, with limited permissions. + +| Feature | Owner Role | Admin Role | Member Role | +| ------------------ | ------------------------------------------- | ------------------------------------------- | -------------------------- | +| Logs and Analytics | View, Filter, Group | View, Filter, Group | View, Filter, Group | +| Prompts | List, View, Create, Update, Delete, Publish | List, View, Create, Update, Delete, Publish | List, View, Create, Update | +| Configs | List, View, Create, Update, Delete | List, View, Create, Update, Delete | List, View, Create | +| Guardrails | List, View, Create, Update, Delete | List, View, Create, Update, Delete | List, View, Create, Update | +| Virtual Keys | List, Create, Edit, Duplicate, Delete, Copy | List, Create, Edit, Duplicate, Delete, Copy | List, Copy | +| Team | Add users, assign roles | Add users, assign roles | \- | +| Organisation | Update | Update | \- | +| API Keys | Create, Edit, Delete, Update, Rotate | Create, Edit, Delete, Update, Rotate | \- | +| Billing | Manage | \- | \- | + +You can easily add team members to your organization and assign them appropriate roles based on their responsibilities. Portkey's user-friendly interface simplifies the process of inviting users and managing their roles, ensuring that the right people have access to the right resources. + + + + + +## 3\. Secure and Customizable API Key Management + +Portkey provides a secure and flexible API key management system that allows you to create and manage multiple API keys with fine-grained permissions. Each API key can be customized to grant specific access levels to different features, such as metrics, completions, prompts, configs, guardrails, virtual keys, team management, and API key management. + +| Feature | Permissions | Default | +| --------------------------- | ----------------------------- | -------- | +| Metrics | Disabled, Enabled | Disabled | +| Completions (all LLM calls) | Disabled, Enabled | Enabled | +| Prompts | Disabled, Read, Write, Delete | Read | +| Configs | Disabled, Read, Write, Delete | Disabled | +| Guardrails | Disabled, Read, Write, Delete | Disabled | +| Virtual Keys | Disabled, Read, Write, Delete | Disabled | +| Users (Team Management) | Disabled, Read, Write, Delete | Disabled | + +By default, a new organization is provisioned with a master API key that has all permissions enabled. Owners and admins can edit and manage these keys, as well as create new API keys with tailored permissions. This granular control enables you to enforce the principle of least privilege, ensuring that each API key has access only to the necessary resources. + +Portkey's API key management system provides a secure and auditable way to control access to your organization's data and resources, reducing the risk of unauthorized access and data breaches. + +## Audit Logs + +Portkey maintains detailed audit logs that capture all administrative activities across the platform. These logs provide visibility into actions related to prompts, configs, guardrails, virtual keys, team management, organization updates, and API key modifications. + +Each log entry includes information about the user, the action performed, the affected resource, and a timestamp. This ensures traceability and accountability, helping teams monitor changes and investigate any unauthorized activity. + +Audit logs can be filtered by user, action type, resource, and time range, making it easy to track specific events. Organizations can use this data to enforce security policies, ensure compliance, and maintain operational integrity. + + + + + +Portkeyโ€™s audit logging system provides a clear and structured way to review platform activity, ensuring security and compliance across all operations. diff --git a/virtual_key_old/product/enterprise-offering/analytics-logs-export.mdx b/virtual_key_old/product/enterprise-offering/analytics-logs-export.mdx new file mode 100644 index 00000000..b3ed07b3 --- /dev/null +++ b/virtual_key_old/product/enterprise-offering/analytics-logs-export.mdx @@ -0,0 +1,50 @@ +--- +title: OTel Integration (Analytics Data) +--- + + Portkey supports sending your Analytics data to OpenTelemetry (OTel) compatible collectors, allowing you to integrate Portkey's analytics with your existing observability infrastructure. + + ## Overview + + While Portkey leverages Clickhouse as the primary Analytics Store for the Control Panel by default, enterprise customers can integrate Portkey's analytics data with their existing data infrastructure through OpenTelemetry. + + ## Configuration + + Portkey supports pushing your analytics data to an OTEL compatible endpoint. The following environment variables are needed for pushing to OTEL: + + ```yaml + OTEL_PUSH_ENABLED: true + OTEL_ENDPOINT: http://localhost:4318 + ``` + + Additionally, you can configure arbitrary resource attributes of the OTEL logs by setting a comma-separated value for `OTEL_RESOURCE_ATTRIBUTES`: + + ``` + OTEL_RESOURCE_ATTRIBUTES: ApplicationShortName=gateway,AssetId=12323,deployment.service=production + ``` + + ## Integration Options + + Enterprise customers commonly use these analytics exports with: + + - **Datadog**: Monitor and analyze your AI operations alongside other application metrics + - **AWS S3**: Store analytics data for long-term retention and analysis + - **Other OTEL-compatible systems**: Any system that can ingest OpenTelemetry data can be used with this feature + + ## Use Cases + + This feature enables: + + - Centralized observability across your entire tech stack + - Long-term storage of analytics data + - Custom analytics dashboards in your preferred tools + - Integration with existing alerting systems + + ## Getting Support + + For additional assistance with setting up analytics data export: + + - Join our [Discord community](https://portkey.sh/reddit-discord) + - Email us at support@portkey.ai + + Our team can help you with best practices for configuring your OTEL collectors and integrating with your existing systems. diff --git a/virtual_key_old/product/enterprise-offering/audit-logs.mdx b/virtual_key_old/product/enterprise-offering/audit-logs.mdx new file mode 100644 index 00000000..7da163d5 --- /dev/null +++ b/virtual_key_old/product/enterprise-offering/audit-logs.mdx @@ -0,0 +1,140 @@ +--- +title: "Audit Logs" +description: "Track and monitor all administrative activities across your Portkey organization with comprehensive audit logging." +--- + + + This is a Portkey [**Enterprise**](https://portkey.ai/docs/product/enterprise-offering) plan feature. + + +## Overview + +Audit Logs in Portkey provide a comprehensive record of all administrative activities across your organization. These logs capture detailed information about who performed what actions, on which resources, and when those actions occurred. This level of visibility is crucial for security monitoring, compliance requirements, and troubleshooting operational issues. + + + + + +## Key Benefits + +- **Enhanced Security**: Track all changes to your organization's resources and configurations +- **Compliance Support**: Maintain detailed records to help meet regulatory requirements +- **Operational Visibility**: Understand who is making changes and when +- **Troubleshooting**: Investigate issues by reviewing recent configuration changes +- **Accountability**: Ensure users are responsible for their actions within the platform + +## Logged Information + +Each audit log entry contains detailed information about administrative activities: + +| Field | Description | +|-------|-------------| +| Timestamp | Date and time when the action occurred | +| User | The individual who performed the action | +| Workspace | The workspace context in which the action was performed (if applicable) | +| Action | The type of operation performed (create, update, delete, etc.) | +| Resource | The specific resource or entity that was affected | +| Response Status | HTTP status code indicating the result of the action | +| Client IP | IP address from which the request originated | +| Country | Geographic location associated with the request | + + + + Setting up Audit Logs is straightforward: + + 1. Access is available to org owners and admins + 2. Audit logs are automatically collected across all workspaces + 3. No additional configuration needed - just enable and go + 4. Access audit logs through the `Admin Settings` > `Audit Logs` page + + + +## Filtering Capabilities + +Portkey provides powerful filtering options to help you find specific audit events quickly: + + + + + +### Available Filters + +- **Method**: Filter by HTTP method (PUT, POST, DELETE) +- **Request ID**: Search for a specific request by its unique identifier +- **Resource Type**: Filter by type of resource affected: + - Workspaces + - API Keys + - Virtual Keys + - Configs + - Prompts + - Guardrails + - Integrations + - Collections + - Organization + - Labels + - Custom Resource Types +- **Action**: Filter by the type of action performed: + - Create + - Update + - Delete + - Publish + - Export + - Rotate + - Manage + - Duplicate +- **Response Status**: Filter by HTTP response status codes +- **Workspace**: Filter by specific workspace +- **User**: Filter by the user who performed the action +- **Client IP**: Filter by originating IP address +- **Country**: Filter by geographic location of requests +- **Time Range**: Filter logs within a specific time period + +## Enterprise Features + +Portkey's Audit Logs include enterprise-grade capabilities: + +### 1. Complete Visibility +- Full user attribution for every action +- Detailed timestamps and change history +- Cross-workspace tracking +- Searchable audit trail + +### 2. Compliance & Security +- SOC 2, ISO 27001, GDPR, and HIPAA compliant +- PII data protection +- Indefinite log retention + +### 3. Enterprise-Grade Features +- Role-based access control +- Cross-organization visibility +- Custom retention policies + +> As a Director of AI Infrastructure at a Fortune 100 Healthcare company explained: *"Having a detailed audit trail isn't just about compliance. It's about being able to debug production issues quickly, understand usage patterns, and make data-driven decisions about our AI infrastructure."* + + +## Related Features + + + + Manage user roles and permissions across your organization + + + Learn about organization structure and management + + + Understand workspace management within your organization + + + Authentication and authorization with API keys + + + +## Support + +For questions about Audit Logs or assistance with interpreting specific entries, contact [Portkey support](mailto:support@portkey.ai) or reach out on [Discord](https://portkey.sh/reddit-discord). + + + Ready to bring enterprise-grade governance to your AI infrastructure? Learn more about Portkey's Audit Logs and other enterprise features in a personalized demo. + + +--- diff --git a/virtual_key_old/product/enterprise-offering/budget-limits.mdx b/virtual_key_old/product/enterprise-offering/budget-limits.mdx new file mode 100644 index 00000000..be58e8e4 --- /dev/null +++ b/virtual_key_old/product/enterprise-offering/budget-limits.mdx @@ -0,0 +1,4 @@ +--- +title: Budget Limits +url: /product/ai-gateway/virtual-keys/budget-limits +--- diff --git a/virtual_key_old/product/enterprise-offering/cloud-marketplace/aws.mdx b/virtual_key_old/product/enterprise-offering/cloud-marketplace/aws.mdx new file mode 100644 index 00000000..6398b7c8 --- /dev/null +++ b/virtual_key_old/product/enterprise-offering/cloud-marketplace/aws.mdx @@ -0,0 +1,1671 @@ +--- +title: "AWS" +description: This enterprise-focused document provides comprehensive instructions for deploying the Portkey software using AWS Marketplace. +--- + + It includes specific steps to subscribe Portkey AI Hybrid Enterprise Edition using AWS Marketplace and deploy the Portkey AI Gateway on AWS EKS with Quick Launch. + + +## Architecture + + + + + +## Components and Sizing Recommendations + +| Component | Options | Sizing Recommendations | +| --------------------------------------- | ------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------- | +| AI Gateway | Deploy as a Docker container in your Kubernetes cluster using Helm Charts | AWS NodeGroup t4g.medium instance, with at least 4GiB of memory and two vCPUs For high reliability, deploy across multiple Availability Zones. | +| Logs store | AWS S3 | Each log document is \~10kb in size (uncompressed) | +| Cache (Prompts, Configs & Virtual Keys) | Elasticache or self-hosted Redis | Deploy in the same VPC as the Portkey Gateway. | + +## Helm Chart + +This deployment uses the Portkey AI hybrid Helm chart to deploy the Portkey AI Gateway. You can find more information about the Helm chart in the [Portkey AI Helm Chart GitHub Repository](https://github.com/Portkey-AI/helm/blob/main/charts/portkey-gateway/README.md). + +## Prerequisites + +1. Create a Portkey account on [Portkey AI](https://app.portkey.ai) +2. Portkey team will share the credentials for the private Docker registry. + +## Markeplace Listing + +### Visit Portkey AI AWS Marketplace Listing + +You can find the Portkey AI AWS Marketplace listing [here](https://aws.amazon.com/marketplace/pp/prodview-o2leb4xcrkdqa). + + + + + +### Subscribe to Portkey AI Enterprise Edition + +Subscribe to the Portkey AI Enterprise Edition to gain access to the Portkey AI Gateway. + + + + + +### Quick Launch + +Upon subscribing to the Portkey AI Enterprise Edition, you will be able to select Quick Launch from within your AWS Console Subscriptions. + + + + + +### Launch the Cloud Formation Template + +Select the Portkey AI Enterprise Edition and click on Quick Launch. + + + + +### Run the Cloud Formation Template + +Fill the required parameters and click on Next and run the Cloud Formation Template. + + + + + +## Cloud Formation Steps + +- Creates a new EKS cluster and NodeGroup in your selected VPC and Subnets +- Sets up IAM Roles needed for S3 bucket access using STS and Lambda execution +- Uses AWS Lambda to: + - Install the Portkey AI Helm chart to your EKS cluster + - Upload the values.yaml file to the S3 bucket +- Allows for changes to the values file or helm chart deployment by updating and re-running the same Lambda function in your AWS account + +### Cloudformation Template + +> Our cloudformation template has passed the AWS Marketplace validation and security review. + +```yaml portkey-hybrid-eks-cloudformation.template.yaml [expandable] +AWSTemplateFormatVersion: "2010-09-09" +Description: Portkey deployment template for AWS Marketplace + +Metadata: + AWS::CloudFormation::Interface: + ParameterGroups: + - Label: + default: "Required Parameters" + Parameters: + - VPCID + - Subnet1ID + - Subnet2ID + - ClusterName + - NodeGroupName + - NodeGroupInstanceType + - SecurityGroupID + - CreateNewCluster + - HelmChartVersion + - PortkeyDockerUsername: + NoEcho: true + - PortkeyDockerPassword: + NoEcho: true + - PortkeyClientAuth: + NoEcho: true + - Label: + default: "Optional Parameters" + Parameters: + - PortkeyOrgId + - PortkeyGatewayIngressEnabled + - PortkeyGatewayIngressSubdomain + - PortkeyFineTuningEnabled + +Parameters: + # Required Parameters + VPCID: + Type: AWS::EC2::VPC::Id + Description: VPC where the EKS cluster will be created + Default: Select a VPC + + Subnet1ID: + Type: AWS::EC2::Subnet::Id + Description: First subnet ID for EKS cluster + Default: Select your subnet + + Subnet2ID: + Type: AWS::EC2::Subnet::Id + Description: Second subnet ID for EKS cluster + Default: Select your subnet + + # Optional Parameters with defaults + ClusterName: + Type: String + Description: Name of the EKS cluster (if not provided, a new EKS cluster will be created) + Default: portkey-eks-cluster + + NodeGroupName: + Type: String + Description: Name of the EKS node group (if not provided, a new EKS node group will be created) + Default: portkey-eks-cluster-node-group + + NodeGroupInstanceType: + Type: String + Description: EC2 instance type for the node group (if not provided, t3.medium will be used) + Default: t3.medium + AllowedValues: + - t3.medium + - t3.large + - t3.xlarge + + PortkeyDockerUsername: + Type: String + Description: Docker username for Portkey (provided by the Portkey team) + Default: portkeyenterprise + + PortkeyDockerPassword: + Type: String + Description: Docker password for Portkey (provided by the Portkey team) + Default: "" + NoEcho: true + + PortkeyClientAuth: + Type: String + Description: Portkey Client ID (provided by the Portkey team) + Default: "" + NoEcho: true + + PortkeyOrgId: + Type: String + Description: Portkey Organisation ID (provided by the Portkey team) + Default: "" + + HelmChartVersion: + Type: String + Description: Version of the Helm chart to deploy + Default: "latest" + AllowedValues: + - latest + + SecurityGroupID: + Type: String + Description: Optional security group ID for the EKS cluster (if not provided, a new security group will be created) + Default: "" + + CreateNewCluster: + Type: String + AllowedValues: [true, false] + Default: true + Description: Whether to create a new EKS cluster or use an existing one + + PortkeyGatewayIngressEnabled: + Type: String + AllowedValues: [true, false] + Default: false + Description: Whether to enable the Portkey Gateway ingress + + PortkeyGatewayIngressSubdomain: + Type: String + Description: Subdomain for the Portkey Gateway ingress + Default: "" + + PortkeyFineTuningEnabled: + Type: String + AllowedValues: [true, false] + Default: false + Description: Whether to enable the Portkey Fine Tuning + +Conditions: + CreateSecurityGroup: !Equals [!Ref SecurityGroupID, ""] + ShouldCreateCluster: !Equals [!Ref CreateNewCluster, true] + +Resources: + PortkeyAM: + Type: AWS::IAM::Role + DeletionPolicy: Delete + Properties: + RoleName: PortkeyAM + AssumeRolePolicyDocument: + Version: "2012-10-17" + Statement: + - Effect: Allow + Principal: + AWS: !Sub "arn:aws:iam::${AWS::AccountId}:root" + Action: sts:AssumeRole + Policies: + - PolicyName: PortkeyEKSAccess + PolicyDocument: + Version: "2012-10-17" + Statement: + - Effect: Allow + Action: + - "eks:DescribeCluster" + - "eks:ListClusters" + - "eks:ListNodegroups" + - "eks:ListFargateProfiles" + - "eks:ListNodegroups" + - "eks:CreateCluster" + - "eks:CreateNodegroup" + - "eks:DeleteCluster" + - "eks:DeleteNodegroup" + - "eks:UpdateClusterConfig" + - "eks:UpdateKubeconfig" + Resource: !Sub "arn:aws:eks:${AWS::Region}:${AWS::AccountId}:cluster/${ClusterName}" + - Effect: Allow + Action: + - "sts:AssumeRole" + Resource: !Sub "arn:aws:iam::${AWS::AccountId}:role/PortkeyAM" + - Effect: Allow + Action: + - "sts:GetCallerIdentity" + Resource: "*" + - Effect: Allow + Action: + - "iam:ListRoles" + - "iam:GetRole" + Resource: "*" + - Effect: Allow + Action: + - "bedrock:InvokeModel" + - "bedrock:InvokeModelWithResponseStream" + Resource: "*" + - Effect: Allow + Action: + - "s3:GetObject" + - "s3:PutObject" + Resource: + - !Sub "arn:aws:s3:::${AWS::AccountId}-${AWS::Region}-portkey-logs/*" + + PortkeyLogsBucket: + Type: AWS::S3::Bucket + DeletionPolicy: Delete + Properties: + BucketName: !Sub "${AWS::AccountId}-${AWS::Region}-portkey-logs" + VersioningConfiguration: + Status: Enabled + PublicAccessBlockConfiguration: + BlockPublicAcls: true + BlockPublicPolicy: true + IgnorePublicAcls: true + RestrictPublicBuckets: true + BucketEncryption: + ServerSideEncryptionConfiguration: + - ServerSideEncryptionByDefault: + SSEAlgorithm: AES256 + + # EKS Cluster Role + EksClusterRole: + Type: AWS::IAM::Role + DeletionPolicy: Delete + Properties: + RoleName: EksClusterRole-Portkey + AssumeRolePolicyDocument: + Version: "2012-10-17" + Statement: + - Effect: Allow + Principal: + Service: eks.amazonaws.com + Action: sts:AssumeRole + ManagedPolicyArns: + - arn:aws:iam::aws:policy/AmazonEKSClusterPolicy + + # EKS Cluster Security Group (if not provided) + EksSecurityGroup: + Type: AWS::EC2::SecurityGroup + Condition: CreateSecurityGroup + DeletionPolicy: Delete + Properties: + GroupDescription: Security group for Portkey EKS cluster + VpcId: !Ref VPCID + SecurityGroupIngress: + - IpProtocol: tcp + FromPort: 8787 + ToPort: 8787 + CidrIp: PORTKEY_IP + SecurityGroupEgress: + - IpProtocol: tcp + FromPort: 443 + ToPort: 443 + CidrIp: 0.0.0.0/0 + + # EKS Cluster + EksCluster: + Type: AWS::EKS::Cluster + Condition: ShouldCreateCluster + DeletionPolicy: Delete + DependsOn: EksClusterRole + Properties: + Name: !Ref ClusterName + Version: "1.32" + RoleArn: !GetAtt EksClusterRole.Arn + ResourcesVpcConfig: + SecurityGroupIds: + - !If + - CreateSecurityGroup + - !Ref EksSecurityGroup + - !Ref SecurityGroupID + SubnetIds: + - !Ref Subnet1ID + - !Ref Subnet2ID + AccessConfig: + AuthenticationMode: API_AND_CONFIG_MAP + + LambdaExecutionRole: + Type: AWS::IAM::Role + DeletionPolicy: Delete + DependsOn: EksCluster + Properties: + RoleName: PortkeyLambdaRole + AssumeRolePolicyDocument: + Version: "2012-10-17" + Statement: + - Effect: Allow + Principal: + Service: lambda.amazonaws.com + Action: sts:AssumeRole + ManagedPolicyArns: + - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole + Policies: + - PolicyName: EKSAccess + PolicyDocument: + Version: "2012-10-17" + Statement: + - Effect: Allow + Action: + - ec2:DescribeInstances + - ec2:DescribeRegions + Resource: "*" + - Effect: Allow + Action: + - "sts:AssumeRole" + Resource: !GetAtt PortkeyAM.Arn + - Effect: Allow + Action: + - "s3:GetObject" + - "s3:PutObject" + Resource: + - !Sub "arn:aws:s3:::${AWS::AccountId}-${AWS::Region}-portkey-logs/*" + - Effect: Allow + Action: + - "eks:DescribeCluster" + - "eks:ListClusters" + - "eks:ListNodegroups" + - "eks:ListFargateProfiles" + - "eks:ListNodegroups" + - "eks:CreateCluster" + - "eks:CreateNodegroup" + - "eks:DeleteCluster" + - "eks:DeleteNodegroup" + - "eks:CreateFargateProfile" + - "eks:DeleteFargateProfile" + - "eks:DescribeFargateProfile" + - "eks:UpdateClusterConfig" + - "eks:UpdateKubeconfig" + Resource: !Sub "arn:aws:eks:${AWS::Region}:${AWS::AccountId}:cluster/${ClusterName}" + + LambdaClusterAdmin: + Type: AWS::EKS::AccessEntry + DependsOn: EksCluster + Properties: + ClusterName: !Ref ClusterName + PrincipalArn: !GetAtt LambdaExecutionRole.Arn + Type: STANDARD + KubernetesGroups: + - system:masters + AccessPolicies: + - PolicyArn: "arn:aws:eks::aws:cluster-access-policy/AmazonEKSClusterAdminPolicy" + AccessScope: + Type: "cluster" + + # Node Group Role + NodeGroupRole: + Type: AWS::IAM::Role + DeletionPolicy: Delete + Properties: + RoleName: NodeGroupRole-Portkey + AssumeRolePolicyDocument: + Version: "2012-10-17" + Statement: + - Effect: Allow + Principal: + Service: ec2.amazonaws.com + Action: sts:AssumeRole + ManagedPolicyArns: + - arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy + - arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy + - arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly + + # EKS Node Group + EksNodeGroup: + Type: AWS::EKS::Nodegroup + DependsOn: EksCluster + DeletionPolicy: Delete + Properties: + CapacityType: ON_DEMAND + ClusterName: !Ref ClusterName + NodegroupName: !Ref NodeGroupName + NodeRole: !GetAtt NodeGroupRole.Arn + InstanceTypes: + - !Ref NodeGroupInstanceType + ScalingConfig: + MinSize: 1 + DesiredSize: 1 + MaxSize: 1 + Subnets: + - !Ref Subnet1ID + - !Ref Subnet2ID + + PortkeyInstallerFunction: + Type: AWS::Lambda::Function + DependsOn: EksNodeGroup + DeletionPolicy: Delete + Properties: + FunctionName: portkey-eks-installer + Runtime: nodejs18.x + Handler: index.handler + MemorySize: 1024 + EphemeralStorage: + Size: 1024 + Code: + ZipFile: | + const fs = require('fs'); + const zlib = require('zlib'); + const { pipeline } = require('stream'); + const path = require('path'); + const https = require('https'); + const { promisify } = require('util'); + const { execSync } = require('child_process'); + const { EKSClient, DescribeClusterCommand } = require('@aws-sdk/client-eks'); + + async function unzipAwsCli(zipPath, destPath) { + // ZIP file format: https://en.wikipedia.org/wiki/ZIP_(file_format) + const data = fs.readFileSync(zipPath); + let offset = 0; + + // Find end of central directory record + const EOCD_SIGNATURE = 0x06054b50; + for (let i = data.length - 22; i >= 0; i--) { + if (data.readUInt32LE(i) === EOCD_SIGNATURE) { + offset = i; + break; + } + } + + // Read central directory info + const numEntries = data.readUInt16LE(offset + 10); + let centralDirOffset = data.readUInt32LE(offset + 16); + + // Process each file + for (let i = 0; i < numEntries; i++) { + // Read central directory header + const signature = data.readUInt32LE(centralDirOffset); + if (signature !== 0x02014b50) { + throw new Error('Invalid central directory header'); + } + + const fileNameLength = data.readUInt16LE(centralDirOffset + 28); + const extraFieldLength = data.readUInt16LE(centralDirOffset + 30); + const fileCommentLength = data.readUInt16LE(centralDirOffset + 32); + const localHeaderOffset = data.readUInt32LE(centralDirOffset + 42); + + // Get filename + const fileName = data.slice( + centralDirOffset + 46, + centralDirOffset + 46 + fileNameLength + ).toString(); + + // Read local file header + const localSignature = data.readUInt32LE(localHeaderOffset); + if (localSignature !== 0x04034b50) { + throw new Error('Invalid local file header'); + } + + const localFileNameLength = data.readUInt16LE(localHeaderOffset + 26); + const localExtraFieldLength = data.readUInt16LE(localHeaderOffset + 28); + + // Get file data + const fileDataOffset = localHeaderOffset + 30 + localFileNameLength + localExtraFieldLength; + const compressedSize = data.readUInt32LE(centralDirOffset + 20); + const uncompressedSize = data.readUInt32LE(centralDirOffset + 24); + const compressionMethod = data.readUInt16LE(centralDirOffset + 10); + + // Create directory if needed + const fullPath = path.join(destPath, fileName); + const directory = path.dirname(fullPath); + if (!fs.existsSync(directory)) { + fs.mkdirSync(directory, { recursive: true }); + } + + // Extract file + if (!fileName.endsWith('/')) { // Skip directories + const fileData = data.slice(fileDataOffset, fileDataOffset + compressedSize); + + if (compressionMethod === 0) { // Stored (no compression) + fs.writeFileSync(fullPath, fileData); + } else if (compressionMethod === 8) { // Deflate + const inflated = require('zlib').inflateRawSync(fileData); + fs.writeFileSync(fullPath, inflated); + } else { + throw new Error(`Unsupported compression method: ${compressionMethod}`); + } + } + + // Move to next entry + centralDirOffset += 46 + fileNameLength + extraFieldLength + fileCommentLength; + } + } + + + async function extractTarGz(source, destination) { + // First, let's decompress the .gz file + const gunzip = promisify(zlib.gunzip); + console.log('Reading source file...'); + const compressedData = fs.readFileSync(source); + + console.log('Decompressing...'); + const tarData = await gunzip(compressedData); + + // Now we have the raw tar data + // Tar files are made up of 512-byte blocks + let position = 0; + + while (position < tarData.length) { + // Read header block + const header = tarData.slice(position, position + 512); + position += 512; + + // Get filename from header (first 100 bytes) + const filename = header.slice(0, 100) + .toString('utf8') + .replace(/\0/g, '') + .trim(); + + if (!filename) break; // End of tar + + // Get file size from header (bytes 124-136) + const sizeStr = header.slice(124, 136) + .toString('utf8') + .replace(/\0/g, '') + .trim(); + const size = parseInt(sizeStr, 8); // Size is in octal + + console.log(`Found file: ${filename} (${size} bytes)`); + + if (filename === 'linux-amd64/helm') { + console.log('Found helm binary, extracting...'); + // Extract the file content + const content = tarData.slice(position, position + size); + + // Write to destination + const outputPath = path.join(destination, 'helm'); + fs.writeFileSync(outputPath, content); + console.log(`Helm binary extracted to: ${outputPath}`); + return; // We found what we needed + } + + // Move to next file + position += size; + // Move to next 512-byte boundary + position += (512 - (size % 512)) % 512; + } + + throw new Error('Helm binary not found in archive'); + } + + async function downloadFile(url, dest) { + return new Promise((resolve, reject) => { + const file = fs.createWriteStream(dest); + https.get(url, (response) => { + response.pipe(file); + file.on('finish', () => { + file.close(); + resolve(); + }); + }).on('error', reject); + }); + } + + async function setupBinaries() { + const { STSClient, GetCallerIdentityCommand, AssumeRoleCommand } = require("@aws-sdk/client-sts"); + const { SignatureV4 } = require("@aws-sdk/signature-v4"); + const { defaultProvider } = require("@aws-sdk/credential-provider-node"); + const crypto = require('crypto'); + + const tmpDir = '/tmp/bin'; + if (!fs.existsSync(tmpDir)) { + fs.mkdirSync(tmpDir, { recursive: true }); + } + console.log('Setting up AWS CLI...'); + const awsCliUrl = 'https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip'; + const awsZipPath = `${tmpDir}/awscliv2.zip`; + await unzipAwsCli(awsZipPath, tmpDir); + execSync(`chmod +x ${tmpDir}/aws/install ${tmpDir}/aws/dist/aws`); + execSync(`${tmpDir}/aws/install --update --install-dir /tmp/aws-cli --bin-dir /tmp/aws-bin`, { stdio: 'inherit' }); + + try { + await new Promise((resolve, reject) => { + const https = require('https'); + const fs = require('fs'); + + const file = fs.createWriteStream('/tmp/kubectl'); + + const request = https.get('https://dl.k8s.io/release/v1.32.1/bin/linux/amd64/kubectl', response => { + if (response.statusCode === 302 || response.statusCode === 301) { + https.get(response.headers.location, redirectResponse => { + redirectResponse.pipe(file); + file.on('finish', () => { + file.close(); + resolve(); + }); + }).on('error', err => { + fs.unlink('/tmp/kubectl', () => {}); + reject(err); + }); + return; + } + + response.pipe(file); + file.on('finish', () => { + file.close(); + resolve(); + }); + }); + + request.on('error', err => { + fs.unlink('/tmp/kubectl', () => {}); + reject(err); + }); + }); + + execSync('chmod +x /tmp/kubectl', { + stdio: 'inherit' + }); + } catch (error) { + console.error('Error installing kubectl:', error); + throw error; + } + + console.log('Setting up helm...'); + const helmUrl = 'https://get.helm.sh/helm-v3.12.0-linux-amd64.tar.gz'; + const helmTarPath = `${tmpDir}/helm.tar.gz`; + await downloadFile(helmUrl, helmTarPath); + + await extractTarGz(helmTarPath, tmpDir); + execSync(`chmod +x ${tmpDir}/helm`); + fs.unlinkSync(helmTarPath); + + process.env.PATH = `${tmpDir}:${process.env.PATH}`; + + execSync(`/tmp/aws-bin/aws --version`); + } + + exports.handler = async (event, context) => { + try { + + const { CLUSTER_NAME, NODE_GROUP_NAME, CLUSTER_ARN, CHART_VERSION, + PORTKEY_AWS_REGION, PORTKEY_AWS_ACCOUNT_ID, PORTKEYAM_ROLE_ARN, + PORTKEY_DOCKER_USERNAME, PORTKEY_DOCKER_PASSWORD, + PORTKEY_CLIENT_AUTH, ORGANISATIONS_TO_SYNC } = process.env; + + console.log(process.env) + + if (!CLUSTER_NAME || !PORTKEY_AWS_REGION || !CHART_VERSION || + !PORTKEY_AWS_ACCOUNT_ID || !PORTKEYAM_ROLE_ARN) { + throw new Error('Missing one or more required environment variables.'); + } + + await setupBinaries(); + + const awsCredentialsDir = '/tmp/.aws'; + if (!fs.existsSync(awsCredentialsDir)) { + fs.mkdirSync(awsCredentialsDir, { recursive: true }); + } + + // Write AWS credentials file + const credentialsContent = `[default] + aws_access_key_id = ${process.env.AWS_ACCESS_KEY_ID} + aws_secret_access_key = ${process.env.AWS_SECRET_ACCESS_KEY} + aws_session_token = ${process.env.AWS_SESSION_TOKEN} + region = ${process.env.PORTKEY_AWS_REGION} + `; + + fs.writeFileSync(`${awsCredentialsDir}/credentials`, credentialsContent); + + // Write AWS config file + const configContent = `[default] + region = ${process.env.PORTKEY_AWS_REGION} + output = json + `; + fs.writeFileSync(`${awsCredentialsDir}/config`, configContent); + + // Set AWS config environment variables + process.env.AWS_CONFIG_FILE = `${awsCredentialsDir}/config`; + process.env.AWS_SHARED_CREDENTIALS_FILE = `${awsCredentialsDir}/credentials`; + + // Define kubeconfig path + const kubeconfigDir = `/tmp/${CLUSTER_NAME.trim()}`; + const kubeconfigPath = path.join(kubeconfigDir, 'config'); + + // Create the directory if it doesn't exist + if (!fs.existsSync(kubeconfigDir)) { + fs.mkdirSync(kubeconfigDir, { recursive: true }); + } + + console.log(`Updating kubeconfig for cluster: ${CLUSTER_NAME}`); + execSync(`/tmp/aws-bin/aws eks update-kubeconfig --name ${process.env.CLUSTER_NAME} --region ${process.env.PORTKEY_AWS_REGION} --kubeconfig ${kubeconfigPath}`, { + stdio: 'inherit', + env: { + ...process.env, + HOME: '/tmp', + AWS_CONFIG_FILE: `${awsCredentialsDir}/config`, + AWS_SHARED_CREDENTIALS_FILE: `${awsCredentialsDir}/credentials` + } + }); + + // Set KUBECONFIG environment variable + process.env.KUBECONFIG = kubeconfigPath; + + let kubeconfig = fs.readFileSync(kubeconfigPath, 'utf8'); + + // Replace the command line to use full path + kubeconfig = kubeconfig.replace( + 'command: aws', + 'command: /tmp/aws-bin/aws' + ); + + fs.writeFileSync(kubeconfigPath, kubeconfig); + + // Setup Helm repository + console.log('Setting up Helm repository...'); + await new Promise((resolve, reject) => { + try { + execSync(`helm repo add portkey-ai https://portkey-ai.github.io/helm`, { + stdio: 'inherit', + env: { ...process.env, HOME: '/tmp' } + }); + resolve(); + } catch (error) { + reject(error); + } + }); + + await new Promise((resolve, reject) => { + try { + execSync(`helm repo update`, { + stdio: 'inherit', + env: { ...process.env, HOME: '/tmp' } + }); + resolve(); + } catch (error) { + reject(error); + } + }); + + // Create values.yaml + const valuesYAML = ` + replicaCount: 1 + + images: + gatewayImage: + repository: "docker.io/portkeyai/gateway_enterprise" + pullPolicy: IfNotPresent + tag: "1.9.0" + dataserviceImage: + repository: "docker.io/portkeyai/data-service" + pullPolicy: IfNotPresent + tag: "1.0.2" + + imagePullSecrets: [portkeyenterpriseregistrycredentials] + nameOverride: "" + fullnameOverride: "" + + imageCredentials: + - name: portkeyenterpriseregistrycredentials + create: true + registry: https://index.docker.io/v1/ + username: ${PORTKEY_DOCKER_USERNAME} + password: ${PORTKEY_DOCKER_PASSWORD} + + useVaultInjection: false + + environment: + create: true + secret: true + data: + SERVICE_NAME: portkeyenterprise + PORT: "8787" + LOG_STORE: s3_assume + LOG_STORE_REGION: ${PORTKEY_AWS_REGION} + AWS_ROLE_ARN: ${PORTKEYAM_ROLE_ARN} + LOG_STORE_GENERATIONS_BUCKET: portkey-gateway + ANALYTICS_STORE: control_plane + CACHE_STORE: redis + REDIS_URL: redis://redis:6379 + REDIS_TLS_ENABLED: "false" + PORTKEY_CLIENT_AUTH: ${PORTKEY_CLIENT_AUTH} + ORGANISATIONS_TO_SYNC: ${ORGANISATIONS_TO_SYNC} + + serviceAccount: + create: true + automount: true + annotations: {} + name: "" + + podAnnotations: {} + podLabels: {} + + podSecurityContext: {} + securityContext: {} + + service: + type: LoadBalancer + port: 8787 + targetPort: 8787 + protocol: TCP + additionalLabels: {} + annotations: {} + + ingress: + enabled: ${PORTKEY_GATEWAY_INGRESS_ENABLED} + className: "" + annotations: {} + hosts: + - host: ${PORTKEY_GATEWAY_INGRESS_SUBDOMAIN} + paths: + - path: / + pathType: ImplementationSpecific + tls: [] + + resources: {} + + livenessProbe: + httpGet: + path: /v1/health + port: 8787 + initialDelaySeconds: 30 + periodSeconds: 60 + timeoutSeconds: 5 + failureThreshold: 5 + readinessProbe: + httpGet: + path: /v1/health + port: 8787 + initialDelaySeconds: 30 + periodSeconds: 60 + timeoutSeconds: 5 + successThreshold: 1 + failureThreshold: 5 + + autoscaling: + enabled: true + minReplicas: 1 + maxReplicas: 10 + targetCPUUtilizationPercentage: 80 + + volumes: [] + volumeMounts: [] + nodeSelector: {} + tolerations: [] + affinity: {} + autoRestart: false + + dataservice: + name: "dataservice" + enabled: ${PORTKEY_FINE_TUNING_ENABLED} + containerPort: 8081 + finetuneBucket: ${PORTKEY_AWS_ACCOUNT_ID}-${PORTKEY_AWS_REGION}-portkey-logs + logexportsBucket: ${PORTKEY_AWS_ACCOUNT_ID}-${PORTKEY_AWS_REGION}-portkey-logs + deployment: + autoRestart: true + replicas: 1 + labels: {} + annotations: {} + podSecurityContext: {} + securityContext: {} + resources: {} + startupProbe: + httpGet: + path: /health + port: 8081 + initialDelaySeconds: 60 + failureThreshold: 3 + periodSeconds: 10 + timeoutSeconds: 1 + livenessProbe: + httpGet: + path: /health + port: 8081 + failureThreshold: 3 + periodSeconds: 10 + timeoutSeconds: 1 + readinessProbe: + httpGet: + path: /health + port: 8081 + failureThreshold: 3 + periodSeconds: 10 + timeoutSeconds: 1 + extraContainerConfig: {} + nodeSelector: {} + tolerations: [] + affinity: {} + volumes: [] + volumeMounts: [] + service: + type: ClusterIP + port: 8081 + labels: {} + annotations: {} + loadBalancerSourceRanges: [] + loadBalancerIP: "" + serviceAccount: + create: true + name: "" + labels: {} + annotations: {} + autoscaling: + enabled: false + createHpa: false + minReplicas: 1 + maxReplicas: 5 + targetCPUUtilizationPercentage: 80` + + // Write values.yaml + const valuesYamlPath = '/tmp/values.yaml'; + fs.writeFileSync(valuesYamlPath, valuesYAML); + + const { S3Client, PutObjectCommand, GetObjectCommand } = require("@aws-sdk/client-s3"); + const s3Client = new S3Client({ region: process.env.PORTKEY_AWS_REGION }); + try { + const response = await s3Client.send(new GetObjectCommand({ + Bucket: `${process.env.PORTKEY_AWS_ACCOUNT_ID}-${process.env.PORTKEY_AWS_REGION}-portkey-logs`, + Key: 'values.yaml' + })); + const existingValuesYAML = await response.Body.transformToString(); + console.log('Found existing values.yaml in S3, using it instead of default'); + fs.writeFileSync(valuesYamlPath, existingValuesYAML); + } catch (error) { + if (error.name === 'NoSuchKey') { + // Upload the default values.yaml to S3 + await s3Client.send(new PutObjectCommand({ + Bucket: `${process.env.PORTKEY_AWS_ACCOUNT_ID}-${process.env.PORTKEY_AWS_REGION}-portkey-logs`, + Key: 'values.yaml', + Body: valuesYAML, + ContentType: 'text/yaml' + })); + console.log('Default values.yaml written to S3 bucket'); + } else { + throw error; + } + } + + // Install/upgrade Helm chart + console.log('Installing helm chart...'); + await new Promise((resolve, reject) => { + try { + execSync(`helm upgrade --install portkey-ai portkey-ai/gateway -f ${valuesYamlPath} -n portkeyai --create-namespace --kube-context ${process.env.CLUSTER_ARN} --kubeconfig ${kubeconfigPath}`, { + stdio: 'inherit', + env: { + ...process.env, + HOME: '/tmp', + PATH: `/tmp/aws-bin:${process.env.PATH}` + } + }); + resolve(); + } catch (error) { + reject(error); + } + }); + + return { + statusCode: 200, + body: JSON.stringify({ + message: 'EKS installation and helm chart deployment completed successfully', + event: event + }) + }; + } catch (error) { + console.error('Error:', error); + return { + statusCode: 500, + body: JSON.stringify({ + message: 'Error during EKS installation and helm chart deployment', + error: error.message + }) + }; + } + }; + Role: !GetAtt LambdaExecutionRole.Arn + Timeout: 900 + Environment: + Variables: + CLUSTER_NAME: !Ref ClusterName + NODE_GROUP_NAME: !Ref NodeGroupName + CLUSTER_ARN: !GetAtt EksCluster.Arn + CHART_VERSION: !Ref HelmChartVersion + PORTKEY_AWS_REGION: !Ref "AWS::Region" + PORTKEY_AWS_ACCOUNT_ID: !Ref "AWS::AccountId" + PORTKEYAM_ROLE_ARN: !GetAtt PortkeyAM.Arn + PORTKEY_DOCKER_USERNAME: !Ref PortkeyDockerUsername + PORTKEY_DOCKER_PASSWORD: !Ref PortkeyDockerPassword + PORTKEY_CLIENT_AUTH: !Ref PortkeyClientAuth + ORGANISATIONS_TO_SYNC: !Ref PortkeyOrgId + PORTKEY_GATEWAY_INGRESS_ENABLED: !Ref PortkeyGatewayIngressEnabled + PORTKEY_GATEWAY_INGRESS_SUBDOMAIN: !Ref PortkeyGatewayIngressSubdomain + PORTKEY_FINE_TUNING_ENABLED: !Ref PortkeyFineTuningEnabled +``` + +### Lambda Function + + + + + +#### Steps + +1. **Sets up required binaries** - Downloads and configures AWS CLI, kubectl, and Helm binaries in the Lambda environment to enable interaction with AWS services and Kubernetes. +2. **Configures AWS credentials** - Creates temporary AWS credential files in the Lambda environment to authenticate with AWS services. +3. **Connects to EKS cluster** - Updates the kubeconfig file to establish a connection with the specified Amazon EKS cluster. +4. **Manages Helm chart deployment** - Adds the Portkey AI Helm repository and deploys/upgrades the Portkey AI Gateway using Helm charts. +5. **Handles configuration values** - Creates a values.yaml file with environment-specific configurations and stores it in an S3 bucket for future reference or updates. +6. **Provides idempotent deployment** - Checks for existing configurations in S3 and uses them if available, allowing the function to be run multiple times for updates without losing custom configurations. + +```javascript portkey-hybrid-eks-cloudformation.lambda.js [expandable] + const fs = require('fs'); + const zlib = require('zlib'); + const { pipeline } = require('stream'); + const path = require('path'); + const https = require('https'); + const { promisify } = require('util'); + const { execSync } = require('child_process'); + const { EKSClient, DescribeClusterCommand } = require('@aws-sdk/client-eks'); + + async function unzipAwsCli(zipPath, destPath) { + // ZIP file format: https://en.wikipedia.org/wiki/ZIP_(file_format) + const data = fs.readFileSync(zipPath); + let offset = 0; + + // Find end of central directory record + const EOCD_SIGNATURE = 0x06054b50; + for (let i = data.length - 22; i >= 0; i--) { + if (data.readUInt32LE(i) === EOCD_SIGNATURE) { + offset = i; + break; + } + } + + // Read central directory info + const numEntries = data.readUInt16LE(offset + 10); + let centralDirOffset = data.readUInt32LE(offset + 16); + + // Process each file + for (let i = 0; i < numEntries; i++) { + // Read central directory header + const signature = data.readUInt32LE(centralDirOffset); + if (signature !== 0x02014b50) { + throw new Error('Invalid central directory header'); + } + + const fileNameLength = data.readUInt16LE(centralDirOffset + 28); + const extraFieldLength = data.readUInt16LE(centralDirOffset + 30); + const fileCommentLength = data.readUInt16LE(centralDirOffset + 32); + const localHeaderOffset = data.readUInt32LE(centralDirOffset + 42); + + // Get filename + const fileName = data.slice( + centralDirOffset + 46, + centralDirOffset + 46 + fileNameLength + ).toString(); + + // Read local file header + const localSignature = data.readUInt32LE(localHeaderOffset); + if (localSignature !== 0x04034b50) { + throw new Error('Invalid local file header'); + } + + const localFileNameLength = data.readUInt16LE(localHeaderOffset + 26); + const localExtraFieldLength = data.readUInt16LE(localHeaderOffset + 28); + + // Get file data + const fileDataOffset = localHeaderOffset + 30 + localFileNameLength + localExtraFieldLength; + const compressedSize = data.readUInt32LE(centralDirOffset + 20); + const uncompressedSize = data.readUInt32LE(centralDirOffset + 24); + const compressionMethod = data.readUInt16LE(centralDirOffset + 10); + + // Create directory if needed + const fullPath = path.join(destPath, fileName); + const directory = path.dirname(fullPath); + if (!fs.existsSync(directory)) { + fs.mkdirSync(directory, { recursive: true }); + } + + // Extract file + if (!fileName.endsWith('/')) { // Skip directories + const fileData = data.slice(fileDataOffset, fileDataOffset + compressedSize); + + if (compressionMethod === 0) { // Stored (no compression) + fs.writeFileSync(fullPath, fileData); + } else if (compressionMethod === 8) { // Deflate + const inflated = require('zlib').inflateRawSync(fileData); + fs.writeFileSync(fullPath, inflated); + } else { + throw new Error(`Unsupported compression method: ${compressionMethod}`); + } + } + + // Move to next entry + centralDirOffset += 46 + fileNameLength + extraFieldLength + fileCommentLength; + } + } + + + async function extractTarGz(source, destination) { + // First, let's decompress the .gz file + const gunzip = promisify(zlib.gunzip); + console.log('Reading source file...'); + const compressedData = fs.readFileSync(source); + + console.log('Decompressing...'); + const tarData = await gunzip(compressedData); + + // Now we have the raw tar data + // Tar files are made up of 512-byte blocks + let position = 0; + + while (position < tarData.length) { + // Read header block + const header = tarData.slice(position, position + 512); + position += 512; + + // Get filename from header (first 100 bytes) + const filename = header.slice(0, 100) + .toString('utf8') + .replace(/\0/g, '') + .trim(); + + if (!filename) break; // End of tar + + // Get file size from header (bytes 124-136) + const sizeStr = header.slice(124, 136) + .toString('utf8') + .replace(/\0/g, '') + .trim(); + const size = parseInt(sizeStr, 8); // Size is in octal + + console.log(`Found file: ${filename} (${size} bytes)`); + + if (filename === 'linux-amd64/helm') { + console.log('Found helm binary, extracting...'); + // Extract the file content + const content = tarData.slice(position, position + size); + + // Write to destination + const outputPath = path.join(destination, 'helm'); + fs.writeFileSync(outputPath, content); + console.log(`Helm binary extracted to: ${outputPath}`); + return; // We found what we needed + } + + // Move to next file + position += size; + // Move to next 512-byte boundary + position += (512 - (size % 512)) % 512; + } + + throw new Error('Helm binary not found in archive'); + } + + async function downloadFile(url, dest) { + return new Promise((resolve, reject) => { + const file = fs.createWriteStream(dest); + https.get(url, (response) => { + response.pipe(file); + file.on('finish', () => { + file.close(); + resolve(); + }); + }).on('error', reject); + }); + } + + async function setupBinaries() { + const { STSClient, GetCallerIdentityCommand, AssumeRoleCommand } = require("@aws-sdk/client-sts"); + const { SignatureV4 } = require("@aws-sdk/signature-v4"); + const { defaultProvider } = require("@aws-sdk/credential-provider-node"); + const crypto = require('crypto'); + + const tmpDir = '/tmp/bin'; + if (!fs.existsSync(tmpDir)) { + fs.mkdirSync(tmpDir, { recursive: true }); + } + + // Download and setup AWS CLI + console.log('Setting up AWS CLI...'); + const awsCliUrl = 'https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip'; + const awsZipPath = `${tmpDir}/awscliv2.zip`; + await downloadFile(awsCliUrl, awsZipPath); + // Extract using our custom unzip function + await unzipAwsCli(awsZipPath, tmpDir); + execSync(`chmod +x ${tmpDir}/aws/install ${tmpDir}/aws/dist/aws`); + // Install AWS CLI + execSync(`${tmpDir}/aws/install --update --install-dir /tmp/aws-cli --bin-dir /tmp/aws-bin`, { stdio: 'inherit' }); + + // Download and setup kubectl + try { + // Download kubectl binary using Node.js https + await new Promise((resolve, reject) => { + const https = require('https'); + const fs = require('fs'); + + const file = fs.createWriteStream('/tmp/kubectl'); + + const request = https.get('https://dl.k8s.io/release/v1.32.1/bin/linux/amd64/kubectl', response => { + if (response.statusCode === 302 || response.statusCode === 301) { + https.get(response.headers.location, redirectResponse => { + redirectResponse.pipe(file); + file.on('finish', () => { + file.close(); + resolve(); + }); + }).on('error', err => { + fs.unlink('/tmp/kubectl', () => {}); + reject(err); + }); + return; + } + + response.pipe(file); + file.on('finish', () => { + file.close(); + resolve(); + }); + }); + + request.on('error', err => { + fs.unlink('/tmp/kubectl', () => {}); + reject(err); + }); + }); + + execSync('chmod +x /tmp/kubectl', { + stdio: 'inherit' + }); + } catch (error) { + console.error('Error installing kubectl:', error); + throw error; + } + + console.log('Setting up helm...'); + const helmUrl = 'https://get.helm.sh/helm-v3.12.0-linux-amd64.tar.gz'; + const helmTarPath = `${tmpDir}/helm.tar.gz`; + await downloadFile(helmUrl, helmTarPath); + + await extractTarGz(helmTarPath, tmpDir); + execSync(`chmod +x ${tmpDir}/helm`); + fs.unlinkSync(helmTarPath); + + process.env.PATH = `${tmpDir}:${process.env.PATH}`; + + execSync(`/tmp/aws-bin/aws --version`); + } + + exports.handler = async (event, context) => { + try { + + const { CLUSTER_NAME, NODE_GROUP_NAME, CLUSTER_ARN, CHART_VERSION, + PORTKEY_AWS_REGION, PORTKEY_AWS_ACCOUNT_ID, PORTKEYAM_ROLE_ARN, + PORTKEY_DOCKER_USERNAME, PORTKEY_DOCKER_PASSWORD, + PORTKEY_CLIENT_AUTH, ORGANISATIONS_TO_SYNC } = process.env; + + console.log(process.env) + + if (!CLUSTER_NAME || !PORTKEY_AWS_REGION || !CHART_VERSION || + !PORTKEY_AWS_ACCOUNT_ID || !PORTKEYAM_ROLE_ARN) { + throw new Error('Missing one or more required environment variables.'); + } + + await setupBinaries(); + + const awsCredentialsDir = '/tmp/.aws'; + if (!fs.existsSync(awsCredentialsDir)) { + fs.mkdirSync(awsCredentialsDir, { recursive: true }); + } + + // Write AWS credentials file + const credentialsContent = `[default] + aws_access_key_id = ${process.env.AWS_ACCESS_KEY_ID} + aws_secret_access_key = ${process.env.AWS_SECRET_ACCESS_KEY} + aws_session_token = ${process.env.AWS_SESSION_TOKEN} + region = ${process.env.PORTKEY_AWS_REGION} + `; + + fs.writeFileSync(`${awsCredentialsDir}/credentials`, credentialsContent); + + // Write AWS config file + const configContent = `[default] + region = ${process.env.PORTKEY_AWS_REGION} + output = json + `; + fs.writeFileSync(`${awsCredentialsDir}/config`, configContent); + + // Set AWS config environment variables + process.env.AWS_CONFIG_FILE = `${awsCredentialsDir}/config`; + process.env.AWS_SHARED_CREDENTIALS_FILE = `${awsCredentialsDir}/credentials`; + + // Define kubeconfig path + const kubeconfigDir = `/tmp/${CLUSTER_NAME.trim()}`; + const kubeconfigPath = path.join(kubeconfigDir, 'config'); + + // Create the directory if it doesn't exist + if (!fs.existsSync(kubeconfigDir)) { + fs.mkdirSync(kubeconfigDir, { recursive: true }); + } + + console.log(`Updating kubeconfig for cluster: ${CLUSTER_NAME}`); + execSync(`/tmp/aws-bin/aws eks update-kubeconfig --name ${process.env.CLUSTER_NAME} --region ${process.env.PORTKEY_AWS_REGION} --kubeconfig ${kubeconfigPath}`, { + stdio: 'inherit', + env: { + ...process.env, + HOME: '/tmp', + AWS_CONFIG_FILE: `${awsCredentialsDir}/config`, + AWS_SHARED_CREDENTIALS_FILE: `${awsCredentialsDir}/credentials` + } + }); + + // Set KUBECONFIG environment variable + process.env.KUBECONFIG = kubeconfigPath; + + let kubeconfig = fs.readFileSync(kubeconfigPath, 'utf8'); + + // Replace the command line to use full path + kubeconfig = kubeconfig.replace( + 'command: aws', + 'command: /tmp/aws-bin/aws' + ); + + fs.writeFileSync(kubeconfigPath, kubeconfig); + + // Setup Helm repository + console.log('Setting up Helm repository...'); + await new Promise((resolve, reject) => { + try { + execSync(`helm repo add portkey-ai https://portkey-ai.github.io/helm`, { + stdio: 'inherit', + env: { ...process.env, HOME: '/tmp' } + }); + resolve(); + } catch (error) { + reject(error); + } + }); + + await new Promise((resolve, reject) => { + try { + execSync(`helm repo update`, { + stdio: 'inherit', + env: { ...process.env, HOME: '/tmp' } + }); + resolve(); + } catch (error) { + reject(error); + } + }); + + // Create values.yaml + const valuesYAML = ` + replicaCount: 1 + + images: + gatewayImage: + repository: "docker.io/portkeyai/gateway_enterprise" + pullPolicy: IfNotPresent + tag: "1.9.0" + dataserviceImage: + repository: "docker.io/portkeyai/data-service" + pullPolicy: IfNotPresent + tag: "1.0.2" + + imagePullSecrets: [portkeyenterpriseregistrycredentials] + nameOverride: "" + fullnameOverride: "" + + imageCredentials: + - name: portkeyenterpriseregistrycredentials + create: true + registry: https://index.docker.io/v1/ + username: ${PORTKEY_DOCKER_USERNAME} + password: ${PORTKEY_DOCKER_PASSWORD} + + useVaultInjection: false + + environment: + create: true + secret: true + data: + SERVICE_NAME: portkeyenterprise + PORT: "8787" + LOG_STORE: s3_assume + LOG_STORE_REGION: ${PORTKEY_AWS_REGION} + AWS_ROLE_ARN: ${PORTKEYAM_ROLE_ARN} + LOG_STORE_GENERATIONS_BUCKET: portkey-gateway + ANALYTICS_STORE: control_plane + CACHE_STORE: redis + REDIS_URL: redis://redis:6379 + REDIS_TLS_ENABLED: "false" + PORTKEY_CLIENT_AUTH: ${PORTKEY_CLIENT_AUTH} + ORGANISATIONS_TO_SYNC: ${ORGANISATIONS_TO_SYNC} + + serviceAccount: + create: true + automount: true + annotations: {} + name: "" + + podAnnotations: {} + podLabels: {} + + podSecurityContext: {} + securityContext: {} + + service: + type: LoadBalancer + port: 8787 + targetPort: 8787 + protocol: TCP + additionalLabels: {} + annotations: {} + + ingress: + enabled: ${PORTKEY_GATEWAY_INGRESS_ENABLED} + className: "" + annotations: {} + hosts: + - host: ${PORTKEY_GATEWAY_INGRESS_SUBDOMAIN} + paths: + - path: / + pathType: ImplementationSpecific + tls: [] + + resources: {} + + livenessProbe: + httpGet: + path: /v1/health + port: 8787 + initialDelaySeconds: 30 + periodSeconds: 60 + timeoutSeconds: 5 + failureThreshold: 5 + readinessProbe: + httpGet: + path: /v1/health + port: 8787 + initialDelaySeconds: 30 + periodSeconds: 60 + timeoutSeconds: 5 + successThreshold: 1 + failureThreshold: 5 + + autoscaling: + enabled: true + minReplicas: 1 + maxReplicas: 10 + targetCPUUtilizationPercentage: 80 + + volumes: [] + volumeMounts: [] + nodeSelector: {} + tolerations: [] + affinity: {} + autoRestart: false + + dataservice: + name: "dataservice" + enabled: ${PORTKEY_FINE_TUNING_ENABLED} + containerPort: 8081 + finetuneBucket: ${PORTKEY_AWS_ACCOUNT_ID}-${PORTKEY_AWS_REGION}-portkey-logs + logexportsBucket: ${PORTKEY_AWS_ACCOUNT_ID}-${PORTKEY_AWS_REGION}-portkey-logs + deployment: + autoRestart: true + replicas: 1 + labels: {} + annotations: {} + podSecurityContext: {} + securityContext: {} + resources: {} + startupProbe: + httpGet: + path: /health + port: 8081 + initialDelaySeconds: 60 + failureThreshold: 3 + periodSeconds: 10 + timeoutSeconds: 1 + livenessProbe: + httpGet: + path: /health + port: 8081 + failureThreshold: 3 + periodSeconds: 10 + timeoutSeconds: 1 + readinessProbe: + httpGet: + path: /health + port: 8081 + failureThreshold: 3 + periodSeconds: 10 + timeoutSeconds: 1 + extraContainerConfig: {} + nodeSelector: {} + tolerations: [] + affinity: {} + volumes: [] + volumeMounts: [] + service: + type: ClusterIP + port: 8081 + labels: {} + annotations: {} + loadBalancerSourceRanges: [] + loadBalancerIP: "" + serviceAccount: + create: true + name: "" + labels: {} + annotations: {} + autoscaling: + enabled: false + createHpa: false + minReplicas: 1 + maxReplicas: 5 + targetCPUUtilizationPercentage: 80` + + // Write values.yaml + const valuesYamlPath = '/tmp/values.yaml'; + fs.writeFileSync(valuesYamlPath, valuesYAML); + + const { S3Client, PutObjectCommand, GetObjectCommand } = require("@aws-sdk/client-s3"); + const s3Client = new S3Client({ region: process.env.PORTKEY_AWS_REGION }); + try { + const response = await s3Client.send(new GetObjectCommand({ + Bucket: `${process.env.PORTKEY_AWS_ACCOUNT_ID}-${process.env.PORTKEY_AWS_REGION}-portkey-logs`, + Key: 'values.yaml' + })); + const existingValuesYAML = await response.Body.transformToString(); + console.log('Found existing values.yaml in S3, using it instead of default'); + fs.writeFileSync(valuesYamlPath, existingValuesYAML); + } catch (error) { + if (error.name === 'NoSuchKey') { + // Upload the default values.yaml to S3 + await s3Client.send(new PutObjectCommand({ + Bucket: `${process.env.PORTKEY_AWS_ACCOUNT_ID}-${process.env.PORTKEY_AWS_REGION}-portkey-logs`, + Key: 'values.yaml', + Body: valuesYAML, + ContentType: 'text/yaml' + })); + console.log('Default values.yaml written to S3 bucket'); + } else { + throw error; + } + } + + // Install/upgrade Helm chart + console.log('Installing helm chart...'); + await new Promise((resolve, reject) => { + try { + execSync(`helm upgrade --install portkey-ai portkey-ai/gateway -f ${valuesYamlPath} -n portkeyai --create-namespace --kube-context ${process.env.CLUSTER_ARN} --kubeconfig ${kubeconfigPath}`, { + stdio: 'inherit', + env: { + ...process.env, + HOME: '/tmp', + PATH: `/tmp/aws-bin:${process.env.PATH}` + } + }); + resolve(); + } catch (error) { + reject(error); + } + }); + + return { + statusCode: 200, + body: JSON.stringify({ + message: 'EKS installation and helm chart deployment completed successfully', + event: event + }) + }; + } catch (error) { + console.error('Error:', error); + return { + statusCode: 500, + body: JSON.stringify({ + message: 'Error during EKS installation and helm chart deployment', + error: error.message + }) + }; + } + }; +``` + +### Post Deployment Verification + +#### Verify Portkey AI Deployment + +```bash +kubectl get all -n portkeyai +``` + + + + + + +#### Verify Portkey AI Gateway Endpoint + +```bash +export POD_NAME=$(kubectl get pods -n portkeyai -l app.kubernetes.io/name=gateway -o jsonpath="{.items[0].metadata.name}") +kubectl port-forward $POD_NAME 8787:8787 -n portkeyai +``` + +Visiting localhost:8787/v1/health will return `Server is healthy` + + + + + +Your Portkey AI Gateway is now ready to use! diff --git a/virtual_key_old/product/enterprise-offering/components.mdx b/virtual_key_old/product/enterprise-offering/components.mdx new file mode 100644 index 00000000..970f6671 --- /dev/null +++ b/virtual_key_old/product/enterprise-offering/components.mdx @@ -0,0 +1,55 @@ +--- +title: "Enterprise Components" +--- + +Portkey's Enterprise Components provide the core infrastructure needed for production deployments. Each component handles a specific function - analytics, logging, or caching - with multiple implementation options to match your requirements. + +--- + +## Analytics Store + +Portkey leverages Clickhouse as the primary Analytics Store for the Control Panel, offering powerful capabilities for handling large-scale analytical workloads. + + + + + + + Portkey supports exporting your Clickhouse analytics data to OpenTelemetry (OTEL) compatible collectors, allowing you to integrate Portkey's analytics with your existing observability infrastructure. + + + +--- + +## Log Store + +Portkey provides flexible options for storing and managing logs in your enterprise deployment. Choose from various storage solutions including MongoDB for document-based storage, AWS S3 for cloud-native object storage, or Wasabi for cost-effective cloud storage. Each option offers different benefits in terms of scalability, cost, and integration capabilities. + + + + + + + +--- + +## Cache Store + +Portkey supports robust caching solutions to optimize performance and reduce latency in your enterprise deployment. Choose between Redis for in-memory caching or AWS ElastiCache for a fully managed caching service. + + + + + + + +## Semantic Cache Store + +Portkey supports semantic caching capabilities that allow for intelligent caching based on the semantic meaning of requests rather than exact matches. For this functionality, Portkey requires a vector database to efficiently store and retrieve embeddings. Both Milvus and Pinecone are supported vector databases for Portkey's semantic cache implementation. + + +- You need a **Milvus** deployment with a collection configured with 1536 dimensions + +- **Pinecone** can be configured with a Pinecone index of 1536 dimensions to store the embeddings + +This specific dimension size is required as Portkey uses OpenAI's text-embedding-small model to create embeddings diff --git a/virtual_key_old/product/enterprise-offering/kms.mdx b/virtual_key_old/product/enterprise-offering/kms.mdx new file mode 100644 index 00000000..7daf2e33 --- /dev/null +++ b/virtual_key_old/product/enterprise-offering/kms.mdx @@ -0,0 +1,109 @@ +--- +title: "KMS Integration" +description: Customers can bring their own encryption keys to Portkey AI to encrypt data at storage. +--- +This document outlines how customers can bring their own encryption keys to Portkey AI to encrypt data at storage. + +## Overview + +Portkey AI supports integration with Key Management Services (KMS) to encrypt data at storage. This integration allows customers to manage their encryption keys and data protection policies through their existing KMS infrastructure. + +## Supported KMS Providers + +Portkey AI supports integration with the following KMS providers: + +- AWS KMS + +### Encryption Methodology + +Envelope encryption is used to encrypt data at storage. The data is encrypted with a key that is stored in the KMS provider. + +```mermaid +sequenceDiagram + participant Application + participant AWS_KMS as AWS KMS (CMK) + participant Encrypted_Storage + + Application->>AWS_KMS: Request DEK (GenerateDataKey) + AWS_KMS-->>Application: Returns Plaintext DEK + Encrypted DEK + Application->>Encrypted_Storage: Encrypt Data with DEK + Application->>Encrypted_Storage: Store Encrypted Data + Encrypted DEK + Application->>AWS_KMS: Decrypt Encrypted DEK + AWS_KMS-->>Application: Returns Plaintext DEK + Application->>Application: Decrypt Data using DEK +``` + +### Encrypted Fields +- Configs + - Full template +- Virtual Keys + - Auth Key + - Auth Configuration +- Prompts + - Full Template +- Prompt Partials + - Full Template +- Guardrails + - Checks + - Actions +- Integrations/Plugins + - Auth Credentials/Keys +- SSO/OAuth + - Client Secret + - Auth Settings + +### Integration Steps: + +Integrating with a KMS provider requires the following steps: +1. Create a KMS key in your KMS provider. +2. Update the key policy to allow Portkey AI to access the key. +3. Share the ARN of the key with the Portkey AI team. + +For AWS KMS, the Portkey Account ARN is: +```sh Portkey Account ARN +arn:aws:iam::299329113195:role/EnterpriseKMSPolicy +``` + + +The above ARN only works when control plane is hosted on [hosted app](https://app.portkey.ai/).
+ +To enable KMS for AWS in your Portkey Enterprise self hosted control plane deployment. Please reach out to your Portkey representative or contact us on support@portkey.ai. +
+ +## AWS KMS Key Creation Guide + +1. Go to **Key Management Service (KMS)** in the AWS console and navigate to **Customer Managed Keys**. +2. Click on **Create Key**. +3. Select: + - **Key Type**: Symmetric + - **Key Usage**: Encrypt and Decrypt +4. Name the key according to your criteria. +5. Define key administrative permissions according to your criteria. +6. Define key usage permissions according to your criteria. +7. Once created, update the **Key Policy** with the following policy: + +```json +{ + "Version": "2012-10-17", + "Id": "key-consolepolicy-3", + "Statement": [ + { + "Sid": "Allow use of the key", + "Effect": "Allow", + "Principal": { + "AWS": "arn:aws:iam::299329113195:role/EnterpriseKMSPolicy" + }, + "Action": [ + "kms:Encrypt", + "kms:Decrypt", + "kms:ReEncrypt*", + "kms:GenerateDataKey*", + "kms:DescribeKey" + ], + "Resource": "*" + } + ] +} +``` + +8. Update the Key Arn in Portkey AI Admin Settings. diff --git a/virtual_key_old/product/enterprise-offering/logs-export.mdx b/virtual_key_old/product/enterprise-offering/logs-export.mdx new file mode 100644 index 00000000..b8b73a95 --- /dev/null +++ b/virtual_key_old/product/enterprise-offering/logs-export.mdx @@ -0,0 +1,4 @@ +--- +title: Logs Export +url: /product/observability/logs-export +--- diff --git a/virtual_key_old/product/enterprise-offering/org-management.mdx b/virtual_key_old/product/enterprise-offering/org-management.mdx new file mode 100644 index 00000000..39a09775 --- /dev/null +++ b/virtual_key_old/product/enterprise-offering/org-management.mdx @@ -0,0 +1,34 @@ +--- +title: "Org Management" +description: "A high-level introduction to Portkey's organization management structure and key concepts." +--- + +Portkey's organization management structure provides a hierarchical system for managing teams, resources, and access within your AI development environment. This structure is designed to offer flexibility and security for enterprises of various sizes. + +The account hierarchy in Portkey is organized as follows: + + + + + +This hierarchy allows for efficient management of resources and access control across your organization. At the top level, you have your Account, which can contain one or more Organizations. Each Organization can have multiple Workspaces, providing a way to separate teams, projects, or departments within your company. + + + **Workspaces** is currently a feature only enabled on the **Enterprise Plans**. If you're looking to add this feature to your organisation, please reach out to us on our [Discord Community](https://portkey.ai/community) or via email on [support@portkey.ai](mailto:support@portkey.ai) + + +Organizations contain User Invites & Users, Admin API Keys, and Workspaces. Workspaces, in turn, have their own Team structure (with Managers and Members), Workspace API Keys, and various features like Virtual Keys, Configs, Prompts, and more. + +This structure enables you to: + +* Maintain overall control at the Organization level +* Delegate responsibilities and access at the Workspace level +* Ensure data separation and project scoping +* Manage teams efficiently across different projects or departments + + + + + + + diff --git a/virtual_key_old/product/enterprise-offering/org-management/api-keys-authn-and-authz.mdx b/virtual_key_old/product/enterprise-offering/org-management/api-keys-authn-and-authz.mdx new file mode 100644 index 00000000..1e8befba --- /dev/null +++ b/virtual_key_old/product/enterprise-offering/org-management/api-keys-authn-and-authz.mdx @@ -0,0 +1,196 @@ +--- +title: "API Keys (AuthN and AuthZ)" +description: "Discover how Admin and Workspace API Keys are used to manage access and operations in Portkey." +--- + +## API Keys + +Portkey uses two types of API keys to manage access to resources and operations: **Admin API Keys** and **Workspace API Keys**. These keys play crucial roles in authenticating and authorizing various operations within your [organization](/product/enterprise-offering/org-management/organizations) and [workspaces](/api-reference/admin-api/control-plane/admin/workspaces/create-workspace). + +### Admin API Keys + +Admin API Keys operate at the organization level and provide broad access across all workspaces within an organization. + +Key features of Admin API Keys: + +* Created and managed by organization owners and admins +* Provide access to organization-wide operations +* Can perform actions across all workspaces in the organization +* Used for administrative tasks and integrations that require broad access +* When making updates to entities, can specify a workspace\_id to target specific workspaces + +Admin API Keys should be carefully managed and their use should be limited to necessary administrative operations due to their broad scope of access. + +#### Admin API Key Permission Scopes + + + + + | Scope | Description | + |-------|-------------| + | `organisation_users.create` | Create new users in the organization | + | `organisation_users.read` | View organization user details | + | `organisation_users.update` | Modify organization user settings | + | `organisation_users.delete` | Remove users from the organization | + | `organisation_users.list` | List all organization users | + | `organisation_service_api_keys.create` | Create new organization service API keys | + | `organisation_service_api_keys.update` | Modify organization service API keys | + | `organisation_service_api_keys.read` | View organization service API key details | + | `organisation_service_api_keys.delete` | Delete organization service API keys | + | `organisation_service_api_keys.list` | List all organization service API keys | + | `audit_logs.list` | Access system-wide audit logs | + + + + | Scope | Description | + |-------|-------------| + | `workspaces.create` | Create new workspaces | + | `workspaces.read` | View workspace details | + | `workspaces.update` | Modify workspace settings | + | `workspaces.delete` | Delete workspaces | + | `workspaces.list` | List all workspaces | + | `workspace_service_api_keys.create` | Create new workspace service API keys | + | `workspace_service_api_keys.update` | Modify workspace service API keys | + | `workspace_service_api_keys.read` | View workspace service API key details | + | `workspace_service_api_keys.delete` | Delete workspace service API keys | + | `workspace_service_api_keys.list` | List all workspace service API keys | + | `workspace_user_api_keys.create` | Create new workspace user API keys | + | `workspace_user_api_keys.update` | Modify workspace user API keys | + | `workspace_user_api_keys.read` | View workspace user API key details | + | `workspace_user_api_keys.delete` | Delete workspace user API keys | + | `workspace_user_api_keys.list` | List all workspace user API keys | + | `workspace_users.create` | Create new workspace users | + | `workspace_users.read` | View workspace user details | + | `workspace_users.update` | Update workspace user settings | + | `workspace_users.delete` | Remove users from workspace | + | `workspace_users.list` | List workspace users | + + + + | Scope | Description | + |-------|-------------| + | `prompts.create` | Create new prompt templates | + | `prompts.read` | View prompt template details | + | `prompts.update` | Modify existing prompt templates | + | `prompts.delete` | Delete prompt templates | + | `prompts.list` | List available prompt templates | + | `prompts.publish` | Publish prompt templates | + | `configs.create` | Create new configurations | + | `configs.update` | Update existing configurations | + | `configs.delete` | Delete configurations | + | `configs.read` | View configuration details | + | `configs.list` | List available configurations | + | `virtual_keys.create` | Create new virtual keys | + | `virtual_keys.update` | Update existing virtual keys | + | `virtual_keys.delete` | Delete virtual keys | + | `virtual_keys.duplicate` | Duplicate existing virtual keys | + | `virtual_keys.read` | View virtual key details | + | `virtual_keys.list` | List available virtual keys | + | `virtual_keys.copy` | Copy virtual keys between workspaces | + + + + | Scope | Description | + |-------|-------------| + | `analytics.view` | Access analytics data | + | `logs.export` | Export logs to external systems | + | `logs.list` | List available logs | + | `logs.view` | View log details | + + + +### Workspace API Keys + +Workspace API Keys are scoped to a specific workspace and are used for operations within that workspace only. + +Key features of Workspace API Keys: + +* Two types: Service Account and User + * Service Account: Used for automated processes and integrations + * User: Associated with individual user accounts for personal access +* Scoped to a single workspace by default +* Can only execute actions within the workspace they belong to +* Used for most day-to-day operations and integrations within a workspace +* Completion APIs are always scoped by workspace and can only be accessed using Workspace API Keys +* Can be created and managed by workspace managers + +Workspace API Keys provide a more granular level of access control, allowing you to manage permissions and resource usage at the project or team level. + +#### Workspace API Key Permission Scopes + + + + | Scope | Description | + |-------|-------------| + | `workspaces.read` | View workspace details | + | `workspaces.update` | Modify workspace settings | + | `workspaces.list` | List available workspaces | + | `workspace_service_api_keys.create` | Create new workspace service API keys | + | `workspace_service_api_keys.update` | Modify workspace service API keys | + | `workspace_service_api_keys.read` | View workspace service API key details | + | `workspace_service_api_keys.delete` | Delete workspace service API keys | + | `workspace_service_api_keys.list` | List all workspace service API keys | + | `workspace_user_api_keys.create` | Create new workspace user API keys | + | `workspace_user_api_keys.update` | Modify workspace user API keys | + | `workspace_user_api_keys.read` | View workspace user API key details | + | `workspace_user_api_keys.delete` | Delete workspace user API keys | + | `workspace_user_api_keys.list` | List all workspace user API keys | + | `workspace_users.create` | Create new workspace users | + | `workspace_users.read` | View workspace user details | + | `workspace_users.update` | Update workspace user settings | + | `workspace_users.delete` | Remove users from workspace | + | `workspace_users.list` | List workspace users | + + + + | Scope | Description | + |-------|-------------| + | `completions.write` | Create and manage completions (includes /chat/completions, /completions, /images, /audio) | + | `prompts.create` | Create new prompt templates | + | `prompts.read` | View prompt template details | + | `prompts.update` | Modify existing prompt templates | + | `prompts.delete` | Delete prompt templates | + | `prompts.list` | List available prompt templates | + | `prompts.publish` | Publish prompt templates | + | `prompts.render` | Render prompt templates | + + + + | Scope | Description | + |-------|-------------| + | `configs.create` | Create new configurations | + | `configs.update` | Update existing configurations | + | `configs.delete` | Delete configurations | + | `configs.read` | View configuration details | + | `configs.list` | List available configurations | + | `virtual_keys.create` | Create new virtual keys | + | `virtual_keys.update` | Update existing virtual keys | + | `virtual_keys.delete` | Delete virtual keys | + | `virtual_keys.duplicate` | Duplicate existing virtual keys | + | `virtual_keys.read` | View virtual key details | + | `virtual_keys.list` | List available virtual keys | + | `virtual_keys.copy` | Copy virtual keys between workspaces | + + + + | Scope | Description | + |-------|-------------| + | `analytics.view` | Access analytics data | + | `logs.export` | Export logs to external systems | + | `logs.list` | List available logs | + | `logs.view` | View log details | + | `logs.write` | Create and modify logs | + + + +## Using API Key Scopes + +When creating or updating API keys, you can define specific permission scopes based on your security requirements: + +Both types of API keys play important roles in Portkey's security model, enabling secure and efficient access to resources while maintaining proper separation of concerns between organization-wide administration and workspace-specific operations. + +### Related Topics + + + + diff --git a/virtual_key_old/product/enterprise-offering/org-management/jwt.mdx b/virtual_key_old/product/enterprise-offering/org-management/jwt.mdx new file mode 100644 index 00000000..046c305b --- /dev/null +++ b/virtual_key_old/product/enterprise-offering/org-management/jwt.mdx @@ -0,0 +1,334 @@ +--- +title: JWT Authentication +description: Configure JWT-based authentication for your organization in Portkey +--- + + + This feature is available only on the [Enterprise Plan](/product/enterprise-offering) of Portkey. + + +Portkey supports JWT-based authentication in addition to API Key authentication. Clients can authenticate API requests using a JWT token, which is validated against a configured JWKS (JSON Web Key Set). This guide explains the requirements and setup process for JWT authentication in Portkey. + + + Validate your JWT Token before making a LLM request using Portkey. + + +## Configuring JWT Authentication + +JWT authentication can be configured under **Admin Settings** โ†’ **Organisation** โ†’ **Authentication**. + + + + + + + +### JWKS Configuration + +To validate JWTs, you must configure one of the following: + +- **JWKS URL**: A URL from which the public keys will be dynamically fetched. +- **JWKS JSON**: A static JSON containing public keys. + +## JWT Requirements + +### Supported Algorithm + +- JWTs must be signed using **RS256** (RSA Signature with SHA-256). + +### Required Claims + +Your JWT payload must contain the following claims: + +| **Claim Key** | **Description** | +|------------------------------------|------------------------------------------| +| `portkey_oid` / `organisation_id` | Unique identifier for the organization. | +| `portkey_workspace` / `workspace_slug` | Identifier for the workspace. | +| `scope` / `scopes` | Permissions granted by the token. | + +### User Identification + +Portkey identifies users in the following order of precedence for logging and metrics: + +1. `email_id` +2. `sub` +3. `uid` + +## Authentication Process + +1. The client sends an HTTP request with the JWT in the `x-portkey-api-key` header: + + ```http + x-portkey-api-key: + ``` + +2. The server validates the JWT: + - Verifies the signature using the JWKS. + - Checks if the token is expired. + - Ensures the required claims are present. + +3. If valid, the request is authenticated, and user details are extracted for authorization and logging. +4. If invalid, the request is rejected with an HTTP **401 Unauthorized** response. + +## Authorization & Scopes + +Once the JWT is validated, the server checks for the required **scope**. Scopes can be provided in the JWT as either a single string or an array of strings using the `scope` or `scopes` claim. + + + + + View workspace details + + + Modify workspace settings + + + List available workspaces + + + + + + Export logs to external systems + + + List available logs + + + View log details + + + Create and modify logs + + + Access analytics data + + + + + + Create new configurations + + + Update existing configurations + + + Delete configurations + + + View configuration details + + + List available configurations + + + + + + Create new virtual keys + + + Update existing virtual keys + + + Delete virtual keys + + + Duplicate existing virtual keys + + + View virtual key details + + + List available virtual keys + + + Copy virtual keys between workspaces + + + + + + Create new workspace users + + + View workspace user details + + + Update workspace user settings + + + Remove users from workspace + + + List workspace users + + + + + + Render prompt templates + + + Create and manage completions + + + + +Scopes can also be prefixed with `portkey.` (e.g., `portkey.completions.write`). + + + + JWT tokens with appropriate scopes function identically to workspace API keys, providing access to workspace-specific operations. They cannot be used as organization API keys, which have broader administrative permissions across all workspaces. + + +## Example JWT Payload + +```json +{ + "portkey_oid" : "org_123456", + "portkey_workspace": "workspace_abc", + "scope": ["completions.write", "logs.view"], + "email_id": "user@example.com", + "sub": "user-123", + "exp": 1735689600 +} +``` + +## Making API Calls with JWT Authentication + +Once you have a valid JWT token, you can use it to authenticate your API calls to Portkey. Below are examples showing how to use JWT authentication with different SDKs. + + + +Install the Portkey SDK with npm +```sh +npm install portkey-ai +``` + +```ts Chat Completions +import Portkey from 'portkey-ai'; + +const client = new Portkey({ + apiKey: '', // Use JWT token instead of API key +}); + +async function main() { + const response = await client.chat.completions.create({ + messages: [{ role: "user", content: "Hello, how are you today?" }], + model: "gpt-4o", + }); + + console.log(response.choices[0].message.content); +} + +main(); +``` + + + +Install the Portkey SDK with pip +```sh +pip install portkey-ai +``` + +```py Chat Completions +from portkey_ai import Portkey + +client = Portkey( + api_key = "" # Use JWT token instead of API key +) + +response = client.chat.completions.create( + model="gpt-4o", + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "Hello!"} + ] +) + +print(response.choices[0].message) +``` + + + + +```sh Chat Completions +curl https://api.portkey.ai/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: " \ + -d '{ + "model": "gpt-4o", + "messages": [ + { "role": "user", "content": "Hello!" } + ] + }' +``` + + + +Install the OpenAI & Portkey SDKs with pip +```sh +pip install openai portkey-ai +``` + +```py Chat Completions +from openai import OpenAI +from portkey_ai import createHeaders, PORTKEY_GATEWAY_URL + +client = OpenAI( + api_key="xx", + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key="" # Use JWT token instead of API key + ) +) + +completion = client.chat.completions.create( + model="gpt-4o", + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "Hello!"} + ] +) + +print(completion.choices[0].message) +``` + + + +Install the OpenAI & Portkey SDKs with npm +```sh +npm install openai portkey-ai +``` + +```ts Chat Completions +import OpenAI from 'openai'; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + apiKey: 'xx', + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + apiKey: "" // Use JWT token instead of API key + }) +}); + +async function main() { + const completion = await openai.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'gpt-4o', + }); + + console.log(completion.choices[0].message); +} + +main(); +``` + + + + +## Caching & Token Revocation + +- JWTs are cached until they expire to reduce validation overhead. diff --git a/virtual_key_old/product/enterprise-offering/org-management/ldap.mdx b/virtual_key_old/product/enterprise-offering/org-management/ldap.mdx new file mode 100644 index 00000000..3d720a5f --- /dev/null +++ b/virtual_key_old/product/enterprise-offering/org-management/ldap.mdx @@ -0,0 +1,320 @@ +--- +title: LDAP Bridge +description: Enterprise LDAP connectivity through modern identity bridges +--- + +Portkey provides LDAP integration support for enterprises through industry-standard identity provider bridges. This approach enables organizations to maintain their existing LDAP infrastructure while benefiting from modern cloud-native authentication protocols. + +## Overview + +Rather than implementing native LDAP support, Portkey leverages proven bridge solutions from leading identity providers. This strategy offers: + +* **Enhanced Security**: Modern token-based authentication with MFA support +* **Improved Scalability**: Cloud-native architecture without on-premises limitations +* **Reduced Complexity**: Leverage specialized identity provider expertise +* **Future-Ready**: Seamless path to modern protocols + +## How LDAP Bridging Works + +LDAP bridge solutions translate between your existing LDAP directory and Portkey's modern authentication protocols (SAML, OIDC, SCIM). + +### Architecture Overview + +```mermaid +flowchart LR + A[LDAP Directory] --> B[Identity Provider Bridge] + B --> C[SCIM / SAML / OIDC] + C --> D[Portkey] +``` + +The bridge maintains synchronization between your LDAP directory and the identity provider, enabling: + +* **User Authentication**: LDAP credentials validated through SAML/OIDC +* **User Provisioning**: Directory changes synchronized via SCIM +* **Group Management**: LDAP groups mapped to Portkey workspaces +* **Attribute Mapping**: Custom LDAP attributes preserved + +## Supported Identity Providers + + + + Microsoftโ€™s solution for organizations using Active Directory. +

+ - Synchronizes on-premises AD to Azure AD
+ - Supports password hash sync or pass-through authentication
+ - Enables SAML, OIDC, and SCIM for cloud applications
+ - Free tier available, Premium features $6-9/user/month +

+ Best For: Organizations already using Microsoft 365 or Azure services +
+ + + On-premises agent connects to your LDAP directory. +

+ - No credential replication to cloud
+ - Real-time authentication against LDAP
+

+ Best For: Organizations with on-premises AD +
+ + + Cloud-based LDAP endpoint. +

+ - No on-premises infrastructure required
+ - Simplified management
+

+ Best For: Organizations with on-premises AD +
+
+
+ + +**Alternative Solutions** + +- **OneLogin Virtual LDAP**: Cloud-based LDAP service +- **Auth0 AD/LDAP Connector**: Developer-friendly integration +- **Keycloak**: Open-source option with enterprise features +- **JumpCloud**: Directory-as-a-Service with LDAP support + +## Azure AD Setup + + + Requires Azure AD Connect installed on-premises with connectivity to your domain controllers. + + + + + ```powershell + # Download from Microsoft + # Run installer with admin privileges + ``` + + + + + + - Password Hash Sync (recommended) + - Pass-through Authentication + - Federation (advanced) + + + - Select OUs to synchronize + + + - Configure attribute filtering + + + + + + - Navigate to Azure Portal > Enterprise Applications + - Add new application > Non-gallery application + + + + - Entity ID: get from Portkey Control Plane. + - Reply URL: get from Portkey Control Plane. + - Sign-on URL: `https://app.portkey.ai` + + + + + + + + - In application settings, go to Provisioning + - Set mode to Automatic + - Configure with Portkey SCIM endpoint and token + + + - Map attributes according to [SCIM setup guide](/product/enterprise-offering/org-management/scim/azure-ad) + + + + + + + + + + + + +## Okta Setup + +### Using Okta LDAP Agent + + + + - Access Okta Admin Console + - Navigate to Directory > Directory Integrations + - Add LDAP Directory > Download Agent + - Install on server with LDAP connectivity + + + + ```yaml + LDAP Host: ldap.company.com + LDAP Port: 389 (or 636 for LDAPS) + Bind DN: cn=okta-service,ou=services,dc=company,dc=com + Base DN: dc=company,dc=com + ``` + + + + - Create SAML application following [SSO guide](./sso#okta) + - Enable SCIM provisioning per [SCIM guide](./okta) + - Configure attribute mappings + + + +### Using Okta LDAP Interface + + + + - Okta Admin > Directory > LDAP Interface + - Generate LDAP credentials + - Note the LDAP endpoint URL + + + + - Point LDAP applications to Okta endpoint + - Use generated credentials for binding + - Test authentication flow + + + +## Attribute Mapping + +Ensure critical LDAP attributes map correctly: + +| LDAP Attribute | SCIM Attribute | Portkey Field | +|---------------|----------------|---------------| +| uid/sAMAccountName | userName | username | +| mail | emails[primary] | email | +| givenName | name.givenName | firstName | +| sn | name.familyName | lastName | +| memberOf | groups | workspaces | +| title | title | jobTitle | + + + Custom LDAP attributes can be mapped through extended schema support in most identity providers. + + +## Troubleshooting + + + + Symptom: Users cannot log in despite correct credentials + + + Causes: + - Certificate validation errors + - Time synchronization issues + - Incorrect attribute Mapping + + + Solution: + - Verify SSL certificates are trusted + - Ensure NTP synchronization + - Check authentication logs in identity provider + + + + Symptom: Users not appearing in Portkey or incorrect attributes + + Causes: + - SCIM endpoint connectivity issues + - Attribute mapping conflicts + - Insufficient permissions + + + Solution: + - Test SCIM endpoint with bearer token + - Review attribute mapping configuration + - Verify service account permissions + + + + Symptom: Slow authentication or provisioning + + + Causes: + - Unindexed LDAP queries + - Network latency + - Large group memberships + + + Solution: + - Add indexes for commonly queried attributes + - Deploy bridge closer to LDAP servers + - Implement group filtering + + + +### Best Practices + + + +Deploy multiple bridge instances
+Configure load balancing
+Implement health monitoring +
+ +Use LDAPS (LDAP over SSL) always
+Implement service account restrictions
+Enable audit logging +
+ +Cache frequently accessed data
+Implement connection pooling
+Monitor query performance +
+
+ +## Frequently Asked Questions + + + + + Modern cloud architectures benefit from stateless, token-based protocols. LDAP's stateful binary protocol creates security and scalability challenges in cloud environments. Industry leaders like Slack, Salesforce, and others follow the same approach. + + + + While identity provider licenses add cost, most organizations see overall savings through: +
    +
  • Reduced infrastructure maintenance
  • +
  • Improved security posture
  • +
  • Decreased administrative overhead
  • +
  • Better user experience
  • +
+
+ + + Typical enterprise migrations complete in 3-6 months: +
    +
  • Small organizations (<1,000 users): 4-8 weeks
  • +
  • Medium organizations (1,000-10,000): 2-4 months
  • +
  • Large enterprises (10,000+): 4-6 months
  • +
+
+ + + Yes, bridge solutions maintain your existing LDAP infrastructure. Other applications continue working unchanged while Portkey uses modern protocols. + + +
+ +## Support + +For assistance with LDAP integration: + +* Review our [SSO documentation](../sso) for authentication setup +* Configure [SCIM provisioning](../scim) for user management +* Contact [support@portkey.ai](mailto:support@portkey.ai) for integration help + +--- + + + Enterprise customers can request a guided migration workshop. Our solution architects will help design and implement your LDAP bridge strategy. + \ No newline at end of file diff --git a/virtual_key_old/product/enterprise-offering/org-management/organizations.mdx b/virtual_key_old/product/enterprise-offering/org-management/organizations.mdx new file mode 100644 index 00000000..642122ad --- /dev/null +++ b/virtual_key_old/product/enterprise-offering/org-management/organizations.mdx @@ -0,0 +1,17 @@ +--- +title: "Organizations" +description: "Understand the role and features of Organizations, the highest level of abstraction in Portkey's structure." +--- + +Organizations in Portkey represent the highest level of abstraction within an account. They serve as containers for all users, entities, and workspaces (if the feature is enabled) associated with your company or project. + +Key aspects of Organizations: + +* **Comprehensive Scope:** An organization encompasses all the users and entities within it, providing a broad view of your entire operation. +* **Workspace Support:** Organizations can contain workspaces, which act as sub-organizations for more granular team management and project scoping. +* **Hierarchical Roles:** Organizations have owners and org admins who have access to all workspaces and can create and use admin keys. +* **Multi-Org Flexibility:** Portkey offers an org switcher, allowing users to switch between different organizations they have access to. +* **Security Features:** SSO (Single Sign-On) settings are applied at the organization level, enhancing security and user management. +* **Private Cloud Integration:** When deploying a gateway in a private cloud, the connection to the control panel is established at the organization level. + +Organizations provide a robust framework for managing large-scale AI development projects, enabling efficient resource allocation, access control, and team management across your entire operation. diff --git a/virtual_key_old/product/enterprise-offering/org-management/scim/azure-ad.mdx b/virtual_key_old/product/enterprise-offering/org-management/scim/azure-ad.mdx new file mode 100644 index 00000000..4e76fdc0 --- /dev/null +++ b/virtual_key_old/product/enterprise-offering/org-management/scim/azure-ad.mdx @@ -0,0 +1,134 @@ +--- +title: "Azure Entra" +description: "Setup Azure Entra for SCIM provisioning with Portkey." +--- + +#### Azure Active Directory (Azure AD) + [Reference](https://learn.microsoft.com/en-us/azure/active-directory/app-provisioning/use-scim-to-provision-users-and-groups) + + Setting up Azure Entra for SCIM provisioning consists of the following steps: + + - **New Entra Application & SCIM Provisioning** + - **Application Roles** + - **SCIM Attribute Mapping Update** + +--- + +##### New Entra Application + +First, create a new Azure Entra application to set up SCIM provisioning with Portkey. + +1. Navigate to the [Entra Applications Page](https://entra.microsoft.com/?culture=en-in&country=in#view/Microsoft_AAD_IAM/AppGalleryBladeV2) and click **`Create your own application`**. + + ![Application Creation](/images/scim/azure/create.png) + +2. Complete the required fields to create a new application. +3. Once the application is created, navigate to the application's **Provisioning** page under the **Manage** section. +4. Click **`New Configuration`** to go to the provisioning settings page. + + ![Provisioning Settings](/images/scim/azure/settings.png) + +5. Obtain the **Tenant URL** and **Secret Token** from the Portkey Admin Settings page (if SCIM is enabled for your organization). + - [Portkey Settings Page](https://app.portkey.ai/settings/organisation/sso) + + ![Portkey Admin Settings](/images/scim/portkey.png) + +6. Fill in the values from the Portkey dashboard in Entra's provisioning settings and click **`Test Connection`**. If successful, click **`Create`**. +> If the test connection returns any errors, please contact us at [support@portkey.ai](mailto:support@portkey.ai). + +--- + +##### Application Roles + +Portkey supported roles should match Entra's application roles. + +1. Navigate to **App Registrations** under **Enterprise Applications**, click **All Applications**, and select the application created earlier. +2. Go to the **App Roles** page and click **`Create app role`**. + > Portkey supports two application-level roles: + > - **`member`** (Organization Member) + > - **`admin`** (Organization Admin) + > - **`owner`** (Organization Owner) + +![App Roles](/images/scim/azure/roles.png) + + > Users assigned any other role will default to the **member** role. + +3. To support group roles, create a role with the value **`group`** and a name in title-case (e.g., `Group` for the value `group`). + + ![Creating App Roles](/images/scim/azure/create-roles.png) + +4. Assign users to the application with the desired role (e.g., **`owner`**, **`member`**, or **`admin`**) for the organization. + + ![Assigning Roles](/images/scim/azure/select-role.png) + +--- + +#### Attribute Mapping + +###### Adding a New Attribute + +1. Go to the **Provisioning** page and click **Attribute Mapping (Preview)** to access the attributes page. +2. Enable advanced options and click **`Edit attribute list for customappsso`**. + + ![Edit Attribute List](/images/scim/azure/new-mapping.png) + +3. Add a new attribute called **`roles`** with the following properties: + - **Multi-valued:** Enabled + - **Type:** String + + ![Roles Attribute Properties](/images/scim/azure/mapping-properties.png) + +###### Adding a new mapping + +1. Click on the **`Add new mapping`** link to add a new mapping. (refer to the above images). +2. Follow the values from the below image to add a new mapping. + +![New Mapping Attributes](/images/scim/new-mapping.png) + +3. Once done, save the changes. + +###### Removing Unnecessary Attributes + +Delete the following unsupported attributes: +- **preferredLanguage** +- **addresses (all fields)** +- **phoneNumbers** + +--- + +#### Updating Attributes + +**Update `displayName`** + +1. Edit the **`displayName`** field to concatenate `firstName + lastName` instead of using the default `displayName` value from Entra records. + + ![Update displayName Expression](/images/scim/azure/update-displayname.png) + +2. Save the changes and enable provisioning on the **Overview** page of the provisioning settings. + + +--- + +##### Group (Workspace) Provisioning + +Portkey supports RBAC (Role-Based Access Control) for workspaces mapped to groups in Entra. Use the following naming convention for groups: + +- **Format:** `ws-{group}-role-{role}` + - **Role:** One of `admin`, `member`, or `manager` +- A user should belong to only one group per `{group}`. + +**Example:** +For a `Sales` workspace: +- `ws-Sales-role-admin` +- `ws-Sales-role-manager` +- `ws-Sales-role-member` + +Users assigned to these groups will inherit the corresponding role in Portkey. + +![Entra Group Role Mapping](/images/scim/portkey-group-roles.png) + +--- + +### Support + +If you face any issues with the group provisioning, please reach out to us at [here](mailto:support@portkey.ai). diff --git a/virtual_key_old/product/enterprise-offering/org-management/scim/okta.mdx b/virtual_key_old/product/enterprise-offering/org-management/scim/okta.mdx new file mode 100644 index 00000000..2df148d3 --- /dev/null +++ b/virtual_key_old/product/enterprise-offering/org-management/scim/okta.mdx @@ -0,0 +1,145 @@ +--- +title: "Okta" +description: "Set up Okta for SCIM provisioning with Portkey." +--- + +Portkey supports provisioning Users & Groups with Okta SAML Apps. + + + Okta does not support SCIM Provisioning with OIDC apps; only SAML apps are supported. + + +To set up SCIM provisioning between Portkey and Okta, you must first create a SAML App on Okta. + +--- + +### Setting up SCIM Provisioning + +1. Navigate to the app settings. Under general settings, enable the SCIM provisioning checkbox. + + ![Enable SCIM Provisioning](/images/scim/okta/enable.png) + + + The `Provisioning` tab should be visible after enabling SCIM provisioning. Navigate to that page. + + +2. Obtain the Tenant URL and Secret Token from the Portkey Admin Settings page (if SCIM is enabled for your organization). + + - [Portkey Settings Page](https://app.portkey.ai/settings/organisation/sso) + + ![Portkey Admin Settings](/images/scim/portkey.png) + +3. Fill in the values from the Portkey dashboard into Okta's provisioning settings and click **`Test Connection`**. If successful, click **`Save`**. + + Ensure you choose the Authentication Mode as `HTTP Header`. + + +3. Check all the boxes as specified in the image below for full support of SCIM provisioning operations. + + ![Provisioning Settings](/images/scim/okta/operations.png) + +4. Once the details are saved, you will see two more options along with integration, namely `To App` and `To Okta`. + + Select `To App` to configure provisioning from Okta to Portkey. + + Enable the following checkboxes: + * Create Users + * Update User Attributes + * Deactivate Users + + ![Provisioning Settings](/images/scim/okta/user-settings.png) + + After saving the settings, the application header should resemble the following image. + + ![Okta App Provisioning Status](/images/scim/okta/status.png) + +This completes the SCIM provisioning settings between Okta and Portkey. + +Whenever you assign a `User` or `Group` to the application, Okta automatically pushes the updates to Portkey. + +--- + +### Organisation role support + +Portkey supports the following organisation roles: + +- **`owner`** (Organization Owner) +- **`admin`** (Organization Admin) +- **`member`** (Organization Member) + +Users assigned any other role will default to the **member** role. + +#### Editing Attributes + +Okta by default doesn't support role attributes. To support role attributes, you need to edit the attributes in Okta. + +1. Navigate to the app settings. Under general settings, click on the `Provisioning` tab. + +2. Click on the `Go to Profile Editor` button, found under **Attribute Mappings** section. + +3. Click on the `Add Attribute` button. + +4. Fill the form with the following details: + + ![Edit Attribute List](/images/scim/okta/add-attribute.png) + +5. Click on the `Save` button. + +#### Verifying the changes + +To verify the changes, you can assign a user to the application with the desired role (e.g., **`owner`**, **`member`**, or **`admin`**) for the organization. + + ![Assigning Roles](/images/scim/okta/select-roles.png) + + + Make sure to select only **one** role for a user, if multiple selected user will be assigned to highest qualified role. + +--- + +### Group Provisioning with Okta + +Portkey supports RBAC (Role-Based Access Control) for workspaces mapped to groups in Okta. Use the following naming convention for groups: + +- **Format:** `ws-{group}-role-{role}` + - **Role:** One of `admin`, `member`, or `manager` +- A user should belong to only one group per `{group}`. + +**Example:** +For a `Sales` workspace: +- `ws-Sales-role-admin` +- `ws-Sales-role-manager` +- `ws-Sales-role-member` + +Users assigned to these groups will inherit the corresponding role in Portkey. + +![Group Role Mapping](/images/scim/portkey-group-roles.png) + + + Automatic provisioning with Okta works for `Users`, but it does not automatically work for `Groups`. + + +To support automatic provisioning for groups, you must first push the groups to the App (Portkey). Then, Okta will automatically provision updates. + +To push the groups to Portkey, navigate to the `Push Groups` tab. If it is not found, ensure you have followed all the steps correctly and enabled all the fields mentioned in the Provisioning steps. + +1. Click on **Push Groups**. + + ![Push Groups](/images/scim/okta/push-groups.png) + +2. Select **Find group by name**. + +3. Enter the name of the group, select the group from the list, and click **Save** or **Save & Add Another** to assign a new group. + + + You can also use `Find groups by rule` to push multiple groups using a filter. + + + + If there is any discrepancy or issue with group provisioning, you can retry provisioning by clicking the `Push Now` option. This can be found under the `Push Status` column in the groups list. + + +--- + +### Support + +If you encounter any issues with group provisioning, please reach out to us [here](mailto:support@portkey.ai). \ No newline at end of file diff --git a/virtual_key_old/product/enterprise-offering/org-management/scim/scim.mdx b/virtual_key_old/product/enterprise-offering/org-management/scim/scim.mdx new file mode 100644 index 00000000..1184c266 --- /dev/null +++ b/virtual_key_old/product/enterprise-offering/org-management/scim/scim.mdx @@ -0,0 +1,96 @@ +--- +title: "Overview" +description: "SCIM integration with Portkey." +--- + +# SCIM Integration Guide + +Portkey supports **SCIM (System for Cross-domain Identity Management)** to automate user provisioning and deprovisioning. +This guide will walk you through integrating SCIM with your identity provider to manage users and workspaces seamlessly. + +--- + +## Table of Contents +- [What is SCIM?](#what-is-scim) +- [SCIM Base URL](#scim-base-url) +- [Authentication](#authentication) +- [Supported Operations](#supported-operations) +- [Required Configuration](#required-configuration) +- [Identity Provider Setup](#identity-provider-setup) +- [Troubleshooting](#troubleshooting) + +--- + +## What is SCIM? + +SCIM is an open standard that allows organizations to automate the management of user identities and groups across applications. By integrating with SCIM, you can: + +- Automatically provision and update user accounts. +- Deprovision users when they leave your organization. +- Sync user attributes and workspace memberships. + +## SCIM Base URL + +To integrate SCIM with our platform, get the SCIM Base URL from Portkey Control Plane. + +`Admin Settings > Authentication Settings > SCIM Provisioning > SCIM URL` + +## Authentication + +We use **Bearer Token Authentication** for SCIM requests. + +You need to generate an **API token** from Portkey Control Plane (``Admin Settings > Authentication Settings > SCIM Provisioning`) and use it as a bearer token in the SCIM requests. + +You need to include the following header in the SCIM requests: +``` +Authorization: Bearer +``` + +## Supported Operations + +Our SCIM implementation supports the following operations: + +| Operation | Supported | +|----------------|-----------| +| User Provisioning | โœ… | +| User Deprovisioning | โœ… | +| User Updates | โœ… | +| Group (Workspace) Provisioning | โœ… | +| Group (Workspace) Updates | โœ… | +| Group (Workspace) Deprovisioning | โœ… | + +## Required Configuration + +Before integrating SCIM, ensure you have the following details: + +- **SCIM Base URL**: Provided above. +- **Bearer Token**: Generate this token from our platformโ€™s **API Settings** section. + +You will need to provide these details in your identity provider's SCIM configuration section. + +## Identity Provider Setup + +Follow your identity provider's documentation to set up SCIM integration. Below are the key fields youโ€™ll need to configure: + +| Field | Value | +|-------------------- |---------------------------| +| SCIM Base URL | `` | +| Bearer Token | `` | + +Currently, we support SCIM provisioning for the following identity providers: + +- [Azure AD](./azure-ad) +- [Okta](./okta) + +## Troubleshooting + +### Common Issues + +- **Invalid Token**: Ensure the bearer token is correctly generated and included in the request header. +- **403 Forbidden**: Check if the provided SCIM Base URL and token are correct. +- **User Not Provisioned**: Ensure the user attributes meet our platform's requirements. + +--- + +For further assistance, please contact our support team at [support@portkey.ai](mailto:support@portkey.ai). + diff --git a/virtual_key_old/product/enterprise-offering/org-management/sso.mdx b/virtual_key_old/product/enterprise-offering/org-management/sso.mdx new file mode 100644 index 00000000..9d0bc803 --- /dev/null +++ b/virtual_key_old/product/enterprise-offering/org-management/sso.mdx @@ -0,0 +1,131 @@ +--- +title: "SSO" +description: "SSO support for enterprises" +--- + +Portkey Control plane supports following authentication protocols for enterprise customers. +1. **OIDC** (OpenID Connect) +2. **SAML 2.0** (Security Assertion Markup Language) + +Below are the steps to integrate your identity provider with our system. + +## Table of Contents +- [OIDC Integration](#oidc-integration) +- [SAML Integration](#saml-integration) + +## OIDC Integration + +For OIDC integration, we require the following information from your identity provider: + +### Required Information + +- **Issuer URL**: The URL of your identity provider's OIDC authorization endpoint. Wellknown OIDC configuration should be available at this URL. +- **Client ID**: The client ID provided by your identity provider. +- **Client Secret Key**: The client secret provided by your identity provider. + +### Setup Steps + + Following scopes are required for Portkey to work with OIDC: + - openid + - profile + - email + - offline_access + + +#### General +- Create an OIDC application in your identity provider. +- Once the application is created, please note the following details: + - `Issuer URL` + - `Client Id` + - `Client Secret` +- Update the above details in Portkey Control Plane in `Admin Settings > Authentication Settings > OIDC`. + +#### Okta +- Go to `Applications` tab on Okta dashboard and `create a new app integration`. +- Select `OIDC - OpenID Connect` as the signin method. +- Select Application Type as `Web` Application +- On the next step, fill in the required fields. The `signin redirect URI` should be https://app.portkey.ai/v2/auth/callback and the `Grant Type` should have `Authorization code` and `Refresh Token` as checked +- Create Application +- After the application is created, go to the `General` section of the application. +- Click on the `edit` button for the General Settings section. +- Select `Either Okta or app` for the `Login initiated by` field. +- Add https://app.portkey.ai/v2/auth/callback as the `initiate login URI` +- Go to the `Sign On` section and click on `Edit`. Select `Okta Url` as the `issuer` and save the updated details +- Once everything is setup please note the following details + - `Issuer URL` will be the `Issuer` from above step + - `Client Id` would be same as `Audience`/ `Client ID` + - `Client Secret` is needed for Web App based flow. It can be found under `General > Client Credentials > Client Secrets` in your Okta App. +- Update the above details in Portkey Control Plane in `Admin Settings > Authentication Settings > OIDC` + +#### Azure AD +- Sign in to the Azure portal. +- Search for and select Azure Active Directory. +- Under Manage, select App registrations. +- Select New registration. +- Enter a name. +- Select one of the Supported account types that best reflects your organization requirements. +- Under `Redirect URI`, + - Select `Web` as the platform + - Enter https://app.portkey.ai/v2/auth/callback as redirect url +- Click on Register +- Once saved, go to `Certificates & secrets` + - Click on `Client Secrets` + - Click on `New client secret` + - Use appropriate settings according to your organisation + - Click on `Add` +- Once everything is set up. Please go to `Overview` + - Click on `Endpoints` and note the `OpenID Connect metadata document` url + - Please note the `Application (client) ID` from `Essentials` + - Please note the `Client Secret` from `Certificates & secrets` +- Update the above details in Portkey Control Plane in `Admin Settings > Authentication Settings > OIDC` + + +## SAML Integration + +For SAML integration, we require the following information from your identity provider: + +### Required Information + +Either of the following information is required: +- **Provider Metadata URL**: The URL from your identity provider containing the metadata, including SAML configuration details. +- **Provider Metadata XML**: The XML metadata of your identity provider. + +### Setup Steps + +#### General +- Create an SAML application in your identity provider. +- Once the application is created, please note the following details: + - `Provider Metadata URL` + - `Provider Metadata XML` +- Update the above details in Portkey Control Plane in `Admin Settings > Authentication Settings > SAML`. + +#### Okta +- Go to `Applications` tab on okta dashboard and `create a new app integration`. +- Select `SAML 2.0` as the signin method. +- In `Configure SAML`, update + - `Single sign-on URL` with Saml redirect url. You can find the Saml redirect url from the `Admin Settings > Authentication Settings > SAML Redirect/Consumer Service URL` from Portkey Control Plane. + - `Audience URI (SP Entity ID)` with SAML Entity ID from Portkey Control Plane. +- Create Application +- Once everything is set up, please note the following details + - `Sign On tab > SAML 2.0 tab > Metadata details > Metadata URL` +- Update the above details in Portkey Control Plane in `Admin Settings > Authentication Settings > SAML` + +#### Azure AD +- Sign in to the Azure portal. +- Search for and select Azure Active Directory. +- Under Manage, select App registrations. +- Select New registration. +- Enter a name. +- Select one of the Supported account types that best reflects your organization requirements. +- Under `Redirect URI`, + - Select `Web` as the platform + - Enter the `SAML Redirect/Consumer Service URL` from Portkey Control Plane as redirect url +- Select `Register`. +- Select `Endpoints` at the top of the page. +- Find the `Federation metadata document URL` and select the copy icon. +- In the left side panel, select `Expose an API`. +- To the right of `Application ID URI`, select `Add`. + - Enter `SAML Entity ID` from Portkey Control Plane as the `App ID URI`. +- Select `Save`. +- Once everything is set up, please note the following details + - Copy the `Federation metadata document URL` and paste it in Portkey Control Plane in `Admin Settings > Authentication Settings > SAML > Provider Metadata URL` \ No newline at end of file diff --git a/virtual_key_old/product/enterprise-offering/org-management/user-roles-and-permissions.mdx b/virtual_key_old/product/enterprise-offering/org-management/user-roles-and-permissions.mdx new file mode 100644 index 00000000..6a158ad5 --- /dev/null +++ b/virtual_key_old/product/enterprise-offering/org-management/user-roles-and-permissions.mdx @@ -0,0 +1,103 @@ +--- +title: "User Roles & Permissions" +description: "Learn about Portkey's comprehensive role-based access control system across Organizations and Workspaces." +--- + +Portkey implements a comprehensive role-based access control (RBAC) system that operates across two main hierarchical levels: **Organizations** and **Workspaces**. This dual-layer approach ensures precise control over who can access what resources, enhancing security while enabling effective team collaboration. + +## Organization Level + +Organizations represent the highest level of structure within Portkey. At this level, there are three distinct roles with varying levels of administrative control: + + + + The highest authority with complete control of the organization + + + Extensive administrative privileges across the organization + + + Base-level organization access, typically assigned to workspace roles + + + +### Organization Role Permissions + +| Capability | Owner | Admin | Member (without workspace) | +|------------|:-----:|:-----:|:-------------------------:| +| Billing Management | | | | +| Manage Organization Settings | | | | +| Create/Delete Workspaces | | | | +| Manage Admin API Keys | | | | +| Edit User Roles | | | | +| Invite Organization Users | | | | +| Configure Access Permissions | | | | +| Access to All Workspaces | | | | + + + **Important**: Organization Owners and Admins automatically receive Admin-level access to all workspaces within the organization. All users must first be added as Organization Members before they can be invited to any workspace. + + +## Workspace Level + +Workspaces are sub-organizational units that enable better team and project management. Each workspace maintains its own access control structure with three distinct roles: + + + + Complete control over workspace configuration and team management + + + Administrative capabilities for team and resource management + + + Read-only access to workspace resources + + + +### Workspace Role Permissions + +| Capability | Admin | Manager | Member | +|------------|:-----:|:-------:|:------:| +| Invite Organization Members to Workspace | | | | +| Assign Workspace Roles (including Admin) | | | | +| Create Workspace API Keys | | | | +| Create/Update/Delete Resources | | | | +| View Workspace Resources | | | | + + + **Member Access**: Workspace Members have read-only access to workspace resources (logs, prompts, config, virtual keys etc.) but cannot create, update, or delete any resources. + + + + +## Access Permission Configuration + +Organization Owners and Admins can configure access permissions for various resources across workspaces. These settings determine what each role can access: + + + By default Workspace Admins and Managers have the same permissions unless changed by Organization Owner or Admin. + + + + + Control which roles can view, filter, and export logs + + + Manage access to provider API keys for different roles + + + Configure API key creation and management rights + + + + + + **Key Workflow**: Users must first be added as organization members before they can be invited to any workspace. Workspace admins and managers can then invite organization members to their workspace and assign appropriate roles. + + +## Related Topics + + + + + diff --git a/virtual_key_old/product/enterprise-offering/org-management/workspaces.mdx b/virtual_key_old/product/enterprise-offering/org-management/workspaces.mdx new file mode 100644 index 00000000..e845214c --- /dev/null +++ b/virtual_key_old/product/enterprise-offering/org-management/workspaces.mdx @@ -0,0 +1,86 @@ +--- +title: "Workspaces" +description: "Explore Workspaces, the sub-organizational units that enable granular project and team management." +--- + +## Workspaces + +Workspaces in Portkey are sub-organizations that enable better separation of data, teams, scope, and visibility within your larger organization. + +They provide a more granular level of control and organization, allowing you to structure your projects and teams efficiently. + +Key features of Workspaces: + +* **Team Management:** You can add team members to workspaces with specific roles (manager or member), allowing for precise access control. +* **Dedicated API Keys:** Workspaces contain their own API keys, which can be of two types: + * Service Account type: For automated processes and integrations + * User type: For individual user access + * Both these types of keys are scoped to the workspace by default and can only execute actions within that workspace. +* **Completion API Scoping:** Completion APIs are always scoped by workspace and can only be accessed using workspace API keys. +* **Admin Control:** While only org admins can create workspaces, managers can add API keys and team members with roles to existing workspaces. +* **Flexible Updates:** When making updates to entities via admin keys (at the org level), you can specify the `workspace_id` to target specific workspaces. + +Workspaces provide a powerful way to organize your projects, teams, and resources within your larger organization, ensuring proper access control and data separation. + +```mermaid +graph TD + A[Workspace] --> B[Team] + B --> C[Managers] + B --> D[Members] + A --> E[Workspace API Keys] + E --> F[Service Account Type] + E --> G[User Type] + A --> H[Scoped Features] + H --> I[Logs] + H --> J[Prompts] + H --> K[Virtual Keys] + H --> L[Configs] + H --> M[Guardrails] + H --> N[Other Features] +``` + +This structure allows for efficient management of resources and access within each workspace, providing a clear separation between different projects or teams within your organization. + + + +### Deleting a Workspace + + +- Before deleting a workspace, all resources within it must be removed. +- You can't delete the default Shared Team Workspace. + + +To delete a workspace in Portkey, follow these steps: + +1. Navigate to the sidebar in the Portkey app +2. Open the workspace menu dropdown +3. Select the workspace you wish to delete +4. Click on the delete option (trash icon) next to the workspace name + +When attempting to delete a workspace, you'll receive a confirmation dialog. If the workspace still contains resources, you'll see a warning message prompting you to delete these resources first. + + + + + +Resources that must be deleted before removing a workspace like - Prompts, Prompt partials, Virtual keys, Configs, Guardrails and more. + +Once all resources have been removed, enter the workspace name in the confirmation field to proceed with deletion. + + + Workspace deletion is permanent and cannot be undone + + + +Alternatively you can also delete workspaces using the Admin API: + + + + + + +### Related Topics + + + + diff --git a/virtual_key_old/product/enterprise-offering/private-cloud-deployments.mdx b/virtual_key_old/product/enterprise-offering/private-cloud-deployments.mdx new file mode 100644 index 00000000..69217359 --- /dev/null +++ b/virtual_key_old/product/enterprise-offering/private-cloud-deployments.mdx @@ -0,0 +1,24 @@ +--- +title: "Private Cloud Deployments" +--- + + + + + + + + + + + + + + + + + + + + + diff --git a/virtual_key_old/product/enterprise-offering/private-cloud-deployments/architecture.mdx b/virtual_key_old/product/enterprise-offering/private-cloud-deployments/architecture.mdx new file mode 100644 index 00000000..8bc9108e --- /dev/null +++ b/virtual_key_old/product/enterprise-offering/private-cloud-deployments/architecture.mdx @@ -0,0 +1,240 @@ +--- +title: "Enterprise Architecture" +sidebarTitle: "Architecture" +description: "Comprehensive guide to Portkey's hybrid deployment architecture for enterprises" +--- + +Portkey Enterprise offers a **secure hybrid deployment model** that balances security, flexibility, and fast deployment timelines: + +- **Data Plane** runs within your VPC, keeping sensitive LLM data and AI traffic in your environment +- **Control Plane** hosted by Portkey handles administration, configs, and analytics + + + Portkey's Hybrid Deployment Architecture showing Data Plane in customer VPC and Control Plane in Portkey VPC + + + + Want to learn more about our hybrid deployment model? Schedule a personalized demo with our solutions team to see how Portkey Enterprise can fit your security and compliance requirements. + + + +## Core Architecture Components + +### Data Plane (Your VPC) + +The Data Plane is deployed in your cloud environment and processes all your AI traffic: + +| Component | Description | Security Benefit | +| :--------- | :----------- | :--------------- | +| **AI Gateway** | Core engine that routes traffic across LLM providers and implements metering, access control, and guardrails | All LLM requests remain in your network perimeter | +| **Cache Store** | Local cache storage for gateway consumption | Eliminates runtime dependency on Control Plane | +| **Data Store** | Storage for LLM request/response logs | Keep sensitive LLM data completely in your environment | + +The AI Gateway runs as containerized workloads in your infrastructure, deployable via your preferred orchestration method (Kubernetes, ECS, etc.). + +### Control Plane (Portkey VPC) + +The Control Plane is fully managed by Portkey and provides the administrative layer for your deployment: + +- Hosts the web dashboard for managing configurations, tracking analytics, and viewing logs +- Maintains routing configs, provider integrations +- Stores non-sensitive metadata and aggregated metrics +- Automatically updates with new features and provider integrations without requiring changes to your infrastructure + +## Data Flow Between Planes + + + All LLM traffic stays within your network boundary: + + 1. Your application sends requests to the AI Gateway + 2. The Gateway processes the request (applying routing, caching, guardrails) + 3. The Gateway forwards the request to the appropriate LLM provider + 4. Responses from LLMs return through the same path + + **Security Benefit**: Complete isolation of sensitive prompt data and responses + + + + The AI Gateway periodically synchronizes with the Control Plane: + + - **Frequency**: 30-second heartbeat intervals + - **Data Retrieved**: Prompt templates, routing configs, virtual keys, API keys + - **Process**: Data is fetched, decrypted locally, and stored in the Gateway cache + - **Resilience**: Gateway operates independently between syncs using cached configs + + **Security Benefit**: Continuous operation even during Control Plane disconnection + + + + The Gateway sends anonymized metrics to the Analytics Store: + + - **Data Sent**: Non-sensitive operational metrics (model used, token counts, response times) + - **Purpose**: Powers analytics dashboards for monitoring performance and costs + - **Example**: [View sample analytics data](#sample-files) + + **Security Benefit**: Provides insights without exposing sensitive information + + + + **Option A: Logs in Your VPC** (Recommended for high-security environments) + - Logs stored in your environment's Blob Store + - When viewing logs in Dashboard UI, Control Plane requests them from Gateway + + **Option B: Logs in Portkey Cloud** + - Gateway encrypts and sends logs to Portkey Log Store + - No connections required from Portkey to your environment for viewing logs + + **Security Benefit**: Flexibility to match your compliance requirements + + +## Deployment Architecture + +Portkey AI Gateway is deployed as containerized workloads using Helm charts for Kubernetes environments, with flexible deployment options for various cloud providers. + +### Infrastructure Components + +| Component | Description | Configuration Options | +|:----------|:------------|:----------------------| +| **AI Gateway** | Core container running the routing logic | Deployed as stateless containers that can scale horizontally | +| **Cache System** | Stores routing configs, virtual keys, and more | Redis (in-cluster, AWS ElastiCache, or custom endpoint) | +| **Log Storage** | Persistence for request/response data | Multiple options (see below) | + +### Storage Options + + + + S3-compatible storage options including: + - AWS S3 (standard credentials or assumed roles) + - Google Cloud Storage (S3 compatible interoperability mode) + - Azure Blob Storage (key, managed identity, or Entra ID) + - Any S3-compatible Blob Storage + + + MongoDB/DocumentDB for structured log storage with options for: + - Direct connection string with username/password + - Certificate-based authentication (PEM) + - Connection pooling configurations + + + + +### Authentication Methods + + + + - IAM roles for service accounts (IRSA) in Kubernetes + - Instance Metadata Service (IMDS) for EC2/ECS + - Managed identities in Azure environments + + + - Standard access/secret keys + - Certificate-based authentication + - JWT-based authentication + + + +```mermaid +flowchart LR + subgraph "Customer VPC" + app["Customer Application"] --> gw["AI Gateway"] + gw <--> cache["Cache Store"] + gw --> logs["Log Storage"] + gw --> llm["LLM Providers"] + end + + subgraph "Portkey VPC" + cp["Control Plane"] + end + + gw -- "Config Sync\n(Outbound HTTPS)" --> cp + gw -- "Anonymous Metrics\n(Outbound HTTPS)" --> cp + cp -- "Fetch individual logs\n(Inbound HTTPS)" --> gw +``` + +### Infrastructure Requirements + +- **Kubernetes Cluster**: K8s 1.20+ with Helm 3.x +- **Outbound Network**: HTTPS access to Control Plane endpoints +- **Container Registry Access**: For pulling gateway container images +- **Recommended Resource Requirements**: + - CPU: 1-2 cores per gateway instance + - Memory: 2-4GB per gateway instance + - Storage: Dependent on logging configuration + +## Data Security & Encryption + + + + **Your Sensitive Data Stays in Your VPC** + - All prompt content and LLM responses remain within your network + - Only anonymized metrics data cross network boundaries + - Log storage location is configurable based on your requirements + + + **Multi-layered Encryption Approach** + - All data in the Control Plane is encrypted at rest + - Communication between planes uses TLS 1.3 encryption in transit + - Sensitive data uses envelope encryption + - Optional BYOK (Bring Your Own Key) support with AWS KMS integration + + + **Defense-in-Depth Security Model** + - Network-level controls limit Control Plane access to authorized IPs + - Role-based access control for administrative functions + - Audit logging of all administrative actions + - Access tokens are short-lived with automatic rotation + + + +## Advantages of Hybrid Architecture + +| Benefit | Technical Implementation | Business Value | +|:--------|:------------------------|:---------------| +| **Security & Compliance** | - Sensitive data never leaves VPC
- Configurable encryption methods
- Flexible authentication options | - Meets data residency requirements
- Supports regulated industries
- Simplifies security reviews | +| **Operational Efficiency** | - No database management overhead
- Automatic model config updates
- Horizontally scalable architecture | - Low operational burden
- Always up-to-date with LLM ecosystem
- Scales with your traffic patterns | +| **Deployment Flexibility** | - Kubernetes-native deployment
- Support for major cloud providers
- Multiple storage backend options | - Fits into existing infrastructure
- Avoids vendor lock-in
- Customizable to specific needs | +| **Developer Experience** | - OpenAI-compatible API
- Simple integration patterns
- Comprehensive observability | - Minimal code changes needed
- Smooth developer onboarding
- Full visibility into system behavior | + +## Technical Rationale + + + 1. **Real-time Model Updates**: LLM providers frequently change model parameters, pricing, and availability. Centralizing this data ensures all gateways operate with current information. + + 2. **Feature Velocity**: AI landscape evolves rapidly. Control Plane architecture allows Portkey to deliver new features multiple times per week without requiring customer-side deployments. + + 3. **Operational Efficiency**: Eliminates need for customers to maintain complex database infrastructure solely for non-sensitive object management. + + + + 1. **Performance**: Eliminates network latency during LLM requests by having all routing and configs data available locally. + + 2. **Resilience**: Gateway continues operating even if temporarily disconnected from Control Plane. + + 3. **Security**: Reduces attack surface by minimizing runtime external dependencies. + + +## Sample Files + +These samples demonstrate the typical data patterns flowing between systems: + + + + + + +## Resources & Next Steps + + + + + + + + +## Have Questions? + +Our solution architects are available to discuss your specific deployment requirements and security needs. + + + Book a personalized consultation with our enterprise team to explore how Portkey's architecture can be tailored to your organization's specific requirements. + diff --git a/virtual_key_old/product/enterprise-offering/private-cloud-deployments/aws.mdx b/virtual_key_old/product/enterprise-offering/private-cloud-deployments/aws.mdx new file mode 100644 index 00000000..063d92bb --- /dev/null +++ b/virtual_key_old/product/enterprise-offering/private-cloud-deployments/aws.mdx @@ -0,0 +1,265 @@ +--- +title: "AWS" +description: This enterprise-focused document provides comprehensive instructions for deploying the Portkey software on AWS, tailored to meet the needs of large-scale, mission-critical applications. +--- + + It includes specific recommendations for component sizing, high availability, disaster recovery, and integration with monitoring systems. + +## Components and Sizing Recommendations + +| Component | Options | Sizing Recommendations | +| --------------------------------------- | ------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------- | +| AI Gateway | Deploy as a Docker container in your Kubernetes cluster using Helm Charts | AWS EC2 t4g.medium instance, with at least 4GiB of memory and two vCPUs For high reliability, deploy across multiple Availability Zones. | +| Logs Store (optional) | Hosted MongoDB, Document DB or AWS S3 | Each log document is \~10kb in size (uncompressed) | +| Cache (Prompts, Configs & Virtual Keys) | Elasticache or self-hosted Redis | Deploy in the same VPC as the Portkey Gateway. | + +## Deployment Steps + +#### Prerequisites + +Ensure the following tools are installed: + +* Docker +* Kubectl +* Helm (v3 or above) + +### Step 1: Clone the Portkey repo containing helm chart + + +```sh +git clone https://github.com/Portkey-AI/helm-chart +``` + +### Step 2: Update values.yaml for Helm + +Modify the values.yaml file in the Helm chart directory to include the Docker registry credentials and necessary environment variables. You can find the sample file at `./helm-chart/helm/enterprise/values.yaml` + +**Image Credentials Configuration** + +```yaml +imageCredentials: + name: portkey-enterprise-registry-credentials + create: true + registry: kubernetes.io/dockerconfigjson + username: + password: +``` + +The Portkey team will share the credentials for your image + +**Environment Variables Configuration** + +```yaml +environment: + ... + data: + SERVICE_NAME: + LOG_STORE: + MONGO_DB_CONNECTION_URL: + MONGO_DATABASE: + MONGO_COLLECTION_NAME: + LOG_STORE_REGION: + LOG_STORE_ACCESS_KEY: + LOG_STORE_SECRET_KEY: + LOG_STORE_GENERATIONS_BUCKET: + ANALYTICS_STORE: + ANALYTICS_STORE_ENDPOINT: + ANALYTICS_STORE_USER: + ANALYTICS_STORE_PASSWORD: + ANALYTICS_LOG_TABLE: + ANALYTICS_FEEDBACK_TABLE: + CACHE_STORE: + REDIS_URL: + REDIS_TLS_ENABLED: + PORTKEY_CLIENT_AUTH: + ORGANISATIONS_TO_SYNC: +``` + +**Notes on the Log Store** `LOG_STORE` can be + +* an S3 compatible store (AWS S3 `s3`, GCS `gcs`, Wasabi `wasabi`) +* or a MongoDB store (Hosted MongoDB `mongo`, AWS DocumentDB `mongo`) + +If the `LOG_STORE` is `mongo`, the following environment variables are needed + +```yaml +MONGO_DB_CONNECTION_URL: +MONGO_DATABASE: +MONGO_COLLECTION_NAME: +``` + +If the `LOG_STORE` is `s3` or `wasabi` or `gcs`, the following values are mandatory + +```yaml +LOG_STORE_REGION: +LOG_STORE_ACCESS_KEY: +LOG_STORE_SECRET_KEY: +LOG_STORE_GENERATIONS_BUCKET: +``` + +All the above mentioned are S3 Compatible document storages and interoperable with S3 API. You need to generate the `Access Key` and `Secret Key` from the respective providers. + +**Notes on Cache** If `CACHE_STORE` is set as `redis`, a redis instance will also get deployed in the cluster. If you are using custom redis, then leave it blank. + +The following values are mandatory + +```yaml +REDIS_URL: +REDIS_TLS_ENABLED: +``` + +`REDIS_URL` defaults to `redis://redis:6379` and `REDIS_TLS_ENABLED` defaults to `false`. + +**Notes on Analytics Store** This is hosted in Portkeyโ€™s control plane and these credentials will be shared by the Portkey team. + +The following are mandatory and are shared by the Portkey Team. + +```yaml +PORTKEY_CLIENT_AUTH: +ORGANISATIONS_TO_SYNC: +``` + +### Step 3: Deploy using Helm Charts + +Navigate to the directory containing your Helm chart and run the following command to deploy the application: + +```sh +helm install portkey-gateway ./helm/enterprise --namespace portkeyai --create-namespace +``` + +This command installs the Helm chart into the `portkeyai` namespace. + +### Step 4: Verify the Deployment + +Check the status of your deployment to ensure everything is running correctly: + +```sh +kubectl get pods -n portkeyai +``` + +### Step 5: Port Forwarding (Optional) + +To access the service over internet, use port forwarding: + +```sh +kubectl port-forward -n portkeyai 443:8787 +``` + +Replace `` with the name of your pod. + +### Uninstalling the Deployment + +If you need to remove the deployment, run: + +```sh +helm uninstall portkey-app --namespace portkeyai +``` + +This command will uninstall the Helm release and clean up the resources. + +## Network Configuration + +### Step 1: Allow access to the service + +To make the service accessible from outside the cluster, define a Service of type LoadBalancer in your values.yaml or Helm templates. Specify the desired port for external access. + +```yaml +service: + type: LoadBalancer + port: + targetPort: 8787 +``` + +Replace `` with the port number for external access with the port the application listens on internally. + +### Step 2: Ensure Outbound Network Access + +By default, Kubernetes allows full outbound access, but if your cluster has NetworkPolicies that restrict egress, configure them to allow outbound traffic. + +Example NetworkPolicy for Outbound Access: + +```yaml +apiVersion: networking.k8s.io/v1 +kind: NetworkPolicy +metadata: + name: allow-all-egress + namespace: portkeyai +spec: + podSelector: {} + policyTypes: + - Egress + egress: + - to: + - ipBlock: + cidr: 0.0.0.0/0 +``` + +This allows the gateway to access LLMs hosted within your VPC and outside as well. This also enables connection for the sync service to the Portkey Control Plane. + +### Step 3: Configure Inbound Access for Portkey Control Plane + +Ensure the Portkey control plane can access the service either over the internet or through VPC peering. + +**Over the Internet:** + +* Ensure the LoadBalancer security group allows inbound traffic on the specified port. +* Document the public IP/hostname and port for the control plane connection. + +**Through VPC Peering:** + +Set up VPC peering between your AWS account and the control plane's AWS account. Requires manual setup by Portkey Team. + +## Required Permissions + +To ensure the smooth operation of Portkey AI in your private cloud deployment on AWS, specific permissions are required based on the type of log store you are using. Below are the details for S3 or MongoDB compliant databases. + +**S3 Bucket** + +If using S3 as the log store, the following IAM policy permissions are required: + +```JSON +{ + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Action": [ + "s3:ListBucket", + "s3:GetObject", + "s3:PutObject", + "s3:DeleteObject" + ], + "Resource": [ + "arn:aws:s3:::YOUR_BUCKET_NAME", + "arn:aws:s3:::YOUR_BUCKET_NAME/*" + ] + } + ] +} +``` + +Please replace `YOUR_BUCKET_NAME` with your actual bucket name. + +**MongoDB Compliant Database** + +If using a MongoDB compliant database, ensure the AI Gateway has access to the database. The database user should have following role: + +```JSON +{ + "roles": [ + { + "role": "readWrite", + "db": "YOUR_DATABASE_NAME" + } + ] +} +``` + +The `readWrite` role provides the necessary read and write access to the specified database. Please replace `YOUR_DATABASE_NAME` with your actual database name. + +**Cache Store - Redis** + +The Portkey Gateway image ships with a redis installed. You can choose to use the inbuilt redis or connect to an outside Redis instance. + +1. **Redis as Part of the Image:** No additional permissions or networking configurations are required. +2. **Separate Redis Instance:** The gateway requires permissions to perform read and write operations on the Redis instance. The redis connections can be configured with or without TLS. diff --git a/virtual_key_old/product/enterprise-offering/private-cloud-deployments/azure.mdx b/virtual_key_old/product/enterprise-offering/private-cloud-deployments/azure.mdx new file mode 100644 index 00000000..eb011cd0 --- /dev/null +++ b/virtual_key_old/product/enterprise-offering/private-cloud-deployments/azure.mdx @@ -0,0 +1,251 @@ +--- +title: "Azure" +description: This enterprise-focused document provides comprehensive instructions for deploying the Portkey software on Microsoft Azure, tailored to meet the needs of large-scale, mission-critical applications. It includes specific recommendations for component sizing, high availability, disaster recovery, and integration with monitoring systems. +--- + + + These documents are in beta and are WIP. Please reach out in case you face issues with these instructions. + + + + Portkey is also available on the Azure Marketplace. You can deploy Portkey directly through your Azure console, which streamlines procurement and deployment processes. + + [Deploy via Azure Marketplace โ†’](https://azuremarketplace.microsoft.com/en-in/marketplace/apps/portkey.enterprise-saas?tab=Overview) + + + +## Components and Sizing Recommendations + +### Component: AI Gateway + +* **Deployment:** Deploy as a Docker container in your Kubernetes cluster using Helm Charts. +* **Instance Type:** Azure Standard B2ms instance, with at least 4GiB of memory and two vCPUs. +* **High Availability:** Deploy across multiple zones for high reliability. + +### Component: Logs Store (optional) + +* **Options:** Azure Cosmos DB, Azure Blob Storage. +* **Sizing:** Each log document is \~10kb in size (uncompressed). + +### Component: Cache (Prompts, Configs & Virtual Keys) + +* **Options:** Azure Cache for Redis or self-hosted Redis. +* **Deployment:** Deploy in the same VNet as the Portkey Gateway. + +## Deployment Steps + +### Prerequisites + +Ensure the following tools are installed: + +* Docker +* Kubectl +* Helm (v3 or above) + +### Step 1: Clone the Portkey Repo Containing Helm Chart + +```sh +git clone https://github.com/Portkey-AI/helm-chart +``` + +### Step 2: Update values.yaml for Helm + +Modify the `values.yaml` file in the Helm chart directory to include the Docker registry credentials and necessary environment variables. You can find the sample file at `./helm-chart/helm/enterprise/values.yaml`. + +**Image Credentials Configuration** + +```yaml +imageCredentials: + name: portkey-enterprise-registry-credentials + create: true + registry: kubernetes.io/dockerconfigjson + username: + password: +``` + +_The Portkey team will share the credentials for your image._ + +**Environment Variables Configuration** + +Can be fetched from a secrets store + +```yaml +environment: + data: + SERVICE_NAME: "gateway_enterprise" + PORTKEY_CLIENT_AUTH: "" + PORTKEY_ORGANISATION_ID: "" + ORGANISATIONS_TO_SYNC: "" + LOG_STORE: "" + + # If you're using Cosmos DB as Logs Store + COSMOS_DB_CONNECTION_URL: "" + COSMOS_DATABASE: "" + COSMOS_COLLECTION_NAME: "" + + # If you're using Azure Blob Storage as Logs Store + AZURE_STORAGE_ACCOUNT_NAME: "" + AZURE_STORAGE_ACCOUNT_KEY: "" + AZURE_CONTAINER_NAME: "" + + # Analytics Store credentials shared by Portkey + ANALYTICS_STORE: "" + ANALYTICS_STORE_USER: "" + ANALYTICS_STORE_PASSWORD: "" + ANALYTICS_STORE_HOST: "" + ANALYTICS_STORE_TABLE: "" + ANALYTICS_STORE_FEEDBACK_TABLE: "" + + # Your cache details + CACHE_STORE: "" + REDIS_URL: "" + REDIS_TLS_ENABLED: "" + + # For semantic cache, when using Pinecone + PINECONE_NAMESPACE: "" + PINECONE_INDEX_HOST: "" + PINECONE_API_KEY: "" + SEMCACHE_OPENAI_EMBEDDINGS_API_KEY: "" + SEMCACHE_OPENAI_EMBEDDINGS_MODEL: "text-embedding-3-small" +``` + +**Notes on the Log Store** + +`LOG_STORE` can be: + +* `s3`, for Azure Blob Storage +* `mongo`, for Azure Cosmos DB + +If the `LOG_STORE` is `mongo`, the following environment variables are needed: + +```yaml +MONGO_DB_CONNECTION_URL: +MONGO_DATABASE: +MONGO_COLLECTION_NAME: +``` + +If the `LOG_STORE` is `s3`, the following values are mandatory: + +```yaml +LOG_STORE_REGION: +LOG_STORE_ACCESS_KEY: +LOG_STORE_SECRET_KEY: +LOG_STORE_GENERATIONS_BUCKET: +``` + +_You need to generate the Access Key and Secret Key from the respective providers._ + +**Notes on Cache** + +If `CACHE_STORE` is set as `redis`, a Redis instance will also get deployed in the cluster. If you are using custom Redis, then leave it blank. The following values are mandatory: + +```yaml +REDIS_URL: +REDIS_TLS_ENABLED: +``` + +`REDIS_URL` defaults to `redis://redis:6379` and `REDIS_TLS_ENABLED` defaults to `false`. + +**Notes on Analytics Store** + +This is hosted in Portkeyโ€™s control plane and these credentials will be shared by the Portkey team. + +The following are mandatory and are shared by the Portkey Team. + + +```yaml +PORTKEY_CLIENT_AUTH: +ORGANISATIONS_TO_SYNC: +``` + +### Step 3: Deploy Using Helm Charts + +Navigate to the directory containing your Helm chart and run the following command to deploy the application: + +```sh +helm install portkey-gateway ./helm/enterprise --namespace portkeyai --create-namespace +``` + +_This command installs the Helm chart into the_ `_portkeyai_` _namespace._ + +### Step 4: Verify the Deployment + +Check the status of your deployment to ensure everything is running correctly: + +```sh +kubectl get pods -n portkeyai +``` + +### Step 5: Port Forwarding (Optional) + +To access the service over the internet, use port forwarding: + +```sh +kubectl port-forward -n portkeyai 443:8787 +``` + +_Replace_ `__` _with the name of your pod._ + +### Uninstalling the Deployment + +If you need to remove the deployment, run: + +```sh +helm uninstall portkey-app --namespace portkeyai +``` + +_This command will uninstall the Helm release and clean up the resources._ + +## Network Configuration + +### Step 1: Allow Access to the Service + +To make the service accessible from outside the cluster, define a Service of type `LoadBalancer` in your `values.yaml` or Helm templates. Specify the desired port for external access. + +```yaml +service: + type: LoadBalancer + port: + targetPort: 8787 +``` + +_Replace_ `__` _with the port number for external access with the port the application listens on internally._ + +### Step 2: Ensure Outbound Network Access + +By default, Kubernetes allows full outbound access, but if your cluster has NetworkPolicies that restrict egress, configure them to allow outbound traffic. + +**Example NetworkPolicy for Outbound Access:** + +```yaml +apiVersion: networking.k8s.io/v1 +kind: NetworkPolicy +metadata: + name: allow-all-egress + namespace: portkeyai +spec: + podSelector: {} + policyTypes: + - Egress + egress: + - to: + - ipBlock: + cidr: 0.0.0.0/0 +``` + +_This allows the gateway to access LLMs hosted within your VNet and outside as well. This also enables connection for the sync service to the Portkey Control Plane._ + +### Step 3: Configure Inbound Access for Portkey Control Plane + +Ensure the Portkey control plane can access the service either over the internet or through VNet peering. + +**Over the Internet:** + +* Ensure the LoadBalancer security group allows inbound traffic on the specified port. +* Document the public IP/hostname and port for the control plane connection. + +**Through VNet Peering:** + +* Set up VNet peering between your Azure account and the control plane's Azure account. Requires manual setup by Portkey Team. + +This guide provides the necessary steps and configurations to deploy Portkey on Azure effectively, ensuring high availability, scalability, and integration with your existing infrastructure. diff --git a/virtual_key_old/product/enterprise-offering/private-cloud-deployments/cloudflare-workers.mdx b/virtual_key_old/product/enterprise-offering/private-cloud-deployments/cloudflare-workers.mdx new file mode 100644 index 00000000..7e4371c1 --- /dev/null +++ b/virtual_key_old/product/enterprise-offering/private-cloud-deployments/cloudflare-workers.mdx @@ -0,0 +1,8 @@ +--- +title: "Cloudflare Workers" +--- + + + + These documents have not been moved to the public repo yet. Please reach out to support@portkey.ai for more information. + diff --git a/virtual_key_old/product/enterprise-offering/private-cloud-deployments/f5-app-stack.mdx b/virtual_key_old/product/enterprise-offering/private-cloud-deployments/f5-app-stack.mdx new file mode 100644 index 00000000..704f796f --- /dev/null +++ b/virtual_key_old/product/enterprise-offering/private-cloud-deployments/f5-app-stack.mdx @@ -0,0 +1,122 @@ +--- +title: "F5 App Stack" +--- + + +Follow the instructions [here](https://docs.cloud.f5.com/docs/how-to/site-management/create-voltstack-site). + + + +```sh +export DISTRIBUTED_CLOUD_TENANT=mytenantname + +# find tenant id in the F5 Distributed Cloud GUI at +# Account -> Account Settings -> Tenant Overview -> Tenant ID + +export DISTRIBUTED_CLOUD_TENANT_ID=mytenantnamewithextensionfoundintheconsole + +# create an API token in the F5 Distributed Cloud GUI at +# Account -> Account Settings -> Credentials -> Add Credentials +# set Credential Type to API Token, not API Certificate + +export DISTRIBUTED_CLOUD_API_TOKEN=myapitoken +export DISTRIBUTED_CLOUD_SITE_NAME=appstacksitename +export DISTRIBUTED_CLOUD_NAMESPACE=mydistributedcloudnamespace +export DISTRIBUTED_CLOUD_APP_STACK_NAMESPACE=portkeyai +export DISTRIBUTED_CLOUD_APP_STACK_SITE=myappstacksite +export DISTRIBUTED_CLOUD_SERVICE_NAME=portkeyai + +# adjust the expiry date to a time no more than 90 days in the future + +export KUBECONFIG_CERT_EXPIRE_DATE="2021-09-14T09:02:25.547659194Z" +export PORTKEY_GATEWAY_FQDN=the.host.nameof.theservice +export PORTKEY_PROVIDER=openai +export PORTKEY_PROVIDER_AUTH_TOKEN=authorizationtoken + +curl --location --request POST 'https://$DISTRIBUTED_CLOUD_TENANT.console.ves.volterra.io/api/web/namespaces/system/sites/$DISTRIBUTED_CLOUD_SITE_NAME/global-kubeconfigs' \ +--header 'Authorization: APIToken $DISTRIBUTED_CLOUD_API_TOKEN' \ +--header 'Access-Control-Allow-Origin: *' \ +--header 'x-volterra-apigw-tenant: $DISTRIBUTED_CLOUD_TENANT'\ +--data-raw '{"expirationTimestamp":"$KUBECONFIG_CERT_EXPIRE_DATE"}' +``` + +Save the response in a YAML file for later use.[more detailed instructions for retrieving the App Stack kubeconfig file](https://f5cloud.zendesk.com/hc/en-us/articles/4407917988503-How-to-download-kubeconfig-via-API-or-vesctl) + + + +```sh +wget https://raw.githubusercontent.com/Portkey-AI/gateway/main/deployment.yaml +``` + + + +```sh +export KUBECONFIG=path/to/downloaded/global/kubeconfig/in/step/two + +# apply the file downloaded in step 3 +kubectl apply -f deployment.yaml +``` + + + +```sh +# create origin pool +curl --request POST \ + --url https://$DISTRIBUTED_CLOUD_TENANT.console.ves.volterra.io/api/config/namespaces/$DISTRIBUTED_CLOUD_NAMESPACE/origin_pools \ + --header 'authorization: APIToken $DISTRIBUTED_CLOUD_API_TOKEN' \ + --header 'content-type: application/json' \ + --data '{"metadata": {"name": "$DISTRIBUTED_CLOUD_SERVICE_NAME","namespace": "$DISTRIBUTED_CLOUD_NAMESPACE","labels": {},"annotations": {},"description": "","disable": false},"spec": {"origin_servers": [{"k8s_service": {"service_name": "$DISTRIBUTED_CLOUD_SERVICE_NAME.$DISTRIBUTED_CLOUD_APP_STACK_NAMESPACE","site_locator": {"site": {"tenant": "$DISTRIBUTED_CLOUD_TENANT_ID","namespace": "system","name": "$DISTRIBUTED_CLOUD_APP_STACK_SITE"}},"inside_network": {}},"labels": {}}],"no_tls": {},"port": 8787,"same_as_endpoint_port": {},"healthcheck": [],"loadbalancer_algorithm": "LB_OVERRIDE","endpoint_selection": "LOCAL_PREFERRED","advanced_options": null}}' +``` + +or [use the UI](https://docs.cloud.f5.com/docs/how-to/app-networking/origin-pools) + + + +```sh +curl --request POST \ + --url https://$DISTRIBUTED_CLOUD_TENANT.console.ves.volterra.io/api/config/namespaces/$DISTRIBUTED_CLOUD_NAMESPACE/http_loadbalancers \ + --header 'authorization: APIToken $DISTRIBUTED_CLOUD_API_TOKEN' \ + --header 'content-type: application/json' \ + --data '{"metadata": {"name": "$DISTRIBUTED_CLOUD_SERVICE_NAME","namespace": "$DISTRIBUTED_CLOUD_NAMESPACE","labels": {},"annotations": {},"description": "","disable": false},"spec": {"domains": ["$PORTKEY_GATEWAY_FQDN"],"https_auto_cert": {"http_redirect": true,"add_hsts": false,"tls_config": {"default_security": {}},"no_mtls": {},"default_header": {},"enable_path_normalize": {},"port": 443,"non_default_loadbalancer": {},"header_transformation_type": {"default_header_transformation": {}},"connection_idle_timeout": 120000,"http_protocol_options": {"http_protocol_enable_v1_v2": {}}},"advertise_on_public_default_vip": {},"default_route_pools": [{"pool": {"tenant": "$DISTRIBUTED_CLOUD_TENANT_ID","namespace": "$DISTRIBUTED_CLOUD_NAMESPACE","name": "$DISTRIBUTED_CLOUD_SERVICE_NAME"},"weight": 1,"priority": 1,"endpoint_subsets": {}}],"origin_server_subset_rule_list": null,"routes": [],"cors_policy": null,"disable_waf": {},"add_location": true,"no_challenge": {},"more_option": {"request_headers_to_add": [{"name": "x-portkey-provider","value": "$PORTKEY_PROVIDER","append": false},{"name": "Authorization","value": "Bearer $PORTKEY_PROVIDER_AUTH_TOKEN","append": false}],"request_headers_to_remove": [],"response_headers_to_add": [],"response_headers_to_remove": [],"max_request_header_size": 60,"buffer_policy": null,"compression_params": null,"custom_errors": {},"javascript_info": null,"jwt": [],"idle_timeout": 30000,"disable_default_error_pages": false,"cookies_to_modify": []},"user_id_client_ip": {},"disable_rate_limit": {},"malicious_user_mitigation": null,"waf_exclusion_rules": [],"data_guard_rules": [],"blocked_clients": [],"trusted_clients": [],"api_protection_rules": null,"ddos_mitigation_rules": [],"service_policies_from_namespace": {},"round_robin": {},"disable_trust_client_ip_headers": {},"disable_ddos_detection": {},"disable_malicious_user_detection": {},"disable_api_discovery": {},"disable_bot_defense": {},"disable_api_definition": {},"disable_ip_reputation": {},"disable_client_side_defense": {},"csrf_policy": null,"graphql_rules": [],"protected_cookies": [],"host_name": "","dns_info": [],"internet_vip_info": [],"system_default_timeouts": {},"jwt_validation": null,"disable_threat_intelligence": {},"l7_ddos_action_default": {},}}' +``` + +or [use the UI](https://docs.cloud.f5.com/docs/how-to/app-networking/http-load-balancer) + + + +```sh +curl --request POST \ + --url https://$PORTKEY_GATEWAY_FQDN/v1/chat/completions \ + --header 'content-type: application/json' \ + --data '{"messages": [{"role": "user","content": "Say this might be a test."}],"max_tokens": 20,"model": "gpt-4"}' +``` + +In addition to the response headers, you should get a response body like + +```sh +{ + "id": "chatcmpl-abcde......09876", + "object": "chat.completion", + "created": "0123456789", + "model": "gpt-4-0321", + "choices": [ + { + "index": 0, + "message": { + "role": "assistant", + "content": "This might be a test." + }, + "logprobs": null, + "finish_reason": "stop" + } + ], + "usage": { + "prompt_tokens": 14, + "completion_tokens": 6, + "total_tokens": 20 + }, + "system_fingerprint": null +} +``` + + diff --git a/virtual_key_old/product/enterprise-offering/private-cloud-deployments/gcp.mdx b/virtual_key_old/product/enterprise-offering/private-cloud-deployments/gcp.mdx new file mode 100644 index 00000000..f083bbb5 --- /dev/null +++ b/virtual_key_old/product/enterprise-offering/private-cloud-deployments/gcp.mdx @@ -0,0 +1,227 @@ +--- +title: "GCP" +description: This enterprise-focused document provides comprehensive instructions for deploying the Portkey software on Google Cloud Platform (GCP), tailored to meet the needs of large-scale, mission-critical applications. +--- + +It includes specific recommendations for component sizing, high availability, disaster recovery, and integration with monitoring systems. + +## Components and Sizing Recommendations + +### Component: AI Gateway + +* **Deployment:** Deploy as a Docker container in your Kubernetes cluster using Helm Charts. +* **Instance Type:** GCP n1-standard-2 instance, with at least 4GiB of memory and two vCPUs. +* **High Availability:** Deploy across multiple zones for high reliability. + +### Component: Logs Store (optional) + +* **Options:** Hosted MongoDB, Google Cloud Storage (GCS), or Google Firestore. +* **Sizing:** Each log document is \~10kb in size (uncompressed). + +### Component: Cache (Prompts, Configs & Virtual Keys) + +* **Options:** Google Memorystore for Redis or self-hosted Redis. +* **Deployment:** Deploy in the same VPC as the Portkey Gateway. + +## Deployment Steps + +### Prerequisites + +Ensure the following tools are installed: + +* Docker +* Kubectl +* Helm (v3 or above) + +### Step 1: Clone the Portkey Repo Containing Helm Chart + +```sh +git clone https://github.com/Portkey-AI/helm-chart +``` + +### Step 2: Update values.yaml for Helm + +Modify the `values.yaml` file in the Helm chart directory to include the Docker registry credentials and necessary environment variables. You can find the sample file at `./helm-chart/helm/enterprise/values.yaml`. + +**Image Credentials Configuration** + +```yaml +imageCredentials: + name: portkey-enterprise-registry-credentials + create: true + registry: kubernetes.io/dockerconfigjson + username: + password: +``` + +_The Portkey team will share the credentials for your image._ + +**Environment Variables Configuration** + +These can be stored & fetched from a vault as well + +```yaml +environment: + ... + data: + SERVICE_NAME: + LOG_STORE: + MONGO_DB_CONNECTION_URL: + MONGO_DATABASE: + MONGO_COLLECTION_NAME: + LOG_STORE_REGION: + LOG_STORE_ACCESS_KEY: + LOG_STORE_SECRET_KEY: + LOG_STORE_GENERATIONS_BUCKET: + ANALYTICS_STORE: + ANALYTICS_STORE_ENDPOINT: + ANALYTICS_STORE_USER: + ANALYTICS_STORE_PASSWORD: + ANALYTICS_LOG_TABLE: + ANALYTICS_FEEDBACK_TABLE: + CACHE_STORE: + REDIS_URL: + REDIS_TLS_ENABLED: + PORTKEY_CLIENT_AUTH: + ORGANISATIONS_TO_SYNC: +``` + +**Notes on the Log Store** + +`LOG_STORE` can be: + +* Google Cloud Storage (`gcs`) +* Hosted MongoDB (`mongo`) + +If the `LOG_STORE` is `mongo`, the following environment variables are needed: + +```yaml +MONGO_DB_CONNECTION_URL: +MONGO_DATABASE: +MONGO_COLLECTION_NAME: +``` + +If the `LOG_STORE` is `gcs`, the following values are mandatory: + +```yaml +LOG_STORE_REGION: +LOG_STORE_ACCESS_KEY: +LOG_STORE_SECRET_KEY: +LOG_STORE_GENERATIONS_BUCKET: +``` + +_You need to generate the Access Key and Secret Key from the respective providers._ + +**Notes on Cache** + +If `CACHE_STORE` is set as `redis`, a Redis instance will also get deployed in the cluster. If you are using custom Redis, then leave it blank. The following values are mandatory: + +```yaml +REDIS_URL: +REDIS_TLS_ENABLED: +``` + +`REDIS_URL` defaults to `redis://redis:6379` and `REDIS_TLS_ENABLED` defaults to `false`. + +**Notes on Analytics Store** + +This is hosted in Portkeyโ€™s control plane and these credentials will be shared by the Portkey team. + +The following are mandatory and are shared by the Portkey Team. + +```yaml +PORTKEY_CLIENT_AUTH: +ORGANISATIONS_TO_SYNC: +``` + +### Step 3: Deploy Using Helm Charts + +Navigate to the directory containing your Helm chart and run the following command to deploy the application: + +```sh +helm install portkey-gateway ./helm/enterprise --namespace portkeyai --create-namespace +``` + +_This command installs the Helm chart into the_ `_portkeyai_` _namespace._ + +### Step 4: Verify the Deployment + +Check the status of your deployment to ensure everything is running correctly: + +```sh +kubectl get pods -n portkeyai +``` + +### Step 5: Port Forwarding (Optional) + +To access the service over the internet, use port forwarding: + +```sh +kubectl port-forward -n portkeyai 443:8787 +``` + +_Replace_ `__` _with the name of your pod._ + +### Uninstalling the Deployment + +If you need to remove the deployment, run: + +```sh +helm uninstall portkey-app --namespace portkeyai +``` + +_This command will uninstall the Helm release and clean up the resources._ + +## Network Configuration + +### Step 1: Allow Access to the Service + +To make the service accessible from outside the cluster, define a Service of type `LoadBalancer` in your `values.yaml` or Helm templates. Specify the desired port for external access. + +```yaml +service: + type: LoadBalancer + port: + targetPort: 8787 +``` + +_Replace_ `__` _with the port number for external access with the port the application listens on internally._ + +### Step 2: Ensure Outbound Network Access + +By default, Kubernetes allows full outbound access, but if your cluster has NetworkPolicies that restrict egress, configure them to allow outbound traffic. + +**Example NetworkPolicy for Outbound Access:** + +```yaml +apiVersion: networking.k8s.io/v1 +kind: NetworkPolicy +metadata: + name: allow-all-egress + namespace: portkeyai +spec: + podSelector: {} + policyTypes: + - Egress + egress: + - to: + - ipBlock: + cidr: 0.0.0.0/0 +``` + +_This allows the gateway to access LLMs hosted within your VPC and outside as well. This also enables connection for the sync service to the Portkey Control Plane._ + +### Step 3: Configure Inbound Access for Portkey Control Plane + +Ensure the Portkey control plane can access the service either over the internet or through VPC peering. + +**Over the Internet:** + +* Ensure the LoadBalancer security group allows inbound traffic on the specified port. +* Document the public IP/hostname and port for the control plane connection. + +**Through VPC Peering:** + +* Set up VPC peering between your GCP account and the control plane's GCP account. Requires manual setup by Portkey Team. + +This guide provides the necessary steps and configurations to deploy Portkey on GCP effectively, ensuring high availability, scalability, and integration with your existing infrastructure. diff --git a/virtual_key_old/product/enterprise-offering/security-portkey.mdx b/virtual_key_old/product/enterprise-offering/security-portkey.mdx new file mode 100644 index 00000000..a761620b --- /dev/null +++ b/virtual_key_old/product/enterprise-offering/security-portkey.mdx @@ -0,0 +1,97 @@ +--- +title: "Security @ Portkey" +description: Portkey AI provides a secure, reliable AI gateway for the seamless integration and management of large language models (LLMs). +--- +This document outlines our robust security protocols, designed to meet the needs of businesses requiring high standards of data protection and operational reliability. + +At Portkey AI, we understand the critical importance of security in today's digital landscape and are committed to delivering state-of-the-art solutions that protect our clients' data and ensure their AI applications run smoothly. + +## Security Framework + +### Authentication + +Portkey AI ensures secure API access through token-based authentication mechanisms, supporting Single Sign-On (SSO) via OIDC on enterprise plans. + +We also implement [Virtual Keys](/product/ai-gateway/virtual-keys), which provide an added layer of security by securely storing provider API keys within a controlled and monitored environment. + +This multi-tier authentication strategy is crucial for protecting against unauthorized access and ensuring the integrity of user interactions. + +### Encryption + +To protect data integrity and privacy, all data in transit to and from Portkey AI is encrypted using TLS 1.2 or higher. We employ AES-256 encryption for data at rest, safeguarding it against unauthorized access and breaches. + +These encryption standards are part of our commitment to maintaining secure data channels and storage. + +### Access Control + +Our access control measures are designed with granularity to offer precise control over who can see and manage data. + +For enterprise clients, we provide enhanced [Role-Based Access Control (RBAC)](/product/enterprise-offering/access-control-management#id-2.-fine-grained-user-roles-and-permissions), allowing for stringent governance suited to complex organizational needs. + +This system is pivotal for enforcing security policies and ensuring that only authorized personnel have access to sensitive operations and data. + +## Compliance and Data Privacy + +### Compliance with Standards + +Our platform is compliant with leading security standards, including [SOC2, ISO27001, GDPR, and HIPAA](https://trust.portkey.ai). + +Portkey AI undergoes regular audits, compliance checks, and penetration testing conducted by third-party security experts to ensure continuous adherence to these standards. + +These certifications demonstrate our commitment to global security practices and our ability to meet diverse regulatory requirements. + + + + + +### Privacy Protections + +At Portkey AI, we prioritize user privacy. Our privacy protocols are designed to comply with international data protection regulations, ensuring that all data is handled responsibly. + +We engage in minimal data retention and deploy advanced anonymization technologies to protect personal information and sensitive data from being exposed or improperly used. + +Read our privacy policy here - [https://portkey.ai/privacy](https://portkey.ai/privacy)[\-policy](https://portkey.ai/privacy-policy) + +## System Integrity and Reliability + +### **Network and System Security**: + +We protect our systems with advanced firewall technologies and DDoS prevention mechanisms to thwart a wide range of online threats. Our security measures are designed to shield our infrastructure from malicious attacks and ensure continuous service availability. + +### **Reliability and Availability**: + +Portkey AI offers an industry-leading [99.995% uptime](https://status.portkey.ai), supported by a global network of 310 data centers. + +This extensive distribution allows for effective load balancing and edge deployments, minimizing latency and ensuring fast, reliable service delivery across geographical locations. + +Our failover mechanisms are sophisticated, designed to handle unexpected scenarios seamlessly and without service interruption. + +## Incident Management and Continuous Improvement + +### Incident Response + +Our proactive incident response team is equipped with the tools and procedures necessary to quickly address and resolve security incidents. + +This includes comprehensive risk assessments, immediate containment actions, and detailed investigations to prevent future occurrences. + +We maintain transparent communication with our clients throughout the incident management process. Please review our [status page](https://status.portkey.ai) for incident reports. + +### Updates and Continuous Improvement + +Security at Portkey AI is dynamic; we continually refine our security measures and systems to address emerging threats and incorporate best practices. Our ongoing commitment to improvement helps us stay ahead of the curve in cybersecurity and operational performance. + +## Contact Information + +For more detailed information or specific inquiries regarding our security measures, please reach out to our support team: + +* **Email**: support@portkeyai.com, dpo@portkey.ai + +## Useful Links + +[Privacy Policy](https://portkey.ai/privacy-policy) + +[Terms of Service](https://portkey.ai/terms) + +[Data Processing Agreement](https://portkey.ai/dpa) + +[Trust Portal](https://trust.portkey.ai) diff --git a/virtual_key_old/product/guardrails.mdx b/virtual_key_old/product/guardrails.mdx new file mode 100644 index 00000000..64e22d2a --- /dev/null +++ b/virtual_key_old/product/guardrails.mdx @@ -0,0 +1,665 @@ +--- +title: "Guardrails" +description: "Ship to production confidently with Portkey Guardrails on your requests & responses" +--- + + +This feature is available on all plans. +* **Developer**: Access to `BASIC` Guardrails +* **Production**: Access to `BASIC`, `PARTNER`, `PRO` Guardrails. +* **Enterprise**: Access to **all** Guardrails plus `custom` Guardrails. + + +LLMs are brittle - not just in API uptimes or their inexplicable `400`/`500` errors, but also in their core behavior. You can get a response with a `200` status code that completely errors out for your app's pipeline due to mismatched output. With Portkey's Guardrails, we now help you enforce LLM behavior in real-time with our _Guardrails on the Gateway_ pattern. + +Use Portkey's Guardrails to verify your LLM inputs AND outputs, adhering to your specifed checks. Since Guardrails are built on top of our [Gateway](https://github.com/portkey-ai/gateway), you can orchestrate your request - with actions ranging from _denying the request_, _logging the guardrail result_, _creating an evals dataset_, _falling back to another LLM or prompt_, _retrying the request_, and more. + +#### Examples of Guardrails Portkey offers: + +* **Regex match** \- Check if the request or response text matches a regex pattern +* **JSON Schema** \- Check if the response JSON matches a JSON schema +* **Contains Code** \- Checks if the content contains code of format SQL, Python, TypeScript, etc. +* **Custom guardrail** \- If you are running a custom guardrail currently, you can also integrate it with Portkey +* ...and many more. + +Portkey currently offers 20+ deterministic guardrails like the ones described above as well as LLM-based guardrails like `Detect Gibberish`, `Scan for prompt injection`, and more. These guardrails serve as protective barriers that help mitigate risks associated with Gen AI, ensuring its responsible and ethical deployment within organizations. + + + + + Portkey also integrates with your favourite Guardrail platforms like [Aporia](https://www.aporia.com/), [SydeLabs](https://sydelabs.ai/), [Pillar Security](https://www.pillar.security/) and more. Just add their API keys to Portkey and you can enable their guardrails policies on your Portkey calls! [More details on Guardrail Partners here.](/product/guardrails/list-of-guardrail-checks) + + +--- + +## Using Guardrails + +Putting Portkey Guardrails in production is just a 4-step process: + +1. Create Guardrail Checks +2. Create Guardrail Actions +3. Enable Guardrail through Configs +4. Attach the Config to a Request + +This flowchart shows how Portkey processes a Guardrails request: + + +Let's see in detail below: + + + Portkey only evaluates the last message in the request body when running guardrails checks. + +--- + +## 1\. Create a New Guardrail & Add Checks + +On the `Guardrails` page, click on `Create` and add your preferred Guardrail checks from the right sidebar. + + + + On Portkey, you can configure Guardrails to be run on either the `INPUT` (i.e. `PROMPT`) or the `OUTPUT`. Hence, for the Guardrail you create, make sure your Guardrail is only validating **ONLY ONE OF** the **Input** or the **Output**. + + + + + + +Each Guardrail Check has a custom input field based on its usecase โ€” just add the relevant details to the form and save your check. + + + +* You can add as many checks as you want to a single Guardrail. +* A check ONLY returns a boolean (`Yes`/`No`) verdict. + + + + +--- + +## 2\. Add Guardrail Actions + +Define a basic orchestration logic for your Guardrail here. + + + Guardrail is created to validate **ONLY ONE OF** the `Input` or the `Output`. The Actions set here will also apply only to either the `request` or the `response`. + + + + + + +### There are 6 Types of Guardrail Actions + +| Action | State | Description | Impact | +|:-------------- |:-------------------------------------- |:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| **Async** | **TRUE** This is the **default** state | Run the Guardrail checks **asynchronously** along with the LLM request. | Will add no latency to your requestUseful when you only want to log guardrail checks without affecting the request | +| **Async** | **FALSE** | **On Request** Run the Guardrail check **BEFORE** sending the request to the **LLM** **On Response** Run the Guardrail check **BEFORE** sending the response to the **user** | Will add latency to the requestUseful when your Guardrail critical and you want more orchestration over your request based on the Guardrail result | +| **Deny** | **TRUE** | **On Request & Response** If any of the Guardrail checks **FAIL**, the request will be killed with a **446** status code. If all of the Guardrail checks **SUCCEED**, the request/response will be sent further with a **200** status code. | This is useful when your Guardrails are critical and upon them failing, you can not run the requestWe would advice running this action on a subset of your requests to first see the impact | +| **Deny** | **FALSE** This is the **default** state | **On Request & Response** If any of the Guardrail checks **FAIL**, the request will STILL be sent, but with a **246** status code. If all of the Guardrail checks **SUCCEED**, the request/response will be sent further with a **200** status code. | This is useful when you want to log the Guardrail result but do not want it to affect your result | +| **On Success** | **Send Feedback** | If **all of the** Guardrail checks **PASS**, append your custom defined feedback to the request | We recommend setting up this actionThis will help you build an "Evals dataset" of Guardrail results on your requests over time | +| **On Failure** | **Send Feedback** | If **any of the** Guardrail checks **FAIL**, append your custom feedback to the request | We recommend setting up this actionThis will help you build an "Evals dataset" of Guardrail results on your requests over time | + +Set the relevant actions you want with your checks, name your Guardrail and save it! When you save the Guardrail, you will get an associated `$Guardrail_ID` that you can then add to your request. + +--- + +## 3\. "Enable" the Guardrails through Configs + +This is where Portkey's magic comes into play. The Guardrail you created above is yet not an `Active` guardrail because it is not attached to any request. + +Configs is one of Portkey's most powerful features and is used to define all kinds of request orchestration - everything from caching, retries, fallbacks, timeouts, to load balancing. + + + + Now, you can use Configs to add **Guardrail checks** & **actions** to your request. + + + + + +### Option 1: Direct Guardrail Configuration (Recommended) + +Portkey now offers a more intuitive way to add guardrails to your configurations: + +| Config Key | Value | Description | +|:---------------------|:--------------------------------------|:-----------------------------------------------------------------| +| **input_guardrails** | `["guardrails-id-xxx", "guardrails-id-yyy"]` | Apply these guardrails to the **INPUT** before sending to LLM | +| **output_guardrails**| `["guardrails-id-xxx", "guardrails-id-yyy"]` | Apply these guardrails to the **OUTPUT** from the LLM | + +```json +{ + "retry": { + "attempts": 3 + }, + "cache": { + "mode": "simple" + }, + "virtual_key": "openai-xxx", + "input_guardrails": ["guardrails-id-xxx", "guardrails-id-yyy"], + "output_guardrails": ["guardrails-id-xxx", "guardrails-id-yyy"] +} +``` + +### Option 2: Hook-Based Configuration (For Creating [Raw Guardrails](/product/guardrails/creating-raw-guardrails-in-json)) + +You can also continue to use the original hook-based approach: + +| Type | Config Key | Value | Description | +|:------------------- |:--------------------------|:----------------------------|:----------------------------------------------------------------------| +| Before Request Hook | **before_request_hooks** | `[{"id":"$guardrail_id"}]` | These hooks run on the **INPUT** before sending to the LLM | +| After Request Hook | **after_request_hooks** | `[{"id":"$guardrail_id"}]` | These hooks run on the **OUTPUT** after receiving from the LLM | + + +```json +{ + "retry": { + "attempts": 3 + }, + "cache": { + "mode": "simple" + }, + "virtual_key": "openai-xxx", + "before_request_hooks": [{ + "id": "input-guardrail-id-xx" + }], + "after_request_hooks": [{ + "id": "output-guardrail-id-xx" + }] +} +``` + + +Both configuration approaches work identically - choose whichever is more intuitive for your team. The simplified `input_guardrails` and `output_guardrails` fields are recommended for better readability. + + +### Guardrail Behaviour on the Gateway + +For **asynchronous** guardrails (`async= TRUE`), Portkey returns the standard, default status codes from the LLM providers โ€” this is because the Guardrails verdict is not affecting how you orchestrate your requests. Portkey will only log the Guardrail result for you. + +But for **synchronous** requests (`async= FALSE`), Portkey can orchestrate your requests based on the Guardrail verdict. The behaviour is dependent on the following: + +* Guardrail Check Verdict (`PASS` or `FAIL`) AND +* Guardrail Action โ€” DENY Setting (`TRUE` or `FALSE`) + +Portkey sends different `request status codes` corresponding to your set Guardrail behaviour. + +For requests where `async= FALSE`: + +| Guardrail Verdict | DENY Setting | Returned Status Code | Description | +| :----------------- | :------------ | :-------------------- | :---------------------------------------------------------------------------------------------------------------------------------------- | +| **PASS** | **FALSE** | **200** | Guardrails have **passed**, request will be processed regardless | +| **PASS** | **TRUE** | **200** | Guardrails have **passed**, request will be processed regardless | +| **FAIL** | **FALSE** | **246** | Guardrails have **failed**, but the request should still **be processed.**Portkey introduces a new Status code to indicate this state. | +| **FAIL** | **TRUE** | **446** | Guardrails have **failed**, and the request should **not** **be processed.**Portkey introduces a new Status code to indicate this state. | + +### Example Config Using the New `246` & `446` Status Codes + + + + +```json +{ + "strategy": { + "mode": "fallback", + "on_status_codes": [246, 446] + }, + "targets": [ + {"virtual_key": "openai-key-xxx"}, + {"virtual_key": "anthropic-key-xxx"} + ], + "input_guardrails": ["guardrails-id-xxx"] +} +``` + + +```json +{ + "retry": { + "on_status_codes": [246], + "attempts": 5 + }, + "output_guardrails": ["guardrails-id-xxx"] +} +``` + + + + +Create these Configs in Portkey UI, save them, and get an associated Config ID to attach to your requests. [More here](/product/ai-gateway/configs). + +## 4\. Final Step - Attach Config to Request + +Now, while instantiating your Portkey client or while sending headers, just pass the Config ID. + + + + +```js +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + config: "pc-***" // Supports a string config id or a config object +}); +``` + + + +```py +portkey = Portkey( + api_key="PORTKEY_API_KEY", + config="pc-***" # Supports a string config id or a config object +) +``` + + + +```js +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + apiKey: "PORTKEY_API_KEY", + config: "CONFIG_ID" + }) +}); +``` + + + +```py +client = OpenAI( + api_key="OPENAI_API_KEY", # defaults to os.environ.get("OPENAI_API_KEY") + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY", # defaults to os.environ.get("PORTKEY_API_KEY") + config="CONFIG_ID" + ) +) +``` + + + +```sh +curl https://api.portkey.ai/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-config: $CONFIG_ID" \ + -d '{ + "model": "gpt-3.5-turbo", + "messages": [{ + "role": "user", + "content": "Hello!" + }] + }' +``` + + + +For more, refer to the [Config documentation](/product/ai-gateway/configs). + +--- + +## Viewing Guardrail Results in Portkey Logs + +Portkey Logs will show you detailed information about Guardrail results for each request. + +### On the `Feedback & Guardrails` tab on the log drawer, you can see + +#### Guardrail Details + +* **Overview**: How many checks `passed` and how many `failed` +* **Verdict**: Guardrail verdict for each of the checks in your Guardrail +* **Latency**: Round trip time for each check in your Guardrail + +#### Feedback Details + +Portkey will also show the feedback object logged for each request + +* `Value`:** The numerical feedback value you passed +* `Weight`: The numerical feedback weight +* `Metadata Key & Value`:** Any custom metadata sent with the feedback +* `successfulChecks`: Which checks associated with this request `passed` +* `failedChecks`: Which checks associated with this request `failed` +* `erroredChecks`: If there were any checks that errored out along the way + + + + + + +--- + + + + + + + + +## Understanding Guardrail Response Structure + +When Guardrails are enabled and configured to run synchronously (`async=false`), Portkey adds a `hook_results` object to your API responses. This object provides detailed information about the guardrail checks that were performed and their outcomes. + +### Hook Results Structure + +The `hook_results` object contains two main sections: + +```json +"hook_results": { + "before_request_hooks": [...], // Guardrails applied to the input + "after_request_hooks": [...] // Guardrails applied to the output +} +``` + +Each section contains an array of guardrail execution results, structured as follows: + + + + Overall verdict of this guardrail (true = passed, false = failed). Only true if all checks within this guardrail passed. + + + The ID of the guardrail that was executed (e.g., "pg-check-cfa540") + + + Indicates if the guardrail modified the request or response (e.g., PII redaction) + + + An array of individual check results within this guardrail + + + Check-specific data that varies based on the guardrail type (e.g., for wordCount it includes counts and thresholds) + + + Result of this specific check (true = passed, false = failed) + + + Identifier of the specific check (e.g., "default.sentenceCount", "default.wordCount") + + + Time taken to execute this check in milliseconds + + + Whether this check modified the content + + + Timestamp when the check was executed + + + Additional logging information (if available) + + + Error message or object if the guardrail encountered an error + + + + + Feedback information configured in the guardrail + + + The numerical feedback value + + + The weight assigned to this feedback + + + Additional metadata including which checks succeeded or failed + + Comma-separated list of checks that passed + + + Comma-separated list of checks that failed + + + Comma-separated list of checks that encountered errors + + + + + + Total execution time for the guardrail in milliseconds + + + Whether the guardrail was run asynchronously + + + Always "guardrail" for guardrail hooks + + + Timestamp when the guardrail was executed + + + Whether failed checks should deny the request/response + + + +### Example Hook Results + +Here's a simplified example of what the `hook_results` might look like in a response: + +```json [expandable] +"hook_results": { + "before_request_hooks": [ + { + "verdict": true, + "id": "sentence_and_word_check_guardrail", + "transformed": false, + "checks": [ + { + "data": { + "sentenceCount": 1, + "minCount": 1, + "maxCount": 99999, + "not": false, + "verdict": true, + "explanation": "The sentence count (1) is within the specified range of 1 to 99999.", + "textExcerpt": "when does the flight from new york to bengaluru land tomorrow, what time, what is its flight number,..." + }, + "verdict": true, + "id": "default.sentenceCount", + "execution_time": 0, + "transformed": false, + "created_at": "2025-05-20T08:00:59.492Z", + "log": null + }, + { + "data": { + "wordCount": 24, + "minWords": 1, + "maxWords": 99999, + "not": false, + "verdict": true, + "explanation": "The text contains 24 words, which is within the specified range of 1-99999 words.", + "textExcerpt": "when does the flight from new york to bengaluru land tomorrow, what time, what is its flight number,..." + }, + "verdict": true, + "id": "default.wordCount", + "execution_time": 0, + "transformed": false, + "created_at": "2025-05-20T08:00:59.492Z", + "log": null + } + ], + "feedback": { + "value": 5, + "weight": 1, + "metadata": { + "successfulChecks": "default.sentenceCount, default.wordCount", + "failedChecks": "", + "erroredChecks": "" + } + }, + "execution_time": 0, + "async": false, + "type": "guardrail", + "created_at": "2025-05-20T08:00:59.492Z", + "deny": false + }, + { + "verdict": true, + "id": "character_check_guardrai", + "transformed": false, + "checks": [ + { + "data": { + "characterCount": 130, + "minCharacters": 1, + "maxCharacters": 9999999, + "not": false, + "verdict": true, + "explanation": "The text contains 130 characters, which is within the specified range of 1-9999999 characters.", + "textExcerpt": "when does the flight from new york to bengaluru land tomorrow, what time, what is its flight number,..." + }, + "verdict": true, + "id": "default.characterCount", + "execution_time": 0, + "transformed": false, + "created_at": "2025-05-20T08:00:59.492Z", + "log": null + } + ], + "feedback": { + "value": 5, + "weight": 1, + "metadata": { + "successfulChecks": "default.characterCount", + "failedChecks": "", + "erroredChecks": "" + } + }, + "execution_time": 0, + "async": false, + "type": "guardrail", + "created_at": "2025-05-20T08:00:59.492Z", + "deny": false + } + ], + "after_request_hooks": [ + { + "verdict": true, + "id": "sentence_and_word_check_guardrail", + "transformed": false, + "checks": [ + { + "data": { + "sentenceCount": 2, + "minCount": 1, + "maxCount": 99999, + "not": false, + "verdict": true, + "explanation": "The sentence count (2) is within the specified range of 1 to 99999.", + "textExcerpt": "I'm unable to provide real-time flight information, such as specific flight times, numbers, or bagga..." + }, + "verdict": true, + "id": "default.sentenceCount", + "execution_time": 0, + "transformed": false, + "created_at": "2025-05-20T08:01:02.229Z", + "log": null + }, + { + "data": { + "wordCount": 46, + "minWords": 1, + "maxWords": 99999, + "not": false, + "verdict": true, + "explanation": "The text contains 46 words, which is within the specified range of 1-99999 words.", + "textExcerpt": "I'm unable to provide real-time flight information, such as specific flight times, numbers, or bagga..." + }, + "verdict": true, + "id": "default.wordCount", + "execution_time": 0, + "transformed": false, + "created_at": "2025-05-20T08:01:02.229Z", + "log": null + } + ], + "feedback": { + "value": 5, + "weight": 1, + "metadata": { + "successfulChecks": "default.sentenceCount, default.wordCount", + "failedChecks": "", + "erroredChecks": "" + } + }, + "execution_time": 0, + "async": false, + "type": "guardrail", + "created_at": "2025-05-20T08:01:02.229Z", + "deny": false + } + ] +} +} +``` +This example corresponds to a `config` like: +```json +{ + "input_guardrails": [ + "sentence_and_word_check_guardrail", // Contains sentenceCount and wordCount checks + "characer_check_guardrail" // Contains characterCount check + ], + "output_guardrails": [ + "sentence_and_word_check_guardrail" // The same guardrail can be used for both input and output + ] +} +``` +### Important Notes + +- If a guardrail is configured to run asynchronously (`async=true`), the `hook_results` will not appear in the API response. The results will only be available in the Portkey logs. +- The `data` field varies based on the type of guardrail check being performed. Each guardrail type returns different information relevant to its function. +- The overall `verdict` for a guardrail is `true` only if all individual checks pass. If any check fails, the verdict will be `false`. +- When `transformed` is `true`, it indicates that the guardrail has modified the content (such as redacting PII). +- If `deny` is `true` and the verdict is `false`, the request will be denied with a 446 status code. + +### Special Fields + +- **Check-specific data**: Each guardrail type provides different data in the `data` field. For example, a sentence count check provides information about the number of sentences, while a PII check might provide information about detected PII entities. +- **Feedback metadata**: The `metadata` object within `feedback` keeps track of which checks were successful, failed, or encountered errors. + + + + + + + + + + + + + + +## Defining Guardrails Directly in JSON + +On Portkey, you can also create the Guardrails in code and add them to your Configs. Read more about this here: + + + +--- + + + +## Bring Your Own Guardrails + +If you already have a custom guardrail pipeline where you send your inputs/outputs for evaluation, you can integrate it with Portkey using a modular, custom webhook! Read more here: + + + +--- + +## Examples of When to Deny Requests with Guardrails + +1. **Prompt Injection Checks**: Preventing inputs that could alter the behavior of the AI model or manipulate its responses. +2. **Moderation Checks**: Ensuring responses do not contain offensive, harmful, or inappropriate content. +3. **Compliance Checks**: Verifying that inputs and outputs comply with regulatory requirements or organizational policies. +4. **Security Checks**: Blocking requests that contain potentially harmful content, such as SQL injection attempts or cross-site scripting (XSS) payloads. + +By appropriately configuring Guardrail Actions, you can maintain the integrity and reliability of your AI app, ensuring that only safe and compliant requests are processed. + + +To enable Guardrails for your org, ping us on the [Portkey Discord](https://portkey.ai/community) + +--- diff --git a/virtual_key_old/product/guardrails/creating-raw-guardrails-in-json.mdx b/virtual_key_old/product/guardrails/creating-raw-guardrails-in-json.mdx new file mode 100644 index 00000000..faf03b76 --- /dev/null +++ b/virtual_key_old/product/guardrails/creating-raw-guardrails-in-json.mdx @@ -0,0 +1,62 @@ +--- +title: "Creating Raw Guardrails (in JSON)" +description: With the raw Guardrails mode, we let you define your Guardrail checks & actions however you want, directly in code. +--- + +At Portkey, we believe in helping you make your workflows [as modular as possible](https://portkey.ai/blog/what-it-means-to-go-to-prod/). +This is useful when: +* You want the same Guardrail checks but want to take different basic actions on them +* Your Guardrail checks definitions are dependent on an upstream task and are updated in code +* You want greater control over how you want to handle Guardrails + +With the Raw Guardrails mode, you can achieve all this. + +### Example of a Raw Guardrail + +```JSON +"before_request_hooks": [{ + "type": "guardrail", + "id": "my_solid_guardrail", + "checks": [{ + "id": "default.regexMatch", + "parameters": { + "regex": "test" + } + }] +}] +``` + +In this example: + +* `type`: Specifies the type of hook, which is `guardrail`. +* `name`: Gives a name to the guardrail for identification. +* `checks`: Lists the checks that make up the guardrail. Each check includes an `id` and `parameters` for the specific conditions to validate. + +### Configuring Guardrail Actions + +```JSON +"before_request_hooks": [{ + "type": "guardrail", + "id": "my_solid_guardrail", + "checks": [{ + "id": "default.regexMatch", + "parameters": { + "regex": "test" + } + }], + "deny": false, + "async": false, + "on_success": { + "feedback": {"value": 1,"weight": 1} + }, + "on_fail": { + "feedback": {"value": -1,"weight": 1} + } +}] +``` +In this example, + +* `deny`: Is set to `TRUE` or `FALSE` +* `async`: Is set to `TRUE` or `FALSE` +* `on_success`: Used to pass custom `feedback` +* `on_failure`: Used to pass custom `feedback` diff --git a/virtual_key_old/product/guardrails/embedding-guardrails.mdx b/virtual_key_old/product/guardrails/embedding-guardrails.mdx new file mode 100644 index 00000000..76ef6c42 --- /dev/null +++ b/virtual_key_old/product/guardrails/embedding-guardrails.mdx @@ -0,0 +1,220 @@ +--- +title: "Guardrails for Embedding Requests" +description: "Apply security and data validation measures to vector embedding requests to protect sensitive information and ensure data quality." +--- + +Portkey's guardrails aren't limited to chat completions - they can also be applied to embedding requests. This means you can protect your embedding workflows with the same robust security measures you use for your other LLM interactions. +## Why Use Guardrails for Embeddings? + +Vector embeddings form the backbone of modern AI applications, transforming text into numerical representations that power semantic search, recommendation systems, and RAG pipelines. However, unprotected embedding workflows create significant business risks that technical leaders cannot ignore. + +Without proper guardrails, sensitive customer data can leak into vector databases, toxic content can contaminate downstream systems, and resources are wasted embedding low-quality inputs. Protecting these workflows is essential because: + +1. **Data leakage prevention**: Stop PII, PHI, or sensitive information from being sent to embedding models +2. **Data quality control**: Ensure only clean, formatted data gets embedded +3. **Cost optimization**: Avoid unnecessary API calls for data that doesn't meet your criteria +4. **Compliance**: Maintain regulatory compliance by filtering problematic content + +By implementing guardrails at the embedding stage, you create a critical safety layer that protects your entire AI pipeline. For technical teams already building with embeddings, Portkey's guardrails integrate seamlessly with existing workflows while providing the security measures that enterprise applications demand. + + + +## How It Works + +Guardrails for embeddings are applied at the "before request" stage, examining your text before it's sent to the embedding model: + + +```mermaid +flowchart LR + App[Your Application] --> Portkey[Portkey Gateway] + Portkey --> Guardrails[Guardrail Checks] + + Guardrails -->|Pass| Success[/Success\nStatus: 240/] + Guardrails -->|Fail + Deny=false| Warning[/Warning\nStatus: 446/] + Guardrails -->|Fail + Deny=true| Blocked[/Blocked\nStatus: 446/] + + + Success --> LLM[LLM Provider] + Warning --> LLM + + + class Success success + class Warning warning + class Blocked danger +``` + +1. Your application sends text to Portkey for embedding +2. Portkey's guardrails analyze the text before sending to the LLM provider +3. If the text passes all checks, it's sent to the embedding model +4. If it fails, the configured [guardrail action](/product/guardrails) is taken (deny, feedback, etc.) + +## Supported Guardrails for Embeddings + +You can use any of Portkey's "before request" guardrails with embedding requests: + + + + Protect user privacy by preventing PII from being embedded + + + Filter content based on custom pattern matching + + + Block embedding requests with specific words/phrases + + + Ensure embeddings meet appropriate length requirements + + + Detect and block code snippets from being embedded + + + Implement your own custom guardrail logic + + + Utilize guardrails from Pangea, Pillar, and other partners + + + Prevent healthcare data from entering embedding systems + + + Filter out harmful content before embedding + + + +[Learn More...](/product/guardrails) +## Setting Up Embedding Guardrails + +### 1. Create a Guardrail + +Follow the standard process to create a guardrail in Portkey: + +* Navigate to the `Guardrails` page and click `Create` +* Select the appropriate check from available guardrails (e.g., PII Detection, Regex Match) +* Configure the check parameters & set desired actions for failed checks +* Save the guardrail to get its ID + + + Make sure to select guardrails that support the `beforeRequestHook` since embeddings only use pre-request validation. + + +### 2. Add the Guardrail to Your Config + +Add your guardrail ID to the `before_request_hooks` in your Portkey [config](/product/ai-gateway/configs): + +```json +{ + "input_guardrails": ["gr-xxx", "gr-yyy", ...] +} +``` + +### 3. Use the Config with Embedding Requests + + + +```python +# Initialize Portkey with your config +portkey = Portkey( + api_key="PORTKEY_API_KEY", + config="pc-xxx" # Config with embedding guardrails +) + +# Make your embedding request +response = portkey.embeddings.create( + input="Your text string goes here", + model="text-embedding-3-small" +) + +``` + + +```javascript +// Initialize Portkey with your config +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + config: "pc-xxx" // Config with embedding guardrails +}); + +// Make your embedding request +const response = await portkey.embeddings.create({ + input: "Your text string goes here", + model: "text-embedding-3-small" +}); + + +``` + + +```python +from openai import OpenAI +from portkey.api_client.openai import createHeaders + +client = OpenAI( + api_key="OPENAI_API_KEY", + base_url="PORTKEY_GATEWAY_URL", + default_headers=createHeaders( + provider="openai", + api_key="PORTKEY_API_KEY", + config="pc-xxx" # Config with embedding guardrails + ) +) + +# Make your embedding request +response = client.embeddings.create( + input="Your text string goes here", + model="text-embedding-3-small" +) + + +``` + + +```bash +curl https://api.portkey.ai/v1/embeddings \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-config: pc-xxx" \ + -d '{ + "model": "text-embedding-3-small", + "input": "Your text string goes here" + }' +``` + + + +## Common Use Cases + +- **Protecting Against PII in Embeddings**: When building search systems or RAG applications, you need to ensure no personally identifiable information is inadvertently embedded: + +- **Filtering Code from Document Embeddings**: If you're building a knowledge base that shouldn't include code snippets: + +- **Size-Based Filtering**: Ensure only appropriately sized documents get embedded: + +- **Custom Regex Filtering**: Create domain-specific filters using regex patterns: + + + +## Monitoring and Logs + +All guardrail actions on embedding requests are logged in the Portkey dashboard, just like other guardrail activities. You can: + +- See which embedding requests were blocked +- View detected issues (PII, regex matches, etc.) +- Track guardrail performance over time +- Export logs for compliance reporting + + + + + + +## Get Support + +If you're implementing guardrails for embeddings and need assistance, reach out to the Portkey team on the [community forum](https://discord.gg/portkey-llms-in-prod-1143393887742861333). + +## Learn More + +- [Portkey Guardrails Overview](/product/guardrails) +- [List of Guardrail Checks](/product/guardrails/list-of-guardrail-checks) +- [Creating Raw Guardrails in JSON](/product/guardrails/creating-raw-guardrails-in-json) diff --git a/virtual_key_old/product/guardrails/list-of-guardrail-checks.mdx b/virtual_key_old/product/guardrails/list-of-guardrail-checks.mdx new file mode 100644 index 00000000..cf2baa3d --- /dev/null +++ b/virtual_key_old/product/guardrails/list-of-guardrail-checks.mdx @@ -0,0 +1,226 @@ +--- +title: "List of Guardrail Checks" +--- + +Each Guardrail Check has a specific purpose, it's own parameters, supported hooks, and sources. + +## Partner Guardrails + + + + * Scan prompts and responses for security threats + * Detect PII, toxicity, and prompt injections + * Real-time content analysis and filtering + + + * Validate custom Aporia policies via project ID + * Define policies on your Aporia dashboard + * Seamless integration with Portkey checks + + + * Detect and redact sensitive PII data + * Apply Azure's comprehensive content safety checks + * Enterprise-grade security compliance + + + * Analyze and redact PII to prevent manipulation + * Integrate AWS Guardrails directly in Portkey + * Advanced security policy enforcement + + + * Analyze content for security risks and jailbreaks + * Enforce custom policy violations detection + * AI-powered threat detection and prevention + + + * Detect and filter harmful content automatically + * Multi-dimensional content safety analysis + * Real-time moderation capabilities + + + * Guard LLM inputs and outputs with Text Guard + * Detect malicious content and data transfers + * Prevent model manipulation attempts + + + * Detect hallucinations and factual errors + * Assess quality: conciseness, helpfulness, tone + * Identify gender and racial bias in outputs + + + * Scan prompts and responses comprehensively + * Detect PII, toxicity, and injection attacks + * Enterprise security and compliance features + + + * Scan prompts for security vulnerabilities + * Analyze responses for policy violations + * Advanced threat detection and mitigation + + + + + + +The logic for all of the Guardrail Checks (including Partner Guardrails) is open source. + + +View it [here](https://github.com/Portkey-AI/gateway/tree/feat/plugins/plugins/default) and [here](https://github.com/Portkey-AI/gateway/tree/feat/plugins/plugins/portkey) on the Portkey Gateway repo. + + +## Bring Your Own Guardrail + +We have built Guardrails in a very modular way, and support bringing your own Guardrail using a custom webhook! [Learn more here](/product/guardrails/list-of-guardrail-checks/bring-your-own-guardrails). + + +## Portkey's Guardrails + +Along with the partner Guardrails, there are also deterministic as well as LLM-based Guardrails supported natively by Portkey. + + +`BASIC` Guardrails are available on all Portkey plans.

+`PRO` Guardrails are available on Portkey Pro & Enterprise plans. +
+ + + + + +### `BASIC` โ€” Deterministic Guardrails + + +
+Checks if the request or response text matches a regex pattern.

+**Parameters**: rule: `string`
+**Supported On**: `input_guardrails`, `output_guardrails` +
+ +
+Checks if the content contains a certain number of sentences. Ranges allowed.

+**Parameters**: minSentences: `number`, maxSentences: `number`
+**Supported On**: `input_guardrails`, `output_guardrails` +
+ +
+Checks if the content contains a certain number of words. Ranges allowed.

+**Parameters**: minWords: `number`, maxWords: `number`
+**Supported On**: `input_guardrails`, `output_guardrails` +
+ +
+Checks if the content contains a certain number of characters. Ranges allowed.

+**Parameters**: minCharacters: `number`, maxCharacters: `number`
+**Supported On**: `input_guardrails`, `output_guardrails` +
+ +
+Check if the response JSON matches a JSON schema.

+**Parameters**: schema: `json`
+**Supported On**: `output_guardrails` only +
+ +
+Check if the response JSON contains any, all or none of the mentioned keys.

+**Parameters**: keys: `array`, operator: `string`
+**Supported On**: `output_guardrails` only +
+ +
+Checks if the content contains any, all or none of the words or phrases.

+**Parameters**: words: `array`, operator: `string`
+**Supported On**: `output_guardrails` only +
+ +
+Checks if all the URLs mentioned in the content are valid

+**Parameters**: onlyDNS: `boolean`
+**Supported On**: `output_guardrails` only +
+ +
+Checks if the content contains code of format SQL, Python, TypeScript, etc.

+**Parameters**: format: `string`
+**Supported On**: `output_guardrails` only +
+ +
+Check if the given string is lowercase or not.

+**Parameters**: format: `string`
+**Supported On**: `input_guardrails`, `output_guardrails` +
+ +
+Check if the content ends with a specified string.

+**Parameters**: Suffix: `string`
+**Supported On**: `input_guardrails`, `output_guardrails` +
+ +
+Makes a webhook request for custom guardrails

+**Parameters**: webhookURL: `string`, headers: `json`
+**Supported On**: `input_guardrails`, `output_guardrails` +
+ +
+Check if the JWT token is valid. +
+**Parameters**: +JWKS URI: `string`, +Header Key: `string`, +Cache Max Age: `number`, +Clock Tolerance: `number`, +Max Token Age: `number` (in seconds) + + +**Supported On**: `input_guardrails` +
+ +
+Check if the inference model to be used is in the whitelist. +
+**Parameters**: +Models: `array`, Inverse: `boolean` + + +**Supported On**: `input_guardrails` +
+ + +
+ +### `PRO` โ€” LLM Guardrails + + +
+Checks if the content passes the mentioned content moderation checks.

+**Parameters**: categories: `array`
+**Supported On**: `input_guardrails` only +
+ +
+Checks if the response content is in the mentioned language.

+**Parameters**: language: `string`
+**Supported On**: `input_guardrails` only +
+ +
+Detects Personally Identifiable Information (PII) in the content.

+**Parameters**: categories: `array`
+**Supported On**: `input_guardrails`, `output_guardrails` +
+ +
+Detects if the content is gibberish.

+**Parameters**: `boolean`
+**Supported On**: `input_guardrails`, `output_guardrails` +
+
+ + + + You can now have configurable timeouts for Partner & Pro Guardrails! + + +## Contribute Your Guardrail +Integrate your Guardrail platform with Portkey Gateway and reach our growing user base. +Check out some [existing integrations](https://github.com/portkey-ai/gateway) to get started. diff --git a/virtual_key_old/product/guardrails/pii-redaction.mdx b/virtual_key_old/product/guardrails/pii-redaction.mdx new file mode 100644 index 00000000..fffff3f2 --- /dev/null +++ b/virtual_key_old/product/guardrails/pii-redaction.mdx @@ -0,0 +1,132 @@ +--- +title: PII Redaction +description: Replace any sensitive data in requests with standard identifiers +--- + +Advanced PII Redaction feature automatically detects and redacts sensitive information from requests before they reach the LLM. This feature works seamlessly with our entire guardrails ecosystem, providing an additional layer of security for your AI interactions. + +## Enabling PII Redaction +On the Guardrail creation page, for select PII guardrails, you will see a **Redact PII** toggle. Just enable it to start redacting PII in your requests. + + + + +## Guardrails Support + +PII redaction is supported across 5 guardrail providers: + + + +Redact `Phone number`, `Email addresses`, `Location information`, `IP addresses`, `Social Security Numbers (SSN)`, `Names`, `Credit card information` from requests + + + You can select from a list of predefined PII or define a custom sensitive-information type using regular expressions (RegEx) and redact PII. + + + + Advanced detection of personally identifiable information (PII) and protected health information (PHI) to safeguard sensitive data. + + + + Based on Patronus's EnterprisePII dataset, this guardrail can detect and redact confidential information typically found in business documents like meeting notes, commercial contracts, marketing emails, performance reviews, and more + + + + Pangea's redact feature can redact PII like geographic locations, payment card industry (PCI) data, and many other types of sensitive information, with support for rule customization + + + + Promptfoo helps detect multiple PII exposures - in session data, via social engineering, or a direct exposure. + + + +## How It Works + +1. **Detection**: When enabled, the system scans incoming or outgoing requests for PII using the configured guardrail provider. + +2. **Redaction**: Detected PII is automatically replaced with standardized identifiers: + - Email addresses โ†’ `{{EMAIL_ADDRESS_1}}`, `{{EMAIL_ADDRESS_2}}`, etc. + - Phone numbers โ†’ `{{PHONE_NUMBER_1}}`, `{{PHONE_NUMBER_2}}`, etc. + - And similar patterns for other PII types + +3. **Processing**: The redacted request is then forwarded to the LLM, ensuring sensitive data never reaches the model. + +Example: + +``` +Original Request: +"Hi, you can reach me at john@example.com or 555-0123" + +Redacted Request: +"Hi, you can reach me at {{EMAIL_ADDRESS_1}} or {{PHONE_NUMBER_1}}" +``` + +## Monitoring PII Redaction + +You can track request transformations through two key indicators in the request/response body: +1. `transformed` boolean flag: Indicates whether any redaction occurred +2. `check_results` object: Contains detailed information about specific transformations + +## Best Practices + +1. **Gradual Implementation**: + - Start by enabling the feature for a subset of requests + - Monitor the logs and transformation results + - Gradually expand coverage after validation + +2. **Regular Monitoring**: + - Review transformation logs periodically + - Validate that sensitive information is being caught appropriately + +3. **Documentation**: + - Maintain records of what types of PII you're scanning for + - Document any specific compliance requirements being addressed + +## Security Considerations + +- Redaction is irreversible by design +- Original PII storage and handling varies by guardrail provider +- The feature can be applied to both input and output content + +**Compliance Implications** + +This feature can help organizations meet various compliance requirements by: +- Preventing accidental exposure of sensitive data to LLMs +- Providing audit trails of PII handling +- Supporting data minimization principles +- Enabling systematic PII management across AI operations + +## Limitations + +- Redaction patterns are not customizable +- Transformation is one-way (non-reversible) +- Performance may vary based on chosen guardrail provider + +## Troubleshooting + +If you experience issues: +1. Verify the feature is enabled in your guardrails configuration +2. Check the `transformed` flag and `check_results` for specific transformation details +3. Review logs for any error messages or unexpected behavior +4. [Contact us here](https://portkey.wiki/community) for additional assistance + +## FAQs + + + + +Currently, redaction patterns are standardized and not customizable. + + +Each instance receives a numbered identifier (e.g., `{{EMAIL_ADDRESS_1}}`, `{{EMAIL_ADDRESS_2}}`, etc.). + + +Impact varies by guardrail provider and request complexity. + + +Yes, the feature works with any LLM supported by Portkey. + + +Yes, you can configure the guardrail to scan both requests and responses. + + diff --git a/virtual_key_old/product/integrations.mdx b/virtual_key_old/product/integrations.mdx new file mode 100644 index 00000000..047c164c --- /dev/null +++ b/virtual_key_old/product/integrations.mdx @@ -0,0 +1,110 @@ +--- +title: LLM Integrations +description: A step-by-step guide for organization admins to set up their first integration. +--- + +The **Integrations** page is the central command center for Organization Admins. It's where you securely manage all third-party LLM provider credentials and govern their use across all workspaces from a single, unified dashboard. + +This "create once, provision many" model saves significant time, reduces configuration errors, and gives you complete oversight of your AI stack. + +### **Understanding the Integrations Dashboard** + +The Integrations page is organized into three tabs, each serving a distinct purpose: + +* **`All`**: This is a comprehensive list of all 50+ providers Portkey supports. This is your starting point for connecting a new provider to your organization. +* **`Connected`**: This tab lists all the integrations that you have personally connected at the organization level. It's your primary view for managing your centrally-governed providers. +* **`Workspace-Created`**: This tab gives you complete visibility and governance over any integrations created *by Workspace Admins* for their specific workspaces. It ensures that even with delegated control, you maintain a full audit trail and can manage these instances if needed. + +--- + +### **Creating and Provisioning a New Integration** + +This guide walks you through connecting a new provider and making it available to your workspaces. + +#### **Step 1: Connect the Provider** + + +If you are an existing Portkey user, this step is similar to creating a Virtual Key, but it's happening at the organization level. + + +1. From the **`All`** tab, find the provider you want to connect (e.g., OpenAI, Azure OpenAI, AWS Bedrock) and click **Connect**. +2. Fill in the details: + * **Integration Name:** A friendly name for you to identify this connection (e.g., "Azure Production - US East"). + * **Slug:** A unique, URL-friendly identifier. This will be used by developers to call models (e.g., `azure-prod-useast`). + * **Credentials:** Securely enter your API keys or other authentication details. These are encrypted and will not be visible after saving. +3. Click **Next**. + +##### **Special Case: Connecting to Azure OpenAI** + +Portkey allows you to manage multiple Azure deployments under a single integration. This is useful when different models or versions are in separate deployments. + +1. When connecting an Azure integration, you can add multiple deployments. +2. Assign a unique **`alias`** to each deployment (e.g., `gpt-4o-deploy`, `gpt-35-deploy`). +3. Set one deployment as the default. + +To use a specific deployment, developers will pass its `alias` as the `model` in their LLM request. If no alias is provided, the default deployment is used. + + + + + +##### **Special Case: Connecting to Self-Hosted LLMs** + +You can manage your privately hosted models alongside commercial providers. + +1. When creating a new integration, enable the **"Local/Privately hosted provider"** toggle. +2. Select the API specification your LLM implements (e.g., OpenAI). +3. Enter your model's base URL in the **"Custom Host"** field and add any required authentication headers. + +This allows you to use your self-hosted models with all Portkey features. For more details, see [Bring Your Own LLM](/product/ai-gateway/byollm). + +#### **Step 2: Provision to Workspaces** + +Here, you decide which teams get access to this provider and under what conditions. + +1. You will see a list of all workspaces within your organization. +2. Use the toggle next to a workspace name to **enable or disable** access. +3. For each enabled workspace, you can optionally click **Edit Budget & Rate Limits** to set specific spending caps or request limits that apply *only to that workspace* for this integration. +4. **(Optional) For Provisioning to New Workspaces:** Toggle on **"Automatically provision this integration for new workspaces"** to ensure any future teams automatically get access with a default budget/rate limit you define. +5. Click **Next**. + +#### **Step 3: Provision Specific Models** + +This is where you enforce model governance and control costs. + +1. You will see a list of all models available from the provider you're connecting. +2. You can **Clear all** and then select only the models you wish to approve for use. +3. **(Optional) For Dynamic Models:** If you're using a provider like Fireworks AI with many community models, you can toggle on **"Automatically enable new models"**. For providers like OpenAI or Azure, we recommend an explicit allow-list for better cost control. +4. Click **Create Integration**. + +**That's it!** You have successfully created and provisioned a centrally managed integration. It will now appear in your **`Connected`** tab. The workspaces you provisioned will see this as an available "AI Provider" in their Model Catalog, with access only to the models you specified and constrained by the budgets you set. + +### **Setting Budgets and Rate Limits** + +When provisioning an integration to a workspace, you can enforce powerful governance rules by setting budget and rate limits. This gives you granular control over costs and usage patterns. + + +Budget and Rate Limits are available on **Enterprise** and select **Pro** plans. + + +Click on **Edit Budget & Rate Limits** for any workspace to configure the following: + +* **Cost-Based Budgets:** Set a maximum budget in USD. Once the limit is reached, the provider will be disabled for that workspace to prevent overspending. +* **Token-Based Budgets:** Set a maximum number of tokens that can be consumed. +* **Request-Based Rate Limits:** Control the number of requests allowed per minute, hour, or day. +* **Token-Based Rate Limits:** Control the number of tokens consumed per minute, hour, or day. + +You can also configure **periodic resets** (weekly or monthly) for these limits, which is perfect for managing recurring team budgets. + +--- + +### FAQs + + + + Your API keys are always encrypted and stored in secure, isolated vaults. They are only decrypted in-memory, within sandboxed workers, at the exact moment a request is made to the provider. This ensures the highest level of security for your credentials. + + + We generate unique, random identifiers for each AI Provider. These identifiers are linked to your securely stored keys, but there is no mathematical relationship between them. This means your raw API keys can never be reverse-engineered from the provider slugs. + + diff --git a/virtual_key_old/product/integrations/agents.mdx b/virtual_key_old/product/integrations/agents.mdx new file mode 100644 index 00000000..94931f39 --- /dev/null +++ b/virtual_key_old/product/integrations/agents.mdx @@ -0,0 +1,4 @@ +--- +title: Agents +url: /integrations/agents +--- \ No newline at end of file diff --git a/virtual_key_old/product/integrations/ai-apps.mdx b/virtual_key_old/product/integrations/ai-apps.mdx new file mode 100644 index 00000000..bdfc568c --- /dev/null +++ b/virtual_key_old/product/integrations/ai-apps.mdx @@ -0,0 +1,4 @@ +--- +title: AI Apps +url: /integrations/ai-apps +--- \ No newline at end of file diff --git a/virtual_key_old/product/integrations/cloud.mdx b/virtual_key_old/product/integrations/cloud.mdx new file mode 100644 index 00000000..9236d6c2 --- /dev/null +++ b/virtual_key_old/product/integrations/cloud.mdx @@ -0,0 +1,4 @@ +--- +title: Cloud Providers +url: /integrations/cloud +--- \ No newline at end of file diff --git a/virtual_key_old/product/integrations/guardrails.mdx b/virtual_key_old/product/integrations/guardrails.mdx new file mode 100644 index 00000000..ec1585c9 --- /dev/null +++ b/virtual_key_old/product/integrations/guardrails.mdx @@ -0,0 +1,4 @@ +--- +title: Guardrails +url: /product/guardrails/list-of-guardrail-checks +--- diff --git a/virtual_key_old/product/integrations/libraries.mdx b/virtual_key_old/product/integrations/libraries.mdx new file mode 100644 index 00000000..0db8adf8 --- /dev/null +++ b/virtual_key_old/product/integrations/libraries.mdx @@ -0,0 +1,4 @@ +--- +title: Libraries +url: /integrations/libraries +--- \ No newline at end of file diff --git a/virtual_key_old/product/integrations/plugins.mdx b/virtual_key_old/product/integrations/plugins.mdx new file mode 100644 index 00000000..26ca513d --- /dev/null +++ b/virtual_key_old/product/integrations/plugins.mdx @@ -0,0 +1,4 @@ +--- +title: Gateway Plugins +url: /integrations/plugins +--- \ No newline at end of file diff --git a/virtual_key_old/product/integrations/tracing.mdx b/virtual_key_old/product/integrations/tracing.mdx new file mode 100644 index 00000000..efae6925 --- /dev/null +++ b/virtual_key_old/product/integrations/tracing.mdx @@ -0,0 +1,4 @@ +--- +title: Tracing +url: /integrations/tracing +--- \ No newline at end of file diff --git a/virtual_key_old/product/mcp.mdx b/virtual_key_old/product/mcp.mdx new file mode 100644 index 00000000..9807d680 --- /dev/null +++ b/virtual_key_old/product/mcp.mdx @@ -0,0 +1,4 @@ +--- +title: MCP +url: https://portkey.ai/features/mcp +--- diff --git a/virtual_key_old/product/model-catalog.mdx b/virtual_key_old/product/model-catalog.mdx new file mode 100644 index 00000000..98df55ac --- /dev/null +++ b/virtual_key_old/product/model-catalog.mdx @@ -0,0 +1,158 @@ +--- +title: Model Catalog +description: Explore and query every AI model available to your workspace, with instant code snippets for all supported providers. +--- + +The **Model Catalog** is the evolution of Virtual Keys, providing a centralized and powerful way to manage, discover, and use AI models within your workspace. It consists of two main sections: **AI Providers**, where you manage your connections, and **Models**, where you explore what you can use. + +### **How it Works: Inheritance from the Organization** + +The most significant change with the Model Catalog is the concept of inheritance. Think of it this way: + +1. Your **Organization Admin** creates a master **Integration** at the company level (e.g., for "Azure Production"). They add the credentials and can set default budgets, rate limits, and an allow-list of approved models for that integration. +2. When they provision this integration to your workspace, a corresponding **AI Provider** is automatically created in your Model Catalog. +3. This new AI Provider in your workspace *inherits* all the settings from the organization-level integration, including its credentials, model access, and spending limits. + +This "create once, provision many" approach provides central governance while giving workspaces the flexibility they need. + +--- + +### **The Model Catalog Experience by Role** + +Your experience with the Model Catalog will differ based on your role within the Portkey organization. + +#### **For Workspace Members (Developers): Discover and Build** + +As a developer, your experience is simplified and streamlined. You primarily interact with the **Models** tab, which acts as your personal "Model Garden." + +- **Discover Models:** The "Models" tab is a complete gallery of every single model you have been given access to by your admins. +- **Get Code Snippets:** Click on any model, and Portkey will generate the exact code snippet you need to start making calls, with the correct provider and model slugs already included. +- **Simplified API Calls:** You can now call any model directly using the `model` parameter, formatted as `@{provider_slug}/{model_slug}`. This lets you switch between providers and models on the fly with a single Portkey API key. + +```python +# Switch between a model on OpenAI and one on Bedrock seamlessly +client.chat.completions.create( + model="@openai-prod/gpt-4o", + messages=[...] +) + +client.chat.completions.create( + model="@bedrock-us/claude-3-sonnet-v1", + messages=[...] +) +``` + +#### **For Workspace Admins: Manage and Customize** + +As a Workspace Admin, you have more control over the providers within your workspace via the **AI Providers** tab. + +You will see a list of providers that have been inherited from the organization. From here, you have two primary options when you click **Create Provider**: + +1. **Inherit from an Org Integration:** You can create *additional* providers that are based on an existing org-level integration. This is useful for subdividing access within your team. For example, if your workspace has a $1000 budget on the main "Azure Prod" integration, you could create a new provider from it named "azure-prod-experimental" and give it a stricter $100 budget for a specific project. +2. **Create a New Workspace-Exclusive Integration:** If your Org Admin has enabled the permission, you can create a brand new integration from scratch. This provider is exclusive to your workspace and functions just like the old Virtual Keys did. + +#### **For Organization Admins: A View into Workspaces** + +While Org Admins primarily work in the main **[Integrations](/product/integrations)** dashboard, the Model Catalog provides a crucial feedback loop: + +When a Workspace Admin creates a new, workspace-exclusive integration (option #2 above), you gain full visibility. This new integration will automatically appear on your main Integrations page under the **"Workspace-Created"** tab, ensuring you always have a complete audit trail of all provider credentials being used across the organization. + +### **SDK Integration and Advanced Usage** + +While the new `model` parameter is the recommended approach for its simplicity, Portkey maintains full backward compatibility and offers flexible integration options for various SDKs. + + +Remember, the term "Virtual Key" is now synonymous with the **AI Provider slug** found in your Model Catalog. + + +#### **Using with Portkey SDK** + +You can set a default AI Provider at initialization or override it per request. + + + + +```js +import Portkey from 'portkey-ai'; + +// Set a default AI Provider for the client +const portkey = new Portkey({ + apiKey: process.env.PORTKEY_API_KEY, + virtualKey: "YOUR_AI_PROVIDER_SLUG" +// provider: "@YOUR_AI_PROVIDER_SLUG" +}); + +// Or, override it for a specific call +const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'gpt-3.5-turbo', +}, { virtualKey: "ANOTHER_AI_PROVIDER_SLUG" }); +``` + + + + +```python +from portkey_ai import Portkey + +# Set a default AI Provider for the client +portkey = Portkey( + api_key="PORTKEY_API_KEY", + virtual_key="YOUR_AI_PROVIDER_SLUG" +# provider="@YOUR_AI_PROVIDER_SLUG" +) + +# Or, override it for a specific call +completion = portkey.with_options( + virtual_key="ANOTHER_AI_PROVIDER_SLUG" +).chat.completions.create( + messages = [{ "role": 'user', "content": 'Say this is a test' }], + model = 'gpt-3.5-turbo' +) +``` + + + + +#### **Using with OpenAI SDK** + +Simply point the OpenAI client to Portkey's gateway and pass your AI Provider slug in the headers. + + + + +```python +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +client = OpenAI( + api_key="", # can be left blank + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + api_key="PORTKEY_API_KEY", + virtual_key="YOUR_AI_PROVIDER_SLUG" +# provider="@YOUR_AI_PROVIDER_SLUG" + ) +) +``` + + + + +```javascript +import OpenAI from "openai"; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'; + +const openai = new OpenAI({ + apiKey: '', // can be left blank + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "YOUR_AI_PROVIDER_SLUG" +// provider: "@YOUR_AI_PROVIDER_SLUG" + }) +}); +``` + + + \ No newline at end of file diff --git a/virtual_key_old/product/observability.mdx b/virtual_key_old/product/observability.mdx new file mode 100644 index 00000000..d7884765 --- /dev/null +++ b/virtual_key_old/product/observability.mdx @@ -0,0 +1,44 @@ +--- +title: "Observability (OpenTelemetry)" +description: "Gain real-time insights, track key metrics, and streamline debugging with our comprehensive observability suite." +--- + +If you're working with an LLM - visibility across all your requests can be a BIG pain. How do you trace and measure cost, latency, accuracy of your requests? + +Portkey's OpenTelemetry-compliant observability suite gives you complete control over all your requests. And Portkey's analytics dashboards provide the insights you're looking for. Fast. + + + + + +## Features + + + +

Portkey records all your multimodal requests and responses, making it easy to view, monitor, and debug interactions.

+
+ + +

Portkey supports request tracing to help you monitor your applications throughout the lifecycle of a request.

+
+ + +

A comprehensive view of 21+ key metrics. Use it to analyze data, spot trends, and make informed decisions.

+
+ + +

Streamline your data view with customizable filters. Zero in on data that matters most.

+
+ + +

Enrich your LLM APIs with custom metadata. Assign unique tags for swift grouping and troubleshooting.

+
+ + +

Add feedback values and weights to complete the loop.

+
+ + +

Set up budget limits for your provider API keys and gain confidence over your application's costs.

+
+
diff --git a/virtual_key_old/product/observability/analytics.mdx b/virtual_key_old/product/observability/analytics.mdx new file mode 100644 index 00000000..895927f3 --- /dev/null +++ b/virtual_key_old/product/observability/analytics.mdx @@ -0,0 +1,72 @@ +--- +title: "Analytics" +--- + + +This feature is available for all plans:- + +* [Developer](https://app.portkey.ai/): 30 days retention +* [Production](https://app.portkey.ai/): 365 days retention +* [Enterprise](https://portkey.ai/docs/product/enterprise-offering): Unlimited + + +As soon as you integrate Portkey, you can start to view detailed & real-time analytics on cost, latency and accuracy across all your LLM requests. + +The analytics dashboard provides an interactive interface to understand your LLM application Here, you can see various graphs and metrics related to requests to different LLMs, costs, latencies, tokens, user activity, feedback, cache hits, errors, and much more. + +The metrics in the Analytics section can help you understand the overall efficiency of your application, discover patterns, identify areas of optimization, and much more. + + + + + +## Charts + +The dashboard provides insights into your [users](/product/observability/analytics#users), [errors](/product/observability/analytics#errors), [cache](/product/observability/analytics#cache), [feedback](/product/observability/analytics#feedback) and also summarizes information by [metadata](/product/observability/analytics#metadata-summary). + +### Overview + +The overview tab is a 70,000ft view of your application's performance. This highlights the cost, tokens used, mean latency, requests and information on your users and top models. + +This is a good starting point to then dive deeper. + +### Users + +The users tab provides an overview of the user information associated with your Portkey requests. This data is derived from the `user` parameter in OpenAI SDK requests or the special `_user` key in the Portkey [metadata header](/product/observability/metadata). + +Portkey currently does not provide analytics on usage patterns for individual team members in your Portkey organization. The users tab is designed to track end-user behavior in your application, not internal team usage. + + + + +### Errors + +Portkey captures errors automatically for API and Accuracy errors. The charts give you a quick sense of error rates allowing you to debug further when needed. + +The dashboard also shows you the number of requests rescued by Portkey through the various AI gateway strategies. + + + + + + +### Cache + +When you enable cache through the AI gateway, you can view data on the latency improvements and cost savings due to cache. + +### Feedback + +Portkey allows you to collect feedback on LLM requests through the logs dashboard or via API. You can view analytics on this feedback collected on this dashboard. + +### Metadata Summary + +Group your request data by metadata parameters to unlock insights on usage. Select the metadata property to use in the dropdown and view the request data grouped by values of that metadata parameter. + +This lets you answer questions like: + +1. Which users are we spending the most on? +2. Which organisations have the highest latency? + + + + diff --git a/virtual_key_old/product/observability/auto-instrumentation.mdx b/virtual_key_old/product/observability/auto-instrumentation.mdx new file mode 100644 index 00000000..123b6467 --- /dev/null +++ b/virtual_key_old/product/observability/auto-instrumentation.mdx @@ -0,0 +1,40 @@ +--- +title: "Auto-Instrumentation [BETA]" +description: "Portkey's auto-instrumentation allows you to instrument tracing and logging for multiple LLM/Agent frameworks and view the logs, traces, and metrics in a single place." +--- + + + This feature is currently in beta. We're rolling out support for more frameworks and will also be exposing the otel collector endpoint to view the traces and logs. + + +## Overview + +There's two main components to auto-instrumentation: +1. The Portkey SDK, which can be used for tracing along with being used as a unified API gateway. +2. The Portkey dashboard, which can be used to view the logs, traces, and metrics. + +## Portkey SDK + +Using portkey for auto-instrumentation is fairly straightforward. A one line addition at the top of your code execution will start sending traces and logs to Portkey. + +```python +from portkey import Portkey + +Portkey(api_key="{{PORTKEY_API_KEY}}", instrumentation=True) +``` + +## Portkey Dashboard + +The Portkey dashboard can be used to view the logs, traces, and metrics. + +![Portkey dashboard auto-instrumentation image](/images/product/opentelemetry.png) + +## Supported Frameworks + +We currently support auto-instrumentation for the following frameworks: +- [CrewAI](/integrations/agents/crewai#auto-instrumentation) +- [LangGraph](/integrations/agents/langgraph#auto-instrumentation) + + + To request support for another framework, please reach out to us on [Discord here](https://portkey.ai/community). + diff --git a/virtual_key_old/product/observability/budget-limits.mdx b/virtual_key_old/product/observability/budget-limits.mdx new file mode 100644 index 00000000..be58e8e4 --- /dev/null +++ b/virtual_key_old/product/observability/budget-limits.mdx @@ -0,0 +1,4 @@ +--- +title: Budget Limits +url: /product/ai-gateway/virtual-keys/budget-limits +--- diff --git a/virtual_key_old/product/observability/feedback.mdx b/virtual_key_old/product/observability/feedback.mdx new file mode 100644 index 00000000..bae10939 --- /dev/null +++ b/virtual_key_old/product/observability/feedback.mdx @@ -0,0 +1,136 @@ +--- +title: "Feedback" +description: "Portkey's Feedback APIs provide a simple way to get weighted feedback from customers on any request you served, at any stage in your app." +--- + + + This feature is available on all Portkey plans. + + + +You can capture this feedback on a request or conversation level and analyze it by adding meta data to the relevant request. + +## Adding Feedback to Requests + +### 1\. Find the \`trace-id\` + +Portkey adds trace ids to all incoming requests. You can find this in the `x-portkey-trace-id` response header. + +To use your own trace IDs, send them as part of the request headers - [Adding a trace ID to your requests](/product/observability/traces#how-to-enable-request-tracing) + +### 2\. Add feedback + +You can append feedback to a request through the SDKs or the REST API. + + + + +```js +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY" +}); + +// Add the feedback +portkey.feedback.create({ + traceID: "your trace id", + value: 5, // Integer between -10 and 10 + weight: 1, // Optional + metadata: { + ... // Pass any additional context here like comments, _user and more + } +}) +``` + + + +```python +portkey = Portkey( + api_key="PORTKEY_API_KEY" +) + +feedback = portkey.feedback.create( + trace_id="TRACE_ID", + value=5, # Integer between -10 and 10 + weight=1, # Optional + metadata={ + # Pass any additional context here like comments, _user and more + } +) +print(feedback) +``` + + + +```sh +curl --location 'https://api.portkey.ai/v1/feedback' \ + --header 'x-portkey-api-key: YOUR_API_KEY' \ + --header 'Content-Type: application/json' \ + --data '{ + "trace_id": "YOUR_TRACE_ID", + "value": -10, + "weight": 0.5, + "metadata": { + "text": "title was irrelevant", + "_user": "fef653", + "_organisation": "o9876", + "_prompt": "test_prompt", + "_environment": "production" + } + }' +``` + + +The **Payload** takes the following keys: `traceID/trace_id, value, weight, metadata` + +| Key | Required? | Description | Type | +| ------------------- | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------ | +| trace\_id / traceID | Required | The trace id on which the feedback will be logged | string | +| value | Required | Feedback value | integer between \[-10,10\] | +| weight | Optional | Add weight value to feedback value. Helpful if you're collecting multiple feedback for a single trace | float between \[0,1\], Default = 1.0 | +| metadata | Optional | JSON string of any metadata you want to send along with the feedback.\_user, \_organisation, \_prompt and \_environment are special fields indexed by default | string | + +### Examples + +A simple & effective feedback from the user is a thumbs up or thumbs down. Just set `value` to `1` for ๐Ÿ‘ and -1 for ๐Ÿ‘Ž. `Weight` would be default `1.0`. + + + + +```js +portkey.feedback.create({ + traceID: "your trace id", + value: 1 +}) +``` + + + +```py +portkey.feedback.create( + trace_id = "your trace id", + value = 1 +) +``` + + + +```sh +curl --location 'https://api.portkey.ai/v1/feedback' \ +--header 'x-portkey-api-key: ' \ +--header 'Content-Type: application/json' \ +--data '{ + 'trace_id': 'REQUEST_TRACE_ID', + 'value': 1 +}' +``` + + + +#### Other Ideas for collecting feedback + +* Business metrics make for great feedback. If you're generating an email, the email being sent out could be a positive feedback metric. The level of editing could indicate the value. +* When a user retries a generation, store the negative feedback since something probably went wrong. Use a lower weight for this feedback since it could be circumstantial. + +### Feedback Analytics + +You can see the `Feedback Count` and `Value: Weight` pairs for each `trace-id` on the logs page. You can also view the feedback details on [Analytics](/product/observability/analytics#feedback) and on the Prompt Eval tabs. diff --git a/virtual_key_old/product/observability/filters.mdx b/virtual_key_old/product/observability/filters.mdx new file mode 100644 index 00000000..85746c0a --- /dev/null +++ b/virtual_key_old/product/observability/filters.mdx @@ -0,0 +1,41 @@ +--- +title: "Filters" +--- + +This feature is available on all Portkey plans. + + +You can filter analytics & logs by the following parameters: + +1. **Model Used**: The AI provider and the model used. +2. **Cost**: The cost of the request in cents +3. **Tokens**: The total number of tokens used in the request +4. **Status**: The API status of the response that was received from the LLM provider +5. **Meta**: The [metadata](/product/observability/metadata) properties sent to Portkey +6. **Avg Weighted Feedback**: The average weighted feedback scores calculated for the requests +7. **Virtual Key:** The virtual key that's used +8. **Config:** The Config ID that's passed to the request +9. **Trace ID:** Request trace ID +10. **Time Range**: The date and time range for the analytics & logs +10. **API Key**: The API Key used to make the request +12. **Prompt ID**: The unique request ID +13. **Cache Status**: The status of the cache for the request +14. **Workspace**: The workspace used to make the request +15. **Saved Filters**: The saved filters that you can use to quickly access your frequently used filter combinations + + + + Depending on your role in the organization, you may have access to different filters. + + + + + +## Saved Filters + +Quickly access your frequently used filter combinations with the `Saved Filters` feature. Save any set of filters directly from the search bar on the Logs or Analytics pages. Saved filters allow you to instantly apply complex filter rules without retyping them every time. + + + + +Saved filters are accessible to all organization members, who can also edit, rename, or delete them as needed. Share saved filters with teammates to streamline collaboration and ensure everyone has access to the right data views. diff --git a/virtual_key_old/product/observability/logs-export-deprecated.mdx b/virtual_key_old/product/observability/logs-export-deprecated.mdx new file mode 100644 index 00000000..5d0bf6a6 --- /dev/null +++ b/virtual_key_old/product/observability/logs-export-deprecated.mdx @@ -0,0 +1,72 @@ +--- +title: "(DEPRECATED) Logs Export" +description: "Easily access your Portkey logs data for further analysis and reporting" +--- + + + Logs export feature is only available for [**Production**](https://portkey.ai/pricing) and [**Enterprise**](https://portkey.ai/docs/product/enterprise-offering) users. + + + +Portkey offers an in-app logs export feature that is not generally available yet. If you're interested in early access, contact us at support@portkey.ai. + + +At Portkey, we understand the importance of data analysis and reporting for businesses and teams. That's why we provide a comprehensive logs export feature for our paid users. With this feature, you can easily request and obtain your Portkey logs data in a **structured JSON** format, allowing you to gain valuable insights into your LLM usage, performance, costs, and more. + +## Requesting Logs Export + +To submit a data export request, simply follow these steps: + +1. Ensure you are an admin of your organization on Portkey. +2. Send an email to [support@portkey.ai](mailto:support@portkey.ai) with the subject line `Logs Export - [Your_Organization_Name]`. +3. In the email body, + * Specify the **time frame** for which you require the logs data. **Pro plan** supports logs export of **last 30** days. + * Share names of the **specific columns** you require (see the "[Exported Data](/product/observability/logs-export#exported-data)" section below for a complete list of available columns). +4. Our team will process your request and provide you with the exported logs data in JSON format. + + + Note: Portkey only supports data exports in the `JSONL` format, and can not process exports in any other formats at the moment. + + +## Exported Data + +The exported logs data will include the following columns: + +| Column Name | Column Description / Property | +| ---------------------- | --------------------------------------------------------- | +| created\_at | Timestamp of the request | +| request.body | Request JSON payload (as seen in the Portkey logs) | +| response.body | Response JSON payload (as seen in the Portkey logs) | +| is\_success | Request success status (1 = success, 0 = failure) | +| ai\_org | AI provider name | +| ai\_model | AI model name | +| req\_units | Number of tokens in the request | +| res\_units | Number of tokens in the response | +| total\_units | Total number of tokens (request + response) | +| cost | Cost of the request in cents (USD) | +| cost\_currency | Currency of the cost (USD) | +| request\_url | Final provider API URL | +| request\_method | HTTP request method | +| response\_status\_code | HTTP response status code | +| response\_time | Response time in milliseconds | +| cache\_status | Cache status (SEMANTIC HIT, HIT, MISS, DISABLED) | +| cache\_type | Cache type (SIMPLE, SEMANTIC) | +| stream\_mode | Stream mode status (TRUE, FALSE) | +| retry\_success\_count | Number of retries after which request was successful | +| trace\_id | Trace ID for the request | +| mode | Config top level strategy (SINGLE, FALLBACK, LOADBALANCE) | +| virtual\_key | Virtual key used for the request | +| runtime | Runtime environment | +| runtime\_version | Runtime environment version | +| sdk\_version | Portkey SDK version | +| config | Config ID used for the request | +| prompt\_slug | Prompt ID used for the request | +| prompt\_version\_id | Version number of the prompt template slug | +| metadata.key | Custom metadata key | +| metadata.value | Custom metadata value | + +With this comprehensive data, you can analyze your API usage patterns, monitor performance, optimize costs, and make data-driven decisions for your business or team. + +## Support + +If you have any questions or need assistance with the logs export feature, reach out to the Portkey team at support@portkey.ai or hop on to our [Discord server](https://portkey.ai/community). diff --git a/virtual_key_old/product/observability/logs-export.mdx b/virtual_key_old/product/observability/logs-export.mdx new file mode 100644 index 00000000..6fa15b51 --- /dev/null +++ b/virtual_key_old/product/observability/logs-export.mdx @@ -0,0 +1,99 @@ +--- +title: "Logs Export" +description: "Easily access your Portkey logs data for further analysis and reporting" +--- + + + Logs export feature is only available on the enterprise plan. For more info reach us at support@portkey.ai + + +At Portkey, we understand the importance of data analysis and reporting for businesses and teams. That's why we provide a comprehensive logs export feature that allows you to download your Portkey logs data in a **structured format**, enabling you to gain valuable insights into your LLM usage, performance, costs, and more. + +## Exporting Logs In-App + +You can now export logs directly from the Portkey application by following these steps: + +1. Navigate to the **Exports** section on the main sidebar. + +2. Click the **Request Data** button in the top-right corner. + +3. Configure your export parameters: + - **Time Range**: Select the period for which you want to export logs + - **Logs Limits**: Choose maximum number of logs or set a custom offset + - **Requested Fields**: Select which data columns to include in your export + + + Export logs parameters configuration screen + + +4. After setting your parameters, click **Request Export**. + + +## Available Export Fields + +When configuring your log export, you can select from the following fields: + +| Field Name | Description | +| -------------------- | --------------------------------------------------------- | +| ID | Unique identifier for the log entry | +| Trace ID | Identifier for tracing related requests | +| Created At | Timestamp of the request | +| Request | Request JSON payload | +| Response | Response JSON payload | +| AI Provider | Name of the AI provider used | +| AI Model | Name of the AI model used | +| Request Tokens | Number of tokens in the request | +| Response Tokens | Number of tokens in the response | +| Total Tokens | Total number of tokens (request + response) | +| Cost | Cost of the request in cents (USD) | +| Cost Currency | Currency of the cost (USD) | +| Response Time | Response time in milliseconds | +| Status Code | HTTP response status code | +| Config | Config ID used for the request | +| Prompt Slug | Prompt ID used for the request | +| Metadata | Custom metadata key-value pairs | + + +5. Once your export is processed, you'll see it in the exports list with a status indicator: + - **Draft**: Export job created but not yet started + - **Success**: Export completed successfully + - **Failure**: Export job failed. Click on the `Start Again` button to retry the job. + +6. Clik on the **Start** button the dashboard to start the logs-export job + +7. For completed exports, click the **Download** button to get your logs data file. You can + + + List of exports with status and download options + + + + Currently we only support exporting 50k logs per job. For more help reach out to the Portkey team at [support@portkey.ai](mailto:support@portkey.ai) + + +## Export File Details + +Exported logs are provided in JSONL format (JSON Lines), where each line is a valid JSON object representing a single log entry. This format is ideal for data processing and analysis with tools like Python's Pandas or other data analysis frameworks. + +Each export includes: +- Up to 50,000 logs per export job (as shown in the preview panel) +- All fields selected during the export configuration +- A timestamp indicating when the export was created + +## Use Cases for Exported Logs + +With your exported logs data, you can: +- Generate custom reports for stakeholders +- Feed data into business intelligence tools +- Identify patterns in user behavior and model performance + +You can analyze your API usage patterns, monitor performance, optimize costs, and make data-driven decisions for your business or team. + +## Support + +If you have any questions or need assistance with the logs export feature, reach out to the Portkey team at [support@portkey.ai](mailto:support@portkey.ai) or join our [Discord community](https://portkey.ai/community). + + + + + diff --git a/virtual_key_old/product/observability/logs.mdx b/virtual_key_old/product/observability/logs.mdx new file mode 100644 index 00000000..b4134e0b --- /dev/null +++ b/virtual_key_old/product/observability/logs.mdx @@ -0,0 +1,207 @@ +--- +title: "Logs" +description: "The Logs section presents a chronological list of all the requests processed through Portkey." +--- + + +This feature is available for all plans: +* [Developer](https://app.portkey.ai/): 10k Logs / Month with 3 day Log Retention +* [Production](https://app.portkey.ai/): 100k Logs / Month + $9 for additional 100k with 30 Days Log Retention +* [Enterprise](https://portkey.ai/docs/product/enterprise-offering): Unlimited + + +Each log entry provides useful data such as the timestamp, request type, LLM used, tokens generated, thinking tokens and cost. For [multimodal models](/product/ai-gateway/multimodal-capabilities), Logs will also show the image sent with vision/image models, as well as the image generated. + +By clicking on an entry, a side panel opens up, revealing the entire raw data with the request and response objects. + +This detailed log can be invaluable when troubleshooting issues or understanding specific interactions. It provides full transparency into each request and response, enabling you to see exactly what data was sent and received. + + + + +## Share Logs with Teammates + +Each log on Portkey has a unique URL. You can copy the link from the address bar and directly share it with anyone in your org. + +## Request Status Guide + +The Status column on the Logs page gives you a snapshot of the gateway activity for every request. + +Portkeyโ€™s gateway featuresโ€”[Cache](/product/ai-gateway/cache-simple-and-semantic), [Retries](/product/ai-gateway/automatic-retries), [Fallback](/product/ai-gateway/fallbacks), [Loadbalance](/product/ai-gateway/load-balancing), [Conditional Routing](/product/ai-gateway/conditional-routing)โ€”are all tracked here with their exact states (`disabled`, `triggered`, etc.), making it a breeze to monitor and optimize your usage. + +**Common Queries Answered:** + +* **Is the cache working?**: Enabled caching but unsure if it's active? The Status column will confirm it for you. +* **How many retries happened?**: Curious about the retry count for a successful request? See it in a glance. +* **Fallback and Loadbalance**: Want to know if load balance is active or which fallback option was triggered? See it in a glance. + + + + + +| Option | ๐Ÿ”ด Inactive State | ๐ŸŸข Possible Active States | +| --------------- | --------------------- | ------------------------------------------------------- | +| **Cache** | Cache Disabled | Cache Miss,Cache Refreshed,Cache Hit,Cache Semantic Hit | +| **Retry** | Retry Not Triggered | Retry Success on {x} Tries,Retry Failed | +| **Fallback** | Fallback Disabled | Fallback Active | +| **Loadbalance** | Loadbalancer Disabled | Loadbalancer Active | + +## Manual Feedback + +As you're viewing logs, you can also add manual feedback on the logs to be analysed and filtered later. This data can be viewed on the [feedback analytics dashboards](/product/observability/analytics#feedback). + + + + + +## Configs & Prompt IDs in Logs + +If your request has an attached [Config](/product/ai-gateway/configs) or if it's originating from a [prompt template](/product/prompt-library), you can see the relevant Config or Prompt IDs separately in the log's details on Portkey. And to dig deeper, you can just click on the IDs and Portkey will take you to the respective Config or Prompt playground where you can view the full details. + + + + +## Debug Requests with Log Replay + +You can rerun any buggy request with just one click, straight from the log details page. The `Replay` button opens your request in a fresh prompt playground where you can rerun the request and edit it right there until it works. + + + + + + + + `Replay` **button will be inactive for a log in the following cases:** + +1. If the request is sent to any endpoint other than `/chat/completions,` `/completions`, `/embeddings` +2. If the virtual key used in the log is archived on Portkey +3. If the request originates from a prompt template which is called from inside a Config target + + +## DO NOT TRACK + +The `DO NOT TRACK` option allows you to process requests without logging the request and response data. When enabled, only high-level statistics like **tokens** used, **cost**, and **latency** will be recorded, while the actual request and response content will be omitted from the logs. + +This feature is particularly useful when dealing with sensitive data or complying with data privacy regulations. It ensures that you can still capture critical operational metrics without storing potentially sensitive information in your logs. + +To enable `DO NOT TRACK` for a specific request, set the `debug` flag to `false` when instantiating your **Portkey** or **OpenAI** client, or include the `x-portkey-debug:false` header with your request. + + + + +```js +import Portkey from 'portkey-ai'; + +const portkey = new Portkey({ + virtualKey: "OPENAI_VIRTUAL_KEY", + apiKey: "PORTKEY_API_KEY", + debug: false +}) + +async function main(){ + const response = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: '1729' }], + model: 'gpt-4', + }); + console.log(response.choices[0].message?.content) +} + +main() +``` + + +```Python +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY", + virtual_key="OPENAI_VIRTUAL_KEY", + debug=False +) + +response = portkey.chat.completions.create( + messages=[{'role': 'user', 'content': 'Say this is a test'}], + model='gpt-4' +) + +print(response.choices[0].message.content) +``` + + +```sh +curl 'https://api.portkey.ai/v1/chat/completions' \ + -H 'Content-Type: application/json' \ + -H 'x-portkey-virtual-key: $OPENAI_VIRTUAL_KEY' \ + -H 'x-portkey-api-key: $PORTKEY_API_KEY' \ + -H 'x-portkey-debug: false' \ + -d '{ + "model": "gpt-4o", + "messages": [ + { + "role": "system", + "content": "You are a helpful assistant" + }, + { + "role": "user", + "content": "what is a portkey?" + } + ] +}' +``` + + + +```py +from openai import OpenAI +from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders + +client = OpenAI( + base_url=PORTKEY_GATEWAY_URL, + default_headers=createHeaders( + virtual_key="OPENAI_VIRTUAL_KEY", + api_key="PORTKEY_API_KEY", + debug=False + ) +) + +chat_complete = client.chat.completions.create( + model="gpt-4", + messages=[{"role": "user", "content": "Say this is a test"}], +) + +print(chat_complete.choices[0].message.content) +``` + + + +```js +import OpenAI from 'openai'; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + virtualKey: "OPENAI_VIRTUAL_KEY", + apiKey: "PORTKEY_API_KEY", + debug: false + }) +}); + +async function main() { + const chatCompletion = await openai.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'gpt-3.5-turbo', + }); + console.log(chatCompletion.choices); +} + +main(); +``` + + + +### Side-by-side comparison on how a `debug:false` request will be logged + + + + diff --git a/virtual_key_old/product/observability/metadata.mdx b/virtual_key_old/product/observability/metadata.mdx new file mode 100644 index 00000000..8fa2b5fa --- /dev/null +++ b/virtual_key_old/product/observability/metadata.mdx @@ -0,0 +1,208 @@ +--- +title: Metadata +description: Add custom context to your AI requests for better observability and analytics +--- + + +This feature is available on all Portkey plans. + + +## What is Metadata? + +Metadata in Portkey allows you to attach custom contextual information to your AI requests. Think of it as tagging your requests with important business context that helps you: + +- **Track usage** across different users, environments, or features +- **Filter logs** to isolate specific request types +- **Analyze patterns** in how your AI is being used +- **Audit activities** for compliance and security + +```python +# Example metadata +{ + "_user": "user-123", # Who made this request? + "environment": "prod", # Where was it made from? + "feature": "chat-assist", # What feature was using AI? + "request_id": "42aff12" # Your internal tracking ID +} +``` + +## Quick Implementation + + + +```python +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY", + virtual_key="OPENAI_VIRTUAL_KEY" +) + +# Add metadata to track context +response = portkey.with_options( + metadata={ + "_user": "user-123", + "environment": "production", + "feature": "summarization", + "request_id": "1729" + } +).chat.completions.create( + messages=[{"role": "user", "content": "Summarize this article"}], + model="gpt-4" +) +``` + + +```javascript +import {Portkey} from 'portkey-ai' + +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "OPENAI_VIRTUAL_KEY" +}) + +// Request with business context metadata +const completion = await portkey.chat.completions.create( + { + messages: [{ role: 'user', content: 'Summarize this article' }], + model: 'gpt-4', + }, + { + metadata: { + "_user": "user-123", + "environment": "production", + "feature": "summarization", + "request_id": "1729" + } + } +); +``` + + +```javascript +// Using with OpenAI SDK +import OpenAI from 'openai'; +import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' + +const openai = new OpenAI({ + baseURL: PORTKEY_GATEWAY_URL, + defaultHeaders: createHeaders({ + apiKey: "PORTKEY_API_KEY", + metadata: { + "_user": "user-123", + "feature": "customer-support" + } + }) +}); + +const completion = await openai.chat.completions.create({ + messages: [{ role: 'user', content: 'Help with my order' }], + model: 'gpt-4', +}); +``` + + +```bash +curl https://api.portkey.ai/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-virtual-key: $OPENAI_VIRTUAL_KEY" \ + -H "x-portkey-metadata: {\"_user\":\"user-123\",\"feature\":\"search\"}" \ + -d '{ + "model": "gpt-4", + "messages": [{"role": "user","content": "Find relevant docs"}] + }' +``` + + + +## Common Use Cases + + + + Track which users are using AI features by adding the `_user` identifier in metadata + + + Attribute AI costs to teams, features or products by adding identifiers in metadata + + + Differentiate between dev/staging/prod usage with the `environment` metadata key + + + See which product features are using AI most heavily with feature identifiers + + + +## Metadata Keys and Values + +- You can send **any number** of metadata keys with each request +- All values must be **strings** with a maximum length of **128** characters +- Keys can be any string, but some have special meaning (like `_user`) + +### Special Metadata Keys + +| Key | Purpose | Notes | +|-----|---------|-------| +| `_user` | User tracking | Powers user-level analytics in the Portkey dashboard | + + +**About the `_user` key:** If you pass a `user` field in your OpenAI request body, we'll automatically copy it to the `_user` metadata key. If both exist, the explicit `_user` metadata value takes precedence. + + +## Where to See Your Metadata + +### Analytics Dashboard + +Analytics dashboard has a dedicated Tab to view aggregate stats on all your metadat keys: + + Analytics with metadata filters + + + + +### Request Logs + +You can also apply any metadata filters to the logs or analytics and filter data by any metadata key you've used: + + + + + +## Enterprise Features + +For enterprise users, Portkey offers advanced metadata governance and lets you define metadata at multiple levels: + +1. **Request level** - Applied to a single request +2. **API key level** - Applied to all requests using that key +3. **Workspace level** - Applied to all requests in a workspace + + + Define mandatory metadata fields that must be included with all requests + + +When the same key appears at multiple levels, the **precedence order** is: + +1. Workspace metadata (highest priority) +2. API key metadata +3. Request metadata (lowest priority) + + + Please note that before 1.10.20, the precedence order was: + 1. Request metadata (highest priority) + 2. API key metadata + 3. Workspace metadata (lowest priority) + + +## Best Practices + +- Use **consistent keys** across your organization +- Create **naming conventions** for metadata keys +- Consider adding these common fields: + - `_user`: Who initiated this request + - `environment`: Which environment (dev/staging/prod) + - `feature` or `component`: Which part of your product + - `version`: API or app version + - `session_id`: To group related requests + - `request_id`: Your internal tracking ID + +- For proper tracking, **always include the `_user` field** when the request is on behalf of an end-user diff --git a/virtual_key_old/product/observability/traces.mdx b/virtual_key_old/product/observability/traces.mdx new file mode 100644 index 00000000..3e65cd03 --- /dev/null +++ b/virtual_key_old/product/observability/traces.mdx @@ -0,0 +1,430 @@ +--- +title: "Tracing" +description: "The **Tracing** capabilities in Portkey empowers you to monitor the lifecycle of your LLM requests in a unified, chronological view." +--- + + +This feature is available for all plans:- + +* [Developer](https://app.portkey.ai/): 10k Logs / Month with 3 day Log Retention +* [Production](https://app.portkey.ai/): 100k Logs / Month + $9 for additional 100k with 30 Days Log Retention +* [Enterprise](https://portkey.ai/docs/product/enterprise-offering): Unlimited + + +This is perfect for **agentic workflows**, **chatbots**, or **multi-step LLM calls**, by helping you understand and optimize your AI application's performance. + + + + +## How Tracing Works + +Portkey implements OpenTelemetry-compliant tracing. When you include a `trace ID` with your requests, all related LLM calls are grouped together in the Traces View, appearing as "spans" within that trace. + +> "Span" is another word for subgrouping of LLM calls. Based on how you instrument, it can refer to another group within your trace or to a single LLM call. + +## Trace Tree Structure + +Portkey uses a tree data structure for tracing, **similar to OTel.** + +Each node in the tree is a span with a unique `spanId` and optional `spanName`. Child spans link to a parent via the `parentSpanId`. Parentless spans become root nodes. + +``` +traceId +โ”œโ”€ parentSpanId +โ”‚ โ”œโ”€ spanId +โ”‚ โ”œโ”€ spanName +``` + +| Key - Node | Key - Python | Expected Value | Required? | +| ------------ | ---------------- | -------------- | --------- | +| traceId | trace\_id | Unique string | YES | +| spanId | span\_id | Unique string | NO | +| spanName | span\_name | string | NO | +| parentSpanId | parent\_span\_id | Unique string | NO | + +--- + +## Enabling Tracing + +You can enable tracing by passing the `trace tree` values while making your request (or while instantiating your client). + +Based on these values, Portkey will instrument your requests, and will show the exact trace with its spans on the "Traces" view in Logs page. + + + + **Add tracing details to a single request (recommended)** + +```js +const requestOptions = { + traceId: "1729", + spanId: "11", + spanName: "LLM Call" +} + +const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'gpt-4o', +}, requestOptions); +``` + +#### Or, add trace details while instantiating your client + +```js +import Portkey from 'portkey-ai'; + +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "VIRTUAL_KEY", + traceId: "1729", + spanId: "11", + spanName: "LLM Call" +}) +``` + + + +```python +completion = portkey.with_options( + trace_id="1729", + span_id="11", + span_name="LLM Call" +).chat.completions.create( + messages = [{ "role": 'user', "content": 'Say this is a test' }], + model = 'gpt-3.5-turbo' +) +``` + +#### Pass Trace details while instantiating your client + +```py +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY", + virtual_key="VIRTUAL_KEY", + trace_id="1729", + span_id="11", + span_name="LLM Call" +) +``` + + + + +```js +import { createHeaders } from 'portkey-ai' + +const requestOptions = { + traceId: "1729", + spanId: "11", + spanName: "LLM Call" +} + +const chatCompletion = await openai.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'gpt-3.5-turbo', +}, requestOptions); +``` + + + + ```py + from portkey_ai import createHeaders + + req_headers = createHeaders( + trace_id="1729", + span_id="11", + span_name="LLM Call + ) + + chat_complete = client.with_options(headers=req_headers).chat.completions.create( + model="gpt-4", + messages=[{"role": "user", "content": "Say this is a test"}], + ) +``` + + + +```sh +curl https://api.portkey.ai/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-provider: openai" \ + -H "x-portkey-trace-id: 1729"\ + -H "x-portkey-span-id: 11"\ + -H "x-portkey-span-name: LLM_CALL"\ + -d '{ + "model": "gpt-4o", + "messages": [{"role": "user","content": "Hello!"}] + }' +``` + + + + + +If you are only passing trace ID and not the span details, you can set the trace ID while making your request or while instantiating your client. + + + + + + + ```js +const requestOptions = {traceID: "YOUR_TRACE_ID"} + +const chatCompletion = await portkey.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'gpt-4o', +}, requestOptions); + +console.log(chatCompletion.choices); +``` + +#### Pass Trace ID while instantiating your client + +```js +import Portkey from 'portkey-ai'; + +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY", + virtualKey: "VIRTUAL_KEY", + traceID: "TRACE_ID" +}) +``` + + + +```python +completion = portkey.with_options( + trace_id = "TRACE_ID" +).chat.completions.create( + messages = [{ "role": 'user', "content": 'Say this is a test' }], + model = 'gpt-3.5-turbo' +) +``` + +#### Pass Trace ID while instantiating your client + +```py +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY", + virtual_key="VIRTUAL_KEY", + trace_id="TRACE_ID" +) +``` + + + +```js +import { createHeaders } from 'portkey-ai' + +const reqHeaders = {headers: createHeaders({"traceID": "TRACE_ID"})} + +const chatCompletion = await openai.chat.completions.create({ + messages: [{ role: 'user', content: 'Say this is a test' }], + model: 'gpt-3.5-turbo', +}, reqHeaders); +``` + + + +```py +from portkey_ai import createHeaders + +req_headers = createHeaders(trace_id="TRACE_ID") + +chat_complete = client.with_options(headers=req_headers).chat.completions.create( + model="gpt-4", + messages=[{"role": "user", "content": "Say this is a test"}], +) +``` + + + +```sh +curl https://api.portkey.ai/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -H "x-portkey-provider: openai" \ + -H "x-portkey-trace-id: TRACE_ID" \ + -d '{ + "model": "gpt-4-turbo", + "messages": [{ + "role": "system", + "content": "You are a helpful assistant." + },{ + "role": "user", + "content": "Hello!" + }] + }' +``` + + + + +## See Tracing in Action + + + +## Tracing in Langchain + +Portkey has a dedicated handler that can instrument your Langchain chains and agents to trace them. + + + +1. First, install Portkey SDK, and Langchain's packages + +```sh +$ pip install langchain_OpenAI portkey-ai langchain_community +``` + +2. Import the packages + +```py +from langchain_openai import ChatOpenAI +from langchain.chains import LLMChain +from portkey_ai.langchain import LangchainCallbackHandler +from portkey_ai import createHeaders +``` + +3. Instantiate Portkey's Langchain Callback Handler + +```py +portkey_handler = LangchainCallbackHandler( + api_key="YOUR_PORTKEY_API_KEY", + metadata={ + "user_name": "User_Name", + "traceId": "Langchain_sample_callback_handler" + } +) +``` + +4. Add the callback to the `ChatOpenAI` instance + +```py +llm = ChatOpenAI( + api_key="OPENAI_API_KEY", + callbacks=[portkey_handler], +) +``` + +5. Also add the callback when you define or run your LLM chain + +```py +chain = LLMChain( + llm=llm, + prompt=prompt, + callbacks=[portkey_handler] +) + +handler_config = {'callbacks' : [portkey_handler]} + +chain.invoke({"input": "what is langchain?"}, config=handler_config) +``` + + + +--- + +## Tracing Llamaindex Requests + +Portkey has a dedicated handler to instrument your Llamaindex requests on Portkey. + + + + +1. First, install Portkey SDK, and LlamaIndex packages + +```sh +$ pip install openai portkey-ai llama-index +``` + +2. Import the packages + +```py +from llama_index.llms.openai import OpenAI +from portkey_ai.llamaindex import LlamaIndexCallbackHandler +``` + +3. Instantiate Portkey's LlamaIndex Callback Handler + +```py +portkey_handler = LlamaIndexCallbackHandler( + api_key="PORTKEY_API_KEY", + metadata={ + "user_name": "User_Name", + "traceId": "Llamaindex_sample_callback_handler" + } +) +``` + +4. Add it to `OpenAI` llm class + +```py +llm = OpenAI( + model="gpt-4o", + api_key="OPENAI_API_KEY", + callback_manager=[portkey_handler], +) +``` + +5. In Llama Index, you can also set the callback at a global level + +```python +from llama_index.core import Settings +from llama_index.core.callbacks import CallbackManager + +Settings.callback_manager = CallbackManager([portkey_handler]) +Settings.llm = llm +``` + + + +--- + +## Inserting Logs + +If you are using the [Insert Log API](/portkey-endpoints/logs/insert-a-log) to add logs to Portkey, your `traceId`, `spanId` etc. will become part of the metadata object in your log, and Portkey will instrument your requests to take those values into account. + +The logger endpoint supports inserting a single log as well as log array, and helps you build traces of any depth or complexity. For more, check here: + + +--- + +## Tracing for Gateway Features + +Tracing also works very well to capture the Gateway behavior on retries, fallbacks, and other routing mechanisms on Portkey Gateway. + +Portkey automatically groups all the requests that were part of a single fallback or retry config and shows the failed and succeeded requests chronologically as "spans" inside a "trace". + +This is especially useful when you want to understand the total latency and behavior of your app when retry or fallbacks were triggered. + +For more, check out the [Fallback](/product/ai-gateway/fallbacks) & [Automatic Retries](/product/ai-gateway/automatic-retries) docs. + +--- + +## Why Use Tracing? + +* **Cost Insights**: View aggregate LLM costs at the trace level. +* **Debugging**: Easily browse all requests in a single trace and identify failures. +* **Performance Analysis**: Understand your entire request lifecycle and total trace duration. +* **User Feedback Integration**: Link user feedback to specific traces for targeted improvements. + +--- + +## Capturing User Feedback + +Trace IDs can also be used to link user feedback to specific generations. This can be used in a system where users provide feedback, like a thumbs up or thumbs down, or something more complex via our feedback APIs. This feedback can be linked to traces which can span over a single generation or multiple ones. Read more here: + + diff --git a/virtual_key_old/product/open-source.mdx b/virtual_key_old/product/open-source.mdx new file mode 100644 index 00000000..658543d3 --- /dev/null +++ b/virtual_key_old/product/open-source.mdx @@ -0,0 +1,54 @@ +--- +title: "Open Source" +--- + +## [Portkey AI Gateway](https://github.com/portkey-ai/rubeus) + +We have open sourced our battle-tested AI Gateway to the community - it connects to 250+ LLMs with a unified interface and a single endpoint, and lets you effortlessly setup fallbacks, load balancing, retries, and more. + +This gateway is in production at Portkey processing billions of tokens every day. + +#### [Contribute here](https://github.com/portkey-ai/rubeus). + +--- + +## [AI Grants Finder](https://grantsfinder.portkey.ai/) + +Community resource for AI builders to find `GPU credits`, `grants`, `AI accelerators`, or `investments` \- all in a single place. Continuously updated, and sometimes also featuring [exclusive deals](https://twitter.com/PortkeyAI/status/1692463628514156859). + +Access the data [here](https://airtable.com/appUjtBcdLQIgusqW/shrAU1e4M5twTmRal). + +--- + +## [Gateway Reports](https://portkey.ai/blog/tag/benchmarks/) + +We collaborate with the community to dive deep into how the LLMs & their inference providers are performing at scale, and publish gateway reports. We track latencies, uptime, cost changes, fluctuations across various modalitites like time-of-day, regions, token-lengths, and more. + +#### [2025 AI Infrastructure Benchmark Report](https://portkey.ai/llms-in-prod-25) + + + + + +Insights from analyzing 2 trillion+ tokens, across 90+ regions and 650+ teams in production. The report contains: +- Trends shaping AI adoption and LLM provider growth. +- Benchmarks to optimize speed, cost and reliability. +- Strategies to scale production-grade AI systems. + + + +--- + +#### [GPT-4 is getting faster](https://portkey.ai/blog/gpt-4-is-getting-faster/) + + + + + +--- + +## Collaborations + +Portkey supports various open source projects with additional production capabilities through its custom integrations. + + diff --git a/virtual_key_old/product/product-feature-comparison.mdx b/virtual_key_old/product/product-feature-comparison.mdx new file mode 100644 index 00000000..ded1fd03 --- /dev/null +++ b/virtual_key_old/product/product-feature-comparison.mdx @@ -0,0 +1,346 @@ +--- +title: "Feature Comparison" +description: Comparing Portkey's Open-source version and Dev, Pro, Enterprise plans. +mode: "wide" +--- + +Portkey has a generous free tier (10k requests/month) on our **Dev** plan โ€” but, as you move to production-scale, you may benefit from Portkey's **Pro** or **Enterprise** plans. + + + + Trust Portkey for democratizing and productionizing Gen AI + + + Processed on Portkey so far + + + Ensuring your AI services are always available + + + The most popular and performant AI Gateway in the market + + + +## Why Enterprises Choose Portkey + +Enterprise customers leverage Portkey to **observe**, **govern**, and **optimize** their Gen AI services at scale across the entire org. Our Enterprise plan offers advanced security configurations, dedicated support, and customized infrastructure designed for high-volume production deployments. + + + + โ–ช๏ธ Processing millions of requests + + โ–ช๏ธ Need Five 9s reliability + + โ–ช๏ธ Routing to private LLMs + + โ–ช๏ธ Building for multiple partner teams and departments + + + โ–ช๏ธ SOC2, ISO27001, GDPR, HIPAA + + โ–ช๏ธ VPC/Airgapped deployment + + โ–ช๏ธ PII anonymization + + โ–ช๏ธ Advanced access controls + + + +Here's a detailed comparison of our plans to help you choose the right solution for your needs: + + + + + For developers needing complete control over AI infrastructure on their own servers. + + **Ideal for:** + - Technical teams with privacy requirements + - Self-hosted AI applications + - Local development & experimentation + + + + Free starter plan with basic observability and key management features. + + **Ideal for:** + - Solo developers building POCs + - Startups in early development + - Testing Portkey capabilities + + + + For growing teams with advanced caching, alerts, and access control needs. + + **Ideal for:** + - Startups scaling AI applications + - SaaS products integrating AI + - Teams needing collaboration features + + + + Enterprise-grade security, compliance, and scalability with flexible deployment. + + **Ideal for:** + - Organizations with compliance needs + - Handling sensitive data + - Custom infrastructure requirements + - Multiple deployment options (SaaS, Hybrid, Airgapped) + + + + +|

Product / Plan

| | | | | +|:--------------------------------------------------- | :---------------------------------------------------- | :---------------------------------------- | :----------------------------------------- | :------------------------------------------ | +| Get Started | | | | | +| Requests per Month | No Limit | 10K | 100K | Custom | +| Overage | - | No Overage Allowed | $9/Month
for Every 100K
Up to 3M Requests | Custom Pricing | +|

Observability

| | | | | +| Logs | | | | | +| Traces | | | | | +| Feedback | | | | | +| Custom Metadata | | | | | +| Filters | | | | | +| Alerts | | | | | +| FinOps + Executive Dashboard | | | | | +| Retention Period | | 3 Days | 30 Days | Custom | +|

AI Gateway

| | | | | +| Universal API | | | | | +| Automatic Fallbacks | | | | | +| Loadbalancing | | | | | +| Conditional Routing | | | | | +| Automatic Retries | | | | | +| Request Timeouts | | | | | +| Config Management | | | | | +| Virtual Keys & Key Management | | | | (with Budgeting, Rate Limiting support) | +| Simple Caching | | 1 Day TTL SUport | Unlimited TTL Stream from Cache | Unlimited TTL Stream from Cache | +| Semantic Caching | | | Unlimited TTL Stream from Cache | Unlimited TTL Stream from Cache | +| Unified Construct for Fine-Tuning, Files, Batche APIs | | | | | +| Support for AWS, GCP, Azure Private LLM Deployments | | | | | +|

Prompt Management

| | | | | +| Prompt Templates | | Upto 3 Templates | Unlimited | Unlimited | +| Playground | | | | | +| API Deployment | | | | | +| Versioning | | | | | +| Variable Management | | | | | +| Prompt Partials | | | | | +| Side-by-Side Comparison | | | | | +| User Access Control | | | | | +|

Guardrails

| | | | | +| Deterministic Guardrails | | | | | +| Partner Guardrails (LLM or Non LLM based) | | | | | +| Portkey LLM Guardrails with PII / PHI Redaction | | | | | +|

Autonomous Fine-Tuning

| | | | | +| Continuous Improvement | | | | | +|

Security & Compliance

| | | | | +| Role Based Access Control | | | | (Advanced) | +| Team Management | | | | (Advanced) | +| Audit Logs | | | | | +| Admin APIs (Control Plane & Data Plane) | | | | | +| SCIM Provisioning | | | | | +| JWT-based Authentication | | | | | +| Bring Your Own Key for Encryption | | | | | +| Enforce Org-level Metadata Reporting | | | | | +| Enforce Org-level LLM Guardrails | | | | | +| SSO with Okta Auth | | | | | +| SOC2, ISO27001, GDPR, HIPAA Compliance Certificates | | | | | +| BAA Signing for Compliances | | | | | +| VPC Managed Hosting | | | | | +| Private Tenancy | | | | | +| Configurable Retention Periods | | | | | +| Configurable exports to datalakes | | | | | +| Org Management | | | | | + + +## Enterprise Deployment Options + +Portkey offers a range of deployment options designed to meet the diverse security, compliance, and operational requirements of enterprise organizations. + + + +
+
+ ### Portkey-Managed Enterprise SaaS + + A fully-managed solution on Portkey's secure cloud infrastructure with enterprise-grade features and dedicated resources. + + **Ideal for:** Organizations seeking enterprise capabilities without the operational overhead of self-hosting. + + **Key Features:** + - Isolated cluster exclusively for your organization's data + - Dedicated infrastructure for optimal performance + - Complete suite of enterprise features (RBAC, SSO, audit logs) + - Guaranteed SLAs with priority support + - SOC2, ISO27001, GDPR, and HIPAA compliant +
+
+ + **Benefits:** + - Faster implementation timeline (1-2 weeks) + - No infrastructure management required + - Automatic updates and scaling + - Reduced operational costs + - Predictable pricing model + +
+
+
+ + +
+
+ ### Hybrid Deployment + + A balanced approach where the AI Gateway and data plane run in your environment while Portkey manages the control plane. + + **Ideal for:** Organizations with strict data residency requirements who want operational simplicity. + + **Key Features:** + - AI Gateway deployed in your VPC/environment + - All sensitive LLM data stays within your infrastructure + - Control plane hosted by Portkey for management + - Significantly reduced latency for API calls + - End-to-end encryption between components +
+
+ + + +
+
+
+ + +
+
+ ### Fully Airgapped Deployment + + Complete control with all components (Data plane, Control plane, and AI Gateway) deployed within your infrastructure. + + **Ideal for:** Organizations in highly regulated industries with stringent security requirements. + + **Key Features:** + - 100% of components run in your environment + - Zero data leaves your network, including metrics + - Complete network isolation possible + - Compatible with air-gapped environments + - Custom support structure tailored to your security protocols +
+
+ + **Use Cases:** + - Financial institutions handling sensitive financial data + - Healthcare organizations with strict PHI requirements + - Government agencies with classified information + - Defense contractors working with sensitive IP + - Organizations in regions with strict data sovereignty laws + +
+
+
+
+ +--- + +All enterprise deployment options include comprehensive security features such as SOC2, ISO27001, GDPR, and HIPAA compliance certifications, PII anonymization, custom data retention policies, and encryption at rest and in transit. + + + **Schedule a 30-minute consultation** with our solutions team to discuss your specific requirements and see a live demo. + + [Book Your Consultation](https://calendly.com/portkey-ai/quick-meeting) + + +## Enterprise Implementation Journey + + + + 30-minute discussion to understand your use case and requirements + + + Call with the Portkey engineering team to demo exact requirements & understand deployment options + + + Our team configures the optimal deployment model for your needs + + + Most enterprise customers are fully operational within 3-4 weeks + + + + + Our enterprise team can help assess your requirements and recommend the optimal deployment strategy for your organization's specific needs. + + Book a Consultation here. + + +## Interested? Schedule a Call Below + + + +## Frequently Asked Questions + + + + Portkey Enterprise offers three deployment options: + + 1. **Portkey-Managed SaaS**: A fully-managed solution where your data is hosted on Portkey's secure infrastructure with an isolated cluster exclusively for your organization. + + 2. **Hybrid Deployment**: The AI Gateway and data plane run in your own environment while Portkey manages the control plane, ensuring all sensitive LLM data stays within your infrastructure. + + 3. **Fully Airgapped Deployment**: All components (Data plane, Control plane, and AI Gateway) are deployed within your infrastructure with zero data leaving your network. + + Each option is designed to meet different security, compliance, and operational requirements. Our enterprise team can help you determine which option is best for your specific needs. + + + + Most Enterprise customers can be fully onboarded within 1-2 weeks, depending on specific requirements. Our dedicated implementation team will work with you to ensure a smooth transition. For complex deployments or those requiring special compliance measures, the timeline may extend to 3-4 weeks. + + + + Portkey offers a comprehensive migration service for Enterprise customers, including API mapping, configuration setup, and verification testing. Our team will work with you to ensure minimal disruption during the transition. We provide detailed documentation and support throughout the migration process. + + + + Enterprise customers have access to all Portkey integration capabilities, including custom API integrations, webhooks, SDKs for major programming languages, and specialized connectors for enterprise systems. We also offer custom integration development for specific needs. + + + + Enterprise customers receive priority 24/7 support with guaranteed response times (typically under 1 hour), a dedicated customer success manager, regular performance reviews, and direct access to our engineering team when needed. We also provide customized SLAs based on your specific requirements. + + + + Portkey's Enterprise plan includes comprehensive security features such as SOC2, ISO27001, GDPR, and HIPAA compliance certifications. We offer PII anonymization, custom data retention policies, VPC deployment options, and BAA signing for healthcare organizations. All data is encrypted in transit and at rest. + + + + Yes, Enterprise customers can deploy Portkey in their own AWS, GCP, or Azure environments, with options for VPC peering, private network connectivity, and dedicated infrastructure. Our team will work with your cloud operations staff to ensure optimal deployment. + + \ No newline at end of file diff --git a/virtual_key_old/product/prompt-engineering-studio.mdx b/virtual_key_old/product/prompt-engineering-studio.mdx new file mode 100644 index 00000000..e729ea26 --- /dev/null +++ b/virtual_key_old/product/prompt-engineering-studio.mdx @@ -0,0 +1,66 @@ +--- +title: "Prompt Engineering Studio" +--- + +Effective prompt management is crucial for getting the most out of Large Language Models (LLMs). Portkey provides a comprehensive solution for creating, managing, versioning, and deploying prompts across your AI applications. + +Portkey's Prompt Engineering Studio offers a robust ecosystem of tools to streamline your prompt engineering workflow: + +- **Create and compare prompts** in the interactive Multimodal Playground +- **Version** your prompts for production use +- **Deploy** optimized prompts via simple API endpoints +- **Monitor performance** with built-in observability +- **Collaborate** with your team through shared prompt library + +Whether you're experimenting with different prompts or managing them at scale in production, Prompt Engineering Studio provides the tools you need to build production ready AI applications. + + +You can easily access Prompt Engineering Studio using [https://prompt.new](https://prompt.new) + + +## Setting Up AI Providers + +Before you can create and manage prompts, you'll need to set up your [Virtual Keys](/product/ai-gateway/virtual-keys). After configuring your keys, the respective AI providers become available for running and managing prompts. + +Portkey supports over 1600+ models across all the major providers including OpenAI, Anthropic, Google, and many others. This allows you to build and test prompts across multiple models and providers from a single interface. + +## [Prompt Playground & Templates](/product/prompt-engineering-studio/prompt-playground) + +The [Prompt Playground](/product/prompt-engineering-studio/prompt-playground) is a complete Prompt Engineering IDE for crafting and testing prompts. It provides a rich set of features: + +- **Run on any LLM**: Test your prompts across different models and providers to find the best fit for your use case +- **Multimodal support**: Input and analyze images alongside text in your prompts +- **Side-by-side comparisons**: Compare responses across 1600+ models or prompts in parallel +- **Tool integration**: Add and test custom [tools](/product/prompt-engineering-studio/tool-library) for more powerful interactions +- **Prompt templates**: Create dynamic prompts that can change based on the variables passed in +- **AI-assisted improvements**: Leverage AI to refine your prompts + +The playground provides immediate feedback, allowing you to rapidly iterate on your prompt designs before deploying them to production. Once you're satisfied with a prompt, you can save it to the [prompt library](/product/prompt-engineering-studio/prompt-library) and use it in your code simply. + +## [Prompt Versioning](/product/prompt-engineering-studio/prompt-versioning) +Prompt versioning allows you to maintain a history of your prompt changes and promote stable versions to production. +Any update on the saved prompt will create a new version. You can switch back to an older version anytime. + +Versioning ensures you can safely experiment while maintaining stable prompts in production. + +## [Prompt Library](/product/prompt-engineering-studio/prompt-library) +The Prompt Library is your central repository for managing all prompts across your organization. Within the library, you can organize prompts in folders, set access controls, and collaborate with team members. The library makes it easy to maintain a consistent prompt strategy across your applications and teams. + +## [Prompt Partials](/product/prompt-engineering-studio/prompt-partial) +Prompt Partials allow you to create reusable components that can be shared across multiple prompts. These are especially useful for standard instructions or context that appears in multiple prompts. Partials help reduce duplication and maintain consistency in your prompt library. + +## [Prompt Observability](/product/prompt-engineering-studio/prompt-observability) +Prompt Observability provides insights into how your prompts are performing in production through usage logs, performance metrics, and version comparison. These insights help you continuously improve your prompts based on real-world usage. + +## [Prompt API](/product/prompt-engineering-studio/prompt-api) +The Prompt API allows you to integrate your saved prompts directly into your applications through Completions and Render endpoints. The API makes it simple to use your optimized prompts in production applications, with CRUD operations coming soon. + +## Additional Resources + +Explore these additional features to get the most out of Portkey's Prompt Engineering Studio: + + + + + +--- diff --git a/virtual_key_old/product/prompt-engineering-studio/prompt-api.mdx b/virtual_key_old/product/prompt-engineering-studio/prompt-api.mdx new file mode 100644 index 00000000..4434fc69 --- /dev/null +++ b/virtual_key_old/product/prompt-engineering-studio/prompt-api.mdx @@ -0,0 +1,536 @@ +--- +title: "Prompt API" +description: "Learn how to integrate Portkey's prompt templates directly into your applications using the Prompt API" +--- + + + +This feature is available on all Portkey [plans](https://portkey.ai/pricing). + + + +The Portkey Prompts API allows you to seamlessly integrate your saved prompts directly into your applications. This powerful feature lets you separate prompt engineering from application code, making both easier to maintain while providing consistent, optimized prompts across your AI applications. + +With the Prompt API, you can: +- Use versioned prompts in production applications +- Dynamically populate prompts with variables at runtime +- Override prompt parameters as needed without modifying the original templates +- Retrieve prompt details for use with provider-specific SDKs + +## API Endpoints + +Portkey offers two primary endpoints for working with saved prompts: + +1. **Prompt Completions** (`/prompts/{promptId}/completions`) - Execute your saved prompt templates directly, receiving model completions +2. **Prompt Render** (`/prompts/{promptId}/render`) - Retrieve your prompt template with variables populated, without executing it + +## Prompt Completions + +The Completions endpoint is the simplest way to use your saved prompts in production. It handles the entire process - retrieving the prompt, applying variables, sending it to the appropriate model, and returning the completion. + +### Making a Completion Request + + + +```python +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY" +) + +# Execute the prompt with provided variables +completion = portkey.prompts.completions.create( + prompt_id="YOUR_PROMPT_ID", + variables={ + "user_input": "Hello world" + }, + max_tokens=250, + presence_penalty=0.2 +) + +print(completion) +``` + + +```javascript +import Portkey from 'portkey-ai'; + +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY" +}); + +// Execute the prompt with provided variables +const completion = await portkey.prompts.completions.create({ + promptID: "YOUR_PROMPT_ID", + variables: { + "user_input": "Hello world" + }, + max_tokens: 250, + presence_penalty: 0.2 +}); + +console.log(completion); +``` + + +```bash +curl -X POST "https://api.portkey.ai/v1/prompts/YOUR_PROMPT_ID/completions" \ + -H "Content-Type: application/json" \ + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ + -d '{ + "variables": { + "user_input": "Hello world" + }, + "max_tokens": 250, + "presence_penalty": 0.2 + }' +``` + + + +### Streaming Support + +The completions endpoint also supports streaming responses for real-time interactions: + + + +```javascript +import Portkey from 'portkey-ai'; + +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY" +}); + +// Create a streaming completion +const streamCompletion = await portkey.prompts.completions.create({ + promptID: "YOUR_PROMPT_ID", + variables: { + "user_input": "Hello world" + }, + stream: true +}); + +// Process the stream +for await (const chunk of streamCompletion) { + console.log(chunk); +} +``` + + +```python +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY" +) + +# Create a streaming completion +completion = portkey.prompts.completions.create( + prompt_id="YOUR_PROMPT_ID", + variables={ + "user_input": "Hello world" + }, + stream=True +) + +# Process the stream +for chunk in completion: + print(chunk) +``` + + + +## Prompt Render + +You can retrieve your saved prompts on Portkey using the `/prompts/$PROMPT_ID/render` endpoint. Portkey returns a JSON containing your prompt or messages body along with all the saved parameters that you can directly use in any request. + +This is helpful if you are required to use provider SDKs and can not use the Portkey SDK in production. ([Example of how to use Portkey prompt templates with OpenAI SDK](/product/prompt-library/retrieve-prompts#using-the-render-output-in-a-new-request)) + +## Using the `Render` Endpoint/Method + +1. Make a request to `https://api.portkey.ai/v1/prompts/$PROMPT_ID/render` with your prompt ID +2. Pass your Portkey API key with `x-portkey-api-key` in the header +3. Send up the variables in your payload with `{ "variables": { "VARIABLE_NAME": "VARIABLE_VALUE" } }` + +That's it! See it in action: + + + + +```sh +curl -X POST "https://api.portkey.ai/v1/prompts/$PROMPT_ID/render" \ +-H "Content-Type: application/json" \ +-H "x-portkey-api-key: $PORTKEY_API_KEY" \ +-d '{ + "variables": {"movie":"Dune 2"} +}' +``` + +The Output: + +```JSON +{ + "success": true, + "data": { + "model": "gpt-4", + "n": 1, + "top_p": 1, + "max_tokens": 256, + "temperature": 0, + "presence_penalty": 0, + "frequency_penalty": 0, + "messages": [ + { + "role": "system", + "content": "You're a helpful assistant." + }, + { + "role": "user", + "content": "Who directed Dune 2?" + } + ] + } +} +``` + + + + +```py +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY" +) + +render = portkey.prompts.render( + prompt_id="PROMPT_ID", + variables={ "movie":"Dune 2" } +) + +print(render.data) +``` + +The Output: + +```JSON +{ + "model": "gpt-4", + "n": 1, + "top_p": 1, + "max_tokens": 256, + "temperature": 0, + "presence_penalty": 0, + "frequency_penalty": 0, + "messages": [ + { + "role": "system", + "content": "You're a helpful assistant." + }, + { + "role": "user", + "content": "Who directed Dune 2?" + } + ] +} +``` + + + + +```js +import Portkey from 'portkey-ai' + +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY" +}) + +async function getRender(){ + const render = await portkey.prompts.render({ + promptID: "PROMPT_ID", + variables: { "movie":"Dune 2" } + }) + + console.log(render.data) +} + +getRender() +``` + +The Output: + +```JSON +{ + "model": "gpt-4", + "n": 1, + "top_p": 1, + "max_tokens": 256, + "temperature": 0, + "presence_penalty": 0, + "frequency_penalty": 0, + "messages": [ + { + "role": "system", + "content": "You're a helpful assistant." + }, + { + "role": "user", + "content": "Who directed Dune 2?" + } + ] +} +``` + + + + + +## Updating Prompt Params While Retrieving the Prompt + +If you want to change any model params (like `temperature`, `messages body` etc) while retrieving your prompt from Portkey, you can send the override params in your `render` payload. + +Portkey will send back your prompt with overridden params, **without** making any changes to the saved prompt on Portkey. + + + + + ```sh +curl -X POST "https://api.portkey.ai/v1/prompts/$PROMPT_ID/render" \ +-H "Content-Type: application/json" \ +-H "x-portkey-api-key: $PORTKEY_API_KEY" \ +-d '{ + "variables": {"movie":"Dune 2"}, + "model": "gpt-3.5-turbo", + "temperature": 2 +}' +``` + +Based on the above snippet, `model` and `temperature` params in the retrieved prompt will be **overridden** with the newly passed values + +The New Output: + +```JSON +{ + "success": true, + "data": { + "model": "gpt-3.5-turbo", + "n": 1, + "top_p": 1, + "max_tokens": 256, + "temperature": 2, + "presence_penalty": 0, + "frequency_penalty": 0, + "messages": [ + { + "role": "system", + "content": "You're a helpful assistant." + }, + { + "role": "user", + "content": "Who directed Dune 2?" + } + ] + } +} +``` + + + +```py +from portkey_ai import Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY" +) + +render = portkey.prompts.render( + prompt_id="PROMPT_ID", + variables={ "movie":"Dune 2" }, + model="gpt-3.5-turbo", + temperature=2 +) + +print(render.data) +``` + +Based on the above snippet, `model` and `temperature` params in the retrieved prompt will be **overridden** with the newly passed values. + +**The New Output:** + +```JSOn +{ + "model": "gpt-3.5-turbo", + "n": 1, + "top_p": 1, + "max_tokens": 256, + "temperature": 2, + "presence_penalty": 0, + "frequency_penalty": 0, + "messages": [ + { + "role": "system", + "content": "You're a helpful assistant." + }, + { + "role": "user", + "content": "Who directed Dune 2?" + } + ] +} +``` + + + +```js +import Portkey from 'portkey-ai' + +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY" +}) + +async function getRender(){ + const render = await portkey.prompts.render({ + promptID: "PROMPT_ID", + variables: { "movie":"Dune 2" }, + model: "gpt-3.5-turbo", + temperature: 2 + }) + + console.log(render.data) +} + +getRender() +``` + +Based on the above snippet, `model` and `temperature` params in the retrieved prompt will be **overridden** with the newly passed values. + +**The New Output:** + +```JSON +{ + "model": "gpt-3.5-turbo", + "n": 1, + "top_p": 1, + "max_tokens": 256, + "temperature": 2, + "presence_penalty": 0, + "frequency_penalty": 0, + "messages": [ + { + "role": "system", + "content": "You're a helpful assistant." + }, + { + "role": "user", + "content": "Who directed Dune 2?" + } + ] +} +``` + + + + +## Using the `render` Output in a New Request + +Here's how you can take the output from the `render` API and use it for making a call. We'll take example of OpenAI SDKs, but you can use it simlarly for any other provider SDK as well. + + + + +```js +import Portkey from 'portkey-ai'; +import OpenAI from 'openai'; + +// Retrieving the Prompt from Portkey + +const portkey = new Portkey({ + apiKey: "PORTKEY_API_KEY" +}) + +async function getPromptTemplate() { + const render_response = await portkey.prompts.render({ + promptID: "PROMPT_ID", + variables: { "movie":"Dune 2" } + }) + return render_response.data; +} + +// Making a Call to OpenAI with the Retrieved Prompt + +const openai = new OpenAI({ + apiKey: 'OPENAI_API_KEY', + baseURL: 'https://api.portkey.ai/v1', + defaultHeaders: { + 'x-portkey-provider': 'openai', + 'x-portkey-api-key': 'PORTKEY_API_KEY', + 'Content-Type': 'application/json', + } +}); + +async function main() { + const PROMPT_TEMPLATE = await getPromptTemplate(); + const chatCompletion = await openai.chat.completions.create(PROMPT_TEMPLATE); + console.log(chatCompletion.choices[0]); +} + +main(); +``` + + + +```py +from portkey_ai import Portkey +from openai import OpenAI + +# Retrieving the Prompt from Portkey + +portkey = Portkey( + api_key="PORTKEY_API_KEY" +) + +render_response = portkey.prompts.render( + prompt_id="PROMPT_ID", + variables={ "movie":"Dune 2" } +) + +PROMPT_TEMPLATE = render_response.data + +# Making a Call to OpenAI with the Retrieved Prompt + +openai = OpenAI( + api_key = "OPENAI_API_KEY", + base_url = "https://api.portkey.ai/v1", + default_headers = { + 'x-portkey-provider': 'openai', + 'x-portkey-api-key': 'PORTKEY_API_KEY', + 'Content-Type': 'application/json', + } +) + +chat_complete = openai.chat.completions.create(**PROMPT_TEMPLATE) + +print(chat_complete.choices[0].message.content) +``` + + + + +# CRUD: coming soon ๐Ÿš€ + + +## API Reference + +For complete API details, including all available parameters and response formats, refer to the API reference documentation: + +- [Prompt Completions API Reference](https://portkey.ai/docs/api-reference/inference-api/prompts/prompt-completion) +- [Prompt Render API Reference](https://portkey.ai/docs/api-reference/inference-api/prompts/render) + +## Next Steps + +Now that you understand how to integrate prompts into your applications, explore these related features: + +- [Prompt Playground](/product/prompt-engineering-studio/prompt-playground) - Create and test prompts in an interactive environment +- [Prompt Versioning](/product/prompt-engineering-studio/prompt-versioning) - Track changes to your prompts over time +- [Prompt Observability](/product/prompt-engineering-studio/prompt-observability) - Monitor prompt performance in production diff --git a/virtual_key_old/product/prompt-engineering-studio/prompt-guides.mdx b/virtual_key_old/product/prompt-engineering-studio/prompt-guides.mdx new file mode 100644 index 00000000..eae0ac09 --- /dev/null +++ b/virtual_key_old/product/prompt-engineering-studio/prompt-guides.mdx @@ -0,0 +1,23 @@ +--- +title: "Guides" +description: "Learn how to get the most out of Portkey Prompts with these practical guides" +--- + + +## Getting Started with Portkey Prompts + +Our guides help you master Portkey Prompts - from basic concepts to advanced techniques. Whether you're new to prompt engineering or looking to optimize your existing workflows, these resources will help you build better AI applications. + + +You can easily access Prompt Engineering Studio using [https://prompt.new](https://prompt.new) + + + + + + + + + + +Still have questions? Join our [Discord community](https://portkey.sh/reddit-discord) to connect with other Portkey users and get help from our team. diff --git a/virtual_key_old/product/prompt-engineering-studio/prompt-integration.mdx b/virtual_key_old/product/prompt-engineering-studio/prompt-integration.mdx new file mode 100644 index 00000000..d4e92d7a --- /dev/null +++ b/virtual_key_old/product/prompt-engineering-studio/prompt-integration.mdx @@ -0,0 +1,17 @@ +--- +title: "Integrations" +--- +Portkey prompts can be seamlessly integrated with your development workflow and existing tools. These integrations help you leverage your optimized prompts across your entire AI infrastructure. + + +You can easily access Prompt Engineering Studio using [https://prompt.new](https://prompt.new) + + + + + + + + + + diff --git a/virtual_key_old/product/prompt-engineering-studio/prompt-library.mdx b/virtual_key_old/product/prompt-engineering-studio/prompt-library.mdx new file mode 100644 index 00000000..74b99ed1 --- /dev/null +++ b/virtual_key_old/product/prompt-engineering-studio/prompt-library.mdx @@ -0,0 +1,74 @@ +--- +title: "Prompt Library" +--- + + +This feature is available on all Portkey [plans](https://portkey.ai/pricing). + + +Portkey's Prompt Library serves as your central repository for managing, organizing, and collaborating on prompts across your organization. This feature enables teams to maintain consistent prompt strategies while making prompt templates easily accessible to all team members. + +The Prompt Library offers a structured way to store and manage your prompt templates. It provides: +- A central location for all your prompt templates +- Folder organization for logical grouping +- Collaboration capabilities for team environments + +This centralized approach helps maintain consistency in your AI interactions while making it easy to reuse proven prompt patterns across different applications. + +## Accessing the Prompt Library + +You can access the Prompt Library from the left navigation menu by clicking on "Prompts" under the Prompt Engineering section. This opens the main library view where you can see all your prompt templates and folders. + + +![Prompt Library](/images/product/prompts/prompt-library.png) + + +## Library Organization + +Prompts can be organized into folders for better categorization. For example, you might create separate folders for: +- Customer service prompts +- Content generation prompts +- Data analysis prompts +- Agent-specific prompts + +## Creating New Prompts + +To add a new prompt to your library: +1. Click the "Create" button in the top-right corner +2. Select "Prompt" from the dropdown menu +3. Build your prompt in the [Prompt Playground](/product/prompt-engineering-studio/prompt-playground) +4. Save the prompt to add it to your library + +New prompts are automatically assigned a unique ID that you can use to reference them in your applications via the [Prompt API](/product/prompt-engineering-studio/prompt-api). + +### Organizing with Folders + +To create a new folder: +1. Click "Create" in the top-right corner +2. Select "Folder" from the dropdown +3. Name your folder based on its purpose or content type + +To move prompts into folders: +1. Select the prompts you want to organize +2. Use the move option to place them in the appropriate folder + +## Collaboration Features + +The Prompt Library is designed for team collaboration: +- All team members with appropriate permissions can access shared prompts +- Changes are tracked by user and timestamp through [Prompt Versioning](/product/prompt-engineering-studio/prompt-versioning) +- Multiple team members can work on different prompts simultaneously + +This collaborative approach ensures that your team maintains consistent prompt strategies while allowing everyone to contribute their expertise. + + +For more details on implementing prompts in your code, see the [Prompt API](/product/prompt-engineering-studio/prompt-api) documentation. + +## Next Steps + +Now that you understand the basics of the Prompt Library, explore these related features: +- [Prompt Playground](/product/prompt-engineering-studio/prompt-playground) - Create and test new prompts +- [Prompt Partials](/product/prompt-engineering-studio/prompt-partial) - Create reusable prompt components +- [Prompt Versioning](/product/prompt-engineering-studio/prompt-versioning) - Track changes to your prompts +- [Prompt API](/product/prompt-engineering-studio/prompt-api) - Integrate prompts into your applications +- [Prompt Observability](/product/prompt-engineering-studio/prompt-observability) - Monitor prompt performance diff --git a/virtual_key_old/product/prompt-engineering-studio/prompt-observability.mdx b/virtual_key_old/product/prompt-engineering-studio/prompt-observability.mdx new file mode 100644 index 00000000..474b5d12 --- /dev/null +++ b/virtual_key_old/product/prompt-engineering-studio/prompt-observability.mdx @@ -0,0 +1,70 @@ +--- +title: "Prompt Observability" +--- + +Portkey's Prompt Observability provides comprehensive insights into how your prompts are performing in production. This feature allows you to track usage, monitor performance metrics, and analyze trends to continuously improve your prompts based on real-world usage. + + +This feature is available on all Portkey [plans](https://portkey.ai/pricing). + + +## Overview + +Prompt Observability gives you visibility into your prompt usage and performance through analytics dashboards, detailed logs, and template history. By monitoring these metrics, you can identify which prompts are performing well and which need optimization, helping you make data-driven decisions about your AI applications. + +## Accessing Prompt Observability + +You can access observability data in several ways: + +1. **From the Prompt Template page**: View history and performance metrics for a specific prompt +2. **From the Analytics dashboard**: Filter analytics by prompt ID +3. **From the Logs section**: Filter logs by prompt ID to see detailed usage information + +## Prompt Analytics + +The Analytics dashboard provides high-level metrics for your prompts, showing important information like costs, token usage, latency, request volume, and user engagement. You can easily filter your prompts using `prompt-id` in the analytics dashboard. + + + + + +The dashboard enables you to understand trends in your prompt usage over time and identify potential opportunities for optimization. For more details on using the analytics dashboard and available filters, refer to Portkey's [Analytics documentation](/product/observability/analytics). + +## Prompt Logs + +The Logs section on Portkey's dashboard provides detailed information about each individual prompt call, giving you visibility into exactly how your prompts are being used in real-time. You can easily filter your prompts using `prompt-id` in the logs view. + + + + + +Each log entry shows the timestamp, model used, request path, user, tokens consumed, cost, and status. This granular data helps you understand exactly how your prompts are performing in production and identify any issues that need attention. + +For information on filtering and searching logs, refer to Portkey's [Logs documentation](/product/observability/logs). + + +**Render Calls**: Note that `prompts.render` API calls are not logged in the observability features. Only `prompts.completions` calls are tracked. + + +## Prompt Template History + +Each prompt template includes a "Recent" tab that shows the history of calls made using that specific template: + + + + + +This chronological view makes it easy to see how your template is being used and how it's performing over time. You can quickly access detailed information about each call directly from this history view. + +The template history is particularly useful when you're iterating on a prompt design, as it allows you to see the immediate impact of your changes. Combined with [Prompt Versioning](/product/prompt-engineering-studio/prompt-versioning), this gives you a complete view of your prompt's evolution and performance. + + +## Next Steps + +Now that you understand how to monitor your prompts, explore these related features: + +- [Prompt Versioning](/product/prompt-engineering-studio/prompt-versioning) - Track changes to your prompts over time +- [Prompt API](/product/prompt-engineering-studio/prompt-api) - Integrate optimized prompts into your applications +- [Prompt Playground](/product/prompt-engineering-studio/prompt-playground) - Test and refine your prompts based on observability insights +- [Prompt Partials](/product/prompt-engineering-studio/prompt-partial) - Create reusable components for your prompts +- [Tool Library](/product/prompt-engineering-studio/tool-library) - Enhance your prompts with specialized tools diff --git a/virtual_key_old/product/prompt-engineering-studio/prompt-partial.mdx b/virtual_key_old/product/prompt-engineering-studio/prompt-partial.mdx new file mode 100644 index 00000000..aa9c8107 --- /dev/null +++ b/virtual_key_old/product/prompt-engineering-studio/prompt-partial.mdx @@ -0,0 +1,174 @@ +--- +title: "Prompt Partials" +description: "With Prompt Partials, you can save your commonly used templates (which could be your instruction set, data structure explanation, examples etc.) separately from your prompts and flexibly incorporate them wherever required." +--- + + +This feature is available on all Portkey [plans](https://portkey.ai/pricing). + + +Partials can also serve as a global variable store. You can define common variables that are used across multiple of your prompt templates and can reference or update them easily. + +## Creating Partials + +Partials are directly accessible from the Prompts Page in the [Prompt Engineering Studio](/product/prompt-engineering-studio): + + + + + +You can create a new Partial and use it for any purpose in any of your prompt templates. For example, here's a prompt partial where we are separately storing the instructions: + + + + + +Upon saving, each Partial generates a unique ID that you can use inside [prompt templates](/product/prompt-engineering-studio/prompt-playground#prompt-templates). + +### Template Engine + +Partials also follow the [Mustache template engine](https://mustache.github.io/) and let you easily handle data input at runtime by using tags. + +Portkey supports `{{variable}}`, `{{#block}} {{/block}}`, `{{^block}}` and other tags. + +For more details on template syntax, check out the [Prompt Playground documentation](/product/prompt-engineering-studio/prompt-playground#supported-tags) which includes a comprehensive guide on how to use tags. + +### Versioning + +Portkey follows the same `Update` **&** `Publish` flow as prompt templates. You can keep updating the partial and save new versions, and choose to send any version to production using the `Publish` feature. + +All the version history for any partial is available on the right column and any previous version can be restored to be `latest` or `published` to production easily. For more details on how versioning works, see the [Prompt Versioning](/product/prompt-engineering-studio/prompt-versioning) documentation. + +## Using Partials + +You can call Partials by their ID inside any prompt template by just starting to type `{{>`. + +Portkey lists all of the available prompt partials with their names to help you easily pick: + + + + + +When a partial is incorporated in a template, all the variables/blocks defined are also rendered on the Prompt variables section: + + + + + +When a new Partial version is **Published**, your partial that is in use in any of the prompt templates also gets automatically updated. + +### Using Different Versions of Partials + +Similar to prompt templates, you can reference specific versions of your prompt partials in the playground. By default, when you use a partial, Portkey uses the published version, but you can specify any version you want. + +To reference a specific version of a partial, use the following syntax: + +``` +{{>prompt-partial-id@version-number}} +``` + +For example: + +``` +{{>pp-instructions-123@5}} +``` + +This will use version 5 of the prompt partial with ID "pp-instructions-123". + +**Note:** Unlike prompt templates, prompt partials do not support `labels`, `@latest`, `@published` for versioning. You can only reference partials by their version number, `@latest`, or the published version. + +### Making a Prompt Completion Request + +All the variables/tags defined inside the partial can now be directly called at the time of making a `prompts.completions` request: + + + +```js +import Portkey from 'portkey-ai' + +const portkey = new Portkey({ + apiKey: "YOUR_PORTKEY_API_KEY" +}) + +const response = portkey.prompts.completions.create({ + promptID: "pp-system-pro-34a60b", + variables: { + "user_query":"", + "company":"", + "product":"", + "benefits":"", + "phone number":"", + "name":"", + "device":"", + "query":"" + } +}) +``` + + +```py +from portkey_ai import Portkey + +portkey = Portkey( + api_key="YOUR_PORTKEY_API_KEY" +) + +response = portkey.prompts.completions.create( + prompt_id="PROMPT_ID", + variables={ + "user_query":"", + "company":"", + "product":"", + "benefits":"", + "phone number":"", + "name":"", + "device":"", + "query":"" + } +) +``` + + + +For more details on integrating prompts in your application, see the [Prompt API documentation](/product/prompt-engineering-studio/prompt-api). + +## Benefits of Using Partials + +Using Prompt Partials offers several advantages for your AI applications: + +1. **Reusability**: Create instructions once and use them across multiple prompts +2. **Consistency**: Ensure all prompts follow the same guidelines and structure +3. **Maintainability**: Update instructions in one place and have changes propagate everywhere +4. **Organization**: Keep your prompt library clean by separating reusable components +5. **Collaboration**: Enable team members to use standardized components in their prompts + +## Best Practices for Prompt Partials + +For optimal use of Prompt Partials: + +- Use descriptive names for your partials to make them easy to identify +- Create partials for frequently used instructions, examples, or context +- Keep partials focused on a single purpose for better reusability +- Document your partials to help team members understand their purpose +- Use [Prompt Versioning](/product/prompt-engineering-studio/prompt-versioning) to track changes to your partials +- Consider creating specialized partials for different use cases (e.g., one for detailed instructions, another for examples) + +## Common Use Cases for Partials + +Prompt Partials are particularly useful for: + +- **System Instructions**: Create standardized directives for your AI models +- **Example Sets**: Maintain collections of examples to guide model outputs +- **Context Blocks**: Store context information that can be reused across prompts +- **Output Formats**: Define structured output templates for consistent responses +- **Tool Definitions**: Maintain standard tool definitions for function calling + +## Next Steps + +Now that you understand how to use Prompt Partials, explore these related features: + +- [Prompt Playground](/product/prompt-engineering-studio/prompt-playground) - Create and test prompts using your partials +- [Prompt Library](/product/prompt-engineering-studio/prompt-library) - Organize your prompts and partials +- [Prompt Versioning](/product/prompt-engineering-studio/prompt-versioning) - Track changes to your partials over time +- [Prompt API](/product/prompt-engineering-studio/prompt-api) - Use partials in your applications +- [Prompt Observability](/product/prompt-engineering-studio/prompt-observability) - Monitor how prompts using your partials perform diff --git a/virtual_key_old/product/prompt-engineering-studio/prompt-playground.mdx b/virtual_key_old/product/prompt-engineering-studio/prompt-playground.mdx new file mode 100644 index 00000000..586c1d39 --- /dev/null +++ b/virtual_key_old/product/prompt-engineering-studio/prompt-playground.mdx @@ -0,0 +1,385 @@ +--- +title: "Prompt Playground" +--- + + +This feature is available for all plans: +* [**Developer**](https://portkey.ai/pricing): 3 Prompt Templates +* [**Production**](https://portkey.ai/pricing) & [**Enterprise**](https://portkey.ai/docs/product/enterprise-offering): Unlimited Prompt Templates + + + +You can easily access Prompt Engineering Studio using [https://prompt.new](https://prompt.new) + + +## What is the Prompt Playground? + +Portkey's Prompt Playground is a place to compare, test and deploy perfect prompts for your AI application. It's where you experiment with different models, test variables, compare outputs, and refine your prompt engineering strategy before deploying to production. + + +![Prompt Playground Interface](/images/product/ai-gateway/ai-20.webp) + + +## Getting Started + +When you first open the Playground, you'll see a clean interface with a few key components: +- A model selector where you can choose from 1600+ models across 20+ providers +- A messaging area where you'll craft your prompt +- A completion area where you'll see model responses + +The beauty of the Playground is its simplicity - write a prompt, click "Generate Completion", and instantly see how the model responds. + +### Crafting Your First Prompt + +Creating a prompt is straightforward: + +1. Select your model of choice - from OpenAI's GPT-4o to Anthropic's Claude or any model from your configured providers +2. Enter a system message (like "You're a helpful assistant") +3. Add your user message or query +4. Click "Generate Completion" to see the response + +You can continue the conversation by adding more messages, helping you simulate real-world interactions with your AI. + +### Using Prompt Templates in Your Application + +Once you save a prompt in the Playground, you'll receive a `prompt ID` that you can use directly in your application code. This makes it easy to move from experimentation to production: + + + +```ts +import Portkey from 'portkey-ai' + +const portkey = new Portkey({ + apiKey: "YOUR_PORTKEY_API_KEY" +}) + +async function main() { + // Call your saved prompt template using its ID + const response = await portkey.prompts.completions.create({ + promptID: "pp-system-pro-34a60b", + }) + + console.log(response) +} + +main() +``` + + +```py +from portkey_ai import Portkey + +portkey = Portkey( + api_key="YOUR_PORTKEY_API_KEY" +) + +# Call your saved prompt template using its ID +response = portkey.prompts.completions.create( + prompt_id="pp-system-pro-34a60b" +) + +print(response) +``` + + + +This approach allows you to separate prompt engineering from your application code, making both easier to maintain. For more details on integrating prompts in your applications, check out our [Prompt API](/product/prompt-engineering-studio/prompt-api) documentation. + +### Comparing Models Side-by-Side + +Wondering which model works best for your use case? The side-by-side comparison feature lets you see how different models handle the same prompt. + +Click the "+ Compare" button to add another column, select a different model, and generate completions simultaneously. You will be able to see how each model responds to the same prompt, along with crucial metrics like latency, total tokens, and throughput helping you make informed decisions about which model to use in production. + +You can run comparisons on the same prompt template by selecting the template from the "New Template" dropdown in the UI along with the versions button across multiple models. Once you figure out what is working, you can click on the "Update Prompt" button to update the prompt template with a new version. You can also compare different [prompt versions](/product/prompt-engineering-studio/prompt-versioning) by selecting the version from the UI. + +The variables you define apply across all templates in the comparison, ensuring you're testing against identical inputs. + + + + + +### Enhancing Prompts with Tools + +Some models support function calling, allowing the AI to request specific information or take actions. The Playground makes it easy to experiment with these capabilities. + +Click "Add Tool" button to define functions the model can call. For example: + + + + + +```json +{ + "type": "function", + "function": { + "name": "get_weather", + "description": "Get current weather for a location", + "parameters": { + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "City and state, e.g., San Francisco, CA" + } + }, + "required": ["location"] + } + } +} +``` + +You can add multiple tools from the [tool library](/product/prompt-engineering-studio/tool-library) for the specific prompt template. You can also choose the parameter "tool_choice" from the UI to control how the model uses the available tools. + +This tool definition teaches the model how to request weather information for a specific location. + +### Configuring Model Parameters + +Each model offers various parameters that affect its output. Access these by clicking the "Parameters" button: + +- **Temperature**: Controls randomness (lower = more deterministic) +- **Top P**: Alternative to temperature for controlling diversity +- **Max Tokens**: Limits response length +- **Response Format**: An important setting that allows users to define how they want the model to output. Currently there are 3 options: + - Text (default free-form text) + - JSON object (structured JSON response) + - JSON schema (requires providing a schema in the menu to make the model conform to your exact structure) +- **Thinking Mode**: Reasoning models think before they answer, producing a long internal chain of thought before responding to the user. You can access to the model's reasoning/thinking process sent by the provider. This feature: + - Is only available for select reasoning-capable models (like Claude 3.7 Sonnet) + - Can be activated by checking the "Thinking" checkbox in the Parameters panel + - Allows you to set a budget of tokens dedicated specifically to the thinking process (if the provider supports it) + +And more... Experiment with these settings to find the perfect balance for your use case. + +### Pretty Mode vs JSON Mode + +The Playground offers two interface modes for working with prompts: + +**Pretty Mode** + +The default user-friendly interface with formatted messages and simple controls. This is ideal for most prompt engineering tasks and provides an intuitive way to craft and test prompts. + +**JSON Mode** + +For advanced users who need granular control, you can toggle to JSON mode by clicking the "JSON" button. This reveals the raw JSON structure of your prompt, allowing for precise editing and advanced configurations. + +JSON mode is particularly useful when: +- Working with multimodal inputs like images +- Creating complex conditional logic +- Defining precise message structures +- Debugging API integration issues + +You can switch between modes at any time using the toggle in the interface. + +### Multimodality: Working with Images + +For multimodal models that support images, you can upload images directly in the Playground using the ๐Ÿงท icon on the message input box. + +Alternatively, you can use JSON mode to incorporate images using variables. Toggle from PRETTY to JSON mode using the button on the dashboard, then structure your prompt like this: + +```json +[ + { + "content": [ + { + "type": "text", + "text": "You're a helpful assistant." + } + ], + "role": "system" + }, + { + "role": "user", + "content": [ + { "type": "text", "text": "what's in this image?" }, + { + "type": "image_url", + "image_url": { + "url" : "{{your_image_url}}" + } + } + ] + } +] +``` + +Now you can pass the image URL as a variable in your prompt template, and the model will be able to analyze the image content. + +# Prompt Templates + +**Portkey uses** [**Mustache**](https://mustache.github.io/mustache.5.html) **under the hood to power the prompt templates.** + +Mustache is a commonly used logic-less templating engine that follows a simple schema for defining variables and more. + +With Mustache, prompt templates become even more extensible by letting you incorporate various `{{tags}}` in your prompt template and easily pass your data. + +The most common usage of mustache templates is for `{{variables}}`, used to pass a value at runtime. + +### Using Variables in Prompt Templates + +Let's look at the following template: + + + + + +As you can see, `{{customer_data}}` and `{{chat_query}}` are defined as variables in the template and you can pass their value at runtime: + + + +```ts +import Portkey from 'portkey-ai' + +const portkey = new Portkey() + +async function main() { + const response = await portkey.prompts.completions.create({ + promptID: "pp-hr-bot-5c8c6e", + variables: { + "customer_data":"", + "chat_query":"" + } + }) + + console.log(response) +} + +main() +``` + + +```py +from portkey_ai import Portkey + +portkey = Portkey() + +response = portkey.prompts.completions.create({ + prompt_id="pp-hr-bot-5c8c6e", + variables= { + "customer_data":"", + "chat_query":"" + } +}) +``` + + + +**Using variables is just the start! Portkey supports multiple Mustache tags that let you extend the template functionality:** + +### Supported Variable Tags + +| Tag | Functionality | Example | +| --------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `{{variable}}` | Variable | Template: Hi! My name is `{{name}}`. I work at `{{company}}`.

Data: `Copy{ "name": "Chris", "company": "GitHub" }`

Output: Hi! My name is Chris. I work at Github. | +| `{{#variable}}` `` `{{/variable}}` | Render ```` only if variable is true or non Empty | Template: Hello I am Tesla bot.`{{#chat_mode_pleasant}}` Excited to chat with you! `{{chat_mode_pleasant}}`What can I help you with?

Data: Copy`{ "chat_mode_pleasant": False }`

Output: Hello I am Tesla bot. What can I help you with? | +| `{{^variable}}` ```{{/variable}}` | Render `` only if variable is false or empty | Template: Hello I am Tesla bot.`{{^chat_mode_pleasant}}` Excited to chat with you! `{{/chat_mode_pleasant}}`What can I help you with?

Data: Copy`{ "chat_mode_pleasant": False }`

Output: Hello I am Tesla bot. Excited to chat with you! What can I help you with? | +| `{{#variable}}` `{{sub_variable}}` `{{/variable}}` | Iteratively render all the values of sub\_variable if variable is true or non Empty | Template: Give atomic symbols for the following: `{{#variable}}` - `{{sub_variable}}` `{{/variable}}`

Data: Copy`{ "variable": \[ { "sub\_variable": "Gold" }, { "sub\_variable": "Carbon" }, { "sub\_variable": "Zinc" } \] } `

Output: Give atomic symbols for the following: - Gold - Carbon - Zinc | +| `{{! Comment}} ` | Comments that are ignored | Template: Hello I am Tesla bot.`{{! How do tags work?}}` What can I help you with?

Data: Copy{}

Output: Hello I am Tesla bot. What can I help you with? | +| `{{>Partials}} ` | "Mini-templates" that can be called at runtime. On Portkey, you can save [partials](/product/prompt-engineering-studio/prompt-partial) separately and call them in your prompt templates by typing `{{>` | Template: Hello I am Tesla bot.`{{>pp-tesla-template}}` What can I help you with?

Data in `pp-tesla-template`: CopyTake the context from `{{context}}`. And answer user questions.

Output: Hello I am Tesla bot. Take the context from `{{context}}`. And answer user questions. What can I help you with? | +| `{{>>Partial Variables}} ` | Pass your privately saved partials to Portkey by creating tags with double \>>Like: `{{>> }}` This is helpful if you do not want to save your partials with Portkey but are maintaining them elsewhere | Template: Hello I am Tesla bot.`{{>>My Private Partial}}` What can I help you with? | + +### Using Variable Tags + +You can directly pass your data object containing all the variable/tags info (in JSON) to Portkey's `prompts.completions` method with the `variables` property. + +**For example, here's a [prompt partial](/product/prompt-engineering-studio/prompt-partial) containing the key instructions for an AI support bot:** + + + + + +**And the prompt template uses the partial like this:** + + + + +**We can pass the data object inside the variables:** + + + +```ts +import Portkey from 'portkey-ai' + +const portkey = new Portkey({ + apiKey: "YOUR_PORTKEY_API_KEY" +}) + +const data = { + "company": "NESTLE", + "product": "MAGGI", + "benefits": "HEALTH", + "phone number": "123456", + "name": "Sheila", + "device": "iOS", + "query": "Product related", + "test_variable":"Something unrelated" // Your data object can also contain unrelated variables +} + +async function main() { + // Make the prompt creation call with the variables + const response = await portkey.prompts.completions.create({ + promptID: "pp-system-pro-34a60b", + variables: { + ...data, + "user_query": "I ate Maggi and I think it was stale." + } + }) + + console.log(response) +} + +main() +``` + + +```py +from portkey_ai import Portkey + +portkey = Portkey( + api_key="YOUR_PORTKEY_API_KEY" +) + +data = { + "company": "NESTLE", + "product": "MAGGI", + "benefits": "HEALTH", + "phone number": "123456", + "name": "Sheila", + "device": "iOS", + "query": "Product related", + "test_variable": "Something unrelated" # Your data object can also contain unrelated variables +} + +# Make the prompt creation call with the variables +response = portkey.prompts.completions.create( + prompt_id="pp-system-pro-34a60b", + variables={ + **data, + "user_query": "I ate Maggi and I think it was stale." + } +) + +print(response) +``` + + + +## From Experiment to Production + +Once you've crafted the perfect prompt, save it with a click of the "Save Prompt" button. Your prompt will be [versioned](/product/prompt-engineering-studio/prompt-versioning) automatically, allowing you to track changes over time. + +Saved prompts can be: +- Called directly from your application using the [Prompt API](/product/prompt-engineering-studio/prompt-api) +- Shared with team members for collaboration through the [Prompt Library](/product/prompt-engineering-studio/prompt-library) +- Monitored for performance in production with [Prompt Observability](/product/prompt-engineering-studio/prompt-observability) + +## Next Steps + +Now that you understand the basics of the Prompt Playground, you're ready to create powerful, dynamic prompts for your AI applications. Start by experimenting with different models and prompts to see what works best for your use case. + +Looking for more advanced techniques? Check out our guides on: +- [Prompt Versioning](/product/prompt-engineering-studio/prompt-versioning) +- [Prompt Partials](/product/prompt-engineering-studio/prompt-partial) +- [Prompt API](/product/prompt-engineering-studio/prompt-api) +- [Tool Library](/product/prompt-engineering-studio/tool-library) +- [Prompt Integrations](/product/prompt-engineering-studio/prompt-integration) diff --git a/virtual_key_old/product/prompt-engineering-studio/prompt-versioning.mdx b/virtual_key_old/product/prompt-engineering-studio/prompt-versioning.mdx new file mode 100644 index 00000000..8bcbf7b8 --- /dev/null +++ b/virtual_key_old/product/prompt-engineering-studio/prompt-versioning.mdx @@ -0,0 +1,178 @@ +--- +title: "Prompt Versioning & Labels" +--- + + +This feature is available on all Portkey [plans](https://portkey.ai/pricing). + + +Effective prompt management includes tracking changes, controlling access, and deploying the right version at the right time. Portkey's prompt versioning system helps you maintain a history of your prompt iterations while ensuring production stability. + +## Understanding Prompt Versioning + +Every time you make changes to a prompt template, Portkey tracks these modifications. The versioning system allows you to: + +- Try and test different prompt variations +- Keep a complete history of all prompt changes +- Compare different versions +- Revert to previous versions when needed +- Deploy specific versions to different environments + +**Updating vs. Publishing a Prompt Template** + +When working with prompts in Portkey, it's important to understand the difference between updating and publishing: + +- **Update**: When you edit a prompt, changes are saved as a new version but not pushed to production +- **Publish**: Making a specific version the "production" version that's used by default + + + + +## Managing Prompt Versions + +### Creating New Versions + +Whenever any changes are made to your prompt template, Portkey saves your changes in the browser **but** they are **not pushed** to production. You can click on the `Update` button on the top right to save the latest version of the prompt on Portkey. + + + + + +### Publishing Prompts + +Publishing a prompt version marks it as the default version that will be used when no specific version is requested. This is especially important for production environments. + +Updating the Prompt does not automatically update your prompt in production. While updating, you can tick `Publish prompt changes` which will also update your prompt deployment to the latest version. + +1. Create and test your new prompt version +2. When ready for production, click "Update" and check "Publish prompt changes" +3. Portkey will save the new version and mark it as the published version +4. All default API calls will now use this version + + + + + +### Viewing Version History + +**All** of your prompt versions can be seen by clicking the `Version History` button on the playground: + + + + + +You can `Restore` or `Publish` any of the previous versions by clicking on the ellipsis menu. + +### Comparing Versions + +To compare different versions of your prompt: + +1. Select the versions you want to compare from the version history panel +2. Click "Compare on playground" to see a side-by-side of different prompt versions + +This helps you understand how prompts have evolved and which changes might have impacted performance. + +## Using Different Prompt Versions + +By default, when you pass the `PROMPT_ID` in `prompts.completions.create` method, Portkey sends the request to the `Published` version of your prompt. + +You can also call any specific prompt version by appending version identifiers to your `PROMPT_ID`. + +### Version Number References + +**For example:** + +```js +response = portkey.prompts.completions.create( + prompt_id="pp-classification-prompt@12", + variables={ } +) +``` + +Here, the request is sent to **Version 12** of the prompt template. + +### Special Version References + +Portkey supports special version references: + +```js +// Latest version (may not be published) +response = portkey.prompts.completions.create( + prompt_id="pp-classification-prompt@latest", + variables={ } +) + +// Published version (default when no suffix is provided) +response = portkey.prompts.completions.create( + prompt_id="pp-classification-prompt", + variables={ } +) +``` + +**Important Notes:** +* `@latest` refers to the most recent version, regardless of publication status +* When no suffix is provided, Portkey defaults to the `Published` version +* Each version is immutable once created - to make changes, you must create a new version + +## Prompt Labels + +Labels provide a more flexible and meaningful way to reference prompt versions compared to version numbers. You can add version tags/labels like `platform-team`, `gpt-model-prompt` to any prompt version to track changes and call them directly: + + + + +### Using Labels in Your Code + + + ```ts @staging {2} + const promptCompletion = portkey.prompts.completions.create({ + promptID: "pp-article-xx@staging", + variables: {"":""} + }) + ``` + ```ts @develpoment {2} + const promptCompletion = portkey.prompts.completions.create({ + promptID: "pp-article-xx@development", + variables: {"":""} + }) + ``` + ```ts @production {2} + const promptCompletion = portkey.prompts.completions.create({ + promptID: "pp-article-xx@production", + variables: {"":""} + }) + ``` + ```ts @lorem-ipsum {2} + const promptCompletion = portkey.prompts.completions.create({ + promptID: "pp-article-xx@lorem-ipsum", + variables: {"":""} + }) + ``` + + +### Creating and Managing Labels + +To create or manage labels: + +1. Navigate to the prompt version sidebar +2. Click on "Labels" to view all available labels +3. Select a version and apply the desired label +4. You can move labels between versions as needed + + + You can now assign multiple custom labels to a single prompt version in Portkey. This makes it easier to promote a prompt version in it's developemtn cycle. + + + - There are 3 default labels: `production`, `staging`, `development` which cannot be removed. + - Custom labels are unique to the workspace where they are created. + - If you delete a custom label, any prompt completion requests to that label will start failing. + + +### Best Practices for Using Labels + +- Use `development` for experimental versions +- Use `staging` for versions ready for testing +- Use `production` for versions ready for real users +- Create custom labels for specific use cases or experiments + +Labels make it easy for you to test prompt versions through different environments. diff --git a/virtual_key_old/product/prompt-engineering-studio/tool-library.mdx b/virtual_key_old/product/prompt-engineering-studio/tool-library.mdx new file mode 100644 index 00000000..042d171d --- /dev/null +++ b/virtual_key_old/product/prompt-engineering-studio/tool-library.mdx @@ -0,0 +1,6 @@ +--- +title: "Tool Library" +--- + + +## Coming Soon!!! diff --git a/virtual_key_old/product/security/pii.mdx b/virtual_key_old/product/security/pii.mdx new file mode 100644 index 00000000..a5b475d5 --- /dev/null +++ b/virtual_key_old/product/security/pii.mdx @@ -0,0 +1,4 @@ +--- +title: PII Redaction +url: /product/guardrails/pii-redaction +---