-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodel_utils.py
More file actions
108 lines (90 loc) · 3.57 KB
/
Copy pathmodel_utils.py
File metadata and controls
108 lines (90 loc) · 3.57 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import os
import json
import asyncio
import logging
from typing import Dict, Tuple, Any, Optional
from src.utils.cmd_utils import run_claude_code, prompt_claude
class ModelUtils:
"""
A wrapper class to handle model operations for the Anthropic model.
This class provides unified interfaces for prompting models,
abstracting away the implementation details of the API.
Attributes:
configs (dict): Configuration settings
logger (logging.Logger, optional): Logger to use
"""
def __init__(self, configs: Dict[str, Any] = None, logger: Optional[logging.Logger] = None) -> None:
"""
Initialize the ModelUtils with configuration.
Args:
configs (dict): Configuration settings
logger (logging.Logger, optional): Logger to use. If None, logs to console only.
"""
self.configs = configs or {}
self.logger = logger
if self.logger:
self.logger.info(f"Initialized ModelUtils")
async def prompt_agent(
self,
prompt: str,
feedback: str = "",
agent_name: str = None,
sub_agent_name: str = None,
timeout: int = None,
) -> Tuple[bool, Dict]:
"""
Execute model command via Claude Code API with the given prompt.
This is a wrapper for run_claude_code.
Args:
prompt (str): The prompt to send to the model
feedback (str): Optional feedback to append to the prompt for retries
agent_name (str): The name of the agent running the command
sub_agent_name (str, optional): The name of the sub-agent running the command
timeout (int, optional): Maximum time in seconds to wait for model's response.
If None, no timeout will be applied.
Returns:
tuple[bool, dict]: (success_status, captured_output)
- success_status: True for both normal completions and timeouts
- captured_output: Dictionary containing model response details
Raises:
ValueError: If agent_name is not provided
"""
return await run_claude_code(
prompt=prompt,
feedback=feedback,
configs=self.configs,
logger=self.logger,
agent_name=agent_name,
sub_agent_name=sub_agent_name,
timeout=timeout,
)
async def prompt_model(
self,
prompt: str,
feedback: str = "",
agent_name: str = None,
sub_agent_name: str = None,
) -> Tuple[bool, Dict]:
"""
Execute model via Claude API with the given prompt.
This is a wrapper for prompt_claude.
Args:
prompt (str): The prompt to send to the model
feedback (str): Optional feedback to append to the prompt for retries
agent_name (str): The name of the agent running the command
sub_agent_name (str, optional): The name of the sub-agent running the command
Returns:
tuple[bool, dict]: (success_status, parsed_output)
- success_status: True if command executed successfully and output was valid
- parsed_output: The parsed output from the model, or None if unsuccessful
Raises:
ValueError: If agent_name is not provided
"""
return await prompt_claude(
prompt=prompt,
feedback=feedback,
configs=self.configs,
logger=self.logger,
agent_name=agent_name,
sub_agent_name=sub_agent_name,
)