minor fixes for google sheets backend for anthropic-chat

This commit is contained in:
nai-degen 2024-03-17 12:08:11 -05:00
parent 435280fa04
commit 59107af3d6
2 changed files with 21 additions and 10 deletions

View File

@ -15,7 +15,6 @@ import {
MistralAIChatMessage,
OpenAIChatMessage,
} from "../../../shared/api-schemas";
import { APIFormat } from "../../../shared/key-management";
/** If prompt logging is enabled, enqueues the prompt for logging. */
export const logPrompt: ProxyResHandlerWithBody = async (
@ -36,7 +35,7 @@ export const logPrompt: ProxyResHandlerWithBody = async (
if (!loggable) return;
const promptPayload = getPromptForRequest(req, responseBody);
const promptFlattened = flattenMessages(promptPayload, req.outboundApi);
const promptFlattened = flattenMessages(promptPayload);
const response = getCompletionFromBody(req, responseBody);
const model = getModelFromBody(req, responseBody);
@ -63,7 +62,7 @@ const getPromptForRequest = (
):
| string
| OpenAIChatMessage[]
| AnthropicChatMessage[]
| { system: string; messages: AnthropicChatMessage[] }
| MistralAIChatMessage[]
| OaiImageResult => {
// Since the prompt logger only runs after the request has been proxied, we
@ -72,8 +71,9 @@ const getPromptForRequest = (
switch (req.outboundApi) {
case "openai":
case "mistral-ai":
case "anthropic-chat":
return req.body.messages;
case "anthropic-chat":
return { system: req.body.system, messages: req.body.messages };
case "openai-text":
return req.body.prompt;
case "openai-image":
@ -98,15 +98,15 @@ const flattenMessages = (
| string
| OaiImageResult
| OpenAIChatMessage[]
| AnthropicChatMessage[]
| MistralAIChatMessage[],
format: APIFormat
| { system: string; messages: AnthropicChatMessage[] }
| MistralAIChatMessage[]
): string => {
if (typeof val === "string") {
return val.trim();
}
if (format === "anthropic-chat") {
return flattenAnthropicMessages(val as AnthropicChatMessage[]);
if (isAnthropicChatPrompt(val)) {
const { system, messages } = val;
return `System: ${system}\n\n${flattenAnthropicMessages(messages)}`;
}
if (Array.isArray(val)) {
return val
@ -127,3 +127,14 @@ const flattenMessages = (
}
return val.prompt.trim();
};
function isAnthropicChatPrompt(
val: unknown
): val is { system: string; messages: AnthropicChatMessage[] } {
return (
typeof val === "object" &&
val !== null &&
"system" in val &&
"messages" in val
);
}

View File

@ -260,7 +260,7 @@ export function flattenAnthropicMessages(
): string {
return messages
.map((msg) => {
const name = msg.role === "user" ? "\n\nHuman: " : "\n\nAssistant: ";
const name = msg.role === "user" ? "Human" : "Assistant";
const parts = Array.isArray(msg.content)
? msg.content
: [{ type: "text", text: msg.content }];