Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
177 changes: 177 additions & 0 deletions cookbook/providers/aibadgr.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Portkey + AI Badgr\n",
"\n",
"[Portkey](https://app.portkey.ai/) is the Control Panel for AI apps. With its popular AI Gateway and Observability Suite, hundreds of teams ship reliable, cost-efficient, and fast apps."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Use Budget-Friendly AI Badgr API with OpenAI Compatibility using Portkey!\n",
"\n",
"AI Badgr is a budget/utility OpenAI-compatible provider that offers tier-based model access:\n",
"- **basic**: Budget-tier models for simple tasks\n",
"- **normal**: Standard-tier models for general use\n",
"- **premium**: High-quality models for complex tasks\n",
"\n",
"Since Portkey is fully compatible with the OpenAI signature, you can connect to the Portkey AI Gateway through the OpenAI Client.\n",
"\n",
"- Set the `base_url` as `PORTKEY_GATEWAY_URL`\n",
"- Add `default_headers` to consume the headers needed by Portkey using the `createHeaders` helper method."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You will need Portkey and AI Badgr API keys to run this notebook.\n",
"\n",
"- Sign up for [Portkey here](https://app.portkey.ai/signup) and generate your API key.\n",
"- Get your AI Badgr API key from [AI Badgr](https://aibadgr.com)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!pip install -qU portkey-ai openai"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## With OpenAI Client"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from openai import OpenAI\n",
"from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders\n",
"\n",
"client = OpenAI(\n",
" base_url=PORTKEY_GATEWAY_URL,\n",
" default_headers=createHeaders(\n",
" provider=\"aibadgr\",\n",
" api_key=\"YOUR_AIBADGR_API_KEY\"\n",
" )\n",
")\n",
"\n",
"chat_completion = client.chat.completions.create(\n",
" messages=[{\"role\": \"user\", \"content\": \"What is the meaning of life?\"}],\n",
" model=\"premium\" # Use tier names: basic, normal, or premium\n",
")\n",
"\n",
"print(chat_completion.choices[0].message.content)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## With Portkey Client"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from portkey_ai import Portkey\n",
"\n",
"portkey = Portkey(\n",
" api_key=\"YOUR_PORTKEY_API_KEY\",\n",
" provider=\"aibadgr\",\n",
" Authorization=\"YOUR_AIBADGR_API_KEY\"\n",
")\n",
"\n",
"completion = portkey.chat.completions.create(\n",
" messages=[{\"role\": \"user\", \"content\": \"Explain quantum computing in simple terms\"}],\n",
" model=\"premium\"\n",
")\n",
"\n",
"print(completion.choices[0].message.content)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Streaming Responses"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"stream = portkey.chat.completions.create(\n",
" messages=[{\"role\": \"user\", \"content\": \"Write a short poem about AI\"}],\n",
" model=\"premium\",\n",
" stream=True\n",
")\n",
"\n",
"for chunk in stream:\n",
" if chunk.choices[0].delta.content:\n",
" print(chunk.choices[0].delta.content, end=\"\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Model Tiers\n",
"\n",
"AI Badgr provides tier-based model access optimized for different use cases:\n",
"\n",
"- **basic**: Budget-tier models optimized for cost and speed\n",
"- **normal**: Standard-tier models balancing performance and cost\n",
"- **premium**: High-quality models for complex reasoning and tasks\n",
"\n",
"OpenAI model names are also accepted and automatically mapped to the appropriate tier."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Observability with Portkey\n",
"\n",
"By routing requests through Portkey you can track metrics like:\n",
"- Token usage and costs\n",
"- Request latency\n",
"- Success/error rates\n",
"\n",
"View all your analytics at [Portkey Dashboard](https://app.portkey.ai/)."
]
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
2 changes: 2 additions & 0 deletions src/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export const NEXTBIT: string = 'nextbit';
export const MODAL: string = 'modal';
export const Z_AI: string = 'z-ai';
export const IO_INTELLIGENCE: string = 'iointelligence';
export const AIBADGR: string = 'aibadgr';

export const VALID_PROVIDERS = [
ANTHROPIC,
Expand Down Expand Up @@ -183,6 +184,7 @@ export const VALID_PROVIDERS = [
MODAL,
Z_AI,
IO_INTELLIGENCE,
AIBADGR,
];

export const CONTENT_TYPES = {
Expand Down
18 changes: 18 additions & 0 deletions src/providers/aibadgr/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ProviderAPIConfig } from '../types';

const AIBadgrAPIConfig: ProviderAPIConfig = {
getBaseURL: () => 'https://aibadgr.com/api/v1',
headers: ({ providerOptions }) => {
return { Authorization: `Bearer ${providerOptions.apiKey}` };
},
getEndpoint: ({ fn }) => {
switch (fn) {
case 'chatComplete':
return '/chat/completions';
default:
return '';
}
},
};

export default AIBadgrAPIConfig;
61 changes: 61 additions & 0 deletions src/providers/aibadgr/chatComplete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { AIBADGR } from '../../globals';

interface AIBadgrStreamChunk {
id: string;
object: string;
created: number;
model: string;
choices: {
index: number;
delta: {
role?: string;
content?: string;
tool_calls?: object[];
};
finish_reason: string | null;
}[];
usage?: {
prompt_tokens: number;
completion_tokens: number;
total_tokens: number;
};
}

export const AIBadgrChatCompleteStreamChunkTransform = (
responseChunk: string
) => {
let chunk = responseChunk.trim();
chunk = chunk.replace(/^data: /, '');
chunk = chunk.trim();

if (chunk === '[DONE]') {
return `data: ${chunk}\n\n`;
}

try {
const parsedChunk: AIBadgrStreamChunk = JSON.parse(chunk);
return (
`data: ${JSON.stringify({
id: parsedChunk.id,
object: parsedChunk.object,
created: parsedChunk.created,
model: parsedChunk.model,
provider: AIBADGR,
choices: parsedChunk.choices.map((choice) => ({
index: choice.index,
delta: choice.delta,
finish_reason: choice.finish_reason,
})),
usage: parsedChunk.usage,
})}` + '\n\n'
);
} catch (error) {
console.error(
'Error parsing AI Badgr stream chunk:',
error,
'Chunk:',
chunk
);
return `data: ${chunk}\n\n`;
}
};
18 changes: 18 additions & 0 deletions src/providers/aibadgr/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ProviderConfigs } from '../types';
import AIBadgrAPIConfig from './api';
import { AIBadgrChatCompleteStreamChunkTransform } from './chatComplete';
import { chatCompleteParams, responseTransformers } from '../open-ai-base';
import { AIBADGR } from '../../globals';

const AIBadgrConfig: ProviderConfigs = {
api: AIBadgrAPIConfig,
chatComplete: chatCompleteParams([]),
responseTransforms: {
...responseTransformers(AIBADGR, {
chatComplete: true,
}),
'stream-chatComplete': AIBadgrChatCompleteStreamChunkTransform,
},
};

export default AIBadgrConfig;
2 changes: 2 additions & 0 deletions src/providers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import ZAIConfig from './z-ai';
import MatterAIConfig from './matterai';
import ModalConfig from './modal';
import IOIntelligenceConfig from './iointelligence';
import AIBadgrConfig from './aibadgr';

const Providers: { [key: string]: ProviderConfigs } = {
openai: OpenAIConfig,
Expand Down Expand Up @@ -142,6 +143,7 @@ const Providers: { [key: string]: ProviderConfigs } = {
modal: ModalConfig,
'z-ai': ZAIConfig,
iointelligence: IOIntelligenceConfig,
aibadgr: AIBadgrConfig,
};

export default Providers;