-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from MagnivOrg/6-track-requests-from-js-library
6-track-requests-from-js-library
- Loading branch information
Showing
7 changed files
with
58 additions
and
27 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* @jzone3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ on: | |
workflows: ["CI"] | ||
types: | ||
- completed | ||
branches: | ||
- "main" | ||
push: | ||
branches: | ||
- "main" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { TrackRequest } from "@/types/track-request"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters