Class OpenAiCompatibleClient

java.lang.Object
org.egothor.methodatlas.ai.OpenAiCompatibleClient
All Implemented Interfaces:
AiProviderClient

public final class OpenAiCompatibleClient extends Object implements AiProviderClient
AiProviderClient implementation for AI providers that expose an OpenAI-compatible chat completion API.

This client supports providers that implement the OpenAI-style /v1/chat/completions endpoint. The same implementation is used for:

The client constructs a chat-style prompt consisting of a system message defining the classification rules and a user message containing the test class source together with the taxonomy definition. The model response is expected to contain a JSON object describing the security classification.

Operational Responsibilities

  • constructing OpenAI-compatible chat completion requests
  • injecting the taxonomy-driven classification prompt
  • performing authenticated HTTP requests
  • extracting JSON content from the model response
  • normalizing the result into AiClassSuggestion

The implementation is provider-neutral for APIs that follow the OpenAI protocol, which allows reuse across multiple compatible services such as OpenRouter.

Instances are typically created through AiProviderFactory.create(AiOptions).

See Also:
  • Constructor Details

    • OpenAiCompatibleClient

      public OpenAiCompatibleClient(AiOptions options)
      Creates a new client for an OpenAI-compatible provider.

      The supplied configuration determines the provider endpoint, model name, authentication method, request timeout, and other runtime parameters.

      Parameters:
      options - AI runtime configuration
  • Method Details

    • isAvailable

      public boolean isAvailable()
      Determines whether the configured provider can be used in the current runtime environment.

      For OpenAI-compatible providers, availability is determined by the presence of a usable API key resolved through AiOptions.resolvedApiKey().

      Specified by:
      isAvailable in interface AiProviderClient
      Returns:
      true if a usable API key is available
    • suggestForClass

      public AiClassSuggestion suggestForClass(String fqcn, String classSource, String taxonomyText, List<PromptBuilder.TargetMethod> targetMethods) throws AiSuggestionException
      Submits a classification request to an OpenAI-compatible chat completion API.

      The request payload includes:

      • the configured model identifier
      • a system prompt defining classification rules
      • a user prompt containing the test class source and taxonomy
      • a deterministic temperature setting

      When the selected provider is AiProvider.OPENROUTER, additional HTTP headers are included to identify the calling application.

      The response is expected to contain a JSON object in the message content field. The JSON text is extracted and deserialized into an AiClassSuggestion.

      Specified by:
      suggestForClass in interface AiProviderClient
      Parameters:
      fqcn - fully qualified class name being analyzed
      classSource - complete source code of the class
      taxonomyText - taxonomy definition guiding classification
      targetMethods - deterministically extracted JUnit test methods that must be classified
      Returns:
      normalized classification result
      Throws:
      AiSuggestionException - if the provider request fails, the model response is invalid, or JSON deserialization fails
      See Also: