Spaces:
Sleeping
Sleeping
Create agent.py
Browse files
agent.py
ADDED
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from typing import List, Dict, Optional
|
3 |
+
from openai import OpenAI
|
4 |
+
from strategy import StrategyFactory, ExecutionStrategy
|
5 |
+
|
6 |
+
class Agent:
|
7 |
+
def __init__(self, name: str):
|
8 |
+
self._name = name
|
9 |
+
self._persona = ""
|
10 |
+
self._instruction = ""
|
11 |
+
self._task = ""
|
12 |
+
self._api_key = os.getenv('OPENAI_API_KEY', '')
|
13 |
+
self._model = "gpt-4o-mini"
|
14 |
+
self._history: List[Dict[str, str]] = []
|
15 |
+
self._strategy: Optional[ExecutionStrategy] = None
|
16 |
+
|
17 |
+
@property
|
18 |
+
def name(self) -> str:
|
19 |
+
return self._name
|
20 |
+
|
21 |
+
@property
|
22 |
+
def persona(self) -> str:
|
23 |
+
return self._persona
|
24 |
+
|
25 |
+
@persona.setter
|
26 |
+
def persona(self, value: str):
|
27 |
+
self._persona = value
|
28 |
+
|
29 |
+
@property
|
30 |
+
def instruction(self) -> str:
|
31 |
+
return self._instruction
|
32 |
+
|
33 |
+
@instruction.setter
|
34 |
+
def instruction(self, value: str):
|
35 |
+
self._instruction = value
|
36 |
+
|
37 |
+
@property
|
38 |
+
def task(self) -> str:
|
39 |
+
return self._task
|
40 |
+
|
41 |
+
@task.setter
|
42 |
+
def task(self, value: str):
|
43 |
+
self._task = value
|
44 |
+
|
45 |
+
@property
|
46 |
+
def strategy(self) -> Optional[ExecutionStrategy]:
|
47 |
+
return self._strategy
|
48 |
+
|
49 |
+
@strategy.setter
|
50 |
+
def strategy(self, strategy_name: str):
|
51 |
+
"""Set the execution strategy by name."""
|
52 |
+
self._strategy = StrategyFactory.create_strategy(strategy_name)
|
53 |
+
|
54 |
+
@property
|
55 |
+
def history(self) -> List[Dict[str, str]]:
|
56 |
+
return self._history
|
57 |
+
|
58 |
+
def _build_messages(self, task: Optional[str] = None) -> List[Dict[str, str]]:
|
59 |
+
"""Build the messages list including persona, instruction, and history."""
|
60 |
+
messages = [{"role": "system", "content": self.persona}]
|
61 |
+
|
62 |
+
if self.instruction:
|
63 |
+
messages.append({
|
64 |
+
"role": "user",
|
65 |
+
"content": f"Global Instruction: {self.instruction}"
|
66 |
+
})
|
67 |
+
|
68 |
+
# Add conversation history
|
69 |
+
messages.extend(self._history)
|
70 |
+
|
71 |
+
# Use provided task or stored task
|
72 |
+
current_task = task if task is not None else self._task
|
73 |
+
|
74 |
+
# Apply strategy if set
|
75 |
+
if self._strategy and current_task:
|
76 |
+
current_task = self._strategy.build_prompt(current_task, self.instruction)
|
77 |
+
|
78 |
+
# Add the current task if it exists
|
79 |
+
if current_task:
|
80 |
+
messages.append({"role": "user", "content": current_task})
|
81 |
+
|
82 |
+
return messages
|
83 |
+
|
84 |
+
def execute(self, task: Optional[str] = None) -> str:
|
85 |
+
"""Execute a task using the configured LLM."""
|
86 |
+
if task is not None:
|
87 |
+
self._task = task
|
88 |
+
|
89 |
+
if not self._api_key:
|
90 |
+
return "API key not found. Please set the OPENAI_API_KEY environment variable."
|
91 |
+
|
92 |
+
if not self._task:
|
93 |
+
return "No task specified. Please provide a task to execute."
|
94 |
+
|
95 |
+
client = OpenAI(api_key=self._api_key)
|
96 |
+
messages = self._build_messages()
|
97 |
+
|
98 |
+
try:
|
99 |
+
response = client.chat.completions.create(
|
100 |
+
model=self._model,
|
101 |
+
messages=messages
|
102 |
+
)
|
103 |
+
|
104 |
+
response_content = response.choices[0].message.content
|
105 |
+
|
106 |
+
# Process response through strategy if set
|
107 |
+
if self._strategy:
|
108 |
+
response_content = self._strategy.process_response(response_content)
|
109 |
+
|
110 |
+
# Store the interaction in history
|
111 |
+
self._history.append({"role": "user", "content": self._task})
|
112 |
+
self._history.append({
|
113 |
+
"role": "assistant",
|
114 |
+
"content": response_content
|
115 |
+
})
|
116 |
+
|
117 |
+
# Clear the task after execution
|
118 |
+
self._task = ""
|
119 |
+
|
120 |
+
return response_content
|
121 |
+
except Exception as e:
|
122 |
+
return f"An error occurred: {str(e)}"
|
123 |
+
|
124 |
+
def clear_history(self):
|
125 |
+
"""Clear the conversation history."""
|
126 |
+
self._history = []
|
127 |
+
|
128 |
+
def available_strategies(self) -> List[str]:
|
129 |
+
"""Return a list of available strategy names."""
|
130 |
+
return StrategyFactory.available_strategies()
|