Skip to content

Improve Ollama to implement #summarize and #default_dimension#462

Merged
andreibondarev merged 14 commits into
patterns-ai-core:mainfrom
mission-met:feature/ollama-improvements
Feb 24, 2024
Merged

Improve Ollama to implement #summarize and #default_dimension#462
andreibondarev merged 14 commits into
patterns-ai-core:mainfrom
mission-met:feature/ollama-improvements

Conversation

@rickychilcott

@rickychilcott rickychilcott commented Feb 5, 2024

Copy link
Copy Markdown
Contributor

Because Ollama can run multiple models, embed will return different embedding vector sizes. Based on the current running model, we will generate an embedding and return the size in default_dimension instead of nil. I was not able to find a way to get the size of the embeddings in the docs (see https://github.com/ollama/ollama/blob/main/docs/api.md#show-model-information)

Also, I wanted to implement summarize for Ollama, and it seems that there is no dedicated endpoint for that operation. I used the pattern from OpenAI's ChatGPT to use a prompt to get the summary. However, I was consistently getting "Sure, here is a summary of the text" at the beginning of my response, so I modified the prompt specifically for llama2 to give only a summary. I'm not sure it's ideal, because each model will have its own best prompt to return a summary.

In addition, I added specs for complete, summarize, and default_dimension. Since Ollama can chunk output, I couldn't find a way to shim the tests using the existing patterns (something previous devs were seemingly also stumped by). I added VCR which will at least capture the right chunked response and serve it up. However, it will not actually chunk in the same way it does live, so I had to make a minor change to the streaming code to handle it from VCR and live. I don't believe I've broken streaming.

Addresses #380 for Ollama.

@rickychilcott

rickychilcott commented Feb 5, 2024

Copy link
Copy Markdown
Contributor Author

I know #144 was talking about VCR, but it's a slightly older discussion, and hand-mocking Faraday calls seems sub-optimal too.

Comment thread lib/langchain/llm/response/ollama_response.rb Outdated
Comment thread lib/langchain/llm/ollama.rb Outdated
@rickychilcott

Copy link
Copy Markdown
Contributor Author

Ok , I've done this now and merged in main.

I checked the embedding size for the four most popular LLMs at https://ollama.com/library, and there definitely is a long tail of models provided by Ollama. I added tests for all and also added a vcr spec for the least popular model (i.e. tinydolphin).

@andreibondarev

Copy link
Copy Markdown
Collaborator

@rickychilcott Thanks for this PR btw! Makes me think that we need to implement this dynamic default_dimension() method on other LLMs (in follow-up PRs of course).

Could you please upmerge this PR?

Comment thread lib/langchain/llm/response/base_response.rb Outdated
Comment thread lib/langchain/llm/ollama.rb Outdated
@rickychilcott

Copy link
Copy Markdown
Contributor Author

Merged main. Thanks @andreibondarev

@rickychilcott

Copy link
Copy Markdown
Contributor Author

@andreibondarev - anything I need to do to get this merged?

@andreibondarev

Copy link
Copy Markdown
Collaborator

@rickychilcott Looks great, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants