Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

6-track-requests-from-js-library #8

Merged
Show file tree
Hide file tree
Changes from 4 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
27 changes: 1 addition & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1 @@
import { Configuration, OpenAIApi } from "openai";

const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const target = new OpenAIApi(configuration);

export const openai = new Proxy(target, {
get: (target, prop, receiver) => {
const value = target[prop as keyof OpenAIApi];
if (typeof value === "function") {
return (...args: any[]) => {
let return_pl_id = false;
const newArgs = args.map((arg) => {
if (arg["return_pl_id"] !== undefined) {
return_pl_id = arg["return_pl_id"];
delete arg["return_pl_id"];
}
return arg;
});
return (value as any).apply(target, args);
};
}
return Reflect.get(target, prop, receiver);
},
});
export * as utils from "@/utils";
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { TrackRequest } from "@/types/track-request";
40 changes: 40 additions & 0 deletions src/types/track-request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
export type ApiKey = string;
export type ProviderType = "openai" | "anthropic" | "langchain";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we just make this a string?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jzone3 the types in this file are coming straight from the pydantic models on backend. If we want to change something, we should do it on backend.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not true. You can track requests with cohere for example today.

This is also a library, not the backend. There's a difference

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it @jzone3 will update it.

export type FunctionName = string;
export type Args = unknown[];
export type RequestEndTime = string;
export type RequestStartTime = string;
export type PromptId = number;
export type PromptVersion = number;
export type Tags = string[];
export type PromptInputVariables =
| {
[k: string]: string;
}
| string[];
jzone3 marked this conversation as resolved.
Show resolved Hide resolved

export interface TrackRequest {
api_key: ApiKey;
provider_type?: ProviderType;
function_name: FunctionName;
args?: Args;
kwargs?: Kwargs;
request_end_time: RequestEndTime;
request_start_time: RequestStartTime;
prompt_id?: PromptId;
prompt_version?: PromptVersion;
metadata?: Metadata;
tags?: Tags;
request_response?: RequestResponse;
prompt_input_variables?: PromptInputVariables;
[k: string]: unknown;
}
export interface Kwargs {
[k: string]: unknown;
}
export interface Metadata {
[k: string]: string;
}
export interface RequestResponse {
[k: string]: unknown;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this on purpose?

}
jzone3 marked this conversation as resolved.
Show resolved Hide resolved
33 changes: 33 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { TrackRequest } from "@/types";

const URL_API_PROMPTLAYER =
jzone3 marked this conversation as resolved.
Show resolved Hide resolved
process.env.URL_API_PROMPTLAYER || "https://api.promptlayer.com";

const promptlayerApiRequest = async (body: TrackRequest) => {
jzone3 marked this conversation as resolved.
Show resolved Hide resolved
jzone3 marked this conversation as resolved.
Show resolved Hide resolved
try {
const response = await fetch(`${URL_API_PROMPTLAYER}/track-request`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body),
});
const data = await response.json();
if (response.status !== 200) {
console.warn(
`WARNING: While logging your request PromptLayer had the following error: ${JSON.stringify(
data
)}`
);
}
if (data && body.return_pl_id) {
return data.request_id;
}
} catch (e) {
console.warn(
`WARNING: While logging your request PromptLayer had the following error: ${e}`
);
}
};

export { promptlayerApiRequest };
6 changes: 5 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@
/* Completeness */
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */,
"noEmit": true
"noEmit": true,
"baseUrl": "./src",
"paths": {
"@/*": ["*"]
}
}
}