Skip to content

Commit 5045cdd

Browse files
authored
More specific llm tools (#25072)
1 parent 9fd7b9b commit 5045cdd

File tree

11 files changed

+455
-154
lines changed

11 files changed

+455
-154
lines changed

package.json

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,10 +1465,10 @@
14651465
],
14661466
"languageModelTools": [
14671467
{
1468-
"name": "get_python_environment",
1469-
"displayName": "Get Python Environment Information",
1470-
"userDescription": "%python.languageModelTools.python_environment.userDescription%",
1471-
"modelDescription": "Provides details about the Python environment for a specified file or workspace, including environment type, Python version, run command, and installed packages with their versions. Use this tool to determine the correct command for executing Python code in this workspace.",
1468+
"name": "get_python_environment_info",
1469+
"displayName": "Get Python Environment Info",
1470+
"userDescription": "%python.languageModelTools.get_python_environment_info.userDescription%",
1471+
"modelDescription": "This tool will retrieve the details of the Python Environment for the specified file or workspace. The details returned include the 1. Type of Environment (conda, venv, etec), 2. Version of Python, 3. List of all installed packages with their versions. ",
14721472
"toolReferenceName": "pythonGetEnvironmentInfo",
14731473
"tags": [
14741474
"ms-python.python"
@@ -1483,11 +1483,53 @@
14831483
}
14841484
},
14851485
"description": "The path to the Python file or workspace to get the environment information for.",
1486-
"required": [
1487-
"resourcePath"
1488-
]
1486+
"required": []
1487+
}
1488+
},
1489+
{
1490+
"name": "get_python_executable",
1491+
"displayName": "Get Python Executable",
1492+
"userDescription": "%python.languageModelTools.get_python_executable.userDescription%",
1493+
"modelDescription": "This tool will retrieve the details of the Python Environment for the specified file or workspace. ALWAYS use this tool before executing any Python command in the terminal. This tool returns the details of how to construct the fully qualified path and or command including details such as arguments required to run Python in a terminal. Note: Instead of executing `python --version` or `python -c 'import sys; print(sys.executable)'`, use this tool to get the Python executable path to replace the `python` command. E.g. instead of using `python -c 'import sys; print(sys.executable)'`, use this tool to build the command `conda run -n <env_name> -c 'import sys; print(sys.executable)'`.",
1494+
"toolReferenceName": "pythonExecutableCommand",
1495+
"tags": [
1496+
"ms-python.python"
1497+
],
1498+
"icon": "$(files)",
1499+
"canBeReferencedInPrompt": true,
1500+
"inputSchema": {
1501+
"type": "object",
1502+
"properties": {
1503+
"resourcePath": {
1504+
"type": "string"
1505+
}
1506+
},
1507+
"description": "The path to the Python file or workspace to get the executable information for. If not provided, the current workspace will be used. Where possible pass the path to the file or workspace.",
1508+
"required": []
1509+
}
1510+
},
1511+
{
1512+
"name": "list_python_packages",
1513+
"displayName": "List Python Packages",
1514+
"userDescription": "%python.languageModelTools.list_python_packages.userDescription%",
1515+
"modelDescription": "This tool will retrieve the list of all installed packages installed in a Python Environment for the specified file or workspace. ALWAYS use this tool instead of executing Python command in the terminal to fetch the list of installed packages. WARNING: Packages installed can change over time, hence the list of packages returned by this tool may not be accurate. Use this tool to get the list of installed packages in a Python environment.",
1516+
"toolReferenceName": "listPythonPackages",
1517+
"tags": [
1518+
"ms-python.python"
1519+
],
1520+
"icon": "$(files)",
1521+
"canBeReferencedInPrompt": true,
1522+
"inputSchema": {
1523+
"type": "object",
1524+
"properties": {
1525+
"resourcePath": {
1526+
"type": "string"
1527+
}
1528+
},
1529+
"description": "The path to the Python file or workspace to list the packages. If not provided, the current workspace will be used. Where possible pass the path to the file or workspace.",
1530+
"required": []
14891531
},
1490-
"when": "!pythonEnvExtensionInstalled"
1532+
"when": "false"
14911533
},
14921534
{
14931535
"name": "install_python_package",
@@ -1512,12 +1554,11 @@
15121554
},
15131555
"resourcePath": {
15141556
"type": "string",
1515-
"description": "The path to the Python file or workspace to get the environment information for."
1557+
"description": "The path to the Python file or workspace into which the packages are installed. If not provided, the current workspace will be used. Where possible pass the path to the file or workspace."
15161558
}
15171559
},
15181560
"required": [
1519-
"packageList",
1520-
"resourcePath"
1561+
"packageList"
15211562
]
15221563
},
15231564
"when": "!pythonEnvExtensionInstalled"

package.nls.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
{
22
"python.command.python.startTerminalREPL.title": "Start Terminal REPL",
3-
"python.languageModelTools.python_environment.userDescription": "Get Python environment info for a file or path, including version, packages, and the command to run it.",
4-
"python.languageModelTools.python_install_package.userDescription": "Installs Python packages in the given workspace.",
3+
"python.languageModelTools.get_python_environment_info.userDescription": "Get information for a Python Environment, such as Type, Version, Packages, and more.",
4+
"python.languageModelTools.python_install_package.userDescription": "Installs Python packages in a Python Environment.",
5+
"python.languageModelTools.get_python_executable.userDescription": "Get executable info for a Python Environment",
6+
"python.languageModelTools.list_python_packages.userDescription": "Get a list of all installed packages in a Python Environment.",
57
"python.command.python.startNativeREPL.title": "Start Native Python REPL",
68
"python.command.python.createEnvironment.title": "Create Environment...",
79
"python.command.python.createNewFile.title": "New Python File",

src/client/chat/getExecutableTool.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
import {
5+
CancellationError,
6+
CancellationToken,
7+
l10n,
8+
LanguageModelTextPart,
9+
LanguageModelTool,
10+
LanguageModelToolInvocationOptions,
11+
LanguageModelToolInvocationPrepareOptions,
12+
LanguageModelToolResult,
13+
PreparedToolInvocation,
14+
} from 'vscode';
15+
import { PythonExtension } from '../api/types';
16+
import { IServiceContainer } from '../ioc/types';
17+
import { ICodeExecutionService } from '../terminals/types';
18+
import { TerminalCodeExecutionProvider } from '../terminals/codeExecution/terminalCodeExecution';
19+
import { getEnvDisplayName, getEnvironmentDetails, raceCancellationError } from './utils';
20+
import { resolveFilePath } from './utils';
21+
import { traceError } from '../logging';
22+
import { ITerminalHelper } from '../common/terminal/types';
23+
import { IDiscoveryAPI } from '../pythonEnvironments/base/locator';
24+
25+
export interface IResourceReference {
26+
resourcePath?: string;
27+
}
28+
29+
export class GetExecutableTool implements LanguageModelTool<IResourceReference> {
30+
private readonly terminalExecutionService: TerminalCodeExecutionProvider;
31+
private readonly terminalHelper: ITerminalHelper;
32+
public static readonly toolName = 'get_python_executable';
33+
constructor(
34+
private readonly api: PythonExtension['environments'],
35+
private readonly serviceContainer: IServiceContainer,
36+
private readonly discovery: IDiscoveryAPI,
37+
) {
38+
this.terminalExecutionService = this.serviceContainer.get<TerminalCodeExecutionProvider>(
39+
ICodeExecutionService,
40+
'standard',
41+
);
42+
this.terminalHelper = this.serviceContainer.get<ITerminalHelper>(ITerminalHelper);
43+
}
44+
async invoke(
45+
options: LanguageModelToolInvocationOptions<IResourceReference>,
46+
token: CancellationToken,
47+
): Promise<LanguageModelToolResult> {
48+
const resourcePath = resolveFilePath(options.input.resourcePath);
49+
50+
try {
51+
const message = await getEnvironmentDetails(
52+
resourcePath,
53+
this.api,
54+
this.terminalExecutionService,
55+
this.terminalHelper,
56+
undefined,
57+
token,
58+
);
59+
return new LanguageModelToolResult([new LanguageModelTextPart(message)]);
60+
} catch (error) {
61+
if (error instanceof CancellationError) {
62+
throw error;
63+
}
64+
traceError('Error while getting environment information', error);
65+
const errorMessage: string = `An error occurred while fetching environment information: ${error}`;
66+
return new LanguageModelToolResult([new LanguageModelTextPart(errorMessage)]);
67+
}
68+
}
69+
70+
async prepareInvocation?(
71+
options: LanguageModelToolInvocationPrepareOptions<IResourceReference>,
72+
token: CancellationToken,
73+
): Promise<PreparedToolInvocation> {
74+
const resourcePath = resolveFilePath(options.input.resourcePath);
75+
const envName = await raceCancellationError(getEnvDisplayName(this.discovery, resourcePath, this.api), token);
76+
return {
77+
invocationMessage: envName
78+
? l10n.t('Fetching Python executable information for {0}', envName)
79+
: l10n.t('Fetching Python executable information'),
80+
};
81+
}
82+
}

src/client/chat/getPythonEnvTool.ts

Lines changed: 25 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,27 @@ import {
1111
LanguageModelToolInvocationPrepareOptions,
1212
LanguageModelToolResult,
1313
PreparedToolInvocation,
14-
Uri,
1514
} from 'vscode';
16-
import { PythonExtension, ResolvedEnvironment } from '../api/types';
15+
import { PythonExtension } from '../api/types';
1716
import { IServiceContainer } from '../ioc/types';
1817
import { ICodeExecutionService } from '../terminals/types';
1918
import { TerminalCodeExecutionProvider } from '../terminals/codeExecution/terminalCodeExecution';
20-
import { IProcessService, IProcessServiceFactory, IPythonExecutionFactory } from '../common/process/types';
21-
import { raceCancellationError } from './utils';
19+
import { IProcessServiceFactory, IPythonExecutionFactory } from '../common/process/types';
20+
import { getEnvironmentDetails, raceCancellationError } from './utils';
2221
import { resolveFilePath } from './utils';
23-
import { parsePipList } from './pipListUtils';
24-
import { Conda } from '../pythonEnvironments/common/environmentManagers/conda';
25-
import { traceError } from '../logging';
22+
import { getPythonPackagesResponse } from './listPackagesTool';
23+
import { ITerminalHelper } from '../common/terminal/types';
2624

2725
export interface IResourceReference {
28-
resourcePath: string;
26+
resourcePath?: string;
2927
}
3028

31-
interface EnvironmentInfo {
32-
type: string; // e.g. conda, venv, virtualenv, sys
33-
version: string;
34-
runCommand: string;
35-
packages: string[] | string; //include versions too
36-
}
37-
38-
/**
39-
* A tool to get the information about the Python environment.
40-
*/
4129
export class GetEnvironmentInfoTool implements LanguageModelTool<IResourceReference> {
4230
private readonly terminalExecutionService: TerminalCodeExecutionProvider;
4331
private readonly pythonExecFactory: IPythonExecutionFactory;
4432
private readonly processServiceFactory: IProcessServiceFactory;
45-
public static readonly toolName = 'get_python_environment';
33+
private readonly terminalHelper: ITerminalHelper;
34+
public static readonly toolName = 'get_python_environment_info';
4635
constructor(
4736
private readonly api: PythonExtension['environments'],
4837
private readonly serviceContainer: IServiceContainer,
@@ -53,6 +42,7 @@ export class GetEnvironmentInfoTool implements LanguageModelTool<IResourceRefere
5342
);
5443
this.pythonExecFactory = this.serviceContainer.get<IPythonExecutionFactory>(IPythonExecutionFactory);
5544
this.processServiceFactory = this.serviceContainer.get<IProcessServiceFactory>(IProcessServiceFactory);
45+
this.terminalHelper = this.serviceContainer.get<ITerminalHelper>(ITerminalHelper);
5646
}
5747
/**
5848
* Invokes the tool to get the information about the Python environment.
@@ -66,53 +56,37 @@ export class GetEnvironmentInfoTool implements LanguageModelTool<IResourceRefere
6656
): Promise<LanguageModelToolResult> {
6757
const resourcePath = resolveFilePath(options.input.resourcePath);
6858

69-
// environment info set to default values
70-
const envInfo: EnvironmentInfo = {
71-
type: 'no type found',
72-
version: 'no version found',
73-
packages: 'no packages found',
74-
runCommand: 'no run command found',
75-
};
76-
7759
try {
7860
// environment
7961
const envPath = this.api.getActiveEnvironmentPath(resourcePath);
8062
const environment = await raceCancellationError(this.api.resolveEnvironment(envPath), token);
8163
if (!environment || !environment.version) {
82-
throw new Error('No environment found for the provided resource path: ' + resourcePath.fsPath);
64+
throw new Error('No environment found for the provided resource path: ' + resourcePath?.fsPath);
8365
}
84-
const cmd = await raceCancellationError(
85-
this.terminalExecutionService.getExecutableInfo(resourcePath),
66+
const packages = await getPythonPackagesResponse(
67+
environment,
68+
this.pythonExecFactory,
69+
this.processServiceFactory,
70+
resourcePath,
8671
token,
8772
);
88-
const executable = cmd.pythonExecutable;
89-
envInfo.runCommand = cmd.args.length > 0 ? `${cmd.command} ${cmd.args.join(' ')}` : executable;
90-
envInfo.version = environment.version.sysVersion;
9173

92-
const isConda = (environment.environment?.type || '').toLowerCase() === 'conda';
93-
envInfo.packages = isConda
94-
? await raceCancellationError(
95-
listCondaPackages(
96-
this.pythonExecFactory,
97-
environment,
98-
resourcePath,
99-
await raceCancellationError(this.processServiceFactory.create(resourcePath), token),
100-
),
101-
token,
102-
)
103-
: await raceCancellationError(listPipPackages(this.pythonExecFactory, resourcePath), token);
74+
const message = await getEnvironmentDetails(
75+
resourcePath,
76+
this.api,
77+
this.terminalExecutionService,
78+
this.terminalHelper,
79+
packages,
80+
token,
81+
);
10482

105-
// format and return
106-
return new LanguageModelToolResult([BuildEnvironmentInfoContent(envInfo)]);
83+
return new LanguageModelToolResult([new LanguageModelTextPart(message)]);
10784
} catch (error) {
10885
if (error instanceof CancellationError) {
10986
throw error;
11087
}
11188
const errorMessage: string = `An error occurred while fetching environment information: ${error}`;
112-
const partialContent = BuildEnvironmentInfoContent(envInfo);
113-
return new LanguageModelToolResult([
114-
new LanguageModelTextPart(`${errorMessage}\n\n${partialContent.value}`),
115-
]);
89+
return new LanguageModelToolResult([new LanguageModelTextPart(errorMessage)]);
11690
}
11791
}
11892

@@ -125,75 +99,3 @@ export class GetEnvironmentInfoTool implements LanguageModelTool<IResourceRefere
12599
};
126100
}
127101
}
128-
129-
function BuildEnvironmentInfoContent(envInfo: EnvironmentInfo): LanguageModelTextPart {
130-
// Create a formatted string that looks like JSON but preserves comments
131-
let envTypeDescriptor: string = `This environment is managed by ${envInfo.type} environment manager. Use the install tool to install packages into this environment.`;
132-
133-
// TODO: If this is setup as python.defaultInterpreterPath, then do not include this message.
134-
if (envInfo.type === 'system') {
135-
envTypeDescriptor =
136-
'System pythons are pythons that ship with the OS or are installed globally. These python installs may be used by the OS for running services and core functionality. Confirm with the user before installing packages into this environment, as it can lead to issues with any services on the OS.';
137-
}
138-
const content = `{
139-
// ${JSON.stringify(envTypeDescriptor)}
140-
"environmentType": ${JSON.stringify(envInfo.type)},
141-
// Python version of the environment
142-
"pythonVersion": ${JSON.stringify(envInfo.version)},
143-
// Use this command to run Python script or code in the terminal.
144-
"runCommand": ${JSON.stringify(envInfo.runCommand)},
145-
// Installed Python packages, each in the format <name> or <name> (<version>). The version may be omitted if unknown. Returns an empty array if no packages are installed.
146-
"packages": ${JSON.stringify(Array.isArray(envInfo.packages) ? envInfo.packages : envInfo.packages, null, 2)}
147-
}`;
148-
149-
return new LanguageModelTextPart(content);
150-
}
151-
152-
async function listPipPackages(execFactory: IPythonExecutionFactory, resource: Uri) {
153-
// Add option --format to subcommand list of pip cache, with abspath choice to output the full path of a wheel file. (#8355)
154-
// Added in 202. Thats almost 5 years ago. When Python 3.8 was released.
155-
const exec = await execFactory.createActivatedEnvironment({ allowEnvironmentFetchExceptions: true, resource });
156-
const output = await exec.execModule('pip', ['list'], { throwOnStdErr: false, encoding: 'utf8' });
157-
return parsePipList(output.stdout).map((pkg) => (pkg.version ? `${pkg.name} (${pkg.version})` : pkg.name));
158-
}
159-
160-
async function listCondaPackages(
161-
execFactory: IPythonExecutionFactory,
162-
env: ResolvedEnvironment,
163-
resource: Uri,
164-
processService: IProcessService,
165-
) {
166-
const conda = await Conda.getConda();
167-
if (!conda) {
168-
traceError('Conda is not installed, falling back to pip packages');
169-
return listPipPackages(execFactory, resource);
170-
}
171-
if (!env.executable.uri) {
172-
traceError('Conda environment executable not found, falling back to pip packages');
173-
return listPipPackages(execFactory, resource);
174-
}
175-
const condaEnv = await conda.getCondaEnvironment(env.executable.uri.fsPath);
176-
if (!condaEnv) {
177-
traceError('Conda environment not found, falling back to pip packages');
178-
return listPipPackages(execFactory, resource);
179-
}
180-
const cmd = await conda.getListPythonPackagesArgs(condaEnv, true);
181-
if (!cmd) {
182-
traceError('Conda list command not found, falling back to pip packages');
183-
return listPipPackages(execFactory, resource);
184-
}
185-
const output = await processService.exec(cmd[0], cmd.slice(1), { shell: true });
186-
if (!output.stdout) {
187-
traceError('Unable to get conda packages, falling back to pip packages');
188-
return listPipPackages(execFactory, resource);
189-
}
190-
const content = output.stdout.split(/\r?\n/).filter((l) => !l.startsWith('#'));
191-
const packages: string[] = [];
192-
content.forEach((l) => {
193-
const parts = l.split(' ').filter((p) => p.length > 0);
194-
if (parts.length === 3) {
195-
packages.push(`${parts[0]} (${parts[1]})`);
196-
}
197-
});
198-
return packages;
199-
}

0 commit comments

Comments
 (0)