Skip to content

Commit

Permalink
Merge pull request #114 from Azure-Samples/gk/generative-usecases
Browse files Browse the repository at this point in the history
replace with chat completion in java and fix issue with qdrant in recommendation service
  • Loading branch information
thegovind committed Jul 7, 2023
2 parents cdb1a39 + 7b20eb8 commit 86c3283
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 14 deletions.
6 changes: 6 additions & 0 deletions dotnet/recommendation-service/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@
return kernel;
});

builder.Services.AddSingleton<QdrantMemoryStore>(provider =>
{
var memoryStore = new QdrantMemoryStore(Env.Var("QDRANT_ENDPOINT"), 1536, ConsoleLogger.Log);
return memoryStore;
});

builder.Services.AddSingleton<BingConnector>(provider =>
{
var bingConnector = new BingConnector(Env.Var("BING_API_KEY"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.microsoft.gbb.miyagi.userservice.service;

import com.azure.ai.openai.OpenAIClient;
import com.azure.ai.openai.models.Choice;
import com.azure.ai.openai.models.Completions;
import com.azure.ai.openai.models.CompletionsOptions;
import com.azure.ai.openai.models.*;
import com.microsoft.gbb.miyagi.userservice.config.OpenAIConfig;
import com.microsoft.gbb.miyagi.userservice.entity.Aspirations;
import com.microsoft.gbb.miyagi.userservice.entity.FinancialProfile;
Expand All @@ -22,6 +20,12 @@
@Service
@Qualifier("openaigenerator")
public class OpenAIGeneratorService implements ISyntheticGeneratorService {
private static final String META_PROMPT = """
You are a synthetic data generator that only returns a
single response that is either a string or a number.
The number should be parsable as an integer or a double.
DO NOT RETURN ANYTHING ELSE.
""";
private final OpenAIClient openAIClient;
private final OpenAIConfig openAIConfig;

Expand All @@ -37,7 +41,7 @@ public UserProfile generateFakeUserProfile() {
userProfile.setFirstName(getCompletion("Generate a first name"));
userProfile.setLastName(getCompletion("Generate a last name"));
userProfile.setCity(getCompletion("Generate a city name"));
userProfile.setAge(Integer.parseInt(getCompletion("Generate a number between 18 and 65")));
userProfile.setAge(Integer.parseInt(getCompletion("Generate a number between 18 and 65 that is parsable as integer")));

userProfile.setFinancialProfile(generateFakeFinancialProfile());
userProfile.setAspirations(generateFakeAspirations());
Expand Down Expand Up @@ -85,14 +89,34 @@ public List<String> generateRandomCountryList(int count) {
}

private String getCompletion(String prompt) {
List<String> promptList = new ArrayList<>();
/* List<String> promptList = new ArrayList<>();
promptList.add(META_PROMPT);
promptList.add(prompt);
Completions completions = openAIClient.getCompletions(openAIConfig.getOpenAIModelId(),
new CompletionsOptions(promptList));

Choice choice = completions.getChoices().get(0);
new CompletionsOptions(promptList));*/
List<ChatMessage> chatMessages = new ArrayList<>();
chatMessages.add(new ChatMessage(ChatRole.SYSTEM).setContent(META_PROMPT));
// one shot example
chatMessages.add(new ChatMessage(ChatRole.USER).setContent("Generate a name"));
chatMessages.add(new ChatMessage(ChatRole.ASSISTANT).setContent("John Doe"));
chatMessages.add(new ChatMessage(ChatRole.USER).setContent(prompt));

ChatCompletions chatCompletions = openAIClient.getChatCompletions(openAIConfig.getOpenAIModelId(),
new ChatCompletionsOptions(chatMessages));

System.out.printf("Model ID=%s is created at %d.%n", chatCompletions.getId(), chatCompletions.getCreated());
ChatMessage message = null;
for (ChatChoice choice : chatCompletions.getChoices()) {
message = choice.getMessage();
System.out.printf("Index: %d, Chat Role: %s.%n", choice.getIndex(), message.getRole());
System.out.println("Message:");
System.out.println(message.getContent());
}
/* Choice choice = completions.getChoices().get(0);
return choice.getText().trim();
return choice.getText().trim();*/
assert message != null;
return message.getContent();
}
}
10 changes: 5 additions & 5 deletions java/user-service/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
spring:
application:
name: user-service
openai:
key: ${AZURE_OPENAI_KEY}
endpoint: ${AZURE_OPENAI_ENDPOINT}
model:
id: ${AZURE_OPENAI_MODEL_ID}
kafka:
topic:
name: ${KAFKATOPICNAME}
Expand Down Expand Up @@ -43,6 +38,11 @@ openai:
baseline-on-migrate: 'true'
banner:
location: static/banner.txt
openai:
key: ${AZURE_OPENAI_KEY}
endpoint: ${AZURE_OPENAI_ENDPOINT}
model:
id: ${AZURE_OPENAI_MODEL_ID}
eureka:
client:
register-with-eureka: false
Expand Down

0 comments on commit 86c3283

Please sign in to comment.