From f03d4da21799c426bf152d1f6cd1e3749b58fcc9 Mon Sep 17 00:00:00 2001 From: Simon Farshid Date: Wed, 7 Aug 2024 18:11:57 -0700 Subject: [PATCH] feat(playground): minimal content part refusal support (#662) --- .../src/lib/openai/fromOpenAIMessages.ts | 34 +++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/packages/react-playground/src/lib/openai/fromOpenAIMessages.ts b/packages/react-playground/src/lib/openai/fromOpenAIMessages.ts index 97dc57d4a..446ccb20f 100644 --- a/packages/react-playground/src/lib/openai/fromOpenAIMessages.ts +++ b/packages/react-playground/src/lib/openai/fromOpenAIMessages.ts @@ -16,7 +16,13 @@ export const fromOpenAIMessages = ( const { role, content } = message; switch (role) { case "system": { - lmMessages.push({ role: "system", content }); + lmMessages.push({ + role: "system", + content: + typeof content === "string" + ? content + : content.map((c) => c.text).join("\n"), + }); break; } @@ -57,7 +63,31 @@ export const fromOpenAIMessages = ( )[] = []; if (content != null) { - lmContent.push({ type: "text", text: content }); + lmContent.push( + ...(typeof content === "string" + ? [{ type: "text" as const, text: content }] + : content.map((part) => { + const type = part.type; + switch (type) { + case "text": { + return part; + } + + case "refusal": { + // TODO handle refusals + return { + type: "text" as const, + text: part.refusal, + }; + } + + default: { + const unsupportedPart: "refusal" = type; + throw new Error(`Unsupported part: ${unsupportedPart}`); + } + } + })), + ); } if (message.tool_calls) {