This commit is contained in:
2026-01-18 17:54:34 +03:00
parent 6668001a97
commit 709e3f3867
2 changed files with 32 additions and 2 deletions

View File

@@ -6,8 +6,12 @@ import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.ai.openai.api.ResponseFormat;
import org.springframework.ai.support.ToolCallbacks;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.stereotype.Service;
import ru.ldeloff.aitools.telegrambot.tool.DateTimeTool;
import java.util.List;
@@ -18,12 +22,16 @@ public class LlmService {
private final OpenAiChatModel model;
public List<String> sendMessages(List<Message> messages) {
return model.call(
return model
.call(
new Prompt(
messages,
OpenAiChatOptions.builder()
.temperature(0.1)
.responseFormat(ResponseFormat.builder().type(ResponseFormat.Type.TEXT).build())
.toolCallbacks(ToolCallbacks.from(new DateTimeTool()))
.responseFormat(
ResponseFormat.builder().type(ResponseFormat.Type.TEXT).build()
)
.build()
)

View File

@@ -0,0 +1,22 @@
package ru.ldeloff.aitools.telegrambot.tool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
@Slf4j
public class DateTimeTool {
@Tool(description = "Returns current date and time in the given timezone. Default: Europe/Moscow. Use IANA timezone IDs (e.g., America/New_York, Asia/Tokyo).")
String getCurrentDateTime(@ToolParam(description = "IANA timezone ID (e.g., Europe/Moscow, America/New_York). Default: Europe/Moscow") String timezone) {
log.info("getTime called for timezone: {}", timezone);
ZoneId zoneId = ZoneId.of(timezone==null ? "Europe/Moscow" : timezone);
LocalDateTime now = LocalDateTime.now(zoneId);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return now.format(formatter);
}
}