Skip to content

Commit 83b4736

Browse files
committed
Rename the Picocli command, AssistantService and add a Tool able to read/write file
Signed-off-by: Charles Moulliard <cmoulliard@ibm.com>
1 parent 413c932 commit 83b4736

File tree

4 files changed

+85
-32
lines changed

4 files changed

+85
-32
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package dev.snowdrop.langchain4j.vertexai;
2+
3+
import io.quarkus.picocli.runtime.annotations.TopCommand;
4+
import jakarta.inject.Inject;
5+
import picocli.CommandLine.Command;
6+
import picocli.CommandLine.Parameters;
7+
8+
@TopCommand
9+
@Command(name = "code", mixinStandardHelpOptions = true)
10+
public class CodeAssistantCommand implements Runnable {
11+
12+
@Parameters(defaultValue = "Java Hello World class", description = "The tasks to be executed by the AI coding assistant")
13+
String task;
14+
15+
@Inject
16+
CodeAssistantService codeAssistantService;
17+
18+
@Override
19+
public void run() {
20+
System.out.println(codeAssistantService.writeCode(task));
21+
}
22+
}

ai/src/main/java/dev/snowdrop/langchain4j/vertexai/MyAiService.java renamed to ai/src/main/java/dev/snowdrop/langchain4j/vertexai/CodeAssistantService.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,18 @@
33
import dev.langchain4j.service.SystemMessage;
44
import dev.langchain4j.service.UserMessage;
55
import io.quarkiverse.langchain4j.RegisterAiService;
6+
import io.quarkiverse.langchain4j.ToolBox;
67
import jakarta.enterprise.context.ApplicationScoped;
78

89
@RegisterAiService
910
@ApplicationScoped
10-
public interface MyAiService {
11+
public interface CodeAssistantService {
1112

1213
// We got an error HTTP 400: Unexpected role "system". The Messages API accepts a top-level `system` parameter,
1314
// not "system" as an input message role."
1415
// See: https://platform.claude.com/docs/en/api/messages#message_param
1516
//@SystemMessage("You are a professional poet")
1617

17-
@UserMessage("""
18-
Write a poem about {topic}.
19-
The poem should be {lines} lines long.
20-
""")
21-
String writeAPoem(String topic, int lines);
18+
@ToolBox(FileSystemTool.class)
19+
String writeCode(@UserMessage String task);
2220
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package dev.snowdrop.langchain4j.vertexai;
2+
3+
import dev.langchain4j.agent.tool.P;
4+
import dev.langchain4j.agent.tool.Tool;
5+
import jakarta.enterprise.context.ApplicationScoped;
6+
import org.jboss.logging.Logger;
7+
8+
import java.io.IOException;
9+
import java.nio.file.Files;
10+
import java.nio.file.Path;
11+
import java.nio.file.Paths;
12+
13+
@ApplicationScoped
14+
public class FileSystemTool {
15+
private static final Logger logger = Logger.getLogger(FileSystemTool.class.getName());
16+
17+
@Tool("Reads the full content of a specified file")
18+
public String readFile(@P("Path to the file to analyze") String path) {
19+
try {
20+
logger.info("Reading file: " + path);
21+
return Files.readString(Paths.get(path));
22+
} catch (IOException e) {
23+
return "Error reading file: " + e.getMessage();
24+
}
25+
}
26+
27+
@Tool("Writes the content to the specified file, overwriting it if it exists")
28+
public String writeFile(@P("Path to the file where the content must be changed") String path,
29+
@P("The content that you AI proposes to change") String content) {
30+
31+
logger.debugf("Writing file: %s", path);
32+
logger.debugf("String content : %s", content);
33+
34+
if (path == null || path.isBlank() || content == null) {
35+
return "Error: File path and content cannot be null or empty. Please provide both.";
36+
}
37+
38+
/*
39+
* Console console = System.console(); if (console == null) { return
40+
* "Confirmation failed: Console not available."; }
41+
*
42+
* logger.info("\n--------------------------------------------------");
43+
* logger.info("AI is requesting to WRITE to the file: " + path); logger.info("Content to be written:");
44+
* logger.info(content); logger.info("--------------------------------------------------"); String confirmation
45+
* = console.readLine("Do you want to proceed? (y/n): ");
46+
*
47+
* if (!"y".equalsIgnoreCase(confirmation)) { return "Write operation cancelled by user."; }
48+
*/
49+
50+
try {
51+
Path filePath = Paths.get(path);
52+
logger.debugf("File path is: %s", filePath);
53+
Files.writeString(filePath, content);
54+
return "File '" + path + "' written successfully.";
55+
} catch (IOException e) {
56+
return "Error writing file: " + e.getMessage();
57+
}
58+
}
59+
}

ai/src/main/java/dev/snowdrop/langchain4j/vertexai/PoemCommand.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)