← 返回聊天
新建
删除
Models
gpt5.php
gpt5_file.php
gpt5_mini_file.php
openai_chat.php
Tools
get_time.php
get_weather.php
global_search_messages.php
math.php
memo.php
news.php
search_arxiv.php
search_crossref.php
search_github_code.php
search_pubmed.php
search_semantic_scholar.php
stock_market.php
url.php
<?php declare(strict_types=1); /** * 一个示例“外部信息”工具:用 Open-Meteo 获取天气(无需 API Key) * - 先通过 geocoding 找到经纬度 * - 再拉当前天气 * * 如果你的服务器不能访问外网,这个工具会失败,但不影响其它功能。 */ function http_get_json(string $url, int $timeoutSec = 10): array { $ch = curl_init($url); if ($ch === false) throw new RuntimeException('curl_init failed'); curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_CONNECTTIMEOUT => $timeoutSec, CURLOPT_TIMEOUT => $timeoutSec, CURLOPT_HTTPHEADER => ['Accept: application/json'], ]); $resp = curl_exec($ch); $errno = curl_errno($ch); $err = curl_error($ch); $status = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($errno !== 0) throw new RuntimeException('cURL error: ' . $err); if ($status < 200 || $status >= 300) throw new RuntimeException('HTTP ' . $status); $data = json_decode((string)$resp, true); if (!is_array($data)) throw new RuntimeException('Invalid JSON'); return $data; } return [ 'name' => 'get_weather', 'description' => '根据城市名获取当前天气(Open-Meteo,无需Key)。', 'parameters' => [ 'type' => 'object', 'properties' => [ 'city' => ['type' => 'string', 'description' => '城市名,例如:上海、北京、Shenzhen'], ], 'required' => ['city'], ], 'run' => function(array $args, array $context) { $city = trim((string)$args['city']); if ($city === '') throw new RuntimeException('city 不能为空'); $geoUrl = 'https://geocoding-api.open-meteo.com/v1/search?count=1&language=zh&name=' . rawurlencode($city); $geo = http_get_json($geoUrl, 12); $r = $geo['results'][0] ?? null; if (!is_array($r)) { return ['city' => $city, 'found' => false, 'message' => '未找到城市']; } $lat = $r['latitude']; $lon = $r['longitude']; $name = $r['name'] ?? $city; $country = $r['country'] ?? ''; $admin1 = $r['admin1'] ?? ''; $wxUrl = 'https://api.open-meteo.com/v1/forecast?latitude=' . rawurlencode((string)$lat) . '&longitude=' . rawurlencode((string)$lon) . '¤t=temperature_2m,apparent_temperature,is_day,precipitation,weather_code,wind_speed_10m' . '&timezone=auto'; $wx = http_get_json($wxUrl, 12); $cur = $wx['current'] ?? null; return [ 'found' => true, 'query' => $city, 'place' => trim($name . ' ' . $admin1 . ' ' . $country), 'latitude' => $lat, 'longitude' => $lon, 'current' => $cur, ]; }, ];
保存