Module: Foobara::LlmBackedExecuteMethod::ClassMethods

Defined in:
foobara-llm-backed-command-1.0.5/src/llm_backed_execute_method.rb

Instance Method Summary collapse

Instance Method Details

#build_llm_instructions(user_association_depth, assistant_association_depth) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'src/llm_backed_execute_method.rb', line 280

def build_llm_instructions(user_association_depth, assistant_association_depth)
  <<~INSTRUCTIONS
    You are implementing an API for a command named #{scoped_full_name} which has the following description:

    #{description}

    Here is the inputs JSON schema for the data you will receive:

    #{inputs_json_schema(user_association_depth)}

    Here is the result JSON schema:

    #{result_json_schema(assistant_association_depth)}

    You will receive 1 message containing only JSON data according to the inputs JSON schema above
    and you will generate a JSON response that is a valid response according to the result JSON schema above.

    You will reply with nothing more than the JSON you've generated so that the calling code
    can successfully parse your answer.
  INSTRUCTIONS
end

#inputs_json_schema(association_depth) ⇒ Object



218
219
220
221
222
223
# File 'src/llm_backed_execute_method.rb', line 218

def inputs_json_schema(association_depth)
  JsonSchemaGenerator.to_json_schema(
    inputs_type_without_llm_integration_inputs,
    association_depth:
  )
end

#inputs_type_without_llm_integration_inputs ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'src/llm_backed_execute_method.rb', line 225

def inputs_type_without_llm_integration_inputs
  type_declaration = Util.deep_dup(inputs_type.declaration_data)

  element_type_declarations = type_declaration[:element_type_declarations]

  changed = false

  LLM_INTEGRATION_KEYS.each do |key|
    if element_type_declarations.key?(key)
      changed = true
      element_type_declarations.delete(key)
    end
  end

  if type_declaration.key?(:defaults)
    LLM_INTEGRATION_KEYS.each do |key|
      if type_declaration[:defaults].key?(key)
        changed = true
        type_declaration[:defaults].delete(key)
      end
    end

    if type_declaration[:defaults].empty?
      type_declaration.delete(:defaults)
    end
  end

  if changed
    domain.foobara_type_from_declaration(type_declaration)
  else
    inputs_type
  end
end

#llm_instructions(user_association_depth, assistant_association_depth) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'src/llm_backed_execute_method.rb', line 266

def llm_instructions(user_association_depth, assistant_association_depth)
  key = [user_association_depth, assistant_association_depth]

  @llm_instructions_cache ||= {}

  if @llm_instructions_cache.key?(key)
    # simplecov:disable
    @llm_instructions_cache[key]
    # simplecov:enable
  else
    @llm_instructions_cache[key] = build_llm_instructions(user_association_depth, assistant_association_depth)
  end
end

#result_json_schema(association_depth) ⇒ Object



259
260
261
262
263
264
# File 'src/llm_backed_execute_method.rb', line 259

def result_json_schema(association_depth)
  JsonSchemaGenerator.to_json_schema(
    result_type,
    association_depth:
  )
end