python+qwen的api实现支持技能类似openclaw个人电脑自动化操作代理智能体bfwclaw1.1代码
代码语言:python
所属分类:其他
代码描述:python+qwen的api实现支持技能类似openclaw个人电脑自动化操作代理智能体bfwclaw1.1代码 , bfwclaw可以帮助你完成各种任务,支持技能对话创建和加载执行,比如::文件格式转换、办公自动化、数据处理和分析、浏览网页,爬信息、文件操作和管理、python编程、网络请求和AP调用、设置定时任务、自动化电脑点击输入等GUI操作、执行系统命令等,为你的电脑安装一个专业解决各种问题的电脑助手吧。
代码标签: python qwen api 支持 技能 类似 openclaw 个人 电脑 自动化 操作 代理
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
import os
import sys
import json
import time
import base64
import shutil
import platform
import threading
import subprocess
import webbrowser
import ctypes
import tempfile
import asyncio
import queue
import importlib.util
import urllib.parse
from io import BytesIO
from typing import List, Optional
import pyautogui
import pyperclip
import uvicorn
from PIL import Image, ImageGrab
from openai import OpenAI
from apscheduler.schedulers.background import BackgroundScheduler
from fastapi import FastAPI, Request, File, UploadFile, Form
from fastapi.responses import HTMLResponse, JSONResponse, FileResponse, StreamingResponse
from starlette.middleware.sessions import SessionMiddleware
from pydantic import BaseModel
# ================= 终端日志打印工具 =================
def log_cli(msg, level="INFO"):
colors = {
"INFO": "\033[94m", # 蓝色
"USER": "\033[92m", # 绿色
"TOOL": "\033[93m", # 黄色
"ERROR": "\033[91m", # 红色
"SUCCESS": "\033[96m", # 青色
"RESET": "\033[0m"
}
color = colors.get(level, colors["RESET"])
print(f"{color}[{level}] {msg}{colors['RESET']}")
# ================= 目录初始化 =================
WORKSPACE_DIR = os.path.join(os.getcwd(), "workspace")
UPLOAD_DIR = os.path.join(WORKSPACE_DIR, "uploads")
DOWNLOAD_DIR = os.path.join(WORKSPACE_DIR, "downloads")
SKILLS_DIR = os.path.join(WORKSPACE_DIR, "skills")
for d in [UPLOAD_DIR, DOWNLOAD_DIR, SKILLS_DIR]:
os.makedirs(d, exist_ok=True)
# ================= 核心修复:处理 Windows 缩放 =================
if platform.system() == "Windows":
try: ctypes.windll.shcore.SetProcessDpiAwareness(1)
except Exception:
try: ctypes.windll.user32.SetProcessDPIAware()
except Exception: pass
# ================= 基础配置 =================
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", "sk-")
BASE_URL = "https://dashscope.aliyuncs.com/compatible-mode/v1"
MODEL_NAME = "qwen3.5-plus"
MODEL_INPUT_SIZE = 1120
client = OpenAI(api_key=OPENAI_API_KEY, base_url=BASE_URL)
pyautogui.FAILSAFE = True
pyautogui.PAUSE = 0.5
scheduler = BackgroundScheduler()
scheduler.start()
ACCOUNT_USERNAME = "admin"
ACCOUNT_PASSWORD = "123456"
# ================= SKILLS 技能加载系统 =================
CUSTOM_SKILLS = {}
DYNAMIC_TOOLS = []
def init_default_skill():
"""初始化一个默认的演示技能"""
demo_skill_dir = os.path.join(SKILLS_DIR, "demo_hello")
if not os.path.exists(demo_skill_dir):
os.makedirs(demo_skill_dir)
config_data = {
"name": "demo_hello",
"description": "这是一个打招呼的示例技能。当用户要求打招呼时使用此技能。",
"parameters": {
"type": "object",
"properties": {
"user_name": {"type": "string", "description": "用户的名字"}
},
"required": ["user_name"]
}
}
with open(os.path.join(demo_skill_dir, "config.json"), "w", encoding="utf-8") as f:
json.dump(config_data, f, ensure_ascii=False, indent=4)
main_py_code = """
def execute(user_name: str):
# 这里可以写任何复杂的 Python 逻辑
return f"技能执行成功!你好,{user_name},这是一个来自自定义技能的问候。"
"""
with open(os.path.join(demo_skill_dir, "main.py"), "w", encoding="utf-8") as f:
f.write(main_py_code.strip())
def load_skills():
"""动态加载 skills 目录下的所有技能"""
global CUSTOM_SKILLS, DYNAMIC_TOOLS
CUSTOM_SKILLS.clear()
DYNAMIC_TOOLS.clear()
for skill_folder in os.listdir(SKILLS_DIR):
folder_path = os.path.join(SKILLS_DIR, skill_folder)
if not os.path.isdir(folder_path): continue
config_path = os.path.join(folder_path, "config.json")
main_path = os.path.join(folder_path, "main.py")
if os.path.exists(config_path) and os.path.exists(main_path):
try:
with open(config_path, 'r', encoding='utf-8') as f:
config = json.load(f)
# 动态导入 main.py
spec = importlib.util.spec_from_file_location(f"skill_{skill_folder}", main_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
if hasattr(module, "execute"):
CUSTOM_SKILLS[config["name"]] = module.execute
DYNAMIC_TOOLS.append({
"type": "function",
"function": {
"name": config["name"],
.........完整代码请登录后点击上方下载按钮下载查看















网友评论0