2025年3月24日 星期一 甲辰(龙)年 月廿三 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > Python

AI自动对话聊天程序代码

时间:03-29来源:作者:点击数:66

基于python实现的AI自动对话聊天机器人,回复“再见”可退出聊天

  • # -*- coding:utf-8 -*-
  • # 向服务器发送请求
  • import urllib.request
  • # 对中文进行ASCII字符的转换
  • import urllib.parse
  • # 可以将字符串转换成字典类型
  • import json
  • while 1:
  • # 从键盘输入
  • myStr = input("跟AI说句话吧:")
  • # 对输入的字转码
  • text = urllib.parse.quote(myStr)
  • # 将转码的内容拼接到链接上
  • url = '{}={}'.format('http://api.qingyunke.com/api.php?key=free&appid=0&msg', text)
  • # 向服务器发送请求
  • response = urllib.request.urlopen(url)
  • # 读取响应数据并转换成utf-8编码
  • responses = response.read().decode("utf-8")
  • # 转换成字典类型
  • responseText = json.loads(responses)
  • # 从字典类型取出键为content的值并打印输出
  • print(responseText["content"])
  • if myStr == "再见":
  • break

 

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门