feat: enhance Skylark adapter with reasoning content handling & debug log output

This commit is contained in:
Sh1n3zZ 2025-02-20 20:47:38 +08:00
parent 7a57d86894
commit 4d841c4a33
2 changed files with 50 additions and 3 deletions

View File

@ -108,8 +108,18 @@ func getChoice(choice model.ChatCompletionStreamResponse) *globals.Chunk {
func (c *ChatInstance) CreateStreamChatRequest(props *adaptercommon.ChatProps, callback globals.Hook) error { func (c *ChatInstance) CreateStreamChatRequest(props *adaptercommon.ChatProps, callback globals.Hook) error {
req := c.CreateRequest(props) req := c.CreateRequest(props)
c.isFirstReasoning = true
c.isReasonOver = false
if globals.DebugMode {
globals.Debug(fmt.Sprintf("[skylark] request: %v", utils.Marshal(req)))
}
stream, err := c.Instance.CreateChatCompletionStream(context.Background(), req) stream, err := c.Instance.CreateChatCompletionStream(context.Background(), req)
if err != nil { if err != nil {
if globals.DebugMode {
globals.Debug(fmt.Sprintf("[skylark] stream error: %v", err))
}
return err return err
} }
defer stream.Close() defer stream.Close()
@ -120,9 +130,41 @@ func (c *ChatInstance) CreateStreamChatRequest(props *adaptercommon.ChatProps, c
return nil return nil
} }
if err2 != nil { if err2 != nil {
if globals.DebugMode {
globals.Debug(fmt.Sprintf("[skylark] receive error: %v", err2))
}
return err2 return err2
} }
if err = callback(getChoice(recv)); err != nil {
if globals.DebugMode {
globals.Debug(fmt.Sprintf("[skylark] response: %v", utils.Marshal(recv)))
}
choice := getChoice(recv)
if len(recv.Choices) > 0 {
delta := recv.Choices[0].Delta
// Handle reasoning content
if c.isFirstReasoning == false && !c.isReasonOver && delta.ReasoningContent == nil {
c.isReasonOver = true
if delta.Content != "" {
choice.Content = fmt.Sprintf("\n</think>\n\n%s", delta.Content)
} else {
choice.Content = "\n</think>\n\n"
}
}
if delta.ReasoningContent != nil {
if c.isFirstReasoning {
c.isFirstReasoning = false
choice.Content = fmt.Sprintf("<think>\n%s", *delta.ReasoningContent)
} else {
choice.Content = *delta.ReasoningContent
}
}
}
if err = callback(choice); err != nil {
return err return err
} }
} }

View File

@ -3,11 +3,14 @@ package skylark
import ( import (
factory "chat/adapter/common" factory "chat/adapter/common"
"chat/globals" "chat/globals"
"github.com/volcengine/volcengine-go-sdk/service/arkruntime" "github.com/volcengine/volcengine-go-sdk/service/arkruntime"
) )
type ChatInstance struct { type ChatInstance struct {
Instance *arkruntime.Client Instance *arkruntime.Client
isFirstReasoning bool
isReasonOver bool
} }
func NewChatInstance(endpoint, apiKey string) *ChatInstance { func NewChatInstance(endpoint, apiKey string) *ChatInstance {
@ -15,6 +18,8 @@ func NewChatInstance(endpoint, apiKey string) *ChatInstance {
instance := arkruntime.NewClientWithApiKey(apiKey, arkruntime.WithBaseUrl(endpoint)) instance := arkruntime.NewClientWithApiKey(apiKey, arkruntime.WithBaseUrl(endpoint))
return &ChatInstance{ return &ChatInstance{
Instance: instance, Instance: instance,
isFirstReasoning: true,
isReasonOver: false,
} }
} }