Portkey offers native integrations with Upstage 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.
Upstage Documentation
Explore the official Upstage documentation for comprehensive details on their APIs and models.
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 to control your API usage.Use the Portkey app to create a virtual key associated with your Upstage API key.
3
Initialize the Portkey Client
Now that you have your virtual key, set up the Portkey client:
Use the Portkey API key and the Upstage virtual key to initialize the client in your preferred programming language.
from portkey_ai import Portkeyportkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Upstage)
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})
Alternatively, use Portkey’s Open Source AI Gateway to enhance your app’s reliability with minimal code:
from portkey_ai import Portkey, PORTKEY_GATEWAY_URLportkey = 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")
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.
Generate chat completions using Upstage models through Portkey:
completion = portkey.chat.completions.create( messages=[{"role": "user", "content": "Say this is a test"}], model="solar-pro")print(completion.choices[0].message.content)
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 -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" }'
Stream responses for real-time output in your applications:
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)
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 -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 }'
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:
response = portkey.chat.completions.create( model="solar-pro", messages=[{"role": "user", "content": "Say this is a test"}], user="user_123456")
const chatCompletion = await portkey.chat.completions.create({ messages: [{ role: "user", content: "Say this is a test" }], model: "solar-pro", user: "user_12345",});
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: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.
Learn More About Metadata
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:
1
Create a 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).
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.
3
Set Up the Portkey Client
Pass the Gateway configuration to your Portkey client. You can either use the config object or the Config ID from Portkey’s hosted version.
from portkey_ai import Portkeyportkey = Portkey( api_key="PORTKEY_API_KEY", virtual_key="VIRTUAL_KEY", config=portkey_config)
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:
Load Balancing
Distribute requests across multiple targets based on defined weights.
Fallbacks
Automatically switch to backup targets if the primary target fails.
Conditional Routing
Route requests to different targets based on specified conditions.
Caching
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: