Skip to content

Commit 6cd310c

Browse files
haoozhangHao Zhang
andauthored
chat agent use same online MySQL database as other components (#52)
## Purpose Make chat agent use the same online MySQL database as other components. Currently the local DB config still exists, because some UI is using it. Will update the UI later and then we can simplify. ## Does this introduce a breaking change? <!-- Mark one with an "x". --> ``` [ ] Yes [x] No ``` ## Pull Request Type What kind of change does this Pull Request introduce? <!-- Please check the one that applies to this PR using "x". --> ``` [ ] Bugfix [ ] Feature [ ] Code style update (formatting, local variables) [ ] Refactoring (no functional changes, no api changes) [ ] Documentation content changes [ ] Other... Please describe: ``` ## How to Test * Get the code ``` git clone [repo-address] cd [repo-name] git checkout [branch-name] npm install ``` * Test the code <!-- Add steps to run the tests suite and/or manually test --> ``` ``` ## What to Check Verify that the following are valid * ... ## Other Information <!-- Add any other helpful information that may be needed here. --> --------- Co-authored-by: Hao Zhang <[email protected]>
1 parent 19ccef6 commit 6cd310c

File tree

15 files changed

+203
-31
lines changed

15 files changed

+203
-31
lines changed

src/spring-petclinic-chat-agent/src/main/java/org/springframework/samples/petclinic/agent/ChatAgentApplication.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.context.annotation.Bean;
56
import org.springframework.context.annotation.ImportRuntimeHints;
7+
import org.springframework.web.client.RestTemplate;
68

79
@SpringBootApplication
810
@ImportRuntimeHints(PetClinicRuntimeHints.class)
@@ -12,4 +14,8 @@ public static void main(String[] args) {
1214
SpringApplication.run(ChatAgentApplication.class, args);
1315
}
1416

17+
@Bean
18+
public RestTemplate restTemplate() {
19+
return new RestTemplate();
20+
}
1521
}

src/spring-petclinic-chat-agent/src/main/java/org/springframework/samples/petclinic/agent/chat/AgentConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.azure.identity.DefaultAzureCredentialBuilder;
66
import org.springframework.ai.azure.openai.AzureOpenAiChatModel;
77
import org.springframework.ai.azure.openai.AzureOpenAiChatOptions;
8+
import org.springframework.ai.azure.openai.AzureOpenAiResponseFormat;
89
import org.springframework.ai.chat.client.ChatClient;
910
import org.springframework.ai.chat.client.ChatClientCustomizer;
1011
import org.springframework.ai.chat.client.advisor.PromptChatMemoryAdvisor;
@@ -49,6 +50,7 @@ public AzureOpenAiChatModel chatModel(OpenAIClient openAIClient, ChatOptionsProp
4950
var openAIChatOptions = AzureOpenAiChatOptions.builder()
5051
.withDeploymentName(properties.getDeploymentName())
5152
.withTemperature(properties.getTemperature())
53+
.withResponseFormat(AzureOpenAiResponseFormat.TEXT)
5254
.build();
5355

5456
// provide Context to load function callbacks

src/spring-petclinic-chat-agent/src/main/java/org/springframework/samples/petclinic/agent/chat/ChatOptionsProperties.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
@Getter
88
@Setter
99
@ConfigurationProperties(prefix = ChatOptionsProperties.PREFIX)
10-
class ChatOptionsProperties {
10+
public class ChatOptionsProperties {
1111

1212
static final String PREFIX = "spring.ai.azure.openai.chat.options";
1313

src/spring-petclinic-chat-agent/src/main/java/org/springframework/samples/petclinic/agent/chat/OwnerTools.java

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,54 +7,51 @@
77
import org.springframework.context.annotation.Bean;
88
import org.springframework.context.annotation.Configuration;
99
import org.springframework.context.annotation.Description;
10-
import org.springframework.data.domain.PageRequest;
11-
import org.springframework.data.domain.Pageable;
12-
import org.springframework.samples.petclinic.agent.owner.Owner;
13-
import org.springframework.samples.petclinic.agent.owner.OwnerRepository;
10+
import org.springframework.samples.petclinic.agent.dto.OwnerDto;
11+
import org.springframework.samples.petclinic.agent.service.OwnerService;
1412

1513
import java.util.List;
1614
import java.util.function.Function;
1715

1816
@Configuration
1917
public class OwnerTools {
2018

21-
private final OwnerRepository owners;
19+
private final OwnerService ownerService;
2220

23-
public OwnerTools(OwnerRepository ownerRepository) {
24-
this.owners = ownerRepository;
21+
public OwnerTools(OwnerService ownerService) {
22+
this.ownerService = ownerService;
2523
}
2624

2725
@Bean
28-
@Description("Query the owners by last name, the owner information include owner id, address, telephone, city, first name and last name"
26+
@Description("Query the owners by first name, the owner information include owner id, address, telephone, city, first name and last name"
2927
+ "\n The owner also include the pets information, include the pet name, pet type and birth"
30-
+ "\n The pet include serveral visit record, include the visit name and visit date")
31-
public Function<OwnerQueryRequest, List<Owner>> queryOwners() {
28+
+ "\n The pet include several visit records, include the visit name and visit date")
29+
public Function<OwnerQueryRequest, List<OwnerDto>> queryOwners() {
3230
return name -> {
33-
Pageable pageable = PageRequest.of(0, 10);
34-
return owners.findByLastName(name.lastName, pageable).toList();
31+
return ownerService.findByFirstName(name.firstName);
3532
};
3633
}
3734

3835
@Bean
3936
@Description("Create a new owner by providing the owner's firstName, lastName, address, telephone and city")
40-
public Function<OwnerCURequest, Owner> addOwner() {
37+
public Function<OwnerCURequest, OwnerDto> addOwner() {
4138
return request -> {
42-
Owner owner = new Owner();
39+
OwnerDto owner = new OwnerDto();
4340
owner.setAddress(request.address);
4441
owner.setTelephone(request.telephone);
4542
owner.setCity(request.city);
4643
owner.setLastName(request.lastName);
4744
owner.setFirstName(request.firstName);
48-
this.owners.save(owner);
45+
ownerService.save(owner);
4946
return owner;
5047
};
5148
}
5249

5350
@Bean
5451
@Description("update a owner's firstName, lastName, address, telephone and city by providing the owner id\"")
55-
public Function<OwnerCURequest, Owner> updateOwner() {
52+
public Function<OwnerCURequest, OwnerDto> updateOwner() {
5653
return request -> {
57-
Owner owner = owners.findById(Integer.parseInt(request.ownerId));
54+
OwnerDto owner = ownerService.findById(Integer.parseInt(request.ownerId));
5855
if (request.address != null) {
5956
owner.setAddress(request.address);
6057
}
@@ -70,15 +67,15 @@ public Function<OwnerCURequest, Owner> updateOwner() {
7067
if (request.firstName != null) {
7168
owner.setFirstName(request.firstName);
7269
}
73-
this.owners.save(owner);
70+
ownerService.save(owner);
7471
return owner;
7572
};
7673
}
7774

7875
@JsonInclude(JsonInclude.Include.NON_NULL)
7976
@JsonClassDescription("Owner Query Request")
8077
public record OwnerQueryRequest(@JsonProperty(required = false,
81-
value = "lastName") @JsonPropertyDescription("The Owner last name") String lastName) {
78+
value = "firstName") @JsonPropertyDescription("The Owner first name") String firstName) {
8279
}
8380

8481
@JsonInclude(JsonInclude.Include.NON_NULL)

src/spring-petclinic-chat-agent/src/main/java/org/springframework/samples/petclinic/agent/chat/VetTools.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,26 @@
77
import org.springframework.context.annotation.Bean;
88
import org.springframework.context.annotation.Configuration;
99
import org.springframework.context.annotation.Description;
10-
import org.springframework.samples.petclinic.agent.vet.Vet;
11-
import org.springframework.samples.petclinic.agent.vet.VetRepository;
10+
import org.springframework.samples.petclinic.agent.dto.VetDto;
11+
import org.springframework.samples.petclinic.agent.service.VetService;
1212

1313
import java.util.Collection;
1414
import java.util.function.Function;
1515

1616
@Configuration
1717
public class VetTools {
1818

19-
private final VetRepository vetRepository;
19+
private final VetService vetService;
2020

21-
public VetTools(VetRepository vetRepository) {
22-
this.vetRepository = vetRepository;
21+
public VetTools(VetService vetService) {
22+
this.vetService = vetService;
2323
}
2424

2525
@Bean
2626
@Description("return list of Vets, include their specialties")
27-
public Function<Request, Collection<Vet>> queryVets() {
27+
public Function<Request, Collection<VetDto>> queryVets() {
2828
return request -> {
29-
return vetRepository.findAll();
29+
return vetService.findAll();
3030
};
3131
}
3232

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.springframework.samples.petclinic.agent.dto;
2+
3+
import lombok.Data;
4+
5+
import java.util.Set;
6+
7+
@Data
8+
public class OwnerDto {
9+
10+
private Integer id;
11+
12+
private String firstName;
13+
14+
private String lastName;
15+
16+
private String address;
17+
18+
private String city;
19+
20+
private String telephone;
21+
22+
private Set<PetDto> pets;
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.springframework.samples.petclinic.agent.dto;
2+
3+
import lombok.Data;
4+
5+
import java.util.Date;
6+
7+
@Data
8+
public class PetDto {
9+
10+
private Integer id;
11+
12+
private String name;
13+
14+
private Date birthDate;
15+
16+
private PetTypeDto type;
17+
18+
private OwnerDto owner;
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.springframework.samples.petclinic.agent.dto;
2+
3+
import lombok.Data;
4+
5+
@Data
6+
public class PetTypeDto {
7+
8+
private Integer id;
9+
10+
private String name;
11+
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.springframework.samples.petclinic.agent.dto;
2+
3+
import lombok.Data;
4+
5+
@Data
6+
public class SpecialtyDto {
7+
8+
private Integer id;
9+
10+
private String name;
11+
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.springframework.samples.petclinic.agent.dto;
2+
3+
import lombok.Data;
4+
5+
import java.util.Set;
6+
7+
@Data
8+
public class VetDto {
9+
10+
private Integer id;
11+
12+
private String firstName;
13+
14+
private String lastName;
15+
16+
private Set<SpecialtyDto> specialties;
17+
18+
}

0 commit comments

Comments
 (0)