-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Ollama as a supported provider #10
Draft
ldmosquera
wants to merge
9
commits into
crmne:main
Choose a base branch
from
ldmosquera:ollama-provider
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9b387c1
Providers are disabled unless specifically configured
ldmosquera a030bcc
WIP: first bits of Ollama provider, adapted from Gemini
ldmosquera 0ab3337
Basic chat functionality
ldmosquera 52306ba
Add support for NDJSON streaming
ldmosquera e7ae937
Basic streaming functionality
ldmosquera a475ead
Basic embedding functionality
ldmosquera fe39682
Mention Ollama in getting-started.md
ldmosquera 1801adc
rubocop autocorrects
ldmosquera 6bcdfa2
More rubocop appeasing
ldmosquera File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
module RubyLLM | ||
module Providers | ||
# Native Ollama API implementation | ||
module Ollama | ||
extend Provider | ||
extend Ollama::Chat | ||
extend Ollama::Embeddings | ||
extend Ollama::Models | ||
extend Ollama::Streaming | ||
|
||
module_function | ||
|
||
def enabled? | ||
!!RubyLLM.config.ollama_api_base_url | ||
end | ||
|
||
def api_base | ||
RubyLLM.config.ollama_api_base_url | ||
end | ||
|
||
def headers | ||
{} | ||
end | ||
|
||
def capabilities | ||
Ollama::Capabilities | ||
end | ||
|
||
def slug | ||
'ollama' | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
# frozen_string_literal: true | ||
|
||
module RubyLLM | ||
module Providers | ||
module Ollama | ||
# Determines capabilities for Ollama | ||
module Capabilities # rubocop:disable Metrics/ModuleLength | ||
module_function | ||
|
||
# Returns the context window size (input token limit) for the given model | ||
# @param model_id [String] the model identifier | ||
# @return [Integer] the context window size in tokens | ||
def context_window_for(model_id) | ||
# FIXME: revise | ||
4_192 # Sensible (and conservative) default for unknown models | ||
end | ||
|
||
# Returns the maximum output tokens for the given model | ||
# @param model_id [String] the model identifier | ||
# @return [Integer] the maximum output tokens | ||
def max_tokens_for(model_id) | ||
# FIXME: revise | ||
32_768 | ||
end | ||
|
||
# Returns the input price per million tokens for the given model | ||
# @param model_id [String] the model identifier | ||
# @return [Float] the price per million tokens in USD | ||
def input_price_for(model_id) | ||
0.0 | ||
end | ||
|
||
# Returns the output price per million tokens for the given model | ||
# @param model_id [String] the model identifier | ||
# @return [Float] the price per million tokens in USD | ||
def output_price_for(model_id) | ||
0.0 | ||
end | ||
|
||
# Determines if the model supports vision (image/video) inputs | ||
# @param model_id [String] the model identifier | ||
# @return [Boolean] true if the model supports vision inputs | ||
def supports_vision?(model_id) | ||
# FIXME: revise | ||
false | ||
end | ||
|
||
# Determines if the model supports function calling | ||
# @param model_id [String] the model identifier | ||
# @return [Boolean] true if the model supports function calling | ||
def supports_functions?(model_id) | ||
# FIXME: revise | ||
false | ||
end | ||
|
||
# Determines if the model supports JSON mode | ||
# @param model_id [String] the model identifier | ||
# @return [Boolean] true if the model supports JSON mode | ||
def supports_json_mode?(model_id) | ||
# FIXME: revise | ||
false | ||
end | ||
|
||
# Formats the model ID into a human-readable display name | ||
# @param model_id [String] the model identifier | ||
# @return [String] the formatted display name | ||
def format_display_name(model_id) | ||
model_id | ||
.delete_prefix('models/') | ||
.split('-') | ||
.map(&:capitalize) | ||
.join(' ') | ||
.gsub(/(\d+\.\d+)/, ' \1') # Add space before version numbers | ||
.gsub(/\s+/, ' ') # Clean up multiple spaces | ||
.strip | ||
end | ||
|
||
# Determines if the model supports context caching | ||
# @param model_id [String] the model identifier | ||
# @return [Boolean] true if the model supports caching | ||
def supports_caching?(model_id) | ||
# FIXME: revise | ||
true | ||
end | ||
|
||
# Determines if the model supports tuning | ||
# @param model_id [String] the model identifier | ||
# @return [Boolean] true if the model supports tuning | ||
def supports_tuning?(model_id) | ||
# FIXME: revise | ||
false | ||
end | ||
|
||
# Determines if the model supports audio inputs | ||
# @param model_id [String] the model identifier | ||
# @return [Boolean] true if the model supports audio inputs | ||
def supports_audio?(model_id) | ||
# FIXME: revise | ||
false | ||
end | ||
|
||
# Returns the type of model (chat, embedding, image) | ||
# @param model_id [String] the model identifier | ||
# @return [String] the model type | ||
def model_type(model_id) | ||
# FIXME: revise | ||
'chat' | ||
end | ||
|
||
# Returns the model family identifier | ||
# @param model_id [String] the model identifier | ||
# @return [String] the model family identifier | ||
def model_family(model_id) # rubocop:disable Metrics/CyclomaticComplexity,Metrics/MethodLength | ||
'other' | ||
end | ||
|
||
# Returns the context length for the model | ||
# @param model_id [String] the model identifier | ||
# @return [Integer] the context length in tokens | ||
def context_length(model_id) | ||
context_window_for(model_id) | ||
end | ||
|
||
# Default input price for unknown models | ||
# @return [Float] the default input price per million tokens | ||
def default_input_price | ||
0.0 | ||
end | ||
|
||
# Default output price for unknown models | ||
# @return [Float] the default output price per million tokens | ||
def default_output_price | ||
0.0 | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not super sure about this; as far as I could see, Ollama uses newline delimited JSON lines rather than standard event streams.
Of the API providers, I only have a Gemini key and it does work after this commit (both streaming and synch) so this might be correct.