moves api schema validators from transform-outbound-payload into shared

This commit is contained in:
nai-degen
2024-01-29 19:38:22 -06:00
parent 924db33f7e
commit 7eb6eb90ad
15 changed files with 601 additions and 562 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
import { MistralAIChatMessage } from "../../proxy/middleware/request/preprocessors/transform-outbound-payload";
import * as tokenizer from "./mistral-tokenizer-js";
import { MistralAIChatMessage } from "../api-schemas";
export function init() {
tokenizer.initializemistralTokenizer();
+4 -6
View File
@@ -2,11 +2,7 @@ import { Tiktoken } from "tiktoken/lite";
import cl100k_base from "tiktoken/encoders/cl100k_base.json";
import { logger } from "../../logger";
import { libSharp } from "../file-storage";
import type {
GoogleAIChatMessage,
OpenAIChatMessage,
} from "../../proxy/middleware/request/preprocessors/transform-outbound-payload";
import { z } from "zod";
import { GoogleAIChatMessage, OpenAIChatMessage } from "../api-schemas";
const log = logger.child({ module: "tokenizer", service: "openai" });
const GPT4_VISION_SYSTEM_PROMPT_SIZE = 170;
@@ -233,7 +229,9 @@ export function getOpenAIImageCost(params: {
};
}
export function estimateGoogleAITokenCount(prompt: string | GoogleAIChatMessage[]) {
export function estimateGoogleAITokenCount(
prompt: string | GoogleAIChatMessage[]
) {
if (typeof prompt === "string") {
return getTextTokenCount(prompt);
}
+23 -19
View File
@@ -1,25 +1,25 @@
import { Request } from "express";
import type {
import { assertNever } from "../utils";
import {
getTokenCount as getClaudeTokenCount,
init as initClaude,
} from "./claude";
import {
estimateGoogleAITokenCount,
getOpenAIImageCost,
getTokenCount as getOpenAITokenCount,
init as initOpenAi,
} from "./openai";
import {
getTokenCount as getMistralAITokenCount,
init as initMistralAI,
} from "./mistral";
import { APIFormat } from "../key-management";
import {
GoogleAIChatMessage,
MistralAIChatMessage,
OpenAIChatMessage,
} from "../../proxy/middleware/request/preprocessors/transform-outbound-payload";
import { assertNever } from "../utils";
import {
init as initClaude,
getTokenCount as getClaudeTokenCount,
} from "./claude";
import {
init as initOpenAi,
getTokenCount as getOpenAITokenCount,
getOpenAIImageCost,
estimateGoogleAITokenCount,
} from "./openai";
import {
init as initMistralAI,
getTokenCount as getMistralAITokenCount,
} from "./mistral";
import { APIFormat } from "../key-management";
} from "../api-schemas";
export async function init() {
initClaude();
@@ -37,7 +37,11 @@ type TokenCountRequest = { req: Request } & (
service: "openai-text" | "anthropic" | "google-ai";
}
| { prompt?: GoogleAIChatMessage[]; completion?: never; service: "google-ai" }
| { prompt: MistralAIChatMessage[]; completion?: never; service: "mistral-ai" }
| {
prompt: MistralAIChatMessage[];
completion?: never;
service: "mistral-ai";
}
| { prompt?: never; completion: string; service: APIFormat }
| { prompt?: never; completion?: never; service: "openai-image" }
);