applied spring formatter, temp removed gradle to support deployment with buildpacks

This commit is contained in:
Oded Shopen 2024-09-10 21:34:58 +03:00
parent 2371f6af56
commit 6f93676ace
10 changed files with 97 additions and 35 deletions

View file

@ -0,0 +1,25 @@
apiVersion: gateway.networking.k8s.io/v1beta1
kind: HTTPRoute
metadata:
name: petclinic-route
annotations:
healthcheck.gslb.tanzu.vmware.com/service: spring-petclinic
healthcheck.gslb.tanzu.vmware.com/path: /
healthcheck.gslb.tanzu.vmware.com/port: "80"
spec:
parentRefs:
- group: gateway.networking.k8s.io
kind: Gateway
name: default-gateway
sectionName: http-petclinic
rules:
- backendRefs:
- group: ""
kind: Service
name: spring-petclinic
port: 8080
weight: 1
matches:
- path:
type: PathPrefix
value: /

View file

@ -0,0 +1,18 @@
apiVersion: apps.tanzu.vmware.com/v1
kind: ContainerApp
metadata:
creationTimestamp: null
name: spring-petclinic
spec:
nonSecretEnv:
- name: SPRING_PROFILES_ACTIVE
value: postgres-openai
build:
buildpacks: {}
nonSecretEnv:
- name: BP_JVM_VERSION
value: "17"
path: ../..
ports:
- name: main
port: 8080

View file

@ -12,24 +12,32 @@ import org.springframework.samples.petclinic.owner.Pet;
@Configuration @Configuration
class AIFunctionConfiguration { class AIFunctionConfiguration {
@Bean @Bean
@Description("List the owners that the pet clinic has") @Description("List the owners that the pet clinic has")
public Function<OwnerRequest, OwnersResponse> listOwners(PetclinicAiProvider petclinicAiProvider) { public Function<OwnerRequest, OwnersResponse> listOwners(PetclinicAiProvider petclinicAiProvider) {
return request -> { return request -> {
return petclinicAiProvider.getAllOwners(); return petclinicAiProvider.getAllOwners();
}; };
} }
@Bean
@Description("Add a pet to an owner identified by the ownerId")
public Function<AddPetRequest, AddedPetResponse> addPetToOwner(PetclinicAiProvider petclinicAiProvider) {
return request -> {
return petclinicAiProvider.addPetToOwner(request);
};
}
@Bean
@Description("Add a pet to an owner identified by the ownerId")
public Function<AddPetRequest, AddedPetResponse> addPetToOwner(PetclinicAiProvider petclinicAiProvider) {
return request -> {
return petclinicAiProvider.addPetToOwner(request);
};
}
} }
record AddPetRequest (Pet pet, Integer ownerId) {}; record AddPetRequest(Pet pet, Integer ownerId) {
record OwnerRequest (Owner owner) {}; };
record OwnersResponse(List<Owner> owners) {};
record AddedPetResponse(Owner owner) {}; record OwnerRequest(Owner owner) {
};
record OwnersResponse(List<Owner> owners) {
};
record AddedPetResponse(Owner owner) {
};

View file

@ -12,4 +12,5 @@ public class LoggingAdvisor implements RequestResponseAdvisor {
System.out.println("Request: " + request); System.out.println("Request: " + request);
return request; return request;
} }
} }

View file

@ -8,13 +8,15 @@ import org.springframework.web.client.RestClient;
@Configuration @Configuration
public class PetclinicAIConfiguration { public class PetclinicAIConfiguration {
@Bean @Bean
public RestClient restClient() { public RestClient restClient() {
return RestClient.create(); return RestClient.create();
} }
@Bean @Bean
public ChatMemory chatMemory() { public ChatMemory chatMemory() {
return new InMemoryChatMemory(); return new InMemoryChatMemory();
} }
} }

View file

@ -9,18 +9,19 @@ import org.springframework.stereotype.Service;
@Service @Service
public class PetclinicAiProvider { public class PetclinicAiProvider {
OwnerRepository ownerRepository; OwnerRepository ownerRepository;
public PetclinicAiProvider(OwnerRepository ownerRepository) { public PetclinicAiProvider(OwnerRepository ownerRepository) {
this.ownerRepository = ownerRepository; this.ownerRepository = ownerRepository;
} }
public OwnersResponse getAllOwners() { public OwnersResponse getAllOwners() {
Pageable pageable = PageRequest.of(0, Integer.MAX_VALUE); Pageable pageable = PageRequest.of(0, Integer.MAX_VALUE);
Page<Owner> ownerPage = ownerRepository.findAll(pageable); Page<Owner> ownerPage = ownerRepository.findAll(pageable);
return new OwnersResponse(ownerPage.getContent()); return new OwnersResponse(ownerPage.getContent());
} }
public AddedPetResponse addPetToOwner(AddPetRequest request) { public AddedPetResponse addPetToOwner(AddPetRequest request) {
Owner owner = ownerRepository.findById(request.ownerId()); Owner owner = ownerRepository.findById(request.ownerId());
owner.addPet(request.pet()); owner.addPet(request.pet());

View file

@ -9,24 +9,25 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@RestController @RestController
@RequestMapping("/") @RequestMapping("/")
public class PetclinicChatClient { public class PetclinicChatClient {
// ChatModel is the primary interfaces for interacting with an LLM // ChatModel is the primary interfaces for interacting with an LLM
// it is a request/response interface that implements the ModelModel // it is a request/response interface that implements the ModelModel
// interface. Make suer to visit the source code of the ChatModel and // interface. Make suer to visit the source code of the ChatModel and
// checkout the interfaces in the core spring ai package. // checkout the interfaces in the core spring ai package.
private final ChatClient chatClient; private final ChatClient chatClient;
public PetclinicChatClient(ChatClient.Builder builder, ChatMemory chatMemory) { public PetclinicChatClient(ChatClient.Builder builder, ChatMemory chatMemory) {
// @formatter:off // @formatter:off
this.chatClient = builder this.chatClient = builder
.defaultSystem(""" .defaultSystem("""
You are a friendly AI assistant designed to help with the management of a veterinarian pet clinic called Spring Petclinic. You are a friendly AI assistant designed to help with the management of a veterinarian pet clinic called Spring Petclinic.
Your job is to answer questions about the existing veterinarians and to perform actions on the customer's behalf, mainly around Your job is to answer questions about the existing veterinarians and to perform actions on the customer's behalf, mainly around
pet owners, their pets and their visits. pet owners, their pets and their visits.
You are required to answer an a professional manner. If you don't know the answer, politely tell the customer You are required to answer an a professional manner. If you don't know the answer, politely tell the customer
you don't know the answer, then ask the customer a followup qusetion to try and clarify the question they are asking. you don't know the answer, then ask the customer a followup qusetion to try and clarify the question they are asking.
If you do know the answer, provide the answer but do not provide any additional helpful followup questions. If you do know the answer, provide the answer but do not provide any additional helpful followup questions.
""") """)

6
tanzu.yml Normal file
View file

@ -0,0 +1,6 @@
apiVersion: config.tanzu.vmware.com/v1
configuration:
dev:
paths:
- .tanzu/config/
kind: TanzuConfig