ModelFusion

The TypeScript library for building AI applications.

README

ModelFusion


The TypeScript library for building AI applications.


Introduction


ModelFusion is an abstraction layer for integrating AI models into JavaScript and TypeScript applications, unifying the API for common operations such as text streaming, object generation, and tool usage. It provides features to support production environments, including observability hooks, logging, and automatic retries. You can use ModelFusion to build AI applications, chatbots, and agents.

- Vendor-neutral: ModelFusion is a non-commercial open source project that is community-driven. You can use it with any supported provider.
- Multi-modal: ModelFusion 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: ModelFusion infers TypeScript types wherever possible and validates model responses.
- Observability and logging: ModelFusion provides an observer framework and logging support.
- Resilience and robustness: ModelFusion ensures seamless operation through automatic retries, throttling, and error handling mechanisms.
- Built for production: ModelFusion is fully tree-shakeable, can be used in serverless environments, and only uses a minimal set of dependencies.

Quick Install


  1. ```sh
  2. npm install modelfusion
  3. ```

Or use a starter template:


Usage Examples


[!TIP]

The basic examples are a great way to get started and to explore in parallel with the documentation. You can find them in the examples/basic folder.


You can provide API keys for the different integrations using environment variables (e.g.,OPENAI_API_KEY) or pass them into the model constructors as options.


Generate text using a language model and a prompt. You can stream the text if it is supported by the model. You can use images for multi-modal prompting if the model supports it (e.g. with llama.cpp).
You can use prompt styles to use text, instruction, or chat prompts.

generateText


  1. ```ts
  2. import { generateText, openai } from "modelfusion";

  3. const text = await generateText({
  4.   model: openai.CompletionTextGenerator({ model: "gpt-3.5-turbo-instruct" }),
  5.   prompt: "Write a short story about a robot learning to love:\n\n",
  6. });
  7. ```

streamText


  1. ```ts
  2. import { streamText, openai } from "modelfusion";

  3. const textStream = await streamText({
  4.   model: openai.CompletionTextGenerator({ model: "gpt-3.5-turbo-instruct" }),
  5.   prompt: "Write a short story about a robot learning to love:\n\n",
  6. });

  7. for await (const textPart of textStream) {
  8.   process.stdout.write(textPart);
  9. }
  10. ```

streamText with multi-modal prompt


Multi-modal vision models such as GPT 4 Vision can process images as part of the prompt.

  1. ```ts
  2. import { streamText, openai } from "modelfusion";
  3. import { readFileSync } from "fs";

  4. const image = readFileSync("./image.png");

  5. const textStream = await streamText({
  6.   model: openai
  7.     .ChatTextGenerator({ model: "gpt-4-vision-preview" })
  8.     .withInstructionPrompt(),

  9.   prompt: {
  10.     instruction: [
  11.       { type: "text", text: "Describe the image in detail." },
  12.       { type: "image", image, mimeType: "image/png" },
  13.     ],
  14.   },
  15. });

  16. for await (const textPart of textStream) {
  17.   process.stdout.write(textPart);
  18. }
  19. ```


Generate typed objects using a language model and a schema.