为进一步验证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真的很棒 ~~~