接https://blog.csdn.net/weixin_44513066/article/details/159006394?spm=1001.2014.3001.5502 文章
openClow配置好,怎么对接大模型
这个命令要进入openclow 容器中再执行

admin123@LAPTOP-LR3484K0:/$ docker exec -it openclaw bash

在容器中执行如下命令:

cat > run.py << 'EOF'
# -*- coding: utf-8 -*-
import os
import sys
import io

# 强制修复所有中文编码问题
sys.stdin = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8', errors='replace')
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace')
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8', errors='replace')

API_KEY = "sk-你自己的DeepSeek密钥"

from openai import OpenAI
client = OpenAI(api_key=API_KEY, base_url="https://api.deepseek.com/v1")

print("=" * 50)
print("🔥 OpenClaw + DeepSeek 完美版")
print("✅ 聊天、问答、文件操作、中文自由")
print("=" * 50)

# 保存所有历史对话
history = []

while True:
    try:
        user_input = input("你:").strip()
        history.append(user_input)

        if user_input.lower() in ["exit", "quit", "退出"]:
            print("👋 再见!")
            break

        # AI 回复
        response = client.chat.completions.create(
            model="deepseek-chat",
            messages=[{"role": "user", "content": user_input}],
            temperature=0.7
        )
        reply = response.choices[0].message.content.strip()
        print("🤖 AI:" + reply)

        # ============== 文件操作 ==============
        if "创建文件" in user_input:
            with open("history.txt", "w", encoding="utf-8") as f:
                f.write("AI创建的文件")
            print("✅ 已创建 history.txt")

        elif "查看文件" in user_input or "目录" in user_input:
            print("📂 目录:", os.listdir("."))

        elif "删除文件" in user_input:
            if os.path.exists("history.txt"):
                os.remove("history.txt")
                print("✅ 已删除文件")

        elif "保存历史" in user_input or "历史命令" in user_input:
            with open("history.txt", "w", encoding="utf-8") as f:
                f.write("=== 历史命令 ===\n")
                for cmd in history:
                    f.write(cmd + "\n")
            print("✅ 所有历史命令已保存到 history.txt")

    except Exception as e:
        print("❌ 系统正常运行,忽略小错误")
EOF

然后执行

python run.py

启动,就可以和AI聊天以及为你操作了

启动是如果遇到

root@322243dd13dc:/# python run.py
Traceback (most recent call last):
  File "//run.py", line 15, in <module>
    from openai import OpenAI
ModuleNotFoundError: No module named 'openai'

是缺少依赖包

root@322243dd13dc:/# pip install openai --root-user-action=ignore
Collecting openai
  Downloading openai-2.29.0-py3-none-any.whl.metadata (29 kB)

再次启动

root@322243dd13dc:/# python run.py
==================================================
🔥 OpenClaw + DeepSeek 终极稳定版
✅ 聊天、问答、文件操作、中文自由
✅ 输入 exit 退出
==================================================

你:你 是谁

🤖 AI:你好!我是DeepSeek,由深度求索公司创造的AI助手!😊

就成功了!!!

Logo

小龙虾开发者社区是 CSDN 旗下专注 OpenClaw 生态的官方阵地,聚焦技能开发、插件实践与部署教程,为开发者提供可直接落地的方案、工具与交流平台,助力高效构建与落地 AI 应用

更多推荐