Skip to content

Commit

Permalink
feat: support a array of books
Browse files Browse the repository at this point in the history
  • Loading branch information
Angular2Guy committed Dec 16, 2024
1 parent 2edec13 commit b0764ab
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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> jsonResult) { }
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<List<JsonResult>>() {});
return new FunctionResult(null, result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
</div>
} @else {
<p *ngIf="!!responseText" class="result-text">{{responseText}}</p>
<p *ngIf="!!responseJson?.value1" class="result-text">
<p *ngIf="!!responseJson?.at(0)?.value1" class="result-text">
<!-- {{responseJson | json}} -->
<mat-tree
[dataSource]="dataSource"
Expand All @@ -88,7 +88,7 @@
{{ treeControl.isExpanded(node) ? "expand_more" : "chevron_right" }}
</mat-icon>
</button>
<span i18n="@@fuctionSearchAuthor">Author</span>: {{ node.value1 }}
<span class="book-author" i18n="@@functionSearchAuthor">Author</span><span class="book-author">: {{ node.value1 }}</span>
</div>
<div
[class.example-tree-invisible]="!treeControl.isExpanded(node)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
display: block;
width: 90vw;
white-space: wrap;
font-size: 16px;
}

.book-author {
font-size: 16px;
}

.spinner-container {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -20,115 +20,115 @@ import { MatInputModule } from '@angular/material/input';
import { MatTooltipModule } from '@angular/material/tooltip';
import { MatFormFieldModule } from '@angular/material/form-field';
import {
FormControl,
FormsModule,
ReactiveFormsModule,
Validators,
FormControl,
FormsModule,
ReactiveFormsModule,
Validators,
} from '@angular/forms';
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
import { Book, FunctionResponse, FunctionSearch, JsonResult } from '../model/functions';
import { Book, FunctionResponse, FunctionSearch, JsonBook, JsonResult } from '../model/functions';
import { FunctionSearchService } from '../service/function-search.service';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { Subscription, interval, map, tap } from 'rxjs';
import { NestedTreeControl } from '@angular/cdk/tree';
import { MatIconModule } from '@angular/material/icon';
import {MatRadioModule} from '@angular/material/radio';
import { MatRadioModule } from '@angular/material/radio';

interface TreeNode {
value1: string;
value2: string;
children?: TreeNode[];
value1: string;
value2: string;
children?: TreeNode[];
}

@Component({
selector: 'app-function-search',
imports: [
CommonModule,
MatToolbarModule,
MatButtonModule,
MatInputModule,
MatTooltipModule,
MatTreeModule,
MatIconModule,
selector: 'app-function-search',
imports: [
CommonModule,
MatToolbarModule,
MatButtonModule,
MatInputModule,
MatTooltipModule,
MatTreeModule,
MatIconModule,
MatRadioModule,
MatFormFieldModule,
FormsModule,
ReactiveFormsModule,
MatProgressSpinnerModule,
],
templateUrl: './function-search.component.html',
styleUrl: './function-search.component.scss'
MatFormFieldModule,
FormsModule,
ReactiveFormsModule,
MatProgressSpinnerModule,
],
templateUrl: './function-search.component.html',
styleUrl: './function-search.component.scss'
})
export class FunctionSearchComponent {
private repeatSub: Subscription | null = null;
protected searchValueControl = new FormControl('', [
Validators.required,
Validators.minLength(3),
]);
protected searching = false;
protected msWorking = 0;
protected treeControl = new NestedTreeControl<TreeNode>(
(node) => node.children
);
protected dataSource = new MatTreeNestedDataSource<TreeNode>();
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<TreeNode>(
(node) => node.children
);
protected dataSource = new MatTreeNestedDataSource<TreeNode>();
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');
}
}
2 changes: 1 addition & 1 deletion frontend/src/angular/src/app/model/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export interface FunctionResponse {

export interface FunctionResult {
result?: string;
jsonResult?: JsonResult;
jsonResult?: JsonResult[];
}

export interface JsonResult {
Expand Down

0 comments on commit b0764ab

Please sign in to comment.