Initial commit: EGBE chatbot template
Stripped from vercel/chatbot (Apache 2.0): - Dropped @vercel/* packages and AI Gateway - Removed artifacts feature (code/text/sheet/image side panel) - Switched AI provider to @ai-sdk/openai-compatible -> EGBE LiteLLM - Replaced Vercel Blob upload with data URLs - Dropped Redis resumable streams and rate limiter (in-memory now) - Added Dockerfile (Next.js standalone) + entrypoint that runs migrations - Wired DATABASE_URL, EGBE_AI_API_URL/KEY, NEXT_PUBLIC_BASE_URL for app-deploy.sh
This commit is contained in:
commit
3e21c2334c
129 changed files with 21913 additions and 0 deletions
78
lib/ai/tools/get-weather.ts
Normal file
78
lib/ai/tools/get-weather.ts
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
import { tool } from "ai";
|
||||
import { z } from "zod";
|
||||
|
||||
async function geocodeCity(
|
||||
city: string
|
||||
): Promise<{ latitude: number; longitude: number } | null> {
|
||||
try {
|
||||
const response = await fetch(
|
||||
`https://geocoding-api.open-meteo.com/v1/search?name=${encodeURIComponent(city)}&count=1&language=en&format=json`
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (!data.results || data.results.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const result = data.results[0];
|
||||
return {
|
||||
latitude: result.latitude,
|
||||
longitude: result.longitude,
|
||||
};
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export const getWeather = tool({
|
||||
description:
|
||||
"Get the current weather at a location. You can provide either coordinates or a city name.",
|
||||
inputSchema: z.object({
|
||||
latitude: z.number().optional(),
|
||||
longitude: z.number().optional(),
|
||||
city: z
|
||||
.string()
|
||||
.describe("City name (e.g., 'San Francisco', 'New York', 'London')")
|
||||
.optional(),
|
||||
}),
|
||||
execute: async (input) => {
|
||||
let latitude: number;
|
||||
let longitude: number;
|
||||
|
||||
if (input.city) {
|
||||
const coords = await geocodeCity(input.city);
|
||||
if (!coords) {
|
||||
return {
|
||||
error: `Could not find coordinates for "${input.city}". Please check the city name.`,
|
||||
};
|
||||
}
|
||||
latitude = coords.latitude;
|
||||
longitude = coords.longitude;
|
||||
} else if (input.latitude !== undefined && input.longitude !== undefined) {
|
||||
latitude = input.latitude;
|
||||
longitude = input.longitude;
|
||||
} else {
|
||||
return {
|
||||
error:
|
||||
"Please provide either a city name or both latitude and longitude coordinates.",
|
||||
};
|
||||
}
|
||||
|
||||
const response = await fetch(
|
||||
`https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}¤t=temperature_2m&hourly=temperature_2m&daily=sunrise,sunset&timezone=auto`
|
||||
);
|
||||
|
||||
const weatherData = await response.json();
|
||||
|
||||
if ("city" in input) {
|
||||
weatherData.cityName = input.city;
|
||||
}
|
||||
|
||||
return weatherData;
|
||||
},
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue