Skip to content

Commit

Permalink
Merge pull request #8 from MagnivOrg/6-track-requests-from-js-library
Browse files Browse the repository at this point in the history
6-track-requests-from-js-library
  • Loading branch information
abubakarsohail authored Oct 5, 2023
2 parents 2e381be + 9752cd5 commit e4160f9
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 27 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @jzone3
2 changes: 2 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ on:
workflows: ["CI"]
types:
- completed
branches:
- "main"
push:
branches:
- "main"
Expand Down
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";
16 changes: 16 additions & 0 deletions src/types/track-request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export interface TrackRequest {
api_key: string;
provider_type?: string;
function_name: string;
args?: unknown[];
kwargs?: Record<string, unknown>;
request_end_time: string;
request_start_time: string;
prompt_id?: number;
prompt_version?: number;
metadata?: Record<string, string>;
tags?: string[];
request_response?: Record<string, unknown>;
prompt_input_variables?: Record<string, string> | string[];
[k: string]: unknown;
}
32 changes: 32 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { TrackRequest } from "@/types";

const URL_API_PROMPTLAYER = "https://api.promptlayer.com";

const promptLayerApiRequest = async (body: TrackRequest) => {
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": {
"@/*": ["*"]
}
}
}

0 comments on commit e4160f9

Please sign in to comment.