openapi_openai 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +72 -0
- data/README.md +171 -0
- data/Rakefile +10 -0
- data/bin/bundle +109 -0
- data/bin/byebug +27 -0
- data/bin/coderay +27 -0
- data/bin/htmldiff +27 -0
- data/bin/ldiff +27 -0
- data/bin/pry +27 -0
- data/bin/racc +27 -0
- data/bin/rake +27 -0
- data/bin/rspec +27 -0
- data/bin/rubocop +27 -0
- data/bin/ruby-parse +27 -0
- data/bin/ruby-rewrite +27 -0
- data/docs/ChatCompletionFunctions.md +22 -0
- data/docs/ChatCompletionRequestMessage.md +24 -0
- data/docs/ChatCompletionRequestMessageFunctionCall.md +20 -0
- data/docs/ChatCompletionResponseMessage.md +22 -0
- data/docs/ChatCompletionStreamResponseDelta.md +22 -0
- data/docs/CreateChatCompletionRequest.md +44 -0
- data/docs/CreateChatCompletionRequestFunctionCall.md +49 -0
- data/docs/CreateChatCompletionRequestFunctionCallOneOf.md +18 -0
- data/docs/CreateChatCompletionRequestModel.md +47 -0
- data/docs/CreateChatCompletionRequestStop.md +49 -0
- data/docs/CreateChatCompletionResponse.md +28 -0
- data/docs/CreateChatCompletionResponseChoicesInner.md +22 -0
- data/docs/CreateChatCompletionStreamResponse.md +26 -0
- data/docs/CreateChatCompletionStreamResponseChoicesInner.md +22 -0
- data/docs/CreateCompletionRequest.md +48 -0
- data/docs/CreateCompletionRequestModel.md +47 -0
- data/docs/CreateCompletionRequestPrompt.md +53 -0
- data/docs/CreateCompletionRequestStop.md +49 -0
- data/docs/CreateCompletionResponse.md +28 -0
- data/docs/CreateCompletionResponseChoicesInner.md +24 -0
- data/docs/CreateCompletionResponseChoicesInnerLogprobs.md +24 -0
- data/docs/CreateCompletionResponseUsage.md +22 -0
- data/docs/CreateEditRequest.md +28 -0
- data/docs/CreateEditRequestModel.md +47 -0
- data/docs/CreateEditResponse.md +24 -0
- data/docs/CreateEditResponseChoicesInner.md +24 -0
- data/docs/CreateEmbeddingRequest.md +22 -0
- data/docs/CreateEmbeddingRequestInput.md +53 -0
- data/docs/CreateEmbeddingRequestModel.md +47 -0
- data/docs/CreateEmbeddingResponse.md +24 -0
- data/docs/CreateEmbeddingResponseDataInner.md +22 -0
- data/docs/CreateEmbeddingResponseUsage.md +20 -0
- data/docs/CreateFineTuneRequest.md +40 -0
- data/docs/CreateFineTuneRequestModel.md +47 -0
- data/docs/CreateImageRequest.md +26 -0
- data/docs/CreateModerationRequest.md +20 -0
- data/docs/CreateModerationRequestInput.md +49 -0
- data/docs/CreateModerationRequestModel.md +47 -0
- data/docs/CreateModerationResponse.md +22 -0
- data/docs/CreateModerationResponseResultsInner.md +22 -0
- data/docs/CreateModerationResponseResultsInnerCategories.md +30 -0
- data/docs/CreateModerationResponseResultsInnerCategoryScores.md +30 -0
- data/docs/CreateTranscriptionRequestModel.md +47 -0
- data/docs/CreateTranscriptionResponse.md +18 -0
- data/docs/CreateTranslationResponse.md +18 -0
- data/docs/DeleteFileResponse.md +22 -0
- data/docs/DeleteModelResponse.md +22 -0
- data/docs/Error.md +24 -0
- data/docs/ErrorResponse.md +18 -0
- data/docs/FineTune.md +42 -0
- data/docs/FineTuneEvent.md +24 -0
- data/docs/ImagesResponse.md +20 -0
- data/docs/ImagesResponseDataInner.md +20 -0
- data/docs/ListFilesResponse.md +20 -0
- data/docs/ListFineTuneEventsResponse.md +20 -0
- data/docs/ListFineTunesResponse.md +20 -0
- data/docs/ListModelsResponse.md +20 -0
- data/docs/Model.md +24 -0
- data/docs/OpenAIApi.md +1499 -0
- data/docs/OpenAIFile.md +32 -0
- data/git_push.sh +57 -0
- data/lib/openapi_openai/api/open_ai_api.rb +1583 -0
- data/lib/openapi_openai/api_client.rb +393 -0
- data/lib/openapi_openai/api_error.rb +58 -0
- data/lib/openapi_openai/configuration.rb +291 -0
- data/lib/openapi_openai/models/chat_completion_functions.rb +244 -0
- data/lib/openapi_openai/models/chat_completion_request_message.rb +285 -0
- data/lib/openapi_openai/models/chat_completion_request_message_function_call.rb +226 -0
- data/lib/openapi_openai/models/chat_completion_response_message.rb +276 -0
- data/lib/openapi_openai/models/chat_completion_stream_response_delta.rb +269 -0
- data/lib/openapi_openai/models/create_chat_completion_request.rb +537 -0
- data/lib/openapi_openai/models/create_chat_completion_request_function_call.rb +106 -0
- data/lib/openapi_openai/models/create_chat_completion_request_function_call_one_of.rb +222 -0
- data/lib/openapi_openai/models/create_chat_completion_request_model.rb +105 -0
- data/lib/openapi_openai/models/create_chat_completion_request_stop.rb +106 -0
- data/lib/openapi_openai/models/create_chat_completion_response.rb +296 -0
- data/lib/openapi_openai/models/create_chat_completion_response_choices_inner.rb +266 -0
- data/lib/openapi_openai/models/create_chat_completion_stream_response.rb +287 -0
- data/lib/openapi_openai/models/create_chat_completion_stream_response_choices_inner.rb +266 -0
- data/lib/openapi_openai/models/create_completion_request.rb +586 -0
- data/lib/openapi_openai/models/create_completion_request_model.rb +105 -0
- data/lib/openapi_openai/models/create_completion_request_prompt.rb +108 -0
- data/lib/openapi_openai/models/create_completion_request_stop.rb +106 -0
- data/lib/openapi_openai/models/create_completion_response.rb +296 -0
- data/lib/openapi_openai/models/create_completion_response_choices_inner.rb +299 -0
- data/lib/openapi_openai/models/create_completion_response_choices_inner_logprobs.rb +249 -0
- data/lib/openapi_openai/models/create_completion_response_usage.rb +253 -0
- data/lib/openapi_openai/models/create_edit_request.rb +362 -0
- data/lib/openapi_openai/models/create_edit_request_model.rb +105 -0
- data/lib/openapi_openai/models/create_edit_response.rb +271 -0
- data/lib/openapi_openai/models/create_edit_response_choices_inner.rb +276 -0
- data/lib/openapi_openai/models/create_embedding_request.rb +247 -0
- data/lib/openapi_openai/models/create_embedding_request_input.rb +108 -0
- data/lib/openapi_openai/models/create_embedding_request_model.rb +105 -0
- data/lib/openapi_openai/models/create_embedding_response.rb +271 -0
- data/lib/openapi_openai/models/create_embedding_response_data_inner.rb +255 -0
- data/lib/openapi_openai/models/create_embedding_response_usage.rb +237 -0
- data/lib/openapi_openai/models/create_fine_tune_request.rb +374 -0
- data/lib/openapi_openai/models/create_fine_tune_request_model.rb +105 -0
- data/lib/openapi_openai/models/create_image_request.rb +341 -0
- data/lib/openapi_openai/models/create_moderation_request.rb +230 -0
- data/lib/openapi_openai/models/create_moderation_request_input.rb +106 -0
- data/lib/openapi_openai/models/create_moderation_request_model.rb +105 -0
- data/lib/openapi_openai/models/create_moderation_response.rb +255 -0
- data/lib/openapi_openai/models/create_moderation_response_results_inner.rb +253 -0
- data/lib/openapi_openai/models/create_moderation_response_results_inner_categories.rb +317 -0
- data/lib/openapi_openai/models/create_moderation_response_results_inner_category_scores.rb +317 -0
- data/lib/openapi_openai/models/create_transcription_request_model.rb +105 -0
- data/lib/openapi_openai/models/create_transcription_response.rb +221 -0
- data/lib/openapi_openai/models/create_translation_response.rb +221 -0
- data/lib/openapi_openai/models/delete_file_response.rb +253 -0
- data/lib/openapi_openai/models/delete_model_response.rb +253 -0
- data/lib/openapi_openai/models/error.rb +261 -0
- data/lib/openapi_openai/models/error_response.rb +221 -0
- data/lib/openapi_openai/models/fine_tune.rb +410 -0
- data/lib/openapi_openai/models/fine_tune_event.rb +269 -0
- data/lib/openapi_openai/models/images_response.rb +239 -0
- data/lib/openapi_openai/models/images_response_data_inner.rb +223 -0
- data/lib/openapi_openai/models/list_files_response.rb +239 -0
- data/lib/openapi_openai/models/list_fine_tune_events_response.rb +239 -0
- data/lib/openapi_openai/models/list_fine_tunes_response.rb +239 -0
- data/lib/openapi_openai/models/list_models_response.rb +239 -0
- data/lib/openapi_openai/models/model.rb +269 -0
- data/lib/openapi_openai/models/open_ai_file.rb +320 -0
- data/lib/openapi_openai/version.rb +15 -0
- data/lib/openapi_openai.rb +99 -0
- data/openapi_openai.gemspec +39 -0
- data/spec/api/open_ai_api_spec.rb +306 -0
- data/spec/models/chat_completion_functions_spec.rb +48 -0
- data/spec/models/chat_completion_request_message_function_call_spec.rb +42 -0
- data/spec/models/chat_completion_request_message_spec.rb +58 -0
- data/spec/models/chat_completion_response_message_spec.rb +52 -0
- data/spec/models/chat_completion_stream_response_delta_spec.rb +52 -0
- data/spec/models/create_chat_completion_request_function_call_one_of_spec.rb +36 -0
- data/spec/models/create_chat_completion_request_function_call_spec.rb +32 -0
- data/spec/models/create_chat_completion_request_model_spec.rb +32 -0
- data/spec/models/create_chat_completion_request_spec.rb +114 -0
- data/spec/models/create_chat_completion_request_stop_spec.rb +32 -0
- data/spec/models/create_chat_completion_response_choices_inner_spec.rb +52 -0
- data/spec/models/create_chat_completion_response_spec.rb +66 -0
- data/spec/models/create_chat_completion_stream_response_choices_inner_spec.rb +52 -0
- data/spec/models/create_chat_completion_stream_response_spec.rb +60 -0
- data/spec/models/create_completion_request_model_spec.rb +32 -0
- data/spec/models/create_completion_request_prompt_spec.rb +32 -0
- data/spec/models/create_completion_request_spec.rb +126 -0
- data/spec/models/create_completion_request_stop_spec.rb +32 -0
- data/spec/models/create_completion_response_choices_inner_logprobs_spec.rb +54 -0
- data/spec/models/create_completion_response_choices_inner_spec.rb +58 -0
- data/spec/models/create_completion_response_spec.rb +66 -0
- data/spec/models/create_completion_response_usage_spec.rb +48 -0
- data/spec/models/create_edit_request_model_spec.rb +32 -0
- data/spec/models/create_edit_request_spec.rb +66 -0
- data/spec/models/create_edit_response_choices_inner_spec.rb +58 -0
- data/spec/models/create_edit_response_spec.rb +54 -0
- data/spec/models/create_embedding_request_input_spec.rb +32 -0
- data/spec/models/create_embedding_request_model_spec.rb +32 -0
- data/spec/models/create_embedding_request_spec.rb +48 -0
- data/spec/models/create_embedding_response_data_inner_spec.rb +48 -0
- data/spec/models/create_embedding_response_spec.rb +54 -0
- data/spec/models/create_embedding_response_usage_spec.rb +42 -0
- data/spec/models/create_fine_tune_request_model_spec.rb +32 -0
- data/spec/models/create_fine_tune_request_spec.rb +102 -0
- data/spec/models/create_image_request_spec.rb +68 -0
- data/spec/models/create_moderation_request_input_spec.rb +32 -0
- data/spec/models/create_moderation_request_model_spec.rb +32 -0
- data/spec/models/create_moderation_request_spec.rb +42 -0
- data/spec/models/create_moderation_response_results_inner_categories_spec.rb +72 -0
- data/spec/models/create_moderation_response_results_inner_category_scores_spec.rb +72 -0
- data/spec/models/create_moderation_response_results_inner_spec.rb +48 -0
- data/spec/models/create_moderation_response_spec.rb +48 -0
- data/spec/models/create_transcription_request_model_spec.rb +32 -0
- data/spec/models/create_transcription_response_spec.rb +36 -0
- data/spec/models/create_translation_response_spec.rb +36 -0
- data/spec/models/delete_file_response_spec.rb +48 -0
- data/spec/models/delete_model_response_spec.rb +48 -0
- data/spec/models/error_response_spec.rb +36 -0
- data/spec/models/error_spec.rb +54 -0
- data/spec/models/fine_tune_event_spec.rb +54 -0
- data/spec/models/fine_tune_spec.rb +108 -0
- data/spec/models/images_response_data_inner_spec.rb +42 -0
- data/spec/models/images_response_spec.rb +42 -0
- data/spec/models/list_files_response_spec.rb +42 -0
- data/spec/models/list_fine_tune_events_response_spec.rb +42 -0
- data/spec/models/list_fine_tunes_response_spec.rb +42 -0
- data/spec/models/list_models_response_spec.rb +42 -0
- data/spec/models/model_spec.rb +54 -0
- data/spec/models/open_ai_file_spec.rb +78 -0
- data/spec/spec_helper.rb +111 -0
- metadata +348 -0
|
@@ -0,0 +1,586 @@
|
|
|
1
|
+
=begin
|
|
2
|
+
#OpenAI API
|
|
3
|
+
|
|
4
|
+
#APIs for sampling from and fine-tuning language models
|
|
5
|
+
|
|
6
|
+
The version of the OpenAPI document: 2.0.0
|
|
7
|
+
Contact: blah+oapicf@cliffano.com
|
|
8
|
+
Generated by: https://openapi-generator.tech
|
|
9
|
+
Generator version: 7.4.0
|
|
10
|
+
|
|
11
|
+
=end
|
|
12
|
+
|
|
13
|
+
require 'date'
|
|
14
|
+
require 'time'
|
|
15
|
+
|
|
16
|
+
module OpenApiOpenAIClient
|
|
17
|
+
class CreateCompletionRequest
|
|
18
|
+
attr_accessor :model
|
|
19
|
+
|
|
20
|
+
attr_accessor :prompt
|
|
21
|
+
|
|
22
|
+
# The suffix that comes after a completion of inserted text.
|
|
23
|
+
attr_accessor :suffix
|
|
24
|
+
|
|
25
|
+
# The maximum number of [tokens](/tokenizer) to generate in the completion. The token count of your prompt plus `max_tokens` cannot exceed the model's context length. [Example Python code](https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb) for counting tokens.
|
|
26
|
+
attr_accessor :max_tokens
|
|
27
|
+
|
|
28
|
+
# What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. We generally recommend altering this or `top_p` but not both.
|
|
29
|
+
attr_accessor :temperature
|
|
30
|
+
|
|
31
|
+
# An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. We generally recommend altering this or `temperature` but not both.
|
|
32
|
+
attr_accessor :top_p
|
|
33
|
+
|
|
34
|
+
# How many completions to generate for each prompt. **Note:** Because this parameter generates many completions, it can quickly consume your token quota. Use carefully and ensure that you have reasonable settings for `max_tokens` and `stop`.
|
|
35
|
+
attr_accessor :n
|
|
36
|
+
|
|
37
|
+
# Whether to stream back partial progress. If set, tokens will be sent as data-only [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format) as they become available, with the stream terminated by a `data: [DONE]` message. [Example Python code](https://github.com/openai/openai-cookbook/blob/main/examples/How_to_stream_completions.ipynb).
|
|
38
|
+
attr_accessor :stream
|
|
39
|
+
|
|
40
|
+
# Include the log probabilities on the `logprobs` most likely tokens, as well the chosen tokens. For example, if `logprobs` is 5, the API will return a list of the 5 most likely tokens. The API will always return the `logprob` of the sampled token, so there may be up to `logprobs+1` elements in the response. The maximum value for `logprobs` is 5.
|
|
41
|
+
attr_accessor :logprobs
|
|
42
|
+
|
|
43
|
+
# Echo back the prompt in addition to the completion
|
|
44
|
+
attr_accessor :echo
|
|
45
|
+
|
|
46
|
+
attr_accessor :stop
|
|
47
|
+
|
|
48
|
+
# Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics. [See more information about frequency and presence penalties.](/docs/api-reference/parameter-details)
|
|
49
|
+
attr_accessor :presence_penalty
|
|
50
|
+
|
|
51
|
+
# Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim. [See more information about frequency and presence penalties.](/docs/api-reference/parameter-details)
|
|
52
|
+
attr_accessor :frequency_penalty
|
|
53
|
+
|
|
54
|
+
# Generates `best_of` completions server-side and returns the \"best\" (the one with the highest log probability per token). Results cannot be streamed. When used with `n`, `best_of` controls the number of candidate completions and `n` specifies how many to return – `best_of` must be greater than `n`. **Note:** Because this parameter generates many completions, it can quickly consume your token quota. Use carefully and ensure that you have reasonable settings for `max_tokens` and `stop`.
|
|
55
|
+
attr_accessor :best_of
|
|
56
|
+
|
|
57
|
+
# Modify the likelihood of specified tokens appearing in the completion. Accepts a json object that maps tokens (specified by their token ID in the GPT tokenizer) to an associated bias value from -100 to 100. You can use this [tokenizer tool](/tokenizer?view=bpe) (which works for both GPT-2 and GPT-3) to convert text to token IDs. Mathematically, the bias is added to the logits generated by the model prior to sampling. The exact effect will vary per model, but values between -1 and 1 should decrease or increase likelihood of selection; values like -100 or 100 should result in a ban or exclusive selection of the relevant token. As an example, you can pass `{\"50256\": -100}` to prevent the <|endoftext|> token from being generated.
|
|
58
|
+
attr_accessor :logit_bias
|
|
59
|
+
|
|
60
|
+
# A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. [Learn more](/docs/guides/safety-best-practices/end-user-ids).
|
|
61
|
+
attr_accessor :user
|
|
62
|
+
|
|
63
|
+
# Attribute mapping from ruby-style variable name to JSON key.
|
|
64
|
+
def self.attribute_map
|
|
65
|
+
{
|
|
66
|
+
:'model' => :'model',
|
|
67
|
+
:'prompt' => :'prompt',
|
|
68
|
+
:'suffix' => :'suffix',
|
|
69
|
+
:'max_tokens' => :'max_tokens',
|
|
70
|
+
:'temperature' => :'temperature',
|
|
71
|
+
:'top_p' => :'top_p',
|
|
72
|
+
:'n' => :'n',
|
|
73
|
+
:'stream' => :'stream',
|
|
74
|
+
:'logprobs' => :'logprobs',
|
|
75
|
+
:'echo' => :'echo',
|
|
76
|
+
:'stop' => :'stop',
|
|
77
|
+
:'presence_penalty' => :'presence_penalty',
|
|
78
|
+
:'frequency_penalty' => :'frequency_penalty',
|
|
79
|
+
:'best_of' => :'best_of',
|
|
80
|
+
:'logit_bias' => :'logit_bias',
|
|
81
|
+
:'user' => :'user'
|
|
82
|
+
}
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# Returns all the JSON keys this model knows about
|
|
86
|
+
def self.acceptable_attributes
|
|
87
|
+
attribute_map.values
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Attribute type mapping.
|
|
91
|
+
def self.openapi_types
|
|
92
|
+
{
|
|
93
|
+
:'model' => :'CreateCompletionRequestModel',
|
|
94
|
+
:'prompt' => :'CreateCompletionRequestPrompt',
|
|
95
|
+
:'suffix' => :'String',
|
|
96
|
+
:'max_tokens' => :'Integer',
|
|
97
|
+
:'temperature' => :'Float',
|
|
98
|
+
:'top_p' => :'Float',
|
|
99
|
+
:'n' => :'Integer',
|
|
100
|
+
:'stream' => :'Boolean',
|
|
101
|
+
:'logprobs' => :'Integer',
|
|
102
|
+
:'echo' => :'Boolean',
|
|
103
|
+
:'stop' => :'CreateCompletionRequestStop',
|
|
104
|
+
:'presence_penalty' => :'Float',
|
|
105
|
+
:'frequency_penalty' => :'Float',
|
|
106
|
+
:'best_of' => :'Integer',
|
|
107
|
+
:'logit_bias' => :'Object',
|
|
108
|
+
:'user' => :'String'
|
|
109
|
+
}
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# List of attributes with nullable: true
|
|
113
|
+
def self.openapi_nullable
|
|
114
|
+
Set.new([
|
|
115
|
+
:'prompt',
|
|
116
|
+
:'suffix',
|
|
117
|
+
:'max_tokens',
|
|
118
|
+
:'temperature',
|
|
119
|
+
:'top_p',
|
|
120
|
+
:'n',
|
|
121
|
+
:'stream',
|
|
122
|
+
:'logprobs',
|
|
123
|
+
:'echo',
|
|
124
|
+
:'stop',
|
|
125
|
+
:'presence_penalty',
|
|
126
|
+
:'frequency_penalty',
|
|
127
|
+
:'best_of',
|
|
128
|
+
:'logit_bias',
|
|
129
|
+
])
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# Initializes the object
|
|
133
|
+
# @param [Hash] attributes Model attributes in the form of hash
|
|
134
|
+
def initialize(attributes = {})
|
|
135
|
+
if (!attributes.is_a?(Hash))
|
|
136
|
+
fail ArgumentError, "The input argument (attributes) must be a hash in `OpenApiOpenAIClient::CreateCompletionRequest` initialize method"
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# check to see if the attribute exists and convert string to symbol for hash key
|
|
140
|
+
attributes = attributes.each_with_object({}) { |(k, v), h|
|
|
141
|
+
if (!self.class.attribute_map.key?(k.to_sym))
|
|
142
|
+
fail ArgumentError, "`#{k}` is not a valid attribute in `OpenApiOpenAIClient::CreateCompletionRequest`. Please check the name to make sure it's valid. List of attributes: " + self.class.attribute_map.keys.inspect
|
|
143
|
+
end
|
|
144
|
+
h[k.to_sym] = v
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if attributes.key?(:'model')
|
|
148
|
+
self.model = attributes[:'model']
|
|
149
|
+
else
|
|
150
|
+
self.model = nil
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
if attributes.key?(:'prompt')
|
|
154
|
+
self.prompt = attributes[:'prompt']
|
|
155
|
+
else
|
|
156
|
+
self.prompt = nil
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
if attributes.key?(:'suffix')
|
|
160
|
+
self.suffix = attributes[:'suffix']
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
if attributes.key?(:'max_tokens')
|
|
164
|
+
self.max_tokens = attributes[:'max_tokens']
|
|
165
|
+
else
|
|
166
|
+
self.max_tokens = 16
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
if attributes.key?(:'temperature')
|
|
170
|
+
self.temperature = attributes[:'temperature']
|
|
171
|
+
else
|
|
172
|
+
self.temperature = 1
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
if attributes.key?(:'top_p')
|
|
176
|
+
self.top_p = attributes[:'top_p']
|
|
177
|
+
else
|
|
178
|
+
self.top_p = 1
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
if attributes.key?(:'n')
|
|
182
|
+
self.n = attributes[:'n']
|
|
183
|
+
else
|
|
184
|
+
self.n = 1
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
if attributes.key?(:'stream')
|
|
188
|
+
self.stream = attributes[:'stream']
|
|
189
|
+
else
|
|
190
|
+
self.stream = false
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
if attributes.key?(:'logprobs')
|
|
194
|
+
self.logprobs = attributes[:'logprobs']
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
if attributes.key?(:'echo')
|
|
198
|
+
self.echo = attributes[:'echo']
|
|
199
|
+
else
|
|
200
|
+
self.echo = false
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
if attributes.key?(:'stop')
|
|
204
|
+
self.stop = attributes[:'stop']
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
if attributes.key?(:'presence_penalty')
|
|
208
|
+
self.presence_penalty = attributes[:'presence_penalty']
|
|
209
|
+
else
|
|
210
|
+
self.presence_penalty = 0
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
if attributes.key?(:'frequency_penalty')
|
|
214
|
+
self.frequency_penalty = attributes[:'frequency_penalty']
|
|
215
|
+
else
|
|
216
|
+
self.frequency_penalty = 0
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
if attributes.key?(:'best_of')
|
|
220
|
+
self.best_of = attributes[:'best_of']
|
|
221
|
+
else
|
|
222
|
+
self.best_of = 1
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
if attributes.key?(:'logit_bias')
|
|
226
|
+
self.logit_bias = attributes[:'logit_bias']
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
if attributes.key?(:'user')
|
|
230
|
+
self.user = attributes[:'user']
|
|
231
|
+
end
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
# Show invalid properties with the reasons. Usually used together with valid?
|
|
235
|
+
# @return Array for valid properties with the reasons
|
|
236
|
+
def list_invalid_properties
|
|
237
|
+
warn '[DEPRECATED] the `list_invalid_properties` method is obsolete'
|
|
238
|
+
invalid_properties = Array.new
|
|
239
|
+
if @model.nil?
|
|
240
|
+
invalid_properties.push('invalid value for "model", model cannot be nil.')
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
if !@max_tokens.nil? && @max_tokens < 0
|
|
244
|
+
invalid_properties.push('invalid value for "max_tokens", must be greater than or equal to 0.')
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
if !@temperature.nil? && @temperature > 2
|
|
248
|
+
invalid_properties.push('invalid value for "temperature", must be smaller than or equal to 2.')
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
if !@temperature.nil? && @temperature < 0
|
|
252
|
+
invalid_properties.push('invalid value for "temperature", must be greater than or equal to 0.')
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
if !@top_p.nil? && @top_p > 1
|
|
256
|
+
invalid_properties.push('invalid value for "top_p", must be smaller than or equal to 1.')
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
if !@top_p.nil? && @top_p < 0
|
|
260
|
+
invalid_properties.push('invalid value for "top_p", must be greater than or equal to 0.')
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
if !@n.nil? && @n > 128
|
|
264
|
+
invalid_properties.push('invalid value for "n", must be smaller than or equal to 128.')
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
if !@n.nil? && @n < 1
|
|
268
|
+
invalid_properties.push('invalid value for "n", must be greater than or equal to 1.')
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
if !@logprobs.nil? && @logprobs > 5
|
|
272
|
+
invalid_properties.push('invalid value for "logprobs", must be smaller than or equal to 5.')
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
if !@logprobs.nil? && @logprobs < 0
|
|
276
|
+
invalid_properties.push('invalid value for "logprobs", must be greater than or equal to 0.')
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
if !@presence_penalty.nil? && @presence_penalty > 2
|
|
280
|
+
invalid_properties.push('invalid value for "presence_penalty", must be smaller than or equal to 2.')
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
if !@presence_penalty.nil? && @presence_penalty < -2
|
|
284
|
+
invalid_properties.push('invalid value for "presence_penalty", must be greater than or equal to -2.')
|
|
285
|
+
end
|
|
286
|
+
|
|
287
|
+
if !@frequency_penalty.nil? && @frequency_penalty > 2
|
|
288
|
+
invalid_properties.push('invalid value for "frequency_penalty", must be smaller than or equal to 2.')
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
if !@frequency_penalty.nil? && @frequency_penalty < -2
|
|
292
|
+
invalid_properties.push('invalid value for "frequency_penalty", must be greater than or equal to -2.')
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
if !@best_of.nil? && @best_of > 20
|
|
296
|
+
invalid_properties.push('invalid value for "best_of", must be smaller than or equal to 20.')
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
if !@best_of.nil? && @best_of < 0
|
|
300
|
+
invalid_properties.push('invalid value for "best_of", must be greater than or equal to 0.')
|
|
301
|
+
end
|
|
302
|
+
|
|
303
|
+
invalid_properties
|
|
304
|
+
end
|
|
305
|
+
|
|
306
|
+
# Check to see if the all the properties in the model are valid
|
|
307
|
+
# @return true if the model is valid
|
|
308
|
+
def valid?
|
|
309
|
+
warn '[DEPRECATED] the `valid?` method is obsolete'
|
|
310
|
+
return false if @model.nil?
|
|
311
|
+
return false if !@max_tokens.nil? && @max_tokens < 0
|
|
312
|
+
return false if !@temperature.nil? && @temperature > 2
|
|
313
|
+
return false if !@temperature.nil? && @temperature < 0
|
|
314
|
+
return false if !@top_p.nil? && @top_p > 1
|
|
315
|
+
return false if !@top_p.nil? && @top_p < 0
|
|
316
|
+
return false if !@n.nil? && @n > 128
|
|
317
|
+
return false if !@n.nil? && @n < 1
|
|
318
|
+
return false if !@logprobs.nil? && @logprobs > 5
|
|
319
|
+
return false if !@logprobs.nil? && @logprobs < 0
|
|
320
|
+
return false if !@presence_penalty.nil? && @presence_penalty > 2
|
|
321
|
+
return false if !@presence_penalty.nil? && @presence_penalty < -2
|
|
322
|
+
return false if !@frequency_penalty.nil? && @frequency_penalty > 2
|
|
323
|
+
return false if !@frequency_penalty.nil? && @frequency_penalty < -2
|
|
324
|
+
return false if !@best_of.nil? && @best_of > 20
|
|
325
|
+
return false if !@best_of.nil? && @best_of < 0
|
|
326
|
+
true
|
|
327
|
+
end
|
|
328
|
+
|
|
329
|
+
# Custom attribute writer method with validation
|
|
330
|
+
# @param [Object] max_tokens Value to be assigned
|
|
331
|
+
def max_tokens=(max_tokens)
|
|
332
|
+
if !max_tokens.nil? && max_tokens < 0
|
|
333
|
+
fail ArgumentError, 'invalid value for "max_tokens", must be greater than or equal to 0.'
|
|
334
|
+
end
|
|
335
|
+
|
|
336
|
+
@max_tokens = max_tokens
|
|
337
|
+
end
|
|
338
|
+
|
|
339
|
+
# Custom attribute writer method with validation
|
|
340
|
+
# @param [Object] temperature Value to be assigned
|
|
341
|
+
def temperature=(temperature)
|
|
342
|
+
if !temperature.nil? && temperature > 2
|
|
343
|
+
fail ArgumentError, 'invalid value for "temperature", must be smaller than or equal to 2.'
|
|
344
|
+
end
|
|
345
|
+
|
|
346
|
+
if !temperature.nil? && temperature < 0
|
|
347
|
+
fail ArgumentError, 'invalid value for "temperature", must be greater than or equal to 0.'
|
|
348
|
+
end
|
|
349
|
+
|
|
350
|
+
@temperature = temperature
|
|
351
|
+
end
|
|
352
|
+
|
|
353
|
+
# Custom attribute writer method with validation
|
|
354
|
+
# @param [Object] top_p Value to be assigned
|
|
355
|
+
def top_p=(top_p)
|
|
356
|
+
if !top_p.nil? && top_p > 1
|
|
357
|
+
fail ArgumentError, 'invalid value for "top_p", must be smaller than or equal to 1.'
|
|
358
|
+
end
|
|
359
|
+
|
|
360
|
+
if !top_p.nil? && top_p < 0
|
|
361
|
+
fail ArgumentError, 'invalid value for "top_p", must be greater than or equal to 0.'
|
|
362
|
+
end
|
|
363
|
+
|
|
364
|
+
@top_p = top_p
|
|
365
|
+
end
|
|
366
|
+
|
|
367
|
+
# Custom attribute writer method with validation
|
|
368
|
+
# @param [Object] n Value to be assigned
|
|
369
|
+
def n=(n)
|
|
370
|
+
if !n.nil? && n > 128
|
|
371
|
+
fail ArgumentError, 'invalid value for "n", must be smaller than or equal to 128.'
|
|
372
|
+
end
|
|
373
|
+
|
|
374
|
+
if !n.nil? && n < 1
|
|
375
|
+
fail ArgumentError, 'invalid value for "n", must be greater than or equal to 1.'
|
|
376
|
+
end
|
|
377
|
+
|
|
378
|
+
@n = n
|
|
379
|
+
end
|
|
380
|
+
|
|
381
|
+
# Custom attribute writer method with validation
|
|
382
|
+
# @param [Object] logprobs Value to be assigned
|
|
383
|
+
def logprobs=(logprobs)
|
|
384
|
+
if !logprobs.nil? && logprobs > 5
|
|
385
|
+
fail ArgumentError, 'invalid value for "logprobs", must be smaller than or equal to 5.'
|
|
386
|
+
end
|
|
387
|
+
|
|
388
|
+
if !logprobs.nil? && logprobs < 0
|
|
389
|
+
fail ArgumentError, 'invalid value for "logprobs", must be greater than or equal to 0.'
|
|
390
|
+
end
|
|
391
|
+
|
|
392
|
+
@logprobs = logprobs
|
|
393
|
+
end
|
|
394
|
+
|
|
395
|
+
# Custom attribute writer method with validation
|
|
396
|
+
# @param [Object] presence_penalty Value to be assigned
|
|
397
|
+
def presence_penalty=(presence_penalty)
|
|
398
|
+
if !presence_penalty.nil? && presence_penalty > 2
|
|
399
|
+
fail ArgumentError, 'invalid value for "presence_penalty", must be smaller than or equal to 2.'
|
|
400
|
+
end
|
|
401
|
+
|
|
402
|
+
if !presence_penalty.nil? && presence_penalty < -2
|
|
403
|
+
fail ArgumentError, 'invalid value for "presence_penalty", must be greater than or equal to -2.'
|
|
404
|
+
end
|
|
405
|
+
|
|
406
|
+
@presence_penalty = presence_penalty
|
|
407
|
+
end
|
|
408
|
+
|
|
409
|
+
# Custom attribute writer method with validation
|
|
410
|
+
# @param [Object] frequency_penalty Value to be assigned
|
|
411
|
+
def frequency_penalty=(frequency_penalty)
|
|
412
|
+
if !frequency_penalty.nil? && frequency_penalty > 2
|
|
413
|
+
fail ArgumentError, 'invalid value for "frequency_penalty", must be smaller than or equal to 2.'
|
|
414
|
+
end
|
|
415
|
+
|
|
416
|
+
if !frequency_penalty.nil? && frequency_penalty < -2
|
|
417
|
+
fail ArgumentError, 'invalid value for "frequency_penalty", must be greater than or equal to -2.'
|
|
418
|
+
end
|
|
419
|
+
|
|
420
|
+
@frequency_penalty = frequency_penalty
|
|
421
|
+
end
|
|
422
|
+
|
|
423
|
+
# Custom attribute writer method with validation
|
|
424
|
+
# @param [Object] best_of Value to be assigned
|
|
425
|
+
def best_of=(best_of)
|
|
426
|
+
if !best_of.nil? && best_of > 20
|
|
427
|
+
fail ArgumentError, 'invalid value for "best_of", must be smaller than or equal to 20.'
|
|
428
|
+
end
|
|
429
|
+
|
|
430
|
+
if !best_of.nil? && best_of < 0
|
|
431
|
+
fail ArgumentError, 'invalid value for "best_of", must be greater than or equal to 0.'
|
|
432
|
+
end
|
|
433
|
+
|
|
434
|
+
@best_of = best_of
|
|
435
|
+
end
|
|
436
|
+
|
|
437
|
+
# Checks equality by comparing each attribute.
|
|
438
|
+
# @param [Object] Object to be compared
|
|
439
|
+
def ==(o)
|
|
440
|
+
return true if self.equal?(o)
|
|
441
|
+
self.class == o.class &&
|
|
442
|
+
model == o.model &&
|
|
443
|
+
prompt == o.prompt &&
|
|
444
|
+
suffix == o.suffix &&
|
|
445
|
+
max_tokens == o.max_tokens &&
|
|
446
|
+
temperature == o.temperature &&
|
|
447
|
+
top_p == o.top_p &&
|
|
448
|
+
n == o.n &&
|
|
449
|
+
stream == o.stream &&
|
|
450
|
+
logprobs == o.logprobs &&
|
|
451
|
+
echo == o.echo &&
|
|
452
|
+
stop == o.stop &&
|
|
453
|
+
presence_penalty == o.presence_penalty &&
|
|
454
|
+
frequency_penalty == o.frequency_penalty &&
|
|
455
|
+
best_of == o.best_of &&
|
|
456
|
+
logit_bias == o.logit_bias &&
|
|
457
|
+
user == o.user
|
|
458
|
+
end
|
|
459
|
+
|
|
460
|
+
# @see the `==` method
|
|
461
|
+
# @param [Object] Object to be compared
|
|
462
|
+
def eql?(o)
|
|
463
|
+
self == o
|
|
464
|
+
end
|
|
465
|
+
|
|
466
|
+
# Calculates hash code according to all attributes.
|
|
467
|
+
# @return [Integer] Hash code
|
|
468
|
+
def hash
|
|
469
|
+
[model, prompt, suffix, max_tokens, temperature, top_p, n, stream, logprobs, echo, stop, presence_penalty, frequency_penalty, best_of, logit_bias, user].hash
|
|
470
|
+
end
|
|
471
|
+
|
|
472
|
+
# Builds the object from hash
|
|
473
|
+
# @param [Hash] attributes Model attributes in the form of hash
|
|
474
|
+
# @return [Object] Returns the model itself
|
|
475
|
+
def self.build_from_hash(attributes)
|
|
476
|
+
return nil unless attributes.is_a?(Hash)
|
|
477
|
+
attributes = attributes.transform_keys(&:to_sym)
|
|
478
|
+
transformed_hash = {}
|
|
479
|
+
openapi_types.each_pair do |key, type|
|
|
480
|
+
if attributes.key?(attribute_map[key]) && attributes[attribute_map[key]].nil?
|
|
481
|
+
transformed_hash["#{key}"] = nil
|
|
482
|
+
elsif type =~ /\AArray<(.*)>/i
|
|
483
|
+
# check to ensure the input is an array given that the attribute
|
|
484
|
+
# is documented as an array but the input is not
|
|
485
|
+
if attributes[attribute_map[key]].is_a?(Array)
|
|
486
|
+
transformed_hash["#{key}"] = attributes[attribute_map[key]].map { |v| _deserialize($1, v) }
|
|
487
|
+
end
|
|
488
|
+
elsif !attributes[attribute_map[key]].nil?
|
|
489
|
+
transformed_hash["#{key}"] = _deserialize(type, attributes[attribute_map[key]])
|
|
490
|
+
end
|
|
491
|
+
end
|
|
492
|
+
new(transformed_hash)
|
|
493
|
+
end
|
|
494
|
+
|
|
495
|
+
# Deserializes the data based on type
|
|
496
|
+
# @param string type Data type
|
|
497
|
+
# @param string value Value to be deserialized
|
|
498
|
+
# @return [Object] Deserialized data
|
|
499
|
+
def self._deserialize(type, value)
|
|
500
|
+
case type.to_sym
|
|
501
|
+
when :Time
|
|
502
|
+
Time.parse(value)
|
|
503
|
+
when :Date
|
|
504
|
+
Date.parse(value)
|
|
505
|
+
when :String
|
|
506
|
+
value.to_s
|
|
507
|
+
when :Integer
|
|
508
|
+
value.to_i
|
|
509
|
+
when :Float
|
|
510
|
+
value.to_f
|
|
511
|
+
when :Boolean
|
|
512
|
+
if value.to_s =~ /\A(true|t|yes|y|1)\z/i
|
|
513
|
+
true
|
|
514
|
+
else
|
|
515
|
+
false
|
|
516
|
+
end
|
|
517
|
+
when :Object
|
|
518
|
+
# generic object (usually a Hash), return directly
|
|
519
|
+
value
|
|
520
|
+
when /\AArray<(?<inner_type>.+)>\z/
|
|
521
|
+
inner_type = Regexp.last_match[:inner_type]
|
|
522
|
+
value.map { |v| _deserialize(inner_type, v) }
|
|
523
|
+
when /\AHash<(?<k_type>.+?), (?<v_type>.+)>\z/
|
|
524
|
+
k_type = Regexp.last_match[:k_type]
|
|
525
|
+
v_type = Regexp.last_match[:v_type]
|
|
526
|
+
{}.tap do |hash|
|
|
527
|
+
value.each do |k, v|
|
|
528
|
+
hash[_deserialize(k_type, k)] = _deserialize(v_type, v)
|
|
529
|
+
end
|
|
530
|
+
end
|
|
531
|
+
else # model
|
|
532
|
+
# models (e.g. Pet) or oneOf
|
|
533
|
+
klass = OpenApiOpenAIClient.const_get(type)
|
|
534
|
+
klass.respond_to?(:openapi_any_of) || klass.respond_to?(:openapi_one_of) ? klass.build(value) : klass.build_from_hash(value)
|
|
535
|
+
end
|
|
536
|
+
end
|
|
537
|
+
|
|
538
|
+
# Returns the string representation of the object
|
|
539
|
+
# @return [String] String presentation of the object
|
|
540
|
+
def to_s
|
|
541
|
+
to_hash.to_s
|
|
542
|
+
end
|
|
543
|
+
|
|
544
|
+
# to_body is an alias to to_hash (backward compatibility)
|
|
545
|
+
# @return [Hash] Returns the object in the form of hash
|
|
546
|
+
def to_body
|
|
547
|
+
to_hash
|
|
548
|
+
end
|
|
549
|
+
|
|
550
|
+
# Returns the object in the form of hash
|
|
551
|
+
# @return [Hash] Returns the object in the form of hash
|
|
552
|
+
def to_hash
|
|
553
|
+
hash = {}
|
|
554
|
+
self.class.attribute_map.each_pair do |attr, param|
|
|
555
|
+
value = self.send(attr)
|
|
556
|
+
if value.nil?
|
|
557
|
+
is_nullable = self.class.openapi_nullable.include?(attr)
|
|
558
|
+
next if !is_nullable || (is_nullable && !instance_variable_defined?(:"@#{attr}"))
|
|
559
|
+
end
|
|
560
|
+
|
|
561
|
+
hash[param] = _to_hash(value)
|
|
562
|
+
end
|
|
563
|
+
hash
|
|
564
|
+
end
|
|
565
|
+
|
|
566
|
+
# Outputs non-array value in the form of hash
|
|
567
|
+
# For object, use to_hash. Otherwise, just return the value
|
|
568
|
+
# @param [Object] value Any valid value
|
|
569
|
+
# @return [Hash] Returns the value in the form of hash
|
|
570
|
+
def _to_hash(value)
|
|
571
|
+
if value.is_a?(Array)
|
|
572
|
+
value.compact.map { |v| _to_hash(v) }
|
|
573
|
+
elsif value.is_a?(Hash)
|
|
574
|
+
{}.tap do |hash|
|
|
575
|
+
value.each { |k, v| hash[k] = _to_hash(v) }
|
|
576
|
+
end
|
|
577
|
+
elsif value.respond_to? :to_hash
|
|
578
|
+
value.to_hash
|
|
579
|
+
else
|
|
580
|
+
value
|
|
581
|
+
end
|
|
582
|
+
end
|
|
583
|
+
|
|
584
|
+
end
|
|
585
|
+
|
|
586
|
+
end
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
=begin
|
|
2
|
+
#OpenAI API
|
|
3
|
+
|
|
4
|
+
#APIs for sampling from and fine-tuning language models
|
|
5
|
+
|
|
6
|
+
The version of the OpenAPI document: 2.0.0
|
|
7
|
+
Contact: blah+oapicf@cliffano.com
|
|
8
|
+
Generated by: https://openapi-generator.tech
|
|
9
|
+
Generator version: 7.4.0
|
|
10
|
+
|
|
11
|
+
=end
|
|
12
|
+
|
|
13
|
+
require 'date'
|
|
14
|
+
require 'time'
|
|
15
|
+
|
|
16
|
+
module OpenApiOpenAIClient
|
|
17
|
+
# ID of the model to use. You can use the [List models](/docs/api-reference/models/list) API to see all of your available models, or see our [Model overview](/docs/models/overview) for descriptions of them.
|
|
18
|
+
module CreateCompletionRequestModel
|
|
19
|
+
class << self
|
|
20
|
+
# List of class defined in oneOf (OpenAPI v3)
|
|
21
|
+
def openapi_one_of
|
|
22
|
+
[
|
|
23
|
+
:'String'
|
|
24
|
+
]
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Builds the object
|
|
28
|
+
# @param [Mixed] Data to be matched against the list of oneOf items
|
|
29
|
+
# @return [Object] Returns the model or the data itself
|
|
30
|
+
def build(data)
|
|
31
|
+
# Go through the list of oneOf items and attempt to identify the appropriate one.
|
|
32
|
+
# Note:
|
|
33
|
+
# - We do not attempt to check whether exactly one item matches.
|
|
34
|
+
# - No advanced validation of types in some cases (e.g. "x: { type: string }" will happily match { x: 123 })
|
|
35
|
+
# due to the way the deserialization is made in the base_object template (it just casts without verifying).
|
|
36
|
+
# - TODO: scalar values are de facto behaving as if they were nullable.
|
|
37
|
+
# - TODO: logging when debugging is set.
|
|
38
|
+
openapi_one_of.each do |klass|
|
|
39
|
+
begin
|
|
40
|
+
next if klass == :AnyType # "nullable: true"
|
|
41
|
+
typed_data = find_and_cast_into_type(klass, data)
|
|
42
|
+
return typed_data if typed_data
|
|
43
|
+
rescue # rescue all errors so we keep iterating even if the current item lookup raises
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
openapi_one_of.include?(:AnyType) ? data : nil
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
SchemaMismatchError = Class.new(StandardError)
|
|
53
|
+
|
|
54
|
+
# Note: 'File' is missing here because in the regular case we get the data _after_ a call to JSON.parse.
|
|
55
|
+
def find_and_cast_into_type(klass, data)
|
|
56
|
+
return if data.nil?
|
|
57
|
+
|
|
58
|
+
case klass.to_s
|
|
59
|
+
when 'Boolean'
|
|
60
|
+
return data if data.instance_of?(TrueClass) || data.instance_of?(FalseClass)
|
|
61
|
+
when 'Float'
|
|
62
|
+
return data if data.instance_of?(Float)
|
|
63
|
+
when 'Integer'
|
|
64
|
+
return data if data.instance_of?(Integer)
|
|
65
|
+
when 'Time'
|
|
66
|
+
return Time.parse(data)
|
|
67
|
+
when 'Date'
|
|
68
|
+
return Date.parse(data)
|
|
69
|
+
when 'String'
|
|
70
|
+
return data if data.instance_of?(String)
|
|
71
|
+
when 'Object' # "type: object"
|
|
72
|
+
return data if data.instance_of?(Hash)
|
|
73
|
+
when /\AArray<(?<sub_type>.+)>\z/ # "type: array"
|
|
74
|
+
if data.instance_of?(Array)
|
|
75
|
+
sub_type = Regexp.last_match[:sub_type]
|
|
76
|
+
return data.map { |item| find_and_cast_into_type(sub_type, item) }
|
|
77
|
+
end
|
|
78
|
+
when /\AHash<String, (?<sub_type>.+)>\z/ # "type: object" with "additionalProperties: { ... }"
|
|
79
|
+
if data.instance_of?(Hash) && data.keys.all? { |k| k.instance_of?(Symbol) || k.instance_of?(String) }
|
|
80
|
+
sub_type = Regexp.last_match[:sub_type]
|
|
81
|
+
return data.each_with_object({}) { |(k, v), hsh| hsh[k] = find_and_cast_into_type(sub_type, v) }
|
|
82
|
+
end
|
|
83
|
+
else # model
|
|
84
|
+
const = OpenApiOpenAIClient.const_get(klass)
|
|
85
|
+
if const
|
|
86
|
+
if const.respond_to?(:openapi_one_of) # nested oneOf model
|
|
87
|
+
model = const.build(data)
|
|
88
|
+
return model if model
|
|
89
|
+
else
|
|
90
|
+
# raise if data contains keys that are not known to the model
|
|
91
|
+
raise if const.respond_to?(:acceptable_attributes) && !(data.keys - const.acceptable_attributes).empty?
|
|
92
|
+
model = const.build_from_hash(data)
|
|
93
|
+
return model if model
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
raise # if no match by now, raise
|
|
99
|
+
rescue
|
|
100
|
+
raise SchemaMismatchError, "#{data} doesn't match the #{klass} type"
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
end
|