Giter VIP home page Giter VIP logo

Comments (11)

lgrammel avatar lgrammel commented on July 28, 2024 1

Ah, very cool. I'm leaning towards providing access to the raw stream responses as a first step - then you could extract the information from there. Going forward I'll think about how to integrate it into the JSON functionality.

from modelfusion.

bjsi avatar bjsi commented on July 28, 2024 1

I also use it for the fields of objects. Eg for a flashcard creating assistant in a note-taking app:

const flashcard = z.object({
  question: z.string(),
  answer: z.string()
})

I can start updating the editor as soon as the question field is non-empty. This really helps with responsiveness.

Also in my case I am not just requesting a simple array of flashcards but also asking GPT to cite the original sentence the flashcard was created from so I can create "citation pins" (you can see them get created at the end of the GIF). This slows the request down considerably, so it's doubly important for me to stream token by token.

stream-flashcards

from modelfusion.

bjsi avatar bjsi commented on July 28, 2024 1

Here's the gist for the partial JSON parsing I'm using: https://gist.github.com/bjsi/3ad6297345aec91460cbce5aecac939c

from modelfusion.

lgrammel avatar lgrammel commented on July 28, 2024 1

Started to work on this: #113

from modelfusion.

lgrammel avatar lgrammel commented on July 28, 2024 1

A first version is available in v0.35.0. See https://modelfusion.dev/guide/function/generate-structure#streamstructure

Thanks for your input!

from modelfusion.

bjsi avatar bjsi commented on July 28, 2024

I was able to get something quick working by doing this:

import { OpenAIChatDelta, OpenAIChatModel } from "modelfusion";

export class StreamingToolModel extends OpenAIChatModel {
  functionCall:
    | {
        name: string;
        arguments?: string;
      }
    | undefined;
  extractTextDelta(fullDelta: OpenAIChatDelta): string | undefined {
    const msg = fullDelta[0];
    if (msg?.["delta"]?.["function_call"]) {
      if (!this.functionCall) {
        this.functionCall = {
          name: "",
          arguments: "",
        };
      }
      if ("name" in msg["delta"]["function_call"]) {
        this.functionCall["name"] += msg["delta"]["function_call"]["name"];
      }
      if (msg["delta"]["function_call"]["arguments"]) {
        this.functionCall["arguments"] +=
          msg["delta"]["function_call"]["arguments"];
      }
      return JSON.stringify(this.functionCall);
    } else {
      return undefined;
    }
  }
}

from modelfusion.

lgrammel avatar lgrammel commented on July 28, 2024

Very cool!

I thought a bit about it earlier, but didn't see any advantages for streaming over regular generation for JSON, because you need to wait for the JSON to be fully available before it can be parsed and typechecked.

I'm curious what your use case is and what advantages streaming has for it?

from modelfusion.

bjsi avatar bjsi commented on July 28, 2024

I like using function calls for most things because I feel like I have more control over LLM output and the output is more reliable. If I'm generating a JSON array then I don't need to wait for the entire array to finish generating before I start processing some of the entries. My solution has been to use partial json parser to parse items out of the incomplete JSON 👍

from modelfusion.

lgrammel avatar lgrammel commented on July 28, 2024

@bjsi array streaming is totally useful - exploring this in more detail. Did you find any use cases for JSON streaming besides streaming top-level arrays item-by-item?

from modelfusion.

lgrammel avatar lgrammel commented on July 28, 2024

Thanks for sharing! So you are also interested in streaming partial text content from what I understand. Do you validate the partial responses with Zod or only the full responses?

from modelfusion.

bjsi avatar bjsi commented on July 28, 2024

from modelfusion.

Related Issues (20)

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.