- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章AI人工智能 Python实现人机对话由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
在人工智能进展的如火如荼的今天,我们如果不尝试去接触新鲜事物,马上就要被世界淘汰啦~ 。
本文拟使用Python开发语言实现类似于WIndows平台的“小娜”,或者是IOS下的“Siri”。最终达到人机对话的效果.
【实现功能】 。
这篇文章将要介绍的主要内容如下:
1、搭建人工智能--人机对话服务端平台 2、实现调用服务端平台进行人机对话交互 。
【实现思路】 。
AIML 。
AIML由Richard Wallace发明。他设计了一个名为 A.L.I.C.E. (Artificial Linguistics Internet Computer Entity 人工语言网计算机实体) 的机器人,并获得了多项人工智能大奖。有趣的是,图灵测试的其中一项就在寻找这样的人工智能:人与机器人通过文本界面展开数分钟的交流,以此查看机器人是否会被当作人类.
本文就使用了Python语言调用AIML库进行智能机器人的开发.
本系统的运作方式是使用Python搭建服务端后台接口,供各平台可以直接调用。然后客户端进行对智能对话api接口的调用,服务端分析参数数据,进行语句的分析,最终返回应答结果.
当前系统前端使用HTML进行简单地聊天室的设计与编写,使用异步请求的方式渲染数据.
【开发及部署环境】 。
开发环境:Windows 7 ×64 英文版 。
JetBrains PyCharm 2017.1.3 x64 。
测试环境:Windows 7 ×64 英文版 。
【所需技术】 。
1、Python语言的熟练掌握,Python版本2.7 2、Python服务端开发框架tornado的使用 3、aiml库接口的简单使用 4、HTML+CSS+Javascript(jquery)的熟练使用 5、Ajax技术的掌握 。
【实现过程】 。
1、安装Python aiml库 。
1
|
pip install aiml
|
2、获取alice资源 。
Python aiml安装完成后在Python安装目录下的 Lib/site-packages/aiml下会有alice子目录,将此目录复制到工作区。 或者在Google code上下载alice brain: aiml-en-us-foundation-alice.v1-9.zip 。
3、Python下加载alice 。
取得alice资源之后就可以直接利用Python aiml库加载alice brain了:
1
2
3
4
5
|
import
aiml
os.chdir(
'./src/alice'
)
# 将工作区目录切换到刚才复制的alice文件夹
alice
=
aiml.Kernel()
alice.learn(
"startup.xml"
)
alice.respond(
'LOAD ALICE'
)
|
注意加载时需要切换工作目录到alice(刚才复制的文件夹)下.
4、 与alice聊天 。
加载之后就可以与alice聊天了,每次只需要调用respond接口:
1
|
alice.respond(
'hello'
)
#这里的hello即为发给机器人的信息
|
5. 用Tornado搭建聊天机器人网站 。
Tornado可以很方便地搭建一个web网站的服务端,并且接口风格是Rest风格,可以很方便搭建一个通用的服务端接口.
这里写两个方法:
get:渲染界面 。
post:获取请求参数,并分析,返回聊天结果 。
Class类的代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
class
ChatHandler(tornado.web.RequestHandler):
def
get(
self
):
self
.render(
'chat.html'
)
def
post(
self
):
try
:
message
=
self
.get_argument(
'msg'
,
None
)
print
(
str
(message))
result
=
{
'is_success'
:
True
,
'message'
:
str
(alice.respond(message))
}
print
(
str
(result))
respon_json
=
tornado.escape.json_encode(result)
self
.write(respon_json)
except
Exception, ex:
repr
(ex)
print
(
str
(ex))
result
=
{
'is_success'
:
False
,
'message'
: ''
}
self
.write(
str
(result))
|
6. 简单搭建一个聊天界面 。
该界面是基于BootStrap的,我们简单搭建这么一个聊天的界面用于展示我们的接口结果。同时进行简单的聊天.
7. 接口调用 。
我们异步请求服务端接口,并将结果渲染到界面 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
$.ajax({
type
:
'post'
,
url: AppDomain
+
'chat'
,
async: true,
/
/
异步
dataType:
'json'
,
data: (
{
"msg"
:request_txt
}),
success: function (data)
{
console.log(JSON.stringify(data));
if
(data.is_success
=
=
true) {
setView(resUser,data.message);
}
},
error: function (data)
{
console.log(JSON.stringify(data));
}
});
/
/
end Ajax
|
这里我附上系统的完整目录结构以及完整代码-> 。
8、目录结构 。
9、Python服务端代码 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import
os.path
import
tornado.auth
import
tornado.escape
import
tornado.httpserver
import
tornado.ioloop
import
tornado.options
import
tornado.web
from
tornado.options
import
define, options
import
os
import
aiml
os.chdir(
'./src/alice'
)
alice
=
aiml.Kernel()
alice.learn(
"startup.xml"
)
alice.respond(
'LOAD ALICE'
)
define(
'port'
, default
=
3999
,
help
=
'run on the given port'
,
type
=
int
)
class
Application(tornado.web.Application):
def
__init__(
self
):
handlers
=
[
(r
'/'
, MainHandler),
(r
'/chat'
, ChatHandler),
]
settings
=
dict
(
template_path
=
os.path.join(os.path.dirname(__file__),
'templates'
),
static_path
=
os.path.join(os.path.dirname(__file__),
'static'
),
debug
=
True
,
)
# conn = pymongo.Connection('localhost', 12345)
# self.db = conn['demo']
tornado.web.Application.__init__(
self
, handlers,
*
*
settings)
class
MainHandler(tornado.web.RequestHandler):
def
get(
self
):
self
.render(
'index.html'
)
def
post(
self
):
result
=
{
'is_success'
:
True
,
'message'
:
'123'
}
respon_json
=
tornado.escape.json_encode(result)
self
.write(
str
(respon_json))
def
put(
self
):
respon_json
=
tornado.escape.json_encode(
"{'name':'qixiao','age':123}"
)
self
.write(respon_json)
class
ChatHandler(tornado.web.RequestHandler):
def
get(
self
):
self
.render(
'chat.html'
)
def
post(
self
):
try
:
message
=
self
.get_argument(
'msg'
,
None
)
print
(
str
(message))
result
=
{
'is_success'
:
True
,
'message'
:
str
(alice.respond(message))
}
print
(
str
(result))
respon_json
=
tornado.escape.json_encode(result)
self
.write(respon_json)
except
Exception, ex:
repr
(ex)
print
(
str
(ex))
result
=
{
'is_success'
:
False
,
'message'
: ''
}
self
.write(
str
(result))
def
main():
tornado.options.parse_command_line()
http_server
=
tornado.httpserver.HTTPServer(Application())
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
if
__name__
=
=
'__main__'
:
print
(
'HTTP server starting ...'
)
main()
|
9、Html前端代码 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
<!DOCTYPE html>
<
html
>
<
head
>
<
link
rel
=
"icon"
href
=
"qixiao.ico"
type
=
"image/x-icon"
/>
<
title
>qixiao tools</
title
>
<
link
rel
=
"stylesheet"
type
=
"text/css"
href
=
"../static/css/bootstrap.min.css"
>
<
script
type
=
"text/javascript"
src
=
"../static/js/jquery-3.2.0.min.js"
></
script
>
<
script
type
=
"text/javascript"
src
=
"../static/js/bootstrap.min.js"
></
script
>
<
style
type
=
"text/css"
>
.top-margin-20{
margin-top: 20px;
}
#result_table,#result_table thead th{
text-align: center;
}
#result_table .td-width-40{
width: 40%;
}
</
style
>
<
script
type
=
"text/javascript"
>
</
script
>
<
script
type
=
"text/javascript"
>
var AppDomain = 'http://localhost:3999/'
$(document).ready(function(){
$("#btn_sub").click(function(){
var user = 'qixiao(10011)';
var resUser = 'alice (3333)';
var request_txt = $("#txt_sub").val();
setView(user,request_txt);
$.ajax({
type: 'post',
url: AppDomain+'chat',
async: true,//异步
dataType: 'json',
data: (
{
"msg":request_txt
}),
success: function (data)
{
console.log(JSON.stringify(data));
if (data.is_success == true) {
setView(resUser,data.message);
}
},
error: function (data)
{
console.log(JSON.stringify(data));
}
});//end Ajax
});
});
function setView(user,text)
{
var subTxt = user + " "+new Date().toLocaleTimeString() +'\n·'+ text;
$("#txt_view").val($("#txt_view").val()+'\n\n'+subTxt);
var scrollTop = $("#txt_view")[0].scrollHeight;
$("#txt_view").scrollTop(scrollTop);
}
</
script
>
</
head
>
<
body
class
=
"container"
>
<
header
class
=
"row"
>
<
header
class
=
"row"
>
<
a
href
=
"/"
class
=
"col-md-2"
style
=
"font-family: SimHei;font-size: 20px;text-align:center;margin-top: 30px;"
>
<
span
class
=
"glyphicon glyphicon-home"
></
span
>Home
</
a
>
<
font
class
=
"col-md-4 col-md-offset-2"
style
=
"font-family: SimHei;font-size: 30px;text-align:center;margin-top: 30px;"
>
<
a
href
=
"/tools"
style
=
"cursor: pointer;"
>QiXiao - Chat</
a
>
</
font
>
</
header
>
<
hr
>
<
article
class
=
"row"
>
<
section
class
=
"col-md-10 col-md-offset-1"
style
=
"border:border:solid #4B5288 1px;padding:0"
>Admin : QiXiao </
section
>
<
section
class
=
"col-md-10 col-md-offset-1 row"
style
=
"border:solid #4B5288 1px;padding:0"
>
<
section
class
=
"col-md-9"
style
=
"height: 400px;"
>
<
section
class
=
"row"
style
=
"height: 270px;"
>
<
textarea
class
=
"form-control"
style
=
"width:100%;height: 100%;resize: none;overflow-x: none;overflow-y: scroll;"
readonly
=
"true"
id
=
"txt_view"
></
textarea
>
</
section
>
<
section
class
=
"row"
style
=
"height: 130px;border-top:solid #4B5288 1px; "
>
<
textarea
class
=
"form-control"
style
=
"overflow-y: scroll;overflow-x: none;resize: none;width: 100%;height:70%;border: #fff"
id
=
"txt_sub"
></
textarea
>
<
button
class
=
"btn btn-primary"
style
=
"float: right;margin: 0 5px 0 0"
id
=
"btn_sub"
>Submit</
button
>
</
section
>
</
section
>
<
section
class
=
"col-md-3"
style
=
"height: 400px;border-left: solid #4B5288 1px;"
></
section
>
</
section
>
</
article
>
</
body
>
</
html
>
|
【系统测试】 。
1、首先我们将我们的服务运行起来 。
2、调用测试 。
然后我们进行前台界面的调用 。
这里我们可以看到,我们的项目完美运行,并且达到预期效果.
【可能遇到问题】 。
中文乱码 。
【系统展望】 。
经过测试,中文目前不能进行对话,只能使用英文进行对话操作,有待改善.
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.
原文链接:https://www.cnblogs.com/qixiaoyizhan/p/7246387.html 。
最后此篇关于AI人工智能 Python实现人机对话的文章就讲到这里了,如果你想了解更多关于AI人工智能 Python实现人机对话的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
这一年来,AI领域层出不穷的技术和应用,真的有点让人疲于奔命。其中AIGC领域的diffusion model 和 NLP领域的 ChatGBT 引领了这一轮风潮。AI取代越来越多脑力工作者的趋势已
人工智能,有多能? "AI"二字,相信大家并不陌生。但是,全力发展人工智能,真的合适吗? 六年前,小米发布了小爱同学。这大概是离大家最近的人工智能
我正在使用 Flash 中的 Box2D 实现一个简单的 Volley 游戏。我需要为 CPU 播放器实现一些 AI。我们就叫他 jack 吧。因此,Jack 需要预测球被人类球员 John 击中后会
我正在尝试查找有关尝试创建可以理解英语单词、语法和上下文的基本 AI 工具的信息(希望是 C# 源代码)。 想法是通过使用尽可能多的书面文档来训练 AI,然后基于这些文档,让 AI 用对人类有意义的正
我正在编写一个个人项目,该项目旨在拥有一个可以进化的类或对象,我们称之为“机器人”。机器人需要能够在给定命令的情况下创建类、字段和函数,并能够将逻辑写入这些对象以进行操作(但通常都是这样做的)。 为了
我正在做一个项目,其中有一系列要拍卖的卡片;出价最高的人将赢得拍卖中值(value)最高的卡片。 当 AI 玩家轮到时,他需要评估数组并确定他的出价估值。 忽略与验证或规则有关的任何事情;因为我已经对
假设我有 20 名玩家 [姓名 A .. T] 参加锦标赛。锦标赛的规则规定每个玩家与其他玩家对战两次 [A 对 B、B 对 A、A 对 C .. 等等]。有 20 名玩家,总共有 380 场比赛。
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
我正在开发一个应该像异或运算符一样工作的程序。 为了调整权重,我使用反向传播。 我还包括了深度学习(它几乎按其应有的方式工作,这里同样的斗争)但这不应该是出于重要性。 (当有像 这样的 if 子句时
是否有围绕 AI 的标准规则引擎/算法来预测用户对特定类型产品(如衣服)的品味。我知道这是所有电子商务网站都会为之扼杀的一件事。但我正在寻找在那里定义的理论模式,这将有助于以更好的方式做出预测,即使不
所以这是一项大学作业,除了 AI 不知道如何“取胜”之外,一切都已经完成,而且它在开始时的 Action 并不像我希望的那样随机走到顶角,然后往下走。该任务只需要 AI 的两条规则,即它可以“获胜”或
我认为 Online-Depth-Search Algorithm 存在一些问题,因为我没有看到任何递归调用。 这是来自 peter Norvig 的代码。 如果正确或错误,请帮助我理解这一点。 fu
这是 C 语言中的玩家与 AI 井字棋游戏。如果 AI 滚到一个被占用的位置,我该如何让它再次滚到一个未被占用的位置? char boardchar[3][3] = { {'1', '2', '3'}
我正在尝试为我的 connect 4 java 程序设计一个 AI,需要有不同级别的难度,我已经为简单级别创建了一个算法,但现在我需要另一个针对困难级别的算法。 有人可以推荐或给我一些伪代码来实现人工
我一直在尝试一些框架和算法,但我找不到一个可以做我想做的事情——根据值对数据列进行分类。 我尝试使用贝叶斯算法,但它不是很精确,因为我不能期望正在搜索的数据在训练集中 - 但我可以期望模式在训练中。
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 5 年前。 Improve this ques
我有两个类,人类和怪物。 两者都有一个名为 MoveBehavior 的属性 Human有HumanMoveBehavior,Monster有MonsterMoveBehavior 我希望 Human
我在研究informed search算法,对于Iterative Deepening A* Search,我知道空间复杂度是O(d),其中d是最浅目标节点的深度。我试图找出它的时间复杂度是多少,但我
我是一名优秀的程序员,十分优秀!