Skip to content

Commit 1e4835c

Browse files
committed
Code cleanup
1 parent 1b5e37f commit 1e4835c

40 files changed

+722
-881
lines changed
Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,18 @@
11
package com.microsoft.openai.samples.rag;
22

3-
import com.nimbusds.oauth2.sdk.util.StringUtils;
43
import org.slf4j.Logger;
54
import org.slf4j.LoggerFactory;
6-
import org.slf4j.MarkerFactory;
7-
import org.springframework.beans.factory.annotation.Value;
85
import org.springframework.boot.SpringApplication;
96
import org.springframework.boot.autoconfigure.SpringBootApplication;
10-
import org.springframework.core.env.Environment;
11-
12-
import java.net.InetAddress;
13-
import java.net.UnknownHostException;
14-
import java.util.Optional;
157

168
@SpringBootApplication
179
public class Application {
1810

19-
private static final Logger log = LoggerFactory.getLogger(Application.class);
20-
21-
public static void main(String[] args) {
22-
23-
log.info ("Application profile from system property is [{}]", System.getProperty("spring.profiles.active"));
24-
new SpringApplication(Application.class).run(args);
25-
26-
}
27-
11+
private static final Logger LOG = LoggerFactory.getLogger(Application.class);
2812

13+
public static void main(String[] args) {
14+
LOG.info("Application profile from system property is [{}]", System.getProperty("spring.profiles.active"));
15+
new SpringApplication(Application.class).run(args);
16+
}
2917

3018
}

app/backend/src/main/java/com/microsoft/openai/samples/rag/approaches/ContentSource.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
package com.microsoft.openai.samples.rag.approaches;
22

33
public class ContentSource {
4+
45
private String sourceName;
56
private String sourceContent;
6-
private boolean noNewLine = true;
7+
private final boolean noNewLine;
78

8-
public ContentSource(String sourceName, String sourceContent, Boolean noNewLine){
9+
public ContentSource(String sourceName, String sourceContent, Boolean noNewLine) {
910
this.noNewLine = noNewLine;
1011
this.sourceName = sourceName;
1112
buildContent(sourceContent);
1213
}
14+
1315
public ContentSource(String sourceName, String sourceContent) {
1416
this(sourceName, sourceContent, true);
15-
1617
}
1718

1819
public String getSourceName() {
@@ -35,9 +36,10 @@ public boolean isNoNewLine() {
3536
return noNewLine;
3637
}
3738

38-
private void buildContent(String sourceContent){
39-
if(this.noNewLine){
40-
this.sourceContent = sourceContent.replaceAll("\n", "");
39+
private void buildContent(String sourceContent) {
40+
if (this.noNewLine) {
41+
this.sourceContent = sourceContent.replace("\n", "");
4142
}
4243
}
44+
4345
}

app/backend/src/main/java/com/microsoft/openai/samples/rag/approaches/PromptTemplate.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@
33
public interface PromptTemplate {
44

55
String getPrompt();
6+
67
void setVariables();
8+
79
}
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package com.microsoft.openai.samples.rag.approaches;
22

3-
import java.util.Optional;
3+
public interface RAGApproach<I, O> {
44

5-
public interface RAGApproach<INPUT,OUTPUT> {
6-
7-
public OUTPUT run(INPUT questionOrCoversation, RAGOptions options);
5+
O run(I questionOrConversation, RAGOptions options);
86

97
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.microsoft.openai.samples.rag.approaches;
22

3-
public interface RAGApproachFactory<I,O> {
3+
public interface RAGApproachFactory<I, O> {
4+
5+
RAGApproach<I, O> createApproach(String approachName, RAGType ragType);
46

5-
RAGApproach<I,O> createApproach(String approachName,RAGType ragType);
67
}

app/backend/src/main/java/com/microsoft/openai/samples/rag/approaches/RAGApproachFactorySpringBootImpl.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,14 @@
99

1010
@Component
1111
public class RAGApproachFactorySpringBootImpl implements RAGApproachFactory, ApplicationContextAware {
12-
private ApplicationContext applicationContext;
1312

14-
private static String READ_RETRIEVE_READ = "rrr";
15-
private static String RETRIEVE_THEN_READ = "rtr";
13+
private static final String READ_RETRIEVE_READ = "rrr";
14+
private static final String RETRIEVE_THEN_READ = "rtr";
15+
private ApplicationContext applicationContext;
1616

17-
public void setApplicationContext(ApplicationContext applicationContext) {
18-
this.applicationContext = applicationContext;
19-
}
2017
/**
2118
* Retrieve a specific approach bean definition based on approachName
19+
*
2220
* @param approachName
2321
* @return
2422
*/
@@ -37,4 +35,9 @@ else if (READ_RETRIEVE_READ.equals(approachName))
3735
//if this point is reached then the combination of approach and rag type is not supported
3836
throw new IllegalArgumentException("Invalid combination for approach[%s] and rag type[%s]: ".formatted(approachName, ragType));
3937
}
38+
39+
public void setApplicationContext(ApplicationContext applicationContext) {
40+
this.applicationContext = applicationContext;
41+
}
42+
4043
}

app/backend/src/main/java/com/microsoft/openai/samples/rag/approaches/RAGOptions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,5 @@ public RAGOptions build() {
8787
return ragOptions;
8888
}
8989
}
90+
9091
}

app/backend/src/main/java/com/microsoft/openai/samples/rag/approaches/RAGResponse.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,20 @@
44

55
public class RAGResponse {
66

7-
private String question;
8-
private List<ContentSource> sources;
9-
private String sourcesAsText;
10-
private String answer;
11-
private String prompt;
7+
private final String question;
8+
private final List<ContentSource> sources;
9+
private final String sourcesAsText;
10+
private final String answer;
11+
private final String prompt;
1212

1313
private RAGResponse(Builder builder) {
1414
this.question = builder.question;
1515
this.sources = builder.sources;
1616
this.answer = builder.answer;
1717
this.prompt = builder.prompt;
18-
this.sourcesAsText =builder.sourcesAsText;
18+
this.sourcesAsText = builder.sourcesAsText;
1919
}
2020

21-
2221
public String getQuestion() {
2322
return question;
2423
}
@@ -27,13 +26,14 @@ public List<ContentSource> getSources() {
2726
return sources;
2827
}
2928

30-
public String getSourcesAsText() { return sourcesAsText;}
29+
public String getSourcesAsText() {
30+
return sourcesAsText;
31+
}
3132

3233
public String getAnswer() {
3334
return answer;
3435
}
3536

36-
3737
public String getPrompt() {
3838
return prompt;
3939
}
@@ -76,7 +76,4 @@ public RAGResponse build() {
7676
}
7777
}
7878

79-
80-
81-
8279
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.microsoft.openai.samples.rag.approaches;
22

33
public enum RAGType {
4-
CHAT, ASK;
5-
4+
CHAT, ASK
65

76
}

app/backend/src/main/java/com/microsoft/openai/samples/rag/ask/approaches/RetrieveThenReadApproach.java

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,57 +25,56 @@
2525
*/
2626
@Component
2727
public class RetrieveThenReadApproach implements RAGApproach<String, RAGResponse> {
28-
private static final Logger logger = LoggerFactory.getLogger(RetrieveThenReadApproach.class);
29-
private CognitiveSearchProxy cognitiveSearchProxy;
30-
private OpenAIProxy openAIProxy;
28+
29+
private static final Logger LOGGER = LoggerFactory.getLogger(RetrieveThenReadApproach.class);
30+
private final CognitiveSearchProxy cognitiveSearchProxy;
31+
private final OpenAIProxy openAIProxy;
32+
3133
public RetrieveThenReadApproach(CognitiveSearchProxy cognitiveSearchProxy, OpenAIProxy openAIProxy) {
3234
this.cognitiveSearchProxy = cognitiveSearchProxy;
3335
this.openAIProxy = openAIProxy;
3436
}
3537

3638
/**
37-
* @param question
39+
* @param questionOrConversation
3840
* @param options
3941
* @return
4042
*/
4143
@Override
42-
public RAGResponse run(String question, RAGOptions options) {
43-
//TODO exception handling
44-
SearchPagedIterable searchResults = getCognitiveSearchResults(question, options);
44+
public RAGResponse run(String questionOrConversation, RAGOptions options) {
45+
//TODO exception handling
46+
SearchPagedIterable searchResults = getCognitiveSearchResults(questionOrConversation, options);
4547

4648
List<ContentSource> sources = buildSourcesFromSearchResults(options, searchResults);
47-
logger.info("Total %s sources found in cognitive search for keyword search query[%s]".formatted(sources.size(),question));
48-
49-
var retrieveThenReadPrompt = new SemanticSearchAskPrompt(sources,question);
49+
LOGGER.info("Total {} sources found in cognitive search for keyword search query[{}]", sources.size(),
50+
questionOrConversation);
5051

52+
var retrieveThenReadPrompt = new SemanticSearchAskPrompt(sources, questionOrConversation);
5153

5254
var completionsOptions = buildCompletionsOptions(retrieveThenReadPrompt);
5355

54-
5556
Completions completionsResults = openAIProxy.getCompletions(completionsOptions);
5657

57-
logger.info("Completion generated with Prompt Tokens[{}], Completions Tokens[{}], Total Tokens[{}]",
58+
LOGGER.info("Completion generated with Prompt Tokens[{}], Completions Tokens[{}], Total Tokens[{}]",
5859
completionsResults.getUsage().getPromptTokens(),
5960
completionsResults.getUsage().getCompletionTokens(),
6061
completionsResults.getUsage().getTotalTokens());
6162

62-
6363
return new RAGResponse.Builder()
6464
.prompt(retrieveThenReadPrompt.getFormattedPrompt())
6565
.answer(completionsResults.getChoices().get(0).getText())
6666
.sources(sources)
67-
.question(question)
67+
.question(questionOrConversation)
6868
.build();
69-
7069
}
7170

7271
private CompletionsOptions buildCompletionsOptions(SemanticSearchAskPrompt retrieveThenReadPrompt) {
73-
CompletionsOptions completionsOptions = new CompletionsOptions(new ArrayList<>( Arrays.asList(retrieveThenReadPrompt.getFormattedPrompt())));
72+
CompletionsOptions completionsOptions = new CompletionsOptions(new ArrayList<>(Collections.singletonList(retrieveThenReadPrompt.getFormattedPrompt())));
7473

7574
// Due to a potential bug when using JVM 17 and java openai SDK 1.0.0-beta.2, we need to provide default for all properties to avoid 404 bad Request on the server
7675
completionsOptions.setMaxTokens(1024);
7776
completionsOptions.setTemperature(0.3);
78-
completionsOptions.setStop(Arrays.asList("\n"));
77+
completionsOptions.setStop(List.of("\n"));
7978
completionsOptions.setLogitBias(new HashMap<>());
8079
completionsOptions.setEcho(false);
8180
completionsOptions.setN(1);
@@ -93,7 +92,7 @@ private SearchPagedIterable getCognitiveSearchResults(String question, RAGOption
9392
var searchOptions = new SearchOptions();
9493

9594
Optional.ofNullable(options.getTop()).ifPresentOrElse(
96-
value -> searchOptions.setTop(value),
95+
searchOptions::setTop,
9796
() -> searchOptions.setTop(3));
9897
Optional.ofNullable(options.getExcludeCategory())
9998
.ifPresentOrElse(
@@ -111,27 +110,24 @@ private SearchPagedIterable getCognitiveSearchResults(String question, RAGOption
111110
}
112111
});
113112

114-
SearchPagedIterable searchResults = this.cognitiveSearchProxy.search(question, searchOptions, Context.NONE);
115-
return searchResults;
113+
return this.cognitiveSearchProxy.search(question, searchOptions, Context.NONE);
116114
}
117115

118116
private List<ContentSource> buildSourcesFromSearchResults(RAGOptions options, SearchPagedIterable searchResults) {
119-
List<ContentSource> sources = new ArrayList<ContentSource>();
117+
List<ContentSource> sources = new ArrayList<>();
120118

121119
searchResults.iterator().forEachRemaining(result ->
122120
{
123121
var searchDocument = result.getDocument(SearchDocument.class);
124122

125-
/**
123+
/*
126124
If captions is enabled the content source is taken from the captions generated by the semantic ranker.
127125
Captions are appended sequentially and separated by a dot.
128126
*/
129127
if(options.isSemanticCaptions()) {
130-
StringBuffer sourcesContentBuffer = new StringBuffer();
128+
StringBuilder sourcesContentBuffer = new StringBuilder();
131129

132-
result.getCaptions().forEach(caption -> {
133-
sourcesContentBuffer.append(caption.getText()).append(".");
134-
});
130+
result.getCaptions().forEach(caption -> sourcesContentBuffer.append(caption.getText()).append("."));
135131

136132
sources.add(new ContentSource((String)searchDocument.get("sourcepage"), sourcesContentBuffer.toString()));
137133
} else {

0 commit comments

Comments
 (0)