目录
特点
安装
用法
示例
选项
参数
确认这是 Node 库问题,而不是底层 OpenAI API 问题
描述错误
重现
Code snippets
OS
Node version
Library version
首页 web前端 js教程 构建码移

构建码移

Sep 10, 2024 am 11:08 AM

本周,我一直在开发一个名为 codeshift 的命令行工具,它可以让用户输入源代码文件,选择编程语言,并将其翻译成他们选择的语言。

Building codeshift

幕后并没有什么花哨的东西 - 它只是使用名为 Groq 的 AI 提供商来处理翻译 - 但我想了解开发过程、它的使用方式以及它提供的功能。

Building codeshift 乌代拉纳 / 码移

代码转换

将源代码文件转换为任何语言的命令行工具。

Building codeshift

特点

  • 接受多个输入文件
  • 将输出流式传输到标准输出
  • 可以选择输出语言
  • 可以指定文件路径将输出写入文件
  • 可以在.env中使用自定义API密钥

安装

  • 安装 Node.js
  • 获取 Groq API 密钥
  • 使用 Git 克隆存储库或下载为 .zip
  • 在包含 package.json 的 repo 目录中,运行 npm install
    • (可选)运行 npm install -g 。全局安装该包(让您无需添加节点前缀即可运行它)
  • 创建一个名为 .env 的文件并添加您的 Groq API 密钥:GROQ_API_KEY=API_KEY_HERE

用法

codeshift [-o ]

示例

codeshift -o index.go go Examples/index.js

Building codeshift

选项

  • -o, --output: 指定将输出写入
  • 的文件名
  • -h, --help: 显示命令的帮助
  • -v, --version: 输出版本号

参数

  • :将源文件转换为所需的语言
  • :路径...
在 GitHub 上查看

特征

  • 接受多个输入文件
  • 可以选择输出语言
  • 将输出流式传输到标准输出
  • 可以指定文件路径将输出写入文件
  • 可以在.env中使用自定义API密钥

用法

codeshift [-o ]

例如,要将文件examples/index.js 翻译为Go 并将输出保存到index.go:

codeshift -o index.go go Examples/index.js

Building codeshift

选项

  • -o, --output: 指定文件名以将输出写入
  • -h, --help: 显示命令的帮助
  • -v, --version: 输出版本号

论据

  • :将源文件转换为的所需语言
  • :源文件的路径,以空格分隔

发展

我一直致力于这个项目,作为安大略省多伦多塞内卡理工学院开源开发主题课程的一部分。一开始,我想坚持使用我熟悉的技术,但该项目的说明鼓励我们学习新的东西,比如新的编程语言或新的运行时。

虽然我一直想学习 Java,但在网上做了一些研究后,它似乎不是开发 CLI 工具或与 AI 模型交互的最佳选择。它没有得到 OpenAI 的正式支持,并且其文档中的社区库已被弃用。

我一直坚持使用流行技术 - 它们往往很可靠,并且拥有完整的文档和大量在线信息。但这一次,我决定采取不同的做法。我决定使用 Bun,这是一个很酷的新 JavaScript 运行时,旨在取代 Node。

事实证明我应该坚持我的直觉。我在尝试编译我的项目时遇到了麻烦,我所能做的就是希望开发人员能够解决这个问题。

无法将 OpenAI SDK 与 Sentry Node 代理一起使用:TypeError: getDefaultAgent is not a function 第1010章

Building codeshift
基思沃尔 发布于

确认这是 Node 库问题,而不是底层 OpenAI API 问题

  • [X] 这是 Node 库的问题

描述错误

之前在这里引用过,未解决就关闭:https://github.com/openai/openai-node/issues/903

这是一个相当大的问题,因为它会阻止在使用最新的 Sentry 监控包时使用 SDK。

重现

  1. 通过 npm i @sentry/node --save 安装 Sentry Node sdk
  2. 输入以下代码;
import * as Sentry from '@sentry/node';

// Start Sentry
  Sentry.init({
    dsn: "https://your-sentry-url",
    environment: "your-env",
    tracesSampleRate: 1.0, //  Capture 100% of the transactions
  });
登录后复制
进入全屏模式 退出全屏模式
  1. Try to create a completion somewhere in the process after Sentry has been initialized:
const params = {
  model: model,
  stream: true,
  stream_options: {
    include_usage: true
  },
  messages
};
const completion = await openai.chat.completions.create(params);
登录后复制
Enter fullscreen mode Exit fullscreen mode

Results in error:

TypeError: getDefaultAgent is not a function
    at OpenAI.buildRequest (file:///my-project/node_modules/openai/core.mjs:208:66)
    at OpenAI.makeRequest (file:///my-project/node_modules/openai/core.mjs:279:44)
登录后复制

Code snippets

(Included)

OS

All operating systems (macOS, Linux)

Node version

v20.10.0

Library version

v4.56.0

View on GitHub

This turned me away from Bun. I'd found out from our professor we were going to compile an executable later in the course, and I did not want to deal with Bun's problems down the line.

So, I switched to Node. It was painful going from Bun's easy-to-use built-in APIs to having to learn how to use commander for Node. But at least it wouldn't crash.

I had previous experience working with AI models through code thanks to my co-op, but I was unfamiliar with creating a command-line tool. Configuring the options and arguments turned out to be the most time-consuming aspect of the project.

Apart from the core feature we chose for each of our projects - mine being code translation - we were asked to implement any two additional features. One of the features I chose to implement was to save output to a specified file. Currently, I'm not sure this feature is that useful, since you could just redirect the output to a file, but in the future I want to use it to extract the code from the response to the file, and include the AI's rationale behind the translation in the full response to stdout. Writing this feature also helped me learn about global and command-based options using commander.js. Since there was only one command (run) and it was the default, I wanted the option to show up in the default help menu, not when you specifically typed codeshift help run, so I had to learn to implement it as a global option.

I also ended up "accidentally" implementing the feature for streaming the response to stdout. I was at first scared away from streaming, because it sounded too difficult. But later, when I was trying to read the input files, I figured reading large files in chunks would be more efficient. I realized I'd already implemented streaming in my previous C++ courses, and figuring it wouldn't be too bad, I got to work.

Then, halfway through my implementation I realized I'd have to send the whole file at once to the AI regardless.

But this encouraged me to try streaming the output from the AI. So I hopped on MDN and started reading about ReadableStreams and messing around with ReadableStreamDefaultReader.read() for what felt like an hour - only to scroll down the AI provider's documentation and realize all I had to do was add stream: true to my request.

Either way, I may have taken the scenic route but I ended up implementing streaming.

Planned Features

Right now, the program parses each source file individually, with no shared context. So if a file references another, it wouldn't be reflected in the output. I'd like to enable it to have that context eventually. Like I mentioned, another feature I want to add is writing the AI's reasoning behind the translation to stdout but leaving it out of the output file. I'd also like to add some of the other optional features, like options to specify the AI model to use, the API key to use, and reading that data from a .env file in the same directory.

That's about it for this post. I'll be writing more in the coming weeks.

以上是构建码移的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

<🎜>:泡泡胶模拟器无穷大 - 如何获取和使用皇家钥匙
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系统,解释
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆树的耳语 - 如何解锁抓钩
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Java教程
1673
14
CakePHP 教程
1429
52
Laravel 教程
1333
25
PHP教程
1278
29
C# 教程
1257
24
Python vs. JavaScript:学习曲线和易用性 Python vs. JavaScript:学习曲线和易用性 Apr 16, 2025 am 12:12 AM

Python更适合初学者,学习曲线平缓,语法简洁;JavaScript适合前端开发,学习曲线较陡,语法灵活。1.Python语法直观,适用于数据科学和后端开发。2.JavaScript灵活,广泛用于前端和服务器端编程。

JavaScript和Web:核心功能和用例 JavaScript和Web:核心功能和用例 Apr 18, 2025 am 12:19 AM

JavaScript在Web开发中的主要用途包括客户端交互、表单验证和异步通信。1)通过DOM操作实现动态内容更新和用户交互;2)在用户提交数据前进行客户端验证,提高用户体验;3)通过AJAX技术实现与服务器的无刷新通信。

JavaScript在行动中:现实世界中的示例和项目 JavaScript在行动中:现实世界中的示例和项目 Apr 19, 2025 am 12:13 AM

JavaScript在现实世界中的应用包括前端和后端开发。1)通过构建TODO列表应用展示前端应用,涉及DOM操作和事件处理。2)通过Node.js和Express构建RESTfulAPI展示后端应用。

了解JavaScript引擎:实施详细信息 了解JavaScript引擎:实施详细信息 Apr 17, 2025 am 12:05 AM

理解JavaScript引擎内部工作原理对开发者重要,因为它能帮助编写更高效的代码并理解性能瓶颈和优化策略。1)引擎的工作流程包括解析、编译和执行三个阶段;2)执行过程中,引擎会进行动态优化,如内联缓存和隐藏类;3)最佳实践包括避免全局变量、优化循环、使用const和let,以及避免过度使用闭包。

Python vs. JavaScript:社区,图书馆和资源 Python vs. JavaScript:社区,图书馆和资源 Apr 15, 2025 am 12:16 AM

Python和JavaScript在社区、库和资源方面的对比各有优劣。1)Python社区友好,适合初学者,但前端开发资源不如JavaScript丰富。2)Python在数据科学和机器学习库方面强大,JavaScript则在前端开发库和框架上更胜一筹。3)两者的学习资源都丰富,但Python适合从官方文档开始,JavaScript则以MDNWebDocs为佳。选择应基于项目需求和个人兴趣。

Python vs. JavaScript:开发环境和工具 Python vs. JavaScript:开发环境和工具 Apr 26, 2025 am 12:09 AM

Python和JavaScript在开发环境上的选择都很重要。1)Python的开发环境包括PyCharm、JupyterNotebook和Anaconda,适合数据科学和快速原型开发。2)JavaScript的开发环境包括Node.js、VSCode和Webpack,适用于前端和后端开发。根据项目需求选择合适的工具可以提高开发效率和项目成功率。

C/C在JavaScript口译员和编译器中的作用 C/C在JavaScript口译员和编译器中的作用 Apr 20, 2025 am 12:01 AM

C和C 在JavaScript引擎中扮演了至关重要的角色,主要用于实现解释器和JIT编译器。 1)C 用于解析JavaScript源码并生成抽象语法树。 2)C 负责生成和执行字节码。 3)C 实现JIT编译器,在运行时优化和编译热点代码,显着提高JavaScript的执行效率。

Python vs. JavaScript:比较用例和应用程序 Python vs. JavaScript:比较用例和应用程序 Apr 21, 2025 am 12:01 AM

Python更适合数据科学和自动化,JavaScript更适合前端和全栈开发。1.Python在数据科学和机器学习中表现出色,使用NumPy、Pandas等库进行数据处理和建模。2.Python在自动化和脚本编写方面简洁高效。3.JavaScript在前端开发中不可或缺,用于构建动态网页和单页面应用。4.JavaScript通过Node.js在后端开发中发挥作用,支持全栈开发。

See all articles