Add initial client lib
This commit is contained in:
89
client-libs/js/index.ts
Normal file
89
client-libs/js/index.ts
Normal file
@@ -0,0 +1,89 @@
|
||||
import * as opClient from "./codegen";
|
||||
import { PromptTypes } from "./codegen/promptTypes";
|
||||
import { version } from "./package.json";
|
||||
|
||||
type OPConfigOptions = {
|
||||
apiKey?: string;
|
||||
basePath?: string;
|
||||
};
|
||||
|
||||
export class Configuration {
|
||||
public opConfig?: opClient.Configuration;
|
||||
// TODO: use OpenAPI apiKey or something similar
|
||||
public apiKey: string;
|
||||
|
||||
constructor(config: { opOptions?: OPConfigOptions }) {
|
||||
if (config.opOptions) {
|
||||
this.opConfig = new opClient.Configuration(config.opOptions);
|
||||
this.apiKey = config.opOptions.apiKey ?? "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class OpenPipeApi {
|
||||
public opApi?: opClient.DefaultApi;
|
||||
private apiKey: string;
|
||||
|
||||
constructor(config: Configuration) {
|
||||
if (config.opConfig) {
|
||||
this.opApi = new opClient.DefaultApi(config.opConfig);
|
||||
this.apiKey = config.apiKey;
|
||||
}
|
||||
}
|
||||
|
||||
public async capturePrompt<T extends keyof PromptTypes>(
|
||||
dataFlowId: string,
|
||||
modelProvider: T,
|
||||
promptFunction: () => PromptTypes[T],
|
||||
scenarioVariables: Record<string, unknown>
|
||||
): Promise<{
|
||||
id?: string;
|
||||
prompt: PromptTypes[T];
|
||||
}> {
|
||||
const prompt = promptFunction() as PromptTypes[T];
|
||||
if (!prompt) {
|
||||
console.error("Prompt function returned null", promptFunction.toString());
|
||||
}
|
||||
const promptFunctionString = promptFunction.toString();
|
||||
const startTime = Date.now();
|
||||
|
||||
let resp;
|
||||
|
||||
try {
|
||||
resp = await this.opApi?.externalApiCapturePrompt({
|
||||
apiKey: this.apiKey,
|
||||
dataFlowId,
|
||||
promptFunction: promptFunctionString,
|
||||
scenarioVariables,
|
||||
model: "",
|
||||
modelProvider,
|
||||
prompt,
|
||||
});
|
||||
} catch (err) {
|
||||
console.error("Error reporting to OpenPipe", err);
|
||||
}
|
||||
|
||||
return {
|
||||
id: resp?.data?.loggedCallId,
|
||||
prompt,
|
||||
}
|
||||
}
|
||||
|
||||
public async captureResponse(loggedCallId: string | undefined, responsePayload: any): Promise<void> {
|
||||
if (!loggedCallId) {
|
||||
console.error("No call log ID provided to captureResponse");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await this.opApi?.externalApiCaptureResponse({
|
||||
apiKey: this.apiKey,
|
||||
loggedCallId,
|
||||
responsePayload,
|
||||
});
|
||||
} catch (err) {
|
||||
console.error("Error reporting to OpenPipe", err);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user