Web Search API を使って明日の天気を調べ、 Text to Speech でアナウンサー風に読み上げてみる
こんにちは!クラウド事業本部コンサルティング部のたかくに(@takakuni_)です。
前置き
今まで AWS のアップデートを追い続けていたため、 OpenAI 周りの話に疎いのですが、ついに OpenAI を触る必要が出てきました。N 番煎じ感がすごいですが、 Quickstart を進めていると、 Web search に遭遇しました。
サンプルコードは以下の通りで、
from openai import OpenAI
client = OpenAI()
completion = client.chat.completions.create(
model="gpt-4o-search-preview",
web_search_options={},
messages=[
{
"role": "user",
"content": "What was a positive news story from today?",
}
],
)
print(completion.choices[0].message.content)
実行すると、その日のニュースが出てきました。
% python main.py
As of today, May 10, 2025, a notable positive development is the launch of NASA's Artemis I mission from the Kennedy Space Center in Florida. This unmanned mission marks the inaugural flight of the Space Launch System (SLS) rocket—the most powerful rocket ever built—and the Orion spacecraft, designed to carry humans to the Moon and Mars. The mission aims to test these systems in preparation for future crewed missions, representing a significant milestone in space exploration. ([router.numerologist.com](https://router.numerologist.com/may-10-2025-day/?utm_source=openai))
Additionally, today is recognized as Trust Your Intuition Day, an annual observance encouraging individuals to embrace their instincts and make confident decisions. The day serves as a reminder of the importance of intuition in our daily lives and promotes self-awareness and personal growth. ([nationaltoday.com](https://nationaltoday.com/trust-your-intuition-day/?utm_source=openai))
These events highlight advancements in space exploration and personal development, contributing positively to today's news landscape.
「な、なんだ、これは...!」 と強い衝撃を受けました。
と言うわけで、今回は今更ながら、 Web search について掘り下げていきたいと思います。
Web search
Web search は名前の通り、 Web 検索を行う Tools です。LLM が応答を生成する前に、最新情報を得るためにウェブ検索を行えるよう、ビルドインツールとして提供されています。
Web search は Chat Completions API および、Response API の両方でサポートされています。
Chat Completions API/Response API
OpenAI では、Chat Completions API, Response API の 2 種類の API が用意されており、Chat Completions API のドキュメントを読むに、以下の違いがあるようでした。
- Chat Completions API
- 応答前に常に Web 検索を行う
- 利用可能なモデル
gpt-4o-search-preview
gpt-4o-mini-search-preview
- Response API
- 必要なタイミングで Web 検索を行う
- 利用可能なモデル
gpt-4o
gpt-4o-mini
gpt-4.1
gpt-4.1-mini
When using Chat Completions, the model always retrieves information from the web before responding to your query. To use web_search_preview as a tool that models like
gpt-4o
andgpt-4o-mini
invoke only when necessary, switch to using the Responses API.Currently, you need to use one of these models to use web search in Chat Completions:
- gpt-4o-search-preview
- gpt-4o-mini-search-preview
Web search is currently not supported in the
gpt-4.1-nano
model.
Location
検索結果を最適化するために、次のオプションが提供されています。非常にありがたいですね。
city
やregion
Minneapolis
やMinnesota
のようなフリーテキスト文字列を入力
country
US
のような ISO 3166-1 で定められた国別の識別コードを入力
timezone
America/Chicago
のような IANA timezone を入力
たとえば、東京のニュースを聞きたい時は、次のような指定になります。
from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-4.1",
tools=[{
"type": "web_search_preview",
"user_location": {
"type": "approximate",
"country": "JP",
"city": "Tokyo",
"timezone": "Asia/Tokyo",
}
}],
input="今日の良いニュースを教えてください。"
)
print(response.output_text)
検索コンテキストのサイズ
search_context_size
によって、検索結果のコンテキストサイズを調整できます。
コンテキストサイズは high, mideium, low から選べ、大きくするほど、コンテキストが豊かになり、より正確で包括的な回答が得られます。ただし、応答速度やコストに影響があるため、トレードオフな関係にあります。
from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-4.1",
tools=[{
"type": "web_search_preview",
"user_location": {
"type": "approximate",
"country": "JP",
"city": "Tokyo",
"timezone": "Asia/Tokyo",
},
"search_context_size": "high" # high, medium, low
}],
input="明日の天気を教えてください"
)
print(response.output_text)
参考までに、回答された出力結果の違いを添付します。
明日、5月11日(日)の東京都江戸川区中葛西6丁目13の天気予報は以下の通りです。
## 〒134-0083 東京都江戸川区中葛西6丁目13の天候:
現在の状態: おおむね曇り、71°F (22°C)
時間予測:
* 0:00: 71°F (22°C)、曇り
* 1:00: 70°F (21°C)、曇り
* 2:00: 68°F (20°C)、曇り
* 3:00: 67°F (19°C)、曇り
* 4:00: 65°F (18°C)、曇り
* 5:00: 64°F (18°C)、曇り
* 6:00: 65°F (18°C)、曇り
* 7:00: 67°F (20°C)、もやのかかった晴れ
* 8:00: 70°F (21°C)、もやのかかった晴れ
* 9:00: 71°F (22°C)、もやのかかった晴れ
* 10:00: 73°F (23°C)、もやのかかった晴れ
* 11:00: 75°F (24°C)、もやのかかった晴れ
* 12:00: 76°F (25°C)、もやのかかった晴れ
* 13:00: 78°F (25°C)、もやのかかった晴れ
* 14:00: 76°F (25°C)、もやのかかった晴れ
* 15:00: 76°F (24°C)、もやのかかった晴れ
* 16:00: 75°F (24°C)、もやのかかった晴れ
* 17:00: 74°F (23°C)、もやのかかった晴れ
* 18:00: 72°F (22°C)、もやのかかった晴れ
* 19:00: 70°F (21°C)、曇り
* 20:00: 69°F (21°C)、曇り
* 21:00: 68°F (20°C)、曇り
* 22:00: 68°F (20°C)、曇り
* 23:00: 67°F (19°C)、曇り
悪天候アラート:
* 江戸川区陸上: 強風注意報は、日曜日の午前12:00 (JST)まで続く見込みです。発表者: 気象庁、、開始時刻: 土曜日, 5月 10, 15:00:00 UTC、終了時刻: 日曜日, 5月 11, 00:00:00 UTC
* 江戸川区: 波浪注意報は、日曜日の午前12:00 (JST)まで続く見込みです。発表者: 気象庁、、開始時刻: 土曜日, 5月 10, 15:00:00 UTC、終了時刻: 日曜日, 5月 11, 00:00:00 UTC
なお、強風注意報と波浪注意報が発表されており、日曜日の午前12:00(JST)まで続く見込みです。
明日、5月11日(日)の東京の天気予報は以下の通りです。
## 〒134-0083 東京都江戸川区中葛西6丁目13の天候:
現在の状態: おおむね曇り、71°F (22°C)
時間予測:
* 0:00: 71°F (22°C)、曇り
* 1:00: 70°F (21°C)、曇り
* 2:00: 68°F (20°C)、曇り
* 3:00: 67°F (19°C)、曇り
* 4:00: 65°F (18°C)、曇り
* 5:00: 64°F (18°C)、曇り
* 6:00: 65°F (18°C)、曇り
* 7:00: 67°F (20°C)、もやのかかった晴れ
* 8:00: 70°F (21°C)、もやのかかった晴れ
* 9:00: 71°F (22°C)、もやのかかった晴れ
* 10:00: 73°F (23°C)、もやのかかった晴れ
* 11:00: 75°F (24°C)、もやのかかった晴れ
* 12:00: 76°F (25°C)、もやのかかった晴れ
* 13:00: 78°F (25°C)、もやのかかった晴れ
* 14:00: 76°F (25°C)、もやのかかった晴れ
* 15:00: 76°F (24°C)、もやのかかった晴れ
* 16:00: 75°F (24°C)、もやのかかった晴れ
* 17:00: 74°F (23°C)、もやのかかった晴れ
* 18:00: 72°F (22°C)、もやのかかった晴れ
* 19:00: 70°F (21°C)、曇り
* 20:00: 69°F (21°C)、曇り
* 21:00: 68°F (20°C)、曇り
* 22:00: 68°F (20°C)、曇り
* 23:00: 67°F (19°C)、曇り
悪天候アラート:
* 江戸川区陸上: 強風注意報は、日曜日の午前12:00 (JST)まで続く見込みです。発表者: 気象庁、、開始時刻: 土曜日, 5月 10, 15:00:00 UTC、終了時刻: 日曜日, 5月 11, 00:00:00 UTC
* 江戸川区: 波浪注意報は、日曜日の午前12:00 (JST)まで続く見込みです。発表者: 気象庁、、開始時刻: 土曜日, 5月 10, 15:00:00 UTC、終了時刻: 日曜日, 5月 11, 00:00:00 UTC
なお、江戸川区では強風注意報と波浪注意報が発表されており、日曜日の午前12:00(JST)まで続く見込みです。
最新の情報や詳細は、気象庁の公式サイトや信頼できる天気予報サービスをご確認ください。
明日、5月11日(日)の東京の天気予報は以下の通りです。
## Weather for Tokyo, Japan:
Current Conditions: Mostly cloudy, 71°F (22°C)
Hourly Forecast:
* 12:00 AM: 68°F (20°C), Cloudy
* 1:00 AM: 67°F (20°C), Cloudy
* 2:00 AM: 66°F (19°C), Cloudy
* 3:00 AM: 65°F (18°C), Cloudy
* 4:00 AM: 64°F (18°C), Cloudy
* 5:00 AM: 62°F (17°C), Cloudy
* 6:00 AM: 64°F (18°C), Cloudy
* 7:00 AM: 66°F (19°C), Hazy sunshine
* 8:00 AM: 69°F (20°C), Hazy sunshine
* 9:00 AM: 71°F (22°C), Hazy sunshine
* 10:00 AM: 73°F (23°C), Hazy sunshine
* 11:00 AM: 75°F (24°C), Hazy sunshine
* 12:00 PM: 76°F (24°C), Hazy sunshine
* 1:00 PM: 76°F (25°C), Hazy sunshine
* 2:00 PM: 79°F (26°C), Hazy sunshine
* 3:00 PM: 76°F (25°C), Hazy sunshine
* 4:00 PM: 76°F (24°C), Hazy sunshine
* 5:00 PM: 74°F (24°C), Hazy sunshine
* 6:00 PM: 72°F (22°C), Hazy sunshine
* 7:00 PM: 71°F (21°C), Cloudy
* 8:00 PM: 69°F (20°C), Cloudy
* 9:00 PM: 68°F (20°C), Cloudy
* 10:00 PM: 67°F (20°C), Cloudy
* 11:00 PM: 65°F (18°C), Rain
日中は高い雲を通して日差しがあり、湿度も低く、快適な一日となるでしょう。
注意事項
データの保持規制
Web Search で利用される情報は以下のドキュメントのとおり、HIPAA および BAA の対象外です。
Web Search
Web Search is not HIPAA eligible and is not covered by a BAA.
米国の医療データを扱う場合や、米国の医療業界向けサービスを開発する場合はお気をつけください。
コンテキストウィンドウ
GPT 4.1 はコンテキストウィンドウが 100 万トークンと大きめなコンテキストを扱えるのがウリですが、Web Search を利用する場合は、128,000 に制限されます。この辺りも注意点として、押さえておきましょう。
Web search is limited to a context window size of 128000 (even with gpt-4.1 and gpt-4.1-mini models).
ニュースキャスターみたいに呼んでもらった
OpenAI では Text to Speech(TTS)のモデルをサポートしています。先ほど Web Search で調べた内容を、音声出力してもらいました。
from openai import OpenAI
from pathlib import Path
client = OpenAI()
response = client.responses.create(
model="gpt-4.1",
tools=[{
"type": "web_search_preview",
"user_location": {
"type": "approximate",
"country": "JP",
"city": "Tokyo",
"timezone": "Asia/Tokyo",
},
"search_context_size": "high" # high, medium, low
}],
input="明日の天気を教えてください。",
instructions="""
Format your response as a natural, continuous spoken narration in Japanese, suitable for text-to-speech.
Avoid using lists, headings, bullet points, or mixing English and Japanese.
Present all information in clear, complete Japanese sentences as if you were a professional weather announcer.
If your initial response contains lists or unnatural formatting, please immediately rephrase it into natural, spoken Japanese.
""",
)
print(response.output_text)
speech_file_path = Path(__file__).parent / "speech.mp3"
with client.audio.speech.with_streaming_response.create(
model="gpt-4o-mini-tts",
voice="coral",
input=response.output_text,
instructions="""
Accent: Standard Japanese (Tokyo dialect), with clear and neutral pronunciation.
Emotional range: Narrow; avoid excessive emotional expression. Focus on calmness and reassurance.
Intonation: Moderate and natural intonation. Slightly emphasize important information to avoid monotony and improve clarity.
Impressions: Deliver a trustworthy, calm, and approachable impression.
Speed of speech: Neither too slow nor too fast; standard to slightly slower pace (speech rate: 1.0–1.1x).
Tone: Sincere, polite, and reassuring. Balance accuracy with listener comfort.
Whispering: Do not use whispering; speak at a normal, clear volume.
""",
) as response:
response.stream_to_file(speech_file_path)
インストラクション部分はフォローが必要でしたが、うまくアナウンスできていますね。
まとめ
「Web Search API を使って明日の天気を調べ、 Text to Speech でアナウンサー風に読み上げてみる」でした。主に Web Search 側の検証でしたが、非常に便利で可能性を感じました。他にどんな API があるのか、どんどん検証してみたいと思います。
クラウド事業本部コンサルティング部のたかくに(@takakuni_)でした!