scripts/ollama-translate/main.go

52 lines
1.1 KiB
Go
Raw Normal View History

2024-06-11 07:06:29 +02:00
package main
import (
"flag"
"fmt"
2024-06-11 07:29:31 +02:00
"os"
2024-06-12 08:01:44 +02:00
"strings"
2024-06-11 07:06:29 +02:00
)
var (
inputFile = flag.String("i", "", "input file")
2024-06-11 07:29:31 +02:00
model = flag.String("m", "llama3", "model file")
2024-06-11 07:06:29 +02:00
)
2024-06-12 08:01:44 +02:00
const (
promptStart = `Could you translate the following text into English? Try te preserve the original tone of voice as much as possible. Only answer with the translation itself, no additional comments needed.`
)
2024-06-11 07:06:29 +02:00
func main() {
flag.Parse()
2024-06-11 07:29:31 +02:00
if *inputFile == "" {
fmt.Println("No input file specified")
os.Exit(1)
}
ollamaHost := os.Getenv("OLLAMA_HOST")
if ollamaHost == "" {
fmt.Println("OLLAMA_HOST environment variable not set")
os.Exit(1)
}
ollama := NewOllama(ollamaHost)
2024-06-12 08:01:44 +02:00
doc, err := os.ReadFile(*inputFile)
2024-06-11 07:29:31 +02:00
if err != nil {
fmt.Println(err)
os.Exit(1)
}
2024-06-11 07:06:29 +02:00
2024-06-12 08:01:44 +02:00
translated := make([]string, 0)
for _, chunk := range strings.Split(string(doc), "\n\n") {
prompt := fmt.Sprintf("%s\n---\n%s", promptStart, chunk)
res, err := ollama.Generate(*model, prompt)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf(".")
translated = append(translated, res)
}
fmt.Printf("\n\n%s\n", strings.Join(translated, "\n"))
2024-06-11 07:06:29 +02:00
}