1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

OpenHands でエージェント AI を体験

Posted at

image.png

OpenHands とは?

OpenHands (GitHub) は、LLMベースでエージェントAIを組めるOSSフレームワークです。

ポイント:

  • モジュール単位で機能追加できる
  • Memory や外部 API との統合が簡単
  • OSS なので自由にカスタマイズ可能

実験

「天気に合わせた提案をしてくれるエージェントAI」をさくっとつくります。

  • 入力:都市名(例: Tokyo)
  • 出力:
    • 現在の天気(OpenWeatherMapから取得)
    • 天気に応じたアクティビティ提案

Step 1: 環境構築

git clone https://github.com/All-Hands-AI/OpenHands.git
cd OpenHands
python -m venv venv
source venv/bin/activate  # Windowsは venv\Scripts\activate
pip install -e .

動作確認用にサンプルを実行します。

python examples/openai_agent.py

APIキーを .env に追記:

OPENAI_API_KEY=sk-xxxx

Step 2: モジュール理解

OpenHandsは「モジュール」を追加してエージェントを拡張します。

例:

pip install openhands-modules-toolsearch

ほかにも:

  • openhands-modules-arxiv
  • openhands-modules-memory
  • openhands-modules-serper

今回は 自作モジュール を作って天気APIを呼び出します。

Step 3: 天気APIツールを自作

APIキー準備

  1. OpenWeatherMap でキーを取得
  2. .env に追加:
OPENWEATHER_API_KEY=your_api_key

モジュールの準備

modules/weather_tool.py
import os
import requests
from openhands.modules.base import ToolModule

class WeatherTool(ToolModule):
    def __init__(self):
        super().__init__(name="weather_tool", description="Get weather info for a city")

    def run(self, input_text: str) -> str:
        city = input_text.strip()
        key = os.getenv("OPENWEATHER_API_KEY")
        res = requests.get(f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={key}&units=metric")
        data = res.json()
        temp = data["main"]["temp"]
        weather = data["weather"][0]["description"]
        return f"{city}の現在の天気は{weather}、気温は{temp}℃です。"

Step 4: エージェント構成

my_agent.py
from openhands.agents.base import BaseAgent
from openhands.prompts.base import Prompt
from weather_tool import WeatherTool

prompt = Prompt(
    system="あなたは天気予報士です。天気を元におすすめアクティビティを提案してください。",
    user="{input}"
)

agent = BaseAgent(
    prompt=prompt,
    tools=[WeatherTool()]
)

agent.run("Tokyo")

Step 5: 実行

python my_agent.py

出力:

Tokyoの現在の天気はclear sky、気温は22℃です。
今日は晴れているので、公園でのんびり過ごすのがおすすめです。

試してみて

15分~20分程度で「自分専用のエージェントAI」を形にできました。モジュール化されているので外部APIの組み込みが簡単です。

少し応用して以下のようなものもできます:

  • Streamlit/Gradio で UI 化
  • Memory で過去の会話保持
  • 複数ツール連携でマルチスキル化

参考リンク

  • ドキュメント

  • OpenWeatherMap

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?