Skip to content
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

Add logRequest method #97

Merged
merged 3 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "promptlayer",
"license": "MIT",
"version": "1.0.9",
"version": "1.0.10",
"main": "dist/index.js",
"module": "dist/index.esm.js",
"types": "dist/index.d.ts",
Expand Down
38 changes: 22 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import * as opentelemetry from '@opentelemetry/api';
import {GroupManager} from "@/groups";
import {promptLayerBase} from "@/promptlayer";
import {wrapWithSpan} from '@/span-wrapper';
import {TemplateManager} from "@/templates";
import {getTracer, setupTracing} from '@/tracing';
import {TrackManager} from "@/track";
import {GetPromptTemplateParams, RunRequest} from "@/types";
import { GroupManager } from "@/groups";
import { promptLayerBase } from "@/promptlayer";
import { wrapWithSpan } from "@/span-wrapper";
import { TemplateManager } from "@/templates";
import { getTracer, setupTracing } from "@/tracing";
import { TrackManager } from "@/track";
import { GetPromptTemplateParams, LogRequest, RunRequest } from "@/types";
import {
anthropicRequest,
anthropicStreamCompletion,
Expand All @@ -15,7 +14,9 @@ import {
openaiStreamCompletion,
streamResponse,
trackRequest,
utilLogRequest,
} from "@/utils";
import * as opentelemetry from "@opentelemetry/api";

const MAP_PROVIDER_TO_FUNCTION_NAME = {
openai: {
Expand Down Expand Up @@ -115,7 +116,7 @@ export class PromptLayer {
}: RunRequest) {
const tracer = getTracer();

return tracer.startActiveSpan('PromptLayer Run', async (span) => {
return tracer.startActiveSpan("PromptLayer Run", async (span) => {
try {
const functionInput = {
promptName,
Expand All @@ -127,7 +128,7 @@ export class PromptLayer {
groupId,
stream,
};
span.setAttribute('function_input', JSON.stringify(functionInput));
span.setAttribute("function_input", JSON.stringify(functionInput));

const prompt_input_variables = inputVariables;
const templateGetParams: GetPromptTemplateParams = {
Expand Down Expand Up @@ -172,7 +173,7 @@ export class PromptLayer {
const config =
MAP_PROVIDER_TO_FUNCTION_NAME[
provider_type as keyof typeof MAP_PROVIDER_TO_FUNCTION_NAME
][promptTemplate.type];
][promptTemplate.type];
const function_name = config.function_name;

const stream_function = config.stream_function;
Expand All @@ -183,7 +184,7 @@ export class PromptLayer {
}
kwargs["stream"] = stream;
if (stream && provider_type === "openai") {
kwargs["stream_options"] = {include_usage: true};
kwargs["stream_options"] = { include_usage: true };
}

const response = await request_function(promptBlueprint, kwargs);
Expand All @@ -210,26 +211,31 @@ export class PromptLayer {
});
};

if (stream) return streamResponse(response, _trackRequest, stream_function);
const requestLog = await _trackRequest({request_response: response});
if (stream)
return streamResponse(response, _trackRequest, stream_function);
const requestLog = await _trackRequest({ request_response: response });

const functionOutput = {
request_id: requestLog.request_id,
raw_response: response,
prompt_blueprint: requestLog.prompt_blueprint,
};
span.setAttribute('function_output', JSON.stringify(functionOutput));
span.setAttribute("function_output", JSON.stringify(functionOutput));

return functionOutput;
} catch (error) {
span.setStatus({
code: opentelemetry.SpanStatusCode.ERROR,
message: error instanceof Error ? error.message : 'Unknown error',
message: error instanceof Error ? error.message : "Unknown error",
});
throw error;
} finally {
span.end();
}
});
}

async logRequest(body: LogRequest) {
return utilLogRequest(this.apiKey, body);
}
}
29 changes: 27 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,14 @@ export type BasePromptTemplate = {
tags?: string[];
};

export type PromptVersion = {
export type PromptBlueprint = {
prompt_template: PromptTemplate;
commit_message?: string;
metadata?: Metadata;
};

export type PublishPromptTemplate = BasePromptTemplate &
PromptVersion & { release_labels?: string[] };
PromptBlueprint & { release_labels?: string[] };

export interface ProviderBaseURL {
id: number;
Expand Down Expand Up @@ -265,3 +265,28 @@ export interface RunRequest {
promptReleaseLabel?: string;
inputVariables?: Record<string, unknown>;
}

export interface LogRequest {
provider: string;
model: string;
input: PromptTemplate;
output: PromptTemplate;
request_start_time: number;
request_end_time: number;
parameters?: Record<string, unknown>;
tags?: string[];
metadata?: Record<string, string>;
prompt_name?: string;
prompt_version_number?: number;
prompt_input_variables?: Record<string, unknown>;
input_tokens?: number;
output_tokens?: number;
price?: number;
function_name?: string;
score?: number;
}

export interface RequestLog {
id: number;
prompt_version: PromptBlueprint;
}
37 changes: 35 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import {
GetPromptTemplateParams,
GetPromptTemplateResponse,
ListPromptTemplatesResponse,
LogRequest,
Pagination,
PublishPromptTemplate,
PublishPromptTemplateResponse,
RequestLog,
TrackGroup,
TrackMetadata,
TrackPrompt,
Expand All @@ -24,7 +26,8 @@ import {
Completion,
} from "openai/resources";

export const URL_API_PROMPTLAYER = process.env.URL_API_PROMPTLAYER || "https://api.promptlayer.com";
export const URL_API_PROMPTLAYER =
process.env.URL_API_PROMPTLAYER || "https://api.promptlayer.com";

const promptlayerApiHandler = async <Item>(
apiKey: string,
Expand Down Expand Up @@ -681,6 +684,35 @@ const anthropicRequest = async (
return requestToMake(client, kwargs);
};

const utilLogRequest = async (
apiKey: string,
body: LogRequest
): Promise<RequestLog | null> => {
try {
const response = await fetch(`${URL_API_PROMPTLAYER}/log-request`, {
method: "POST",
headers: {
"X-API-KEY": apiKey,
"Content-Type": "application/json",
},
body: JSON.stringify(body),
});
if (response.status !== 201) {
warnOnBadResponse(
response,
"WARNING: While logging your request PromptLayer had the following error"
);
return null;
}
return response.json();
} catch (e) {
console.warn(
`WARNING: While tracking your prompt PromptLayer had the following error: ${e}`
);
return null;
}
};

export {
anthropicRequest,
anthropicStreamCompletion,
Expand All @@ -690,14 +722,15 @@ export {
openaiRequest,
openaiStreamChat,
openaiStreamCompletion,
promptlayerApiHandler,
promptLayerApiRequest,
promptLayerCreateGroup,
promptLayerTrackGroup,
promptLayerTrackMetadata,
promptLayerTrackPrompt,
promptLayerTrackScore,
promptlayerApiHandler,
publishPromptTemplate,
streamResponse,
trackRequest,
utilLogRequest,
};
Loading