Skip to content

Commit acb31e7

Browse files
ThomasVitalemarkpollack
authored andcommitted
Add prompt(String) method to ChatClient
This commit introduces a new overloaded method prompt(String content) to the ChatClient API. It provides a convenient way to create prompts using only a string of user content, simplifying the process for basic chat interactions. Fixes gh-1286 Signed-off-by: Thomas Vitale <ThomasVitale@users.noreply.github.com>
1 parent c67442d commit acb31e7

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

spring-ai-core/src/main/java/org/springframework/ai/chat/client/ChatClient.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ static Builder builder(ChatModel chatModel, ObservationRegistry observationRegis
7474

7575
ChatClientRequestSpec prompt();
7676

77+
ChatClientPromptRequestSpec prompt(String content);
78+
7779
ChatClientPromptRequestSpec prompt(Prompt prompt);
7880

7981
/**

spring-ai-core/src/main/java/org/springframework/ai/chat/client/DefaultChatClient.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ public ChatClientRequestSpec prompt() {
9999
return new DefaultChatClientRequestSpec(this.defaultChatClientRequest);
100100
}
101101

102+
@Override
103+
public ChatClientPromptRequestSpec prompt(String content) {
104+
return new DefaultChatClientPromptRequestSpec(this.chatModel, new Prompt(content));
105+
}
106+
102107
@Override
103108
public ChatClientPromptRequestSpec prompt(Prompt prompt) {
104109
return new DefaultChatClientPromptRequestSpec(this.chatModel, prompt);

spring-ai-core/src/test/java/org/springframework/ai/chat/client/ChatClientTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,19 @@ public void defaultUserText() {
435435
assertThat(userMessage.getMessageType()).isEqualTo(MessageType.USER);
436436
}
437437

438+
@Test
439+
public void simpleUserPromptAsString() {
440+
when(chatModel.call(promptCaptor.capture()))
441+
.thenReturn(new ChatResponse(List.of(new Generation(new AssistantMessage("response")))));
442+
443+
assertThat(ChatClient.builder(chatModel).build().prompt("User prompt").call().content())
444+
.isEqualTo("response");
445+
446+
Message userMessage = promptCaptor.getValue().getInstructions().get(0);
447+
assertThat(userMessage.getContent()).isEqualTo("User prompt");
448+
assertThat(userMessage.getMessageType()).isEqualTo(MessageType.USER);
449+
}
450+
438451
@Test
439452
public void simpleUserPrompt() {
440453
when(chatModel.call(promptCaptor.capture()))

spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chatclient.adoc

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,20 @@ ChatClient.Builder builder = ChatClient.builder(myChatModel);
6464
ChatClient chatClient = ChatClient.create(myChatModel);
6565
----
6666

67+
== ChatClient Fluent API
68+
69+
The `ChatClient` fluent API allows you to create a prompt in three distinct ways using an overloaded `prompt` method to initiate the fluent API.
70+
71+
* `prompt()`: This method with no arguments is the most flexible, allowing you to build up user, system, and other parts of the prompt using the fluent API.
72+
73+
* `prompt(Prompt prompt)`: This method accepts a `Prompt` argument, letting you pass in a `Prompt` instance that you have created using the Prompt's non-fluent APIs. The prompt passed to the model is 'sealed', meaning you can't modify it using fluent API methods.
74+
75+
* `prompt(String content)`: This is a convenience method similar to the previous overload. It takes the user's text content and creates a `Prompt` instance internally via `new Prompt(content)`.
76+
77+
6778
== ChatClient Responses
6879

69-
The `ChatClient` API offers several ways to format the response from the AI Model.
80+
The `ChatClient` API offers several ways to format the response from the AI Model using the fluent API.
7081

7182
=== Returning a ChatResponse
7283

@@ -85,6 +96,7 @@ ChatResponse chatResponse = chatClient.prompt()
8596
.chatResponse();
8697
----
8798

99+
88100
=== Returning an Entity
89101

90102
You often want to return an entity class that is mapped from the returned `String`.
@@ -318,6 +330,39 @@ This contextual data can be of different types. Common types include:
318330

319331
* **Conversational history**: The chat model's API is stateless. If you tell the AI model your name, it won't remember it in subsequent interactions. Conversational history must be sent with each request to ensure previous interactions are considered when generating a response.
320332

333+
334+
=== Advisor Configuration in ChatClient
335+
336+
The ChatClient fluent API provides an `AdvisorSpec` interface for configuring advisors. This interface offers methods to add parameters, set multiple parameters at once, and add one or more advisors to the chain.
337+
338+
[source,java]
339+
----
340+
interface AdvisorSpec {
341+
AdvisorSpec param(String k, Object v);
342+
AdvisorSpec params(Map<String, Object> p);
343+
AdvisorSpec advisors(Advisor... advisors);
344+
AdvisorSpec advisors(List<Advisor> advisors);
345+
}
346+
----
347+
348+
IMPORTANT: The order in which advisors are added to the chain is crucial, as it determines the sequence of their execution. Each advisor modifies the prompt or the context in some way, and the changes made by one advisor are passed on to the next in the chain.
349+
350+
[source,java]
351+
----
352+
ChatClient.builder(chatModel)
353+
.build()
354+
.prompt()
355+
.advisors(
356+
new ChatMemoryAdvisor(chatMemory),
357+
new QuestionAnswerAdvisor(vectorStore, SearchRequest.defaults())
358+
)
359+
.user(userText)
360+
.call();
361+
----
362+
363+
In this configuration, the `ChatMemoryAdvisor` will be executed first, adding the conversation history to the prompt. Then, the `QuestionAnswerAdvisor` will perform its search based on the user's question and the added conversation history, potentially providing more relevant results.
364+
365+
321366
=== Retrieval Augmented Generation
322367

323368
A vector database stores data that the AI model is unaware of.
@@ -427,6 +472,8 @@ public class CustomerSupportAssistant {
427472
}
428473
----
429474

475+
476+
430477
=== Logging
431478

432479
The `SimpleLoggerAdvisor` is an advisor that logs the `request` and `response` data of the `ChatClient`.

0 commit comments

Comments
 (0)