AzureからGPTを使ってみる | agentの使い方(その2:検索編)


前回、演算機能をagentとして使用する記事を書きましたが、今回はTavilyを使用して、リアルタイムで正確かつ事実に基づいた回答をするagentを設定したいと思います。
Tavilyは、公式サイトから登録をするとAPIキーを使用することができます。
無料枠でも、毎月1,000 API コールまで無料で利用できるのでちょっと試す分には、十分でしょう。

LangchainからTavilyを使ってみます

from langchain.retrievers.tavily_search_api import TavilySearchAPIRetriever

# Kで取得するドキュメントの数を指定できます。
retriever = TavilySearchAPIRetriever(k=3)
retriever.invoke("日本は、2024パリオリンピックで何個金メダルを取得しましたか?")
[Document(metadata={'title': 'パリオリンピック メダルランキング【8月4日】 日本 金9個 銀5個 銅10個に | Nhk | #パリオリンピック', 'source': 'https://www3.nhk.or.jp/news/html/20240805/k10014537201000.html', 'score': 0.9997584, 'images': []}, page_content='2024年8月5日 6時19分#パリオリンピック 日本が今大会で獲得したメダルは金9個、銀5個、銅10個のあわせて24個となり、メダルの総数は韓国と並んで6 ...'),
 Document(metadata={'title': 'パリオリンピック 日本 金メダル・メダル総数ともに海外の大会での最多を更新 | Nhk | #パリオリンピック', 'source': 'https://www3.nhk.or.jp/news/html/20240811/k10014545621000.html', 'score': 0.9993013, 'images': []}, page_content='また、パリオリンピックで日本は大会15日目までを終えて37個のメダルを獲得していました。 大会16日目の10日は、男子高飛び込みで玉井陸斗選手 ...'),
 Document(metadata={'title': 'ここまでの日本代表のメダル獲得者をチェック!', 'source': 'https://olympics.com/ja/paris-2024/live-updates/b3a18bab-e5aa-44c8-b091-9ea944dcbcb2', 'score': 0.99647564, 'images': []}, page_content='8月8日パリ2024 Day13 - 今日の一枚📸. 本日の一枚はセーリング男女混合ディンギー470級で銀メダルを獲得した岡田奎樹(おかだ・けいじゅ)&吉岡美帆(よしおか・みほ)ペア。. 日本勢がセーリングでメダルを獲得するのはアテネ2004以来、なんと20年ぶりの快挙!')]

chainに組み込みます。

 #langchain -openai=0.1.15 #langchain =0.2.7     #tavily -python=0.3.3

from langchain_openai import AzureChatOpenAI
from langchain.retrievers.tavily_search_api import TavilySearchAPIRetriever
from langchain_core.prompts import PromptTemplate
from langchain_core.runnables import RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser

from dotenv import load_dotenv
import os

# OpenAI APIキーの設定
dotenv_path = ".env"
load_dotenv(dotenv_path)

OPENAI_API_BASE = os.getenv('OPENAI_API_BASE')
OPENAI_API_VERSION = os.getenv('OPENAI_API_VERSION')
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')

TAVILY_API_KEY = os.getenv('TAVILY_API_KEY')

os.environ["AZURE_OPENAI_API_KEY"] = OPENAI_API_KEY
os.environ["AZURE_OPENAI_ENDPOINT"] = OPENAI_API_BASE



template = """You are a helpful assistant.

context:
{context}

user input:{question}

AI:
"""

prompt_template = PromptTemplate(
		template=template,
    input_variables=["context", "question"]
)

llm = AzureChatOpenAI(
    api_version=OPENAI_API_VERSION, 
    azure_deployment="gpt4o",# azure_deployment = "deployment_name"
    verbose=True
    )

# Kで取得するドキュメントの数を指定できます。
retriever = TavilySearchAPIRetriever(k=3)

chain = (
    RunnablePassthrough.assign(context=(lambda x: x["question"]) | retriever)
    | prompt_template
    | llm
    | StrOutputParser()
)

chain.invoke({"question":"日本は、2024パリオリンピックで何個金メダルを取得しているか検索して教えてください。"})
'日本は、2024年パリオリンピックで18個の金メダルを取得しています。これにより、海外開催の五輪での過去最多記録を更新しました。'

8/11時点の金メダル数は18個で正解です。

せっかくなので、検索と演算のagentを設定してみます。

from langchain.chains import LLMMathChain#need pip install numexpr

from langchain.agents import Tool
from langchain.agents import initialize_agent, AgentType
from langchain_experimental.utilities import PythonREPL

from langchain.retrievers.tavily_search_api import TavilySearchAPIRetriever

from langchain_openai import AzureChatOpenAI
from dotenv import load_dotenv
import os

# OpenAI APIキーの設定
dotenv_path = ".env"
load_dotenv(dotenv_path)

OPENAI_API_BASE = os.getenv('OPENAI_API_BASE')
OPENAI_API_VERSION = os.getenv('OPENAI_API_VERSION')
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')

TAVILY_API_KEY = os.getenv('TAVILY_API_KEY')

os.environ["AZURE_OPENAI_API_KEY"] = OPENAI_API_KEY
os.environ["AZURE_OPENAI_ENDPOINT"] = OPENAI_API_BASE

llm = AzureChatOpenAI(
    api_version=OPENAI_API_VERSION, 
    azure_deployment="gpt4o" # azure_deployment = "deployment_name"
    )

llm_math_chain = LLMMathChain(llm=llm, verbose=True)

# TavilySearchAPIRetrieverの準備と確認
retriever = TavilySearchAPIRetriever(k=3)

python_repl = PythonREPL()
# You can create the tool to pass to an agent

tools =[Tool(
        name="python_repl",
        description="A Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`.",
        func=python_repl.run,    
        ),
        Tool(
        name="Search",
        func=retriever.invoke,
        description="useful for when you need to answer questions about current events"            
        ),
        Tool(
        name="Calculator",
        func=llm_math_chain.run,
        description="useful for when you need to answer questions about math"            
        )
    
]


print(f"length of tools: {len(tools)}")
for i in range(len(tools)):
    print(f"description of tool: {tools[i].description}")

# initialize the agent
agent_chain = initialize_agent(
    tools,
    llm,
    agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,
    verbose=True,
)

# run the agent

agent_chai.invoke(
    "2024パリオリンピックで日本は、金メダルを何個取得している検索して教えてください。さらに、その数を2乗した数値を教えてください。",
)
c:\Users\ytaka\anaconda3\envs\langgraph\Lib\site-packages\langchain\chains\llm_math\base.py:58: UserWarning: Directly instantiating an LLMMathChain with an llm is deprecated. Please instantiate with llm_chain argument or using the from_llm class method.
  warnings.warn(
length of tools: 3
description of tool: A Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`.
description of tool: useful for when you need to answer questions about current events
description of tool: useful for when you need to answer questions about math


> Entering new AgentExecutor chain...
c:\Users\ytaka\anaconda3\envs\langgraph\Lib\site-packages\langchain_core\_api\deprecation.py:139: LangChainDeprecationWarning: The function `initialize_agent` was deprecated in LangChain 0.1.0 and will be removed in 0.3.0. Use Use new agent constructor methods like create_react_agent, create_json_agent, create_structured_chat_agent, etc. instead.
  warn_deprecated(
c:\Users\ytaka\anaconda3\envs\langgraph\Lib\site-packages\langchain_core\_api\deprecation.py:139: LangChainDeprecationWarning: The method `Chain.run` was deprecated in langchain 0.1.0 and will be removed in 0.3.0. Use invoke instead.
  warn_deprecated(
Action:
```
{
  "action": "Search",
  "action_input": "2024 Paris Olympics Japan gold medals count"
}
```
Observation: [Document(metadata={'title': 'Medal Count - Paris 2024 Olympic Medal Table', 'source': 'https://olympics.com/en/paris-2024/medals/japan', 'score': 0.99620515, 'images': []}, page_content="Official medal standings for the Paris 2024 Olympics (Jul 26-Aug 11, 2024). Find out which athletes are bringing home medals and breaking records. ... Back. Back. Medal Table - Japan. Medal Table Medallists # NOCs. G. S. B. CHN People's Republic of China. 37 27 24 88. USA United States of America. 35 41 41 117. AUS Australia."), Document(metadata={'title': 'Medal Count - Paris 2024 Olympic Medal Table', 'source': 'https://olympics.com/en/paris-2024/medals', 'score': 0.9914887, 'images': []}, page_content='Official medal standings for the Paris 2024 Olympics (Jul 26-Aug 11, 2024). Find out which athletes are bringing home medals and breaking records. ... 39 27 24 90. USA United States of America. 38 42 42 122. AUS Australia. 18 18 14 50. JPN Japan. 18 12 13 43. FRA France. 16 24 22 62. GBR Great Britain. 14 22 27 63. KOR Republic of Korea. 13 8 9 ...'), Document(metadata={'title': 'Paris 2024 Olympics Medal Count: Gold, Silver, Bronze Tally by Country ...', 'source': 'https://www.bloomberg.com/graphics/paris-2024-summer-olympics-medal-count/', 'score': 0.98166555, 'images': []}, page_content='The latest daily event schedule, final results and medal count for the Paris 2024 Summer Olympics. Skip to content. ... Gold medals 1896-2020 ...')]
Thought:Action:
```
{
  "action": "Search",
  "action_input": "2024 Paris Olympics Japan gold medals count"
}
```
Observation: [Document(metadata={'title': 'Medal Count - Paris 2024 Olympic Medal Table', 'source': 'https://olympics.com/en/paris-2024/medals/japan', 'score': 0.99620515, 'images': []}, page_content="Official medal standings for the Paris 2024 Olympics (Jul 26-Aug 11, 2024). Find out which athletes are bringing home medals and breaking records. ... Back. Back. Medal Table - Japan. Medal Table Medallists # NOCs. G. S. B. CHN People's Republic of China. 37 27 24 88. USA United States of America. 35 41 41 117. AUS Australia."), Document(metadata={'title': 'Medal Table and Results for Japan at the Paris 2024 Olympics: Gold ...', 'source': 'https://en.as.com/results/olympic-games/medallero/japon/', 'score': 0.99465674, 'images': []}, page_content='Classification of Japan and the number of gold, silver, and bronze medals won at the Paris 2024 Olympic Games.'), Document(metadata={'title': 'Medal Count - Paris 2024 Olympic Medal Table', 'source': 'https://olympics.com/en/paris-2024/medals', 'score': 0.9914887, 'images': []}, page_content='Official medal standings for the Paris 2024 Olympics (Jul 26-Aug 11, 2024). Find out which athletes are bringing home medals and breaking records. ... 39 27 24 90. USA United States of America. 38 42 42 122. AUS Australia. 18 18 14 50. JPN Japan. 18 12 13 43. FRA France. 16 24 22 62. GBR Great Britain. 14 22 27 63. KOR Republic of Korea. 13 8 9 ...')]
Thought:
Python REPL can execute arbitrary code. Use with caution.
Based on the search results, Japan has won 18 gold medals in the 2024 Paris Olympics.

Next, I will calculate the square of 18.

Action:
```
{
  "action": "python_repl",
  "action_input": "print(18 ** 2)"
}
```

Observation: 324

Thought:Action:
```
{
  "action": "Final Answer",
  "action_input": "2024年パリオリンピックで日本は18個の金メダルを取得しています。その数を2乗した数値は324です。"
}
```

> Finished chain.

'2024年パリオリンピックで日本は18個の金メダルを取得しています。その数を2乗した数値は324です。'

英語で質問してみます。

agent_chain.invoke(
    #"2024パリオリンピックで日本は、金メダルを何個取得している検索して教えてください。さらに、その数を2乗した数値を教えてください。",
    "Please search and tell me how many gold medals Japan will win at the 2024 Paris Olympics. Also, please tell me the square of that number."
)
c:\Users\ytaka\anaconda3\envs\langgraph\Lib\site-packages\langchain\chains\llm_math\base.py:58: UserWarning: Directly instantiating an LLMMathChain with an llm is deprecated. Please instantiate with llm_chain argument or using the from_llm class method.
  warnings.warn(
length of tools: 3
description of tool: A Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`.
description of tool: useful for when you need to answer questions about current events
description of tool: useful for when you need to answer questions about math


> Entering new AgentExecutor chain...
c:\Users\ytaka\anaconda3\envs\langgraph\Lib\site-packages\langchain_core\_api\deprecation.py:139: LangChainDeprecationWarning: The function `initialize_agent` was deprecated in LangChain 0.1.0 and will be removed in 0.3.0. Use Use new agent constructor methods like create_react_agent, create_json_agent, create_structured_chat_agent, etc. instead.
  warn_deprecated(
Sure, let me first find out how many gold medals Japan is projected to win at the 2024 Paris Olympics.

Action:
```
{
  "action": "Search",
  "action_input": "Japan gold medals projection 2024 Paris Olympics"
}
```
Observation: [Document(metadata={'title': 'Japan Projected to Finish Sixth in Paris Olympics Medal Table', 'source': 'https://www.nippon.com/en/japan-data/h02063/', 'score': 0.99796516, 'images': []}, page_content='US company Gracenote, which analyzes sporting data, has predicted that Japan will win 47 medals at the Paris Olympics: 13 gold, 13 silver, and 21 bronze. This is 11 fewer than at Tokyo 2020.'), Document(metadata={'title': 'Japan predicted to finish sixth in Paris Games medal count', 'source': 'https://www.japantimes.co.jp/olympics/2024/06/27/japan-paris-2024-medal-predictions/', 'score': 0.9923933, 'images': []}, page_content='Jun 27, 2024. With a month to go until the Paris Olympics, Japan is expected to place sixth in total medals with 46, 12 fewer than at the Tokyo Olympics, sports data firm Gracenote predicted ...'), Document(metadata={'title': 'Medal Count - Paris 2024 Olympic Medal Table', 'source': 'https://olympics.com/en/paris-2024/medals/japan', 'score': 0.9875203, 'images': []}, page_content="Official medal standings for the Paris 2024 Olympics (Jul 26-Aug 11, 2024). Find out which athletes are bringing home medals and breaking records. ... Medal Table - Japan. Medal Table Medallists # NOCs. G. S. B. CHN People's Republic of China. 37 27 24 88. USA United States of America. 35 41 41 117. AUS Australia. 18 17 14 49. JPN Japan. 16 12 ...")]
Thought:
Python REPL can execute arbitrary code. Use with caution.
Based on the search results, Japan is projected to win 13 gold medals at the 2024 Paris Olympics. Now, I will calculate the square of that number.

Action:
```
{
  "action": "python_repl",
  "action_input": "print(13 ** 2)"
}
```

Observation: 169

Thought:Based on the search results, Japan is projected to win 13 gold medals at the 2024 Paris Olympics. The square of that number is 169.

Action:
```
{
  "action": "Final Answer",
  "action_input": "Japan is projected to win 13 gold medals at the 2024 Paris Olympics. The square of 13 is 169."
}
```

> Finished chain.

{'input': 'Please search and tell me how many gold medals Japan will win at the 2024 Paris Olympics. Also, please tell me the square of that number.',
 'output': 'Japan is projected to win 13 gold medals at the 2024 Paris Olympics. The square of 13 is 169.'}

オリンピックが現在進行中のためか、検索元の情報が現在の情報に追い付いていない場合、リアルタイムの正確性は落ちる場合があります。
あと、日本語でプロンプトを入れるとchainが途中で止まることがあります。英語で入力すると止まることがないようですが。。

とりあえず、ニッポンがんばれ!!

この記事が気に入ったらサポートをしてみませんか?