1 Star 0 Fork 0

Quantfree/cb_research

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
CustomCodeExecutor.py 2.39 KB
一键复制 编辑 原始数据 按行查看 历史
realamd 提交于 2024-06-16 12:49 . init
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.",
)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/qihuayaocao/cb_research.git
git@gitee.com:qihuayaocao/cb_research.git
qihuayaocao
cb_research
cb_research
master

搜索帮助