Live data from Hacker News

Structured Outputs with Ollama

ollama.com

41–50 of 75 posts

Re: Structured Outputs with Ollama

#41
post #30
post #29

Earlier quoted context omitted.

A lot of the time you can prevent this by prefilling the output with ```\n and stopping at ```.

care to explain further? I am not sure I understand you fully

LLMs are essentially text completion machines, they get some text in as a list of tokens, and their job is to predict what token comes next.

Typically, you're interested in getting more than just one token out of them, so you run them in a loop. You start with the token list containing just the user's message, run the LLM with that list, append the newly obtained token at the end, run the LLM again to see what comes after that new token and so on, until a special end-of-sentence token is generated and the loop terminates.

There's no reason why you have to start with nothing but the users' message, though. You can let the user specify the beginning of the LLM's supposed completion, and ask the LLM to start from that point, instead of generating its completion from scratch. This essentially ensures that what the LLM says begins with a specific string.

Not alll APIs expose this feature, there are good safety reasons not to, but all LLMs are capable of doing it in principle, and doing it with the open ones is trivial.

LLMs are typically trained to output markdown, which uses ```language_name to denote code blocks in language_name, so that user interfaces like Chat GPT's web UI can do proper syntax highlighting.

Therefore, if you make your LLM think that it already started a completion, and that completion began with ```json, it will predict what's most likely to come after that delimiter, and that would be a JSON block.

Re: Structured Outputs with Ollama

#42
post #30
post #29

Earlier quoted context omitted.

A lot of the time you can prevent this by prefilling the output with ```\n and stopping at ```.

care to explain further? I am not sure I understand you fully

The way the LLMs work is you feed them a vector, array of numbers, that represents a sequence of tokens.

You turn the crank and you get a probability distribution for the next token in the sequence. You then sample the distribution to get the next token, append it to the vector, and do it again and again.

Thus the typical LLM have no memory as such, it inferes what it was thinking by looking at what it has already said and uses that to figure out what to say next, so to speak.

The characters in the input prompt are converted to these tokens, but there are also special tokens such as start of input, end of input, start of output and end of output. The end of output token is how the LLM "tells you" it's done talking.

Normally in a chat scenario these special tokens are inserted by the LLM front-end, say Ollama/llama.cpp in this case.

However if you interface more directly you need to add these yourself, and hence can prefill out the output before feeding the vector to the LLM for the first time, and thus the LLM will "think" it already started writing code say, and thus it is likely to continue doing so.

Re: Structured Outputs with Ollama

#43
post #28
post #15

Is there a best approach for providing structured input to LLMs? Example: feed in 100 sentences and get each one classified in different ways. It's easy to get structured data out, but my approach of prefixing line numbers seems clumsy.

Models are trained on Markdown, JSON and various programming languages, so either one of these should work. However, in this case, you're best of giving the model sentences one by one to avoid it being confused. If you structure the prompt like "Classify the following sentence, here are the rules ...." + sentence, then you should be hitting prefix cache and get even better performance than when doing a single query.…

Good idea. I might try that. I think classification quality improves when it has following sentences. I'll have to see if feeding them sequentially makes it worse.

Re: Structured Outputs with Ollama

#44

This is a fantastic news! I spent hours on fine tuning my prompt to summarise text and output in JSON and still have some issues sometimes. Is this feature available also with Go?

It looks like the structured output feature is available in Go, with the `format` field.

  type GenerateRequest struct {
    ...
    // Format specifies the format to return a response in.
    Format json.RawMessage `json:"format,omitempty"`
https://github.com/ollama/ollama/blob/de52b6c2f90ff220ed9469...

Re: Structured Outputs with Ollama

#46
post #24

Earlier quoted context omitted.

I authored the blog with some other contributors and worked on the feature (PR: https://github.com/ollama/ollama/pull/7900 ). The current implementation uses llama.cpp GBNF grammars. The more recent research (Outlines, XGrammar) points to potentially speeding up the sampling process through FSTs and GPU parallelism.

If you want avoid startup cost, llguidance [0] has no compilation phase and by far the fullest JSON support [1] of any library. I did a PoC llama.cpp integration [2] though our focus is mostly server-side [3]. [0] https://github.com/guidance-ai/llguidance [1] https://github.com/guidance-ai/llguidance/blob/main/parser/s... [2] https://github.com/ggerganov/llama.cpp/pull/10224 [3] https://github.com/guidance-ai/llgtrt

I have been thinking about your PR regularly, and pondering about how we should go about getting this merged in.

I really want to see support for additional grammar engines merged into llama.cpp, and I'm a big fan of the work you did on this.

Re: Structured Outputs with Ollama

#49
post #22

Earlier quoted context omitted.

Remember that you still need to include an instruction to produce a CSV to get the prompt into the right context to generate a CSV that makes sense. Otherwise, you may get output that is technically in the CSV format but doesn't make any sense because the model was actually trying to write a paragraph response and the token sampler just selected really low-probability tokens that the model didn't really want to say.

It seems ollama only supports JSON Schema. Interestingly, JSON Schema has much less of this problem than say CSV - when the model is forced to produce `{"first_key":` it will generally understand it's supposed to continue in JSON. It still helps to tell it the schema though, especially due to weird tokenization issues you can get otherwise.

> It seems ollama only supports JSON Schema.

"Encoding" CSV as JSON is trivial though, so make it output JSON then parse the array-of-arrays into CSV :)

Re: Structured Outputs with Ollama

#50

Yay! It works. I used gemma2:2b and gave it below text You have spent 190 at Fresh Mart. Current balance: 5098 and it gave below output {\n\"amount\": 190,\n\"balance\": 5098 ,\"category\": \"Shopping\",\n\"place\":\"Fresh Mart\"\n}

That's some pretty inconsistent JSON, but I guess that makes sense when using a really small model and gemma on top of that.
Post reply on HN