Skip to content

Commit

Permalink
fix: string content
Browse files Browse the repository at this point in the history
  • Loading branch information
zhu327 committed Dec 30, 2023
1 parent 44be210 commit f519857
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
10 changes: 5 additions & 5 deletions pkg/adapter/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (g *GeminiProAdapter) GenerateContent(
cs := model.StartChat()
setGenaiChatByOpenaiRequest(cs, req)

prompt := genai.Text(req.Messages[len(req.Messages)-1].Content)
prompt := genai.Text(req.Messages[len(req.Messages)-1].StringContent())
genaiResp, err := cs.SendMessage(ctx, prompt)
if err != nil {
return nil, errors.Wrap(err, "genai send message error")
Expand All @@ -69,7 +69,7 @@ func (g *GeminiProAdapter) GenerateStreamContent(
cs := model.StartChat()
setGenaiChatByOpenaiRequest(cs, req)

prompt := genai.Text(req.Messages[len(req.Messages)-1].Content)
prompt := genai.Text(req.Messages[len(req.Messages)-1].StringContent())
iter := cs.SendMessageStream(ctx, prompt)

dataChan := make(chan string)
Expand Down Expand Up @@ -272,7 +272,7 @@ func setGenaiChatByOpenaiRequest(cs *genai.ChatSession, req *ChatCompletionReque
cs.History = append(cs.History, []*genai.Content{
{
Parts: []genai.Part{
genai.Text(message.Content),
genai.Text(message.StringContent()),
},
Role: genaiRoleUser,
},
Expand All @@ -286,14 +286,14 @@ func setGenaiChatByOpenaiRequest(cs *genai.ChatSession, req *ChatCompletionReque
case openai.ChatMessageRoleAssistant:
cs.History = append(cs.History, &genai.Content{
Parts: []genai.Part{
genai.Text(message.Content),
genai.Text(message.StringContent()),
},
Role: genaiRoleModel,
})
case openai.ChatMessageRoleUser:
cs.History = append(cs.History, &genai.Content{
Parts: []genai.Part{
genai.Text(message.Content),
genai.Text(message.StringContent()),
},
Role: genaiRoleUser,
})
Expand Down
5 changes: 5 additions & 0 deletions pkg/adapter/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package adapter

import (
"encoding/json"
"strings"

"github.com/pkg/errors"
openai "github.com/sashabaranov/go-openai"
Expand All @@ -12,6 +13,10 @@ type ChatCompletionMessage struct {
Content json.RawMessage `json:"content"`
}

func (m *ChatCompletionMessage) StringContent() string {
return strings.Trim(string(m.Content), "\"")
}

func (m *ChatCompletionMessage) MultiContent() (parts []openai.ChatMessagePart, err error) {
err = json.Unmarshal(m.Content, &parts)
if err != nil {
Expand Down

0 comments on commit f519857

Please sign in to comment.