Skip to content

Commit

Permalink
Add asChatMessages to @modelfusion/vercel-ai.
Browse files Browse the repository at this point in the history
  • Loading branch information
lgrammel committed Dec 26, 2023
1 parent 1ea5ef0 commit ab6a840
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 9 deletions.
7 changes: 7 additions & 0 deletions packages/@modelfusion-vercel-ai/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Changelog

## v1.1.0 - 2023-12-26

### Added

- `asChatMessages()` to convert Vercel AI SDK `Message[]` to ModelFusion `ChatMessage[]`.
11 changes: 5 additions & 6 deletions packages/@modelfusion-vercel-ai/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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),
}
);

Expand Down
7 changes: 4 additions & 3 deletions packages/@modelfusion-vercel-ai/package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down Expand Up @@ -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": {
Expand Down
12 changes: 12 additions & 0 deletions packages/@modelfusion-vercel-ai/src/asChatMessages.ts
Original file line number Diff line number Diff line change
@@ -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[];
}
1 change: 1 addition & 0 deletions packages/@modelfusion-vercel-ai/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./ModelFusionTextStream.js";
export * from "./asChatMessages.js";

1 comment on commit ab6a840

@vercel
Copy link

@vercel vercel bot commented on ab6a840 Dec 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.