class JupyterCodeExecutor(kernel_name: str = 'python3'timeout: int = 60output_dir: Path | str | None = None)[source]

基类: CodeExecutorComponent[JupyterCodeExecutorConfig]

一个代码执行器类,使用 [nbclient](jupyter/nbclient) 有状态地执行代码。

危险

这将在本地机器上执行代码。如果与 LLM 生成的代码一起使用,应谨慎。

直接使用的示例

import asyncio
from autogen_core import CancellationToken
from autogen_core.code_executor import CodeBlock
from autogen_ext.code_executors.jupyter import JupyterCodeExecutor


async def main() -> None:
    async with JupyterCodeExecutor() as executor:
        cancel_token = CancellationToken()
        code_blocks = [CodeBlock(code="print('hello world!')", language="python")]
        code_result = await executor.execute_code_blocks(code_blocks, cancel_token)
        print(code_result)


asyncio.run(main())

与 PythonCodeExecutionTool 一起使用的示例

import asyncio
from autogen_agentchat.agents import AssistantAgent
from autogen_ext.code_executors.jupyter import JupyterCodeExecutor
from autogen_ext.models.openai import OpenAIChatCompletionClient
from autogen_ext.tools.code_execution import PythonCodeExecutionTool


async def main() -> None:
    async with JupyterCodeExecutor() as executor:
        tool = PythonCodeExecutionTool(executor)
        model_client = OpenAIChatCompletionClient(model="gpt-4o")
        agent = AssistantAgent("assistant", model_client=model_client, tools=[tool])
        result = await agent.run(task="What is the 10th Fibonacci number? Use Python to calculate it.")
        print(result)


asyncio.run(main())

在 CodeExecutorAgent 内部使用的示例

import asyncio
from autogen_agentchat.agents import CodeExecutorAgent
from autogen_agentchat.messages import TextMessage
from autogen_ext.code_executors.jupyter import JupyterCodeExecutor
from autogen_core import CancellationToken


async def main() -> None:
    async with JupyterCodeExecutor() as executor:
        code_executor_agent = CodeExecutorAgent("code_executor", code_executor=executor)
        task = TextMessage(
            content='''Here is some code
    ```python
    print('Hello world')
    ```
    ''',
            source="user",
        )
        response = await code_executor_agent.on_messages([task], CancellationToken())
        print(response.chat_message)


asyncio.run(main())

参数:

  • kernel_name (str) – 要使用的内核名称。默认为“python3”。

  • timeout (int) – 代码执行的超时时间,默认为 60。

  • output_dir (Path) – 保存输出文件的目录,默认为临时目录。

注意

使用当前目录(“.”)作为输出目录已弃用。使用它将引发弃用警告。

component_config_schema

的别名 JupyterCodeExecutorConfig

component_provider_override: ClassVar[str | None] = 'autogen_ext.code_executors.jupyter.JupyterCodeExecutor'

覆盖组件的提供者字符串。这应该用于防止内部模块名称成为模块名称的一部分。

async execute_code_blocks(code_blocks: list[CodeBlock]cancellation_token: CancellationToken) → JupyterCodeResult[source]

执行代码块并返回结果。

参数:

code_blocks (list[CodeBlock]) – 要执行的代码块。

返回:

JupyterCodeResult – 代码执行的结果。

async restart() → None[source]

重启代码执行器。

async start() → None[source]

(实验性) 启动代码执行器。

通过创建新的笔记本并使用指定的 Jupyter 内核进行设置来初始化 Jupyter Notebook 执行环境。将执行器标记为已启动,允许执行代码。此方法应在执行任何代码块之前调用。

async stop() → None[source]

(实验性) 停止代码执行器。

通过退出内核上下文并清理相关资源来终止 Jupyter Notebook 执行。

property output_dirPath

class JupyterCodeResult(exit_code: intoutput: stroutput_files: list[Path])[source]

基类: CodeResult

Jupyter 代码执行器的代码结果类。

output_fileslist[Path]

《AI提示工程必知必会》为读者提供了丰富的AI提示工程知识与实战技能。《AI提示工程必知必会》主要内容包括各类提示词的应用,如问答式、指令式、状态类、建议式、安全类和感谢类提示词,以及如何通过实战演练掌握提示词的使用技巧;使用提示词进行文本摘要、改写重述、语法纠错、机器翻译等语言处理任务,以及在数据挖掘、程序开发等领域的应用;AI在绘画创作上的应用,百度文心一言和阿里通义大模型这两大智能平台的特性与功能,以及市场调研中提示词的实战应用。通过阅读《AI提示工程必知必会》,读者可掌握如何有效利用AI提示工程提升工作效率,创新工作流程,并在职场中脱颖而出。

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐