Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eslint update #38

Merged
merged 1 commit into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// module.exports = {
// root: true,
// parser: '@typescript-eslint/parser',
// parserOptions: {
// tsconfigRootDir: __dirname,
// ecmaVersion: 6,
// sourceType: 'module',
// },
// plugins: ['@typescript-eslint/eslint-plugin'],
// extends: ['plugin:@typescript-eslint/recommended', 'plugin:prettier/recommended'],
// rules: {
// 'no-unused-vars': 'off',
// '@typescript-eslint/no-unused-vars': 'off',
// '@typescript-eslint/naming-convention': ['warn', { selector: 'enumMember', format: ['UPPER_CASE'] }],
// '@typescript-eslint/semi': 'warn',
// curly: 'off',
// eqeqeq: 'warn',
// 'no-throw-literal': 'warn',
// semi: 'off',
// '@typescript-eslint/no-empty-function': 'off',
// 'prettier/prettier': 'warn',
// 'no-return-await': 'warn',
// },
// ignorePatterns: ['out', 'dist', '**/*.d.ts'],
// };

{
"root": true,
"extends": ["@10clouds/eslint-config", "plugin:prettier/recommended"],
"plugins": ["prettier", "simple-import-sort", "unused-imports"],
"rules": {
"prettier/prettier": "warn",
"@typescript-eslint/no-floating-promises": "off",
"@typescript-eslint/no-invalid-this": "off",
"@typescript-eslint/no-unnecessary-condition": "off",
"simple-import-sort/imports": "warn",
"simple-import-sort/exports": "warn",
"unused-imports/no-unused-imports": "warn",
"newline-before-return": "warn",
"no-empty-function": "off"
},
"parserOptions": {
"project": true
},
"ignorePatterns": ["out", "dist", "**/*.d.ts"]
}
27 changes: 0 additions & 27 deletions .eslintrc.js

This file was deleted.

5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"trailingComma": "all",
"singleQuote": true,
"printWidth": 80
}
2 changes: 1 addition & 1 deletion examples/runLLMCustomTaskWithStepEvolve/Criterion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ export type MultiCriterion<S> = {
}[];
calculate: (solution: S) => number;
suggestions: (solution: S) => string[];
}
};
4 changes: 2 additions & 2 deletions examples/runLLMCustomTaskWithStepEvolve/CustomTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export class CustomTask implements TaskContext {
stageTargetProgress,
startTime,
logContent,
rejectTask: rejectTask,
resolveTask: resolveTask,
rejectTask,
resolveTask,
}: {
id: string;
userInput: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { FitnessAndNextSolutionsFunction, SolutionWithMeta } from '../../src/stepEvolve/FitnessFunction';
import { createSolutionsFromFixes } from '../../src/stepEvolve/createSolutionsFromFixes';
import { TaskDefinition } from './TaskDefinition';
import {
FitnessAndNextSolutionsFunction,
SolutionWithMeta,
} from '../../src/stepEvolve/FitnessFunction';
import { createFixesForSolution } from './createFixesForSolution';
import { criteriaDefinition } from './criteriaDefinition';
import { rateSolution } from './rateSolution';
import { TaskDefinition } from './TaskDefinition';

export function createFitnessAndNextSolutionsFunction({
task,
Expand All @@ -12,11 +15,19 @@ export function createFitnessAndNextSolutionsFunction({
task: TaskDefinition;
maxBranching: number;
}): FitnessAndNextSolutionsFunction<string> {
const fitnessAndNextSolutionsFunction = async (solutionWithMeta: SolutionWithMeta<string>) => {
const { finalRating, criteriaRatings } = await rateSolution(task, solutionWithMeta);
const fitnessAndNextSolutionsFunction = async (
solutionWithMeta: SolutionWithMeta<string>,
) => {
const { finalRating, criteriaRatings } = await rateSolution(
task,
solutionWithMeta,
);

const criteriaWithRatings = criteriaDefinition.map((c) => {
const criteriaRating = criteriaRatings.find((cr) => cr.criteria === c.name);
const criteriaRating = criteriaRatings.find(
(cr) => cr.criteria === c.name,
);

return {
...c,
rating: criteriaRating?.rating ?? 0,
Expand All @@ -30,7 +41,11 @@ export function createFitnessAndNextSolutionsFunction({
return { id: c.name, fitness: c.rating };
}),
nextPossibleSolutions: async (): Promise<SolutionWithMeta<string>[]> => {
const fixes = await createFixesForSolution(task, solutionWithMeta, criteriaWithRatings);
const fixes = await createFixesForSolution(
task,
solutionWithMeta,
criteriaWithRatings,
);

return createSolutionsFromFixes({
solutionWithMeta,
Expand Down
69 changes: 55 additions & 14 deletions examples/runLLMCustomTaskWithStepEvolve/createFixesForSolution.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
import { z } from 'zod';

import { createFullPromptFromSections } from '../../src/gpt/createFullPromptFromSections';
import { gptExecute } from '../../src/gpt/gptExecute';
import { GPTMode } from '../../src/gpt/types';
import { Fix, SolutionWithMeta } from '../../src/stepEvolve/FitnessFunction';
import { getRandomElement } from '../../src/utils/random/getRandomElement';
import { shuffleArray } from '../../src/utils/random/shuffleArray';
import { Criterion } from './Criterion';
import { TaskDefinition } from './TaskDefinition';
import { createNewSolutionFix } from './fixes/fixCreateNewSolution';
import { improveSolutionFix } from './fixes/fixImproveSolution';
import { TaskDefinition } from './TaskDefinition';

export async function createFixesForSolution(
task: TaskDefinition,
solutionWithMeta: SolutionWithMeta<string>,
criteriaWithRatings: (Criterion<string> & { rating: number; reasoning: string })[],
criteriaWithRatings: (Criterion<string> & {
rating: number;
reasoning: string;
})[],
): Promise<Fix<string>[]> {
let averageCriteriaRating = 0;
if (criteriaWithRatings.length > 0) {
averageCriteriaRating = criteriaWithRatings.map((c) => c.rating).reduce((a, b) => a + b, 0) / criteriaWithRatings.length;
averageCriteriaRating =
criteriaWithRatings.map((c) => c.rating).reduce((a, b) => a + b, 0) /
criteriaWithRatings.length;
}

const criteriaWithBelowAverageRating = criteriaWithRatings.filter((c) => c.rating <= averageCriteriaRating);
const criteriaGPT = criteriaWithBelowAverageRating.filter((c) => c.suggestions === 'GPT');
const criteriaCalculate = criteriaWithBelowAverageRating.filter((c) => c.suggestions !== 'GPT');
const criteriaWithBelowAverageRating = criteriaWithRatings.filter(
(c) => c.rating <= averageCriteriaRating,
);
const criteriaGPT = criteriaWithBelowAverageRating.filter(
(c) => c.suggestions === 'GPT',
);
const criteriaCalculate = criteriaWithBelowAverageRating.filter(
(c) => c.suggestions !== 'GPT',
);

const result =
criteriaGPT.length > 0
Expand All @@ -38,7 +50,10 @@ export async function createFixesForSolution(
PROBLEM: task.task,
SOLUTION: solutionWithMeta.solution,
CRITERIA: shuffleArray(criteriaGPT.slice())
.map((c) => `${c.name} - Max of ${c.maxPoints}pts if ${c.maxPointsIf}`)
.map(
(c) =>
`${c.name} - Max of ${c.maxPoints}pts if ${c.maxPointsIf}`,
)
.join('\n'),
},
}),
Expand All @@ -48,29 +63,55 @@ export async function createFixesForSolution(
outputSchema: z
.object({
suggestions: z
.array(z.string().describe('Suggestion to the user, what should be improved in order to maximize criteria.'))
.array(
z
.string()
.describe(
'Suggestion to the user, what should be improved in order to maximize criteria.',
),
)
.describe('Suggestions'),
})
.describe('Suggestions'),
})
: { result: { suggestions: [] } };

const criteriaWithAboveAverageRating = criteriaWithRatings.filter((c) => c.rating > averageCriteriaRating);
const criteriaWithAboveAverageRating = criteriaWithRatings.filter(
(c) => c.rating > averageCriteriaRating,
);
const allSuggestions = [
...result.result.suggestions,
...criteriaCalculate.map((c) => (c.suggestions !== 'GPT' ? c.suggestions(solutionWithMeta.solution) : [])).flat(),
...criteriaCalculate
.map((c) =>
c.suggestions !== 'GPT' ? c.suggestions(solutionWithMeta.solution) : [],
)
.flat(),
];

console.log('allSuggestions', allSuggestions);

const fixes = [
createNewSolutionFix(task),
...allSuggestions.map((suggestions) =>
improveSolutionFix({ task, solutionWithMeta, suggestions: [suggestions, ...criteriaWithAboveAverageRating.map((c) => c.maintain)].join('\n') }),
),
...Array(3).fill(
improveSolutionFix({ task, solutionWithMeta, suggestions: [...allSuggestions, ...criteriaWithAboveAverageRating.map((c) => c.maintain)].join('\n') }),
improveSolutionFix({
task,
solutionWithMeta,
suggestions: [
suggestions,
...criteriaWithAboveAverageRating.map((c) => c.maintain),
].join('\n'),
}),
),
...(Array(3).fill(
improveSolutionFix({
task,
solutionWithMeta,
suggestions: [
...allSuggestions,
...criteriaWithAboveAverageRating.map((c) => c.maintain),
].join('\n'),
}),
) as Fix<string>[]),
];

return fixes;
Expand Down
12 changes: 8 additions & 4 deletions examples/runLLMCustomTaskWithStepEvolve/criteriaDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ export const criteriaDefinition: Criterion<string>[] = [
const emojiPattern = emojiRegex();
const emojis = solution.match(emojiPattern);
const points = 20 - (emojis?.length || 0) * 5;

return points;
},
suggestions: (solution) => {
if (solution.match(emojiRegex())) {
return ['Use less emojis', 'Remove emojis'];
} else {
return [];
}

return [];
},
},
{
Expand All @@ -30,6 +31,7 @@ export const criteriaDefinition: Criterion<string>[] = [
calculate: (solution) => {
const distance = Math.max(Math.abs(750 - solution.length) - 50, 0);
const points = 20 / (1 + distance / 50);

return points;
},
suggestions: (solution) => {
Expand Down Expand Up @@ -70,8 +72,10 @@ export const criteriaDefinition: Criterion<string>[] = [
},
{
name: 'Style of great CEO',
maxPointsIf: 'post should be written in a style that a person like Steve Jobs, Bill Gates or Elon Musk could have written it',
maintain: 'Keep the post in a style that a person like Steve Jobs, Bill Gates or Elon Musk could have written it',
maxPointsIf:
'post should be written in a style that a person like Steve Jobs, Bill Gates or Elon Musk could have written it',
maintain:
'Keep the post in a style that a person like Steve Jobs, Bill Gates or Elon Musk could have written it',
maxPoints: 20,
calculate: 'GPT',
suggestions: 'GPT',
Expand Down
2 changes: 1 addition & 1 deletion examples/runLLMCustomTaskWithStepEvolve/emptyDirSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fs from 'fs';
export function emptyDirSync(path: string) {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach((file) => {
const curPath = path + '/' + file;
const curPath = `${path}/${file}`;
if (fs.lstatSync(curPath).isDirectory()) {
// if it's a directory, recurse
emptyDirSync(curPath);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { z } from 'zod';

import { gptExecute } from '../../../src/gpt/gptExecute';
import { GPTMode } from '../../../src/gpt/types';
import { TaskDefinition } from '../TaskDefinition';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { z } from 'zod';

import { createFullPromptFromSections } from '../../../src/gpt/createFullPromptFromSections';
import { gptExecute } from '../../../src/gpt/gptExecute';
import { GPTMode } from '../../../src/gpt/types';
Expand All @@ -22,7 +23,12 @@ export function improveSolutionFix({
fullPrompt: createFullPromptFromSections({
intro:
'Improve the following SOLUTION to the PROBLEM described below, use SUGGESTIONS as guidance. Do not output any section markers or additional sections in your response, just the new improved solution.',
sections: { PROBLEM: task.task, SOLUTION: solutionWithMeta.solution, SUGGESTIONS: suggestions, 'YOUR PROPOSED NEW SOLUTION': '' },
sections: {
PROBLEM: task.task,
SOLUTION: solutionWithMeta.solution,
SUGGESTIONS: suggestions,
'YOUR PROPOSED NEW SOLUTION': '',
},
}),
maxTokens: 500,
mode: GPTMode.FAST,
Expand Down
Loading