Skip to content

Commit 6e0d5e2

Browse files
committed
feat(agent): agent can now be asked question using the .ask method
Signed-off-by: Valentin De Matos <[email protected]>
1 parent 070932a commit 6e0d5e2

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

agentarium/Agent.py

+44-2
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,15 @@ class Agent:
7171
},
7272
}
7373

74-
_default_act_prompt = """
74+
_default_self_introduction_prompt = """
7575
Informations about yourself:
7676
{agent_informations}
7777
7878
Your interactions:
7979
{interactions}
80+
"""
81+
82+
_default_act_prompt = """{self_introduction}
8083
8184
Given the above information, think about what you should do next.
8285
@@ -127,6 +130,8 @@ def __init__(self, **kwargs):
127130
self.agent_informations["bio"] = Agent._generate_agent_bio(self.agent_informations)
128131

129132
self._interaction_manager.register_agent(self)
133+
134+
self._self_introduction_prompt = deepcopy(Agent._default_self_introduction_prompt)
130135
self._act_prompt = deepcopy(Agent._default_act_prompt)
131136

132137
self._actions = deepcopy(Agent._default_actions)
@@ -315,9 +320,13 @@ def act(self) -> str:
315320
# [TALK][AGENT_ID][CONTENT]: Talk to the agent with the given ID. Note that you must the agent ID, not the agent name. (i.e [TALK][123][Hello!])
316321
# [CHATGPT][CONTENT]: Use ChatGPT. (i.e [CHATGPT][Hello!])
317322

318-
prompt = self._act_prompt.format(
323+
self_introduction = self._self_introduction_prompt.format(
319324
agent_informations=self.agent_informations,
320325
interactions=self.get_interactions(),
326+
)
327+
328+
prompt = self._act_prompt.format(
329+
self_introduction=self_introduction,
321330
actions="\n".join([f"{action['format']}: {action['prompt']} (i.e {action['example']})" for action in self._actions.values()]),
322331
list_of_actions=list(self._actions.keys()),
323332
)
@@ -439,6 +448,39 @@ def think(self, message: str) -> None:
439448
"message": message,
440449
}
441450

451+
def ask(self, message: str) -> None:
452+
"""
453+
Ask the agent a question and receive a contextually aware response.
454+
455+
The agent considers its characteristics and interaction history when formulating
456+
the response, maintaining consistency with its persona.
457+
458+
Note: The agent will not save the question nor the response in its interaction history.
459+
460+
Args:
461+
message (str): The question to ask the agent.
462+
463+
Returns:
464+
str: The agent's response to the question.
465+
"""
466+
467+
prompt = self._self_introduction_prompt.format(
468+
agent_informations=self.agent_informations,
469+
interactions=self.get_interactions(),
470+
)
471+
472+
prompt += f"\nYou are asked the following question: {message}. Answer the question as best as you can."
473+
474+
response = llm_client.chat.completions.create(
475+
model=f"{config.llm_provider}:{config.llm_model}",
476+
messages=[
477+
{"role": "user", "content": prompt},
478+
],
479+
temperature=0.4,
480+
)
481+
482+
return response.choices[0].message.content
483+
442484
def add_new_action(self, action_descriptor: dict[str, str], action_function: Callable[[Agent, str], str | dict]) -> None:
443485
"""
444486
Add a new action to the agent's capabilities.

agentarium/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from .AgentInteractionManager import AgentInteractionManager
44
from .Interaction import Interaction
55

6-
__version__ = "0.2.7"
6+
__version__ = "0.2.8"
77

88
__all__ = [
99
"Agent",

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "agentarium"
7-
version = "0.2.7"
7+
version = "0.2.8"
88
authors = [
99
{ name = "thytu" },
1010
]

0 commit comments

Comments
 (0)