|
16 | 16 |
|
17 | 17 | from openadapt.config import config |
18 | 18 | from openadapt.custom_logger import logger |
| 19 | +from openadapt.drivers import anthropic |
19 | 20 | from openadapt.db import db |
20 | 21 | from openadapt.privacy.base import ScrubbingProvider, TextScrubbingMixin |
21 | 22 | from openadapt.privacy.providers import ScrubProvider |
@@ -544,6 +545,56 @@ def next_event(self) -> Union["ActionEvent", None]: |
544 | 545 |
|
545 | 546 | return None |
546 | 547 |
|
| 548 | + def prompt_for_description(self) -> str: |
| 549 | + """Use the Anthropic API to describe what is happening in the action event. |
| 550 | +
|
| 551 | + Returns: |
| 552 | + str: The description of the action event. |
| 553 | + """ |
| 554 | + # Collect the relevant information for the prompt |
| 555 | + prompt_details = { |
| 556 | + "name": self.name, |
| 557 | + "mouse_x": self.mouse_x, |
| 558 | + "mouse_y": self.mouse_y, |
| 559 | + "mouse_dx": self.mouse_dx, |
| 560 | + "mouse_dy": self.mouse_dy, |
| 561 | + "text": self.text, |
| 562 | + } |
| 563 | + |
| 564 | + # Convert the details to a readable format for the prompt |
| 565 | + action_text = "\n".join( |
| 566 | + [ |
| 567 | + f"{key}: {value}" |
| 568 | + for key, value in prompt_details.items() if value is not None |
| 569 | + ] |
| 570 | + ) |
| 571 | + prompt_text = f"Action:\n{action_text}" |
| 572 | + |
| 573 | + # Add contextual instructions for the prompt |
| 574 | + system_prompt = ( |
| 575 | + "You are an assistant tasked with describing user interactions in a graphical interface. " |
| 576 | + "Based on the following Action details and the provided screenshot, describe the action taking place " |
| 577 | + "as clearly as possible. Do not describe the screenshot in its entirety, only what is relevant for describing the action." |
| 578 | + ) |
| 579 | + |
| 580 | + logger.info(f"system_prompt=\n{system_prompt}") |
| 581 | + logger.info(f"prompt_text=\n{prompt_text}") |
| 582 | + |
| 583 | + # Call the Anthropic API |
| 584 | + try: |
| 585 | + description = anthropic.prompt( |
| 586 | + prompt=prompt_text, |
| 587 | + system_prompt=system_prompt, |
| 588 | + images=[self.screenshot.image], |
| 589 | + max_tokens=256, # Adjust token limit as necessary |
| 590 | + ) |
| 591 | + except Exception as e: |
| 592 | + logger.exception(f"Error while prompting for description: {e}") |
| 593 | + description = "An error occurred while generating the description." |
| 594 | + |
| 595 | + return description |
| 596 | + |
| 597 | + |
547 | 598 |
|
548 | 599 | class WindowEvent(db.Base): |
549 | 600 | """Class representing a window event in the database.""" |
|
0 commit comments