Skip to content

Commit

Permalink
clean up structured messages for more robustness...
Browse files Browse the repository at this point in the history
  • Loading branch information
yeus committed Oct 12, 2024
1 parent 964bac2 commit e9d3cc9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
26 changes: 18 additions & 8 deletions src/modules/taskyon/taskWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
deepMerge,
keysToLowerCase,
normalizeFalsyValues,
pickProperties,
sleep,
} from '../utils';
import { isTaskyonKey } from '../crypto';
Expand Down Expand Up @@ -444,29 +445,38 @@ async function generateFollowUpTasksFromResult(
// In fact we always decide right here, what we do *after* the structured response and simply add the
// structured response as a normal "message" task to the chain...
// this way we can put all the parsing logic & interpretation and all of this here. While
// our tasks only have to process the actual data ther're receiving
// our tasks only have to process the actual data they are receiving

const retry =
!yesnoToBoolean(structResponse['stop']) ||
yesnoToBoolean(structResponse['try again']) ||
yesnoToBoolean(structResponse['should we retry?']);
yesnoToBoolean(structResponse['use tool']) ||
yesnoToBoolean(structResponse['try again']);

if (retry) {
const newTaskid = await addFollowUpTask(false, {
role: 'assistant',
content: { structuredResponse: choice.message.content },
});

// this doesn't say anything about whether the parameters are
// chosen correctly for this function yet. It only says that
// they are valid parameters for any function...
const res = FunctionCall.safeParse(structResponse.command);
if (res.success) {
const newTaskid = await addFollowUpTask(false, {
role: 'assistant',
content: { structuredResponse: choice.message.content },
});
const command = res.data;
void addFollowUpTask(true, {
parentID: newTaskid,
role: 'assistant',
content: { functionCall: command },
});
} else {
void addFollowUpTask(true, {
parentID: newTaskid,
role: 'system',
content: {
message: `The response (${pickProperties(structResponse, ['use tool', 'try again'])})
suggest we should use a tool, but we could not parse the ${structResponse.command}`,
},
});
}
} else {
// in the case that we don't call a tool, provide a "normal" answer :)
Expand Down
14 changes: 8 additions & 6 deletions src/modules/taskyon/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export const RemoteFunctionResponse = RemoteFunctionBase.extend({
export type RemoteFunctionResponse = z.infer<typeof RemoteFunctionResponse>;

const answer = z.string().nullish();
const yesno = z.enum(['yes', 'no', 'n/a']).or(z.boolean()).nullish();
const yesno = z.enum(['yes', 'no']).or(z.boolean()).nullish();
type yesno = z.infer<typeof yesno>;

// Convert yesno value to boolean
Expand Down Expand Up @@ -269,9 +269,10 @@ const SystemResponseEvaluation = z
'describe your thoughts': answer,
'was there an error?': yesno,
'do you think we can solve the error?': yesno,
'Should we use one of the mentioned tools to answer the task?': yesno,
'Would it help to use one of the mentioned tools to solve the issue?':
yesno,
'Should we try to correct the error': yesno,
'try again': yesno,
stop: yesno,
})
.describe(
'This is used as a short prompt for tasks in order to determine whether we should use a more detailed task prompt',
Expand All @@ -280,10 +281,11 @@ const SystemResponseEvaluation = z
const ToolResultBase = z
.object({
'describe your thoughts': answer,
'was the tool call successfull?': answer.or(yesno),
'was there an error?': yesno,
'should we retry?': yesno,
'should we use another tool?': answer.or(yesno),
'was the tool call successfull?': answer.or(yesno),
'should we use a different tool?': answer.or(yesno),
'should we use different parameters': yesno,
'try again': yesno,
})
.describe(
'Structured answer schema for processing the result of a function call.',
Expand Down
9 changes: 9 additions & 0 deletions src/modules/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,9 @@ export function normalizeFalsyValues(input: unknown): unknown {
// Define the set of "falsy" values
const falsyValues: Set<unknown> = new Set([
'no',
'n/a',
'na',
'nan',
'n',
'false',
false,
Expand Down Expand Up @@ -733,3 +736,9 @@ export function normalizeFalsyValues(input: unknown): unknown {

return traverse(input);
}

export function pickProperties(obj: object, keys: string[]) {
return Object.fromEntries(
Object.entries(obj).filter(([key]) => keys.includes(key)),
);
}

0 comments on commit e9d3cc9

Please sign in to comment.