Skip to content

Commit

Permalink
feat: over-ride of the params in chat and completions route
Browse files Browse the repository at this point in the history
  • Loading branch information
noble-varghese committed Dec 1, 2023
1 parent 632c3b4 commit 87f8fd9
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 23 deletions.
8 changes: 4 additions & 4 deletions src/_types/generalTypes.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export type Headers = Record<string, string | null | undefined>

export interface ApiClientInterface {
apiKey: string | null;
baseURL: string | null;
virtualKey: string | null | undefined;
config: Record<string, any> | string | null | undefined;
apiKey?: string | null;
baseURL?: string | null;
virtualKey?: string | null | undefined;
config?: Record<string, any> | string | null | undefined;
provider?: string | null | undefined;
traceId?: string | null | undefined;
metadata?: string | null | undefined;
Expand Down
12 changes: 9 additions & 3 deletions src/apis/chatCompletions.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { ApiClientInterface } from "../_types/generalTypes";
import { ModelParams } from "../_types/portkeyConstructs";
import { ApiResource } from "../apiResource";
import { APIPromise, RequestOptions } from "../baseClient";
import { CHAT_COMPLETE_API } from "../constants";
import { Stream } from "../streaming";
import { overrideConfig } from "../utils";
import { overrideParams } from "../utils";


export class Chat extends ApiResource {
Expand All @@ -13,24 +14,29 @@ export class Chat extends ApiResource {
class ChatCompletions extends ApiResource {
create(
_body: ChatCompletionsBodyNonStreaming,
params?: ApiClientInterface,
opts?: RequestOptions
): APIPromise<ChatCompletion>;
create(
_body: ChatCompletionsBodyStreaming,
params?: ApiClientInterface,
opts?: RequestOptions
): APIPromise<Stream<ChatCompletion>>;
create(
_body: ChatCompletionsBodyBase,
params?: ApiClientInterface,
opts?: RequestOptions,
): APIPromise<Stream<ChatCompletion> | ChatCompletion>;
create(
_body: ChatCompletionCreateParams,
params?: ApiClientInterface,
opts?: RequestOptions
): APIPromise<ChatCompletion> | APIPromise<Stream<ChatCompletion>> {
const body = _body
// If config is present then override it.
this.client.config = overrideConfig(this.client.config, opts?.config)
delete opts?.config
if (params){
this.client = overrideParams(this.client, params)
}
const stream = _body.stream ?? false
return this.post<ChatCompletion>(CHAT_COMPLETE_API, { body, ...opts, stream }) as
| APIPromise<ChatCompletion>
Expand Down
12 changes: 9 additions & 3 deletions src/apis/completions.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@
import { ApiClientInterface } from "../_types/generalTypes";
import { ModelParams } from "../_types/portkeyConstructs";
import { ApiResource } from "../apiResource";
import { APIPromise, RequestOptions } from "../baseClient";
import { TEXT_COMPLETE_API } from "../constants";
import { Stream } from "../streaming";
import { overrideConfig } from "../utils";
import { overrideParams } from "../utils";



export class Completions extends ApiResource {
create(
_body: CompletionsBodyNonStreaming,
params?: ApiClientInterface,
opts?: RequestOptions
): APIPromise<TextCompletion>;
create(
_body: CompletionsBodyStreaming,
params?: ApiClientInterface,
opts?: RequestOptions
): APIPromise<Stream<TextCompletion>>
create(
_body: CompletionsBodyBase,
params?: ApiClientInterface,
opts?: RequestOptions,
): APIPromise<Stream<TextCompletion> | TextCompletion>;
create(
_body: CompletionCreateParams,
params?: ApiClientInterface,
opts?: RequestOptions
): APIPromise<TextCompletion> | APIPromise<Stream<TextCompletion>> {
const body = _body
// If config is present then override it.
this.client.config = overrideConfig(this.client.config, opts?.config)
delete opts?.config
if (params) {
this.client = overrideParams(this.client, params)
}
const stream = _body.stream ?? false
return this.post(TEXT_COMPLETE_API, { body, ...opts, stream }) as
| APIPromise<TextCompletion>
Expand Down
5 changes: 2 additions & 3 deletions src/baseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export type RequestOptions = {
headers?: Headers | undefined;
httpAgent?: Agent;
stream?: boolean | undefined;
config?: Record<string, any>
}

type APIResponseProps = {
Expand Down Expand Up @@ -104,8 +103,8 @@ export abstract class ApiClient {

private fetch: Fetch;
constructor({ apiKey, baseURL, config, virtualKey, traceId, metadata, provider }: ApiClientInterface) {
this.apiKey = apiKey;
this.baseURL = baseURL || "";
this.apiKey = apiKey ?? "";
this.baseURL = baseURL ?? "";
this.customHeaders = createHeaders({ apiKey, config, virtualKey, traceId, metadata, provider })
this.fetch = fetch;
}
Expand Down
17 changes: 7 additions & 10 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
import { ApiClientInterface } from "./_types/generalTypes";
import * as Types from "./_types/portkeyConstructs";
import * as API from "./apis";
import { ApiClient } from "./baseClient";
import { MISSING_API_KEY_ERROR_MESSAGE, PORTKEY_BASE_URL } from "./constants";
import { castToError, readEnv } from "./utils";

interface ApiClientInterface {
apiKey?: string | null;
baseURL?: string | null;
virtualKey?: string | null;
config?: Record<string, any> | string | null;
provider?: string | null;
traceId?: string | null;
metadata?: string | null;
}

export class Portkey extends ApiClient {
override apiKey: string | null;
override baseURL: string;
virtualKey: string | null;
config: Record<string, any> | string | null | undefined;
provider: string | null | undefined;
traceId: string | null | undefined;
metadata: string | null | undefined;
constructor({
apiKey = readEnv("PORTKEY_API_KEY") ?? null,
baseURL = readEnv("PORTKEY_BASE_URL") ?? null,
Expand All @@ -45,6 +39,9 @@ export class Portkey extends ApiClient {
this.virtualKey = virtualKey || null
this.config = config || null
this.baseURL = baseURL || PORTKEY_BASE_URL;
this.provider = provider
this.traceId = traceId
this.metadata = metadata
}

protected constructLlms(llms?: Array<Types.LLMOptions> | null): Array<Types.LLMOptions> | null {
Expand Down
13 changes: 13 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ApiClientInterface } from "./_types/generalTypes";
import { Portkey } from "./client";
import { PORTKEY_HEADER_PREFIX } from "./constants";

type PlatformProperties = {
Expand Down Expand Up @@ -42,5 +44,16 @@ export const overrideConfig = (initialConfig?: Config, updatedConfig?: Config):
if (typeof initialConfig === "object" && typeof updatedConfig === "object") {
return { ...initialConfig, ...updatedConfig }
}
}


export const overrideParams = (client: Portkey, params: ApiClientInterface): Portkey => {
client.config = overrideConfig(client.config, params.config)
client.apiKey = params.apiKey ?? client.apiKey
client.baseURL = params.baseURL ?? client.baseURL
client.virtualKey = params.virtualKey ?? client.virtualKey
client.provider = params.provider ?? client.provider
client.traceId = params.apiKey ?? client.apiKey
client.apiKey = params.apiKey ?? client.apiKey
return client
}

0 comments on commit 87f8fd9

Please sign in to comment.