forked from microsoft/semantic-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileIOSkill.cs
53 lines (49 loc) · 1.53 KB
/
FileIOSkill.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright (c) Microsoft. All rights reserved.
using System.IO;
using System.Threading.Tasks;
using Microsoft.SemanticKernel.Orchestration;
using Microsoft.SemanticKernel.SkillDefinition;
namespace Microsoft.SemanticKernel.CoreSkills;
/// <summary>
/// Read and write from a file.
/// </summary>
/// <example>
/// Usage: kernel.ImportSkill("file", new FileIOSkill());
/// Examples:
/// {{file.readAsync $path }} => "hello world"
/// {{file.writeAsync}}
/// </example>
public class FileIOSkill
{
/// <summary>
/// Read a file
/// </summary>
/// <example>
/// {{file.readAsync $path }} => "hello world"
/// </example>
/// <param name="path"> Source file </param>
/// <returns> File content </returns>
[SKFunction("Read a file")]
[SKFunctionInput(Description = "Source file")]
public Task<string?> ReadAsync(string path)
{
return File.ReadAllTextAsync(path);
}
/// <summary>
/// Write a file
/// </summary>
/// <example>
/// {{file.writeAsync}}
/// </example>
/// <param name="context">
/// Contains the 'path' for the Destination file and 'content' of the file to write.
/// </param>
/// <returns> An awaitable task </returns>
[SKFunction("Write a file")]
[SKFunctionContextParameter(Name = "path", Description = "Destination file")]
[SKFunctionContextParameter(Name = "content", Description = "File content")]
public Task WriteAsync(SKContext context)
{
return File.WriteAllTextAsync(context["path"], context["content"]);
}
}