Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update openAI.ts #124

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 31 additions & 32 deletions src/utils/openAI.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
import { GoogleGenerativeAI } from '@fuyun/generative-ai'
'@fuyun/generative-ai'导入{ GoogleGenerativeAI }

const apiKey = (import.meta.env.GEMINI_API_KEY)
const apiBaseUrl = (import.meta.env.API_BASE_URL)?.trim().replace(/\/$/, '')
const apiKey = ( import .meta .env .GEMINI_API_KEY ) _ _ _
const apiBaseUrl = ( import .meta .env .API_BASE_URL ) ? . _ _ 修剪()。替换( / \/ $ / , '' )

const genAI = apiBaseUrl
? new GoogleGenerativeAI(apiKey, apiBaseUrl)
: new GoogleGenerativeAI(apiKey)
const genAI = apiBaseUrl
?新的 GoogleGenerativeAI ( apiKey , apiBaseUrl )
:新的 GoogleGenerativeAI ( apiKey )

export const startChatAndSendMessageStream = async(history: ChatMessage[], newMessage: string) => {
const model = genAI.getGenerativeModel({ model: 'gemini-pro' })
导出 const startChatAndSendMessageStream = async (历史记录: ChatMessage [ ] , newMessage : string ) => {
常量 模型 = genAIgetGenerativeModel ( { 模型: 'gemini-pro' } )

const chat = model.startChat({
history: history.map(msg => ({
role: msg.role,
parts: msg.parts.map(part => part.text).join(''), // Join parts into a single string
})),
generationConfig: {
maxOutputTokens: 8000,
},
})
常量 聊天 = 模型。开始聊天({
历史:历史。地图(味精 => ( {
作用:味精。角色,
部分:味精。部分。地图(部分 => 部分.文本)。join ( '' ) , // 将各个部分连接成一个字符串
} ) ) ,
生成配置:{
最大输出令牌:8000
} ,
} )

// Use sendMessageStream for streaming responses
const result = await chat.sendMessageStream(newMessage)
// 使用 sendMessageStream 来传输响应
const 结果 = 等待 聊天。发送消息流(新消息)

const encodedStream = new ReadableStream({
async start(controller) {
const encoder = new TextEncoder()
for await (const chunk of result.stream) {
const text = await chunk.text()
const encoded = encoder.encode(text)
controller.enqueue(encoded)
}
controller.close()
},
})
const 编码流 = 新的 ReadableStream ( {
异步 启动(控制器) {
const 编码器 = new TextEncoder ( )
for等待(结果的常量块.stream){_for await ( const chunk of result .stream ) { _
const text = 等待块。文本()const text = 等待 块。文本()
常量编码 = 编码器。编码(文本)
控制器。入队(编码)
}}
控制器。关闭()
} ,} ,
} )} )

return encodedStream
}
返回编码流