Skip to content

Commit

Permalink
feat: support maxInputTokens for agent (#4397)
Browse files Browse the repository at this point in the history
* feat: support maxInputTokens for agent

* fix: use formatLocalize

* chore: optional maxInputTokens

* fix: clear  startIndex when dispose

* fix: type error

* feat: support noTools options
  • Loading branch information
ensorrow authored Feb 24, 2025
1 parent aca7ff8 commit ebc1030
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/ai-native/src/browser/chat/chat-proxy.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,13 @@ export class ChatProxyService extends Disposable {
} else {
apiKey = this.preferenceService.get<string>(AINativeSettingSectionsId.AnthropicApiKey, '');
}

const MAX_INPUT_TOKENS = 30720;
const stream = await this.aiBackService.requestStream(
prompt,
{
requestId: request.requestId,
sessionId: request.sessionId,
history: this.aiChatService.getHistoryMessages(),
history: this.aiChatService.getHistoryMessages(MAX_INPUT_TOKENS),
clientId: this.applicationService.clientId,
apiKey,
model,
Expand Down
4 changes: 2 additions & 2 deletions packages/ai-native/src/browser/chat/chat.api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ export class ChatService extends Disposable {
this._onChatMessageListLaunch.fire(list);
}

public getHistoryMessages(): IHistoryChatMessage[] {
return this.chatInternalService.sessionModel?.history.getMessages() || [];
public getHistoryMessages(maxInputTokens?: number): IHistoryChatMessage[] {
return this.chatInternalService.sessionModel?.history.getMessages(maxInputTokens) || [];
}

public scrollToBottom(): void {
Expand Down
10 changes: 10 additions & 0 deletions packages/ai-native/src/browser/chat/chat.module.less
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,16 @@
}
}


.chat_tips_container {
display: flex;
align-items: center;
justify-content: center;
color: var(--tab-inactiveForeground);
font-size: 12px;
margin-top: 16px;
}

.chat_history {
width: calc(100% - 60px);
color: var(--design-text-foreground);
Expand Down
8 changes: 8 additions & 0 deletions packages/ai-native/src/browser/chat/chat.view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
IChatComponent,
IChatContent,
MessageType,
formatLocalize,
localize,
uuid,
} from '@opensumi/ide-core-common';
Expand Down Expand Up @@ -696,6 +697,13 @@ export const AIChatView = () => {
dataSource={messageListData}
/>
</div>
{msgHistoryManager.slicedMessageCount ? (
<div className={styles.chat_tips_text}>
<div className={styles.chat_tips_container}>
{formatLocalize('aiNative.chat.ai.assistant.limit.message', msgHistoryManager.slicedMessageCount)}
</div>
</div>
) : null}
<div className={styles.chat_input_wrap}>
<ChatContext />
<div className={styles.header_operate}>
Expand Down
23 changes: 22 additions & 1 deletion packages/ai-native/src/browser/model/msg-history-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class MsgHistoryManager extends Disposable {
public clearMessages() {
this.messageMap.clear();
this.messageAdditionalMap.clear();
this.startIndex = 0;
}

private doAddMessage(message: IExcludeMessage): string {
Expand All @@ -50,10 +51,30 @@ export class MsgHistoryManager extends Disposable {
return id;
}

public getMessages(): IHistoryChatMessage[] {
private get messageList(): IHistoryChatMessage[] {
return Array.from(this.messageMap.values()).sort((a, b) => a.order - b.order);
}

private startIndex = 0;

private get totalTokens(): number {
const list = this.messageList.slice(this.startIndex);
return list.reduce((acc, msg) => acc + (msg.content.length || 0), 0) / 3;
}

public get slicedMessageCount(): number {
return this.startIndex;
}

public getMessages(maxTokens?: number): IHistoryChatMessage[] {
if (maxTokens && this.totalTokens > maxTokens) {
while (this.totalTokens > maxTokens) {
this.startIndex++;
}
}
return this.messageList.slice(this.startIndex);
}

public addUserMessage(
message: Required<Pick<IExcludeMessage, 'agentId' | 'agentCommand' | 'content' | 'relationId'>>,
): string {
Expand Down
2 changes: 1 addition & 1 deletion packages/ai-native/src/node/base-language-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export abstract class BaseLanguageModel {
throw new Error('clientId is required');
}
const registry = this.toolInvocationRegistryManager.getRegistry(clientId);
const allFunctions = registry.getAllFunctions();
const allFunctions = options.noTool ? [] : registry.getAllFunctions();
return this.handleStreamingRequest(
provider,
request,
Expand Down
1 change: 1 addition & 0 deletions packages/core-common/src/types/ai-native/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ export interface IAIBackServiceOption {
topP?: number;
topK?: number;
providerOptions?: any;
noTool?: boolean;
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/i18n/src/common/en-US.lang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1491,6 +1491,7 @@ export const localizationBundle = {
'aiNative.operate.chatHistory.delete': 'Delete',

'aiNative.chat.welcome.loading.text': 'Initializing...',
'aiNative.chat.ai.assistant.limit.message': '{0} earliest messages are dropped due to the input token limit',

'preference.ai.native.inlineChat.title': 'Inline Chat',
'preference.ai.native.chat.title': 'Chat',
Expand Down
1 change: 1 addition & 0 deletions packages/i18n/src/common/zh-CN.lang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,7 @@ export const localizationBundle = {
'aiNative.operate.chatHistory.delete': '删除',

'aiNative.chat.welcome.loading.text': '初始化中...',
'aiNative.chat.ai.assistant.limit.message': '{0} 条最早的消息因输入 Tokens 限制而被丢弃',

'preference.ai.native.inlineChat.title': 'Inline Chat',
'preference.ai.native.chat.title': 'Chat',
Expand Down

0 comments on commit ebc1030

Please sign in to comment.