Fix README new ChatClient migration snippet

This commit is contained in:
Christian Tzolov
2024-05-24 05:51:33 +02:00
parent 4a2ad60059
commit b425b0460c

View File

@@ -14,7 +14,7 @@ On our march to release 1.0.0 M1 we have made several breaking changes. Apologi
**(22.05.2024)**
A major change was made that took the 'old' `ChatClient` and moved the functionality into `ChatModel`. The 'new' `ChatClient` now takes an instance of `ChatModel`. This was done do support a fluent API for creating and executing prompts in a style similar to other client classes in the Spring ecosystem, such as `RestClient`, `WebClient`, and `JdbcClient`. Refer to the [JavaDoc](https://docs.spring.io/spring-ai/docs/1.0.0-SNAPSHOT/api/) for more information on the Fluent API, proper reference documentation is coming shortly.
A major change was made that took the 'old' `ChatClient` and moved the functionality into `ChatModel`. The 'new' `ChatClient` now takes an instance of `ChatModel`. This was done do support a fluent API for creating and executing prompts in a style similar to other client classes in the Spring ecosystem, such as `RestClient`, `WebClient`, and `JdbcClient`. Refer to the [JavaDoc](https://docs.spring.io/spring-ai/docs/1.0.0-SNAPSHOT/api/) for more information on the Fluent API, proper reference documentation is coming shortly.
We renamed the 'old' `ModelClient` to `Model` and renamed implementing classes, for example `ImageClient` was renamed to `ImageModel`. The `Model` implementation represent the portability layer that converts between the Spring AI API and the underlying AI Model API.
@@ -25,39 +25,39 @@ NOTE: The `ChatClient` class is now in the package `org.springframework.ai.chat.
#### Approach 1
Now, instead of getting an Autoconfigured `ChatClient` instance, you will get a `ChatModel` instance. The `call` method signatures after renaming remain the same.
To adapt your code should refactor you code to change use of the type `ChatClient` to `ChatModel`
To adapt your code should refactor you code to change use of the type `ChatClient` to `ChatModel`
Here is an example of existing code before the change
```java
@RestController
public class OldSimpleAiController {
public class OldSimpleAiController {
private final ChatClient chatClient;
public OldSimpleAiController(ChatClient chatClient) {
this.chatClient = chatClient;
}
@GetMapping("/ai/simple")
@GetMapping("/ai/simple")
Map<String, String> completion(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
return Map.of("generation", chatClient.call(message));
}
}
```
Now after the changes this will be
Now after the changes this will be
```java
@RestController
public class SimpleAiController {
private final ChatModel chatModel;
public SimpleAiController(ChatModel chatModel) {
this.chatModel = chatModel;
}
@GetMapping("/ai/simple")
@GetMapping("/ai/simple")
Map<String, String> completion(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
return Map.of("generation", chatModel.call(message));
}
@@ -80,9 +80,9 @@ Here is an example of existing code before the change
```java
@RestController
class OldSimpleAiController {
ChatClient chatClient;
OldSimpleAiController(ChatClient chatClient) {
this.chatClient = chatClient;
}
@@ -97,23 +97,22 @@ class OldSimpleAiController {
}
```
Now after the changes this will be
```java
@RestController
class SimpleAiController {
private final ChatClient.Builder builder;
private final ChatClient chatClient;
SimpleAiController(ChatClient.Builder builder) {
this.builder = builder;
this.builder = builder.build();
}
@GetMapping("/ai/simple")
@GetMapping("/ai/simple")
Map<String, String> completion(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
return Map.of(
"generation",
"generation",
chatClient.prompt().user(message).call().content()
);
}
@@ -130,7 +129,7 @@ There is a tag in the GitHub repository called [v1.0.0-SNAPSHOT-before-chatclien
```bash
git checkout tags/v1.0.0-SNAPSHOT-before-chatclient-changes
./mvnw clean install -DskipTests
./mvnw clean install -DskipTests
```