You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

语音识别聊天Bot无法执行Google/YouTube搜索问题排查求助

问题定位与修复方案

1. 核心逻辑错误:条件判断语法缺失

在maya_ai.py的内层循环中,elif "how are you":这一行漏掉了in query的判断逻辑,导致这个条件永远成立(Python中非空字符串会被视为布尔值True)。当你说出类似"google Sundar Pichai"的指令时,前面的hello、i am fine等条件都不匹配,程序会直接跳转到这个分支,回复"i am perfectly alright sir",完全不会执行后续的Google/YouTube搜索代码。

修复方法:将该条件修改为elif "how are you" in query:

2. 模块导入时的意外行为

maya_google_search.py顶部有一行query = takeCommand().lower(),这行代码会在模块被导入的瞬间自动执行,也就是说每次调用搜索功能时,都会额外触发一次麦克风监听,不仅干扰主程序的指令接收,还可能覆盖传入的查询参数,导致搜索逻辑异常。

修复方法:删除这行全局的query赋值代码,函数仅使用传入的参数即可。

3. 冗余判断优化(非致命但建议修改)

maya_google_search.py中的Googlesearch和Youtubesearch函数内部,重复做了"google" in query和"youtube" in query的判断,但主程序maya_ai.py已经提前完成了这个判断,这些内部判断属于冗余代码,可以删除以简化逻辑。


修复后的关键代码片段

maya_ai.py 核心循环部分

if __name__ == "__main__":
    
    while True:
        query = takeCommand().lower()
        if "wake up" in query:
            from maya_greeting import greetMe
            greetMe()

            while True:
                query = takeCommand().lower()
                if "go to sleep" in query:
                    speak("Ok sir, You can call me anytime...")
                    break
                
                elif "hello" in query:
                    speak("Hello Sir, how are you?")

                elif "i am fine" in query:
                    speak("That's really great to know sir....")

                # 修复条件判断
                elif "how are you" in query:
                    speak("i am perfectly alright sir.")

                elif "thank you" in query:
                    speak("you're welcome sir")

                elif "google" in query:
                    from maya_google_search import Googlesearch
                    Googlesearch(query)

                elif "youtube" in query:
                    from maya_google_search import Youtubesearch
                    Youtubesearch(query)
                
                elif "wikipedia" in query:
                    from maya_google_search import Wikisearch
                    Wikisearch(query)

maya_google_search.py 搜索函数部分

# 删除全局的query赋值
# query = takeCommand().lower()

def Googlesearch(query):
    # 移除冗余的if判断
    query = query.replace("Maya", "")
    query = query.replace("google search", "")
    query = query.replace("google", "")

    speak("This is what I found on Google.....")
    
    try:
        pywhatkit.search(query)
        result = googleScrap.summary(query,sentences=2)
        speak("According to Google..........")
        speak(result)
    
    except:
        speak("No speakable output available")

def Youtubesearch(query):
    # 移除冗余的if判断
    query = query.replace("Maya", "")
    query = query.replace("youtube search", "")
    query = query.replace("youtube", "")
    speak("This is what I found for your search!") 

    web = "https://www.youtube.com/results?search_query=" + query

    webbrowser.open(web)
    pywhatkit.playonyt(query)
    
    speak("Done, sir")

内容的提问来源于stack exchange,提问作者Sai Sasidhar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 18:46:02