From 31a6801738bd79478fb644dc296e77bbe3be9ab0 Mon Sep 17 00:00:00 2001 From: Flavio Brasil Date: Thu, 21 Dec 2023 23:01:11 -0800 Subject: [PATCH] ais: agent sef-repair --- .../src/main/scala/kyo/llm/agents.scala | 16 +++++++++++---- .../kyo/llm/thoughts/reasoning/Repair.scala | 20 +++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 kyo-llm/shared/src/main/scala/kyo/llm/thoughts/reasoning/Repair.scala diff --git a/kyo-llm/shared/src/main/scala/kyo/llm/agents.scala b/kyo-llm/shared/src/main/scala/kyo/llm/agents.scala index f38c355c3..30a18ca05 100644 --- a/kyo-llm/shared/src/main/scala/kyo/llm/agents.scala +++ b/kyo-llm/shared/src/main/scala/kyo/llm/agents.scala @@ -6,6 +6,7 @@ import kyo.seqs._ import kyo.tries._ import kyo.locals._ import kyo.llm.ais._ +import kyo.llm.thoughts.reasoning._ import scala.util._ import kyo.llm.contexts._ import kyo.concurrent.atomics._ @@ -99,10 +100,11 @@ package object agents { } private[kyo] def handle(ai: AI, agents: Set[Agent], calls: List[Call]): Unit < AIs = - Seqs.traverseUnit(calls) { call => + Seqs.traverse(calls) { call => agents.find(_.info.name == call.function) match { case None => - ai.agentMessage(call.id, "Agent not found: " + call) + ai.agentMessage(call.id, "Agent doesn't exist anymore: " + Json.encode(call)) + .andThen(false) case Some(agent) => AIs.ephemeral { Agents.disable { @@ -120,11 +122,17 @@ package object agents { } }.map { case Success(result) => - ai.agentMessage(call.id, result) + ai.agentMessage(call.id, result).andThen(true) case Failure(ex) => - ai.agentMessage(call.id, "Failure:" + ex) + ai.agentMessage(call.id, "Agent failure:" + ex).andThen(false) } } + }.map { l => + if (!l.forall(identity)) { + ai.gen[Repair]("One or more agents failed.").unit + } else { + () + } } } } diff --git a/kyo-llm/shared/src/main/scala/kyo/llm/thoughts/reasoning/Repair.scala b/kyo-llm/shared/src/main/scala/kyo/llm/thoughts/reasoning/Repair.scala new file mode 100644 index 000000000..f3319960d --- /dev/null +++ b/kyo-llm/shared/src/main/scala/kyo/llm/thoughts/reasoning/Repair.scala @@ -0,0 +1,20 @@ +package kyo.llm.thoughts.reasoning + +import kyo.llm.ais._ + +@desc( + p""" + The Repair thought is used for introspection and correction following errors in the LLM's reasoning or output. + - Focuses on analyzing recent actions to identify the causes of failures. + - Aims to develop strategies to prevent similar errors in future reasoning processes. + - Encourages the LLM to critically evaluate its performance and apply corrective measures. + - This thought is crucial for the LLM's learning process, allowing it to adapt and improve over time. + - Example: If an error is detected in JSON generation, Repair directs the LLM to pinpoint the error's source and + modify its approach to ensure compliance with JSON standards. + """ +) +case class Repair( + `Analyze recent actions to identify failure causes`: String, + `Develop strategies to avoid similar errors`: String, + `Elaborate on the corrective measures to be applied`: String +)