Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: add python code tool over http #195

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ BEE_FRAMEWORK_LOG_SINGLE_LINE="false"
# GCP_VERTEXAI_LOCATION=""

# Tools
# CODE_INTERPRETER_URL="http://127.0.0.1:50051"
# CODE_INTERPRETER_URL="http://127.0.0.1:50081"

# For Google Search Tool
# GOOGLE_API_KEY="your-google-api-key"
Expand Down
75 changes: 75 additions & 0 deletions src/tools/python/python.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* Copyright 2024 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { describe, it, expect } from "vitest";
import { PythonTool } from "@/tools/python/python.js";
import { verifyDeserialization } from "@tests/e2e/utils.js";
import { LocalPythonStorage } from "@/tools/python/storage.js";

const codeInterpreterUrl = process.env.CODE_INTERPRETER_URL || "http://localhost:50081";

const getPythonTool = () =>
new PythonTool({
codeInterpreter: { url: codeInterpreterUrl },
storage: new LocalPythonStorage({
interpreterWorkingDir: "/tmp/code-interpreter-storage",
localWorkingDir: "./test_dir/",
}),
});

describe("PythonTool", () => {
it("Is the expected tool", () => {
const tool = getPythonTool();
expect(tool).toBeInstanceOf(PythonTool);
expect(PythonTool.isTool(tool)).toBe(true);
expect(tool.name).toBe("Python");
expect(tool.description).toMatch("Run Python and/or shell code");
});

it("Throws input validation error for wrong language", async () => {
await expect(
getPythonTool().run({
// @ts-ignore
language: "PL/1",

code: "# won't get this far because we don't support PL/1 yet",
inputFiles: [],
}),
).rejects.toThrow("The received tool input does not match the expected schema.");
});

it("Throws input validation error for missing file", async () => {
const sourceCode = `
with open("test_file.txt", 'r') as f:
print(f.read())
`;

await expect(
getPythonTool().run({
language: "python",
code: sourceCode,
inputFiles: ["bogus_file.txt"],
}),
).rejects.toThrow("The received tool input does not match the expected schema.");
});

it("serializes", async () => {
const tool = getPythonTool();
const serialized = tool.serialize();
const deserializedTool = PythonTool.fromSerialized(serialized);
verifyDeserialization(tool, deserializedTool);
});
});
70 changes: 41 additions & 29 deletions src/tools/python/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@
* limitations under the License.
*/

import { BaseToolOptions, BaseToolRunOptions, ToolEmitter, Tool, ToolInput } from "@/tools/base.js";
import { createGrpcTransport } from "@connectrpc/connect-node";
import { PromiseClient, createPromiseClient } from "@connectrpc/connect";
import { CodeInterpreterService } from "bee-proto/code_interpreter/v1/code_interpreter_service_connect";
import {
BaseToolOptions,
BaseToolRunOptions,
ToolEmitter,
Tool,
ToolError,
ToolInput,
} from "@/tools/base.js";
import { z } from "zod";
import { BaseLLMOutput } from "@/llms/base.js";
import { LLM } from "@/llms/llm.js";
Expand Down Expand Up @@ -95,7 +99,6 @@ export class PythonTool extends Tool<PythonToolOutput, PythonToolOptions> {
});
}

protected readonly client: PromiseClient<typeof CodeInterpreterService>;
protected readonly preprocess;

public constructor(options: PythonToolOptions) {
Expand All @@ -109,7 +112,6 @@ export class PythonTool extends Tool<PythonToolOutput, PythonToolOptions> {
},
]);
}
this.client = this._createClient();
this.preprocess = options.preprocess;
this.storage = options.storage;
}
Expand All @@ -118,17 +120,6 @@ export class PythonTool extends Tool<PythonToolOutput, PythonToolOptions> {
this.register();
}

protected _createClient(): PromiseClient<typeof CodeInterpreterService> {
return createPromiseClient(
CodeInterpreterService,
createGrpcTransport({
baseUrl: this.options.codeInterpreter.url,
httpVersion: "2",
nodeOptions: this.options.codeInterpreter.connectionOptions,
}),
);
}

protected async _run(
input: ToolInput<this>,
_options: Partial<BaseToolRunOptions>,
Expand Down Expand Up @@ -156,21 +147,42 @@ export class PythonTool extends Tool<PythonToolOutput, PythonToolOptions> {

const prefix = "/workspace/";

const result = await this.client.execute(
{
sourceCode: await getSourceCode(),
executorId: this.options.executorId ?? "default",
files: Object.fromEntries(
inputFiles.map((file) => [`${prefix}${file.filename}`, file.pythonId]),
),
},
{ signal: run.signal },
);
let response;
const httpUrl = this.options.codeInterpreter.url + "/v1/execute";
try {
response = await fetch(httpUrl, {
method: "POST",
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({
source_code: await getSourceCode(),
executorId: this.options.executorId ?? "default",
files: Object.fromEntries(
inputFiles.map((file) => [`${prefix}${file.filename}`, file.pythonId]),
),
}),
});
} catch (error) {
if (error.cause.name == "HTTPParserError") {
throw new ToolError("Python tool over HTTP failed -- not using HTTP endpoint!", [error]);
} else {
throw new ToolError("Python tool over HTTP failed!", [error]);
}
}

if (!response?.ok) {
throw new ToolError("HTTP request failed!", [new Error(await response.text())]);
}

const result = await response.json();

// replace absolute paths in "files" with relative paths by removing "/workspace/"
// skip files that are not in "/workspace"
// skip entries that are also entries in filesInput
const filesOutput = await this.storage.download(
// @ts-ignore
Object.entries(result.files)
.map(([k, v]) => {
const file = { path: k, pythonId: v };
Expand All @@ -194,19 +206,19 @@ export class PythonTool extends Tool<PythonToolOutput, PythonToolOptions> {
})
.filter(isTruthy),
);
return new PythonToolOutput(result.stdout, result.stderr, result.exitCode, filesOutput);
return new PythonToolOutput(result.stdout, result.stderr, result.exit_code, filesOutput);
}

createSnapshot() {
return {
...super.createSnapshot(),
files: this.files,
storage: this.storage,
preprocess: this.preprocess,
};
}

loadSnapshot(snapshot: ReturnType<typeof this.createSnapshot>): void {
super.loadSnapshot(snapshot);
Object.assign(this, { client: this._createClient() });
}
}
3 changes: 3 additions & 0 deletions test_dir/test_file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This is a test.
This is only a test.

71 changes: 71 additions & 0 deletions tests/e2e/tools/python/python.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* Copyright 2024 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { expect } from "vitest";
import { PythonTool } from "@/tools/python/python.js";
import { LocalPythonStorage } from "@/tools/python/storage.js";

import { ToolError } from "@/tools/base.js";

const codeInterpreterUrl = process.env.CODE_INTERPRETER_URL || "http://localhost:50081";

const getPythonTool = () =>
new PythonTool({
codeInterpreter: { url: codeInterpreterUrl },
storage: new LocalPythonStorage({
interpreterWorkingDir: "/tmp/code-interpreter-storage",
localWorkingDir: "./test_dir/",
}),
});

describe("PythonTool", () => {
it("Returns zero exitCode and stdout results", async () => {
const result = await getPythonTool().run({
language: "python",
code: "print('hello')",
inputFiles: [],
});

expect(result.exitCode).toBe(0);
expect(result.stdout).toBe("hello\n");
});

it("Returns non-zero exitCode and stderr for bad python", async () => {
const result = await getPythonTool().run({
language: "python",
code: "PUT LIST (((ARR(I,J) DO I = 1 TO 5) DO J = 1 TO 5))",
inputFiles: [],
});

expect(result.exitCode).toBe(1);
expect(result.stderr).toMatch("SyntaxError");
});

it("Throws tool error for code exceptions", async () => {
const sourceCode = `
with open("wrong_file_here.txt", 'r') as f:
print(f.read())
`;

await expect(
getPythonTool().run({
language: "python",
code: sourceCode,
inputFiles: ["test_file.txt"],
}),
).rejects.toThrowError(ToolError);
});
});
Loading