-
Notifications
You must be signed in to change notification settings - Fork 6
/
PullRequestSkill.cs
211 lines (173 loc) · 7.89 KB
/
PullRequestSkill.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright (c) Microsoft. All rights reserved.
using System.Reflection;
using Microsoft.Extensions.Logging;
using PRSkill.Utils;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Orchestration;
using Microsoft.SemanticKernel.SkillDefinition;
using CondenseSkillLib;
using Microsoft.SemanticKernel.AI.TextCompletion;
using System.ComponentModel;
namespace PRSkill;
public static class FunctionEx
{
public static async Task<SKContext> RollingChunkProcess(this ISKFunction func, List<string> chunkedInput, SKContext context)
{
context.Variables.Set("previousresults", string.Empty);
foreach (var chunk in chunkedInput)
{
context.Variables.Update(chunk);
context = await func.InvokeAsync(context);
context.Variables.Set("previousresults", context.Result);
}
return context;
}
public static async Task<SKContext> CondenseChunkProcess(this ISKFunction func, CondenseSkill condenseSkill, List<string> chunkedInput, string prompt, SKContext context, string resultTag)
{
var results = new List<string>();
foreach (var chunk in chunkedInput)
{
context.Variables.Update(chunk);
context = await func.InvokeAsync(context);
results.Add(context.Result);
}
if (chunkedInput.Count <= 1)
{
context.Variables.Update(context.Result);
return context;
}
// update memory with serialized list of results
context.Variables.Set("prompt", prompt);
return await condenseSkill.Condense(context, string.Join($"\n ====={resultTag}=====\n", results) + $"\n ====={resultTag}=====\n");
}
public static async Task<SKContext> AggregateChunkProcess(this ISKFunction func, List<string> chunkedInput, SKContext context)
{
var results = new List<string>();
foreach (var chunk in chunkedInput)
{
context.Variables.Update(chunk);
context = await func.InvokeAsync(context);
results.Add(context.Result);
}
context.Variables.Update(string.Join("\n", results));
return context;
}
}
public class PullRequestSkill
{
public const string SEMANTIC_FUNCTION_PATH = "PRSkill";
private const int CHUNK_SIZE = 8000; // Eventually this should come from the kernel
private readonly CondenseSkill condenseSkill;
private readonly IKernel _kernel;
public PullRequestSkill(IKernel kernel)
{
try
{
// Load semantic skill defined with prompt templates
var folder = PRSkillsPath();
var PRSkill = kernel.ImportSemanticSkillFromDirectory(folder, SEMANTIC_FUNCTION_PATH);
this.condenseSkill = new CondenseSkill(kernel);
this._kernel = Kernel.Builder
.WithAIService<ITextCompletion>(null, new RedirectTextCompletion(), true)
.Build();
this._kernel.ImportSemanticSkillFromDirectory(folder, SEMANTIC_FUNCTION_PATH);
}
catch (Exception e)
{
throw new Exception("Failed to load skill.", e);
}
}
[SKFunction, Description("Generate feedback for a pull request based on a git diff or git show file output.")]
public async Task<SKContext> GeneratePullRequestFeedback(
[Description("Output of a `git diff` or `git show` command.")]
string input,
SKContext context)
{
context.Logger.LogTrace("GeneratePullRequestFeedback called");
var prFeedbackGenerator = context.Skills.GetFunction(SEMANTIC_FUNCTION_PATH, "PullRequestFeedbackGenerator");
var chunkedInput = CommitChunker.ChunkCommitInfo(input, CHUNK_SIZE);
return await prFeedbackGenerator.AggregateChunkProcess(chunkedInput, context);
}
[SKFunction, Description("Generate a commit message based on a git diff file output.")]
public async Task<SKContext> GenerateCommitMessage(
[Description("Output of a `git diff` command.")]
string input,
SKContext context)
{
context.Logger.LogTrace("GenerateCommitMessage called");
var commitGenerator = context.Skills.GetFunction(SEMANTIC_FUNCTION_PATH, "CommitMessageGenerator");
var commitGeneratorCapture = this._kernel.Skills.GetFunction(SEMANTIC_FUNCTION_PATH, "CommitMessageGenerator");
var prompt = (await commitGeneratorCapture.InvokeAsync()).Result;
var chunkedInput = CommitChunker.ChunkCommitInfo(input, CHUNK_SIZE);
return await commitGenerator.CondenseChunkProcess(this.condenseSkill, chunkedInput, prompt, context, "CommitMessageResult");
}
[SKFunction, Description("Generate a pull request description based on a git diff or git show file output using a rolling query mechanism.")]
public async Task<SKContext> GeneratePR_Rolling(
[Description("Output of a `git diff` or `git show` command.")]
string input,
SKContext context)
{
var prGenerator_Rolling = context.Skills.GetFunction(SEMANTIC_FUNCTION_PATH, "PullRequestDescriptionGenerator_Rolling");
var chunkedInput = CommitChunker.ChunkCommitInfo(input, CHUNK_SIZE);
return await prGenerator_Rolling.RollingChunkProcess(chunkedInput, context);
}
[SKFunction, Description("Generate a pull request description based on a git diff or git show file output using a reduce mechanism.")]
public async Task<SKContext> GeneratePR(
[Description("Output of a `git diff` or `git show` command.")]
string input,
SKContext context)
{
var prGenerator = context.Skills.GetFunction(SEMANTIC_FUNCTION_PATH, "PullRequestDescriptionGenerator");
var prGeneratorCapture = this._kernel.Skills.GetFunction(SEMANTIC_FUNCTION_PATH, "PullRequestDescriptionGenerator");
var prompt = (await prGeneratorCapture.InvokeAsync()).Result;
var chunkedInput = CommitChunker.ChunkCommitInfo(input, CHUNK_SIZE);
return await prGenerator.CondenseChunkProcess(this.condenseSkill, chunkedInput, prompt, context, "PullRequestDescriptionResult");
}
#region MISC
private static string PRSkillsPath()
{
const string PARENT = "SemanticFunctions";
static bool SearchPath(string pathToFind, out string result, int maxAttempts = 10)
{
var currDir = Path.GetFullPath(Assembly.GetExecutingAssembly().Location);
bool found;
do
{
result = Path.Join(currDir, pathToFind);
found = Directory.Exists(result);
currDir = Path.GetFullPath(Path.Combine(currDir, ".."));
} while (maxAttempts-- > 0 && !found);
return found;
}
if (!SearchPath(PARENT, out string path))
{
throw new Exception("Skills directory not found. The app needs the skills from the library to work.");
}
return path;
}
#endregion MISC
}
public class RedirectTextCompletion : ITextCompletion
{
Task<IReadOnlyList<ITextResult>> ITextCompletion.GetCompletionsAsync(string text, CompleteRequestSettings requestSettings, CancellationToken cancellationToken)
{
return Task.FromResult<IReadOnlyList<ITextResult>>(new List<ITextResult> { new RedirectTextCompletionResult(text) });
}
IAsyncEnumerable<ITextStreamingResult> ITextCompletion.GetStreamingCompletionsAsync(string text, CompleteRequestSettings requestSettings, CancellationToken cancellationToken)
{
throw new NotImplementedException(); // TODO
}
}
internal sealed class RedirectTextCompletionResult : ITextResult
{
private readonly string _completion;
public RedirectTextCompletionResult(string completion)
{
this._completion = completion;
}
public ModelResult ModelResult => new ModelResult(this._completion);
public Task<string> GetCompletionAsync(CancellationToken cancellationToken = default)
{
return Task.FromResult(this._completion);
}
}