Skip to content

Commit 8fa5c64

Browse files
authored
Merge pull request #40 from ut-code/copilot/fix-43024d18-e3cd-4db1-84f6-82adfcff23f6
Migrate from deprecated gemini-1.5-flash and @google/generative-ai to current packages
2 parents af9f2b3 + dc02868 commit 8fa5c64

File tree

5 files changed

+206
-20
lines changed

5 files changed

+206
-20
lines changed

app/actions/chatActions.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ ${userQuestion}
4747
4848
`;
4949
const result = await generateContent(prompt);
50-
const response = result.response;
51-
const text = response.text();
50+
const text = result.text;
51+
if (!text) {
52+
throw new Error("AIからの応答が空でした");
53+
}
5254
return { response: text, error: null };
5355
} catch (error: unknown) {
5456
console.error("Error calling Generative AI:", error);

app/actions/gemini.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
"use server";
22

3-
import { GoogleGenerativeAI, ModelParams } from "@google/generative-ai";
3+
import { GoogleGenAI } from "@google/genai";
44

55
export async function generateContent(prompt: string) {
6-
const params: ModelParams = {
7-
model: "gemini-1.5-flash",
6+
const params = {
7+
model: "gemini-2.5-flash",
8+
contents: prompt,
89
};
910

10-
const genAI = new GoogleGenerativeAI(process.env.API_KEY!);
11+
const ai = new GoogleGenAI({ apiKey: process.env.API_KEY! });
12+
1113
try {
12-
const model = genAI.getGenerativeModel(params);
13-
return await model.generateContent(prompt);
14+
return await ai.models.generateContent(params);
1415
} catch (e: unknown) {
1516
if (String(e).includes("User location is not supported")) {
16-
const model = genAI.getGenerativeModel(params, {
17-
baseUrl: "https://gemini-proxy.utcode.net",
17+
// For the new API, we can use httpOptions to set a custom baseUrl
18+
const aiWithProxy = new GoogleGenAI({
19+
apiKey: process.env.API_KEY!,
20+
httpOptions: {
21+
baseUrl: "https://gemini-proxy.utcode.net",
22+
},
1823
});
19-
return await model.generateContent(prompt);
24+
return await aiWithProxy.models.generateContent(params);
2025
} else {
2126
throw e;
2227
}

app/actions/questionExample.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ export async function getQuestionExample(
3434
${documentContent}
3535
`;
3636
const result = await generateContent(prompt);
37-
const response = result.response;
38-
const text = response.text();
37+
const text = result.text;
38+
if (!text) {
39+
throw new Error("AIからの応答が空でした");
40+
}
3941
return text.trim().split("\n");
4042
}

package-lock.json

Lines changed: 183 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"dependencies": {
1717
"@fontsource-variable/inconsolata": "^5.2.6",
1818
"@fontsource-variable/noto-sans-jp": "^5.2.6",
19-
"@google/generative-ai": "^0.24.1",
19+
"@google/genai": "^1.21.0",
2020
"@opennextjs/cloudflare": "^1.7.1",
2121
"@xterm/addon-fit": "^0.11.0-beta.115",
2222
"@xterm/xterm": "^5.6.0-beta.115",

0 commit comments

Comments
 (0)