News for hackers. Stuff that matters.
Re:Gemma, Ollama and LangChainGo by lolinder on Sunday February 25, 2024 @10:09AM (#39501356)
Just a note from my own experiments with Ollama—it's tempting to use LangChain as a sort of unofficial Ollama client library because it's so easy to set up, but be warned that because LangChain abstracts over many providers it doesn't actually give access to the full power of the Ollama API, so for anything other than very simple toy demos it's not a great choice unless you actually want to use the other LangChain abstractions.

The first pain point you're likely to run into is that Ollama provides a running binary context that represents all the processing your GPU has done in the conversation so far, but the last time I looked into LangChain they wanted you to build up a plaintext context on the client and ask Ollama to re-process it on every request. This works well as part of the wider suite of LangChain tools, but isn't great if you really just need Ollama.

For JS/TS and Python, Ollama now provides an official client library which presumably does take advantage of all Ollama features [0]. For other languages, the Ollama API is actually very simple [1] and easy to wrap yourself, and I'd recommend doing so unless you specifically need some of the abstractions LangChain provides.

[0] https://ollama.com/blog/python-javascript-libraries

[1] https://github.com/ollama/ollama/blob/main/docs/api.md#gener...

Re:Gemma, Ollama and LangChainGo by threecheese on Sunday February 25, 2024 @12:11PM (#39502530)
Can you share a source or docs describing the “running binary context”? I am using this stack (as a dev, not an ml engineer) and appreciate the tip but would like to understand the delta better. Just spent a few minutes searching through code but don’t see anything obvious. Thanks!
Re:Gemma, Ollama and LangChainGo by lolinder on Sunday February 25, 2024 @12:33PM (#39502749)
My second link documents the `context` parameter for Ollama. It's unfortunately pretty hard to search for because the term is overloaded.

`context` is a number array that gets returned with each /generate request and can be returned to the server with the next request. There's not much more documentation than what's there in that link, but as I understand it the array is a JSON representation of the current state of inference at the time that Ollama returned. So when you send the context back up with a second request, Ollama can just load that array into memory and then start inference with the new prompt, rather than having to re-encode the whole conversation up to that point.

Re:Gemma, Ollama and LangChainGo by CuriouslyC on Sunday February 25, 2024 @01:58PM (#39503629)
I'm pretty sure it's just a cache of the context embedding.
Re:Gemma, Ollama and LangChainGo by rybosome on Sunday February 25, 2024 @12:55PM (#39503003)
Since you brought up that context, do you happen to know how it works?

I tested it, and it definitely works like a history - eg you can feed it to a model and ask it a question about what you just talked about, and it works.

Does Ollama essentially just convert the conversation so far into a binary representation, then convert it back to text and feed it back to the model ahead of your newest query? Or is it doing something more involved?

Re:Gemma, Ollama and LangChainGo by CuriouslyC on Sunday February 25, 2024 @01:57PM (#39503620)
Each time you do inference you're basically converting the context to an embedding, why not cache that?
Re:Gemma, Ollama and LangChainGo by rybosome on Sunday February 25, 2024 @06:31PM (#39506059)
Thanks.

I wish it was better documented, have a ton of questions about how it functions in practice that I’ll end up trying to reverse engineer.

Like, what’s the lifetime of this context? If I load a new model into memory then reload the original, is that context valid? Is it valid if the computer restarts? Is it valid if the model gets updated?

Not an LLM expert but based on your explanation that it’s related to embedding (and another comment that Ollama loads it directly into memory) then I’m guessing that the model updating its weights definitely invalidates the context. Not so sure about the other options, like unloading/reloading the same model.

Re:Gemma, Ollama and LangChainGo by CuriouslyC on Sunday February 25, 2024 @07:43PM (#39506513)
It's easier to reason about if you understand what it is. That binary data is basically a big vector representation of a textual context's contents. I suspect that it doesn't matter which model you use with the context binary, as ollama is handling providing it to the model.
Re:Gemma, Ollama and LangChainGo by rybosome on Monday February 26, 2024 @12:43AM (#39508004)
Thanks for the insights.

Any particular resources you’d recommend to learn more about this?

So if I’m understanding it correctly, there’s one consistent way that Ollama will vectorize a set of text. Perhaps there are various ways one could, but Ollama chooses one.

What about multimodality? Ie taking a context from a prompt to llava to identify an image, with further questions about the contents of that image? Any non-llava model would definitely hallucinate, but would llava?

Re:Gemma, Ollama and LangChainGo by CuriouslyC on Monday February 26, 2024 @01:34AM (#39508200)
Multimodal models use embeddings as well, the difference there is that they've been trained to associate the same position in latent space to text and to the image that text describes, that way it can turn a textual response into an image and vice versa. A lot of models use CLIP, an embedding method from openAI.
Re:Gemma, Ollama and LangChainGo by jondwillis on Sunday February 25, 2024 @02:13PM (#39503764)
AFAIU, typically LangChain will use the underlying (Ollama’s in this case) SDK, and prefers using the native configuration types and options, so support for whatever advanced features could be added!