Skip to content

Commit 6b93638

Browse files
committed
feat: next step
1 parent 8698c4b commit 6b93638

File tree

6 files changed

+54
-11
lines changed

6 files changed

+54
-11
lines changed

backend/src/main/java/ch/xxx/aidoclibchat/adapter/controller/FunctionController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public FunctionController(FunctionService functionService) {
3939

4040
@PostMapping(path="/books", produces = MediaType.APPLICATION_JSON_VALUE)
4141
public FunctionResult postQuestion(@RequestBody FunctionSearch functionSearch) {
42-
return new FunctionResult(this.functionService.functionCall(functionSearch.question()));
42+
return this.functionService.functionCall(functionSearch.question(), functionSearch.resultFormat());
4343
}
4444

4545
}

backend/src/main/java/ch/xxx/aidoclibchat/domain/model/dto/FunctionResult.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@
1212
*/
1313
package ch.xxx.aidoclibchat.domain.model.dto;
1414

15-
public record FunctionResult(String result) { }
15+
import ch.xxx.aidoclibchat.usecase.service.FunctionService.JsonResult;
16+
17+
public record FunctionResult(String result, JsonResult jsonResult) { }

backend/src/main/java/ch/xxx/aidoclibchat/usecase/service/FunctionService.java

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,37 @@
1212
*/
1313
package ch.xxx.aidoclibchat.usecase.service;
1414

15+
import java.util.List;
16+
1517
import org.slf4j.Logger;
1618
import org.slf4j.LoggerFactory;
1719
import org.springframework.ai.chat.client.ChatClient;
1820
import org.springframework.ai.chat.client.ChatClient.Builder;
1921
import org.springframework.beans.factory.annotation.Value;
2022
import org.springframework.stereotype.Service;
2123

24+
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
25+
26+
import ch.xxx.aidoclibchat.domain.model.dto.FunctionResult;
27+
import ch.xxx.aidoclibchat.domain.model.dto.FunctionSearch.ResultFormat;
28+
2229
@Service
2330
public class FunctionService {
2431
private static final Logger LOGGER = LoggerFactory.getLogger(FunctionService.class);
2532
private final ChatClient chatClient;
33+
34+
@JsonPropertyOrder({ "title", "summary" })
35+
public record JsonBook(String title, String summary) {
36+
}
37+
38+
@JsonPropertyOrder({ "author", "books" })
39+
public record JsonResult(String author, List<JsonBook> books) {
40+
}
41+
2642
private final String promptStr = """
27-
Make sure to have a parameter when calling a function.
43+
Make sure to have a parameter when calling a function.
2844
If no parameter is provided ask the user for the parameter.
29-
Create a summary for each book based on the function response subject.
45+
Create a summary for each book based on the function response subject.
3046
3147
User Query:
3248
%s
@@ -39,13 +55,27 @@ public FunctionService(Builder builder) {
3955
this.chatClient = builder.build();
4056
}
4157

42-
public String functionCall(String question) {
58+
public FunctionResult functionCall(String question, ResultFormat resultFormat) {
4359
if (!this.activeProfile.contains("ollama")) {
44-
return "";
60+
return new FunctionResult(" ", null);
4561
}
4662

47-
var result = this.chatClient.prompt().user(this.promptStr + question).functions("openLibraryClient").call().content();
63+
FunctionResult result = switch (resultFormat) {
64+
case ResultFormat.Text -> this.functionCallText(question);
65+
case ResultFormat.Json -> this.functionCallJson(question);
66+
};
4867
return result;
4968
}
5069

70+
private FunctionResult functionCallText(String question) {
71+
var result = this.chatClient.prompt().user(this.promptStr + question).functions("openLibraryClient").call()
72+
.content();
73+
return new FunctionResult(result, null);
74+
}
75+
76+
private FunctionResult functionCallJson(String question) {
77+
var result = this.chatClient.prompt().user(this.promptStr + question).functions("openLibraryClient").call()
78+
.entity(JsonResult.class);
79+
return new FunctionResult(null, result);
80+
}
5181
}

frontend/src/angular/src/app/function-search/function-search.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
</div>
6161
</div>
6262
} @else {
63-
<p class="result-text">{{response}}</p>
63+
<p class="result-text">{{responseText}}</p>
6464
<!--
6565
<mat-tree
6666
[dataSource]="dataSource"

frontend/src/angular/src/app/function-search/function-search.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export class FunctionSearchComponent {
7070
(node) => node.children
7171
);
7272
protected dataSource = new MatTreeNestedDataSource<TreeNode>();
73-
protected response = '';
73+
protected responseText = '';
7474
protected resultFormats = ['text','json'];
7575
protected resultFormatControl = new FormControl(this.resultFormats[0]);
7676

@@ -110,7 +110,7 @@ export class FunctionSearchComponent {
110110
takeUntilDestroyed(this.destroyRef),
111111
tap(() => (this.searching = false))
112112
)
113-
.subscribe(value => this.response = value.result
113+
.subscribe(value => this.responseText = value.result ?? ''
114114
);
115115
//.subscribe((value) => (this.dataSource.data = this.mapResult(value)));
116116
}

frontend/src/angular/src/app/model/functions.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,16 @@ export interface FunctionResponse {
3737
}
3838

3939
export interface FunctionResult {
40-
result: string;
40+
result?: string;
41+
jsonResult?: JsonResult;
42+
}
43+
44+
export interface JsonResult {
45+
author: string;
46+
books: JsonBook[];
47+
}
48+
49+
export interface JsonBook {
50+
title: string;
51+
summary: string;
4152
}

0 commit comments

Comments
 (0)