This commit is contained in:
lik
2026-06-01 15:48:50 +08:00
parent 24b56b1875
commit 40c45c6db7
51 changed files with 2079 additions and 9 deletions

View File

@@ -0,0 +1,40 @@
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("城市名称,例如:北京市海淀区")
})
}
)