通过纯文字引导DeepSeek编写一个简单的聊天机器人~

为进一步验证DeepSeek的代码能力 和 自然语言理解力,我花费了大约1个半小时的时间,和DeepSeek仅通过文字对话,编写出一个简单的聊天机器人。

以下是最终运行效果、生成代码、引导沟通过程示例:

一、最终运行效果

二、全部代码:

import re
from datetime import datetime


def calculate(expression):
    try:
        if re.match(r'^[\d\+\-\*/\.\s]+$', expression):
            result = eval(expression)
            return f"结果是: {result}"
        else:
            return "抱歉,我只能计算包含数字和加减乘除的表达式。"
    except Exception as e:
        return "抱歉,计算时出错了。请检查你的输入。"


def extract_expression(text):
    match = re.search(r'[\d\.]+[\s]*[\+\-\*/][\s]*[\d\.]+([\s]*[\+\-\*/][\s]*[\d\.]+)*', text)
    return match.group(0).strip() if match else None


def get_time():
    return datetime.now().strftime('%H:%M:%S')


def get_developer_info(question):
    # 开发者信息
    developers = {
        "deepseek": "主要负责编码工作",
        "happybasic": "负责测试和反馈问题",
    }

    # 根据问题生成回复
    if "谁创造" in question or "谁开发" in question:
        return f"我是由 {', '.join(developers.keys())} 联合开发的,其中 deepseek {developers['deepseek']},而 happybasic {developers['happybasic']}。我们共同创造了一个简单的机器人模型。"
    elif "deepseek" in question:
        return f"deepseek {developers['deepseek']}。"
    elif "happybasic" in question:
        return f"happybasic {developers['happybasic']}。"
    else:
        return "抱歉,我不太明白你想了解哪位开发者的信息。"


def simple_chatbot():
    print(
        "你好!我是简单的聊天机器人。你可以和我聊聊天气、时间,或者让我帮你计算100以内的加减乘除混合运算。输入'退出'来结束对话。")

    # 关键词与回复的映射
    responses = {
        # 问候
        "你好": "你好!",
        "嗨": "你好!",

        # 天气
        "天气": "今天的天气很好,阳光明媚。",

        # 时间
        "时间": f"现在的时间是 {get_time()}。",
        "几点": f"现在的时间是 {get_time()}。",

        # 自我介绍
        "你叫什么名字": "我是一个简单的聊天机器人。",

        # 表扬和感谢
        "真棒": "谢谢你的夸奖!我很高兴能帮到你。",
        "厉害": "谢谢你的夸奖!我很高兴能帮到你。",
        "谢谢": "不客气!这是我的荣幸。",
        "感谢": "不客气!这是我的荣幸。",
        "聪明": "谢谢!我会继续努力的!",
    }

    last_intent = None  # 上一次的意图(如“计算”)

    while True:
        user_input = input("你: ").strip().lower()

        # 检查是否包含“退出”关键词
        if "退出" in user_input:
            print("机器人: 再见!")
            break

        # 处理开发者信息
        if "谁创造" in user_input or "谁开发" in user_input or "deepseek" in user_input or "happybasic" in user_input:
            print(f"机器人: {get_developer_info(user_input)}")
            last_intent = None
            continue

        # 处理表扬和感谢
        matched = False
        for keyword, response in responses.items():
            if keyword in user_input:
                print(f"机器人: {response}")
                last_intent = None
                matched = True
                break

        if not matched:
            # 处理计算请求
            is_calculation = any(word in user_input for word in ["计算", "算", "等于", "几"]) or last_intent == "计算"
            if is_calculation:
                expression = extract_expression(user_input)
                if expression:
                    print(f"机器人: {calculate(expression)}")
                    last_intent = "计算"
                else:
                    print("机器人: 抱歉,我没有找到可以计算的表达式。")
            else:
                print("机器人: 抱歉,我不太明白你的意思。")
                last_intent = None


# 运行聊天机器人
simple_chatbot()

三、 引导沟通过程示例

1. 首次开发需求沟通

2. 功能需求追加:

 

3. 测试bug反馈:

3.1 语言理解类:

3.2 计算BUG类:

* 注意: 实际测试过程中的bug远不止此,但是我均仅采用描述性语言将测试结果反馈给DeepSeek,它均能正确理解并有效修正。

 4.提出优化要求:

DeepSeek果然get到了重点,并且准确给出了优化策略。

5. 经过和DeepSeek沟通,DeepSeek同意将上述设计结果分享给优快云网友,DeepSeek果然大方!~~~ 哈哈~~~

最后,说一句:DeepSeek真的很棒 ~~~ 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

happybasic

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值