一种快速编程的方法,推荐给大伙。

121 天前
 mike163

就是先用 高级语言,例如 ruby python 把功能做出来,然后让大语言模型翻译成 低级语言,例如 go c/

一般来说,翻译完了还有一点小问题,一般是一些语法细节,所以你要懂目标语言,改完了就能得到全功能的目标语言文件了。

用这个办法写了好几个东西了,速度很快,还能享受低级语言的性能 一致性 容易部署。很爽。

4946 次点击
所在节点    程序员
36 条回复
aizya
121 天前
请教下 op ,我都是找网页单个文件手动翻译的,有啥便捷的方式吗?
nekoharuya
121 天前
你的想法,好几年前 facebook 就做过,但是只能做简单的小脚本,比较核心的问题是,出了 bug 完全无法排查,不过当时他们是基础的 seq2seq 模型,没现在模型那么大,设备那么烧钱,虽然目前大模型也做不了太大的项目就是了。纯人工开发的语言转译工具是有的,比如 python 有 nuitka ,不过,性能提升并不明显就是了,转译相对来说是比较复杂的问题,比如进 python3.8 的时候,我给他们提过一个 bug https://github.com/Nuitka/Nuitka/issues/630
cutchop
121 天前
最高级的语言不就是"中文"吗
cutchop
121 天前
AI 编程发展最终就是这样的,把最高级的语言“中文”翻译成最低级的“1010010100101”
mike163
121 天前
@aizya 没看懂,我是直接把代码扔给大模型,让塔用目标语言写一份新的
mike163
121 天前
@nekoharuya 以前模型能力不行。新代码质量很差。现在大模型编程能力上来了,写的代码质量比较好。
cloudzhou
121 天前
"就是先用 高级语言,例如 ruby python 把功能做出来,然后让大语言模型翻译成 低级语言,例如 go c/"

有这个时间,都可以直接 go/java 开发完成了
Kylin30
121 天前
脚本语言应该是最精确的提示词了
xuanbg
120 天前
你写一段工具代码这个办法还可以,但一个使用了多种框架的项目,你是没办法转换语言的。

PS:C 可不是什么低级语言,它也是堂堂正正的高级编程语言呢
FYFX
120 天前
go 有 gc 可能还行,c 大概率不行,我不觉得 LLM 会帮你加上有用的生命周期管理
alexhx
120 天前
依赖怎么办呢
xgdgsc
120 天前
我经常用 julia 先写然后自己翻译成 cpp 也感觉差不多,而且以后需要翻译的场景可能越来越少 https://www.reddit.com/r/Julia/comments/1g1gzlw/does_julia_still_produce_large_executables/
mike163
120 天前
@alexhx 现代大语言模型可以自动 用目标语言合适的模块 替换。这是真正厉害的地方。
levelworm
119 天前
我自己写项目就这样,先用 Python 然后翻译成 C++,不过是纯粹锻炼和好玩。公司的不用。
mike163
119 天前
一个具体的例子,用 ruby 写一个一个 自动生成 图片压缩的服务, 然后 转换成 go

require 'sinatra'
require 'open-uri'
require 'rmagick'
require 'time'


# Initialize cache
$image_cache = {}

# Cleanup task to remove stale cache entries
Thread.new do
loop do
sleep 8 * 60 * 60 # Sleep for 24 hours
current_time = Time.now
$image_cache.delete_if do |url, entry|
current_time - entry[:last_accessed] > 24 * 60 * 60
end
end
end

get '/' do
# Get the URL parameter
url = params['url']

# Ensure the URL is provided
halt 400, 'URL parameter is missing' if url.nil?

# Check cache for the image
if $image_cache.key?(url)
entry = $image_cache[url]
entry[:last_accessed] = Time.now
content_type 'image/jpeg'
return entry[:image_data]
end

# Fetch the image from the URL
begin
image_data = URI.open(url).read
rescue => e
halt 500, "Failed to fetch image: #{e.message}"
end

# Read the image data using RMagick
begin
image = Magick::Image.from_blob(image_data).first
rescue => e
halt 500, "Failed to read image: #{e.message}"
end

# Resize the image while maintaining the aspect ratio
begin
resized_image = image.change_geometry("360") do |cols, rows, img|
img.resize(cols, rows)
end
rescue => e
halt 500, "Failed to resize image: #{e.message}"
end

# Convert the resized image to a blob
resized_image_data = resized_image.to_blob

begin
# Convert the resized image to a blob
resized_image_data = resized_image.to_blob
rescue Magick::ImageMagickError => e
puts "Error: #{e.message}"
puts "Backtrace: #{e.backtrace.join("\n")}"
end

# Store the resized image in the cache
$image_cache[url] = {
image_data: resized_image_data,
last_accessed: Time.now
}

# Send the resized image data to the user
content_type 'image/jpeg'
resized_image_data
end%

转换后的代码,

package main

import (
"bytes"
"fmt"
"image"
"image/jpeg"
"log"
"net/http"
"sync"
"time"

"github.com/nfnt/resize"
)

const (
port = 8300
cacheCleanupInterval = 8 * time.Hour
cacheExpirationTime = 24 * time.Hour
)

type CacheEntry struct {
ImageData []byte
LastAccessed time.Time
}

var (
imageCache = make(map[string]*CacheEntry)
cacheMutex sync.RWMutex
)

func main() {
go cleanupCache()

http.HandleFunc("/", handleRequest)
log.Printf("Server starting on port %d", port)
log.Fatal( http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
}

func cleanupCache() {
for {
time.Sleep(cacheCleanupInterval)
currentTime := time.Now()
cacheMutex.Lock()
for url, entry := range imageCache {
if currentTime.Sub(entry.LastAccessed) > cacheExpirationTime {
delete(imageCache, url)
}
}
cacheMutex.Unlock()
}
}

func handleRequest(w http.ResponseWriter, r *http.Request) {
url := r.URL.Query().Get("url")
if url == "" {
http.Error(w, "URL parameter is missing", http.StatusBadRequest)
return
}

cacheMutex.RLock()
if entry, ok := imageCache[url]; ok {
entry.LastAccessed = time.Now()
cacheMutex.RUnlock()
w.Header().Set("Content-Type", "image/jpeg")
w.Write(entry.ImageData)
return
}
cacheMutex.RUnlock()


log.Printf("Fetching image: %v\n", url)

resp, err := http.Get(url)
if err != nil {
http.Error(w, fmt.Sprintf("Failed to fetch image: %v", err), http.StatusInternalServerError)
return
}
defer resp.Body.Close()

img, _, err := image.Decode(resp.Body)
if err != nil {
http.Error(w, fmt.Sprintf("Failed to decode image: %v", err), http.StatusInternalServerError)
return
}

resizedImg := resize.Resize(360, 0, img, resize.Lanczos3)

var buf bytes.Buffer
if err := jpeg.Encode(&buf, resizedImg, nil); err != nil {
http.Error(w, fmt.Sprintf("Failed to encode resized image: %v", err), http.StatusInternalServerError)
return
}

cacheMutex.Lock()
imageCache[url] = &CacheEntry{
ImageData: buf.Bytes(),
LastAccessed: time.Now(),
}
cacheMutex.Unlock()

w.Header().Set("Content-Type", "image/jpeg")
w.Write(buf.Bytes())
}%
nekoharuya
116 天前
@mike163 不是,我贴的这个不是大模型,这个是人工设计解析转译的,语言转译是非常复杂的,小脚本可以,大一点就没办法了,以我的黄油为例,游戏引擎,渲染框架,UI 框架,到游戏本身,数据结构,AI 算法,游戏逻辑,拼 UI ,还有周边的所有工具链,我做完这一整套,也就四五兆的代码,一个很小的文字黄油,而 AI 要做这种小项目的转译,目前希望也很渺茫

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1081537

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX