-
-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from Zain-ul-din/custom-model
Custom model
- Loading branch information
Showing
9 changed files
with
230 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,10 @@ | |
/session | ||
/auth_info_baileys | ||
|
||
# meta files | ||
|
||
pnpm-lock.yaml | ||
package-lock.json | ||
yarn.lock | ||
|
||
.vscode |
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 @@ | ||
The WhatsApp AI Bot is a chatbot that uses AI models APIs to generate responses to user input. The bot supports several AI models, including CHAT-GPT, DALL-E, and Stability AI, and users can also create their own models to customize the bot's behavior. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* Local modules */ | ||
import { AIModel, AIArguments, AIHandle } from './BaseAiModel'; | ||
import { ENV } from '../baileys/env'; | ||
import { GeminiModel } from './GeminiModel'; | ||
import { ChatGPTModel } from './OpenAIModel'; | ||
import { IModelType, SupportedBaseModels } from '../types/Config'; | ||
import { readFile } from 'fs/promises'; | ||
|
||
class CustomAIModel extends AIModel<AIArguments, AIHandle> { | ||
private geminiModel: GeminiModel; | ||
private chatGPTModel: ChatGPTModel; | ||
private selectedBaseModel: SupportedBaseModels; | ||
private self: IModelType; | ||
|
||
public constructor(model: IModelType) { | ||
const apiKeys: { [key in SupportedBaseModels]: string | undefined } = { | ||
ChatGPT: ENV.API_KEY_OPENAI, | ||
Gemini: ENV.API_KEY_GEMINI | ||
}; | ||
super(apiKeys[model.baseModel], model.modelName as any); | ||
|
||
this.self = model; | ||
this.selectedBaseModel = model.baseModel; | ||
this.geminiModel = new GeminiModel(); | ||
this.chatGPTModel = new ChatGPTModel(); | ||
} | ||
|
||
private static constructInstructAblePrompt({ | ||
prompt, | ||
instructions | ||
}: { | ||
prompt: string; | ||
instructions: string; | ||
}) { | ||
return ` | ||
<context> | ||
${instructions} | ||
</context> | ||
note! | ||
only answer from the given context | ||
prompt: | ||
${prompt} | ||
`; | ||
} | ||
|
||
public async sendMessage({ prompt, ...rest }: AIArguments, handle: AIHandle) { | ||
try { | ||
const instructions = await CustomAIModel.readContext(this.self); | ||
const promptWithInstructions = CustomAIModel.constructInstructAblePrompt({ | ||
prompt, | ||
instructions: instructions | ||
}); | ||
|
||
switch (this.selectedBaseModel) { | ||
case 'ChatGPT': | ||
await this.chatGPTModel.sendMessage({ prompt: promptWithInstructions, ...rest }, handle); | ||
break; | ||
case 'Gemini': | ||
await this.geminiModel.sendMessage({ prompt: promptWithInstructions, ...rest }, handle); | ||
break; | ||
} | ||
} catch (err) { | ||
await handle('', err as string); | ||
} | ||
} | ||
|
||
// read the context | ||
private static async readContext(model: IModelType) { | ||
const supportedFiles = ['.txt', '.text', '.md']; | ||
const httpProtocols = [ | ||
'https://', | ||
'http://', | ||
'ftp://', | ||
'ftps://', | ||
'sftp://', | ||
'ssh://', | ||
'git://', | ||
'svn://', | ||
'ws://', | ||
'wss://' | ||
]; | ||
|
||
if (httpProtocols.filter((protocol) => model.context.trim().startsWith(protocol)).length > 0) { | ||
const res = await fetch(model.context); | ||
return res.text(); | ||
} | ||
|
||
if (supportedFiles.filter((fileExt) => model.context.trim().endsWith(fileExt)).length > 0) { | ||
return readFile(model.context, { encoding: 'utf-8' }); | ||
} | ||
|
||
// plane text | ||
return model.context; | ||
} | ||
} | ||
|
||
export { CustomAIModel }; |
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,2 @@ | ||
export type AIModels = 'ChatGPT' | 'Gemini' | 'FLUX' | 'Stability' | 'Dalle' ; | ||
export type AIModels = 'ChatGPT' | 'Gemini' | 'FLUX' | 'Stability' | 'Dalle' | 'Custom'; | ||
export type AIModelsName = Exclude<AIModels, 'Custom'>; |
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.