Files
api_health/agent/escort/tools/geo/latlon.js
2026-06-01 15:48:50 +08:00

41 lines
1.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import * as z from "zod"
import { tool } from "langchain"
/**
* 获取城市经纬度工具
* @param {string} city - 城市名称
* @returns {Object|null} - 返回经纬度对象 {lat, lng}如果未找到则返回null
*/
export const getLatLngTool = tool(
async (input) => {
const { city } = input
try {
// 使用open-meteo地理编码API获取经纬度
console.log('获取城市经纬度:', city)
const url = `https://geocoding-api.open-meteo.com/v1/search?name=${encodeURIComponent(city)}&language=zh&count=1`
const res = await fetch(url)
const data = await res.json()
if (data.results?.length) {
return {
lat: data.results[0].latitude,
lng: data.results[0].longitude
}
}
// 如果没有找到结果返回null
return null
} catch (error) {
console.error('地理编码API调用失败:', error)
return null
}
},
{
name: "get_lat_lng",
description: `根据城市名称获取经纬度信息`,
schema: z.object({
city: z.string().describe("城市名称,例如:北京市海淀区")
})
}
)