这是用户在 2025-8-7 20:03 为 https://blog.langchain.com/code-interpreter-api/ 保存的双语快照页面,由 沉浸式翻译 提供双语支持。了解如何保存?
Code Interpreter API

Code Interpreter API  代码解释器 API

5 min read

Editor's Note: This is another installation of our guest blog posts highlighting interesting and novel use cases. This blog is written by Shroominic who built an open source implementation of the ChatGPT Code Interpreter.
编辑注:这是我们客座博客文章系列的又一期,重点介绍有趣和新颖的应用案例。这篇博客由 Shroominic 撰写,他构建了一个 ChatGPT 代码解释器的开源实现。

Important Links:  重要链接:

In the world of open-source software, there are always exciting developments. Today, I am thrilled to announce a new project that I have been working on - Code Interpreter API. It's an implementation of the ChatGPT Code Interpreter using LangChain Agents.
在开源软件领域,总是有令人兴奋的发展。今天,我很高兴宣布我正在参与的一个新项目——代码解释器 API。它是使用 LangChain Agents 实现的 ChatGPT 代码解释器。

Keep in mind this is an unofficial implementation and I am not affiliated with OpenAI.
请记住这是一个非官方实现,并且我与 OpenAI 没有关联。

Motivation  动机

As an indie developer, I am constantly searching for new features to add to my projects. While working on a Discord bot, I attempted to incorporate the code interpreter as a feature. I never tried it at that point but got hyped by seeing people using it on YouTube.
作为一名独立开发者,我一直在寻找可以为我的项目添加的新功能。在开发一个 Discord 机器人时,我尝试将代码解释器作为一个功能加入其中。当时我并没有实际尝试,但看到 YouTube 上有人使用它后,我感到非常兴奋。

It started as an experiment, but with some tinkering, it evolved into a functional and progressively improving feature. I noticed a significant interest in this area and identified a gap in the market - there was no API for this and especially not open source.
这最初只是一个实验,但经过一些调试,它逐渐演变成一个功能完善且不断改进的功能。我注意到这个领域有极大的兴趣,并发现了一个市场空白——没有这个功能的 API,尤其是没有开源的。

Unique Features and Benefits
独特功能和优势

The biggest advantage over the already existing Code Interpreter from OpenAI is that this open-source version has internet access.
最大的优势在于,与 OpenAI 现有的 Code Interpreter 相比,这个开源版本具有互联网访问能力。

For example, this is useful when asking “Plot the Bitcoin chart of 2023” the agent uses yahoo finance to download live data about the current situation and then shows it as an image plot to the user. Another advantage is that you can use your hardware.
例如,当询问“绘制 2023 年比特币图表”时,该代理会使用雅虎财经下载有关当前情况的实时数据,然后将其以图像图表的形式展示给用户。另一个优势是你可以使用自己的硬件。

For example, if you want to do something that includes running stable diffusion you need to have a dedicated GPU that can run the model. This is not the case in the cloud environment you have from OpenAI.
例如,如果你想要做涉及运行稳定扩散的事情,你需要有一块能够运行该模型的专用 GPU。而在 OpenAI 提供的云环境中则不是这种情况。

Other features include chat memory so you can ask follow-up questions and automatic Python package installation (when missing) so the agent tries again.
其他功能包括聊天记忆,因此你可以提出后续问题,以及自动 Python 包安装(当缺失时),这样代理会再次尝试。

Technical Details  技术细节

The core of this project is a LangChain Agent, specifically the new OpenAIFunctionsAgent which gives the advantage of calling functions/tools to better handle the user needs.
这个项目的核心是一个 LangChain Agent,具体是新的 OpenAIFunctionsAgent,它具有调用函数/工具以更好地处理用户需求的优点。

For the code interpreter, we need to give it access to a Python interpreter so it can execute Python code. This interpreter is represented as a tool so the Agent can call the tool and use Python to fulfill what the user wants.
对于代码解释器,我们需要给它访问 Python 解释器的权限,以便它能够执行 Python 代码。这个解释器被表示为一个工具,这样 Agent 就可以调用这个工具并使用 Python 来实现用户的愿望。

For example, if the user asks to plot a sin wave and show it as an image, the agent writes Python code and inputs it to the Python interpreter which runs the code, outputs the image and then this image can be displayed to the user.
例如,如果用户要求绘制一个正弦波并将其显示为图像,Agent 会编写 Python 代码并将其输入到 Python 解释器中,解释器运行该代码,输出图像,然后这个图像就可以显示给用户。

Additionally, the Agent can comment on the result and explain what happened or give more details about the output.
此外,代理可以评论结果并解释发生了什么或提供更多关于输出的细节。

It is not straightforward to give LangChain Agents access to a Code Execution Environment. There is already Python REPL but this does not allow for FileIO and it also forgets variables after every run. So I had to write my solution for this: CodeBoxAPI.
让 LangChain 代理访问代码执行环境并不直接。虽然已经存在 Python REPL,但这不允许文件输入输出,并且在每次运行后都会忘记变量。因此我不得不为这个需求编写我的解决方案:CodeBoxAPI。

When starting a Code Interpreter Session it creates a small Jupyter kernel on your device similar to how Jupyter notebooks work. A CodeBox is the underlying Python execution env where the agent can run code and receive its outputs.
当启动代码解释器会话时,它会在您的设备上创建一个小型 Jupyter 内核,类似于 Jupyter 笔记本的工作方式。CodeBox 是底层 Python 执行环境,代理可以在其中运行代码并接收其输出。

A small personal computer the agent has access to and which it can use to serve the user its request.
一个代理可以访问的小型个人计算机,它可以用它来为用户提供请求服务。

Examples  示例

AI: Here is the normalized plot showing the relative performance of the tech giants (Apple, Google, Microsoft, Amazon) in 2023.
AI:这是 2023 年科技巨头(苹果、谷歌、微软、亚马逊)相对性能的标准化图表。

The plot is normalized so that the starting point of each stock is 100, which allows for easy comparison of the percentage changes in each stock's price over the period.
该曲线已进行标准化处理,使每支股票的起始点为 100,这便于比较每支股票在期间内的价格百分比变化。

Another Example  另一个示例

Prompt: ”Select representative ETFs for different sectors (Technology, Healthcare, Consumer Discretionary, etc.). Plot their performance during the last two economic cycles. Analyze how different sectors behave in different stages of an economic cycle.”
提示:“为不同行业(如科技、医疗保健、可选消费品等)选择代表性 ETF,绘制它们在过去两个经济周期中的表现。分析不同行业在经济周期的不同阶段的表现。”

Response  回复

AI: The plot above shows the performance of the selected ETFs representing the Technology, Healthcare, and Consumer Discretionary sectors during the last two economic cycles (the dot-com bubble from 2000-2002 and the financial crisis from 2007-2009).
AI:上图显示了所选代表科技、医疗保健和可选消费品行业的 ETF 在过去两个经济周期(2000-2002 年的互联网泡沫和 2007-2009 年的金融危机)中的表现。

As we can see, all sectors were negatively affected during these periods, with significant drops in value. However, the extent and timing of these drops vary between sectors.
正如我们所见,所有行业在这些时期都受到了负面影响,价值出现显著下跌。然而,这些下跌的幅度和时间在不同行业之间存在差异。

For a more detailed analysis, we could look at the performance of these sectors during different stages of the economic cycles (e.g., pre-recession, during the recession, and post-recession).
为了更详细的分析,我们可以考察这些行业在经济周期不同阶段(例如,衰退前、衰退期间和衰退后)的表现。

However, this would require a more complex analysis and might be beyond the scope of this discussion.
然而,这需要更复杂的研究,可能超出本次讨论的范围。

Please note that this analysis is quite simplified and doesn't take into account many factors that could influence the performance of these sectors during different economic cycles.
请注意,这项分析非常简化,没有考虑许多可能影响这些行业在不同经济周期中表现的因素。

For a more accurate and detailed analysis, you might want to consider consulting with a financial advisor or conducting a more in-depth research.
为了更准确和详细的分析,您可能需要咨询财务顾问或进行更深入的研究。

Roadmap  路线图

The next step is to make CodeBox production-ready, enabling developers to create public apps as it allows for easy scaling. Currently, each user requires a small sandboxed execution environment, making it challenging to deploy to production with this API.
下一步是使 CodeBox 具备生产环境就绪的能力,让开发者能够创建公共应用,因为它支持轻松扩展。目前,每个用户都需要一个小型沙盒执行环境,这使得使用该 API 部署到生产环境变得具有挑战性。

Plans also include making it available using other LLMs) like ClaudeV2 or Open Orca. In time, users may be able to run this API 100% locally and offline on their home PC.
计划还包括使用其他 LLMs(如 ClaudeV2 或 Open Orca)来提供该功能。随着时间的推移,用户可能能够在自己的家用 PC 上 100%本地和离线运行此 API。

How to Use It  如何使用

The installation is straightforward: get your OpenAI API Key here and install the package using pip. You can use the API in your Python code: start a session, generate a response based on user input - stop your session. You can find detailed instructions on the project's GitHub README.
安装过程很简单:在这里获取你的 OpenAI API Key,并使用 pip 安装该包。你可以在你的 Python 代码中使用该 API:启动一个会话,根据用户输入生成响应——停止你的会话。你可以在项目的 GitHub README 中找到详细说明。

Contribute  贡献

If you're interested in contributing to the Code Interpreter API, there are several opportunities. Have a look into the Issues I put some tagged as ToDo. But you can also work on your ideas and just push a PR. Thanks!
如果你有兴趣为 Code Interpreter API 做出贡献,有几个机会。可以查看我标记为 ToDo 的一些问题。但你也可以实现自己的想法,只需提交一个 PR。谢谢!

Do Experiments  做实验

If you have not checked out the repository I highly recommend to do so and just try out your prompts! Try out a lot of different stuff to get an idea of what is possible and what is not. If you encounter any bugs please publish them as GitHub Issues and I will try to fix them.
如果你还没有查看这个仓库,我强烈建议你去查看并尝试输入你的提示!尝试很多不同的事物,以了解可能性和不可能性。如果你遇到任何错误,请作为 GitHub 问题发布,我会尽量修复它们。

This is my first blog post ever so thanks for reading this and I hope you had fun! If you want to get updated feel free to check out my Twitter @shroominic.
这是我写的第一篇博客,感谢你阅读,希望你喜欢!如果你想获取更新,可以随时查看我的推特@shroominic。