Class OpenAIModel

java.lang.Object
me.neurodock.backend.open.ai.OpenAIModel
All Implemented Interfaces:
Model, StreamingModel

public class OpenAIModel extends Object implements StreamingModel
  • Constructor Details

    • OpenAIModel

      public OpenAIModel(@NotNull @NotNull String backendURI, @NotNull @NotNull String modelName) throws ModelNotFoundException
      Constructs an me.neurodock.backend.open.ai.OpenAIModel bound to a specific backend and model.

      This performs a blocking request against the backend to fetch the list of available models, then fuzzy-matches modelName against the returned model IDs, selecting the closest match above a minimum confidence threshold.

      Parameters:
      backendURI - the base URL of the OpenAI-compatible backend (e.g. http://localhost:11434)
      modelName - the model name, or partial name, to fuzzy-match against the backend's available models
      Throws:
      ModelNotFoundException - if no models could be retrieved from the backend (e.g. the server is unreachable, misconfigured, or — see @apiNote — rejects the request due to a missing API key), or if none of the returned models sufficiently match modelName
      RuntimeException - if backendURI is malformed
  • Method Details

    • unload

      public void unload()
      Unloads the current model.
      And ignoring any errors
    • qurryModel

      public CompletableFuture<Message> qurryModel(ChatObject obj, List<Tool> tools)
      Sends the conversation history in obj to the backend and requests the next assistant response.

      This performs the request asynchronously; the returned future completes once the backend has responded (or completes exceptionally if the request fails at the transport level — see queryJSON(JSONObject, String, String)).

      Specified by:
      qurryModel in interface Model
      Parameters:
      obj - the chat context, containing the full conversation history to send to the model
      tools - the tools available to the model for this request (currently unused — tool support is not yet implemented)
      Returns:
      a future resolving to the assistant's reply as a Message. If the backend returns no choices, or the response is otherwise malformed/missing, resolves to a Message with empty content rather than throwing.
    • singleFire

      public CompletableFuture<Message> singleFire(Message msg)
      Description copied from interface: Model
      Sends a single, standalone message to the model, without any prior conversation context.
      Specified by:
      singleFire in interface Model
      Parameters:
      msg - the message to send
      Returns:
      a future resolving to the model's reply as a Message
    • singleFire

      public CompletableFuture<Message> singleFire(Message msg, List<Tool> tools)
      Description copied from interface: Model
      Sends a single, standalone message to the model with tool support, without any prior conversation context.
      Specified by:
      singleFire in interface Model
      Parameters:
      msg - the message to send
      tools - the tools available to the model for this request
      Returns:
      a future resolving to the model's reply as a Message
    • getToolSerializer

      public ToolSerializer getToolSerializer()
      Specified by:
      getToolSerializer in interface Model
      Returns:
      this backend's ToolSerializer, used to convert Tools into the JSON shape this backend expects. Each backend instance owns its own — this is not shared across the JVM.
    • getDefaultToolSerializer

      public static ToolSerializer getDefaultToolSerializer()
    • cancel

      public void cancel()
    • queryJSON

      @Nullable public @Nullable CompletableFuture<org.json.JSONObject> queryJSON(org.json.JSONObject payload, String endpoint, String method, Consumer<org.json.JSONObject> onChunk)
      Sends a blocking HTTP request to the given endpoint on the configured backend and parses the response body as JSON.
      Parameters:
      payload - the JSON request body to send, or null to send no body
      endpoint - the endpoint to request, resolved against backendURL (e.g. "v1/models")
      method - the HTTP method to use (e.g. "GET", "POST")
      Returns:
      the parsed JSONObject response body if the request succeeds with a 200 status, or null if a non-200 status is received
      Throws:
      RuntimeException - if an I/O error occurs while communicating with the backend, or if endpoint cannot be resolved into a valid URL
    • qurryModel

      public CompletableFuture<Message> qurryModel(ChatObject obj, List<Tool> tools, Consumer<org.json.JSONObject> chunkConsumer)
      Specified by:
      qurryModel in interface StreamingModel
    • singleFire

      public CompletableFuture<Message> singleFire(Message msg, Consumer<org.json.JSONObject> chunkConsumer)
      Specified by:
      singleFire in interface StreamingModel
    • singleFire

      public CompletableFuture<Message> singleFire(Message msg, List<Tool> tools, Consumer<org.json.JSONObject> chunkConsumer)
      Specified by:
      singleFire in interface StreamingModel