-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathtask_agent.py
More file actions
44 lines (37 loc) · 1.3 KB
/
task_agent.py
File metadata and controls
44 lines (37 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from agent.base_agent import AgentSystem
from agent.llm_withtools import chat_with_agent
from utils.common import extract_jsons
class TaskAgent(AgentSystem):
def forward(self, inputs):
"""
An agent that solves a given task.
Args:
inputs (dict): A dictionary with input data for the task.
Returns:
tuple:
- prediction (str): The prediction made by the agent.
- new_msg_history (list): A list of messages representing the message history of the interaction.
"""
domain = inputs['domain']
instruction = f"""You are an agent.
Task input:
```
{inputs}
```
Respond in JSON format with the following schema:
<json>
{{
"response": ...
}}
</json>"""
new_msg_history = chat_with_agent(instruction, model=self.model, msg_history=[], logging=self.log)
# Extract the response
prediction = "None"
try:
extracted_jsons = extract_jsons(new_msg_history[-1]['text'])
if extracted_jsons is not None and "response" in extracted_jsons[-1]:
prediction = extracted_jsons[-1]['response']
except Exception as e:
self.log(f"Error extracting prediction: {e}")
prediction = "None"
return prediction, new_msg_history