Skip to content

Commit

Permalink
Merge pull request #1030 from getappmap/feat/token-limit-setting
Browse files Browse the repository at this point in the history
feat: Token limit setting
  • Loading branch information
kgilpin authored Oct 4, 2024
2 parents 1e636fe + f251a0c commit 2c0fb95
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ resources/*.jar

# AppMap
tmp/appmap
.navie/**/diff.txt

9 changes: 9 additions & 0 deletions .navie/prompts/pr_description.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Describe the changes in the manner of a PR description.

In the initial section, describe the changes that directly address the issue description and/or
plan, if provided.

In a subsequent section, describe any changes that were not part of the original plan or issue
description, if provided.

Do not emit any code snippets; just a description of the changes made.
28 changes: 28 additions & 0 deletions .navie/prompts/review_vs_plan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/nocontext

Compare a set of code changes with an implementation plan.

If no code changes are provided, emit a message indicating that the code changes are missing.

If no implementation plan is provided, emit a message indicating that the implementation plan is
missing.

If both code changes and an implementation plan are provided, compare the code changes with the
implementation plan and emit a list of differences between the two.

Output your analysis in the following sections:

## As planned

- A change that is present in the implementation plan and also present in the code changes.
- Another change that is present in the implementation plan and also present in the code changes.

## Unplanned changes

- A change that is present in the code changes but not in the implementation plan.
- Another change that is present in the code changes but not in the implementation plan.

## Unimplemented changes

- A change that is present in the implementation plan but not in the code changes.
- Another change that is present in the implementation plan but not in the code changes.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,11 @@
"type": "number",
"description": "Port number to pass to the Navie UI (used for extension development and debugging only)"
},
"appMap.navie.contextTokenLimit": {
"type": "number",
"default": 8000,
"description": "Default size of the context to send to Navie AI"
},
"appMap.scannerEnabled": {
"type": "boolean",
"default": false,
Expand Down
5 changes: 5 additions & 0 deletions src/configuration/extensionSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ export default class ExtensionSettings {
if (port && typeof port === 'number' && port > 0) return port;
}

public static get navieContextTokenLimit(): number | undefined {
const limit = vscode.workspace.getConfiguration('appMap').get('navie.contextTokenLimit');
if (limit && typeof limit === 'number' && limit > 0) return limit;
}

public static get useVsCodeLM(): boolean {
return [true, 'true'].includes(
vscode.workspace.getConfiguration('appMap').get('navie.useVSCodeLM') || false
Expand Down
16 changes: 14 additions & 2 deletions src/services/chatCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,24 @@ export default class ChatCompletion implements Disposable {

get env(): Record<string, string> {
const pref = ChatCompletion.preferredModel;
return {

const modelTokenLimit = pref?.maxInputTokens ?? 3926;
const tokenLimitSetting = ExtensionSettings.navieContextTokenLimit;
const tokenLimits = [modelTokenLimit, tokenLimitSetting].filter(
(limit) => limit && limit > 0
) as number[];

const env: Record<string, string> = {
OPENAI_API_KEY: this.key,
OPENAI_BASE_URL: this.url,
APPMAP_NAVIE_TOKEN_LIMIT: String(pref?.maxInputTokens ?? 3926),
APPMAP_NAVIE_MODEL: pref?.family ?? 'gpt-4o',
};

if (tokenLimits.length) {
env['APPMAP_NAVIE_TOKEN_LIMIT'] = Math.min(...tokenLimits).toString();
}

return env;
}

private static models: vscode.LanguageModelChat[] = [];
Expand Down
29 changes: 29 additions & 0 deletions test/unit/services/chatCompletion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
LanguageModelChatMessage,
LanguageModelChatResponse,
} from 'vscode';
import { workspace } from 'vscode';

import ChatCompletion from '../../../src/services/chatCompletion';
import { addMockChatModel, resetModelMocks } from '../mock/vscode/lm';
Expand All @@ -33,9 +34,19 @@ describe('ChatCompletion', () => {
beforeEach(async () => {
resetModelMocks();
addMockChatModel(mockModel);

await ChatCompletion.refreshModels();
});

function mockTokenLimitSetting(limit: number | undefined) {
sinon.stub(workspace, 'getConfiguration').returns({
get: sinon.stub().callsFake((key) => {
if (key === 'navie.contextTokenLimit') return limit;
return undefined;
}),
} as any); // eslint-disable-line @typescript-eslint/no-explicit-any
}

before(async () => {
mockModel.sendRequest = sendRequestEcho;
chatCompletion = new ChatCompletion(0, 'test-key');
Expand All @@ -57,6 +68,24 @@ describe('ChatCompletion', () => {
});
});

describe('when token limit is configured below the LLM default', () => {
beforeEach(() => mockTokenLimitSetting(100));

it('applies the configuration setting', () => {
const env = chatCompletion.env;
expect(env['APPMAP_NAVIE_TOKEN_LIMIT']).to.equal('100');
});
});

describe('when token limit is configured above the LLM default', () => {
beforeEach(() => mockTokenLimitSetting(100_000));

it('uses the LLM limit', () => {
const env = chatCompletion.env;
expect(env['APPMAP_NAVIE_TOKEN_LIMIT']).to.equal('325');
});
});

it('should refresh models and set the preferred model', async () => {
resetModelMocks();
const mockModel1 = {
Expand Down

0 comments on commit 2c0fb95

Please sign in to comment.