Search youtube video by API query string
import os

from youtube import FindYoutubeVideoTool
if os.getenv('API_ENV') != 'production':
    from dotenv import load_dotenv

    load_dotenv()

import uvicorn
import logging
from fastapi import FastAPI
from langchain.chat_models import ChatOpenAI
from langchain.agents import AgentType
from langchain.agents import initialize_agent


logging.basicConfig(level=os.getenv('LOG', 'WARNING'))
logger = logging.getLogger(__file__)
app = FastAPI()
model = ChatOpenAI(model="gpt-3.5-turbo-0613")
tools = [FindYoutubeVideoTool()]
open_ai_agent = initialize_agent(
    tools,
    model,
    agent=AgentType.OPENAI_FUNCTIONS,
    verbose=False)


@app.get("/api")
def handle_callback(q: str):
    logging.info(f"testing...data is {q}")
    tool_result = open_ai_agent.run(q)
    return tool_result

if __name__ == "__main__":
    port = int(os.environ.get('PORT', default=8080))
    debug = True if os.environ.get(
        'API_ENV', default='develop') == 'develop' else False
    logging.info('Application will start...')
    uvicorn.run("main:app", host="0.0.0.0", port=port, reload=debug)

main.py
louis70109/youtube-search-langchain
from ast import literal_eval
from langchain.tools import BaseTool
from typing import Optional, Type
from pydantic import BaseModel, Field
from langchain.tools import YouTubeSearchTool
import logging


class YoutubeDefineInput(BaseModel):
    """Youtube video recommendation."""

    title: str = Field(
        ...,
        description="video title which will be recommend to user.")


class FindYoutubeVideoTool(BaseTool):
    name = "youtube_search"
    description = "Find recommendation video from Youtube"

    def _run(self, title: str):
        logging.info("Youtube")
        logging.info('標題：'+title)
        tool = YouTubeSearchTool()
        youtube_str = tool.run(title)  # force change str to list
        youtube_list = literal_eval(youtube_str)
        for i in range(len(youtube_list)):
            youtube_list[i] = f'https://www.youtube.com/{youtube_list[i]}'
        return youtube_list

    args_schema: Optional[Type[BaseModel]] = YoutubeDefineInput

youtube.py
louis70109/youtube-search-langchain