Skip to content

Commit

Permalink
Merge pull request #113 from Azure-Samples/gk/generative-usecases
Browse files Browse the repository at this point in the history
add synthentic data generation use case using azure openai java sdk
  • Loading branch information
thegovind authored Jul 5, 2023
2 parents 80165d2 + ed91c9a commit cdb1a39
Show file tree
Hide file tree
Showing 15 changed files with 249 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name: Memory Service Build and Push to GHCR
name: Expense Service Build and Push to GHCR

on:
push:
branches:
- main
paths:
- 'python/memory-service/**'
- 'python/expense-service/**'
workflow_dispatch:

env:
Expand Down Expand Up @@ -36,6 +36,6 @@ jobs:
- name: Build and push Docker image
uses: docker/build-push-action@v2
with:
context: python/memory-service
context: python/expense-service
push: true
tags: ${{ env.REGISTRY }}/${{ env.REPOSITORY }}/memory-service:${{ env.short_sha }}
tags: ${{ env.REGISTRY }}/${{ env.REPOSITORY }}/expense-service:${{ env.short_sha }}
33 changes: 33 additions & 0 deletions java/user-service/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
COMPOSEPROJECTNAME=miyagi-backing-services
KAFKABOOTSTRAPSERVERS=<replace_with_your_servicebus_id>.servicebus.windows.net:9093
KAFKASECURITYPROTOCOL=SASL_SSL
KAFKASASLMECHANISM=PLAIN
KAFKATOPICGROUP=order-service
KAFKATOPICNAME=miyagi
KAFKASASLJAASCONFIG=org.apache.kafka.common.security.plain.PlainLoginModule required username="$ConnectionString" password="<replace_with_your_kafka_password>";
MYSQLURL=jdbc:mysql://<replace>.mysql.database.azure.com/miyagi
MYSQLUSER=miyagi
MYSQLPASSWORD=<replace_with_your_mysql_password>
AZUREREDISHOST=<replace>.redis.cache.windows.net
AZUREREDISPORT=6380
AZUREREDISACCESSKEY=<replace_with_your_redis_access_key>
AZURECOSMOSDBURI=https://miyagi20988gok29744.documents.azure.com:443/
AZURECOSMOSDBKEY=<replace_with_your_cosmos_db_key>
AZURECOSMOSDBSECONDARYKEY=<replace_with_your_cosmos_db_secondary_key>
AZURECOSMOSDBDATABASENAME=miyagi
AZURESTORAGEACCOUNTNAME=miyagi20988gok29744
AZURESTORAGEACCOUNTKEY=<replace_with_your_storage_account_key>
AZURESTORAGEENDPOINT=https://miyagi20988gok29744.blob.core.windows.net
APPLICATIONINSIGHTSPREVIEWPROFILERENABLED=true
APPLICATIONINSIGHTS_CONNECTION_STRING=InstrumentationKey=<replace_with_your_instrumentation_key>;IngestionEndpoint=https://eastus-8.in.applicationinsights.azure.com/;LiveEndpoint=https://eastus.livediagnostics.monitor.azure.com/
AZUREKEYVAULTCLIENTID=<replace>
AZUREKEYVAULTCLIENTSECRET=<replace_with_your_key_vault_client_secret>
AZUREKEYVAULTENDPOINT=https://miyagispringkv.vault.azure.net/
AZUREKEYVAULTTENANTID=72f988bf-86f1-41af-91ab-2d7cd011db47
SERVICEBUSCONNECTIONSTRING=Endpoint=sb://sbusmiyagi20988gok29744.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=<replace_with_your_service_bus_shared_access_key>
POSTGRESQLURL=jdbc:postgresql://miyagi-pg.postgres.database.azure.com:5432/miyagi?sslmode=require
POSTGRESQLUSER=miyagi_user
POSTGRESQLPASSWORD=<replace_with_your_postgresql_password>
AZURE_OPENAI_KEY=<replace_with_your_openai_key>
AZURE_OPENAI_ENDPOINT=https://gk-oai.openai.azure.com/
AZURE_OPENAI_MODEL_ID=gk-gpt-35
6 changes: 6 additions & 0 deletions java/user-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
<spring-cloud-starter-version>4.0.3</spring-cloud-starter-version>
</properties>
<dependencies>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-ai-openai</artifactId>
<version>1.0.0-beta.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
Expand Down Expand Up @@ -161,6 +166,7 @@
<version>6.0.0-beta.4</version>
<type>pom</type>
</dependency>

</dependencies>
</dependencyManagement>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.microsoft.gbb.miyagi.userservice.config;

import com.azure.ai.openai.OpenAIClient;
import com.azure.ai.openai.OpenAIClientBuilder;
import com.azure.core.credential.AzureKeyCredential;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class OpenAIConfig {

@Value("${openai.key}")
private String openAIKey;

@Value("${openai.endpoint}")
private String openAIEndpoint;

@Value("${openai.model.id}")
private String openAIModelId;

@Bean
public OpenAIClient openAIClient() {
return new OpenAIClientBuilder()
.endpoint(openAIEndpoint)
.credential(new AzureKeyCredential(openAIKey))
.buildClient();
}

public String getOpenAIModelId() {
return openAIModelId;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.microsoft.gbb.miyagi.userservice.controller;

import com.microsoft.gbb.miyagi.userservice.entity.UserProfile;
import com.microsoft.gbb.miyagi.userservice.service.UserProfileService;
import com.microsoft.gbb.miyagi.userservice.service.IUserProfileService;
import jakarta.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -16,10 +16,10 @@
@RequestMapping("/api/v1/userprofile")
public class UserProfileController {

private final UserProfileService userProfileService;
private final IUserProfileService IUserProfileService;

public UserProfileController(UserProfileService userProfileService) {
this.userProfileService = userProfileService;
public UserProfileController(IUserProfileService IUserProfileService) {
this.IUserProfileService = IUserProfileService;
}

/**
Expand All @@ -34,7 +34,7 @@ public UserProfileController(UserProfileService userProfileService) {
@ResponseStatus(HttpStatus.CREATED)
@CrossOrigin(origins = "*")
public ResponseEntity<UserProfile> createNewCustomer(@RequestBody @Valid UserProfile user) {
return ResponseEntity.ok(userProfileService.createUserProfile(user));
return ResponseEntity.ok(IUserProfileService.createUserProfile(user));
}

/**
Expand All @@ -45,7 +45,7 @@ public ResponseEntity<UserProfile> createNewCustomer(@RequestBody @Valid UserPro
*/
@GetMapping("/{id}")
public UserProfile getUserProfileById(@PathVariable long id) {
return userProfileService.getUserProfileById(id);
return IUserProfileService.getUserProfileById(id);
}

/**
Expand All @@ -56,7 +56,7 @@ public UserProfile getUserProfileById(@PathVariable long id) {
*/
@PutMapping
public UserProfile updateUserProfile(@RequestBody UserProfile userProfile) {
return userProfileService.updateUserProfile(userProfile);
return IUserProfileService.updateUserProfile(userProfile);
}

/**
Expand All @@ -67,7 +67,7 @@ public UserProfile updateUserProfile(@RequestBody UserProfile userProfile) {
*/
@DeleteMapping("/{id}")
public boolean deleteUserProfile(@PathVariable long id) {
return userProfileService.deleteUserProfile(id);
return IUserProfileService.deleteUserProfile(id);
}

/**
Expand All @@ -77,6 +77,6 @@ public boolean deleteUserProfile(@PathVariable long id) {
*/
@GetMapping
public List<UserProfile> listUserProfiles() {
return userProfileService.listUserProfiles();
return IUserProfileService.listUserProfiles();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.microsoft.gbb.miyagi.userservice.controller;

import com.microsoft.gbb.miyagi.userservice.entity.UserProfile;
import com.microsoft.gbb.miyagi.userservice.service.UserProfileGeneratorService;
import com.microsoft.gbb.miyagi.userservice.service.FakerGeneratorService;
import com.microsoft.gbb.miyagi.userservice.service.OpenAIGeneratorService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -14,20 +16,35 @@
@RestController
@RequestMapping("/api/v1/userprofile")
public class UserProfileGeneratorController {
private final UserProfileGeneratorService userProfileGeneratorService;
private final OpenAIGeneratorService openAIGeneratorService;
private final FakerGeneratorService fakerGeneratorService;

public UserProfileGeneratorController(UserProfileGeneratorService userProfileGeneratorService) {
this.userProfileGeneratorService = userProfileGeneratorService;
public UserProfileGeneratorController(@Qualifier("openaigenerator") OpenAIGeneratorService openAIGeneratorService,
@Qualifier("fakergenerator") FakerGeneratorService fakerGeneratorService) {

this.openAIGeneratorService = openAIGeneratorService;
this.fakerGeneratorService = fakerGeneratorService;
}

/**
* Generates a new user profile.
* Generates a new user profile using OpenAI.
*
* @return The generated user profile.
*/
@GetMapping(value = "/generate",
produces = "application/json")
public ResponseEntity<UserProfile> generateUserProfile() {
return ResponseEntity.ok(userProfileGeneratorService.generateFakeUserProfile());
return ResponseEntity.ok(openAIGeneratorService.generateFakeUserProfile());
}

/**
* Generates a new user profile using faker.
*
* @return The generated user profile.
*/
@GetMapping(value = "/generate/faker",
produces = "application/json")
public ResponseEntity<UserProfile> generateUserProfileWithFaker() {
return ResponseEntity.ok(fakerGeneratorService.generateFakeUserProfile());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
@Service
@Transactional
@Qualifier("customeruserprofile")
public class CustomerUserProfileService implements UserProfileService {
public class CustomerIUserProfileService implements IUserProfileService {
private final TopicProducer topicProducer;
private final UserProfileRepository userProfileRepository;

public CustomerUserProfileService(TopicProducer topicProducer,
UserProfileRepository userProfileRepository) {
public CustomerIUserProfileService(TopicProducer topicProducer,
UserProfileRepository userProfileRepository) {
this.topicProducer = topicProducer;
this.userProfileRepository = userProfileRepository;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
*/
@Slf4j
@Service
@Qualifier("userprofilegenerator")
public class UserProfileGeneratorService {
@Qualifier("fakergenerator")
public class FakerGeneratorService implements ISyntheticGeneratorService {
private final Faker faker;

public UserProfileGeneratorService() {
public FakerGeneratorService() {
faker = new Faker();
}

Expand All @@ -45,7 +45,7 @@ public UserProfile generateFakeUserProfile() {
* Generates a fake financial profile
* @return a fake financial profile
*/
private FinancialProfile generateFakeFinancialProfile() {
public FinancialProfile generateFakeFinancialProfile() {
FinancialProfile financialProfile = new FinancialProfile();
financialProfile.setAnnualSalary(faker.number().randomDouble(2, 30_000, 200_000));
financialProfile.setCurrentAssets(faker.number().randomDouble(2, 10_000, 1_000_000));
Expand All @@ -59,7 +59,7 @@ private FinancialProfile generateFakeFinancialProfile() {
* Generates a fake aspirations profile
* @return a fake aspirations profile
*/
private Aspirations generateFakeAspirations() {
public Aspirations generateFakeAspirations() {
Aspirations aspirations = new Aspirations();
aspirations.setVacationBucketList(generateRandomCountryList(faker.number().numberBetween(1, 5)));
aspirations.setHobbies(generateRandomHobbyList(faker.number().numberBetween(1, 5)));
Expand All @@ -73,7 +73,7 @@ private Aspirations generateFakeAspirations() {
* @param count the number of strings to generate
* @return a list of random strings
*/
private List<String> generateRandomHobbyList(int count) {
public List<String> generateRandomHobbyList(int count) {
List<String> randomStrings = new ArrayList<>();
for (int i = 0; i < count; i++) {
randomStrings.add(faker.hobby().activity());
Expand All @@ -86,7 +86,7 @@ private List<String> generateRandomHobbyList(int count) {
* @param count the number of strings to generate
* @return a list of random strings
*/
private List<String> generateRandomCountryList(int count) {
public List<String> generateRandomCountryList(int count) {
List<String> randomStrings = new ArrayList<>();
for (int i = 0; i < count; i++) {
randomStrings.add(faker.address().country());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.microsoft.gbb.miyagi.userservice.service;

import com.microsoft.gbb.miyagi.userservice.entity.Aspirations;
import com.microsoft.gbb.miyagi.userservice.entity.FinancialProfile;
import com.microsoft.gbb.miyagi.userservice.entity.UserProfile;

import java.util.List;

public interface ISyntheticGeneratorService {
UserProfile generateFakeUserProfile();
FinancialProfile generateFakeFinancialProfile();
Aspirations generateFakeAspirations();
List<String> generateRandomHobbyList(int count);
List<String> generateRandomCountryList(int count);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/**
* Interface to define CRUD operations for managing user profiles.
*/
public interface UserProfileService {
public interface IUserProfileService {
/**
* Creates a new user profile.
*
Expand Down
Loading

0 comments on commit cdb1a39

Please sign in to comment.