Minggu ini, saya telah mengusahakan alat baris arahan yang saya namakan codeshift, yang membolehkan pengguna memasukkan fail kod sumber, memilih bahasa pengaturcaraan dan menterjemahkannya ke dalam bahasa pilihan mereka.
Tiada perkara mewah yang berlaku di bawah hud - ia hanya menggunakan penyedia AI yang dipanggil Groq untuk mengendalikan terjemahan - tetapi saya ingin masuk ke dalam proses pembangunan, cara ia digunakan dan ciri yang ditawarkannya.
Alat baris arahan yang mengubah fail kod sumber kepada mana-mana bahasa.
perubahan kod [-o
codeshift -o index.go go examples/index.js
perubahan kod [-o
Sebagai contoh, untuk menterjemah fail examples/index.js kepada Go dan simpan output ke index.go:
codeshift -o index.go go examples/index.js
Saya telah mengusahakan projek ini sebagai sebahagian daripada kursus Topik dalam Pembangunan Sumber Terbuka di Politeknik Seneca di Toronto, Ontario. Bermula, saya mahu kekal dengan teknologi yang saya selesa, tetapi arahan untuk projek itu menggalakkan kami mempelajari sesuatu yang baharu, seperti bahasa pengaturcaraan baharu atau masa jalan baharu.
Walaupun saya ingin mempelajari Java, selepas melakukan beberapa penyelidikan dalam talian, nampaknya ia bukan pilihan yang bagus untuk membangunkan alat CLI atau antara muka dengan model AI. Ia tidak disokong secara rasmi oleh OpenAI dan pustaka komuniti yang ditampilkan dalam dokumen mereka tidak digunakan lagi.
Saya sentiasa menjadi orang yang berpegang kepada teknologi popular - mereka cenderung boleh dipercayai dan mempunyai dokumentasi lengkap serta banyak maklumat yang tersedia dalam talian. Tetapi kali ini, saya memutuskan untuk melakukan perkara yang berbeza. Saya memutuskan untuk menggunakan Bun, masa jalan baharu yang menarik untuk JavaScript yang bertujuan untuk menggantikan Node.
Ternyata saya sepatutnya terjebak dengan usus saya. Saya menghadapi masalah cuba menyusun projek saya dan apa yang boleh saya lakukan ialah berharap pembangun akan menyelesaikan isu tersebut.
Dirujuk sebelum ini di sini, ditutup tanpa penyelesaian: https://github.com/openai/openai-node/issues/903
Ini adalah isu yang agak besar kerana ia menghalang penggunaan SDK semasa menggunakan pakej pemantauan Sentry terkini.
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 });
const params = { model: model, stream: true, stream_options: { include_usage: true }, messages }; const completion = await openai.chat.completions.create(params);
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)
(Included)
All operating systems (macOS, Linux)
v20.10.0
v4.56.0
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.
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.
Atas ialah kandungan terperinci Anjakan kod bangunan. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!