Skip to content

Commit

Permalink
chore: add worldbank gdp
Browse files Browse the repository at this point in the history
  • Loading branch information
yutakobayashidev committed Oct 19, 2023
1 parent 3829f70 commit 410f898
Show file tree
Hide file tree
Showing 5 changed files with 567 additions and 4 deletions.
48 changes: 48 additions & 0 deletions frontend/src/app/api/chat/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,21 @@ export const functions: ChatCompletionCreateParams.Function[] = [
type: "object",
},
},
{
name: "get_gdp",
description:
"Search the World Bank for the most current GDP data at this time by country.",
parameters: {
properties: {
country_code: {
description: "ISO 3166-1 alpha-3 format code for country name.",
type: "string",
},
},
required: ["country_code"],
type: "object",
},
},
{
name: "speech_list",
description:
Expand Down Expand Up @@ -120,6 +135,37 @@ async function get_population(countryCode: string) {
}
}

async function get_gdp(countryCode: string) {
try {
const response = await fetch(
`https://api.worldbank.org/v2/country/${countryCode}/indicator/NY.GDP.MKTP.CD?format=json`
);
const result = await response.json();
const data = result[1];

if (!data) {
return "申し訳ありませんが、国番号に対応するデータがないため、GDPデータを取得できませんでした。";
}

const transformedData: TransformedData[] = data.map((datum: any) => {
return {
country_id: datum.country.id,
country_value: datum.country.value,
date: datum.date,
indicator_id: datum.indicator.id,
indicator_value: datum.indicator.value,
value: datum.value,
};
});

transformedData.sort((a, b) => parseInt(a.date) - parseInt(b.date));

return transformedData;
} catch (e: any) {
return `申し訳ありませんが、エラーによりGDPデータを取得できませんでした`;
}
}

async function get_member_info(name: string) {
const query = "SELECT * FROM Member WHERE name = ? LIMIT 1";
const params = [name];
Expand Down Expand Up @@ -197,6 +243,8 @@ export async function runFunction(name: string, args: any) {
return await get_member_info(args["name"]);
case "get_population":
return await get_population(args["country_code"]);
case "get_gdp":
return await get_gdp(args["country_code"]);
case "meeting_list":
return await meeting_list(args);
case "speech_list":
Expand Down
Loading

1 comment on commit 410f898

@vercel
Copy link

@vercel vercel bot commented on 410f898 Oct 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.