-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { GoogleGenerativeAI, ChatSession } from '@google/generative-ai' | ||
import { AbstractBot, AsyncAbstractBot, SendMessageParams } from '../abstract-bot' | ||
import { getUserConfig } from '~services/user-config' | ||
|
||
interface ConversationContext { | ||
chatSession: ChatSession | ||
} | ||
|
||
export class GeminiApiBot extends AbstractBot { | ||
private conversationContext?: ConversationContext | ||
sdk: GoogleGenerativeAI | ||
|
||
constructor(public apiKey: string) { | ||
super() | ||
this.sdk = new GoogleGenerativeAI(apiKey) | ||
} | ||
|
||
async doSendMessage(params: SendMessageParams) { | ||
if (!this.conversationContext) { | ||
const model = this.sdk.getGenerativeModel({ model: 'gemini-pro' }) | ||
const chatSession = model.startChat() | ||
this.conversationContext = { chatSession } | ||
} | ||
|
||
const result = await this.conversationContext.chatSession.sendMessageStream(params.prompt) | ||
|
||
let text = '' | ||
for await (const chunk of result.stream) { | ||
const chunkText = chunk.text() | ||
console.debug('gemini stream', chunkText) | ||
text += chunkText | ||
params.onEvent({ type: 'UPDATE_ANSWER', data: { text } }) | ||
} | ||
|
||
if (!text) { | ||
params.onEvent({ type: 'UPDATE_ANSWER', data: { text: 'Empty response' } }) | ||
} | ||
params.onEvent({ type: 'DONE' }) | ||
} | ||
|
||
resetConversation() { | ||
this.conversationContext = undefined | ||
} | ||
|
||
get name() { | ||
return 'Gemini Pro' | ||
} | ||
} | ||
|
||
export class GeminiBot extends AsyncAbstractBot { | ||
async initializeBot() { | ||
const { geminiApiKey } = await getUserConfig() | ||
if (!geminiApiKey) { | ||
throw new Error('Gemini API key missing') | ||
} | ||
return new GeminiApiBot(geminiApiKey) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters