Skip to content

Commit

Permalink
feat: next step
Browse files Browse the repository at this point in the history
  • Loading branch information
Angular2Guy committed Dec 15, 2024
1 parent 8698c4b commit 6b93638
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public FunctionController(FunctionService functionService) {

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@
*/
package ch.xxx.aidoclibchat.domain.model.dto;

public record FunctionResult(String result) { }
import ch.xxx.aidoclibchat.usecase.service.FunctionService.JsonResult;

public record FunctionResult(String result, JsonResult jsonResult) { }
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,37 @@
*/
package ch.xxx.aidoclibchat.usecase.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.stereotype.Service;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;

import ch.xxx.aidoclibchat.domain.model.dto.FunctionResult;
import ch.xxx.aidoclibchat.domain.model.dto.FunctionSearch.ResultFormat;

@Service
public class FunctionService {
private static final Logger LOGGER = LoggerFactory.getLogger(FunctionService.class);
private final ChatClient chatClient;

@JsonPropertyOrder({ "title", "summary" })
public record JsonBook(String title, String summary) {
}

@JsonPropertyOrder({ "author", "books" })
public record JsonResult(String author, List<JsonBook> books) {
}

private final String promptStr = """
Make sure to have a parameter when calling a function.
Make sure to have a parameter when calling a function.
If no parameter is provided ask the user for the parameter.
Create a summary for each book based on the function response subject.
Create a summary for each book based on the function response subject.
User Query:
%s
Expand All @@ -39,13 +55,27 @@ public FunctionService(Builder builder) {
this.chatClient = builder.build();
}

public String functionCall(String question) {
public FunctionResult functionCall(String question, ResultFormat resultFormat) {
if (!this.activeProfile.contains("ollama")) {
return "";
return new FunctionResult(" ", null);
}

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

private FunctionResult functionCallText(String question) {
var result = this.chatClient.prompt().user(this.promptStr + question).functions("openLibraryClient").call()
.content();
return new FunctionResult(result, null);
}

private FunctionResult functionCallJson(String question) {
var result = this.chatClient.prompt().user(this.promptStr + question).functions("openLibraryClient").call()
.entity(JsonResult.class);
return new FunctionResult(null, result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
</div>
</div>
} @else {
<p class="result-text">{{response}}</p>
<p class="result-text">{{responseText}}</p>
<!--
<mat-tree
[dataSource]="dataSource"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class FunctionSearchComponent {
(node) => node.children
);
protected dataSource = new MatTreeNestedDataSource<TreeNode>();
protected response = '';
protected responseText = '';
protected resultFormats = ['text','json'];
protected resultFormatControl = new FormControl(this.resultFormats[0]);

Expand Down Expand Up @@ -110,7 +110,7 @@ export class FunctionSearchComponent {
takeUntilDestroyed(this.destroyRef),
tap(() => (this.searching = false))
)
.subscribe(value => this.response = value.result
.subscribe(value => this.responseText = value.result ?? ''
);
//.subscribe((value) => (this.dataSource.data = this.mapResult(value)));
}
Expand Down
13 changes: 12 additions & 1 deletion frontend/src/angular/src/app/model/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,16 @@ export interface FunctionResponse {
}

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

export interface JsonResult {
author: string;
books: JsonBook[];
}

export interface JsonBook {
title: string;
summary: string;
}

0 comments on commit 6b93638

Please sign in to comment.