<目次>
(1) LangChainのChainやAgentとは?概要やサンプルプログラムをご紹介
 (1-1) やりたいこと
 (1-2) Chain概要
 (1-3) Chainサンプルプログラム(Chainの適用)
 (1-4) Agent概要
 (1-5) Agentサンプルプログラム
(1) LangChainのChainやAgentとは?概要やサンプルプログラムをご紹介
(1-1) やりたいこと
(1-2) Chain概要
(1-3) Chainサンプルプログラム(Chainの適用)
    # モデルにPrompt(入力)を与えCompletion(出力)を取得する
    response = llm(chat_prompt.format_prompt(
        prefectures=input("都道府県名を入力してください : "),
        text="調子はどうですか?").to_messages()
    )
    # chainを用いて、ModelとPromptを紐づけ
    chain = LLMChain(llm=llm, prompt=chat_prompt)
    # chainを用いてモデルを実行(Prompt(入力)を与えCompletion(出力)を取得する)
    response = chain.run(prefectures=input("都道府県名を入力してください : "), text="調子はどうですか?")
import os
from dotenv import load_dotenv
from langchain.chat_models import ChatOpenAI
from langchain.prompts.chat import (ChatPromptTemplate,SystemMessagePromptTemplate,HumanMessagePromptTemplate)
from langchain import LLMChain
# 環境変数読み込み
load_dotenv()
#OpenAIの接続情報など
api_key = os.environ.get('OPEN_AI_KEY')
def main():
    # 言語モデル(OpenAIのチャットモデル)のラッパークラスをインスタンス化
    llm = ChatOpenAI(
        model="gpt-3.5-turbo",
        openai_api_key=api_key,
        temperature=0.0
    )
    # プロンプトのテンプレート生成
    system_template = "あなたは{prefectures}出身です。{prefectures}の方言で返答してください。"
    system_message_prompt = SystemMessagePromptTemplate.from_template(system_template)
    human_template = "{text}"
    human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
    chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])
    # chainを用いて、ModelとPromptを紐づけ
    chain = LLMChain(llm=llm, prompt=chat_prompt)
    # chainを用いてモデルを実行(Prompt(入力)を与えCompletion(出力)を取得する)
    response = chain.run(prefectures=input("都道府県名を入力してください : "), text="調子はどうですか?")
    print(response)
if __name__ == "__main__":
    main()

(1-4) Agent概要
(1-5) Agentサンプルプログラム
●STEP0:SerpAPIのAPIキー取得
https://serpapi.com/manage-api-key

↓



↓





↓
●STEP1:パッケージのインストール
> pip install google-search-results

●STEP2:サンプルプログラムの実行(Agentが自律的に段取りを考える!)
import os
from dotenv import load_dotenv
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.agents import AgentType
from langchain.llms import OpenAI
# 環境変数読み込み
load_dotenv()
#OpenAIの接続情報など
api_key = os.environ.get('OPEN_AI_KEY')
os.environ["SERPAPI_API_KEY"] = os.environ.get('SERPAPI_API_KEY')
def main():
    # 言語モデル(OpenAIのチャットモデル)のラッパークラスをインスタンス化
    llm = OpenAI(
        openai_api_key=api_key,
        temperature=0.0
    )
    # ツールのロード。`llm-math`はllmを使用するため、引数に与える
    tools = load_tools(["serpapi", "llm-math"], llm=llm)
    # Agentを初期化。Tool、LLM、Agentの種別を与える。
    agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
    # 実行
    agent.run("昨日の東京の気温は摂氏で何度?その数字を3乗すると?")
if __name__ == "__main__":
    main()
> Entering new AgentExecutor chain... I need to find the temperature in Tokyo yesterday and then cube it. Action: Search Action Input: Tokyo temperature yesterday Observation: Tokyo Temperature Yesterday. Maximum temperature yesterday: 70 °F (at 1:00 pm) Minimum temperature yesterday: 61 °F (at 8:30 am) Average temperature ... Thought: I need to convert the temperature from Fahrenheit to Celsius Action: Calculator Action Input: (70-32)*5/9 Observation: Answer: 21.11111111111111 Thought: I now need to cube the temperature Action: Calculator Action Input: 21.11111111111111^3 Observation: Answer: 9408.779149519889 Thought: I now know the final answer Final Answer: 9408.779149519889 > Finished chain.
