-
-
Notifications
You must be signed in to change notification settings - Fork 743
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples: add vision support and example
- Loading branch information
Showing
9 changed files
with
214 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module github.com/tmc/langchaingo/examples/openai-gpt4-vsion-example | ||
|
||
go 1.19 | ||
|
||
require github.com/tmc/langchaingo v0.0.0-20231028223410-5f4451567823 | ||
|
||
require ( | ||
github.com/dlclark/regexp2 v1.8.1 // indirect | ||
github.com/google/uuid v1.3.1 // indirect | ||
github.com/pkoukk/tiktoken-go v0.1.2 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/dlclark/regexp2 v1.8.1 h1:6Lcdwya6GjPUNsBct8Lg/yRPwMhABj269AAzdGSiR+0= | ||
github.com/dlclark/regexp2 v1.8.1/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= | ||
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4= | ||
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= | ||
github.com/pkoukk/tiktoken-go v0.1.2 h1:u7PCSBiWJ3nJYoTGShyM9iHXz4dNyYkurwwp+GHtyHY= | ||
github.com/pkoukk/tiktoken-go v0.1.2/go.mod h1:boMWvk9pQCOTx11pgu0DrIdrAKgQzzJKUP6vLXaz7Rw= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= | ||
github.com/tmc/langchaingo v0.0.0-20231028223410-5f4451567823 h1:vIYDdskaXk+iwfrMIaN9dM66o3wUedqvWWww1o7a1m4= | ||
github.com/tmc/langchaingo v0.0.0-20231028223410-5f4451567823/go.mod h1:wwzKIaam0XFmiWfTlvSvdKwq7CkxE9Tz5rIkz1KKDws= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= |
90 changes: 90 additions & 0 deletions
90
examples/openai-gpt4-vision-example/openai_gpt4_vision_example.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"flag" | ||
"fmt" | ||
"io" | ||
"log" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/tmc/langchaingo/llms/openai" | ||
"github.com/tmc/langchaingo/schema" | ||
) | ||
|
||
var ( | ||
flagImagePath = flag.String("image", "-", "path to image to send to model") | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
llm, err := openai.NewChat(openai.WithModel("gpt-4-vision-preview")) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
ctx := context.Background() | ||
|
||
base64Image, err := loadImageBase64(*flagImagePath) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
completion, err := llm.Call(ctx, []schema.ChatMessage{ | ||
schema.CompoundChatMessage{ | ||
Type: schema.ChatMessageTypeHuman, | ||
Parts: []schema.ChatMessageContentPart{ | ||
schema.ChatMessageContentPartText{ | ||
Type: "text", | ||
Text: "What is in this image?", | ||
}, | ||
schema.ChatMessageContentPartImage{ | ||
Type: "image_url", | ||
ImageURL: schema.ChatMessageContentPartImageURL{ | ||
URL: base64Image, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
fmt.Println(completion) | ||
|
||
} | ||
|
||
func loadImageBase64(path string) (string, error) { | ||
f, err := pathToReader(path) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to open image: %w", err) | ||
} | ||
defer f.Close() | ||
data, err := io.ReadAll(f) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to read image: %w", err) | ||
} | ||
|
||
// Determine the content type of the image file | ||
mimeType := http.DetectContentType(data) | ||
|
||
var base64Encoding string | ||
// Prepend the appropriate URI scheme header depending | ||
// on the MIME type | ||
switch mimeType { | ||
case "image/jpeg": | ||
base64Encoding += "data:image/jpeg;base64," | ||
case "image/png": | ||
base64Encoding += "data:image/png;base64," | ||
} | ||
base64Encoding += base64.StdEncoding.EncodeToString(data) | ||
return base64Encoding, nil | ||
} | ||
|
||
func pathToReader(path string) (io.ReadCloser, error) { | ||
if path == "-" { | ||
return os.Stdin, nil | ||
} | ||
return os.Open(path) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters