Skip to content

Commit

Permalink
openai: Fixup lint issues with vision support
Browse files Browse the repository at this point in the history
  • Loading branch information
tmc committed Nov 21, 2023
1 parent 2e8425e commit c35cade
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 30 deletions.
2 changes: 1 addition & 1 deletion examples/openai-gpt4-vision-example/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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/tmc/langchaingo v0.0.0-20231121041152-264bcdd61b41

require (
github.com/dlclark/regexp2 v1.8.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions examples/openai-gpt4-vision-example/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
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=
github.com/tmc/langchaingo v0.0.0-20231121041152-264bcdd61b41 h1:6jsAPFqYKnFDtGwMtm853LHhaUNFtQMJdf7vSBk4IZg=
github.com/tmc/langchaingo v0.0.0-20231121041152-264bcdd61b41/go.mod h1:wwzKIaam0XFmiWfTlvSvdKwq7CkxE9Tz5rIkz1KKDws=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
29 changes: 12 additions & 17 deletions examples/openai-gpt4-vision-example/openai_gpt4_vision_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
"os"

"github.com/tmc/langchaingo/llms"
"github.com/tmc/langchaingo/llms/openai"
"github.com/tmc/langchaingo/schema"
)
Expand All @@ -20,7 +21,9 @@ var (

func main() {
flag.Parse()
llm, err := openai.NewChat(openai.WithModel("gpt-4-vision-preview"))
llm, err := openai.NewChat(
openai.WithModel("gpt-4-vision-preview"),
)
if err != nil {
log.Fatal(err)
}
Expand All @@ -46,12 +49,15 @@ func main() {
},
},
},
})
}, llms.WithMaxTokens(1024),
llms.WithStreamingFunc(func(ctx context.Context, chunk []byte) error {
fmt.Print(string(chunk))
return nil
}))
if err != nil {
log.Fatal(err)
}

fmt.Println(completion)
_ = completion

}

Expand All @@ -65,21 +71,10 @@ func loadImageBase64(path string) (string, error) {
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
base64Encoding := base64.StdEncoding.EncodeToString(data)
return fmt.Sprintf("data:%s;base64,%s", mimeType, base64Encoding), nil
}

func pathToReader(path string) (io.ReadCloser, error) {
Expand Down
12 changes: 2 additions & 10 deletions llms/openai/internal/openaiclient/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
)

Expand Down Expand Up @@ -143,9 +141,6 @@ func (c *Client) createChat(ctx context.Context, payload *ChatRequest) (*ChatRes
return nil, err
}

// print raw payload:
fmt.Println(string(payloadBytes))

// Build request
body := bytes.NewReader(payloadBytes)
if c.baseURL == "" {
Expand All @@ -165,9 +160,6 @@ func (c *Client) createChat(ctx context.Context, payload *ChatRequest) (*ChatRes
}
defer r.Body.Close()

buf, err := io.ReadAll(r.Body)
io.Copy(os.Stdout, bytes.NewReader(buf))

if r.StatusCode != http.StatusOK {
msg := fmt.Sprintf("API returned unexpected status code: %d", r.StatusCode)

Expand Down Expand Up @@ -228,8 +220,8 @@ func parseStreamingChatResponse(ctx context.Context, r *http.Response, payload *
continue
}
chunk := []byte(streamResponse.Choices[0].Delta.Content)
// response.Choices[0].Message.Content += streamResponse.Choices[0].Delta.Content
response.Choices[0].Message.Content = fmt.Sprintf("%s%s", response.Choices[0].Message.Content, streamResponse.Choices[0].Delta.Content)
c := fmt.Sprintf("%s%s", response.Choices[0].Message.Content, streamResponse.Choices[0].Delta.Content)
response.Choices[0].Message.Content = c
response.Choices[0].FinishReason = streamResponse.Choices[0].FinishReason
if streamResponse.Choices[0].Delta.FunctionCall != nil {
if response.Choices[0].Message.FunctionCall == nil {
Expand Down
1 change: 0 additions & 1 deletion llms/openai/openaillm_chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ func messagesToClientMessages(messages []schema.ChatMessage) []*openaiclient.Cha
msg.Name = n.GetName()
}
if cl, ok := m.(schema.ContentList); ok {
fmt.Println("has content list")
msg.Content = cl.GetContentList()
}
msgs[i] = msg
Expand Down
2 changes: 1 addition & 1 deletion schema/chat_messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ type CompoundChatMessage struct {

func (m CompoundChatMessage) GetType() ChatMessageType { return m.Type }
func (m CompoundChatMessage) GetContent() string {
b, _ := json.Marshal(m.Parts)
b, _ := json.Marshal(m.Parts) // nolint:errchkjson
return string(b)
}

Expand Down

0 comments on commit c35cade

Please sign in to comment.