Skip to content

Commit

Permalink
Give titan conversation ability
Browse files Browse the repository at this point in the history
  • Loading branch information
apmiller108 committed Jan 25, 2025
1 parent 2b0ce8c commit aac58a5
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 11 deletions.
10 changes: 5 additions & 5 deletions TODO.org
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@
* TODO normalize the routes
* TODO Add a scrollspy to conversations
- [ ] each heading can be the first few words of the user message
* TODO support adding files to the prompt [5/9]
* TODO support adding files to the prompt [7/12]
- [X] Add file input and attachment to generate text requests
- [X] Refactor: normalize models across vendors. Just an array. Move default
to property on the struct.
- [X] Refactor: Add vendor to model.
- [X] Pass in just the generate text request obj to invoke model, forward that
to the client and wrap it in a vendor specific request object
- [X] Instantiate the client based on vendor from selected model
- [ ] Remove AWS models from conversations
- [ ] Remove LLAM model
- [ ] Fix summaries not completing
- [ ] Include the image in the HTTP request
- [X] Add support for conversations to titan
- [X] Remove LLAM model
- [ ] Disable file input on titan selected. Maybe add capabilities to model config?
- [ ] Include the image in the HTTP request Anthropic models
- [ ] implement prompt caching for large user messages
https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching
- [ ] Look into client side validation of mime types and file size
Expand Down
11 changes: 8 additions & 3 deletions app/models/generate_text_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ def response_token_count

# @returns [Array<Hash>] A tuple of a user message and assistant response
def to_turn
GenerativeText::Anthropic::Turn.for(self)
case model.vendor
when :anthropic
GenerativeText::Anthropic::Turn.for(self)
when :aws
GenerativeText::AWS::Turn.for(self)
end
end

def model
Expand All @@ -71,10 +76,10 @@ def acceptable_file

def response_wrapper_class
case model.vendor
when :aws
GenerativeText::AWS::Client::InvokeModelResponse
when :anthropic
GenerativeText::Anthropic::InvokeModelResponse
when :aws
GenerativeText::AWS::Client::InvokeModelResponse
end
end
end
3 changes: 2 additions & 1 deletion lib/generative_text.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
class GenerativeText
InvalidRequestError = Class.new(StandardError)

Model = Struct.new(:api_name, :name, :max_tokens, :vendor, keyword_init: true)
Model = Struct.new(:api_name, :name, :max_tokens, :vendor, :capabilities, keyword_init: true)
Model::Capabilities = Struct.new(:images, :presets)

MODELS = [
*Anthropic::MODELS,
Expand Down
1 change: 0 additions & 1 deletion lib/generative_text/aws.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ def self.vendor = :aws

MODELS = [
Model.new(api_name: 'amazon.titan-text-express-v1', name: 'AWS Titan Express', vendor:, max_tokens: 8000),
Model.new(api_name: 'meta.llama2-70b-chat-v1', name: 'Llama 2 70b', vendor:, max_tokens: 4000)
].freeze
end
end
17 changes: 16 additions & 1 deletion lib/generative_text/aws/client/invoke_model_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,26 @@ def to_h

def body
{
'inputText' => prompt,
'inputText' => input_text,
'textGenerationConfig' => text_gen_config
}.to_json
end

def input_text
<<~TXT
#{generate_text_request.system_message}
#{turns.join}
TXT
end

def turns
generate_text_request.conversation.exchange << next_turn
end

def next_turn
format(Turn::TEMPLATE, prompt:, response: '')
end

def text_gen_config
{
'maxTokenCount' => model.max_tokens,
Expand Down
15 changes: 15 additions & 0 deletions lib/generative_text/aws/turn.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class GenerativeText
module AWS
class Turn
TEMPLATE =
<<~TURN.freeze
User: %<prompt>s
Bot: %<response>s
TURN

def self.for(generate_text_request)
format(TEMPLATE, prompt: generate_text_request.prompt, response: generate_text_request.response_content)
end
end
end
end

0 comments on commit aac58a5

Please sign in to comment.