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

support moderations #133

Merged
merged 1 commit into from
Jan 14, 2025
Merged
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
27 changes: 22 additions & 5 deletions packages/proxy/src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
ChatCompletionCreateParams,
CompletionUsage,
CreateEmbeddingResponse,
ModerationCreateResponse,
} from "openai/resources";
import { fetchBedrockAnthropic, fetchBedrockOpenAI } from "./providers/bedrock";
import { Buffer } from "node:buffer";
Expand Down Expand Up @@ -210,7 +211,8 @@ export async function proxyV1({
url === "/auto" ||
url === "/embeddings" ||
url === "/chat/completions" ||
url === "/completions";
url === "/completions" ||
url === "/moderations";

let bodyData = null;
if (
Expand Down Expand Up @@ -474,7 +476,7 @@ export async function proxyV1({
async flush(controller) {
const data = flattenChunksArray(allChunks);
const dataB64 = Buffer.from(data).toString("base64");
cachePut(
await cachePut(
encryptionKey,
cacheKey,
JSON.stringify({ headers: proxyResponseHeaders, data: dataB64 }),
Expand Down Expand Up @@ -637,6 +639,14 @@ export async function proxyV1({
});
}
break;
case "moderation":
{
const data = dataRaw as ModerationCreateResponse;
spanLogger.log({
output: data.results,
});
}
break;
}
}

Expand Down Expand Up @@ -1598,7 +1608,7 @@ function tryParseRateLimitReset(headers: Headers): number | null {
return null;
}

export type SpanType = "chat" | "completion" | "embedding";
export type SpanType = "chat" | "completion" | "embedding" | "moderation";

function spanTypeToName(spanType: SpanType): string {
switch (spanType) {
Expand All @@ -1608,6 +1618,8 @@ function spanTypeToName(spanType: SpanType): string {
return "Completion";
case "embedding":
return "Embedding";
case "moderation":
return "Moderation";
}
}

Expand All @@ -1622,7 +1634,9 @@ export function guessSpanType(
? "completion"
: url === "/embeddings"
? "embedding"
: undefined;
: url === "/moderations"
? "moderation"
: undefined;
if (spanName) {
return spanName;
}
Expand All @@ -1632,6 +1646,8 @@ export function guessSpanType(
return "chat";
} else if (flavor === "completion") {
return "completion";
} else if (url === "/moderations") {
return "moderation";
} else {
return undefined;
}
Expand Down Expand Up @@ -1660,7 +1676,8 @@ function logSpanInputs(
});
break;
}
case "embedding": {
case "embedding":
case "moderation": {
const { input, ...rest } = bodyData;
spanLogger.log({
input: bodyData,
Expand Down
Loading