Skip to content

Latest commit

 

History

History
98 lines (81 loc) · 2.99 KB

11_a_OPTIMIZE_GENERATED_CODE.md

File metadata and controls

98 lines (81 loc) · 2.99 KB

Establishing Dual Agents for Code Development in Hyv

Overview

This guide outlines the process of setting up two distinct agents in Hyv for enhancing code development quality. Here, one agent will be responsible for generating the initial code, while the other will optimize it.

Prerequisites

A basic understanding of JavaScript and familiarity with the Hyv library is essential to follow this guide successfully.

Guide

Importing Required Modules

The first step is to import the necessary modules from the Hyv library and the built-in Node.js 'util' module.

import { inspect } from "node:util";
import { Agent } from "@hyv/core";
import { createInstructionTemplate, GPTModelAdapter } from "@hyv/openai";
import { extractCode, minify } from "@hyv/utils";

Establishing the Developer and Optimizer Agents

Next, we establish two agents, namely developer and optimizer. Each agent will be configured with a distinct GPTModelAdapter that carries a unique system instruction.

const developer = new Agent(
    new GPTModelAdapter({
        maxTokens: 2048,
        model: "gpt-4",
        systemInstruction: createInstructionTemplate(
            "expert JavaScript Developer, expert Canvas2D Developer, **performance expert**",
            minify`
            Achieve the {{goal}}.
            Use the {{boilerplate}}.
            `,
            {
                thoughts: "elaborative thoughts",
                code: "valid JavaScript",
            }
        ),
    }),
    { verbosity: 1 }
);

const optimizer = new Agent(
    new GPTModelAdapter({
        maxTokens: 2048,
        model: "gpt-4",
        systemInstruction: createInstructionTemplate(
            "expert JavaScript Developer, expert Canvas2D Developer, **performance expert**",
            minify`
            Review the {{code}}.
            Look for potential errors and fix them.
            Optimize the {{code}} as needed.
            `,
            {
                review: "elaborative review and critique",
                code: "valid JavaScript (original or optimized)",
            }
        ),
    }),
    {
        verbosity: 1,
        sideEffects: [
            {
                prop: "code",
                async run(value: string) {
                    const { code } = extractCode(value);
                    console.log("SIDE EFFECT");
                    console.log(code);
                },
            },
        ],
    }
);

Here, the developer agent has been assigned to achieve a specified goal using a provided boilerplate. The optimizer agent's responsibility is to review the code generated by the developer, identify any potential errors, and optimize the code as necessary.

Summary

In this guide, we explored how to utilize two agents in Hyv for better code development. This approach allows for initial code generation and subsequent optimization, enhancing the overall code quality.

Tags

Hyv, dual-agents, code-development, code-optimization, AI, GPT-4, JavaScript, Canvas2D