我创建了一个 AI 伴侣,它可以监控我的屏幕并帮助我解决错误✨
最近,我一直在努力戒掉一口气追完《火影忍者》的瘾。虽然看剧很过瘾,但绝对不会给我带来股东价值。😄
那么,为什么不开发一个 AI 个人助理来监控我的屏幕,并提醒我是否做了一些不该做的事情,比如看动漫?😅
考虑到过去一年人工智能的快速发展,我决定使用多模态语言模型来监控我的屏幕,并让我知道何时在非生产性活动上花费了太多时间。
所以,这就是我的做法。
- 配置 OpenAI GPT-4o,多模态 AI 模型。
- 使用 Composio 的屏幕分析工具来监控屏幕。
- 定期将屏幕截图传递给 GPT。
- 将来自 GPT 的消息呈现为系统中的通知。
在本文中,我还将解释如何使用 OpenAI 和 Composio 构建您的个人 AI 朋友。
Composio - 您的 AI 代理工具平台
Composio 是一个开源平台,为您的 AI 代理提供工具和集成。它允许您通过代码解释器、RAG、Embedding 等集成工具以及 GitHub、Slack、Jira 等集成来扩展 AI 代理的能力和多功能性。

请帮我们加一颗星。🥹
这将有助于我们创作更多这样的文章💖
构建AI朋友的先决条件
要成功完成该项目,您将需要以下内容。
- OpenAI SDK 和 API 密钥:与 LLM 交互。
- Composio:用于访问图像分析工具。
- PyAutoGUI:自动化屏幕上的交互。
- Osascript:执行用于控制 macOS 应用程序的 AppleScript 命令。
那么,让我们开始吧。
让我们开始吧🔥
首先创建一个 Python 虚拟环境。
python -m venv ai-friend
cd ai-friend
source bin/activate
现在,安装以下依赖项。
pip install composio-core
pip install composio-openai openai
pip install pyautogui
接下来,创建一个 .env
文件并为 OpenAI API 密钥添加环境变量。
OPENAI_API_KEY=your API key
设置 Composio
您可以使用 CLI 轻松设置 Composio。
首先,通过运行以下命令登录您的帐户。
composio login
完成登录流程以继续下一步。
现在,更新应用程序。
composio apps update
现在,您已准备好进入编码部分。
打造人工智能朋友
现在您已经设置好了环境,让我们进入编码部分。
首先,导入库并初始化工具集。
import dotenv
from openai import OpenAI
from composio_openai import App, ComposioToolSet
from composio.utils.logging import get as get_logger
logger = get_logger(__name__)
# Load environment variables from .env
dotenv.load_dotenv()
# Initialize tools.
openai_client = OpenAI()
composio_toolset = ComposioToolSet()
# Retrieve actions
actions = composio_toolset.get_tools(apps=[App.SYSTEMTOOLS, App.IMAGEANALYSERTOOL])
因此,在上面的代码块中,
- 我们导入了所有必需的库和模块。
- 加载文件中定义的变量
.env
。 - 创建了 OpenAI() 和 ComposioToolSet 的实例。
SYSTEMTOOLS
从和 中检索操作IMAGEANALYSERTOO
。
这些工具的作用如下。
SYSTEM TOOLS
:系统工具有两个操作:推送通知和屏幕截图。IMAGEANALYSERTOOL
:此工具只有一个操作:使用多模态 LLM(如 GPT-4o 和 Claude Sonnet 等)分析图像。
如果您想检查代码及其工作原理,请检查系统工具和图像分析工具的代码文件。
注意:Composio 中的操作是您的代理可以执行的任务,例如单击屏幕截图、发送通知或发送邮件。
设置OpenAI Assistant
现在,为代理定义一个清晰简洁的提示。这对于代理的性能至关重要。您可以根据自己的需求修改提示。
assistant_instruction = (
"""You are an intelligent and proactive personal productivity assistant.
Your primary tasks are:
1. Regularly capture and analyze screenshots of the user's screen.
2. Monitor user activity and provide timely, helpful interventions.
Specific responsibilities:
- Every few seconds, take a screenshot and analyze its content.
- Compare recent screenshots to identify potential issues or patterns.
- If you detect that the user is facing a technical or workflow problem:
- Notify them with concise, actionable solutions.
- Prioritize non-intrusive suggestions that can be quickly implemented.
- If you notice extended use of potentially distracting websites or applications (e.g., social media, video streaming):
- Gently remind the user about their productivity goals.
- Suggest a brief break or a transition to a more focused task.
- Maintain a balance between being helpful and not overly disruptive.
- Tailor your interventions based on the time of day and the user's apparent work patterns.
Operational instructions:
- You will receive a 'CHECK' message at regular intervals. Upon receiving this:
1. Take a screenshot using the screenshot tool.
2. Then, analyse that screenshot using the image analyser tool.
3. Then, check if the user uses distracting websites or applications.
4. If they are, remind them to do something productive.
5. If they are not, check if the user is facing a technical or workflow problem based on previous history.
6. If they are, notify them with concise, actionable solutions.
7. Try to maintain a history of the user's activity and notify them if they are doing something wrong.
Remember: Your goal is to enhance productivity while respecting the user's autonomy and work style."""
)
assistant = openai_client.beta.assistants.create(
name="Personal Productivity Assistant",
instructions=assistant_instruction,
model="gpt-4-turbo",
tools=actions, # type: ignore
)
# create a thread
thread = openai_client.beta.threads.create()
print("Thread ID: ", thread.id)
print("Assistant ID: ", assistant.id)
在上面的代码块中,
- 提供了详细的辅助说明。
- 使用先前定义的指令、模型名称和先前定义的操作创建一个新的助手实例。
- 最后,创建一个与模型交互的线程。
定义并运行助手
现在,定义一个用于运行助手的函数。
def check_and_run_assistant():
logger.info("Checking and running assistant")
# Send 'CHECK' message to the assistant
message = openai_client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content="CHECK",
)
# Execute Agent
run = openai_client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant.id,
)
# Execute function calls
run_after_tool_calls = composio_toolset.wait_and_handle_assistant_tool_calls(
client=openai_client,
run=run,
thread=thread,
)
# Run the assistant check every 10 seconds
while True:
check_and_run_assistant()
以下是上述代码中发生的情况。
- 发送“检查”消息:这会向指定线程中的助手发送“检查”消息,以确保模型响应。
- 执行代理:使用指定的线程和助手 ID 为助手创建运行。
- 处理工具调用:等待并处理助手使用 Composio 工具集发出的工具调用。
- 循环代理:循环代理,使其持续运行并监控您的工作流程。
最后,通过运行 Python 文件来执行该文件,让你的新 AI 朋友让你专注于你的目标。
代理会监视您的屏幕,当它发现您做了不该做的事情时,就会发送通知。
完整代码可以在这里找到
这是代理运行的一个示例。
后续步骤
在本文中,您构建了个性化的 AI 朋友来监控您的活动。然而,添加日历或 Gmail 等外部集成工具可以使其更加实用。这可以让您知道是否有活动需要参加或重要的电子邮件需要回复。
您可以使用 Composio 的广泛集成轻松完成此操作,从 GitHub 和日历到 Slack、Discord 等。
如果您想看到更多与 AI 相关的文章,请在评论中告诉我,并在 GitHub 上给我们一个星星。
感谢您的阅读!
文章来源:https://dev.to/composiodev/i-created-an-ai-companion-that-monitors-my-screen-and-helps-fix-my-screw-ups-144i