Skip to content

Adds support for retrieving the model name directly from DefaultChatClient #3889

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;

import io.micrometer.observation.ObservationRegistry;
Expand Down Expand Up @@ -89,6 +90,14 @@ static Builder builder(ChatModel chatModel, ObservationRegistry observationRegis

ChatClientRequestSpec prompt(Prompt prompt);

/**
* Returns the underlying {@link ChatModel} used by this client. This allows access to
* model metadata such as the model name, temperature, etc.
* @return the ChatModel
* @since 1.0.1
*/
ChatModel getChatModel();

/**
* Return a {@link ChatClient.Builder} to create a new {@link ChatClient} whose
* settings are replicated from the default {@link ChatClientRequestSpec} of this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,19 @@
*/
public class DefaultChatClient implements ChatClient {

private final ChatModel chatModel;

private static final ChatClientObservationConvention DEFAULT_CHAT_CLIENT_OBSERVATION_CONVENTION = new DefaultChatClientObservationConvention();

private static final TemplateRenderer DEFAULT_TEMPLATE_RENDERER = StTemplateRenderer.builder().build();

private final DefaultChatClientRequestSpec defaultChatClientRequest;

public DefaultChatClient(DefaultChatClientRequestSpec defaultChatClientRequest) {
public DefaultChatClient(DefaultChatClientRequestSpec defaultChatClientRequest, @Nullable ChatModel chatModel) {
Assert.notNull(defaultChatClientRequest, "defaultChatClientRequest cannot be null");
Assert.notNull(chatModel, "chatModel cannot be null");
this.defaultChatClientRequest = defaultChatClientRequest;
this.chatModel = chatModel;
}

@Override
Expand Down Expand Up @@ -121,6 +125,16 @@ public ChatClientRequestSpec prompt(Prompt prompt) {
return spec;
}

/**
* Returns the underlying {@link ChatModel} used by this client. This allows access to
* model metadata such as the model name, temperature, etc.
* @return the ChatModel
*/
@Override
public ChatModel getChatModel() {
return this.chatModel;
}

/**
* Return a {@link ChatClient.Builder} to create a new {@link ChatClient} whose
* settings are replicated from this {@link ChatClientRequest}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
*/
public class DefaultChatClientBuilder implements Builder {

private final ChatModel chatModel;

protected final DefaultChatClientRequestSpec defaultRequest;

DefaultChatClientBuilder(ChatModel chatModel) {
Expand All @@ -67,10 +69,11 @@ public DefaultChatClientBuilder(ChatModel chatModel, ObservationRegistry observa
this.defaultRequest = new DefaultChatClientRequestSpec(chatModel, null, Map.of(), null, Map.of(), List.of(),
List.of(), List.of(), List.of(), null, List.of(), Map.of(), observationRegistry,
customObservationConvention, Map.of(), null);
this.chatModel = chatModel;
}

public ChatClient build() {
return new DefaultChatClient(this.defaultRequest);
return new DefaultChatClient(this.defaultRequest, chatModel);
}

public Builder clone() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class DefaultChatClientTests {

@Test
void whenChatClientRequestIsNullThenThrow() {
assertThatThrownBy(() -> new DefaultChatClient(null)).isInstanceOf(IllegalArgumentException.class)
assertThatThrownBy(() -> new DefaultChatClient(null, null)).isInstanceOf(IllegalArgumentException.class)
.hasMessage("defaultChatClientRequest cannot be null");
}

Expand Down Expand Up @@ -1958,6 +1958,23 @@ void whenUserConsumerWithoutUserTextThenReturn() {
assertThat(defaultSpec.getMedia()).hasSize(1);
}

@Test
void whenGetChatModelThenReturnModelName() {
// Arrange
ChatModel mockChatModel = mock(ChatModel.class);
ChatOptions mockOptions = mock(ChatOptions.class);

when(mockChatModel.getDefaultOptions()).thenReturn(mockOptions);
when(mockOptions.getModel()).thenReturn("gpt-4o-mini");

// Act
ChatClient chatClient = new DefaultChatClientBuilder(mockChatModel).build();

// Assert
assertThat(chatClient.getChatModel()).isNotNull();
assertThat(chatClient.getChatModel().getDefaultOptions().getModel()).isEqualTo("gpt-4o-mini");
}

record Person(String name) {
}

Expand Down