From b0764abed7b090c68e453f77ca7794c18a5d5747 Mon Sep 17 00:00:00 2001 From: Angular2guy Date: Mon, 16 Dec 2024 17:42:20 +0100 Subject: [PATCH] feat: support a array of books --- .../domain/model/dto/FunctionResult.java | 4 +- .../usecase/service/FunctionService.java | 3 +- .../function-search.component.html | 4 +- .../function-search.component.scss | 5 + .../function-search.component.ts | 182 +++++++++--------- .../src/angular/src/app/model/functions.ts | 2 +- 6 files changed, 104 insertions(+), 96 deletions(-) diff --git a/backend/src/main/java/ch/xxx/aidoclibchat/domain/model/dto/FunctionResult.java b/backend/src/main/java/ch/xxx/aidoclibchat/domain/model/dto/FunctionResult.java index 1cbc11a..2d9dd42 100644 --- a/backend/src/main/java/ch/xxx/aidoclibchat/domain/model/dto/FunctionResult.java +++ b/backend/src/main/java/ch/xxx/aidoclibchat/domain/model/dto/FunctionResult.java @@ -12,6 +12,8 @@ */ package ch.xxx.aidoclibchat.domain.model.dto; +import java.util.List; + import ch.xxx.aidoclibchat.usecase.service.FunctionService.JsonResult; -public record FunctionResult(String result, JsonResult jsonResult) { } +public record FunctionResult(String result, List jsonResult) { } diff --git a/backend/src/main/java/ch/xxx/aidoclibchat/usecase/service/FunctionService.java b/backend/src/main/java/ch/xxx/aidoclibchat/usecase/service/FunctionService.java index 7487969..4832a46 100644 --- a/backend/src/main/java/ch/xxx/aidoclibchat/usecase/service/FunctionService.java +++ b/backend/src/main/java/ch/xxx/aidoclibchat/usecase/service/FunctionService.java @@ -19,6 +19,7 @@ import org.springframework.ai.chat.client.ChatClient; import org.springframework.ai.chat.client.ChatClient.Builder; import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.ParameterizedTypeReference; import org.springframework.stereotype.Service; import com.fasterxml.jackson.annotation.JsonPropertyOrder; @@ -75,7 +76,7 @@ private FunctionResult functionCallText(String question) { private FunctionResult functionCallJson(String question) { var result = this.chatClient.prompt().user(this.promptStr + question).functions("openLibraryClient").call() - .entity(JsonResult.class); + .entity(new ParameterizedTypeReference>() {}); return new FunctionResult(null, result); } } diff --git a/frontend/src/angular/src/app/function-search/function-search.component.html b/frontend/src/angular/src/app/function-search/function-search.component.html index b1d1510..9e2cf09 100644 --- a/frontend/src/angular/src/app/function-search/function-search.component.html +++ b/frontend/src/angular/src/app/function-search/function-search.component.html @@ -61,7 +61,7 @@ } @else {

{{responseText}}

-

+

- Author: {{ node.value1 }} + Author: {{ node.value1 }}

( - (node) => node.children - ); - protected dataSource = new MatTreeNestedDataSource(); - protected responseText = ''; - protected responseJson = {value1: "", value2: ""} as TreeNode; - protected resultFormats = ['text','json']; - protected resultFormatControl = new FormControl(this.resultFormats[0]); + private repeatSub: Subscription | null = null; + protected searchValueControl = new FormControl('', [ + Validators.required, + Validators.minLength(3), + ]); + protected searching = false; + protected msWorking = 0; + protected treeControl = new NestedTreeControl( + (node) => node.children + ); + protected dataSource = new MatTreeNestedDataSource(); + protected responseText = ''; + protected responseJson = [{ value1: "", value2: "" } as TreeNode]; + protected resultFormats = ['text', 'json']; + protected resultFormatControl = new FormControl(this.resultFormats[0]); - constructor( - private router: Router, - private destroyRef: DestroyRef, - private functionSearchService: FunctionSearchService - ) {} + constructor( + private router: Router, + private destroyRef: DestroyRef, + private functionSearchService: FunctionSearchService + ) { } - protected hasChild = (_: number, node: TreeNode) => - !!node.children && node.children.length > 0; + protected hasChild = (_: number, node: TreeNode) => + !!node.children && node.children.length > 0; - protected showList(): void { - this.router.navigate(['/doclist']); - } + protected showList(): void { + this.router.navigate(['/doclist']); + } - protected search(): void { - this.searching = true; - this.dataSource.data = []; - const startDate = new Date(); - this.repeatSub?.unsubscribe(); - this.repeatSub = interval(100) - .pipe( - map(() => new Date()), - takeUntilDestroyed(this.destroyRef) - ) - .subscribe( - (newDate) => (this.msWorking = newDate.getTime() - startDate.getTime()) - ); - this.functionSearchService - .postLibraryFunction({ - question: this.searchValueControl.value, - resultFormat: this.resultFormatControl.value - } as FunctionSearch) - .pipe( - tap(() => this.repeatSub?.unsubscribe()), - takeUntilDestroyed(this.destroyRef), - tap(() => (this.searching = false)) - ) - .subscribe(value => this.resultFormatControl.value === this.resultFormats[0] ? - this.responseText = value.result || '' : this.responseJson = this.addToDataSource(this.mapResult(value.jsonResult || {author: "", books: []} as JsonResult)) - ); - } + protected search(): void { + this.searching = true; + this.dataSource.data = []; + const startDate = new Date(); + this.repeatSub?.unsubscribe(); + this.repeatSub = interval(100) + .pipe( + map(() => new Date()), + takeUntilDestroyed(this.destroyRef) + ) + .subscribe( + (newDate) => (this.msWorking = newDate.getTime() - startDate.getTime()) + ); + this.functionSearchService + .postLibraryFunction({ + question: this.searchValueControl.value, + resultFormat: this.resultFormatControl.value + } as FunctionSearch) + .pipe( + tap(() => this.repeatSub?.unsubscribe()), + takeUntilDestroyed(this.destroyRef), + tap(() => (this.searching = false)) + ) + .subscribe(value => this.resultFormatControl.value === this.resultFormats[0] ? + this.responseText = value.result || '' : this.responseJson = this.addToDataSource(this.mapResult(value.jsonResult || [{ author: "", books: [] }] as JsonResult[])) + ); + } - private addToDataSource(treeNode: TreeNode): TreeNode { - this.dataSource.data = [treeNode]; - return treeNode; - } - - private mapResult(jsonResult: JsonResult): TreeNode { - const children = jsonResult?.books.map(value => ({value1: value.title, value2: value.summary} as TreeNode)); - const rootNode = {value1: jsonResult.author, value2: "", children: children} as TreeNode; - return rootNode; - } + private addToDataSource(treeNodes: TreeNode[]): TreeNode[] { + this.dataSource.data = treeNodes; + return treeNodes; + } - protected logout(): void { - console.log('logout'); - } + private mapResult(jsonResults: JsonResult[]): TreeNode[] { + const createChildren = (books: JsonBook[]) => books.map(value => ({ value1: value.title, value2: value.summary } as TreeNode)); + const rootNode = jsonResults.map(myValue => ({ value1: myValue.author, value2: "", children: createChildren(myValue.books) } as TreeNode)); + return rootNode; + } + + protected logout(): void { + console.log('logout'); + } } diff --git a/frontend/src/angular/src/app/model/functions.ts b/frontend/src/angular/src/app/model/functions.ts index be34d9c..cda0381 100644 --- a/frontend/src/angular/src/app/model/functions.ts +++ b/frontend/src/angular/src/app/model/functions.ts @@ -38,7 +38,7 @@ export interface FunctionResponse { export interface FunctionResult { result?: string; - jsonResult?: JsonResult; + jsonResult?: JsonResult[]; } export interface JsonResult {