diff --git a/src/modules/llms/api/athropic.ts b/src/modules/llms/api/athropic.ts index 7e951e21..efe7563a 100644 --- a/src/modules/llms/api/athropic.ts +++ b/src/modules/llms/api/athropic.ts @@ -20,6 +20,7 @@ const API_ENDPOINT = config.llms.apiEndpoint // 'http://127.0.0.1:5000' // confi export const anthropicCompletion = async ( conversation: ChatConversation[], + hastools: boolean, model = LlmsModelsEnum.CLAUDE_OPUS ): Promise => { logger.info(`Handling ${model} completion`) @@ -31,7 +32,7 @@ export const anthropicCompletion = async ( messages: conversation.filter(c => c.model === model) .map(m => { return { content: m.content, role: m.role } }) } - const url = `${API_ENDPOINT}/anthropic/completions` + const url = `${API_ENDPOINT}/anthropic/completions${hastools ? '/tools' : ''}` const response = await axios.post(url, data) const respJson = JSON.parse(response.data) if (response) { diff --git a/src/modules/llms/claudeBot.ts b/src/modules/llms/claudeBot.ts index a6518eb8..89c33d38 100644 --- a/src/modules/llms/claudeBot.ts +++ b/src/modules/llms/claudeBot.ts @@ -37,6 +37,7 @@ export class ClaudeBot extends LlmsBase { SupportedCommands.opus, SupportedCommands.opusShort, SupportedCommands.claudeShort, + SupportedCommands.claudeShortTools, SupportedCommands.claudeSonnet, SupportedCommands.sonnet, SupportedCommands.sonnetShort, @@ -75,9 +76,10 @@ export class ClaudeBot extends LlmsBase { async chatCompletion ( conversation: ChatConversation[], - model: LlmsModelsEnum + model: LlmsModelsEnum, + hasTools: boolean ): Promise { - return await anthropicCompletion(conversation, model) + return await anthropicCompletion(conversation, hasTools, model) } public async onEvent (ctx: OnMessageContext | OnCallBackQueryData): Promise { @@ -87,7 +89,10 @@ export class ClaudeBot extends LlmsBase { this.logger.warn(`### unsupported command ${ctx.message?.text}`) return } - + if (ctx.hasCommand([SupportedCommands.claudeShortTools])) { + await this.onChat(ctx, LlmsModelsEnum.CLAUDE_OPUS, false, true) + return + } if (ctx.hasCommand([ SupportedCommands.claudeOpus, SupportedCommands.opus, @@ -95,15 +100,15 @@ export class ClaudeBot extends LlmsBase { SupportedCommands.claudeShort]) || (hasClaudeOpusPrefix(ctx.message?.text ?? '') !== '') ) { - await this.onChat(ctx, LlmsModelsEnum.CLAUDE_OPUS, true) + await this.onChat(ctx, LlmsModelsEnum.CLAUDE_OPUS, false, false) // true) return } if (ctx.hasCommand([SupportedCommands.claudeSonnet, SupportedCommands.sonnet, SupportedCommands.sonnetShort])) { - await this.onChat(ctx, LlmsModelsEnum.CLAUDE_SONNET, true) + await this.onChat(ctx, LlmsModelsEnum.CLAUDE_SONNET, true, false) return } if (ctx.hasCommand([SupportedCommands.claudeHaiku, SupportedCommands.haikuShort])) { - await this.onChat(ctx, LlmsModelsEnum.CLAUDE_HAIKU, false) + await this.onChat(ctx, LlmsModelsEnum.CLAUDE_HAIKU, false, false) } } } diff --git a/src/modules/llms/llmsBase.ts b/src/modules/llms/llmsBase.ts index fe113369..978b258b 100644 --- a/src/modules/llms/llmsBase.ts +++ b/src/modules/llms/llmsBase.ts @@ -79,7 +79,8 @@ export abstract class LlmsBase implements PayableBot { protected abstract chatCompletion ( conversation: ChatConversation[], - model: LlmsModelsEnum + model: LlmsModelsEnum, + usesTools: boolean ): Promise protected abstract hasPrefix (prompt: string): string @@ -109,7 +110,7 @@ export abstract class LlmsBase implements PayableBot { session.requestQueue.push(msg) if (!session.isProcessingQueue) { session.isProcessingQueue = true - await this.onChatRequestHandler(ctx, true).then(() => { + await this.onChatRequestHandler(ctx, true, false).then(() => { session.isProcessingQueue = false }) } @@ -126,7 +127,7 @@ export abstract class LlmsBase implements PayableBot { return supportedAgents } - async onChat (ctx: OnMessageContext | OnCallBackQueryData, model: string, stream: boolean): Promise { + async onChat (ctx: OnMessageContext | OnCallBackQueryData, model: string, stream: boolean, usesTools: boolean): Promise { const session = this.getSession(ctx) try { if (this.botSuspended) { @@ -146,7 +147,7 @@ export abstract class LlmsBase implements PayableBot { }) if (!session.isProcessingQueue) { session.isProcessingQueue = true - await this.onChatRequestHandler(ctx, stream).then(() => { + await this.onChatRequestHandler(ctx, stream, usesTools).then(() => { session.isProcessingQueue = false }) } @@ -165,7 +166,7 @@ export abstract class LlmsBase implements PayableBot { } } - async onChatRequestHandler (ctx: OnMessageContext | OnCallBackQueryData, stream: boolean): Promise { + async onChatRequestHandler (ctx: OnMessageContext | OnCallBackQueryData, stream: boolean, usesTools: boolean): Promise { const session = this.getSession(ctx) while (session.requestQueue.length > 0) { try { @@ -223,9 +224,9 @@ export abstract class LlmsBase implements PayableBot { } let result: { price: number, chat: ChatConversation[] } = { price: 0, chat: [] } if (stream) { - result = await this.completionGen(payload) + result = await this.completionGen(payload, usesTools) } else { - result = await this.promptGen(payload) + result = await this.promptGen(payload, usesTools) } session.chatConversation = [...result.chat] if ( @@ -259,7 +260,7 @@ export abstract class LlmsBase implements PayableBot { ) } - private async completionGen (data: ChatPayload, msgId?: number, outputFormat = 'text'): Promise< { price: number, chat: ChatConversation[] }> { + private async completionGen (data: ChatPayload, usesTools: boolean, msgId?: number, outputFormat = 'text'): Promise< { price: number, chat: ChatConversation[] }> { const { conversation, ctx, model } = data try { if (!msgId) { @@ -304,7 +305,7 @@ export abstract class LlmsBase implements PayableBot { } } } else { - const response = await this.chatCompletion(conversation, model as LlmsModelsEnum) + const response = await this.chatCompletion(conversation, model as LlmsModelsEnum, usesTools) conversation.push({ role: 'assistant', content: response.completion?.content ?? '', @@ -326,7 +327,7 @@ export abstract class LlmsBase implements PayableBot { } } - private async promptGen (data: ChatPayload): Promise<{ price: number, chat: ChatConversation[] }> { + private async promptGen (data: ChatPayload, usesTools: boolean): Promise<{ price: number, chat: ChatConversation[] }> { const { conversation, ctx, model } = data if (!ctx.chat?.id) { throw new Error('internal error') @@ -335,7 +336,7 @@ export abstract class LlmsBase implements PayableBot { await ctx.reply('...', { message_thread_id: ctx.message?.message_thread_id }) ).message_id ctx.chatAction = 'typing' - const response = await this.chatCompletion(conversation, model as LlmsModelsEnum) + const response = await this.chatCompletion(conversation, model as LlmsModelsEnum, usesTools) if (response.completion) { await ctx.api.editMessageText( ctx.chat.id, diff --git a/src/modules/llms/llmsBot.ts b/src/modules/llms/llmsBot.ts index 96863035..f22e2e98 100644 --- a/src/modules/llms/llmsBot.ts +++ b/src/modules/llms/llmsBot.ts @@ -75,7 +75,7 @@ export class LlmsBot extends LlmsBase { } if (ctx.hasCommand(SupportedCommands.j2Ultra)) { this.updateSessionModel(ctx, LlmsModelsEnum.J2_ULTRA) - await this.onChat(ctx, LlmsModelsEnum.J2_ULTRA, false) + await this.onChat(ctx, LlmsModelsEnum.J2_ULTRA, false, false) } } } diff --git a/src/modules/llms/openaiBot.ts b/src/modules/llms/openaiBot.ts index dc9d54d7..28c1545b 100644 --- a/src/modules/llms/openaiBot.ts +++ b/src/modules/llms/openaiBot.ts @@ -133,12 +133,12 @@ export class OpenAIBot extends LlmsBase { ctx.chat?.type === 'private') ) { this.updateSessionModel(ctx, LlmsModelsEnum.GPT_4) - await this.onChat(ctx, LlmsModelsEnum.GPT_4, true) + await this.onChat(ctx, LlmsModelsEnum.GPT_4, true, false) return } if (ctx.hasCommand([SupportedCommands.pdf, SupportedCommands.ctx]) && this.checkModel(ctx)) { - await this.onChat(ctx, ctx.session.currentModel, true) + await this.onChat(ctx, ctx.session.currentModel, true, false) return } @@ -149,19 +149,19 @@ export class OpenAIBot extends LlmsBase { ) { await this.onStop(ctx) this.updateSessionModel(ctx, LlmsModelsEnum.GPT_4) - await this.onChat(ctx, LlmsModelsEnum.GPT_4, true) + await this.onChat(ctx, LlmsModelsEnum.GPT_4, true, false) return } if (ctx.hasCommand(SupportedCommands.ask35)) { this.updateSessionModel(ctx, LlmsModelsEnum.GPT_35_TURBO_16K) - await this.onChat(ctx, LlmsModelsEnum.GPT_35_TURBO_16K, true) + await this.onChat(ctx, LlmsModelsEnum.GPT_35_TURBO_16K, true, false) return } if (ctx.hasCommand(SupportedCommands.ask32)) { this.updateSessionModel(ctx, LlmsModelsEnum.GPT_4_32K) - await this.onChat(ctx, LlmsModelsEnum.GPT_4_32K, true) + await this.onChat(ctx, LlmsModelsEnum.GPT_4_32K, true, false) return } @@ -172,7 +172,7 @@ export class OpenAIBot extends LlmsBase { if (ctx.chat?.type === 'private' || session.isFreePromptChatGroups) { this.updateSessionModel(ctx, LlmsModelsEnum.GPT_4) - await this.onChat(ctx, LlmsModelsEnum.GPT_4, true) + await this.onChat(ctx, LlmsModelsEnum.GPT_4, true, false) return } diff --git a/src/modules/llms/utils/helpers.ts b/src/modules/llms/utils/helpers.ts index 903fe80e..7cc38107 100644 --- a/src/modules/llms/utils/helpers.ts +++ b/src/modules/llms/utils/helpers.ts @@ -21,6 +21,7 @@ export enum SupportedCommands { opus = 'opus', opusShort = 'o', claudeSonnet = 'claudes', + claudeShortTools = 'ct', claudeShort = 'c', sonnet = 'sonnet', sonnetShort = 's', diff --git a/src/modules/llms/vertexBot.ts b/src/modules/llms/vertexBot.ts index 45245c88..596279b2 100644 --- a/src/modules/llms/vertexBot.ts +++ b/src/modules/llms/vertexBot.ts @@ -83,22 +83,22 @@ export class VertexBot extends LlmsBase { } if (ctx.hasCommand([SupportedCommands.bard, SupportedCommands.bardF]) || hasBardPrefix(ctx.message?.text ?? '')) { this.updateSessionModel(ctx, LlmsModelsEnum.BISON) - await this.onChat(ctx, LlmsModelsEnum.BISON, false) + await this.onChat(ctx, LlmsModelsEnum.BISON, false, false) return } if (ctx.hasCommand([SupportedCommands.gemini, SupportedCommands.gShort]) || (hasGeminiPrefix(ctx.message?.text ?? '') !== '')) { this.updateSessionModel(ctx, LlmsModelsEnum.GEMINI) - await this.onChat(ctx, LlmsModelsEnum.GEMINI, true) + await this.onChat(ctx, LlmsModelsEnum.GEMINI, true, false) return } if (ctx.hasCommand([SupportedCommands.gemini15, SupportedCommands.g15short])) { this.updateSessionModel(ctx, LlmsModelsEnum.GEMINI_15) - await this.onChat(ctx, LlmsModelsEnum.GEMINI_15, true) + await this.onChat(ctx, LlmsModelsEnum.GEMINI_15, true, false) // return } if (ctx.hasCommand([SupportedCommands.pdf, SupportedCommands.ctx]) && this.checkModel(ctx)) { - await this.onChat(ctx, ctx.session.currentModel, true) + await this.onChat(ctx, ctx.session.currentModel, true, false) } } }