Skip to content

Commit

Permalink
Updating comments
Browse files Browse the repository at this point in the history
  • Loading branch information
teresaqhoang committed Aug 2, 2023
1 parent fa83129 commit 60cdd11
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
18 changes: 13 additions & 5 deletions webapi/CopilotChat/Skills/ChatSkills/CopilotChatPlanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public class CopilotChatPlanner
/// </summary>
public PlannerOptions? PlannerOptions => this._plannerOptions;

/// <summary>
/// Flag to indicate that a variable is unknown and needs to be filled in by the user.
/// This is used to flag any inputs that had dependencies from removed steps.
/// </summary>
private static readonly string UNKNOWN_VARIABLE_FLAG = "$???";

/// <summary>
/// Initializes a new instance of the <see cref="CopilotChatPlanner"/> class.
/// </summary>
Expand Down Expand Up @@ -78,7 +84,8 @@ public async Task<Plan> CreatePlanAsync(string goal, ILogger logger)
#region Private

/// <summary>
/// Scrubs plan of functions not available in planner's kernel.
/// Scrubs plan of functions not available in planner's kernel
/// and flags any effected input dependencies with '$???' to prompt for user input.
/// <param name="plan">Proposed plan object to sanitize.</param>
/// <param name="availableFunctions">The functions available in the planner's kernel.</param>
/// <param name="logger">Logger from context.</param>
Expand All @@ -91,16 +98,18 @@ private Plan SanitizePlan(Plan plan, FunctionsView availableFunctions, ILogger l

foreach (var step in plan.Steps)
{
// Check if function exists in planner's kernel
if (this.Kernel.Skills.TryGetFunction(step.SkillName, step.Name, out var function))
{
availableOutputs.AddRange(step.Outputs);

// Regex to match variable names
Regex variableRegEx = new(@"\$([A-Za-z_]+)", RegexOptions.Singleline);
Regex variableRegEx = new(@"\$((\w+[_-]*)+)", RegexOptions.Singleline);

// Check for any inputs that may have dependencies from removed steps
foreach (var input in step.Parameters)
{
// Check for any inputs that may have dependencies from removed steps
// Check if input contains a variable
Match inputVariableMatch = variableRegEx.Match(input.Value);
if (inputVariableMatch.Success)
{
Expand All @@ -116,13 +125,12 @@ private Plan SanitizePlan(Plan plan, FunctionsView availableFunctions, ILogger l
&& inputVariableMatch.Groups[1].Captures.Count == 1
&& !unavailableOutputs.Any(output => string.Equals(output, inputVariableValue, StringComparison.OrdinalIgnoreCase))
? "$PLAN.RESULT" // TODO: [Issue #2256] Extract constants from Plan class, requires change on kernel team
: "$???";
: UNKNOWN_VARIABLE_FLAG;
step.Parameters.Set(input.Key, Regex.Replace(input.Value, variableRegEx.ToString(), overrideValue));
}
}
}
}

sanitizedSteps.Add(step);
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,14 @@ public async Task<string> AcquireExternalInformationAsync(
private bool IsRetriableError(Exception e)
{
var retryOnInvalidPlanError = e is PlanningException
&& (e as PlanningException)!.ErrorCode == PlanningException.ErrorCodes.InvalidPlan;
&& (e as PlanningException)!.ErrorCode == PlanningException.ErrorCodes.InvalidPlan
&& this._planner.PlannerOptions!.AllowRetriesOnInvalidPlan;

var retryOnMissingFunctionError = e is KernelException
&& (e as KernelException)!.ErrorCode == KernelException.ErrorCodes.FunctionNotAvailable
&& this._planner.PlannerOptions!.SkipOnMissingFunctionsError;

return this._planner.PlannerOptions!.AllowRetriesOnInvalidPlan
&& (retryOnMissingFunctionError || retryOnInvalidPlanError);
return retryOnMissingFunctionError || retryOnInvalidPlanError;
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions webapi/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"Models": {
"Completion": "gpt-35-turbo", // For OpenAI, change to 'gpt-3.5-turbo' (with a period).
"Embedding": "text-embedding-ada-002",
"Planner": "gpt-4" // For OpenAI, change to 'gpt-3.5-turbo' (with a period).
"Planner": "gpt-35-turbo" // For OpenAI, change to 'gpt-3.5-turbo' (with a period).
}
},
//
Expand All @@ -57,7 +57,7 @@
"Planner": {
"Type": "Sequential",
// Set RelevancyThreshold to a value >= 0.50 if using the SequentialPlanner with gpt-3.5-turbo. Ignored when Planner:Type is "Action"
"RelevancyThreshold": "0.75",
"RelevancyThreshold": "0.80",
// Whether to allow missing functions in the plan on creation then sanitize output. Functions are considered missing if they're not available in the planner's kernel's context.
// If set to true, the plan will be created with missing functions as no-op steps that are filtered from the final proposed plan.
// If this is set to false, the plan creation will fail if any functions are missing.
Expand Down
13 changes: 7 additions & 6 deletions webapp/src/components/chat/plan-viewer/PlanStepInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ const useClasses = makeStyles({

// Regex to match interpolated variables in the form of $VARIABLE_NAME or $VARIABLE2_NAME.
// Variables that are not interpolated will fail to match.
// \$([A-Z]+[_-]*)+ matches the variable name
// (?=[\sa-z[\].]+) is a positive lookahead matching the end of static string
// \$(\w+[_-]*)+ matches the variable name
// (?=[^\w]+) is a positive lookahead matching the end of static string
// (?:.+\s*) is a noncapturing group that matches the start of static string
const INTERPOLATED_VARIABLE_REGEX = /((\$([A-Z]+[_-]*)+)(?=[\sa-z[\].]+))|((?:.+\s*)(\$([A-Z]+[_-]*)+))/g;
const INTERPOLATED_VARIABLE_REGEX = /((\$(\w+[_-]*)+)(?=[^\w]+))|((?:.+\s*)(\$(\w+[_-]*)+))/g;

interface PlanStepInputProps {
input: IPlanInput;
Expand Down Expand Up @@ -94,6 +94,7 @@ export const PlanStepInput: React.FC<PlanStepInputProps> = ({
);

const onSubmitEdit = useCallback(() => {
// Input was corrected, remove validation error from parent component
if (input.Value.includes(Constants.sk.UNKNOWN_VARIABLE_FLAG)) {
setValidationErrors(validationErrors - 1);
}
Expand All @@ -105,10 +106,10 @@ export const PlanStepInput: React.FC<PlanStepInputProps> = ({
}, [formValue, validationErrors, input, onEdit, setValidationErrors]);

const onCancel = useCallback(() => {
setIsEditingInput(formValue.includes(Constants.sk.UNKNOWN_VARIABLE_FLAG));
setEditsRequired(input.Value.includes(Constants.sk.UNKNOWN_VARIABLE_FLAG));
setIsEditingInput(requiresEdits(formValue));
setEditsRequired(requiresEdits(input.Value));
setFormValue(input.Value);
}, [input, formValue]);
}, [requiresEdits, formValue, input.Value]);

return (
<Badge
Expand Down

0 comments on commit 60cdd11

Please sign in to comment.