Skip to content

Commit

Permalink
update gpt4v processor
Browse files Browse the repository at this point in the history
zmh-program committed Nov 2, 2023
1 parent 24c8c9e commit 1547430
Showing 3 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions adapter/chatgpt/chat.go
Original file line number Diff line number Diff line change
@@ -48,15 +48,15 @@ func (c *ChatInstance) GetChatBody(props *ChatProps, stream bool) interface{} {
if props.Token != -1 {
return ChatRequest{
Model: props.Model,
Messages: props.Message,
Messages: formatMessages(props),
MaxToken: props.Token,
Stream: stream,
}
}

return ChatRequestWithInfinity{
Model: props.Model,
Messages: props.Message,
Messages: formatMessages(props),
Stream: stream,
}
}
15 changes: 15 additions & 0 deletions adapter/chatgpt/processor.go
Original file line number Diff line number Diff line change
@@ -24,6 +24,21 @@ func processFormat(data string) string {
return item
}

func formatMessages(props *ChatProps) []globals.Message {
if props.Model == globals.GPT4Vision {
base := props.Message[len(props.Message)-1].Content
urls := utils.ExtractUrls(base)

if len(urls) > 0 {
base = fmt.Sprintf("%s %s", strings.Join(urls, " "), base)
}
props.Message[len(props.Message)-1].Content = base
return props.Message
}

return props.Message
}

func processChatResponse(data string) *ChatStreamResponse {
if strings.HasPrefix(data, "{") {
var form *ChatStreamResponse
6 changes: 6 additions & 0 deletions utils/char.go
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"math/rand"
"regexp"
"strconv"
"strings"
"time"
@@ -131,3 +132,8 @@ func Extract(data string, length int, flow string) string {
return data
}
}

func ExtractUrls(data string) []string {
re := regexp.MustCompile(`(https?://\S+)`)
return re.FindAllString(data, -1)
}

0 comments on commit 1547430

Please sign in to comment.