Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7640c45

Browse files
committed
Rewritten to use Ollama SDK client and let us specify the model.
It still uses the "chat" API because of a bug in Ollama 0.4.0-rc5 thatdoes not support the "generate" API.
1 parent8d4f6c4 commit7640c45

File tree

3 files changed

+27
-77
lines changed

3 files changed

+27
-77
lines changed

‎go.mod‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ require github.com/alexflint/go-arg v1.5.1
66

77
require (
88
github.com/alexflint/go-scalarv1.2.0// indirect
9+
github.com/ollama/ollamav0.3.14// indirect
910
github.com/stretchr/testifyv1.9.0// indirect
1011
)

‎go.sum‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ github.com/alexflint/go-scalar v1.2.0 h1:WR7JPKkeNpnYIOfHRa7ivM21aWAdHD0gEWHCx+W
44
github.com/alexflint/go-scalarv1.2.0/go.mod h1:LoFvNMqS1CPrMVltza4LvnGKhaSpc3oyLEBUZVhhS2o=
55
github.com/davecgh/go-spewv1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
66
github.com/davecgh/go-spewv1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
7+
github.com/ollama/ollamav0.3.14 h1:e94+Fb1PDqmD3O90g5cqUSkSxfNm9U3fHMIyaKQ8aSc=
8+
github.com/ollama/ollamav0.3.14/go.mod h1:YrWoNkFnPOYsnDvsf/Ztb1wxU9/IXrNsQHqcxbY2r94=
79
github.com/pmezard/go-difflibv1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
810
github.com/pmezard/go-difflibv1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
911
github.com/stretchr/testifyv1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=

‎main.go‎

Lines changed: 24 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,47 @@
11
package main
22

33
import (
4-
"bytes"
5-
"encoding/base64"
6-
"encoding/json"
4+
"context"
75
"fmt"
8-
"io"
96
"log"
10-
"net/http"
117
"os"
128
"path/filepath"
139
"strings"
1410

1511
"github.com/alexflint/go-arg"
12+
"github.com/ollama/ollama/api"
1613
)
1714

18-
constOLLAMA_HOST="http://192.168.1.230:11434"
19-
20-
typeOllamaMessagestruct {
21-
Rolestring
22-
Contentstring
23-
Images []string
24-
}
25-
26-
typeOllamaRequeststruct {
27-
Modelstring
28-
Streambool
29-
Messages []OllamaMessage
30-
Formatstring
31-
Options []string
32-
}
33-
34-
typeOllamaResponsestruct {
35-
MessageOllamaMessage`json:"message"`
36-
}
37-
38-
funcGenerateWithImage(prompt,imagePathstring) (string,error) {
15+
funcGenerateWithImage(ol*api.Client,model,prompt,imagePathstring) (string,error) {
3916
// First, convert the image to base64
4017
imageData,err:=os.ReadFile(imagePath)
4118
iferr!=nil {
4219
return"",fmt.Errorf("failed to read image: %w",err)
4320
}
4421

45-
base64Image:=base64.StdEncoding.EncodeToString(imageData)
46-
47-
msg:=OllamaMessage{
22+
msg:= api.Message{
4823
Role:"user",
4924
Content:prompt,
50-
Images: []string{base64Image},
51-
}
52-
53-
// Prepare the request body
54-
reqBody:=OllamaRequest{
55-
Model:"x/llama3.2-vision",
56-
Messages: []OllamaMessage{msg},
57-
Stream:false,
58-
}
59-
60-
// Marshal the request body to JSON
61-
jsonData,err:=json.Marshal(reqBody)
62-
iferr!=nil {
63-
return"",fmt.Errorf("failed to marshal JSON: %w",err)
25+
Images: []api.ImageData{imageData},
6426
}
6527

66-
// Create a new request
67-
req,err:=http.NewRequest(
68-
"POST",
69-
OLLAMA_HOST+"/api/chat",
70-
bytes.NewBuffer(jsonData),
71-
)
72-
iferr!=nil {
73-
return"",fmt.Errorf("failed to create request: %w",err)
28+
ctx:=context.Background()
29+
req:=&api.ChatRequest{
30+
Model:model,
31+
Messages: []api.Message{msg},
7432
}
7533

76-
// Set content-type header
77-
req.Header.Set("Content-Type","application/json")
78-
79-
// Make the request
80-
client:=&http.Client{}
81-
resp,err:=client.Do(req)
82-
iferr!=nil {
83-
return"",fmt.Errorf("failed to make request: %w",err)
34+
varresponse strings.Builder
35+
respFunc:=func(resp api.ChatResponse)error {
36+
response.WriteString(resp.Message.Content)
37+
returnnil
8438
}
85-
deferresp.Body.Close()
8639

87-
// Read the response body
88-
body,err:=io.ReadAll(resp.Body)
40+
err=ol.Chat(ctx,req,respFunc)
8941
iferr!=nil {
90-
return"",fmt.Errorf("failed to read response: %w",err)
91-
}
92-
93-
// Check status code
94-
ifresp.StatusCode!=http.StatusOK {
95-
return"",fmt.Errorf("unexpected status code: %d, body: %s",resp.StatusCode,string(body))
96-
}
97-
98-
// Parse the response
99-
varresponseOllamaResponse
100-
iferr:=json.Unmarshal(body,&response);err!=nil {
101-
return"",fmt.Errorf("failed to parse response: %w",err)
42+
log.Fatal(err)
10243
}
103-
returnresponse.Message.Content,nil
44+
returnresponse.String(),nil
10445
}
10546

10647
// ProcessImages walks through a given path and processes image files
@@ -156,16 +97,22 @@ type Args struct {
15697
StartCaptionstring`arg:"--start,-s" help:"Start the caption with this (image of Leela the dog,)"`
15798
EnddCaptionstring`arg:"--end,-e" help:"End the caption with this (in the style of 'something')"`
15899
Promptstring`arg:"--prompt,-p" help:"The prompt to use" default:"Please describe the content and style of this image in detail. Answer only with one sentence that is starting with \"A ...\""`
100+
Modelstring`arg:"--model,-m" help:"The model that will be used (must be a vision model like \"llava\")" default:"x/llama3.2-vision"`
159101
}
160102

161103
funcmain() {
162104
varargsArgs
163105

164106
arg.MustParse(&args)
165107

108+
ol,err:=api.ClientFromEnvironment()
109+
iferr!=nil {
110+
log.Fatal(err)
111+
}
112+
166113
// and mention "colorized photo"
167-
err:=ProcessImages(args.Path,func(pathstring,rootstring) {
168-
captionText,err:=GenerateWithImage(args.Prompt,path)
114+
err=ProcessImages(args.Path,func(pathstring,rootstring) {
115+
captionText,err:=GenerateWithImage(ol,args.Model,args.Prompt,path)
169116
iferr!=nil {
170117
log.Fatalf("Aborting because of %v",err)
171118
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp