Coder Social home page Coder Social logo

lgrammel / modelfusion-ollama-nextjs-starter Goto Github PK

View Code? Open in Web Editor NEW
61.0 2.0 11.0 355 KB

Starter examples for using Next.js and the Vercel AI SDK with Ollama and ModelFusion.

License: MIT License

TypeScript 78.11% CSS 1.83% JavaScript 20.06%
ai modelfusion next nextjs ollama vercel-ai vercel-ai-sdk llama2 mistral

modelfusion-ollama-nextjs-starter's Introduction

Next.js, Vercel AI SDK, Ollama & ModelFusion starter

This starter example shows how to use Next.js, the Vercel AI SDK, Ollama and ModelFusion to create a ChatGPT-like AI-powered streaming chat bot.

Setup

  1. Install Ollama on your machine.
  2. Pull the model: ollama pull llama2:chat (reference)
  3. Clone the repository: git clone https://github.com/lgrammel/modelfusion-ollama-nextjs-starter.git
  4. Install dependencies: npm install
  5. Start the development server: npm run dev
  6. Go to http://localhost:3000/
  7. Code: app/api/chat/route.ts

Example Route

import { ModelFusionTextStream, asChatMessages } from "@modelfusion/vercel-ai";
import { Message, StreamingTextResponse } from "ai";
import { ollama, streamText } from "modelfusion";

export const runtime = "edge";

export async function POST(req: Request) {
  const { messages }: { messages: Message[] } = await req.json();

  // Use ModelFusion to call Ollama:
  const textStream = await streamText({
    model: ollama.ChatTextGenerator({ model: "llama2:chat" }).withChatPrompt(),
    prompt: {
      system:
        "You are an AI chat bot. " +
        "Follow the user's instructions carefully.",

      // map Vercel AI SDK Message to ModelFusion ChatMessage:
      messages: asChatMessages(messages),
    },
  });

  // Return the result using the Vercel AI SDK:
  return new StreamingTextResponse(
    ModelFusionTextStream(
      textStream,
      // optional callbacks:
      {
        onStart() {
          console.log("onStart");
        },
        onToken(token) {
          console.log("onToken", token);
        },
        onCompletion: () => {
          console.log("onCompletion");
        },
        onFinal(completion) {
          console.log("onFinal", completion);
        },
      }
    )
  );
}

modelfusion-ollama-nextjs-starter's People

Contributors

lgrammel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

modelfusion-ollama-nextjs-starter's Issues

Problems with Llama 3 constantly streaming assistant

Hi there, I came across your tutorial and this repo and was having issues. I'm not sure of it's llama3 related, or a bump in the ai package.

The assistant just permanently streams back to me, somehow it's being sent some sort of input without my doing anything.

There's no llama3:chat as per your current example, so I'm just using straight llama3 and my package json has:

    "@modelfusion/vercel-ai": "^1.1.0",
    "ai": "^3.0.35",
    "modelfusion": "^0.137.0",

CleanShot 2024-04-30 at 14 00 58@2x

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.