-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
44 changed files
with
1,296 additions
and
49 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
module Roseflow | ||
module Actions | ||
module AI | ||
class Chat | ||
extend Roseflow::Action | ||
|
||
expects :messages | ||
expects :model | ||
expects :options, default: {} | ||
|
||
promises :response | ||
|
||
executed do |context| | ||
context[:response] = context.model.chat(context.options.merge(messages: context.messages)) | ||
end | ||
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
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,23 @@ | ||
# frozen_string_literal: true | ||
|
||
module Roseflow | ||
module AI | ||
module ModelInterface | ||
def call(**options) | ||
raise NotImplementedError, "Model must implement #call" | ||
end | ||
|
||
def chat(**options) | ||
raise NotImplementedError, "Model must implement #chat" | ||
end | ||
|
||
def completion(**options) | ||
raise NotImplementedError, "Model must implement #completion" | ||
end | ||
|
||
def operations | ||
raise NotImplementedError, "Model must implement #operations" | ||
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,13 @@ | ||
# frozen_string_literal: true | ||
|
||
module Roseflow | ||
module AI | ||
module Models | ||
class BaseAdapter | ||
def initialize(model) | ||
@model = model | ||
end | ||
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,22 @@ | ||
# frozen_string_literal: true | ||
|
||
module Roseflow | ||
module AI | ||
module Models | ||
class InstanceFactory | ||
def self.create(name, provider) | ||
provider_model = provider.models.find(name) | ||
raise ModelInstanceNotFoundError, "Instance for #{name} not found" if provider_model.nil? | ||
|
||
begin | ||
provider_config = PROVIDER_GEMS.find { |gem, settings| settings[:name] == provider.name.to_sym }.last | ||
adapter_class = Models.const_get(provider_config.fetch(:adapter_class)) | ||
adapter_class.new(provider_model) | ||
rescue => exception | ||
raise NotImplementedError, "Model adapter for provider '#{provider.name}' not implemented" | ||
end | ||
end | ||
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,22 @@ | ||
# frozen_string_literal: true | ||
|
||
require "roseflow/ai/model_interface" | ||
require "roseflow/ai/models/base_adapter" | ||
|
||
module Roseflow | ||
module AI | ||
module Models | ||
class OpenAIAdapter < BaseAdapter | ||
include ModelInterface | ||
|
||
def call(operation, options, &block) | ||
@model.call(operation, options, &block) | ||
end | ||
|
||
def chat(options, &block) | ||
@model.chat(options.delete(:messages), options, &block) | ||
end | ||
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,22 @@ | ||
# frozen_string_literal: true | ||
|
||
require "roseflow/ai/model_interface" | ||
require "roseflow/ai/models/base_adapter" | ||
|
||
module Roseflow | ||
module AI | ||
module Models | ||
class OpenRouterAdapter < OpenAIAdapter | ||
include ModelInterface | ||
|
||
def call(operation, options, &block) | ||
@model.call(operation, options, &block) | ||
end | ||
|
||
def chat(options, &block) | ||
@model.chat(options.delete(:messages), options, &block) | ||
end | ||
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 |
---|---|---|
@@ -1,30 +1,32 @@ | ||
# frozen_string_literal: true | ||
|
||
require "roseflow/openai/provider" | ||
require "roseflow/provider_repository" | ||
require "roseflow/ai/providers/instance_factory" | ||
|
||
module Roseflow | ||
module AI | ||
class Provider | ||
def initialize(name:, credentials:) | ||
attr_reader :name | ||
|
||
def initialize(name:, config:) | ||
instance = create_adapted_instance(config) | ||
@name = name | ||
@credentials = credentials | ||
initialize_provider | ||
@instance = instance | ||
end | ||
|
||
def models | ||
@models ||= provider.models | ||
@models ||= instance.models | ||
end | ||
|
||
private | ||
|
||
attr_reader :name, :credentials, :provider | ||
attr_reader :instance | ||
|
||
def initialize_provider | ||
case name | ||
when :openai | ||
@provider = Roseflow::OpenAI::Provider.new(credentials: credentials) | ||
end | ||
def create_adapted_instance(config) | ||
Providers::InstanceFactory.create(config) | ||
end | ||
end # Provider | ||
|
||
ProviderNotFoundError = ProviderRepository::ProviderNotFoundError | ||
end # AI | ||
end # Roseflow |
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,23 @@ | ||
# frozen_string_literal: true | ||
|
||
module Roseflow | ||
module AI | ||
module ProviderInterface | ||
def call | ||
raise NotImplementedError, "Provider must implement #call" | ||
end | ||
|
||
def chat | ||
raise NotImplementedError, "Provider must implement #chat" | ||
end | ||
|
||
def completion | ||
raise NotImplementedError, "Provider must implement #completion" | ||
end | ||
|
||
def models | ||
raise NotImplementedError, "Provider must implement #models" | ||
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,17 @@ | ||
# frozen_string_literal: true | ||
|
||
require "roseflow/ai/provider_interface" | ||
|
||
module Roseflow | ||
module AI | ||
module Providers | ||
class BaseAdapter | ||
include ProviderInterface | ||
|
||
def initialize(provider) | ||
@provider = provider | ||
end | ||
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,22 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "./openai_adapter" | ||
require_relative "./openrouter_adapter" | ||
|
||
module Roseflow | ||
module AI | ||
module Providers | ||
class InstanceFactory | ||
def self.create(config) | ||
begin | ||
adapter = Providers.const_get(config.fetch(:adapter_class)) | ||
klass = Object.const_get("#{config.fetch(:namespace)}::Provider") | ||
adapter.new(klass.new) | ||
rescue => exception | ||
raise NotImplementedError, "Adapter for provider #{config.fetch(:name)} not implemented" | ||
end | ||
end | ||
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,15 @@ | ||
# frozen_string_literal: true | ||
|
||
require "roseflow/ai/providers/base_adapter" | ||
|
||
module Roseflow | ||
module AI | ||
module Providers | ||
class OpenAIAdapter < BaseAdapter | ||
def models | ||
@provider.models | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.