diff --git a/packages/@modelfusion-vercel-ai/CHANGELOG.md b/packages/@modelfusion-vercel-ai/CHANGELOG.md new file mode 100644 index 000000000..443347c5b --- /dev/null +++ b/packages/@modelfusion-vercel-ai/CHANGELOG.md @@ -0,0 +1,7 @@ +# Changelog + +## v1.1.0 - 2023-12-26 + +### Added + +- `asChatMessages()` to convert Vercel AI SDK `Message[]` to ModelFusion `ChatMessage[]`. diff --git a/packages/@modelfusion-vercel-ai/README.md b/packages/@modelfusion-vercel-ai/README.md index a0526d913..71a41637a 100644 --- a/packages/@modelfusion-vercel-ai/README.md +++ b/packages/@modelfusion-vercel-ai/README.md @@ -8,15 +8,17 @@ You can call `ModelFusionTextStream` with the result from [streamText](https://modelfusion.dev/guide/function/generate-text#streamtext). This stream is compatible with the Vercel AI SDK and supports the callbacks and stream data features. +To convert Vercel AI SDK messages to ModelFusion messages, use the `asChatMessages` helper. + ## Example: Ollama & Next.js This is an example for a Next.js app router API route. It uses the ModelFusion [Ollama](https://github.com/jmorganca/ollama) integration. For a full example app, check out the [Next.js, Vercel AI SDK, Ollama & ModelFusion starter](https://github.com/lgrammel/modelfusion-ollama-nextjs-starter). ```ts // app/api/chat/route.ts -import { ModelFusionTextStream } from "@modelfusion/vercel-ai"; +import { asChatMessages, ModelFusionTextStream } from "@modelfusion/vercel-ai"; import { Message, StreamingTextResponse } from "ai"; -import { ChatMessage, ollama, streamText } from "modelfusion"; +import { ollama, streamText } from "modelfusion"; export const runtime = "edge"; @@ -32,10 +34,7 @@ export async function POST(req: Request) { "Follow the user's instructions carefully.", // map Vercel AI SDK Message to ModelFusion ChatMessage: - messages: messages.filter( - // only user and assistant roles are supported: - (message) => message.role === "user" || message.role === "assistant" - ) as ChatMessage[], + messages: asChatMessages(messages), } ); diff --git a/packages/@modelfusion-vercel-ai/package.json b/packages/@modelfusion-vercel-ai/package.json index 142ae21e5..07df01528 100644 --- a/packages/@modelfusion-vercel-ai/package.json +++ b/packages/@modelfusion-vercel-ai/package.json @@ -1,7 +1,7 @@ { "name": "@modelfusion/vercel-ai", "description": "ModelFusion stream for Vercel AI SDK", - "version": "1.0.0", + "version": "1.1.0", "author": "Lars Grammel", "license": "MIT", "keywords": [ @@ -40,11 +40,12 @@ "scripts": { "lint": "eslint --ext .ts src", "clean": "rimraf build dist .turbo node_modules", + "clean:build": "rimraf build dist", "build": "pnpm build:esm && pnpm build:cjs && pnpm build:copy-files", "build:esm": "tsc --outDir dist/", "build:cjs": "tsc --outDir build/cjs/ -p tsconfig.cjs.json && node bin/prepare-cjs.js", - "build:copy-files": "copyfiles --flat package.json README.md ../../LICENSE dist", - "dist": "pnpm clean && pnpm lint && pnpm test && pnpm build" + "build:copy-files": "copyfiles --flat package.json README.md CHANGELOG.md ../../LICENSE dist", + "dist": "pnpm clean:build && pnpm lint && pnpm build" }, "dependencies": {}, "devDependencies": { diff --git a/packages/@modelfusion-vercel-ai/src/asChatMessages.ts b/packages/@modelfusion-vercel-ai/src/asChatMessages.ts new file mode 100644 index 000000000..7f798400a --- /dev/null +++ b/packages/@modelfusion-vercel-ai/src/asChatMessages.ts @@ -0,0 +1,12 @@ +import { Message } from "ai"; +import { ChatMessage } from "modelfusion"; + +/** + * Convert Vercel AI SDK messages to ModelFusion ChatMessages. + */ +export function asChatMessages(messages: Message[]): ChatMessage[] { + return messages.filter( + // only user and assistant roles are supported: + (message) => message.role === "user" || message.role === "assistant" + ) as ChatMessage[]; +} diff --git a/packages/@modelfusion-vercel-ai/src/index.ts b/packages/@modelfusion-vercel-ai/src/index.ts index 3300294ca..de19243a7 100644 --- a/packages/@modelfusion-vercel-ai/src/index.ts +++ b/packages/@modelfusion-vercel-ai/src/index.ts @@ -1 +1,2 @@ export * from "./ModelFusionTextStream.js"; +export * from "./asChatMessages.js";