Class: RubyConversations::Client
- Inherits:
-
Object
- Object
- RubyConversations::Client
- Defined in:
- lib/ruby_conversations/client.rb
Overview
HTTP client for interacting with the conversations API
Constant Summary collapse
- PROMPT_ATTRIBUTES =
%w[ id name message role temperature valid_placeholders created_at updated_at latest_version_id llm ].freeze
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Instance Method Summary collapse
-
#fetch_all_conversation_templates ⇒ Array<Hash>
Fetch all available conversation templates.
-
#fetch_conversation_template(template_name) ⇒ Hash
Fetch a conversation template by name.
-
#fetch_prompt(name) ⇒ Hash
Fetch a prompt by name.
-
#initialize(url:, jwt_secret:) ⇒ Client
constructor
Initialize a new API client.
-
#store_conversation(conversation) ⇒ Hash
Store a new conversation.
-
#store_conversation_or_message(conversation, message) ⇒ Hash
Store a conversation or add a message to an existing conversation.
-
#store_message(conversation, message) ⇒ Hash
Store a message in an existing conversation.
Constructor Details
#initialize(url:, jwt_secret:) ⇒ Client
Initialize a new API client
19 20 21 22 23 24 25 26 |
# File 'lib/ruby_conversations/client.rb', line 19 def initialize(url:, jwt_secret:) @url = url @jwt_secret = jwt_secret @jwt_token = nil @jwt_expiration = nil @logger = Logger.new($stdout) @client = build_client end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
10 11 12 |
# File 'lib/ruby_conversations/client.rb', line 10 def client @client end |
Instance Method Details
#fetch_all_conversation_templates ⇒ Array<Hash>
Fetch all available conversation templates
73 74 75 76 |
# File 'lib/ruby_conversations/client.rb', line 73 def fetch_all_conversation_templates response = client.get('api/ai_conversations') handle_response(response) end |
#fetch_conversation_template(template_name) ⇒ Hash
Fetch a conversation template by name
66 67 68 69 |
# File 'lib/ruby_conversations/client.rb', line 66 def fetch_conversation_template(template_name) response = client.get("api/ai_conversations/#{template_name}") handle_response(response) end |
#fetch_prompt(name) ⇒ Hash
Fetch a prompt by name
81 82 83 84 85 |
# File 'lib/ruby_conversations/client.rb', line 81 def fetch_prompt(name) response = client.get("api/prompts/#{name}") data = handle_response(response) map_prompt_attributes(data) end |
#store_conversation(conversation) ⇒ Hash
Store a new conversation
44 45 46 47 48 49 50 51 |
# File 'lib/ruby_conversations/client.rb', line 44 def store_conversation(conversation) response = client.post('api/ai_conversations', conversation.conversation_attributes_for_storage) data = handle_response(response) raise RubyConversations::Error, 'API response missing conversation ID' unless data['id'].present? conversation.id = data['id'] data end |
#store_conversation_or_message(conversation, message) ⇒ Hash
Store a conversation or add a message to an existing conversation
32 33 34 35 36 37 38 |
# File 'lib/ruby_conversations/client.rb', line 32 def (conversation, ) if conversation.id.present? (conversation, ) else store_conversation(conversation) end end |
#store_message(conversation, message) ⇒ Hash
Store a message in an existing conversation
57 58 59 60 61 |
# File 'lib/ruby_conversations/client.rb', line 57 def (conversation, ) response = client.post("api/ai_conversations/#{conversation.id}/ai_messages", { ai_message: .attributes_for_api }) handle_response(response) end |