Skip to content

Instantly share code, notes, and snippets.

@aroman
Last active June 9, 2023 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aroman/7973a45185a27e288b8d20e0460665b1 to your computer and use it in GitHub Desktop.
Save aroman/7973a45185a27e288b8d20e0460665b1 to your computer and use it in GitHub Desktop.
PromptLayer.init(self.env.PROMPTLAYER_API_KEY);
PromptLayer.beginRequest({
engine: "gpt-4",
functionName: 'openai.ChatCompletion.create',
messages: [{role: "user", message: "how are you?"]
});
// Actually call the ChatGPT API
const response = await fetch(
'https://api.openai.com/v1/chat/completions',
requestOptions
);
responseData = await response.json();
// Automatically captures start and end time
PromptLayer.endRequest(responseData);
const API_BASE_URL = 'https://api.promptlayer.com';
interface OpenAICompletionKwargs {
functionName: 'openai.Completion.create';
engine: string;
prompt: string | string[];
}
interface OpenAIChatCompletionKwargs {
functionName: 'openai.ChatCompletion.create';
engine: string;
messages: {
content: string;
role: string;
name?: string;
}[];
}
type Kwargs = OpenAICompletionKwargs | OpenAIChatCompletionKwargs;
interface PromptPlayerTrackRequest {
api_key: string;
function_name: string;
kwargs: Kwargs;
request_response?: unknown;
request_start_time: number;
request_end_time?: number;
tags?: string[];
prompt_id?: string;
prompt_input_variables?: Record<string, string>;
prompt_version?: number;
}
const convertDateToSeconds = (date: Date) => Math.floor(date.getTime() / 1000);
class PromptLayer {
private static instance: PromptLayer;
private pendingRequest: Partial<PromptPlayerTrackRequest>;
private constructor(apiKey: string) {
this.pendingRequest = {
api_key: apiKey,
request_start_time: convertDateToSeconds(new Date()),
};
}
static init(apiKey: string) {
if (!PromptLayer.instance) {
PromptLayer.instance = new PromptLayer(apiKey);
}
}
static beginRequest(kwargs: Kwargs) {
PromptLayer.instance.pendingRequest.kwargs = kwargs;
PromptLayer.instance.pendingRequest.function_name = kwargs.functionName;
}
static async endRequest(request_response: unknown) {
PromptLayer.instance.pendingRequest.request_response = request_response;
PromptLayer.instance.pendingRequest.request_end_time = convertDateToSeconds(
new Date()
);
await PromptLayer.instance.trackRequest();
}
private async trackRequest() {
const url = `${API_BASE_URL}/track-request`;
await this.fetchResponse(
url,
this.pendingRequest as PromptPlayerTrackRequest
);
}
private async fetchResponse(url: string, body: PromptPlayerTrackRequest) {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
body: JSON.stringify(body),
});
if (!response.ok) {
const data = await response.json();
throw new Error(
`promptLayer error (${response.statusText}): ${JSON.stringify(data)}`
);
}
return response;
}
}
export default PromptLayer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment