Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions spring-ai-modules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<module>spring-ai-3</module>
<module>spring-ai-4</module>
<module>spring-ai-agentic-patterns</module>
<module>spring-ai-agent-skills</module>
<module>spring-ai-chat-stream</module>
<module>spring-ai-introduction</module>
<module>spring-ai-mcp</module>
Expand Down
65 changes: 65 additions & 0 deletions spring-ai-modules/spring-ai-agent-skills/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.baeldung</groupId>
<artifactId>spring-ai-modules</artifactId>
<version>0.0.1</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>spring-ai-agent-skills</artifactId>
<name>spring-ai-agent-skills</name>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-anthropic</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-core</artifactId>
<version>${tika.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>${spring-ai.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<properties>
<java.version>21</java.version>
<spring-ai.version>1.1.4</spring-ai.version>
<spring-boot.version>3.5.13</spring-boot.version>
<org.slf4j.version>2.0.17</org.slf4j.version>
<logback.version>1.5.18</logback.version>
<tika.version>3.3.0</tika.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.baeldung.springai.agentskills.anthropic;

import javax.validation.Valid;

import org.apache.tika.Tika;

import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/agent-skills")
@Validated
public class AgentSkillsController {

private final AgentSkillsService agentSkillsService;
private final Tika tika = new Tika();

public AgentSkillsController(AgentSkillsService agentSkillsService) {
this.agentSkillsService = agentSkillsService;
}

@GetMapping("/report")
public ResponseEntity<byte[]> genReport(@RequestBody @Valid ReportRequest reportRequest) {
AnthropicDocument document = agentSkillsService.genReport(reportRequest);
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(tika.detect(document.content())))
.header(HttpHeaders.CONTENT_DISPOSITION,
ContentDisposition.attachment()
.filename(document.fileName())
.build()
.toString())
.body(document.content());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.baeldung.springai.agentskills.anthropic;

import java.util.List;

import org.springframework.ai.anthropic.AnthropicChatModel;
import org.springframework.ai.anthropic.AnthropicChatOptions;
import org.springframework.ai.anthropic.AnthropicSkillsResponseHelper;
import org.springframework.ai.anthropic.api.AnthropicApi;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.stereotype.Service;

@Service
public class AgentSkillsService {

private final AnthropicChatModel chatModel;
private final AnthropicApi anthropicApi;
private final MonthlySalesService monthlySalesService;

public AgentSkillsService(AnthropicChatModel chatModel, AnthropicApi anthropicApi, MonthlySalesService monthlySalesService) {
this.chatModel = chatModel;
this.anthropicApi = anthropicApi;
this.monthlySalesService = monthlySalesService;
}

public AnthropicDocument genReport(ReportRequest reportRequest) {
ChatResponse response = ChatClient.create(chatModel)
.prompt()
.system("Given the dataset of monthly sales for our product: " + monthlySalesService.getMonthlySalesForYear(2025))
.user(reportRequest.prompt())
.options(AnthropicChatOptions.builder()
.model("claude-sonnet-4-5")
.skill(AnthropicApi.AnthropicSkill.DOCX)
.skill(AnthropicApi.AnthropicSkill.PDF)
.skill(AnthropicApi.AnthropicSkill.PPTX)
.skill(AnthropicApi.AnthropicSkill.XLSX)
.maxTokens(4096)
.build())
.call()
.chatResponse();

List<String> fileIds = AnthropicSkillsResponseHelper.extractFileIds(response);
if (fileIds.isEmpty()) {
throw new IllegalStateException("No document was generated by the DOCX skill");
}

return downloadReport(fileIds.get(0));
}

public AnthropicDocument downloadReport(String fileId) {
AnthropicApi.FileMetadata metadata = anthropicApi.getFileMetadata(fileId);
byte[] content = anthropicApi.downloadFile(fileId);
return new AnthropicDocument(metadata.filename(), content);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.baeldung.springai.agentskills.anthropic;

public record AnthropicDocument(String fileName, byte[] content) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.baeldung.springai.agentskills.anthropic;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.baeldung.springai.agentskills.anthropic;

import java.math.BigDecimal;
import java.time.Month;

public record MonthlySale(String product, int year, Month month, BigDecimal amount) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.baeldung.springai.agentskills.anthropic;

import java.math.BigDecimal;
import java.time.Month;
import java.util.List;

import org.springframework.stereotype.Service;

@Service
public class MonthlySalesService {

public List<MonthlySale> getMonthlySalesForYear(int year) {
return List.of(
new MonthlySale("Product A", year, Month.JANUARY, new BigDecimal("1200")),
new MonthlySale("Product A", year, Month.FEBRUARY, new BigDecimal("1325")),
new MonthlySale("Product A", year, Month.MARCH, new BigDecimal("1410")),
new MonthlySale("Product A", year, Month.APRIL, new BigDecimal("1380")),
new MonthlySale("Product A", year, Month.MAY, new BigDecimal("1495")),
new MonthlySale("Product A", year, Month.JUNE, new BigDecimal("1560")),
new MonthlySale("Product A", year, Month.JULY, new BigDecimal("1620")),
new MonthlySale("Product A", year, Month.AUGUST, new BigDecimal("1585")),
new MonthlySale("Product A", year, Month.SEPTEMBER, new BigDecimal("1660")),
new MonthlySale("Product A", year, Month.OCTOBER, new BigDecimal("1715")),
new MonthlySale("Product A", year, Month.NOVEMBER, new BigDecimal("1780")),
new MonthlySale("Product A", year, Month.DECEMBER, new BigDecimal("1850")),
new MonthlySale("Product B", year, Month.JANUARY, new BigDecimal("950")),
new MonthlySale("Product B", year, Month.FEBRUARY, new BigDecimal("990")),
new MonthlySale("Product B", year, Month.MARCH, new BigDecimal("1045")),
new MonthlySale("Product B", year, Month.APRIL, new BigDecimal("1015")),
new MonthlySale("Product B", year, Month.MAY, new BigDecimal("1090")),
new MonthlySale("Product B", year, Month.JUNE, new BigDecimal("1135")),
new MonthlySale("Product B", year, Month.JULY, new BigDecimal("1180")),
new MonthlySale("Product B", year, Month.AUGUST, new BigDecimal("1160")),
new MonthlySale("Product B", year, Month.SEPTEMBER, new BigDecimal("1215")),
new MonthlySale("Product B", year, Month.OCTOBER, new BigDecimal("1270")),
new MonthlySale("Product B", year, Month.NOVEMBER, new BigDecimal("1330")),
new MonthlySale("Product B", year, Month.DECEMBER, new BigDecimal("1395"))
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.baeldung.springai.agentskills.anthropic;

import javax.validation.constraints.NotNull;

public record ReportRequest(@NotNull String prompt) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
spring:

application:
name: agentskills-anthropic

ai:
anthropic:
api-key: "${ANTHROPIC_API_KEY}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
spring:
profiles:
active: anthropic
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.baeldung.springai.agentskills.anthropic;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;

@SpringBootTest(properties = {
"spring.profiles.active=test",
"spring.ai.anthropic.api-key=test-key"
})
@AutoConfigureMockMvc
class AgentSkillsControllerIntegrationTest {

@Autowired
private MockMvc mockMvc;

@MockitoBean
private AgentSkillsService agentSkillsService;

@Test
void whenReportRequestIsValid_thenEndpointReturnsGeneratedDocument() throws Exception {
byte[] documentContent = "%PDF-1.7\nMock PDF".getBytes();
ReportRequest reportRequest = new ReportRequest("Generate a monthly sales summary");
AnthropicDocument generatedDocument = new AnthropicDocument("sales-report.pdf", documentContent);

when(agentSkillsService.genReport(reportRequest)).thenReturn(generatedDocument);

mockMvc.perform(get("/agent-skills/report")
.contentType(MediaType.APPLICATION_JSON)
.content("""
{
"prompt": "Generate a monthly sales summary"
}
"""))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_PDF))
.andExpect(header().string(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"sales-report.pdf\""))
.andExpect(content().bytes(documentContent));

verify(agentSkillsService).genReport(reportRequest);
}

}