代码拉取完成,页面将自动刷新
import os
from typing import List
from IPython import get_ipython
from autogen import ConversableAgent
from autogen.coding import CodeBlock, CodeExecutor, CodeExtractor, CodeResult, MarkdownCodeExtractor
class NotebookExecutor(CodeExecutor):
@property
def code_extractor(self) -> CodeExtractor:
# Extact code from markdown blocks.
return MarkdownCodeExtractor()
def __init__(self) -> None:
# Get the current IPython instance running in this notebook.
self._ipython = get_ipython()
def execute_code_blocks(self, code_blocks: List[CodeBlock]) -> CodeResult:
log = ""
for code_block in code_blocks:
result = self._ipython.run_cell("%%capture --no-display cap\n" + code_block.code)
log += self._ipython.ev("cap.stdout")
log += self._ipython.ev("cap.stderr")
if result.result is not None:
log += str(result.result)
exitcode = 0 if result.success else 1
if result.error_before_exec is not None:
log += f"\n{result.error_before_exec}"
exitcode = 1
if result.error_in_exec is not None:
log += f"\n{result.error_in_exec}"
exitcode = 1
if exitcode != 0:
break
return CodeResult(exit_code=exitcode, output=log)
code_writer_agent = ConversableAgent(
name="CodeWriter",
system_message="You are a helpful AI assistant.\n"
"You use your coding skill to solve problems.\n"
"You have access to a IPython kernel to execute Python code.\n"
"You can suggest Python code in Markdown blocks, each block is a cell.\n"
"The code blocks will be executed in the IPython kernel in the order you suggest them.\n"
"All necessary libraries have already been installed.\n"
"Once the task is done, returns 'TERMINATE'.",
llm_config={"config_list": [{ "model": "llama3", "base_url": "http://localhost:11434/v1", "api_key": 'ollama' }]},
)
code_executor_agent = ConversableAgent(
name="CodeExecutor",
llm_config=False,
code_execution_config={"executor": NotebookExecutor()},
is_termination_msg=lambda msg: "TERMINATE" in msg.get("content", "").strip().upper(),
)
chat_result = code_executor_agent.initiate_chat(
code_writer_agent,
message="Create a plot showing the market caps of the top 7 publicly listed companies using data from Yahoo Finance.",
)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。