-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: dogi <[email protected]>
- Loading branch information
Showing
19 changed files
with
303 additions
and
56 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,9 @@ | ||
import { GoogleGenerativeAI } from '@google/generative-ai'; | ||
import dotenv from 'dotenv'; | ||
|
||
dotenv.config(); | ||
|
||
const gemini = new GoogleGenerativeAI(process.env.GEMINI_API_KEY || ''); | ||
|
||
export default gemini; | ||
|
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,6 @@ | ||
export type ProviderName = 'openai' | 'perplexity' | 'gemini'; | ||
|
||
export interface AIProvider { | ||
name: ProviderName; | ||
model?: string; | ||
} |
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 |
---|---|---|
|
@@ -4,5 +4,6 @@ export interface DbDoc { | |
user: any; | ||
title: string; | ||
createdDate: number; | ||
aiProvider: string; | ||
conversations: []; | ||
} |
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,68 @@ | ||
import openai from '../config/openai.config'; | ||
import perplexity from '../config/perplexity.config'; | ||
import gemini from '../config/gemini.config'; | ||
import { AIProvider, ProviderName } from '../models/ai-providers.model'; | ||
import { ChatMessage, GeminiMessage } from '../models/chat-message.model'; | ||
|
||
const providers: { [key in ProviderName]: { ai: any; defaultModel: string } } = { | ||
'openai': { 'ai': openai, 'defaultModel': 'gpt-3.5-turbo' }, | ||
'perplexity': { 'ai': perplexity, 'defaultModel': 'pplx-7b-online' }, | ||
'gemini': { 'ai': gemini, 'defaultModel': 'gemini-pro' }, | ||
}; | ||
|
||
async function handleGemini(messages: ChatMessage[], model: string): Promise<string> { | ||
const geminiModel = gemini.getGenerativeModel({ model }); | ||
|
||
const msg = messages[messages.length - 1].content; | ||
|
||
const geminiMessages: GeminiMessage[] = messages.map((message) => ({ | ||
'role': message.role === 'assistant' ? 'model' : message.role, | ||
'parts': [{ 'text': message.content }], | ||
})); | ||
|
||
geminiMessages.pop(); | ||
|
||
const chat = geminiModel.startChat({ | ||
'history': geminiMessages, | ||
'generationConfig': { | ||
'maxOutputTokens': 100, | ||
}, | ||
}); | ||
|
||
const result = await chat.sendMessage(msg); | ||
const response = await result.response; | ||
const text = response.text(); | ||
|
||
return text; | ||
} | ||
|
||
|
||
|
||
/** | ||
* Uses openai's createChatCompletion endpoint to generate chat completions | ||
* @param messages - Array of chat messages | ||
* @returns Completion text | ||
*/ | ||
export async function aiChat(messages: ChatMessage[], aiProvider: AIProvider): Promise<string> { | ||
const provider = providers[aiProvider.name]; | ||
if (!provider) { | ||
throw new Error('Unsupported AI provider'); | ||
} | ||
const model = aiProvider.model ?? provider.defaultModel; | ||
|
||
if (aiProvider.name === 'gemini') { | ||
return handleGemini(messages, model); | ||
} else { | ||
const completion = await provider.ai.chat.completions.create({ | ||
model, | ||
messages, | ||
}); | ||
|
||
const completionText = completion.choices[0]?.message?.content; | ||
if (!completionText) { | ||
throw new Error('Unexpected API response'); | ||
} | ||
|
||
return completionText; | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
OPENAI_API_KEY= | ||
PERPLEXITY_API_KEY= | ||
GEMINI_API_KEY= |
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
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
Oops, something went wrong.