Skip to content

Commit

Permalink
Merge branch 'main' into importMiddleWareSigned
Browse files Browse the repository at this point in the history
  • Loading branch information
MuraraAllan authored Aug 26, 2024
2 parents a5327bf + df9557b commit 853b23b
Show file tree
Hide file tree
Showing 65 changed files with 1,248 additions and 116 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ Gateway seamlessly integrates with popular agent frameworks. [Read the documenta
|------------------------------|--------|-------------|---------|------|---------------|-------------------|
| [Autogen](https://docs.portkey.ai/docs/welcome/agents/autogen) |||||||
| [CrewAI](https://docs.portkey.ai/docs/welcome/agents/crewai) |||||||
| [LangChain](https://docs.portkey.ai/docs/welcome/agents/langchain-agents) |||||||
| [Phidata](https://docs.portkey.ai/docs/welcome/agents/phidata) |||||||
| [Llama Index](https://docs.portkey.ai/docs/welcome/agents/llama-agents) |||||||
| [Control Flow](https://docs.portkey.ai/docs/welcome/agents/control-flow) |||||||
Expand Down
5 changes: 0 additions & 5 deletions jest.config.cjs

This file was deleted.

8 changes: 8 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/** @type {import('ts-jest').JestConfigWithTsJest} **/
export default {
testEnvironment: 'node',
transform: {
'^.+.tsx?$': ['ts-jest', {}],
},
testTimeout: 30000, // Set default timeout to 30 seconds
};
88 changes: 81 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@portkey-ai/gateway",
"version": "1.6.1",
"version": "1.7.0",
"description": "A fast AI gateway by Portkey",
"repository": {
"type": "git",
Expand Down Expand Up @@ -60,7 +60,7 @@
"jest": "^29.7.0",
"prettier": "3.2.5",
"rollup": "^4.9.1",
"ts-jest": "^29.1.2",
"ts-jest": "^29.2.4",
"tsx": "^4.7.0",
"typescript-eslint": "^8.1.0",
"wrangler": "^3.48.0"
Expand Down
2 changes: 1 addition & 1 deletion plugins/aporia/validateProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
PluginHandler,
PluginParameters,
} from '../types';
import { getText, post } from '../utils';
import { post } from '../utils';

export const APORIA_BASE_URL = 'https://gr-prd.aporia.com';

Expand Down
9 changes: 9 additions & 0 deletions src/errors/RouterError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export class RouterError extends Error {
constructor(
message: string,
public cause?: Error
) {
super(message);
this.name = 'RouterError';
}
}
8 changes: 8 additions & 0 deletions src/globals.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { endpointStrings } from './providers/types';

export const POWERED_BY: string = 'portkey';

export const HEADER_KEYS: Record<string, string> = {
Expand All @@ -6,6 +8,7 @@ export const HEADER_KEYS: Record<string, string> = {
PROVIDER: `x-${POWERED_BY}-provider`,
TRACE_ID: `x-${POWERED_BY}-trace-id`,
CACHE: `x-${POWERED_BY}-cache`,
METADATA: `x-${POWERED_BY}-metadata`,
FORWARD_HEADERS: `x-${POWERED_BY}-forward-headers`,
CUSTOM_HOST: `x-${POWERED_BY}-custom-host`,
REQUEST_TIMEOUT: `x-${POWERED_BY}-request-timeout`,
Expand Down Expand Up @@ -104,3 +107,8 @@ export const CONTENT_TYPES = {
HTML: 'text/html',
GENERIC_IMAGE_PATTERN: 'image/',
};

export const MULTIPART_FORM_DATA_ENDPOINTS: endpointStrings[] = [
'createTranscription',
'createTranslation',
];
13 changes: 11 additions & 2 deletions src/handlers/chatCompletionsHandler.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { RouterError } from '../errors/RouterError';
import {
constructConfigFromRequestHeaders,
tryTargetsRecursively,
Expand Down Expand Up @@ -30,13 +31,21 @@ export async function chatCompletionsHandler(c: Context): Promise<Response> {
return tryTargetsResponse;
} catch (err: any) {
console.log('chatCompletion error', err.message);
let statusCode = 500;
let errorMessage = 'Something went wrong';

if (err instanceof RouterError) {
statusCode = 400;
errorMessage = err.message;
}

return new Response(
JSON.stringify({
status: 'failure',
message: 'Something went wrong',
message: errorMessage,
}),
{
status: 500,
status: statusCode,
headers: {
'content-type': 'application/json',
},
Expand Down
9 changes: 9 additions & 0 deletions src/handlers/completionsHandler.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { RouterError } from '../errors/RouterError';
import {
constructConfigFromRequestHeaders,
tryTargetsRecursively,
Expand Down Expand Up @@ -31,6 +32,14 @@ export async function completionsHandler(c: Context): Promise<Response> {
return tryTargetsResponse;
} catch (err: any) {
console.log('completion error', err.message);
let statusCode = 500;
let errorMessage = 'Something went wrong';

if (err instanceof RouterError) {
statusCode = 400;
errorMessage = err.message;
}

return new Response(
JSON.stringify({
status: 'failure',
Expand Down
46 changes: 46 additions & 0 deletions src/handlers/createSpeechHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {
constructConfigFromRequestHeaders,
tryTargetsRecursively,
} from './handlerUtils';
import { Context } from 'hono';

/**
* Handles the '/audio/speech' API request by selecting the appropriate provider(s) and making the request to them.
*
* @param {Context} c - The Cloudflare Worker context.
* @returns {Promise<Response>} - The response from the provider.
* @throws Will throw an error if no provider options can be determined or if the request to the provider(s) fails.
* @throws Will throw an 500 error if the handler fails due to some reasons
*/
export async function createSpeechHandler(c: Context): Promise<Response> {
try {
let request = await c.req.json();
let requestHeaders = Object.fromEntries(c.req.raw.headers);
const camelCaseConfig = constructConfigFromRequestHeaders(requestHeaders);
const tryTargetsResponse = await tryTargetsRecursively(
c,
camelCaseConfig ?? {},
request,
requestHeaders,
'createSpeech',
'POST',
'config'
);

return tryTargetsResponse;
} catch (err: any) {
console.log('createSpeech error', err.message);
return new Response(
JSON.stringify({
status: 'failure',
message: 'Something went wrong',
}),
{
status: 500,
headers: {
'content-type': 'application/json',
},
}
);
}
}
48 changes: 48 additions & 0 deletions src/handlers/createTranscriptionHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {
constructConfigFromRequestHeaders,
tryTargetsRecursively,
} from './handlerUtils';
import { Context } from 'hono';

/**
* Handles the '/audio/transcriptions' API request by selecting the appropriate provider(s) and making the request to them.
*
* @param {Context} c - The Cloudflare Worker context.
* @returns {Promise<Response>} - The response from the provider.
* @throws Will throw an error if no provider options can be determined or if the request to the provider(s) fails.
* @throws Will throw an 500 error if the handler fails due to some reasons
*/
export async function createTranscriptionHandler(
c: Context
): Promise<Response> {
try {
let request = await c.req.raw.formData();
let requestHeaders = Object.fromEntries(c.req.raw.headers);
const camelCaseConfig = constructConfigFromRequestHeaders(requestHeaders);
const tryTargetsResponse = await tryTargetsRecursively(
c,
camelCaseConfig ?? {},
request,
requestHeaders,
'createTranscription',
'POST',
'config'
);

return tryTargetsResponse;
} catch (err: any) {
console.log('createTranscription error', err.message);
return new Response(
JSON.stringify({
status: 'failure',
message: 'Something went wrong',
}),
{
status: 500,
headers: {
'content-type': 'application/json',
},
}
);
}
}
Loading

0 comments on commit 853b23b

Please sign in to comment.