前言 之前有一篇文章「[Google Cloud] 如何在 GCP Cloud Run 上面透過 LangChain 取得 YouTube 的相關資訊」,雖然有講過使用 Secret Manager 與 GCP. 相關的 LangChain YouTube 套件來試著抓取資料。但是近期 YouTube 又開始修該他的讀取規範,造成原來的方式不能成功,這裡記錄一下主要錯誤訊息,還有該如何解決。 主要的問題 有一天 YouTube 的字幕開始抓不到,查詢 Log 出現以下內容。 During handling of the above exception, another exception occurred: youtube_transcript_api._errors.RequestBlocked: Could not retrieve a transcript for the video https://www.youtube.com/watch?v=ViA4-YWx8Y4! This is most likely caused by: YouTube is blocking requests from your IP. This usually is due to one of the following reasons: - You have done too many requests and your IP has been blocked by YouTube - You are doing requests from an IP belonging to a cloud provider (like AWS, Google Cloud Platform, Azure, etc.). Unfortunately, most IPs from cloud providers are blocked by YouTube. There are two things you can do to work around this: 1. Use proxies to hide your IP address, as explained in the "Working around IP bans" section of the README (https://github.com/jdepoix/youtube-transcript-api?tab=readme-ov-file#working-around-ip-bans-requestblocked-or-ipblocked-exception). 2. (NOT RECOMMENDED)...
前情提要 前面提供相當多透過 LangChain 來打造一個 LINE Bot 的案例。但是如果希望使用更穩定的後台,並且希望使用更多 AI 相關的功能,那麼 Vertex AI 就是就是一個很好的選擇。接下來會開始逐步介紹整個移植過程並且介紹需要介紹的地方,還有可能會出現的問題。 範例程式碼: https://github.com/kkdai/linebot-gemini-python (透過這個程式碼,可以快速部署到 GCP Cloud Run) 透過 LangChain 與 Gemini 打造 LINE Bot 到 Vertex AI 首先先給各位一個簡單的 LangChain + Gemini 打造 LINE Bot 的範例程式碼: 處理 Webhook 相關程式碼: for event in events: if not isinstance(event, MessageEvent): continue if (event.message.type == "text"): # Process text message using LangChain msg = event.message.text response = generate_text_with_langchain(f'{msg}, reply in zh-TW:') reply_msg = TextSendMessage(text=response) await line_bot_api.reply_message( event.reply_token, reply_msg ) 接下來解釋一下 generate_text_with_langchain 的內容: # Initialize LangChain with Gemini os.environ["GOOGLE_API_KEY"] = gemini_key .... def generate_text_with_langchain(prompt): """ Generate a text completion using LangChain with Gemini model. """ # Create a chat prompt template with system instructions prompt_template = ChatPromptTemplate.from_messages([ SystemMessage( content="You are a helpful assistant that responds in Traditional Chinese (zh-TW)."), HumanMessage(content=prompt) ]) # Format the prompt and call the model formatted_prompt = prompt_template.format_messages() response = text_model.invoke(formatted_prompt) return response.content 這就是部分片段的透過 LangChain 加上 Gemini 來打造 LINE Bot 的程式碼,完成程式碼。...