Java: File IO skill (#1399)

Co-authored-by: Mark Wallace <[email protected]>
This commit is contained in:
Bruno Borges
2023-06-16 15:33:14 +01:00
committed by GitHub
co-authored by Mark Wallace
parent 1155eff673
commit 53458c5efc
3 changed files with 154 additions and 0 deletions
+11
View File
@@ -23,6 +23,17 @@
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,82 @@
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.coreskills;
import com.microsoft.semantickernel.skilldefinition.annotations.DefineSKFunction;
import com.microsoft.semantickernel.skilldefinition.annotations.SKFunctionInputAttribute;
import com.microsoft.semantickernel.skilldefinition.annotations.SKFunctionParameters;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* Read and write from a file.
*
* <p>Usage: kernel.ImportSkill("file", new FileIOSkill());
*
* <p>Examples:
*
* <pre>{@code
* file.readAsync($path) => "hello world"
* file.writeAsync()
* }</pre>
*/
public class FileIOSkill {
/**
* Read a file.
*
* <p>Example:
*
* <pre>{@code
* file.readAsync($path) => "hello world"
* }</pre>
*
* @param path Source file.
* @return File content.
*/
@DefineSKFunction(description = "Read a file", name = "readAsync")
public String readFileAsync(
@SKFunctionInputAttribute
@SKFunctionParameters(name = "path", description = "Source file")
String path,
@SKFunctionParameters(name = "charset", description = "Character set to use to read the file", defaultValue="UTF-8")
String charset) throws IOException {
Path filePath = Paths.get(path);
byte[] fileBytes;
fileBytes = Files.readAllBytes(filePath);
String fileContents = new String(fileBytes, charset);
return fileContents;
}
/**
* Write a file.
*
* <p>Example:
*
* <pre>{@code
* file.writeAsync()
* }</pre>
*
* @param path The destination file.
* @param content Contains the 'content' to write
* @return An awaitable task.
*/
@DefineSKFunction(description = "Write a file", name = "writeAsync")
public void writeFileAsync(
@SKFunctionInputAttribute
@SKFunctionParameters(name = "path", description = "Destination file")
String path,
@SKFunctionParameters(
name = "content",
description = "File content",
defaultValue = "",
type = String.class)
String content,
@SKFunctionParameters(name = "charset", description = "Character set to use to read the file", defaultValue="UTF-8")
String charset) throws IOException {
Path filePath = Paths.get(path);
Files.write(filePath, content.getBytes(charset));
}
}
@@ -0,0 +1,61 @@
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.coreskills;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import com.microsoft.semantickernel.orchestration.ContextVariables;
import com.microsoft.semantickernel.orchestration.SKContext;
import reactor.test.StepVerifier;
public class FileIOSkillTest {
@Test
public void testReadFileAsync() throws IOException {
// Create a temporary file with some content
String content = "Hello, World!";
Path tempFile = Files.createTempFile("test", ".txt");
Files.write(tempFile, content.getBytes());
// Create an instance of FileIOSkill
FileIOSkill fileIOSkill = new FileIOSkill();
// Call the readFileAsync method with the temporary file path
String filePath = tempFile.toAbsolutePath().toString();
String outcome = fileIOSkill.readFileAsync(filePath, "UTF-8");
// Assert content (expected) is the same as the outcome (actual)
Assertions.assertEquals(content, outcome);
// Delete the temporary file
Files.delete(tempFile);
}
@Test
public void testWriteFileAsync() throws IOException {
// Create a temporary file
Path tempFile = Files.createTempFile("test", ".txt");
// Create an instance of FileIOSkill
FileIOSkill fileIOSkill = new FileIOSkill();
// Define the content to write
String content = "Hello, World!";
// Call the writeFileAsync method with the temporary file path and content
fileIOSkill.writeFileAsync(tempFile.toAbsolutePath().toString(), content, "UTF-8");
// Read the written file to verify the content
String fileContents = new String(Files.readAllBytes(tempFile));
Assertions.assertEquals(content, fileContents);
// Delete the temporary file
Files.delete(tempFile);
}
}