Open a0soft opened 5 years ago
折腾了好些时间,下面列出较完整的编译过程,以便能帮到遇到类似情形的开发者
00、安装Ubuntu下C/C++开发工具
01、下载dmkit
cd ./PATH_TO_DMKIT
git clone https://github.com/baidu/unit-dmkit.git
02、下载编译protobuf3.6.0,正确的版本很重要!!!否则可能出现链接错误
sudo apt-get install autoconf automake libtool curl make g++ unzip
git clone -b v3.6.0 https://github.com/protocolbuffers/protobuf.git
cd protobuf
git submodule update --init --recursive
cd protobuf
./autogen.sh ./configure --prefix=/usr -with-PACKAGE=yes sudo make
make check sudo make install
sudo ldconfig
protoc --version
03、下载编译brpc,正确的版本很重要!!!否则可能出现链接错误 参考"https://blog.csdn.net/breaksoftware/article/details/81564405" 修改deps.sh成这样子 cd brpc git checkout master
04、安装依赖的剩余部分
sh deps.sh none
mkdir _build && cd _build && cmake ..
BRPC_INCLUDE_PATH:PATH=/home/USER/PATH_TO_DMKIT/unit-dmkit/brpc/_build/output/include
make
这里提供一个基于bot_emulator.py修改后的文件bot_emulator4.py, 其需要4个参数 [bot_id] 机器人ID [client_id] 通过官网获取的AK [client_secret] 通过官网获取的SK [ip_and_port] DMKit服务提供者的IP和端口 调用示例 python bot_emulator4.py 73999 XXXXclient_idXXXX XXXXclient_secretXXXX http://a0ls-00000003:8010
#
#
#
#
""" A simple console program to emulate a bot for user to interact with
""" import json import os import random import requests import sys import urllib, urllib.request import ssl
def main(bot_id, client_id, client_secret, ip_and_port): """ Bot emulator Read user input and get bot response
"""
# 2019-08-12, added for get access_token by a0soft@163.com, BEG
# client_id 为官网获取的AK,client_secret 为官网获取的SK
host = ''.join(
['https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=',
client_id,
'&client_secret=',
client_secret])
print(host)
request = urllib.request.Request(host)
request.add_header('Content-Type', 'application/json; charset=UTF-8')
response = urllib.request.urlopen(request)
content = response.read()
if (content):
resp_json = json.loads(content)
token = resp_json["access_token"]
print(token)
# 2019-08-12, added for get access_token by a0soft@163.com, END
# Url of dmkit, change this if you are runnning dmkit in a different ip/port.
url = ''.join([ip_and_port, "/search?access_token=", token])
json_header = {'Content-Type': 'application/json'}
user_id = str(random.randint(1, 100000))
session = ""
while True:
print ("-----------------------------------------------------------------")
user_input = input("input:\n")
user_input = user_input.strip()
# 退出接口
if (user_input == "exitexit"):
break
if not user_input:
continue
logid = str(random.randint(100000,999999))
payload = {
"version": "2.0",
"bot_id": bot_id,
"log_id": logid,
"request": {
"user_id": user_id,
"query": user_input,
"query_info": {
"type": "TEXT",
"source": "ASR",
"asr_candidates": []
},
"updates": "",
"client_session": "{\"client_results\":\"\", \"candidate_options\":[]}",
"bernard_level": 1
},
"bot_session": session
}
obj = requests.post(url, data=json.dumps(payload), headers=json_header).json()
print ("BOT:")
# Not a valid response.
if obj['error_code'] != 0:
print ("ERROR: %s" % obj['error_msg'])
continue
session = obj['result']['bot_session']
response = obj['result']['response']
action = response['action_list'][0]
# For clarify or failed responses which dmkit does not take actions.
if action['type'] != 'event':
print (action['say'])
continue
custom_reply = json.loads(action['custom_reply'])
if custom_reply['event_name'] != 'DM_RESULT':
print ('ERROR:unknown event name %s' % custom_reply['event_name'])
continue
# This is a dmkit response
dm_result = json.loads(custom_reply['result'])
for item in dm_result['result']:
if item['type'] == 'tts':
print (item['value'])
if name == "main":
# print ("Usage: %s %s %s" % (sys.argv[0], "[bot_id]", "[access_token]"))
#else:
# main(sys.argv[1], sys.argv[2])
if len(sys.argv) < 4:
print ("Usage: %s %s %s %s %s" % (sys.argv[0], "[bot_id]", "[client_id]", "[client_secret]", "[ip_and_port]"))
else:
main(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])
Installing dependencies for Ubuntu... [sudo] password for uuu: Reading package lists... Done Building dependency tree Reading state information... Done coreutils is already the newest version (8.28-1ubuntu1). libgoogle-perftools-dev is already the newest version (2.5-2.2ubuntu3). libleveldb-dev is already the newest version (1.20-2). libprotobuf-dev is already the newest version (3.0.0-9.1ubuntu1). libprotoc-dev is already the newest version (3.0.0-9.1ubuntu1). libsnappy-dev is already the newest version (1.1.7-1). make is already the newest version (4.1-9.1ubuntu1). libgflags-dev is already the newest version (2.2.1-1). protobuf-compiler is already the newest version (3.0.0-9.1ubuntu1). g++ is already the newest version (4:7.4.0-1ubuntu2.3). git is already the newest version (1:2.17.1-1ubuntu0.4). libcurl4-openssl-dev is already the newest version (7.58.0-2ubuntu3.7). libssl-dev is already the newest version (1.1.1-1ubuntu2.1~18.04.4). 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Building brpc... -- The C compiler identification is GNU 7.4.0 -- The CXX compiler identification is GNU 7.4.0 -- Check for working C compiler: /usr/bin/cc -- Check for working C compiler: /usr/bin/cc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Detecting C compile features -- Detecting C compile features - done -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Detecting CXX compile features -- Detecting CXX compile features - done -- Found gflags: /usr/lib/x86_64-linux-gnu/libgflags.so -- Looking for pthread.h -- Looking for pthread.h - found -- Looking for pthread_create -- Looking for pthread_create - not found -- Looking for pthread_create in pthreads -- Looking for pthread_create in pthreads - not found -- Looking for pthread_create in pthread -- Looking for pthread_create in pthread - found -- Found Threads: TRUE -- Found Protobuf: /usr/lib/x86_64-linux-gnu/libprotobuf.so;-lpthread (found version "3.9.0") -- Found OpenSSL: /usr/lib/x86_64-linux-gnu/libcrypto.so (found version "1.1.1") -- Found Protobuf: /usr/lib/x86_64-linux-gnu/libprotobuf.so;-lpthread;-lpthread (found version "3.9.0") -- Configuring done -- Generating done -- Build files have been written to: /home/catching/work/dev/baidu/dmkit/unit-dmkit/brpc/_build
[ 71%] Building CXX object src/CMakeFiles/OBJ_LIB.dir/brpc/esp_message.cpp.o [ 71%] Building CXX object CMakeFiles/PROTO_LIB.dir/brpc/streaming_rpc_meta.pb.cc.o In file included from /home/catching/work/dev/baidu/dmkit/unit-dmkit/brpc/src/brpc/esp_message.cpp:18:0: /home/catching/work/dev/baidu/dmkit/unit-dmkit/brpc/src/brpc/esp_message.h:37:47: error: invalid use of incomplete type ‘class google::protobuf::Message’ class EspMessage : public ::google::protobuf::Message { ^
~~ In file included from /usr/include/google/protobuf/implicit_weak_message.h:37:0, from /usr/include/google/protobuf/generated_message_util.h:49, from /home/catching/work/dev/baidu/dmkit/unit-dmkit/brpc/src/brpc/esp_message.h:21, from /home/catching/work/dev/baidu/dmkit/unit-dmkit/brpc/src/brpc/esp_message.cpp:18: /usr/include/google/protobuf/arena.h:76:7: note: forward declaration of ‘class google::protobuf::Message’ class Message; // defined in message.h ^~~ In file included from /home/catching/work/dev/baidu/dmkit/unit-dmkit/brpc/src/brpc/esp_message.cpp:24:0: /usr/include/google/protobuf/wire_format_lite_inl.h:61:13: error: redefinition of ‘static bool google::protobuf::internal::WireFormatLite::ReadPrimitive(google::protobuf::io::CodedInputStream, CType) [with CType = int; google::protobuf::internal::WireFormatLite::FieldType DeclaredType = (google::protobuf::internal::WireFormatLite::FieldType)5]’ inline bool WireFormatLite::ReadPrimitive<int32, WireFormatLite::TYPE_INT32>( ^~~~~~ In file included from /usr/include/google/protobuf/generated_message_util.h:53:0, from /home/catching/work/dev/baidu/dmkit/unit-dmkit/brpc/src/brpc/esp_message.h:21, from /home/catching/work/dev/baidu/dmkit/unit-dmkit/brpc/src/brpc/esp_message.cpp:18: /usr/include/google/protobuf/wire_format_lite.h:918:13: note: ‘static bool google::protobuf::internal::WireFormatLite::ReadPrimitive(google::protobuf::io::CodedInputStream, CType) [with CType = int; google::protobuf::internal::WireFormatLite::FieldType DeclaredType = (google::protobuf::internal::WireFormatLite::FieldType)5]’ previously declared here inline bool WireFormatLite::ReadPrimitive<int32, WireFormatLite::TYPE_INT32>( ^~~~~~ In file included from /home/catching/work/dev/baidu/dmkit/unit-dmkit/brpc/src/brpc/esp_message.cpp:24:0: /usr/include/google/protobuf/wire_format_lite_inl.h:70:13: error: redefinition of ‘static bool google::protobuf::internal::WireFormatLite::ReadPrimitive(google::protobuf::io::CodedInputStream, CType) [with CType = long int; google::protobuf::internal::WireFormatLite::FieldType DeclaredType = (google::protobuf::internal::WireFormatLite::FieldType)3]’ inline bool WireFormatLite::ReadPrimitive<int64, WireFormatLite::TYPE_INT64>( ^~~~~~ In file included from /usr/include/google/protobuf/generated_message_util.h:53:0, from /home/catching/work/dev/baidu/dmkit/unit-dmkit/brpc/src/brpc/esp_message.h:21, from /home/catching/work/dev/baidu/dmkit/unit-dmkit/brpc/src/brpc/esp_message.cpp:18: /usr/include/google/protobuf/wire_format_lite.h:926:13: note: ‘static bool google::protobuf::internal::WireFormatLite::ReadPrimitive(google::protobuf::io::CodedInputStream, CType) [with CType = long int; google::protobuf::internal::WireFormatLite::FieldType DeclaredType = (google::protobuf::internal::WireFormatLite::FieldType)3]’ previously declared here inline bool WireFormatLite::ReadPrimitive<int64, WireFormatLite::TYPE_INT64>( ^~~~~~ [ 71%] Building CXX object src/CMakeFiles/OBJ_LIB.dir/brpc/event_dispatcher.cpp.o In file included from /home/catching/work/dev/baidu/dmkit/unit-dmkit/brpc/src/brpc/esp_message.cpp:24:0: /usr/include/google/protobuf/wire_format_lite_inl.h:79:13: error: redefinition of ‘static bool google::protobuf::internal::WireFormatLite::ReadPrimitive(google::protobuf::io::CodedInputStream, CType) [with CType = unsigned int; google::protobuf::internal::WireFormatLite::FieldType DeclaredType = (google::protobuf::internal::WireFormatLite::FieldType)13]’ inline bool WireFormatLite::ReadPrimitive<uint32, WireFormatLite::TYPE_UINT32>( ^~~~~~ In file included from /usr/include/google/protobuf/generated_message_util.h:53:0, from /home/catching/work/dev/baidu/dmkit/unit-dmkit/brpc/src/brpc/esp_message.h:21, from /home/catching/work/dev/baidu/dmkit/unit-dmkit/brpc/src/brpc/esp_message.cpp:18: /usr/include/google/protobuf/wire_format_lite.h:934:13: note: ‘static bool google::protobuf::internal::WireFormatLite::ReadPrimitive(google::protobuf::io::CodedInputStream, CType) [with CType = unsigned int; google::protobuf::internal::WireFormatLite::FieldType DeclaredType = (google::protobuf::internal::WireFormatLite::FieldType)13]’ previously declared here inline bool WireFormatLite::ReadPrimitive<uint32, WireFormatLite::TYPE_UINT32>( ^~~~~~ In file included from /home/catching/work/dev/baidu/dmkit/unit-dmkit/brpc/src/brpc/esp_message.cpp:24:0: /usr/include/google/protobuf/wire_format_lite_inl.h:85:13: error: redefinition of ‘static bool google::protobuf::internal::WireFormatLite::ReadPrimitive(google::protobuf::io::CodedInputStream, CType) [with CType = long unsigned int; google::protobuf::internal::WireFormatLite::FieldType DeclaredType = (google::protobuf::internal::WireFormatLite::FieldType)4]’ inline bool WireFormatLite::ReadPrimitive<uint64, WireFormatLite::TYPE_UINT64>( ^~~~~~