Comments (11)
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.
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.
from modelfusion.
Here's the gist for the partial JSON parsing I'm using: https://gist.github.com/bjsi/3ad6297345aec91460cbce5aecac939c
from modelfusion.
Started to work on this: #113
from modelfusion.
A first version is available in v0.35.0
. See https://modelfusion.dev/guide/function/generate-structure#streamstructure
Thanks for your input!
from modelfusion.
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.
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.
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.
@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.
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.
from modelfusion.
Related Issues (20)
- Add OpenChat prompt template
- introduce classify model function
- explore img + text -> image models / prompts
- move out openai cost calc from modelfusion and modelfusion/experimental into a standalone pkg that can also be used w/o modelfusion
- explore if there is a good local TTS that could be integrated
- explore variable output parsers, e.g. for list (when generated using GBNF grammar)
- BUG: Ollama response `prompt_eval_count` is required field. HOT 1
- Tools do not work with ollama HOT 1
- can't connect to https://api.openai.com:443/v1/audio/transcriptions HOT 4
- First parameter has member `readable` when running generate text on AWS Lambda. HOT 3
- experimental-vm-modules bug introduced in v0.134.0 HOT 7
- Ollama Mistral .withPromptTemplate Error HOT 3
- Cannot pass custom parameters to the provider
- Add support for gpt-4-turbo model
- Parameters for Hugging Face are not passed correctly to endpoint
- Streaming example doesn't forward provider request errors to the browser
- Anthropic Claude Support
- Path with space in causes problem HOT 1
- Update docs to include `response_format` for OpenAI ChatTextGenerator; `responseFormat` is supported but not documented HOT 1
- error TS2589: Type instantiation is excessively deep and possibly infinite. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from modelfusion.