From 4d841c4a3366b59a8fa56c27663a3dca0c619f01 Mon Sep 17 00:00:00 2001 From: Sh1n3zZ Date: Thu, 20 Feb 2025 20:47:38 +0800 Subject: [PATCH] feat: enhance Skylark adapter with reasoning content handling & debug log output --- adapter/skylark/chat.go | 44 ++++++++++++++++++++++++++++++++++++++- adapter/skylark/struct.go | 9 ++++++-- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/adapter/skylark/chat.go b/adapter/skylark/chat.go index 989b2bd..3532afd 100644 --- a/adapter/skylark/chat.go +++ b/adapter/skylark/chat.go @@ -108,8 +108,18 @@ func getChoice(choice model.ChatCompletionStreamResponse) *globals.Chunk { func (c *ChatInstance) CreateStreamChatRequest(props *adaptercommon.ChatProps, callback globals.Hook) error { 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) if err != nil { + if globals.DebugMode { + globals.Debug(fmt.Sprintf("[skylark] stream error: %v", err)) + } return err } defer stream.Close() @@ -120,9 +130,41 @@ func (c *ChatInstance) CreateStreamChatRequest(props *adaptercommon.ChatProps, c return nil } if err2 != nil { + if globals.DebugMode { + globals.Debug(fmt.Sprintf("[skylark] receive error: %v", 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\n\n%s", delta.Content) + } else { + choice.Content = "\n\n\n" + } + } + + if delta.ReasoningContent != nil { + if c.isFirstReasoning { + c.isFirstReasoning = false + choice.Content = fmt.Sprintf("\n%s", *delta.ReasoningContent) + } else { + choice.Content = *delta.ReasoningContent + } + } + } + + if err = callback(choice); err != nil { return err } } diff --git a/adapter/skylark/struct.go b/adapter/skylark/struct.go index 873ab94..71e4ad7 100644 --- a/adapter/skylark/struct.go +++ b/adapter/skylark/struct.go @@ -3,18 +3,23 @@ package skylark import ( factory "chat/adapter/common" "chat/globals" + "github.com/volcengine/volcengine-go-sdk/service/arkruntime" ) type ChatInstance struct { - Instance *arkruntime.Client + Instance *arkruntime.Client + isFirstReasoning bool + isReasonOver bool } func NewChatInstance(endpoint, apiKey string) *ChatInstance { //https://ark.cn-beijing.volces.com/api/v3 instance := arkruntime.NewClientWithApiKey(apiKey, arkruntime.WithBaseUrl(endpoint)) return &ChatInstance{ - Instance: instance, + Instance: instance, + isFirstReasoning: true, + isReasonOver: false, } }