diff --git a/ollama-translate/main.go b/ollama-translate/main.go index fe1bb81..5f59eb1 100644 --- a/ollama-translate/main.go +++ b/ollama-translate/main.go @@ -3,14 +3,31 @@ package main import ( "flag" "fmt" + "os" ) var ( inputFile = flag.String("i", "", "input file") + model = flag.String("m", "llama3", "model file") ) func main() { flag.Parse() + 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) + res, err := ollama.Generate(*model, "Could you translate the following text into English? 'Mijn fietsband is lek. Wat moet ik nu doen'") + if err != nil { + fmt.Println(err) + os.Exit(1) + } - fmt.Println("Hello World") + fmt.Println(res) } diff --git a/ollama-translate/ollama.go b/ollama-translate/ollama.go new file mode 100644 index 0000000..6fd31ac --- /dev/null +++ b/ollama-translate/ollama.go @@ -0,0 +1,64 @@ +package main + +import ( + "bytes" + "encoding/json" + "fmt" + "io" + "net/http" +) + +type Ollama struct { + baseURL string + c *http.Client +} + +func NewOllama(baseURL string) *Ollama { + return &Ollama{ + baseURL: baseURL, + c: &http.Client{}, + } +} + +func (o *Ollama) Generate(model, prompt string) (string, error) { + url := fmt.Sprintf("%s/api/generate", o.baseURL) + reqBody := struct { + Model string + Prompt string + Format string + Stream bool + }{ + Model: model, + Prompt: prompt, + Format: "json", + Stream: false, + } + reqBodyJSON, err := json.Marshal(reqBody) + if err != nil { + return "", err + } + + req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(reqBodyJSON)) + if err != nil { + return "", err + } + res, err := o.c.Do(req) + if err != nil { + return "", err + } + + body, err := io.ReadAll(res.Body) + if err != nil { + return "", err + } + defer res.Body.Close() + + resBody := struct { + Response string + }{} + if err := json.Unmarshal(body, &resBody); err != nil { + return "", err + } + + return resBody.Response, nil +}