0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

記事投稿キャンペーン 「AI、機械学習」

LangChainのAgentsを使用し天気情報をWebから取得する

Posted at

Chat Completions APIでFunction callingを試すで実装した処理を、LangChainのAgentsで置き換える。
Function callingでは、関数の実行はアプリケーション側で実行し、実行結果をLLMへ返す必要があったが、AgentsではLLMが実行してくれる。
今回はduckduckgoでWeb検索を行うように実装する。

ライブラリインストール

pip install langchain openai

ソースコード

from langchain.agents import AgentType, initialize_agent, load_tools
from langchain.chat_models import ChatOpenAI

chat = ChatOpenAI(
    model="gpt-3.5-turbo",
)
tools = load_tools(["ddg-search"])
agent_chain = initialize_agent(tools, chat, agent=AgentType.OPENAI_FUNCTIONS)

result = agent_chain.run("What's the weather like in Fukuoka?")
print(result)

レスポンス(例)

The weather in Fukuoka is currently sunny with a temperature of 27°C (81°F). The minimum temperature for tonight is expected to be 16°C (61°F). Tomorrow's temperature is forecasted to be warmer with a maximum of 27°C (81°F) and a minimum of 16°C (61°F).

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?