ModelFusion is a TypeScript library for building AI applications, providing an abstraction layer for integrating AI models into JavaScript and TypeScript applications, and unifying the APIs for common operations such as text streaming generation, object generation, and tool use. The project has joined Vercel and is being integrated into the Vercel AI SDK; the official recommendation is to follow the AI SDK for the latest developments.
Uses ModelFusion can be used to build AI applications, chatbots, and agents. It is designed for production environments and provides features such as observability hooks, logging, and automatic retries.
Main capabilities
- Vendor-neutral: a non-commercial open source project, community-driven, and usable with any supported vendor.
- Multimodal: supports a wide range of models, including text generation, image generation, vision, text-to-speech, speech-to-text, and embedding models.
- Type inference and validation: infers TypeScript types where possible and validates model responses.
- Observability and logging: provides an observer framework and logging support.
- Resilience and robustness: ensures smooth operation through automatic retries, rate limiting, and error handling mechanisms.
- Production-oriented: fully tree-shakeable, usable in serverless environments, and uses very few dependencies.
Functional modules
- Text generation: generate text using language models and prompts, with support for streaming output, multimodal prompts (such as images), and prompt styles such as text, instruction, or chat. Supported vendors include OpenAI, OpenAI-compatible, Llama.cpp, Ollama, Mistral, Hugging Face, and Cohere.
- Vision: multimodal vision models such as GPT-4 Vision can process images as part of a prompt. Vendors include OpenAI, OpenAI-compatible, Llama.cpp, and Ollama.
- Object generation: generate typed objects using language models and schemas, with support for streaming objects (untyped JSON until the final part). Vendors include OpenAI, Ollama, and Llama.cpp.
- Image generation: generate images from prompts. Vendors include OpenAI (Dall·E), Stability AI, and Automatic1111.
- Speech generation (TTS): synthesize speech from text; generateSpeech can generate a stream of speech chunks from text or a text stream, and depending on the model it can be fully duplex.Providers include Eleven Labs, LMNT, OpenAI.
- Transcription (STT): Transcribe speech data into text. Providers include OpenAI (Whisper), Whisper.cpp.
- Embedding: Create embedding vectors for text and other values.
- Classification: Classify values into a certain category; the classifier is EmbeddingSimilarityClassifier.
- Tokenization: Split text into tokens and reconstruct text from tokens. Providers include OpenAI, Llama.cpp, Cohere.
- Tools: Functions that can be executed by AI models and associated metadata, used to build chatbots and agents. Built-in tools include Math.js, MediaWiki Search, SerpAPI, Google Custom Search, and custom tools can also be created. runTool allows a language model compatible with tools to call a single tool; runTools allows the model to generate multiple tool calls and text, and execute these tools, and can be used to implement an agent loop that responds to messages and executes tools.
- Vector store: Memory, SQLite VSS, Pinecone.
Usage API keys for each integration can be provided via environment variables (such as OPENAI_API_KEY), or passed as options to the model constructor. Starter templates are provided: ModelFusion terminal app starter, Next.js + Vercel AI SDK + Llama.cpp + ModelFusion starter, Next.js + Vercel AI SDK + Ollama + ModelFusion starter. Basic examples are located in the examples/basic folder. Text generation models can use .withTextPrompt(), .withChatPrompt(), and .withInstructionPrompt() to access different prompt styles; image models can also use prompt templates. When the fullResponse parameter is set to true, the model function returns a rich response containing the raw response and metadata.
Use cases and examples Suitable for building chatbots, agents, multimodal applications, etc. Official examples include: StoryTeller (an exploratory web app that creates short audio stories for preschoolers), Chatbot (Next.js app, OpenAI GPT-3.5-turbo, streaming, abort handling), PDF Q&A (terminal app, PDF parsing, in-memory vector index, retrieval-augmented generation), Next.js/ModelFusion demo (image generation, transcription, object streaming), duplex voice streaming (Vite/React with ModelFusion Server/Fastify), BabyAGI (TypeScript implementation), Wikipedia Agent (ReAct agent, GPT-4, OpenAI functions, tools), middle school math agent (using a calculator tool), tweet generation (PDF parsing, recursive information extraction, style example retrieval, cost calculation), Cloudflare Workers text generation.
Limitations The materials do not provide specific information such as fees, pricing, platform restrictions, or performance guarantees.