Skip to content

Commit

Permalink
Merge pull request #11 from e-roy:update-streaming
Browse files Browse the repository at this point in the history
update streaming
  • Loading branch information
e-roy authored Dec 26, 2023
2 parents d0e29d8 + 11bee95 commit 0e94a56
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 94 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"@radix-ui/react-label": "^2.0.2",
"@radix-ui/react-slider": "^1.1.2",
"@radix-ui/react-slot": "^1.0.2",
"ai": "^2.2.29",
"ai": "^2.2.30",
"class-variance-authority": "^0.7.0",
"clsx": "^2.0.0",
"lucide-react": "^0.297.0",
Expand Down
69 changes: 23 additions & 46 deletions src/app/api/gemini-pro/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// api/gemini/route.ts
import { Message } from "ai";
import { GoogleGenerativeAIStream, Message, StreamingTextResponse } from "ai";

import {
GoogleGenerativeAI,
Expand Down Expand Up @@ -87,53 +87,30 @@ export async function POST(req: Request) {
];

const genAI = new GoogleGenerativeAI(process.env.GOOGLE_API_KEY as string);
const model = genAI.getGenerativeModel({
model: "gemini-pro",
safetySettings: mappedSafetySettings,
generationConfig: {
// candidateCount: 0,
// stopSequences: [],
maxOutputTokens: maxLength,
temperature,
topP,
topK,
},
});

const countTokens = await model.countTokens(reqContent);
console.log("count tokens ------", countTokens);

try {
const streamingResp = await model.generateContentStream(reqContent);
const tokens = await genAI
.getGenerativeModel({
model: "gemini-pro",
})
.countTokens(reqContent);
console.log("count tokens ------", tokens);

const stream = new ReadableStream({
async start(controller) {
try {
for await (const chunk of streamingResp.stream) {
if (chunk.candidates) {
const parts = chunk.candidates[0].content.parts;
const firstPart = parts[0];
if (typeof firstPart.text === "string") {
// Encode the string text as bytes
const textEncoder = new TextEncoder();
const encodedText = textEncoder.encode(firstPart.text);
controller.enqueue(encodedText);
}
}
}
controller.close();
} catch (error) {
console.error("Streaming error:", error);
controller.error(error);
}
const geminiStream = await genAI
.getGenerativeModel({
model: "gemini-pro",
safetySettings: mappedSafetySettings,
generationConfig: {
// candidateCount: 0,
// stopSequences: [],
maxOutputTokens: maxLength,
temperature,
topP,
topK,
},
});
})
.generateContentStream(reqContent);

return new Response(stream, {
headers: { "Content-Type": "text/plain" },
});
} catch (error) {
console.error("API error:", error);
return new Response("Internal Server Error", { status: 500 });
}
const stream = GoogleGenerativeAIStream(geminiStream);

return new StreamingTextResponse(stream);
}
61 changes: 18 additions & 43 deletions src/app/api/gemini-vision/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// api/gemini-vision/route.ts
import { GoogleGenerativeAIStream, StreamingTextResponse } from "ai";

import { GeneralSettings } from "@/types";
import {
GoogleGenerativeAI,
Expand Down Expand Up @@ -86,49 +88,22 @@ export async function POST(req: Request) {

const genAI = new GoogleGenerativeAI(process.env.GOOGLE_API_KEY as string);

const model = genAI.getGenerativeModel({
model: "gemini-pro-vision",
safetySettings: mappedSafetySettings,
generationConfig: {
// candidateCount: 0,
// stopSequences: [],
maxOutputTokens: maxLength,
temperature,
topP,
topK,
},
});

try {
const streamingResp = await model.generateContentStream(reqContent);

const stream = new ReadableStream({
async start(controller) {
try {
for await (const chunk of streamingResp.stream) {
if (chunk.candidates) {
const parts = chunk.candidates[0].content.parts;
const firstPart = parts[0];
if (typeof firstPart.text === "string") {
const textEncoder = new TextEncoder();
const encodedText = textEncoder.encode(firstPart.text);
controller.enqueue(encodedText);
}
}
}
controller.close();
} catch (error) {
console.error("Streaming error:", error);
controller.error(error);
}
const geminiStream = await genAI
.getGenerativeModel({
model: "gemini-pro-vision",
safetySettings: mappedSafetySettings,
generationConfig: {
// candidateCount: 0,
// stopSequences: [],
maxOutputTokens: maxLength,
temperature,
topP,
topK,
},
});
})
.generateContentStream(reqContent);

return new Response(stream, {
headers: { "Content-Type": "text/plain" },
});
} catch (error) {
console.error("API error:", error);
return new Response("Internal Server Error", { status: 500 });
}
const stream = GoogleGenerativeAIStream(geminiStream);

return new StreamingTextResponse(stream);
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -732,10 +732,10 @@ agent-base@^7.0.2:
dependencies:
debug "^4.3.4"

ai@^2.2.29:
version "2.2.29"
resolved "https://registry.yarnpkg.com/ai/-/ai-2.2.29.tgz#a0522aff58be764c2d9c18c76806b9a8101194c2"
integrity sha512-/zzSTTKF5LxMGQuNVUnNjs7X6PWYfb6M88Zn74gCUnM3KCYgh0CiAWhLyhKP6UtK0H5mHSmXgt0ZkZYUecRp0w==
ai@^2.2.30:
version "2.2.30"
resolved "https://registry.yarnpkg.com/ai/-/ai-2.2.30.tgz#3b0af50859b44cfc7e168c170974a93637e90792"
integrity sha512-7dRgnEbYkbVjxyjiS7WEhNvO8ebeI4Om74D9OKXLK0yis4+s272pJ5I3vOAv3HaUBbVEiIFYQ7E34JH8XT1EeQ==
dependencies:
eventsource-parser "1.0.0"
nanoid "3.3.6"
Expand Down

0 comments on commit 0e94a56

Please sign in to comment.