feat: support history, norms and goals for LLM
ref: N25B-299
This commit is contained in:
@@ -12,8 +12,11 @@ from control_backend.agents.base import BaseAgent
|
||||
from control_backend.core.agent_system import InternalMessage
|
||||
from control_backend.core.config import settings
|
||||
from control_backend.schemas.belief_message import Belief, BeliefMessage
|
||||
from control_backend.schemas.llm_prompt_message import LLMPromptMessage
|
||||
from control_backend.schemas.ri_message import SpeechCommand
|
||||
|
||||
DELIMITER = ";\n" # TODO: temporary until we support lists in AgentSpeak
|
||||
|
||||
|
||||
class BDICoreAgent(BaseAgent):
|
||||
bdi_agent: agentspeak.runtime.Agent
|
||||
@@ -112,7 +115,9 @@ class BDICoreAgent(BaseAgent):
|
||||
self._add_belief(belief.name, belief.arguments)
|
||||
|
||||
def _add_belief(self, name: str, args: Iterable[str] = []):
|
||||
new_args = (agentspeak.Literal(arg) for arg in args)
|
||||
# new_args = (agentspeak.Literal(arg) for arg in args) # TODO: Eventually support multiple
|
||||
merged_args = DELIMITER.join(arg for arg in args)
|
||||
new_args = (agentspeak.Literal(merged_args),)
|
||||
term = agentspeak.Literal(name, new_args)
|
||||
|
||||
self.bdi_agent.call(
|
||||
@@ -178,21 +183,37 @@ class BDICoreAgent(BaseAgent):
|
||||
the function expects (which will be located in `term.args`).
|
||||
"""
|
||||
|
||||
@self.actions.add(".reply", 1)
|
||||
def _reply(agent, term, intention):
|
||||
@self.actions.add(".reply", 3)
|
||||
def _reply(agent: "BDICoreAgent", term, intention):
|
||||
"""
|
||||
Sends text to the LLM.
|
||||
Sends text to the LLM (AgentSpeak action).
|
||||
Example: .reply("Hello LLM!", "Some norm", "Some goal")
|
||||
"""
|
||||
message_text = agentspeak.grounded(term.args[0], intention.scope)
|
||||
norms = agentspeak.grounded(term.args[1], intention.scope)
|
||||
goals = agentspeak.grounded(term.args[2], intention.scope)
|
||||
|
||||
asyncio.create_task(self._send_to_llm(str(message_text)))
|
||||
self.logger.debug("Norms: %s", norms)
|
||||
self.logger.debug("Goals: %s", goals)
|
||||
self.logger.debug("User text: %s", message_text)
|
||||
|
||||
asyncio.create_task(self._send_to_llm(str(message_text), str(norms), str(goals)))
|
||||
yield
|
||||
|
||||
async def _send_to_llm(self, text: str):
|
||||
async def _send_to_llm(self, text: str, norms: str = None, goals: str = None):
|
||||
"""
|
||||
Sends a text query to the LLM agent asynchronously.
|
||||
"""
|
||||
msg = InternalMessage(to=settings.agent_settings.llm_name, sender=self.name, body=text)
|
||||
prompt = LLMPromptMessage(
|
||||
text=text,
|
||||
norms=norms.split("\n") if norms else [],
|
||||
goals=goals.split("\n") if norms else [],
|
||||
)
|
||||
msg = InternalMessage(
|
||||
to=settings.agent_settings.llm_name,
|
||||
sender=self.name,
|
||||
body=prompt.model_dump_json(),
|
||||
)
|
||||
await self.send(msg)
|
||||
self.logger.info("Message sent to LLM agent: %s", text)
|
||||
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
+user_said(Message) <-
|
||||
norms("").
|
||||
goals("").
|
||||
|
||||
+user_said(Message) : norms(Norms) & goals(Goals) <-
|
||||
-user_said(Message);
|
||||
.reply(Message).
|
||||
.reply(Message, Norms, Goals).
|
||||
|
||||
Reference in New Issue
Block a user