我收到 TypeError: 'types.GenericAlias' 对象在我的聊天机器人上不可迭代

问题描述 投票:0回答:1

我目前正在为一个项目构建一个聊天机器人,它不断回复相同的内容

主要功能在这里

def AXIS():
  knowledge_base:dict = open('knowledge_base.json','r',errors = 'ignore')
  while True:
    user_input:str = input('You:')
    if user_input.lower() in ("q", "quit", "shut down", "shutdown", "cancel", "power off", "poweroff", "power down", "powerdown", "off", "turn off", "turnoff"):
      print("AXIS: powering down")
      break

    best_match: str | None = find_best_match(user_input, list['q("question") for q in knowledge_base.json("questions")'])
    if best_match:
      answer: str = get_answer_for_question(best_match, knowledge_base)
      print(f'AXIS: {answer}')
    else:
      print('AXIS: I don\'t know the answer. please teach me?')
      new_answer: str = input('Type the answer or "skip" to skip:  ')
      if new_answer.lower() != 'skip':
        knowledge_base["questions"].append({"question": user_input, "answer": new_answer})
        save_knowledge_base('knowledge_base.json', knowledge_base)
        print('AXIS: I have learned a new response!')

if __name__ =='__main__':
  AXIS()

我不知道从哪里开始解决这个问题

knowledge_base 目前非常简单,如下所示。

{
  "questions": [
    
  ]
}

当您教授聊天机器人时,知识库会建立在自身之上

这是其他功能

import json
from difflib import get_close_matches
def save_knowledge_base(file_path: str, data: dict):
  with open(file_path,'w')as file:
    json.dump(data,file, indent=2)
def find_best_match(user_question: str, questions: list[str]):
  matches: list = get_close_matches(user_question, questions, n=1, cutoff=0.6)
  return matches[0] if matches else None
def get_answer_for_question(question: str, knowledge_base: dict):
  for q in knowledge_base["questions"]:
    if q["question"] == question:
      return q["answer"]

我正在按照 YouTube 教程来构建它。 https://www.youtube.com/watch?v=CkkjXTER2KE

错误在这里

You:hi
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-81-46ef27f29e5d> in <cell line: 21>()
     20 
     21 if __name__ =='__main__':
---> 22   AXIS()

2 frames
/usr/lib/python3.10/difflib.py in get_close_matches(word, possibilities, n, cutoff)
    700     s = SequenceMatcher()
    701     s.set_seq2(word)
--> 702     for x in possibilities:
    703         s.set_seq1(x)
    704         if s.real_quick_ratio() >= cutoff and \

TypeError: 'types.GenericAlias' object is not iterable

它向我显示错误位于预安装的 difflib.py 文件的该区域中


    if not n >  0:
        raise ValueError("n must be > 0: %r" % (n,))
    if not 0.0 <= cutoff <= 1.0:
        raise ValueError("cutoff must be in [0.0, 1.0]: %r" % (cutoff,))
    result = []
    s = SequenceMatcher()
    s.set_seq2(word)
    for x in possibilities:
        s.set_seq1(x)
        if s.real_quick_ratio() >= cutoff and \
           s.quick_ratio() >= cutoff and \
           s.ratio() >= cutoff:
            result.append((s.ratio(), x))
python-3.x typeerror chatbot
1个回答
1
投票

knowledge_base("questions")
:你正在调用字典;也许您想要方括号:
knowledge_base["questions"]

尽管这个错误不应该导致您收到错误消息。但是你告诉人们

knowledge_base
是第 26 行中的一个字典。那么也许它是一个派生类型?什么是
type(knowledge_base)


我注意到以下几点:

def load_knowledge_base(file_path: str):
  return dict
  ...

您正在返回一个类型! 我怀疑这是否是故意的。也许只是简单地删除该行?

它明确解释了错误消息。如果您返回实际实例化的字典,而不是类型,您将遇到我在顶部提到的错误。所以你至少有两件事需要解决。

© www.soinside.com 2019 - 2024. All rights reserved.