UNPKG

@lobehub/chat

Version:

Lobe Chat - an open-source, high-performance chatbot framework that supports speech synthesis, multimodal, and extensible Function Call plugin system. Supports one-click free deployment of your private ChatGPT/LLM web application.

34 lines (29 loc) 1.02 kB
import { PythonInterpreter } from '@lobechat/python-interpreter'; import { CodeInterpreterResponse } from '@lobechat/types'; class PythonService { async runPython( code: string, packages: string[], files: File[], ): Promise<CodeInterpreterResponse | undefined> { if (typeof Worker === 'undefined') return; const interpreter = await new PythonInterpreter!({ pyodideIndexUrl: process.env.NEXT_PUBLIC_PYODIDE_INDEX_URL!, pypiIndexUrl: process.env.NEXT_PUBLIC_PYPI_INDEX_URL!, }); await interpreter.init(); await interpreter.installPackages(packages.filter((p) => p !== '')); await interpreter.uploadFiles(files); const result = await interpreter.runPython(code); const resultFiles = await interpreter.downloadFiles(); return { files: resultFiles.map((file) => ({ data: file, filename: file.name, previewUrl: URL.createObjectURL(file), })), ...result, }; } } export const pythonService = new PythonService();