Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
# This code was automatically generated from the Elasticsearch Specification
# See https://github.com/elastic/elasticsearch-specification
# See Elasticsearch::ES_SPECIFICATION_COMMIT for commit hash.
module Elasticsearch
module API
module Inference
module Actions
# Create a custom inference endpoint.
# The custom service gives more control over how to interact with external inference services that aren't explicitly supported through dedicated integrations.
# The custom service gives you the ability to define the headers, url, query parameters, request body, and secrets.
# The custom service supports the template replacement functionality, which enables you to define a template that can be replaced with the value associated with that key.
# Templates are portions of a string that start with `${` and end with `}`.
# The parameters `secret_parameters` and `task_settings` are checked for keys for template replacement. Template replacement is supported in the `request`, `headers`, `url`, and `query_parameters`.
# If the definition (key) is not found for a template, an error message is returned.
# In case of an endpoint definition like the following:
#
# ```
# PUT _inference/text_embedding/test-text-embedding
# {
# "service": "custom",
# "service_settings": {
# "secret_parameters": {
# "api_key": "<some api key>"
# },
# "url": "...endpoints.huggingface.cloud/v1/embeddings",
# "headers": {
# "Authorization": "Bearer ${api_key}",
# "Content-Type": "application/json"
# },
# "request": "{\"input\": ${input}}",
# "response": {
# "json_parser": {
# "text_embeddings":"$.data[*].embedding[*]"
# }
# }
# }
# }
# ```
#
# To replace `${api_key}` the `secret_parameters` and `task_settings` are checked for a key named `api_key`.
#
# @option arguments [String] :task_type The type of the inference task that the model will perform. (*Required*)
# @option arguments [String] :custom_inference_id The unique identifier of the inference endpoint. (*Required*)
# @option arguments [Boolean] :error_trace When set to `true` Elasticsearch will include the full stack trace of errors
# when they occur.
# @option arguments [String, Array<String>] :filter_path Comma-separated list of filters in dot notation which reduce the response
# returned by Elasticsearch.
# @option arguments [Boolean] :human When set to `true` will return statistics in a format suitable for humans.
# For example `"exists_time": "1h"` for humans and
# `"exists_time_in_millis": 3600000` for computers. When disabled the human
# readable values will be omitted. This makes sense for responses being consumed
# only by machines.
# @option arguments [Boolean] :pretty If set to `true` the returned JSON will be "pretty-formatted". Only use
# this option for debugging only.
# @option arguments [Hash] :headers Custom HTTP headers
# @option arguments [Hash] :body request body
#
# @see https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-inference-put-custom
#
def put_custom(arguments = {})
request_opts = { endpoint: arguments[:endpoint] || 'inference.put_custom' }

defined_params = [:task_type, :custom_inference_id].each_with_object({}) do |variable, set_variables|
set_variables[variable] = arguments[variable] if arguments.key?(variable)
end
request_opts[:defined_params] = defined_params unless defined_params.empty?

raise ArgumentError, "Required argument 'task_type' missing" unless arguments[:task_type]

unless arguments[:custom_inference_id]
raise ArgumentError,
"Required argument 'custom_inference_id' missing"
end

arguments = arguments.clone
headers = arguments.delete(:headers) || {}

body = arguments.delete(:body)

_task_type = arguments.delete(:task_type)

_custom_inference_id = arguments.delete(:custom_inference_id)

method = Elasticsearch::API::HTTP_PUT
path = "_inference/#{Utils.listify(_task_type)}/#{Utils.listify(_custom_inference_id)}"
params = Utils.process_params(arguments)

Elasticsearch::API::Response.new(
perform_request(method, path, params, body, headers, request_opts)
)
end
end
end
end
end
36 changes: 36 additions & 0 deletions elasticsearch-api/spec/unit/actions/inference/put_custom_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

require 'spec_helper'

describe 'client#inference.put_custom' do
let(:expected_args) do
[
'PUT',
'_inference/foo/baz',
{},
nil,
{},
{ defined_params: { custom_inference_id: 'baz', task_type: 'foo' },
endpoint: 'inference.put_custom' }
]
end

it 'performs the request' do
expect(client_double.inference.put_custom(task_type: 'foo', custom_inference_id: 'baz')).to be_a Elasticsearch::API::Response
end
end