mirror of
https://github.com/coaidev/coai.git
synced 2025-05-19 04:50:14 +09:00
feat: enhance Skylark adapter with reasoning content handling & debug log output
This commit is contained in:
parent
7a57d86894
commit
4d841c4a33
@ -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</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
|
||||
}
|
||||
}
|
||||
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user