{"version":3,"file":"prompt_generator.cjs","names":["FINISH_NAME"],"sources":["../../../src/experimental/autogpt/prompt_generator.ts"],"sourcesContent":["import { isInteropZodSchema } from \"@langchain/core/utils/types\";\nimport {\n  type JsonSchema7Type,\n  toJsonSchema,\n} from \"@langchain/core/utils/json_schema\";\nimport { ObjectTool, FINISH_NAME } from \"./schema.js\";\n\n/**\n * Class that generates prompts for generative agents. It maintains a list\n * of constraints, commands, resources, and performance evaluations.\n */\nexport class PromptGenerator {\n  constraints: string[];\n\n  commands: ObjectTool[];\n\n  resources: string[];\n\n  performance_evaluation: string[];\n\n  response_format: object;\n\n  constructor() {\n    this.constraints = [];\n    this.commands = [];\n    this.resources = [];\n    this.performance_evaluation = [];\n    this.response_format = {\n      thoughts: {\n        text: \"thought\",\n        reasoning: \"reasoning\",\n        plan: \"- short bulleted\\n- list that conveys\\n- long-term plan\",\n        criticism: \"constructive self-criticism\",\n        speak: \"thoughts summary to say to user\",\n      },\n      command: { name: \"command name\", args: { \"arg name\": \"value\" } },\n    };\n  }\n\n  /**\n   * Adds a constraint to the list of constraints.\n   * @param constraint The constraint to add.\n   * @returns void\n   */\n  add_constraint(constraint: string): void {\n    this.constraints.push(constraint);\n  }\n\n  /**\n   * Adds a tool to the list of commands.\n   * @param tool The tool to add.\n   * @returns void\n   */\n  add_tool(tool: ObjectTool): void {\n    this.commands.push(tool);\n  }\n\n  _generate_command_string(tool: ObjectTool): string {\n    let output = `\"${tool.name}\": ${tool.description}`;\n    const jsonSchema = (\n      isInteropZodSchema(tool.schema) ? toJsonSchema(tool.schema) : tool.schema\n    ) as { properties?: Record<string, JsonSchema7Type> } | undefined;\n    output += `, args json schema: ${JSON.stringify(jsonSchema?.properties)}`;\n    return output;\n  }\n\n  /**\n   * Adds a resource to the list of resources.\n   * @param resource The resource to add.\n   * @returns void\n   */\n  add_resource(resource: string): void {\n    this.resources.push(resource);\n  }\n\n  /**\n   * Adds a performance evaluation to the list of performance evaluations.\n   * @param evaluation The performance evaluation to add.\n   * @returns void\n   */\n  add_performance_evaluation(evaluation: string): void {\n    this.performance_evaluation.push(evaluation);\n  }\n\n  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  _generate_numbered_list(items: any[], item_type = \"list\"): string {\n    if (item_type === \"command\") {\n      const command_strings = items.map(\n        (item, i) => `${i + 1}. ${this._generate_command_string(item)}`\n      );\n      const finish_description =\n        \"use this to signal that you have finished all your objectives\";\n      const finish_args =\n        '\"response\": \"final response to let people know you have finished your objectives\"';\n      const finish_string = `${\n        items.length + 1\n      }. ${FINISH_NAME}: ${finish_description}, args: ${finish_args}`;\n      return command_strings.concat([finish_string]).join(\"\\n\");\n    }\n\n    return items.map((item, i) => `${i + 1}. ${item}`).join(\"\\n\");\n  }\n\n  /**\n   * Generates a prompt string that includes the constraints, commands,\n   * resources, performance evaluations, and response format.\n   * @returns A string representing the prompt.\n   */\n  generate_prompt_string(): string {\n    const formatted_response_format = JSON.stringify(\n      this.response_format,\n      null,\n      4\n    );\n    const prompt_string =\n      `Constraints:\\n${this._generate_numbered_list(this.constraints)}\\n\\n` +\n      `Commands:\\n${this._generate_numbered_list(\n        this.commands,\n        \"command\"\n      )}\\n\\n` +\n      `Resources:\\n${this._generate_numbered_list(this.resources)}\\n\\n` +\n      `Performance Evaluation:\\n${this._generate_numbered_list(\n        this.performance_evaluation\n      )}\\n\\n` +\n      `You should only respond in JSON format as described below ` +\n      `\\nResponse Format: \\n${formatted_response_format} ` +\n      `\\nEnsure the response can be parsed by Python json.loads`;\n\n    return prompt_string;\n  }\n}\n\n/**\n * Function that generates a prompt string for a given list of tools.\n */\nexport function getPrompt(tools: ObjectTool[]): string {\n  const prompt_generator = new PromptGenerator();\n\n  prompt_generator.add_constraint(\n    \"~4000 word limit for short term memory. \" +\n      \"Your short term memory is short, \" +\n      \"so immediately save important information to files.\"\n  );\n  prompt_generator.add_constraint(\n    \"If you are unsure how you previously did something \" +\n      \"or want to recall past events, \" +\n      \"thinking about similar events will help you remember.\"\n  );\n  prompt_generator.add_constraint(\"No user assistance\");\n  prompt_generator.add_constraint(\n    'Exclusively use the commands listed in double quotes e.g. \"command name\"'\n  );\n\n  for (const tool of tools) {\n    prompt_generator.add_tool(tool);\n  }\n\n  prompt_generator.add_resource(\n    \"Internet access for searches and information gathering.\"\n  );\n  prompt_generator.add_resource(\"Long Term memory management.\");\n  prompt_generator.add_resource(\n    \"GPT-3.5 powered Agents for delegation of simple tasks.\"\n  );\n  prompt_generator.add_resource(\"File output.\");\n\n  prompt_generator.add_performance_evaluation(\n    \"Continuously review and analyze your actions \" +\n      \"to ensure you are performing to the best of your abilities.\"\n  );\n  prompt_generator.add_performance_evaluation(\n    \"Constructively self-criticize your big-picture behavior constantly.\"\n  );\n  prompt_generator.add_performance_evaluation(\n    \"Reflect on past decisions and strategies to refine your approach.\"\n  );\n  prompt_generator.add_performance_evaluation(\n    \"Every command has a cost, so be smart and efficient. \" +\n      \"Aim to complete tasks in the least number of steps.\"\n  );\n\n  const prompt_string = prompt_generator.generate_prompt_string();\n\n  return prompt_string;\n}\n"],"mappings":";;;;;;;;;AAWA,IAAa,kBAAb,MAA6B;CAC3B;CAEA;CAEA;CAEA;CAEA;CAEA,cAAc;AACZ,OAAK,cAAc,EAAE;AACrB,OAAK,WAAW,EAAE;AAClB,OAAK,YAAY,EAAE;AACnB,OAAK,yBAAyB,EAAE;AAChC,OAAK,kBAAkB;GACrB,UAAU;IACR,MAAM;IACN,WAAW;IACX,MAAM;IACN,WAAW;IACX,OAAO;IACR;GACD,SAAS;IAAE,MAAM;IAAgB,MAAM,EAAE,YAAY,SAAS;IAAE;GACjE;;;;;;;CAQH,eAAe,YAA0B;AACvC,OAAK,YAAY,KAAK,WAAW;;;;;;;CAQnC,SAAS,MAAwB;AAC/B,OAAK,SAAS,KAAK,KAAK;;CAG1B,yBAAyB,MAA0B;EACjD,IAAI,SAAS,IAAI,KAAK,KAAK,KAAK,KAAK;EACrC,MAAM,cAAA,GAAA,4BAAA,oBACe,KAAK,OAA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