|
| 1 | +import logging |
| 2 | +from dataclasses import dataclass |
| 3 | + |
| 4 | +import openai |
| 5 | +from openai import OpenAI |
| 6 | + |
| 7 | +from .base_api import AbstractChatModel, BaseModelArgs |
| 8 | + |
| 9 | + |
| 10 | +class ResponseModel(AbstractChatModel): |
| 11 | + def __init__( |
| 12 | + self, |
| 13 | + model_name, |
| 14 | + api_key=None, |
| 15 | + temperature=0.5, |
| 16 | + max_tokens=100, |
| 17 | + extra_kwargs=None, |
| 18 | + ): |
| 19 | + self.model_name = model_name |
| 20 | + self.api_key = api_key |
| 21 | + self.temperature = temperature |
| 22 | + self.max_tokens = max_tokens |
| 23 | + self.extra_kwargs = extra_kwargs or {} |
| 24 | + self.client = OpenAI(api_key=api_key) |
| 25 | + |
| 26 | + def __call__(self, content: dict, temperature: float = None) -> dict: |
| 27 | + temperature = temperature if temperature is not None else self.temperature |
| 28 | + try: |
| 29 | + response = self.client.responses.create( |
| 30 | + model=self.model_name, |
| 31 | + input=content, |
| 32 | + # temperature=temperature, |
| 33 | + # previous_response_id=content.get("previous_response_id", None), |
| 34 | + max_output_tokens=self.max_tokens, |
| 35 | + **self.extra_kwargs, |
| 36 | + tool_choice="required", |
| 37 | + reasoning={ |
| 38 | + "effort": "low", |
| 39 | + "summary": "detailed", |
| 40 | + }, |
| 41 | + ) |
| 42 | + return response |
| 43 | + except openai.OpenAIError as e: |
| 44 | + logging.error(f"Failed to get a response from the API: {e}") |
| 45 | + raise e |
| 46 | + |
| 47 | + |
| 48 | +class OpenAIResponseModel(ResponseModel): |
| 49 | + def __init__( |
| 50 | + self, model_name, api_key=None, temperature=0.5, max_tokens=100, extra_kwargs=None |
| 51 | + ): |
| 52 | + super().__init__( |
| 53 | + model_name=model_name, |
| 54 | + api_key=api_key, |
| 55 | + temperature=temperature, |
| 56 | + max_tokens=max_tokens, |
| 57 | + extra_kwargs=extra_kwargs, |
| 58 | + ) |
| 59 | + |
| 60 | + def __call__(self, messages: list[dict], temperature: float = None) -> dict: |
| 61 | + return super().__call__(messages, temperature) |
| 62 | + # outputs = response.output |
| 63 | + # last_computer_call_id = None |
| 64 | + # answer_type = "call" |
| 65 | + # reasoning = "No reasoning" |
| 66 | + # for output in outputs: |
| 67 | + # if output.type == "reasoning": |
| 68 | + # reasoning = output.summary[0].text |
| 69 | + # elif output.type == "computer_call": |
| 70 | + # action = output.action |
| 71 | + # last_computer_call_id = output.call_id |
| 72 | + # res = response_to_text(action) |
| 73 | + # elif output.type == "message": |
| 74 | + # res = "noop()" |
| 75 | + # answer_type = "message" |
| 76 | + # else: |
| 77 | + # logging.warning(f"Unrecognized output type: {output.type}") |
| 78 | + # continue |
| 79 | + # return { |
| 80 | + # "think": reasoning, |
| 81 | + # "action": res, |
| 82 | + # "last_computer_call_id": last_computer_call_id, |
| 83 | + # "last_response_id": response.id, |
| 84 | + # "outputs": outputs, |
| 85 | + # "answer_type": answer_type, |
| 86 | + # } |
| 87 | + |
| 88 | + |
| 89 | +def response_to_text(action): |
| 90 | + """ |
| 91 | + Given a computer action (e.g., click, double_click, scroll, etc.), |
| 92 | + convert it to a text description. |
| 93 | + """ |
| 94 | + action_type = action.type |
| 95 | + |
| 96 | + try: |
| 97 | + match action_type: |
| 98 | + |
| 99 | + case "click": |
| 100 | + x, y = action.x, action.y |
| 101 | + button = action.button |
| 102 | + print(f"Action: click at ({x}, {y}) with button '{button}'") |
| 103 | + # Not handling things like middle click, etc. |
| 104 | + if button != "left" and button != "right": |
| 105 | + button = "left" |
| 106 | + return f"mouse_click({x}, {y}, button='{button}')" |
| 107 | + |
| 108 | + case "scroll": |
| 109 | + x, y = action.x, action.y |
| 110 | + scroll_x, scroll_y = action.scroll_x, action.scroll_y |
| 111 | + print( |
| 112 | + f"Action: scroll at ({x}, {y}) with offsets (scroll_x={scroll_x}, scroll_y={scroll_y})" |
| 113 | + ) |
| 114 | + return f"mouse_move({x}, {y})\nscroll({scroll_x}, {scroll_y})" |
| 115 | + |
| 116 | + case "keypress": |
| 117 | + keys = action.keys |
| 118 | + for k in keys: |
| 119 | + print(f"Action: keypress '{k}'") |
| 120 | + # A simple mapping for common keys; expand as needed. |
| 121 | + if k.lower() == "enter": |
| 122 | + return "keyboard_press('Enter')" |
| 123 | + elif k.lower() == "space": |
| 124 | + return "keyboard_press(' ')" |
| 125 | + else: |
| 126 | + return f"keyboard_press('{k}')" |
| 127 | + |
| 128 | + case "type": |
| 129 | + text = action.text |
| 130 | + print(f"Action: type text: {text}") |
| 131 | + return f"keyboard_type('{text}')" |
| 132 | + |
| 133 | + case "wait": |
| 134 | + print(f"Action: wait") |
| 135 | + return "noop()" |
| 136 | + |
| 137 | + case "screenshot": |
| 138 | + # Nothing to do as screenshot is taken at each turn |
| 139 | + print(f"Action: screenshot") |
| 140 | + |
| 141 | + # Handle other actions here |
| 142 | + |
| 143 | + case "drag": |
| 144 | + x1, y1 = action.path[0].x, action.path[0].y |
| 145 | + x2, y2 = action.path[1].x, action.path[1].y |
| 146 | + print(f"Action: drag from ({x1}, {y1}) to ({x2}, {y2})") |
| 147 | + return f"mouse_drag_and_drop({x1}, {y1}, {x2}, {y2})" |
| 148 | + |
| 149 | + case _: |
| 150 | + print(f"Unrecognized action: {action}") |
| 151 | + |
| 152 | + except Exception as e: |
| 153 | + print(f"Error handling action {action}: {e}") |
| 154 | + |
| 155 | + |
| 156 | +@dataclass |
| 157 | +class OpenAIResponseModelArgs(BaseModelArgs): |
| 158 | + """Serializable object for instantiating a generic chat model with an OpenAI |
| 159 | + model.""" |
| 160 | + |
| 161 | + def make_model(self, extra_kwargs=None): |
| 162 | + return OpenAIResponseModel( |
| 163 | + model_name=self.model_name, |
| 164 | + temperature=self.temperature, |
| 165 | + max_tokens=self.max_new_tokens, |
| 166 | + extra_kwargs=extra_kwargs, |
| 167 | + ) |
0 commit comments