Exemplo n.º 1
0
def internal_push_nlu(robot_code, version):
    """通知主进程nlu模型训练完毕,进行版本的切换

    Args:
        robot_code (str): 机器人唯一标识,ID
        version (str): 机器人版本号
    """
    url = "http://localhost:{}/xiaoyu/multi/nlu".format(SERVE_PORT)
    data = {"robot_id": robot_code, "method": "checkout", "version": version}
    post_rpc(url, data)
Exemplo n.º 2
0
def faq_delete(robot_id, data):
    """删除faq引擎中的语料数据

    Args:
        robot_id (str):  机器人的唯一标识。
        data (dict): 请求参数,里面包含需要删除的语料id

    Examples:
        >>> robot_id = "doctest_id"
        >>> qids = {"faq_ids": ["id1"]}
        >>> faq_delete(robot_id, qids)
        {'status_code': 0}
    """
    # TODO 这里后续要考虑如何删除similar questions
    url = "http://{}/robot_manager/single/delete_items".format(FAQ_ENGINE_ADDR)
    q_ids = data["faq_ids"]
    if isinstance(q_ids, str):
        q_ids = [q_ids]

    # TODO 由于存储问题时默认会存储相似问题,这里需要把原问题关联的相似问题删除
    # 这里处理方式有点打补丁的意思
    all_ids = []
    for qid in q_ids:
        all_ids.extend(
            [_build_sim_id(qid, i) for i in range(MAX_SIMILAR_QUESTIONS)])
    q_ids.extend(all_ids)

    request_data = {"q_ids": q_ids, "robot_code": robot_id}
    return post_rpc(url, request_data)
Exemplo n.º 3
0
def test_push():

    data = {"robot_id": robot_code, "version": version}

    response = post_rpc("http://127.0.0.1:{}/xiaoyu/push".format(serve_port),
                        data)
    print(response)
Exemplo n.º 4
0
def test_method_add():
    requests_data = {
        "robot_id": TEST_ROBOT_ID,
        "method": "add",
        "data": TEST_FAQ_DATA
    }
    response_data = post_rpc(URL, requests_data)
    assert response_data["code"] == 200
Exemplo n.º 5
0
def test_method_update():
    faq_data = TEST_FAQ_DATA.copy()
    requests_data = {
        "robot_id": TEST_ROBOT_ID,
        "method": "update",
        "data": faq_data
    }
    response_data = post_rpc(URL, requests_data)
    assert response_data["code"] == 200
Exemplo n.º 6
0
def test_method_ask():
    request_data = {
        "robot_id": TEST_ROBOT_ID,
        "method": "ask",
        "data": {
            "question": "苹果手机多少钱"
        }
    }
    response_data = post_rpc(URL, request_data)
    assert response_data["code"] == 200
Exemplo n.º 7
0
def test_method_delete():
    request_data = {
        "robot_id": TEST_ROBOT_ID,
        "method": "delete",
        "data": {
            "faq_ids": [TEST_FAQ_DATA[0]["faq_id"]]
        }
    }
    response_data = post_rpc(URL, request_data)
    assert response_data["code"] == 200

    request_data = {
        "robot_id": TEST_ROBOT_ID,
        "method": "ask",
        "data": {
            "question": "Apple手机多少钱"
        }
    }
    response_data = post_rpc(URL, request_data)
    assert response_data["data"]["faq_id"] == UNK
Exemplo n.º 8
0
def faq_ask(robot_id,
            question,
            faq_params={
                "recommend_num": 5,
                "perspective": FAQ_DEFAULT_PERSPECTIVE
            }):
    """向faq引擎提问
    Args:
        robot_id (str): 机器人的唯一标识
        question (str): 向机器人提问的问题
        raw (bool, optional): 返回faq引擎的原始数据,还是返回解析后的答案数据。Default is False
        faq_params (int, optional): faq相关参数
    Examples:
        >>> robot_id = "doctest_id"
        >>> question = "你好"
        >>> answer = faq_ask(robot_id, question)
        >>> isinstance(answer, dict)
        True
    """
    url = "http://{}/robot_manager/single/ask".format(FAQ_ENGINE_ADDR)
    request_data = {
        "robot_code": robot_id,
        "question": question,
    }
    request_data.update(faq_params)
    response_data = post_rpc(url, request_data)["data"]

    if response_data["answer_type"] == -1:
        answer_data = {
            "faq_id": UNK,
            "title": "",
            "similar_questions": [],
            "related_quesions": [],
            "key_words": [],
            "effective_time": "",
            "tags": [],
            "answer": response_data["answer"],
            "catagory": "",
        }
    else:
        answer_data = json.loads(response_data["answer"])
    # 相关问题加推荐问题的总数为指定数量
    related_questions = answer_data.get("related_questions", [])
    rec_num = faq_params["recommend_num"] - len(related_questions)
    answer_data["recommendQuestions"] = response_data.get(
        "recommendQuestions", [])[rec_num:]
    answer_data["confidence"] = response_data.get("confidence", 0)
    answer_data["hotQuestions"] = response_data.get("hotQuestions", [])
    answer_data["recommendScores"] = response_data.get("recommendScores", [])
    answer_data["reply_mode"] = response_data.get("reply_mode", "1")
    answer_data["sms_content"] = response_data.get("sms_content", "")
    return answer_data
Exemplo n.º 9
0
def faq_push(robot_id):
    """
    复制faq节点index
    """
    if not MASTER_ADDR:
        return {'status_code': 0}
    target_robot_id = get_faq_master_robot_id(robot_id)
    url = "http://{}/robot_manager/single/copy".format(FAQ_ENGINE_ADDR)
    request_data = {
        "robot_code": robot_id,
        "target_robot_code": target_robot_id
    }
    return post_rpc(url, request_data)
def train_nlu():
    with open(os.path.join(cwd, "assets/nlu_training_data.json")) as f:
        nlu_data = json.load(f)
    # 训练nlu模型
    data = {
        "robot_id": robot_code,
        "method": "train",
        "version": version,
        "data": nlu_data
    }
    response = post_rpc(
        "http://127.0.0.1:{}/xiaoyu/multi/nlu".format(serve_port), data)
    print(response)
Exemplo n.º 11
0
def faq_delete_all(robot_id):
    """删除特定机器人的所有语料

    Args:
        robot_id (str): 机器人的唯一标识

    Examples:
        >>> robot_id = "doctest_id"
        >>> faq_delete_all(robot_id)
        {'status_code': 0}
    """
    url = "http://{}/robot_manager/single/delete_robot".format(FAQ_ENGINE_ADDR)
    request_data = {"robot_code": robot_id}
    return post_rpc(url, request_data)
def train_dialogue():
    with open(os.path.join(cwd, "assets/dialogue_graph.json")) as f:
        graph_data = json.load(f)

    # 上传对话流程配置
    data = {
        "robot_id": robot_code,
        "method": "train",
        "version": version,
        "data": graph_data
    }
    response = post_rpc(
        "http://127.0.0.1:{}/xiaoyu/multi/graph".format(serve_port), data)
    print(response)
def test_chat():
    session_id = "test_chat_api"
    data = {
        "robotId": robot_code,
        "userCode": "user1",
        "sessionId": session_id,
        "params": {
            "归属地": "广州"
        },
        "userSays": "你好"
    }
    response = post_rpc(
        "http://127.0.0.1:{}/api/v1/session/reply".format(serve_port), data)
    print(response)

    data = {
        "robotId": robot_code,
        "userCode": "user1",
        "sessionId": session_id,
        "userSays": "今天广州天气怎么样"
    }
    response = post_rpc(
        "http://127.0.0.1:{}/api/v1/session/reply".format(serve_port), data)
    print(response)
Exemplo n.º 14
0
def faq_update(robot_id, data):
    """添加或者更新faq语料数据

    Args:
        robot_id (str): 机器人的唯一标识。
        data (list): 需要存储的问题数据。

    Return:
        response (dict): faq服务器返回的信息

    Examples:
        >>> robot_id = "doctest_id"
        >>> data = [
        ...    {
        ...        "faq_id": "id1",
        ...        "title": "苹果手机多少钱",
        ...        "similar_questions": [
        ...            "Apple手机多少钱",
        ...            "iphone多少钱"
        ...        ],
        ...        "related_quesions": [
        ...            "ipad多少钱",
        ...            "iwatch多少钱"
        ...        ],
        ...        "key_words": [
        ...            "苹果",
        ...            "Apple",
        ...            "iphone"
        ...        ],
        ...        "effective_time": "2020-12-31",
        ...        "tags": [
        ...            "手机",
        ...            "电子产品"
        ...        ],
        ...        "answer": "5400元",
        ...        "catagory": "电子产品价格"
        ...    }
        ... ]
        >>> faq_update(robot_id, data)
        {'status_code': 0}
    """
    url = "http://{}/robot_manager/single/add_items".format(FAQ_ENGINE_ADDR)

    documents = []
    for item in data:
        perspective = item.get("perspective", "")  # 当视角参数为空字符串或者没有此参数时,设置为默认视角
        doc = {
            "answer": json.dumps(item, ensure_ascii=False),
            "perspective":
            perspective if perspective else FAQ_DEFAULT_PERSPECTIVE,
            "question": item["title"],
            "id": item["faq_id"],
            "answer_id": item["faq_id"]
        }
        documents.append(doc)
        for i, sim_q in enumerate(item.get("similar_questions", [])):
            if i >= MAX_SIMILAR_QUESTIONS:
                break
            curdoc = copy.deepcopy(doc)
            curdoc["question"] = sim_q
            curdoc["id"] = _build_sim_id(curdoc["id"], i)
            documents.append(curdoc)

    request_data = {"documents": documents, "robot_code": robot_id}
    return post_rpc(url, request_data)
def test_system():
    # 删除机器人
    data = {"robot_id": robot_code}
    response = post_rpc("http://127.0.0.1:{}/xiaoyu/delete".format(serve_port),
                        data)
    print(response)
serve_port = global_config["serve_port"]

cwd = os.path.abspath(os.path.dirname(__file__))
params = json.load(open(os.path.join(cwd, "params.json")))
session_id = "service_interact"

says = input("用户说:")
data = {
    "robotId": params["robot_code"],
    "userCode": "user1",
    "params": params["params"],
    "userSays": says,
    "sessionId": session_id
}
response = post_rpc(
    "http://127.0.0.1:{}/api/v1/session/reply".format(serve_port), data)
print(response)

while True:
    says = input("用户说:")
    data = {
        "robotId": params["robot_code"],
        "userCode": "user1",
        "sessionId": session_id,
        "userSays": says
    }
    response = post_rpc(
        "http://127.0.0.1:{}/api/v1/session/reply".format(serve_port), data)
    print(response)