mirror of
https://github.com/coalaura/whiskr.git
synced 2025-09-09 09:19:54 +00:00
tweaks
This commit is contained in:
92
chat.go
92
chat.go
@@ -7,6 +7,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/revrost/go-openrouter"
|
||||
@@ -117,13 +118,26 @@ func (r *Request) Parse() (*openrouter.ChatCompletionRequest, error) {
|
||||
|
||||
for index, message := range r.Messages {
|
||||
switch message.Role {
|
||||
case "system", "user":
|
||||
case "system":
|
||||
request.Messages = append(request.Messages, openrouter.ChatCompletionMessage{
|
||||
Role: message.Role,
|
||||
Content: openrouter.Content{
|
||||
Text: message.Text,
|
||||
},
|
||||
})
|
||||
case "user":
|
||||
var content openrouter.Content
|
||||
|
||||
if model.Vision && strings.Contains(message.Text, "![") {
|
||||
content.Multi = SplitImagePairs(message.Text)
|
||||
} else {
|
||||
content.Text = message.Text
|
||||
}
|
||||
|
||||
request.Messages = append(request.Messages, openrouter.ChatCompletionMessage{
|
||||
Role: message.Role,
|
||||
Content: content,
|
||||
})
|
||||
case "assistant":
|
||||
msg := openrouter.ChatCompletionMessage{
|
||||
Role: openrouter.ChatMessageRoleAssistant,
|
||||
@@ -151,7 +165,7 @@ func (r *Request) Parse() (*openrouter.ChatCompletionRequest, error) {
|
||||
}
|
||||
|
||||
func HandleChat(w http.ResponseWriter, r *http.Request) {
|
||||
debug("new chat")
|
||||
debug("parsing chat")
|
||||
|
||||
var raw Request
|
||||
|
||||
@@ -174,6 +188,9 @@ func HandleChat(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
request.Stream = true
|
||||
|
||||
dump("debug.json", request)
|
||||
debug("preparing stream")
|
||||
|
||||
response, err := NewStream(w)
|
||||
if err != nil {
|
||||
RespondJson(w, http.StatusBadRequest, map[string]any{
|
||||
@@ -325,3 +342,74 @@ func RunCompletion(ctx context.Context, response *Stream, request *openrouter.Ch
|
||||
|
||||
return tool, result.String(), nil
|
||||
}
|
||||
|
||||
func SplitImagePairs(text string) []openrouter.ChatMessagePart {
|
||||
rgx := regexp.MustCompile(`(?m)!\[[^\]]*]\((\S+?)\)`)
|
||||
|
||||
var (
|
||||
index int
|
||||
parts []openrouter.ChatMessagePart
|
||||
)
|
||||
|
||||
push := func(str, end int) {
|
||||
rest := text[str:end]
|
||||
|
||||
if rest == "" {
|
||||
return
|
||||
}
|
||||
|
||||
total := len(parts)
|
||||
|
||||
if total > 0 && parts[total-1].Type == openrouter.ChatMessagePartTypeText {
|
||||
parts[total-1].Text += rest
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
parts = append(parts, openrouter.ChatMessagePart{
|
||||
Type: openrouter.ChatMessagePartTypeText,
|
||||
Text: rest,
|
||||
})
|
||||
}
|
||||
|
||||
for {
|
||||
location := rgx.FindStringSubmatchIndex(text[index:])
|
||||
if location == nil {
|
||||
push(index, len(text)-1)
|
||||
|
||||
break
|
||||
}
|
||||
|
||||
start := index + location[0]
|
||||
end := index + location[1]
|
||||
|
||||
urlStart := index + location[2]
|
||||
urlEnd := index + location[3]
|
||||
|
||||
url := text[urlStart:urlEnd]
|
||||
|
||||
if !strings.HasPrefix(url, "https://") && !strings.HasPrefix(url, "http://") {
|
||||
push(index, end)
|
||||
|
||||
index = end
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
if start > index {
|
||||
push(index, start)
|
||||
}
|
||||
|
||||
parts = append(parts, openrouter.ChatMessagePart{
|
||||
Type: openrouter.ChatMessagePartTypeImageURL,
|
||||
ImageURL: &openrouter.ChatMessageImageURL{
|
||||
Detail: openrouter.ImageURLDetailAuto,
|
||||
URL: url,
|
||||
},
|
||||
})
|
||||
|
||||
index = end
|
||||
}
|
||||
|
||||
return parts
|
||||
}
|
||||
|
Reference in New Issue
Block a user