-
Notifications
You must be signed in to change notification settings - Fork 4
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
abubakarsohail
merged 8 commits into
4-proxy-openai-requests-from-js-library
from
6-track-requests-from-js-library
Oct 5, 2023
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
abee951
fix: track requests from js library #6
abubakarsohail 8a2a3fd
export utils
abubakarsohail 7fd1e79
only run publish on main branch
abubakarsohail 786ccbb
revert github action
abubakarsohail b334e3a
only run publish on main branch
abubakarsohail 8427dc3
fix: track requests from js library #6
abubakarsohail 59c1008
added @jzone3 as code owner
abubakarsohail 9752cd5
accept provider_type to be any string
abubakarsohail File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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,40 @@ | ||
export type ApiKey = string; | ||
export type ProviderType = "openai" | "anthropic" | "langchain"; | ||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
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,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 }; |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.