### Full Code Example with Fallback Strategy Source: https://portkey.ai/docs/guides/use-cases/smart-fallback-with-model-optimized-prompts This example demonstrates the complete setup for using Portkey with a fallback strategy, including initializing the Portkey client, defining fallback targets, and making a prompt completion request. Ensure you have the 'portkey-ai' package installed. ```javascript import { Portkey } from 'portkey-ai'; const PORTKEY_API_KEY = 'xssxxrk'; const portkey = new Portkey({ apiKey: PORTKEY_API_KEY, config: { strategy: { mode: 'fallback' }, targets: [ { prompt_id: 'pp-task-to-su-72fbbb' }, { prompt_id: 'pp-task-to-su-051f65' } ] } }); const response = await portkey.prompts.completions.create({ promptID: 'pp-test-811461', variables: { goal: 'I want to acquire an AI engineering skills' } }); console.log(response.choices[0].message.content); ``` -------------------------------- ### JavaScript Quick Start with Portkey and Groq Source: https://portkey.ai/docs/llms-full.txt Integrate Groq models using Portkey in JavaScript with this quick start example. Requires 'portkey-ai' installation and the '@groq' provider setup in your model catalog. ```javascript import Portkey from 'portkey-ai' // 1. Install: npm install portkey-ai // 2. Add @groq provider in model catalog // 3. Use it: const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY" }) const response = await portkey.chat.completions.create({ model: "@groq/llama-3.3-70b-versatile", messages: [{ role: "user", content: "Hello!" }] }) console.log(response.choices[0].message.content) ``` -------------------------------- ### Python Quick Start with OpenAI and Portkey Gateway Source: https://portkey.ai/docs/llms-full.txt This example shows how to use the OpenAI Python library directly with the Portkey gateway URL. Install both 'openai' and 'portkey-ai' libraries and configure an '@openai' provider in your Portkey model catalog. ```python from openai import OpenAI from portkey_ai import PORTKEY_GATEWAY_URL # 1. Install: pip install openai portkey-ai # 2. Add @openai provider in model catalog # 3. Use it: client = OpenAI( api_key="PORTKEY_API_KEY", # Portkey API key base_url=PORTKEY_GATEWAY_URL ) response = client.chat.completions.create( model="@openai/gpt-4o", messages=[{"role": "user", "content": "Say this is a test"}] ) print(response.choices[0].message.content) ``` -------------------------------- ### Run OpenClaw Onboarding Wizard Source: https://portkey.ai/docs/llms-full.txt Initiate the OpenClaw onboarding process, including the installation of the Gateway service. This command starts an interactive setup wizard. ```bash openclaw onboard --install-daemon ``` -------------------------------- ### Complete OpenLIT and Portkey Integration Example Source: https://portkey.ai/docs/integrations/tracing-providers/openlit A comprehensive example demonstrating the full integration of OpenLIT and Portkey, including dependency installation, OpenTelemetry configuration, OpenLIT initialization, and OpenAI client setup for making instrumented LLM calls. ```python import os import openlit from openai import OpenAI from portkey_ai import createHeaders from opentelemetry.sdk.trace import TracerProvider from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter from opentelemetry.sdk.trace.export import SimpleSpanProcessor from opentelemetry import trace # Step 1: Configure Portkey endpoint os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = "https://api.portkey.ai/v1/logs/otel" os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = "x-portkey-api-key=YOUR_PORTKEY_API_KEY" # Step 2: Initialize OpenLIT with custom tracer (as shown in Step 3 above) trace_provider = TracerProvider() trace_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter())) trace.set_tracer_provider(trace_provider) tracer = trace.get_tracer(__name__) openlit.init(tracer=tracer, disable_batch=True) # Step 3: Configure Portkey Gateway (as shown in Step 4 above) client = OpenAI( api_key="PORTKEY_API_KEY", base_url="https://api.portkey.ai/v1", default_headers=createHeaders( api_key="PORTKEY_API_KEY", provider="@openai-prod" ) ) # Step 4: Make Instrumented LLM Calls (as shown in Step 5 above) response = client.chat.completions.create( model="gpt-4o", messages=[{"role": "user", "content": "Explain the benefits of OpenTelemetry in AI applications"}], temperature=0.7 ) print(response.choices[0].message.content) ``` -------------------------------- ### Python Quick Start with OpenAI SDK and Portkey Gateway Source: https://portkey.ai/docs/llms-full.txt This example shows how to use the OpenAI Python SDK with the Portkey Gateway URL to access OpenRouter models. Ensure 'openai' and 'portkey-ai' are installed and the '@openrouter' provider is set up in Portkey. ```python from openai import OpenAI from portkey_ai import PORTKEY_GATEWAY_URL # 1. Install: pip install openai portkey-ai # 2. Add @openrouter provider in model catalog # 3. Use it: client = OpenAI( api_key="PORTKEY_API_KEY", # Portkey API key base_url=PORTKEY_GATEWAY_URL ) response = client.chat.completions.create( model="@openrouter/openai/gpt-4o", messages=[{"role": "user", "content": "Say this is a test"}] ) print(response.choices[0].message.content) ``` -------------------------------- ### Complete Example: OpenTelemetry and Portkey Integration Source: https://portkey.ai/docs/integrations/tracing-providers/opentelemetry-python-sdk A comprehensive example demonstrating the setup of OpenTelemetry, Portkey gateway configuration, and manual instrumentation for LLM calls within a single script. ```python from opentelemetry import trace from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchSpanProcessor from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter from openai import OpenAI from portkey_ai import createHeaders # Step 1: Setup OpenTelemetry provider = TracerProvider() trace.set_tracer_provider(provider) otlp_exporter = OTLPSpanExporter( endpoint="https://api.portkey.ai/v1/otel/v1/traces", headers={"x-portkey-api-key": "YOUR_PORTKEY_API_KEY"} ) span_processor = BatchSpanProcessor(otlp_exporter) provider.add_span_processor(span_processor) tracer = trace.get_tracer(__name__) ``` -------------------------------- ### Example Self-Hosted Model Files Source: https://portkey.ai/docs/llms-full.txt Concrete examples of pricing and general configuration JSON files for a specific provider (e.g., OpenAI) in a self-hosted setup. ```tree pricing/openai.json general/openai.json ``` -------------------------------- ### Install and Run Python Sync Script Source: https://portkey.ai/docs/llms-full.txt Installs the necessary Python package and runs the `owui_to_portkey.py` script. It demonstrates how to specify a sync start time using the `--since` argument, falling back to a Python calculation if the `date` command is unavailable. ```bash pip install requests # Save the script from the Python section as owui_to_portkey.py python3 owui_to_portkey.py --since $(date -d '2 hours ago' +%s 2>/dev/null || python - <<'PY' import time print(int(time.time() - 7200)) PY ) ``` -------------------------------- ### Quickstart Chat Completion Example Source: https://portkey.ai/docs/llms-full.txt A minimal example demonstrating how to create a chat completion using the Portkey SDK. The SDK automatically detects the API key from the environment variable PORTKEY_API_KEY. ```python from portkey_ai import Portkey client = Portkey( api_key="your_api_key_here", # Or use the env var PORTKEY_API_KEY provider="@openai-prod" # Or use config="cf-***" ) response = client.chat.completions.create( messages=[{"role": "user", "content": "Hello, world!"}], model="gpt-4o" # Example provider/model ) ``` -------------------------------- ### Quick Start: Manual Sync with Node.js Source: https://portkey.ai/docs/guides/integrations/openwebui-to-portkey Install the 'node-fetch' package and execute the Node.js script to synchronize feedback. The --since argument specifies the start time for fetching feedback data. ```bash npm install node-fetch # Save the script from the Node section as owui_to_portkey.mjs node owui_to_portkey.mjs --since=$(date -d '2 hours ago' +%s 2>/dev/null || node -e "console.log(Math.floor(Date.now() / 1000) - 7200)") ``` -------------------------------- ### Python Quick Start with OpenAI SDK and Portkey Gateway Source: https://portkey.ai/docs/llms-full.txt This Python example demonstrates integrating Reka AI via the Portkey Gateway using the OpenAI SDK. Ensure 'openai' and 'portkey-ai' are installed, and the '@reka' provider is set up in your Model Catalog. ```python from openai import OpenAI from portkey_ai import PORTKEY_GATEWAY_URL # 1. Install: pip install openai portkey-ai # 2. Add @reka provider in model catalog # 3. Use it: client = OpenAI( api_key="PORTKEY_API_KEY", # Portkey API key base_url=PORTKEY_GATEWAY_URL ) response = client.chat.completions.create( model="@reka/reka-core", messages=[{"role": "user", "content": "Say this is a test"}] ) print(response.choices[0].message.content) ``` -------------------------------- ### Install and Run Node.js Sync Script Source: https://portkey.ai/docs/llms-full.txt Installs the required Node.js package and executes the `owui_to_portkey.mjs` script. It shows how to set the synchronization start time with `--since`, using a Node.js expression for calculation if the `date` command fails. ```bash npm install node-fetch # Save the script from the Node section as owui_to_portkey.mjs node owui_to_portkey.mjs --since=$(date -d '2 hours ago' +%s 2>/dev/null || node -e "console.log(Math.floor(Date.now() / 1000) - 7200)") ``` -------------------------------- ### Run OpenClaw Onboarding Wizard Source: https://portkey.ai/docs/integrations/libraries/openclaw Initiate the OpenClaw setup process by running the onboard command with the install-daemon flag. This guides you through initial configuration. ```bash openclaw onboard --install-daemon ``` -------------------------------- ### Configure and Run Plugins Source: https://portkey.ai/docs/guides/use-cases/creating-partner-guardrails Update your configuration file to enable the new plugin and then build and start your application. This example enables the 'default' and 'profanity-filter' plugins. ```json { "plugins_enabled": ["default", "profanity-filter"], "credentials": {}, "cache": false } ``` -------------------------------- ### Python Example with Exa Enabled Source: https://portkey.ai/docs/integrations/plugins Shows how to initialize the Portkey client in Python with a configuration that includes Exa search capabilities and make a chat completion request. ```python 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?"} ] ) ``` -------------------------------- ### Install OpenAI and Portkey SDKs for NodeJS Source: https://portkey.ai/docs/product/enterprise-offering/org-management/jwt Install the OpenAI and Portkey SDKs using npm. This setup is required for the NodeJS chat completions example that uses both libraries. ```bash npm install openai portkey-ai ``` -------------------------------- ### Portkey Python SDK Quickstart Source: https://portkey.ai/docs/api-reference/sdk/python A minimal example demonstrating how to initialize the Portkey client and make a chat completion request. You can use either your API key directly or the environment variable. ```python from portkey_ai import Portkey client = Portkey( api_key="your_api_key_here", # Or use the env var PORTKEY_API_KEY provider="@openai-prod" # Or use config="cf-***" ) response = client.chat.completions.create( messages=[{"role": "user", "content": "Hello, world!"}], model="gpt-4o" # Example provider/model ) ``` -------------------------------- ### Full Code Example with Initialization Source: https://portkey.ai/docs/guides/getting-started/return-repeat-requests-from-cache A complete JavaScript example demonstrating the initialization of Portkey and making both simple and semantic cached API calls. ```javascript import { Portkey } from 'portkey-ai'; const portkey = new Portkey({ apiKey: 'YOUR_PORTKEY_API_KEY', provider: '@openai-prod' }); let simpleCacheResponse = await portkey.chat.completions.create( { model: 'gpt-4', messages: [ { role: 'user', content: 'What are 7 wonders of the world?' } ] }, { config: JSON.stringify({ cache: { mode: 'simple' } }) } ); console.log('Simple Cached Response:\n', simpleCacheResponse.choices[0].message.content); let semanticCacheResponse = await portkey.chat.completions.create( { model: 'gpt-4', messages: [ { role: 'user', content: 'List the 5 senses of Human beings?' } ] }, { config: JSON.stringify({ cache: { mode: 'semantic' } }) } ); console.log('\nSemantically Cached Response:\n', semanticCacheResponse.choices[0].message.content); ``` -------------------------------- ### Quick Start with Voyage AI Embeddings Source: https://portkey.ai/docs/integrations/llms/voyage-ai Get started generating embeddings with Voyage AI models via Portkey. Ensure you have installed the portkey-ai library and added the @voyage provider in your model catalog. ```python from portkey_ai import Portkey # 1. Install: pip install portkey-ai # 2. Add @voyage provider in model catalog # 3. Use it: portkey = Portkey(api_key="PORTKEY_API_KEY") embedding = portkey.embeddings.create( model="@voyage/voyage-3", input="Name the tallest buildings in Hawaii" ) print(embedding.data[0].embedding) ``` ```javascript import Portkey from 'portkey-ai' // 1. Install: npm install portkey-ai // 2. Add @voyage provider in model catalog // 3. Use it: const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY" }) const embedding = await portkey.embeddings.create({ model: "@voyage/voyage-3", input: "Name the tallest buildings in Hawaii" }) console.log(embedding.data[0].embedding) ``` ```python from openai import OpenAI from portkey_ai import PORTKEY_GATEWAY_URL # 1. Install: pip install openai portkey-ai # 2. Add @voyage provider in model catalog # 3. Use it: client = OpenAI( api_key="PORTKEY_API_KEY", # Portkey API key base_url=PORTKEY_GATEWAY_URL ) embedding = client.embeddings.create( model="@voyage/voyage-3", input="Name the tallest buildings in Hawaii" ) print(embedding.data[0].embedding) ``` ```javascript import OpenAI from "openai" import { PORTKEY_GATEWAY_URL } from "portkey-ai" // 1. Install: npm install openai portkey-ai // 2. Add @voyage provider in model catalog // 3. Use it: const client = new OpenAI({ apiKey: "PORTKEY_API_KEY", // Portkey API key baseURL: PORTKEY_GATEWAY_URL }) const embedding = await client.embeddings.create({ model: "@voyage/voyage-3", input: "Name the tallest buildings in Hawaii" }) console.log(embedding.data[0].embedding) ``` ```bash # 1. Add @voyage provider in model catalog # 2. Use it: curl https://api.portkey.ai/v1/embeddings \ -H "Content-Type: application/json" \ -H "x-portkey-api-key: $PORTKEY_API_KEY" \ -d '{ "model": "@voyage/voyage-3", "input": "Name the tallest buildings in Hawaii" }' ``` -------------------------------- ### Quickstart: Create a Chat Completion Source: https://portkey.ai/docs/api-reference/sdk/node A minimal example demonstrating how to initialize the Portkey SDK and create a chat completion. Ensure you have a provider slug from the Model Catalog and your API key set as an environment variable. ```typescript import Portkey from 'portkey-ai'; const portkey = new Portkey({ apiKey: process.env.PORTKEY_API_KEY, provider: '@openai-prod' // Provider slug from Model Catalog }); const response = await portkey.chat.completions.create({ messages: [{ role: 'user', content: 'Hello, world!' }], model: 'gpt-4o' }); console.log(response.choices[0].message.content); ``` -------------------------------- ### Build and Start Application Source: https://portkey.ai/docs/guides/use-cases/creating-partner-guardrails Execute these npm commands to build your plugins and start the development server. ```bash npm run build-plugins && npm run dev ``` -------------------------------- ### JavaScript Quick Start with Deepbricks Source: https://portkey.ai/docs/llms-full.txt Utilize the Portkey JavaScript SDK for seamless integration with Deepbricks models. Installation and provider setup are required before use. ```javascript import Portkey from 'portkey-ai' // 1. Install: npm install portkey-ai // 2. Add @deepbricks provider in model catalog // 3. Use it: const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY" }) const response = await portkey.chat.completions.create({ model: "@deepbricks/deepseek-ai/DeepSeek-V2-Chat", messages: [{ role: "user", content: "Hello!" }] }) console.log(response.choices[0].message.content) ``` -------------------------------- ### Python Quick Start with OpenAI SDK, Portkey, and Groq Source: https://portkey.ai/docs/llms-full.txt This Python snippet demonstrates using the OpenAI SDK with Portkey as a gateway to Groq models. Ensure 'openai' and 'portkey-ai' are installed and the '@groq' provider is configured. ```python from openai import OpenAI from portkey_ai import PORTKEY_GATEWAY_URL # 1. Install: pip install openai portkey-ai # 2. Add @groq provider in model catalog # 3. Use it: client = OpenAI( api_key="PORTKEY_API_KEY", # Portkey API key base_url=PORTKEY_GATEWAY_URL ) response = client.chat.completions.create( model="@groq/llama-3.3-70b-versatile", messages=[{"role": "user", "content": "Hello!"}] ) print(response.choices[0].message.content) ``` -------------------------------- ### NodeJS Example with Exa Enabled Source: https://portkey.ai/docs/integrations/plugins Demonstrates how to initialize the Portkey client in NodeJS with a configuration that includes Exa search capabilities and make a chat completion request. ```javascript 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?" } ] }); ``` -------------------------------- ### Quick Start: Lepton AI Chat Completions (JavaScript) Source: https://portkey.ai/docs/integrations/llms/lepton Use this snippet to quickly get started with Lepton AI chat completions using the Portkey JavaScript SDK. Ensure you have installed the SDK and added the '@lepton' provider in your Portkey Model Catalog. ```javascript import Portkey from 'portkey-ai' // 1. Install: npm install portkey-ai // 2. Add @lepton provider in model catalog // 3. Use it: const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY" }) const response = await portkey.chat.completions.create({ model: "@lepton/llama-3.1-8b", messages: [{ role: "user", content: "Hello!" }] }) console.log(response.choices[0].message.content) ``` -------------------------------- ### Example Local Path for Pricing Files Source: https://portkey.ai/docs/llms-full.txt Example showing how to configure the local directory path where pricing JSON files are mounted on the backend. ```bash MODEL_CONFIGS_PRICING_LOCAL_PATH: /app/model-configs/pricing ``` -------------------------------- ### Quick Start: Lepton AI Chat Completions (Python) Source: https://portkey.ai/docs/integrations/llms/lepton Use this snippet to quickly get started with Lepton AI chat completions using the Portkey Python SDK. Ensure you have installed the SDK and added the '@lepton' provider in your Portkey Model Catalog. ```python from portkey_ai import Portkey # 1. Install: pip install portkey-ai # 2. Add @lepton provider in model catalog # 3. Use it: portkey = Portkey(api_key="PORTKEY_API_KEY") response = portkey.chat.completions.create( model="@lepton/llama-3.1-8b", messages=[{"role": "user", "content": "Hello!"}] ) print(response.choices[0].message.content) ``` -------------------------------- ### Quick Start: Lepton AI Chat Completions (OpenAI JavaScript) Source: https://portkey.ai/docs/integrations/llms/lepton Use this snippet to get started with Lepton AI chat completions using the OpenAI JavaScript client configured with Portkey. Ensure you have installed both SDKs and added the '@lepton' provider in your Portkey Model Catalog. ```javascript import OpenAI from "openai" import { PORTKEY_GATEWAY_URL } from "portkey-ai" // 1. Install: npm install openai portkey-ai // 2. Add @lepton provider in model catalog // 3. Use it: const client = new OpenAI({ apiKey: "PORTKEY_API_KEY", // Portkey API key baseURL: PORTKEY_GATEWAY_URL }) const response = await client.chat.completions.create({ model: "@lepton/llama-3.1-8b", messages: [{ role: "user", content: "Hello!" }] }) console.log(response.choices[0].message.content) ``` -------------------------------- ### Quick Start: Lepton AI Chat Completions (OpenAI Python) Source: https://portkey.ai/docs/integrations/llms/lepton Use this snippet to get started with Lepton AI chat completions using the OpenAI Python client configured with Portkey. Ensure you have installed both SDKs and added the '@lepton' provider in your Portkey Model Catalog. ```python from openai import OpenAI from portkey_ai import PORTKEY_GATEWAY_URL # 1. Install: pip install openai portkey-ai # 2. Add @lepton provider in model catalog # 3. Use it: client = OpenAI( api_key="PORTKEY_API_KEY", # Portkey API key base_url=PORTKEY_GATEWAY_URL ) response = client.chat.completions.create( model="@lepton/llama-3.1-8b", messages=[{"role": "user", "content": "Hello!"}] ) print(response.choices[0].message.content) ``` -------------------------------- ### Complete Example: Phoenix, Portkey, and OpenAI Integration Source: https://portkey.ai/docs/integrations/tracing-providers/phoenix A full working example demonstrating the integration of Phoenix, Portkey, and OpenAI, including configuration and instrumented LLM calls. ```python from phoenix.otel import register from openinference.instrumentation.openai import OpenAIInstrumentor import os from openai import OpenAI from portkey_ai import createHeaders # Step 1: Configure Portkey endpoint os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = "https://api.portkey.ai/v1/logs/otel" os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = "x-portkey-api-key=YOUR_PORTKEY_API_KEY" # Step 2: Register Phoenix and instrument OpenAI register(set_global_tracer_provider=False) OpenAIInstrumentor().instrument() # Step 3: Configure Portkey Gateway client = OpenAI( api_key="PORTKEY_API_KEY", base_url="https://api.portkey.ai/v1", default_headers=createHeaders( api_key="PORTKEY_API_KEY", provider="@openai-prod" ) ) # Step 4: Make instrumented calls response = client.chat.completions.create( messages=[ {"role": "system", "content": "You are a helpful AI assistant."}, {"role": "user", "content": "Explain how observability helps in production AI systems"} ], model="gpt-4o", temperature=0.7 ) print(response.choices[0].message.content) ``` -------------------------------- ### Quick Start: OpenAI JavaScript Client Source: https://portkey.ai/docs/integrations/llms/modal Utilize the OpenAI JavaScript client, pointing to the Portkey gateway URL, to communicate with Modal providers. Ensure necessary installations and Modal provider setup in Portkey. ```javascript import OpenAI from "openai" import { PORTKEY_GATEWAY_URL } from "portkey-ai" // 1. Install: npm install openai portkey-ai // 2. Add @modal provider in model catalog // 3. Use it: const client = new OpenAI({ apiKey: "PORTKEY_API_KEY", // Portkey API key baseURL: PORTKEY_GATEWAY_URL }) const response = await client.chat.completions.create({ model: "@modal/your-model-name", messages: [{ role: "user", content: "Hello!" }] }) console.log(response.choices[0].message.content) ``` -------------------------------- ### Python Quick Start with OpenAI and Portkey AI Source: https://portkey.ai/docs/llms-full.txt This example shows how to use the OpenAI Python library with Portkey AI for Workers AI models. Install both 'openai' and 'portkey-ai' libraries and set up the '@workers-ai' provider. ```python from openai import OpenAI from portkey_ai import PORTKEY_GATEWAY_URL # 1. Install: pip install openai portkey-ai # 2. Add @workers-ai provider in model catalog # 3. Use it: client = OpenAI( api_key="PORTKEY_API_KEY", # Portkey API key base_url=PORTKEY_GATEWAY_URL ) response = client.chat.completions.create( model="@workers-ai/@cf/meta/llama-3.2-3b-instruct", messages=[{"role": "user", "content": "Hello!"}] ) print(response.choices[0].message.content) ``` -------------------------------- ### Example Project-Level Configuration Source: https://portkey.ai/docs/integrations/mcp-clients/cursor An example of a `.cursor/mcp.json` file demonstrating how to configure multiple MCP servers for a project. This ensures project-specific tools are only available where needed. ```json { "mcpServers": { "linear": { "url": "https://mcp.portkey.ai/ws-abc123/linear-def456/mcp" }, "github": { "url": "https://mcp.portkey.ai/ws-abc123/github-ghi789/mcp" } } } ``` -------------------------------- ### Example Local File Paths Source: https://portkey.ai/docs/self-hosting/airgapped/model-pricing Illustrative paths for local pricing and general configuration files for a specific provider (e.g., OpenAI). ```bash pricing/openai.json general/openai.json ``` -------------------------------- ### Quick Start with Replicate via Portkey Source: https://portkey.ai/docs/integrations/llms/replicate Install the Portkey SDK and use it to proxy requests to Replicate. Ensure the Replicate provider is added to your model catalog. ```python from portkey_ai import Portkey # 1. Install: pip install portkey-ai # 2. Add @replicate provider in model catalog # 3. Use it: portkey = Portkey( api_key="PORTKEY_API_KEY", provider="@replicate" ) response = portkey.post( url="predictions", data={ "version": "MODEL_VERSION_ID", "input": {"prompt": "Hello, world!"} } ) print(response) ``` ```javascript import Portkey from 'portkey-ai' // 1. Install: npm install portkey-ai // 2. Add @replicate provider in model catalog // 3. Use it: const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", provider: "@replicate" }) const response = await portkey.post({ url: "predictions", data: { version: "MODEL_VERSION_ID", input: { prompt: "Hello, world!" } } }) console.log(response) ``` ```sh # 1. Add @replicate provider in model catalog # 2. Use it: curl https://api.portkey.ai/v1/predictions \ -H "Content-Type: application/json" \ -H "x-portkey-api-key: $PORTKEY_API_KEY" \ -d '{ "version": "MODEL_VERSION_ID", "input": {"prompt": "Hello, world!"} }' ``` -------------------------------- ### Quick Start with Lambda Labs using OpenAI SDK (JavaScript) Source: https://portkey.ai/docs/llms-full.txt This JavaScript example shows how to leverage the OpenAI SDK with Portkey to access Lambda Labs' models. Ensure 'openai' and 'portkey-ai' are installed, the '@lambda' provider is set up, and the Portkey gateway URL is correctly configured. ```javascript import OpenAI from "openai" import { PORTKEY_GATEWAY_URL } from "portkey-ai" // 1. Install: npm install openai portkey-ai // 2. Add @lambda provider in model catalog // 3. Use it: const client = new OpenAI({ apiKey: "PORTKEY_API_KEY", // Portkey API key baseURL: PORTKEY_GATEWAY_URL }) const response = await client.chat.completions.create({ model: "@lambda/llama3.1-8b-instruct", messages: [{ role: "user", content: "Hello!" }] }) console.log(response.choices[0].message.content) ``` -------------------------------- ### Install Dependencies for Portkey JWT Example Source: https://portkey.ai/docs/llms-full.txt Installs Node.js and the 'jose' library, which are required for the JWT generation and signing process. ```sh # Node 18+ recommended npm init -y npm install jose ``` -------------------------------- ### Example Local Path for Capabilities Files Source: https://portkey.ai/docs/llms-full.txt Example showing how to configure the local directory path where capability JSON files are mounted on the backend. ```bash MODEL_CONFIGS_CAPABILITIES_LOCAL_PATH: /app/model-configs/general ``` -------------------------------- ### Install OpenAI and Portkey SDKs for Python Source: https://portkey.ai/docs/product/enterprise-offering/org-management/jwt Install both the OpenAI and Portkey SDKs using pip. This is necessary for using the combined Python chat completions example. ```bash pip install openai portkey-ai ``` -------------------------------- ### Install Portkey and Arize Packages Source: https://portkey.ai/docs/integrations/tracing-providers/arize Install the necessary Python packages for Portkey AI and Arize Phoenix integration. Choose the appropriate Arize package based on your setup. ```bash pip install portkey-ai openinference-instrumentation-portkey arize-otel ``` ```bash pip install portkey-ai openinference-instrumentation-portkey arize-phoenix-otel ``` -------------------------------- ### Quick Start: Python Source: https://portkey.ai/docs/integrations/llms/modal Install the Portkey library and use it to create chat completions with a Modal provider. Ensure you have added the Modal provider to your Portkey Model Catalog. ```python from portkey_ai import Portkey # 1. Install: pip install portkey-ai # 2. Add @modal provider in model catalog # 3. Use it: portkey = Portkey(api_key="PORTKEY_API_KEY") response = portkey.chat.completions.create( model="@modal/your-model-name", messages=[{"role": "user", "content": "Hello!"}] ) print(response.choices[0].message.content) ``` -------------------------------- ### JavaScript Tool Calling Example Source: https://portkey.ai/docs/llms-full.txt Shows how to implement tool calling with DeepSeek in JavaScript using the Portkey SDK. This example requires the Portkey SDK to be installed and the API key to be set. ```javascript import Portkey from 'portkey-ai' const client = new Portkey({ apiKey: "PORTKEY_API_KEY", provider: "@deepseek" }) const tools = [{ type: "function", function: { name: "get_weather", description: "Get the current weather for a location", parameters: { type: "object", properties: { location: { type: "string", description: "City name" } }, required: ["location"] } } }] const response = await client.chat.completions.create({ model: "deepseek-chat", messages: [{ role: "user", content: "What's the weather in Tokyo?" }], tools: tools }) console.log(response.choices[0].message.tool_calls) ``` -------------------------------- ### Full Code Example with Retries Source: https://portkey.ai/docs/guides/getting-started/trigger-automatic-retries-on-llm-failures A complete example demonstrating how to import Portkey, instantiate the client, and make a chat completion call with retry configurations. ```javascript import { Portkey } from 'portkey-ai'; const portkey = new Portkey({ apiKey: 'YOUR_PORTKEY_API_KEY', provider: '@openai-prod' }); let response = await portkey.chat.completions.create( { model: 'gpt-4', messages: [ { role: 'user', content: 'What are 7 wonders of the world?' } ] }, { config: JSON.stringify({ retry: { attempts: 3 } }) } ); console.log(response.choices[0].message.content); ``` -------------------------------- ### Define Tools and Get Weather Forecast Source: https://portkey.ai/docs/guides/getting-started/function-calling Define available functions and their parameters for the LLM. This example shows how to set up the 'get_weather' function and make a request to get the weather for a specific location. ```javascript import Portkey from "portkey-ai"; const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", }); 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", }, unit: { type: "string", enum: ["celsius", "fahrenheit"] }, }, required: ["location"], }, }, }]; const response = await portkey.chat.completions.create({ model: "MODEL_NAME", messages: [ { role: "user", content: "What's the weather like in Delhi?" } ], tools, tool_choice: "auto", }); console.log(response.choices[0].message.tool_calls); ``` -------------------------------- ### Create MCP Server (Minimal Example) Source: https://portkey.ai/docs/api-reference/admin-api/control-plane/mcp-servers/create-mcp-server Use this example to create a new MCP Server with only the required fields. Ensure you provide a valid name and MCP integration ID. ```json { "name": "My MCP Server", "mcp_integration_id": "MCP_INTEGRATION_ID_OR_SLUG" } ``` -------------------------------- ### Main Application Flow Setup Source: https://portkey.ai/docs/integrations/agents/pydantic-ai Sets up the main application flow, including defining usage limits for LLM requests and initializing dependencies and usage tracking objects. ```python import datetime from pydantic_ai.usage import Usage, UsageLimits # 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() ``` -------------------------------- ### Multi-agent System Setup with Portkey Source: https://portkey.ai/docs/integrations/agents/pydantic-ai Integrate Portkey with a multi-agent system for unified tracing, token tracking, and cost management across multiple agents. This example outlines the setup and interaction flow. ```python import datetime from dataclasses import dataclass from typing import Literal from pydantic import BaseModel, Field from rich.prompt import Prompt ``` -------------------------------- ### Complete Portkey AI Observability Example Source: https://portkey.ai/docs/llms-full.txt This example shows the full setup for instrumenting OpenAI calls with Portkey AI and Phoenix. Ensure you have set the necessary environment variables for Portkey API keys and endpoint. ```python from phoenix.otel import register from openinference.instrumentation.openai import OpenAIInstrumentor import os from openai import OpenAI from portkey_ai import createHeaders # Step 1: Configure Portkey endpoint os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = "https://api.portkey.ai/v1/logs/otel" os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = "x-portkey-api-key=YOUR_PORTKEY_API_KEY" # Step 2: Register Phoenix and instrument OpenAI register(set_global_tracer_provider=False) OpenAIInstrumentor().instrument() # Step 3: Configure Portkey Gateway client = OpenAI( api_key="PORTKEY_API_KEY", base_url="https://api.portkey.ai/v1", default_headers=createHeaders( api_key="PORTKEY_API_KEY", provider="@openai-prod" ) ) # Step 4: Make instrumented calls response = client.chat.completions.create( messages=[ {"role": "system", "content": "You are a helpful AI assistant."}, {"role": "user", "content": "Explain how observability helps in production AI systems"} ], model="gpt-4o", temperature=0.7 ) print(response.choices[0].message.content) ``` -------------------------------- ### Complete MLflow and Portkey Integration Example Source: https://portkey.ai/docs/integrations/tracing-providers/ml-flow A full example demonstrating the setup and usage of MLflow tracing with the Portkey gateway for OpenAI LLM calls. This includes environment configuration, MLflow autologging, and client initialization. ```python import os import mlflow from openai import OpenAI from portkey_ai import createHeaders # Step 1: Configure Portkey endpoint os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = "https://api.portkey.ai/v1/logs/otel" os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = "x-portkey-api-key=YOUR_PORTKEY_API_KEY" os.environ["OTEL_EXPORTER_OTLP_TRACES_PROTOCOL"] = "http/protobuf" # Step 2: Enable MLflow instrumentation mlflow.openai.autolog() # Step 3: Configure Portkey Gateway client = OpenAI( api_key="PORTKEY_API_KEY", base_url="https://api.portkey.ai/v1", default_headers=createHeaders( api_key="PORTKEY_API_KEY", provider="@openai-prod" ) ) # Step 4: Make instrumented calls response = client.chat.completions.create( model="gpt-4o", messages=[ {"role": "system", "content": "You are a helpful AI assistant."}, {"role": "user", "content": "What are the benefits of observability in production AI systems?"} ] ) print(response.choices[0].message.content) ```