Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
babaohuang committed Dec 14, 2023
1 parent 4b3b167 commit f2b4b6e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
19 changes: 17 additions & 2 deletions src/pages/api/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ export const post: APIRoute = async(context) => {
const body = await context.request.json()
const { sign, time, messages, pass } = body

if (!messages || messages.length === 0 || messages[messages.length - 1].role !== 'user') {
return new Response(JSON.stringify({
error: {
message: 'Invalid message history: The last message must be from user role.',
},
}), { status: 400 })
}

if (!messages) {
return new Response(JSON.stringify({
error: {
Expand All @@ -34,12 +42,19 @@ export const post: APIRoute = async(context) => {
}

try {
const result = await sendMessage(messages)
const history = messages.slice(0, -1) // All messages except the last one
const newMessage = messages[messages.length - 1].parts.map(part => part.text).join('')

// Start chat and send message with streaming
const stream = await startChatAndSendMessageStream(history, newMessage)

// Handle the stream
let text = ''
for await (const chunk of result.stream) {
for await (const chunk of stream) {
const chunkText = chunk.text()
text += chunkText
}

return new Response(JSON.stringify({ text }), { status: 200 })
} catch (error) {
console.error(error)
Expand Down
16 changes: 7 additions & 9 deletions src/utils/openAI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@ import { GoogleGenerativeAI } from '@google/generative-ai'
const apiKey = process.env.GEMINI_API_KEY
const genAI = new GoogleGenerativeAI(apiKey)

export const sendMessage = async(messages: ChatMessage[]) => {
export const startChatAndSendMessageStream = async(history: ChatMessage[], newMessage: string) => {
const model = genAI.getGenerativeModel({ model: 'gemini-pro' })

const chat = model.startChat({
history: messages.map(msg => ({
history: history.map(msg => ({
role: msg.role,
parts: msg.parts.map(part => part.text),
parts: msg.parts.map(part => part.text).join(''), // Join parts into a single string
})),
generationConfig: {
maxOutputTokens: 4000, // or your desired token limit
maxOutputTokens: 100,
},
})

const lastMessage = messages[messages.length - 1]
const lastMessagelog = lastMessage.parts.map(part => part.text).join('')
console.log('Sending message:', lastMessagelog)
const result = await chat.sendMessageStream(lastMessage.parts.map(part => part.text).join(''))
return result
// Use sendMessageStream for streaming responses
const result = await chat.sendMessageStream(newMessage)
return result.stream
}

0 comments on commit f2b4b6e

Please sign in to comment.