zen: remove hardcoded safety identifier
This commit is contained in:
@@ -141,7 +141,10 @@ export async function handler(
|
|||||||
)
|
)
|
||||||
validateModelSettings(billingSource, authInfo)
|
validateModelSettings(billingSource, authInfo)
|
||||||
updateProviderKey(authInfo, providerInfo)
|
updateProviderKey(authInfo, providerInfo)
|
||||||
logger.metric({ provider: providerInfo.id })
|
logger.metric({
|
||||||
|
provider: providerInfo.id,
|
||||||
|
"provider.model": providerInfo.model,
|
||||||
|
})
|
||||||
|
|
||||||
const startTimestamp = Date.now()
|
const startTimestamp = Date.now()
|
||||||
const reqUrl = providerInfo.modifyUrl(providerInfo.api, isStream)
|
const reqUrl = providerInfo.modifyUrl(providerInfo.api, isStream)
|
||||||
@@ -149,12 +152,23 @@ export async function handler(
|
|||||||
providerInfo.modifyBody({
|
providerInfo.modifyBody({
|
||||||
...createBodyConverter(opts.format, providerInfo.format)(body),
|
...createBodyConverter(opts.format, providerInfo.format)(body),
|
||||||
model: providerInfo.model,
|
model: providerInfo.model,
|
||||||
...providerInfo.payloadModifier,
|
...(() => {
|
||||||
...Object.fromEntries(
|
const replacer = (obj: Record<string, any>): Record<string, any> =>
|
||||||
Object.entries(providerInfo.payloadMappings ?? {})
|
Object.fromEntries(
|
||||||
.map(([k, v]) => [k, input.request.headers.get(v)])
|
Object.entries(obj).flatMap(([k, v]) => {
|
||||||
.filter(([_k, v]) => !!v),
|
if (Array.isArray(v)) return [[k, v]]
|
||||||
),
|
if (typeof v === "object") return [[k, replacer(v)]]
|
||||||
|
if (v === "$ip") return [[k, ip]]
|
||||||
|
if (v === "$workspace") return authInfo?.workspaceID ? [[k, authInfo?.workspaceID]] : []
|
||||||
|
if (v.startsWith("$header.")) {
|
||||||
|
const headerValue = input.request.headers.get(v.slice(8))
|
||||||
|
return headerValue ? [[k, headerValue]] : []
|
||||||
|
}
|
||||||
|
return [[k, v]]
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
return replacer(providerInfo.payloadModifier ?? {})
|
||||||
|
})(),
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
logger.debug("REQUEST URL: " + reqUrl)
|
logger.debug("REQUEST URL: " + reqUrl)
|
||||||
@@ -514,7 +528,6 @@ export async function handler(
|
|||||||
reqModel,
|
reqModel,
|
||||||
providerModel: modelProvider.model,
|
providerModel: modelProvider.model,
|
||||||
adjustCacheUsage: providerProps.adjustCacheUsage,
|
adjustCacheUsage: providerProps.adjustCacheUsage,
|
||||||
safetyIdentifier: modelProvider.safetyIdentifier ? ip : undefined,
|
|
||||||
workspaceID: authInfo?.workspaceID,
|
workspaceID: authInfo?.workspaceID,
|
||||||
}
|
}
|
||||||
if (format === "anthropic") return anthropicHelper(opts)
|
if (format === "anthropic") return anthropicHelper(opts)
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ type Usage = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const oaCompatHelper: ProviderHelper = ({ adjustCacheUsage, safetyIdentifier }) => ({
|
export const oaCompatHelper: ProviderHelper = ({ adjustCacheUsage }) => ({
|
||||||
format: "oa-compat",
|
format: "oa-compat",
|
||||||
modifyUrl: (providerApi: string) => providerApi + "/chat/completions",
|
modifyUrl: (providerApi: string) => providerApi + "/chat/completions",
|
||||||
modifyHeaders: (headers: Headers, body: Record<string, any>, apiKey: string) => {
|
modifyHeaders: (headers: Headers, body: Record<string, any>, apiKey: string) => {
|
||||||
@@ -34,7 +34,6 @@ export const oaCompatHelper: ProviderHelper = ({ adjustCacheUsage, safetyIdentif
|
|||||||
return {
|
return {
|
||||||
...body,
|
...body,
|
||||||
...(body.stream ? { stream_options: { include_usage: true } } : {}),
|
...(body.stream ? { stream_options: { include_usage: true } } : {}),
|
||||||
...(safetyIdentifier ? { safety_identifier: safetyIdentifier } : {}),
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
createBinaryStreamDecoder: () => undefined,
|
createBinaryStreamDecoder: () => undefined,
|
||||||
|
|||||||
@@ -18,10 +18,7 @@ export const openaiHelper: ProviderHelper = ({ workspaceID }) => ({
|
|||||||
modifyHeaders: (headers: Headers, body: Record<string, any>, apiKey: string) => {
|
modifyHeaders: (headers: Headers, body: Record<string, any>, apiKey: string) => {
|
||||||
headers.set("authorization", `Bearer ${apiKey}`)
|
headers.set("authorization", `Bearer ${apiKey}`)
|
||||||
},
|
},
|
||||||
modifyBody: (body: Record<string, any>) => ({
|
modifyBody: (body: Record<string, any>) => body,
|
||||||
...body,
|
|
||||||
...(workspaceID ? { safety_identifier: workspaceID } : {}),
|
|
||||||
}),
|
|
||||||
createBinaryStreamDecoder: () => undefined,
|
createBinaryStreamDecoder: () => undefined,
|
||||||
streamSeparator: "\n\n",
|
streamSeparator: "\n\n",
|
||||||
createUsageParser: () => {
|
createUsageParser: () => {
|
||||||
|
|||||||
@@ -37,7 +37,6 @@ export type ProviderHelper = (input: {
|
|||||||
reqModel: string
|
reqModel: string
|
||||||
providerModel: string
|
providerModel: string
|
||||||
adjustCacheUsage?: boolean
|
adjustCacheUsage?: boolean
|
||||||
safetyIdentifier?: string
|
|
||||||
workspaceID?: string
|
workspaceID?: string
|
||||||
}) => {
|
}) => {
|
||||||
format: ZenData.Format
|
format: ZenData.Format
|
||||||
|
|||||||
@@ -40,7 +40,6 @@ export namespace ZenData {
|
|||||||
disabled: z.boolean().optional(),
|
disabled: z.boolean().optional(),
|
||||||
storeModel: z.string().optional(),
|
storeModel: z.string().optional(),
|
||||||
payloadModifier: z.record(z.string(), z.any()).optional(),
|
payloadModifier: z.record(z.string(), z.any()).optional(),
|
||||||
safetyIdentifier: z.boolean().optional(),
|
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user