Skip to content

Commit

Permalink
?
Browse files Browse the repository at this point in the history
  • Loading branch information
emmalin-7 committed Feb 6, 2025
1 parent 4e86d31 commit b1b17c5
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 35 deletions.
4 changes: 1 addition & 3 deletions agential/training/agent_optimizer/strategies/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@ def __init__(
max_steps: int,
max_tokens: int,
enc: Encoding,
max_actions_per_step: int,
max_trials: int, #is same thing as above? check again
testing: bool = False,
) -> None:
"""Initialization."""
super().__init__(llm=llm, testing=testing)
self.max_steps = max_steps
self.max_tokens = max_tokens
self.enc = enc
self.max_actions_per_step = max_actions_per_step
self.max_steps = max_steps
self._max_trials = 3
self._trial_conversations_history = []
self._trial_conversations_performance = []
Expand Down
34 changes: 4 additions & 30 deletions agential/training/agent_optimizer/strategies/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class PromptOptimizerGeneralStrategy(PromptOptimizerBaseStrategy):
def __init__(
self,
llm: BaseLLM,
max_actions_per_step: int,
max_steps: int,
max_steps: int = 6,
max_tokens: int = 5000,
enc: Encoding = tiktoken.encoding_for_model("gpt-3.5-turbo"),
Expand All @@ -68,15 +68,15 @@ def __init__(
"""Initialization."""
super().__init__(
llm=llm,
max_actions_per_step=max_actions_per_step,
max_steps=max_steps,
max_steps=max_steps,
max_tokens=max_tokens,
enc=enc,
testing=testing,
optimizer_model=optimizer_model,
)

self.max_actions_per_step = max_actions_per_step
self.max_steps = max_steps
self._max_trials = 3
self.optimizer_model = optimizer_model

Expand Down Expand Up @@ -175,32 +175,6 @@ def execute_prompt(

return str(result)


def execute_func(name, packages, code, **args):
"""
The wrapper for generated functions.
"""
pip_install = (
f"""print("Installing package: {packages}")\nsubprocess.run(["pip", "-qq", "install", "{packages}"])"""
if packages
else ""
)
str = f"""
import subprocess
{pip_install}
print("Result of {name} function execution:")
{code}
args={args}
result={name}(**args)
if result is not None: print(result)
"""
print(f"execute_code:\n{str}")
result = execute_code(str, use_docker="shaokun529/evoagent:v1")
if result[0] != 0:
raise Exception("Error in executing function:" + result[1])
print(f"Result: {result[1]}")
return result[1]

def generate(
self,
objective: str,
Expand Down Expand Up @@ -382,7 +356,7 @@ def step(self):
statistic_prompts = [f"Step statistics: Current performance: {performance}"]
experience_prompts = failure_prompts if failure_prompts else best_prompts

for action_index in range(self.max_actions_per_step):
for action_index in range(self.max_steps):
action_prompts = [
f"Action {action_index}: Generate a strategy based on the current performance.",
f"Best prompts so far: {', '.join(best_prompts)}",
Expand Down
4 changes: 2 additions & 2 deletions notebooks/agent_optimizer.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 5,
"metadata": {},
"outputs": [
{
Expand All @@ -88,7 +88,7 @@
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[2], line 5\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01magential\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mtraining\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01magent_optimizer\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01magent\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m PromptOptimizer\n\u001b[1;32m 3\u001b[0m question \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mWho was once considered the best kick boxer in the world, however he has been involved in a number of controversies relating to his \u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124munsportsmanlike conducts\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m in the sport and crimes of violence outside of the ring\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[0;32m----> 5\u001b[0m agent \u001b[38;5;241m=\u001b[39m \u001b[43mPromptOptimizer\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 6\u001b[0m \u001b[43m \u001b[49m\u001b[43mllm\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mllm\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 7\u001b[0m \u001b[43m \u001b[49m\u001b[43mbenchmark\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mhotpotqa\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 8\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;66;43;03m# kwargs.\u001b[39;49;00m\n\u001b[1;32m 9\u001b[0m \u001b[43m \u001b[49m\u001b[43mmax_steps\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m8\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 10\u001b[0m \u001b[43m \u001b[49m\u001b[43mmax_tokens\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m5000\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 11\u001b[0m \u001b[43m \u001b[49m\u001b[43menc\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtiktoken\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mencoding_for_model\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mgpt-3.5-turbo\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 12\u001b[0m \u001b[43m \u001b[49m\u001b[43mdocstore\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mDocstoreExplorer\u001b[49m\u001b[43m(\u001b[49m\u001b[43mWikipedia\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 13\u001b[0m \u001b[43m\t\u001b[49m\u001b[43mmax_actions_per_step\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m3\u001b[39;49m\n\u001b[1;32m 14\u001b[0m \u001b[43m)\u001b[49m\n\u001b[1;32m 16\u001b[0m out \u001b[38;5;241m=\u001b[39m agent\u001b[38;5;241m.\u001b[39mgenerate(\n\u001b[1;32m 17\u001b[0m question\u001b[38;5;241m=\u001b[39mquestion,\n\u001b[1;32m 18\u001b[0m examples\u001b[38;5;241m=\u001b[39mHOTPOTQA_FEWSHOT_EXAMPLES_REACT, \n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 21\u001b[0m reset\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m,\n\u001b[1;32m 22\u001b[0m )\n",
"Cell \u001b[0;32mIn[5], line 5\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01magential\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mtraining\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01magent_optimizer\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01magent\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m PromptOptimizer\n\u001b[1;32m 3\u001b[0m question \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mWho was once considered the best kick boxer in the world, however he has been involved in a number of controversies relating to his \u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124munsportsmanlike conducts\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m in the sport and crimes of violence outside of the ring\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[0;32m----> 5\u001b[0m agent \u001b[38;5;241m=\u001b[39m \u001b[43mPromptOptimizer\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 6\u001b[0m \u001b[43m \u001b[49m\u001b[43mllm\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mllm\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 7\u001b[0m \u001b[43m \u001b[49m\u001b[43mbenchmark\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mhotpotqa\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 8\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;66;43;03m# kwargs.\u001b[39;49;00m\n\u001b[1;32m 9\u001b[0m \u001b[43m \u001b[49m\u001b[43mmax_steps\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m8\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 10\u001b[0m \u001b[43m \u001b[49m\u001b[43mmax_tokens\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m5000\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 11\u001b[0m \u001b[43m \u001b[49m\u001b[43menc\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtiktoken\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mencoding_for_model\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mgpt-3.5-turbo\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 12\u001b[0m \u001b[43m \u001b[49m\u001b[43mdocstore\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mDocstoreExplorer\u001b[49m\u001b[43m(\u001b[49m\u001b[43mWikipedia\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 13\u001b[0m \u001b[43m\t\u001b[49m\u001b[43mmax_actions_per_step\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m3\u001b[39;49m\n\u001b[1;32m 14\u001b[0m \u001b[43m)\u001b[49m\n\u001b[1;32m 16\u001b[0m out \u001b[38;5;241m=\u001b[39m agent\u001b[38;5;241m.\u001b[39mgenerate(\n\u001b[1;32m 17\u001b[0m question\u001b[38;5;241m=\u001b[39mquestion,\n\u001b[1;32m 18\u001b[0m examples\u001b[38;5;241m=\u001b[39mHOTPOTQA_FEWSHOT_EXAMPLES_REACT, \n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 21\u001b[0m reset\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m,\n\u001b[1;32m 22\u001b[0m )\n",
"File \u001b[0;32m~/agential/agential/training/agent_optimizer/agent.py:140\u001b[0m, in \u001b[0;36mPromptOptimizer.__init__\u001b[0;34m(self, llm, benchmark, testing, **strategy_kwargs)\u001b[0m\n\u001b[1;32m 137\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Initialization.\"\"\"\u001b[39;00m\n\u001b[1;32m 138\u001b[0m \u001b[38;5;28msuper\u001b[39m()\u001b[38;5;241m.\u001b[39m\u001b[38;5;21m__init__\u001b[39m(llm\u001b[38;5;241m=\u001b[39mllm, benchmark\u001b[38;5;241m=\u001b[39mbenchmark, testing\u001b[38;5;241m=\u001b[39mtesting)\n\u001b[0;32m--> 140\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mstrategy \u001b[38;5;241m=\u001b[39m \u001b[43mPromptOptimizer\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_strategy\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 141\u001b[0m \u001b[43m \u001b[49m\u001b[43mbenchmark\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mbenchmark\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 142\u001b[0m \u001b[43m \u001b[49m\u001b[43mllm\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mllm\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 143\u001b[0m \u001b[43m \u001b[49m\u001b[43mtesting\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtesting\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 144\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mstrategy_kwargs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 145\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/agential/agential/training/agent_optimizer/agent.py:205\u001b[0m, in \u001b[0;36mPromptOptimizer.get_strategy\u001b[0;34m(benchmark, **kwargs)\u001b[0m\n\u001b[1;32m 202\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mUnsupported benchmark: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mbenchmark\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m for agent ReAct\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 204\u001b[0m strategy \u001b[38;5;241m=\u001b[39m REACT_STRATEGIES[benchmark]\n\u001b[0;32m--> 205\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mstrategy\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
"\u001b[0;31mTypeError\u001b[0m: PromptOptimizerQAStrategy.__init__() got an unexpected keyword argument 'max_actions_per_step'"
Expand Down

0 comments on commit b1b17c5

Please sign in to comment.