Skip to content
Merged
4 changes: 2 additions & 2 deletions src/main/java/nl/dannyj/mistral/MistralClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public void createChatCompletionStream(@NonNull ChatCompletionRequest request, @
* @return A new instance of MistralService
*/
private MistralService buildMistralService() {
return new MistralService(this, new HttpService(this));
return new MistralService(new HttpService(this.httpClient), this.objectMapper);
}

/**
Expand All @@ -260,7 +260,7 @@ private MistralService buildMistralService() {
* @return A new instance of OkHttpClient
*/
private OkHttpClient buildHttpClient(int readTimeoutSeconds, int connectTimeoutSeconds, int writeTimeoutSeconds) {
MistralHeaderInterceptor mistralInterceptor = new MistralHeaderInterceptor(this);
MistralHeaderInterceptor mistralInterceptor = new MistralHeaderInterceptor(this.getApiKey());

return new OkHttpClient.Builder()
.readTimeout(readTimeoutSeconds, TimeUnit.SECONDS)
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/nl/dannyj/mistral/builders/MessageListBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import jakarta.annotation.Nullable;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.NonNull;
import nl.dannyj.mistral.models.completion.message.AssistantMessage;
import nl.dannyj.mistral.models.completion.message.ChatMessage;
import nl.dannyj.mistral.models.completion.message.SystemMessage;
Expand Down Expand Up @@ -59,7 +59,7 @@ public MessageListBuilder(List<ChatMessage> messages) {
* @param content The text content of the system message. Cannot be null.
* @return This builder instance.
*/
public MessageListBuilder system(@NotNull String content) {
public MessageListBuilder system(@NonNull String content) {
this.messages.add(new SystemMessage(content));
return this;
}
Expand All @@ -70,7 +70,7 @@ public MessageListBuilder system(@NotNull String content) {
* @param content The text content of the assistant message. Cannot be null.
* @return This builder instance.
*/
public MessageListBuilder assistant(@NotNull String content) {
public MessageListBuilder assistant(@NonNull String content) {
this.messages.add(new AssistantMessage(content));
return this;
}
Expand All @@ -81,7 +81,7 @@ public MessageListBuilder assistant(@NotNull String content) {
* @param toolCalls The list of tool calls. Cannot be null or empty.
* @return This builder instance.
*/
public MessageListBuilder assistant(@NotNull @NotEmpty List<ToolCall> toolCalls) {
public MessageListBuilder assistant(@NonNull @NotEmpty List<ToolCall> toolCalls) {
this.messages.add(new AssistantMessage(toolCalls));
return this;
}
Expand All @@ -92,7 +92,7 @@ public MessageListBuilder assistant(@NotNull @NotEmpty List<ToolCall> toolCalls)
* @param content The text content of the user message. Cannot be null.
* @return This builder instance.
*/
public MessageListBuilder user(@NotNull String content) {
public MessageListBuilder user(@NonNull String content) {
this.messages.add(new UserMessage(content));
return this;
}
Expand All @@ -104,7 +104,7 @@ public MessageListBuilder user(@NotNull String content) {
* @param toolCallId The ID of the tool call this message responds to. Can be null.
* @return This builder instance.
*/
public MessageListBuilder tool(@NotNull String content, @Nullable String toolCallId) {
public MessageListBuilder tool(@NonNull String content, @Nullable String toolCallId) {
this.messages.add(new ToolMessage(content, toolCallId));
return this;
}
Expand All @@ -117,7 +117,7 @@ public MessageListBuilder tool(@NotNull String content, @Nullable String toolCal
* @param message The ChatMessage object to be added. Cannot be null.
* @return This builder instance.
*/
public MessageListBuilder message(@NotNull ChatMessage message) {
public MessageListBuilder message(@NonNull ChatMessage message) {
this.messages.add(message);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package nl.dannyj.mistral.interceptors;

import lombok.NonNull;
import nl.dannyj.mistral.MistralClient;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
Expand All @@ -27,22 +26,22 @@

public class MistralHeaderInterceptor implements Interceptor {

private final MistralClient client;
private final String apiKey;

public MistralHeaderInterceptor(@NonNull MistralClient client) {
this.client = client;
public MistralHeaderInterceptor(@NonNull String apiKey) {
if (apiKey.isBlank()) {
throw new IllegalArgumentException("No API key provided");
}

this.apiKey = apiKey;
}

@NotNull
@Override
public Response intercept(@NotNull Chain chain) throws IOException {
public Response intercept(@NonNull Chain chain) throws IOException {
Request request = chain.request();
Request.Builder newRequestBuilder = request.newBuilder();

if (client.getApiKey() == null || client.getApiKey().isBlank()) {
throw new IllegalArgumentException("No API key provided in MistralClient");
}

if (request.header("Content-Type") == null) {
newRequestBuilder.addHeader("Content-Type", "application/json");
}
Expand All @@ -52,7 +51,7 @@ public Response intercept(@NotNull Chain chain) throws IOException {
}

if (request.header("Authorization") == null) {
newRequestBuilder.addHeader("Authorization", "Bearer " + client.getApiKey());
newRequestBuilder.addHeader("Authorization", "Bearer " + this.apiKey);
}

Request newRequest = newRequestBuilder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.annotation.Nullable;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.Setter;
import nl.dannyj.mistral.models.completion.content.ContentChunk;
import nl.dannyj.mistral.models.completion.content.TextChunk;
Expand Down Expand Up @@ -69,7 +69,7 @@ public class AssistantMessage extends ChatMessage {
*
* @param textContent The text content.
*/
public AssistantMessage(@NotNull String textContent) {
public AssistantMessage(@NonNull String textContent) {
this.content = Collections.singletonList(new TextChunk(textContent));
this.toolCalls = null;
}
Expand All @@ -79,7 +79,7 @@ public AssistantMessage(@NotNull String textContent) {
*
* @param toolCalls The list of tool calls. Cannot be null or empty.
*/
public AssistantMessage(@NotNull @NotEmpty List<ToolCall> toolCalls) {
public AssistantMessage(@NonNull @NotEmpty List<ToolCall> toolCalls) {
this.content = null;
this.toolCalls = toolCalls;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public abstract class ChatMessage {
/**
* The content of the message. Can be null or a list of content chunks.
*
* @param content The list of content chunks, or null.
* @return The list of content chunks, or null.
*/
@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.annotation.Nullable;
import jakarta.validation.constraints.NotNull;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.Setter;
import nl.dannyj.mistral.models.completion.content.ContentChunk;
import nl.dannyj.mistral.models.completion.content.TextChunk;
Expand Down Expand Up @@ -68,7 +68,7 @@ public class ToolMessage extends ChatMessage {
* @param textContent The text content (result) of the tool call. Cannot be null.
* @param toolCallId The ID of the tool call this message responds to. Can be null.
*/
public ToolMessage(@NotNull String textContent, @Nullable String toolCallId) {
public ToolMessage(@NonNull String textContent, @Nullable String toolCallId) {
this.content = Collections.singletonList(new TextChunk(textContent));
this.toolCallId = toolCallId;
}
Expand All @@ -79,7 +79,7 @@ public ToolMessage(@NotNull String textContent, @Nullable String toolCallId) {
* @param contentChunks The list of content chunks representing the tool result. Cannot be null or empty.
* @param toolCallId The ID of the tool call this message responds to. Can be null.
*/
public ToolMessage(@NotNull @jakarta.validation.constraints.NotEmpty List<ContentChunk> contentChunks, @Nullable String toolCallId) {
public ToolMessage(@NonNull @jakarta.validation.constraints.NotEmpty List<ContentChunk> contentChunks, @Nullable String toolCallId) {
this.content = contentChunks;
this.toolCallId = toolCallId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
package nl.dannyj.mistral.models.completion.message;

import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import nl.dannyj.mistral.models.completion.content.ContentChunk;
import nl.dannyj.mistral.models.completion.content.TextChunk;

Expand All @@ -39,7 +39,7 @@ public class UserMessage extends ChatMessage {
*
* @param textContent The text content for the user message. Cannot be null or empty.
*/
public UserMessage(@NotNull String textContent) {
public UserMessage(@NonNull String textContent) {
if (textContent.isEmpty()) {
throw new IllegalArgumentException("User message text content cannot be empty.");
}
Expand All @@ -51,7 +51,7 @@ public UserMessage(@NotNull String textContent) {
*
* @param contentChunks The list of content chunks. Cannot be null or empty.
*/
public UserMessage(@NotNull @NotEmpty List<ContentChunk> contentChunks) {
public UserMessage(@NonNull @NotEmpty List<ContentChunk> contentChunks) {
this.content = contentChunks;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

public class ContentChunkListDeserializer extends StdDeserializer<List<ContentChunk>> implements ContextualDeserializer {

private JsonDeserializer<?> defaultDeserializer;
private transient JsonDeserializer<?> defaultDeserializer;

public ContentChunkListDeserializer() {
this(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,8 @@ public ToolChoiceOption deserialize(JsonParser jp, DeserializationContext ctxt)

if (token == JsonToken.VALUE_STRING) {
String enumValue = jp.getText().toUpperCase();
try {
return ToolChoiceEnum.valueOf(enumValue);
} catch (IllegalArgumentException e) {
if ("ANY".equalsIgnoreCase(enumValue)) return ToolChoiceEnum.ANY;
if ("AUTO".equalsIgnoreCase(enumValue)) return ToolChoiceEnum.AUTO;
if ("NONE".equalsIgnoreCase(enumValue)) return ToolChoiceEnum.NONE;
if ("REQUIRED".equalsIgnoreCase(enumValue)) return ToolChoiceEnum.REQUIRED;

throw ctxt.weirdStringException(enumValue, ToolChoiceEnum.class, "Not a valid ToolChoiceEnum value");
}

return ToolChoiceEnum.valueOf(enumValue);
} else if (token == JsonToken.START_OBJECT) {
return mapper.readValue(jp, SpecificToolChoice.class);
}
Expand Down
14 changes: 5 additions & 9 deletions src/main/java/nl/dannyj/mistral/services/HttpService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package nl.dannyj.mistral.services;

import lombok.NonNull;
import nl.dannyj.mistral.MistralClient;
import nl.dannyj.mistral.exceptions.MistralAPIException;
import okhttp3.Callback;
import okhttp3.MediaType;
Expand All @@ -37,15 +36,15 @@ public class HttpService {

private static final String API_URL = "https://api.mistral.ai/v1";

private final MistralClient client;
private final OkHttpClient httpClient;

/**
* Constructor that initializes the HttpService with a provided MistralClient.
* Constructor that initializes the HttpService with a provided OkHttpClient.
*
* @param client The MistralClient to be used for making requests to the Mistral AI API
* @param httpClient The OkHttpClient to be used for making requests to the Mistral AI API
*/
public HttpService(@NonNull MistralClient client) {
this.client = client;
public HttpService(@NonNull OkHttpClient httpClient) {
this.httpClient = httpClient;
}

/**
Expand Down Expand Up @@ -91,7 +90,6 @@ public void streamPost(@NonNull String urlPath, @NonNull String body, Callback c
.url(API_URL + urlPath)
.post(RequestBody.create(body, MediaType.parse("application/json")))
.build();
OkHttpClient httpClient = client.getHttpClient();

httpClient.newCall(request).enqueue(callBack);
}
Expand All @@ -104,8 +102,6 @@ public void streamPost(@NonNull String urlPath, @NonNull String body, Callback c
* @throws MistralAPIException If the response is not successful, the response body is null or an IOException occurs in the objectmapper
*/
private String executeRequest(Request request) {
OkHttpClient httpClient = client.getHttpClient();

try (Response response = httpClient.newCall(request).execute()) {
if (!response.isSuccessful()) {
throw new MistralAPIException("Received unexpected response code " + response.code() + ": " + (response.body() != null ? response.body().string() : response));
Expand Down
Loading