見出し画像

AutoGPTをゼロからの学習(コード付き)

Youtubeで、「AutoGPTアプリを25分間で作成しよう」という動画を見つけましたので紹介していきます。作成するアプリは、streamlitを使用したYoutube GPT Creatorというアプリです。鍵となるキーワードを入力したら、Youtube動画の件名とスクリプトをWikipediaベースで考えてくれるアプリです。



動画でもコードが出てきますが、githubにもありますので、下記を参考にさせて頂きました。非常にコードがシンプルでlangchainの使い方もわかりやすく理解しやすいのではないかと思われます。



今回は、自PC環境で実行しました。

D:\Python\29.autogpt>python -m venv venv

D:\Python\29.autogpt>venv\Scripts\activate

(venv) D:\Python\29.autogpt>pip install apikey streamlit langchain openai wikipedia


次に、test.pyという下記内容のファイルを作成します。Your-API-KeyにはOpenAIで取得したAPIキーを入力します。

# Bring in deps
import os 
from apikey import apikey 

import streamlit as st 
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain, SequentialChain 
from langchain.memory import ConversationBufferMemory
from langchain.utilities import WikipediaAPIWrapper 
import openai


os.environ['OPENAI_API_KEY']= "Your-API-Key"

# App framework
st.title('🦜🔗 YouTube GPT Creator')
prompt = st.text_input('Plug in your prompt here') 

# Prompt templates
title_template = PromptTemplate(
    input_variables = ['topic'], 
    template='write me a youtube video title about {topic}'
)

script_template = PromptTemplate(
    input_variables = ['title', 'wikipedia_research'], 
    template='write me a youtube video script based on this title TITLE: {title} while leveraging this wikipedia reserch:{wikipedia_research} '
)

# Memory 
title_memory = ConversationBufferMemory(input_key='topic', memory_key='chat_history')
script_memory = ConversationBufferMemory(input_key='title', memory_key='chat_history')


# Llms
llm = OpenAI(temperature=0.9) 
title_chain = LLMChain(llm=llm, prompt=title_template, verbose=True, output_key='title', memory=title_memory)
script_chain = LLMChain(llm=llm, prompt=script_template, verbose=True, output_key='script', memory=script_memory)

wiki = WikipediaAPIWrapper()

# Show stuff to the screen if there's a prompt
if prompt: 
    title = title_chain.run(prompt)
    wiki_research = wiki.run(prompt) 
    script = script_chain.run(title=title, wikipedia_research=wiki_research)

    st.write(title) 
    st.write(script) 

    with st.expander('Title History'): 
        st.info(title_memory.buffer)

    with st.expander('Script History'): 
        st.info(script_memory.buffer)

    with st.expander('Wikipedia Research'): 
        st.info(wiki_research)


次に、自PC端末で下記を実行します。

(venv) D:\Python\29.autogpt>streamlit run test.py


すると、ウェブ画面が開きます。


OpenAIと入力して、OpenAIについてのYoutubeシナリオを作成してもらいます。


タイトルはAIの未来ですね。

今回のコードのままでは、日本語はおかしな結果になり英語しか使えないです。しかし、template内の文言を日本語にすることにより日本語に対応できるのではないかと考えています。

また、st.titleを変更したり、templateを変更することにより、Youtube以外のシナリオも作成できたりします。

また、メモリやLLMChainをうまく使えば他にも色々なことができます。

いいなと思ったら応援しよう!