Refactor NameToEmoji function to use Google Generative AI and update error handling
This commit is contained in:
parent
6fbfd5364e
commit
ed3b1575b3
3 changed files with 28 additions and 28 deletions
|
|
@ -6,6 +6,7 @@
|
|||
"name": "app",
|
||||
"dependencies": {
|
||||
"@azure-rest/ai-inference": "^1.0.0-beta.6",
|
||||
"@google/generative-ai": "^0.24.1",
|
||||
"@imnyang/comcigan.ts": "^0.3.0",
|
||||
},
|
||||
"devDependencies": {
|
||||
|
|
@ -35,6 +36,8 @@
|
|||
|
||||
"@azure/logger": ["@azure/logger@1.3.0", "", { "dependencies": { "@typespec/ts-http-runtime": "^0.3.0", "tslib": "^2.6.2" } }, "sha512-fCqPIfOcLE+CGqGPd66c8bZpwAji98tZ4JI9i/mlTNTlsIWslCfpg48s/ypyLxZTump5sypjrKn2/kY7q8oAbA=="],
|
||||
|
||||
"@google/generative-ai": ["@google/generative-ai@0.24.1", "", {}, "sha512-MqO+MLfM6kjxcKoy0p1wRzG3b4ZZXtPI+z2IE26UogS2Cm/XHO+7gGRBh6gcJsOiIVoH93UwKvW4HdgiOZCy9Q=="],
|
||||
|
||||
"@imnyang/comcigan.ts": ["@imnyang/comcigan.ts@0.3.0", "", { "dependencies": { "iconv-lite": "^0.6.3", "undici": "^6.23.0" } }, "sha512-IqOoqsrziSOZe0vUBvVjCysv8Ydz5uYBoSBiBtX3eopR2b+Em5W1C7mRJwrMa/9Tt2bgZxn30F3yq7w5yJzEdg=="],
|
||||
|
||||
"@types/bun": ["@types/bun@1.3.10", "", { "dependencies": { "bun-types": "1.3.10" } }, "sha512-0+rlrUrOrTSskibryHbvQkDOWRJwJZqZlxrUs1u4oOoTln8+WIXBPmAuCF35SWB2z4Zl3E84Nl/D0P7803nigQ=="],
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import ModelClient, { isUnexpected } from "@azure-rest/ai-inference";
|
||||
import { AzureKeyCredential } from "@azure/core-auth";
|
||||
import { GoogleGenerativeAI } from "@google/generative-ai";
|
||||
|
||||
const KEY = process.env.NEIS_API_KEY;
|
||||
|
||||
|
|
@ -45,19 +44,14 @@ export async function getMealInfo(MLSV_YMD: string, ATPT_OFCDC_SC_CODE: string,
|
|||
};
|
||||
|
||||
}
|
||||
|
||||
export async function NameToEmoji(name: string): Promise<string> {
|
||||
const token = process.env.GITHUB_TOKEN;
|
||||
if (!token) {
|
||||
throw new Error("GITHUB_TOKEN environment variable is not set.");
|
||||
export async function NameToEmoji(name: string): Promise<string | undefined> {
|
||||
const apiKey = process.env.GOOGLE_API_KEY;
|
||||
if (!apiKey) {
|
||||
throw new Error("GOOGLE_API_KEY environment variable is not set.");
|
||||
}
|
||||
const endpoint = "https://models.github.ai/inference";
|
||||
const model = "openai/gpt-5-mini";
|
||||
|
||||
const client = ModelClient(
|
||||
endpoint,
|
||||
new AzureKeyCredential(token),
|
||||
);
|
||||
const genAI = new GoogleGenerativeAI(apiKey);
|
||||
const model = genAI.getGenerativeModel({ model: "gemini-3.1-flash-lite-preview" });
|
||||
|
||||
const systemPrompt = `⚠️ 중요한 지침: 당신은 오직 이모지로만 응답하는 AI입니다. 다음 규칙을 예외 없이 철저히 준수해야 합니다. ⚠️
|
||||
|
||||
|
|
@ -105,24 +99,26 @@ A: 🥰,☮️,🗽
|
|||
|
||||
`;
|
||||
|
||||
const response = await client.path("/chat/completions").post({
|
||||
body: {
|
||||
messages: [
|
||||
{ role: "system", content: systemPrompt },
|
||||
{ role: "user", content: name }
|
||||
const response = await model.generateContent({
|
||||
contents: [
|
||||
{ role: "user", parts: [{ text: systemPrompt + "\n\nUser input: " + name }] }
|
||||
],
|
||||
generationConfig: {
|
||||
temperature: 1.0,
|
||||
top_p: 1.0,
|
||||
model: model
|
||||
}
|
||||
topP: 1.0,
|
||||
candidateCount: 1,
|
||||
},
|
||||
});
|
||||
if (isUnexpected(response)) {
|
||||
throw response.body.error;
|
||||
}
|
||||
|
||||
const choices = response.body?.choices;
|
||||
if (!choices || !choices[0]?.message?.content) {
|
||||
const result = response.response;
|
||||
if (!result.candidates || !result.candidates[0]) {
|
||||
throw new Error("No valid response from the model.");
|
||||
}
|
||||
return choices[0].message.content as string;
|
||||
|
||||
const textContent = result.candidates[0].content.parts[0];
|
||||
if (!textContent) {
|
||||
throw new Error("Invalid response format from the model.");
|
||||
}
|
||||
|
||||
return textContent.text;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@
|
|||
},
|
||||
"dependencies": {
|
||||
"@azure-rest/ai-inference": "^1.0.0-beta.6",
|
||||
"@google/generative-ai": "^0.24.1",
|
||||
"@imnyang/comcigan.ts": "^0.3.0"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue