Skip to content

Commit

Permalink
fix: config override
Browse files Browse the repository at this point in the history
  • Loading branch information
noble-varghese committed Dec 1, 2023
1 parent 011e546 commit b2ba4c2
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 27 deletions.
9 changes: 5 additions & 4 deletions src/apis/chatCompletions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { ApiResource } from "../apiResource";
import { APIPromise, RequestOptions } from "../baseClient";
import { CHAT_COMPLETE_API } from "../constants";
import { Stream } from "../streaming";
import { overrideParams } from "../utils";

import { overrideConfig } from "../utils";
import { createHeaders } from "./createHeaders";

export class Chat extends ApiResource {
completions: ChatCompletions = new ChatCompletions(this.client);
Expand Down Expand Up @@ -34,8 +34,9 @@ class ChatCompletions extends ApiResource {
): APIPromise<ChatCompletion> | APIPromise<Stream<ChatCompletion>> {
const body = _body
// If config is present then override it.
if (params){
this.client = overrideParams(this.client, params)
if (params) {
const config = overrideConfig(this.client.config, params.config)
this.client.customHeaders = { ...this.client.customHeaders, ...createHeaders({ ...params, config }) }
}
const stream = _body.stream ?? false
return this.post<ChatCompletion>(CHAT_COMPLETE_API, { body, ...opts, stream }) as
Expand Down
7 changes: 4 additions & 3 deletions src/apis/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { ApiResource } from "../apiResource";
import { APIPromise, RequestOptions } from "../baseClient";
import { TEXT_COMPLETE_API } from "../constants";
import { Stream } from "../streaming";
import { overrideParams } from "../utils";

import { overrideConfig } from "../utils";
import { createHeaders } from "./createHeaders";


export class Completions extends ApiResource {
Expand All @@ -32,7 +32,8 @@ export class Completions extends ApiResource {
const body = _body
// If config is present then override it.
if (params) {
this.client = overrideParams(this.client, params)
const config = overrideConfig(this.client.config, params.config)
this.client.customHeaders = { ...this.client.customHeaders, ...createHeaders({ ...params, config }) }
}
const stream = _body.stream ?? false
return this.post(TEXT_COMPLETE_API, { body, ...opts, stream }) as
Expand Down
9 changes: 8 additions & 1 deletion src/apis/embeddings.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { ApiClientInterface } from "portkey-ai/_types/generalTypes";
import { ModelParams } from "../_types/portkeyConstructs";
import { ApiResource } from "../apiResource";
import { APIPromise, RequestOptions } from "../baseClient";
import { EMBEDDINGS_API } from "../constants";

import { overrideConfig } from "../utils";
import { createHeaders } from "./createHeaders";

export interface EmbeddingsBody extends ModelParams {
input: string;
Expand All @@ -14,9 +16,14 @@ export type Embeddings = Record<string, any>
export class Prompt extends ApiResource {
create(
_body: EmbeddingsBody,
params?: ApiClientInterface,
opts?: RequestOptions
): APIPromise<Embeddings> {
const body = _body
if (params) {
const config = overrideConfig(this.client.config, params.config)
this.client.customHeaders = { ...this.client.customHeaders, ...createHeaders({ ...params, config }) }
}
const response = this.post<Embeddings>(EMBEDDINGS_API, { body, ...opts })
return response
}
Expand Down
14 changes: 11 additions & 3 deletions src/apis/feedback.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { FEEDBACK_API } from "portkey-ai/constants";
import { ApiClientInterface } from "../_types/generalTypes";
import { ApiResource } from "../apiResource";
import { APIPromise, RequestOptions } from "../baseClient";
import { FEEDBACK_API } from "../constants";
import { overrideConfig } from "../utils";
import { createHeaders } from "./createHeaders";

interface FeedbackBodyBase {
trace_id?: string;
value?: string;
weight?: string;
value?: number;
weight?: number;
metadata?: Record<string, any>
}

Expand All @@ -19,9 +22,14 @@ export interface FeedbackResponse {
export class Feedback extends ApiResource {
create(
_body: FeedbackBody,
params?: ApiClientInterface,
opts?: RequestOptions
): APIPromise<FeedbackResponse> {
const body = _body
if (params) {
const config = overrideConfig(this.client.config, params.config)
this.client.customHeaders = { ...this.client.customHeaders, ...createHeaders({ ...params, config }) }
}
const response = this.post<FeedbackResponse>(FEEDBACK_API, { body, ...opts })
return response
}
Expand Down
11 changes: 11 additions & 0 deletions src/apis/generations.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { ApiClientInterface } from "portkey-ai/_types/generalTypes";
import { ModelParams } from "../_types/portkeyConstructs";
import { ApiResource } from "../apiResource";
import { APIPromise, RequestOptions } from "../baseClient";
import { Stream } from "../streaming";
import { overrideConfig } from "../utils";
import { createHeaders } from "./createHeaders";

export class Generations extends ApiResource {
create(
Expand Down Expand Up @@ -44,20 +47,28 @@ type PromptsResponse = Record<string, any>;
export class Prompt extends ApiResource {
create(
_body: PromptsCreateNonStreaming,
params?: ApiClientInterface,
opts?: RequestOptions
): APIPromise<PromptsCreateNonStreaming>;
create(
_body: PromptsCreateStreaming,
params?: ApiClientInterface,
opts?: RequestOptions
): APIPromise<Stream<PromptsCreateStreaming>>;
create(
_body: PromptsCreateParams,
params?: ApiClientInterface,
opts?: RequestOptions,
): APIPromise<Stream<PromptsCreateStreaming> | PromptsCreateNonStreaming>;
create(
_body: PromptsCreateParams,
params?: ApiClientInterface,
opts?: RequestOptions
): APIPromise<PromptsResponse> | APIPromise<Stream<PromptsResponse>> {
if (params) {
const config = overrideConfig(this.client.config, params.config)
this.client.customHeaders = { ...this.client.customHeaders, ...createHeaders({ ...params, config }) }
}
const promptId = _body.promptId
const body = _body
const stream = _body.stream ?? false
Expand Down
12 changes: 11 additions & 1 deletion src/apis/postMethod.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@
import { ApiClientInterface } from "portkey-ai/_types/generalTypes";
import { ApiResource } from "../apiResource";
import { APIPromise, RequestOptions } from "../baseClient";
import { Stream } from "../streaming";

import { overrideConfig } from "../utils";
import { createHeaders } from "./createHeaders";

export class Post extends ApiResource {
create(
url: string,
_body: PostBodyNonStreaming,
params?: ApiClientInterface,
opts?: RequestOptions
): APIPromise<PostResponse>;
create(
url: string,
_body: PostBodyStreaming,
params?: ApiClientInterface,
opts?: RequestOptions
): APIPromise<Stream<PostResponse>>
create(
url: string,
_body: Record<string, any>,
params?: ApiClientInterface,
opts?: RequestOptions,
): APIPromise<Stream<PostResponse>> | APIPromise<PostResponse>
create(
url: string,
_body: PostBodyParams,
params?: ApiClientInterface,
opts?: RequestOptions
): APIPromise<PostResponse> | APIPromise<Stream<PostResponse>> {
const body = _body
if (params) {
const config = overrideConfig(this.client.config, params.config)
this.client.customHeaders = { ...this.client.customHeaders, ...createHeaders({ ...params, config }) }
}
const response = this.post<PostResponse>(url, { body, ...opts }) as
| APIPromise<PostResponse>
| APIPromise<Stream<PostResponse>>
Expand Down
2 changes: 1 addition & 1 deletion src/baseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ export abstract class ApiClient {
protected defaultHeaders(): Record<string, string> {
return {
"Content-Type": "application/json",
[`${PORTKEY_HEADER_PREFIX}api-key`]: this.apiKey || "",
[`${PORTKEY_HEADER_PREFIX}package-version`]: `portkey-${VERSION}`,
...getPlatformProperties()
}
Expand Down Expand Up @@ -167,6 +166,7 @@ export abstract class ApiClient {
const reqHeaders: Record<string, string> = {
...this.defaultHeaders(), ...this.customHeaders,
};

const httpAgent: Agent | undefined = defaultHttpAgent
const req: RequestInit = {
method,
Expand Down
14 changes: 0 additions & 14 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { ApiClientInterface } from "./_types/generalTypes";
import { Portkey } from "./client";
import { PORTKEY_HEADER_PREFIX } from "./constants";

type PlatformProperties = {
Expand Down Expand Up @@ -45,15 +43,3 @@ export const overrideConfig = (initialConfig?: Config, updatedConfig?: Config):
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 b2ba4c2

Please sign in to comment.