Refactor Neo API assistant message handling and function integration

- Updated streamChat method to utilize content.Type for function handling, enhancing the interaction with function calls.
- Improved the String method in Content struct to handle function arguments more robustly, including JSON unmarshalling for completed content.
- Set default message type to "text" in NewOpenAI function, ensuring consistent message processing.

These changes enhance the flexibility and maintainability of the Neo API, paving the way for improved assistant functionalities and message management.
This commit is contained in:
Max 2025-01-15 16:35:58 +08:00
parent e7185aad10
commit 5e5524293f
3 changed files with 32 additions and 4 deletions

View file

@ -240,7 +240,7 @@ func (ast *Assistant) streamChat(
content.Append(value)
if value != "" {
// Handle stream
res, err := ast.HookStream(c, ctx, messages, content.String(), msg.Type == "tool_calls")
res, err := ast.HookStream(c, ctx, messages, content.String(), content.Type == "function")
if err == nil && res != nil {
if res.Output != "" {
value = res.Output
@ -277,7 +277,7 @@ func (ast *Assistant) streamChat(
// Call HookDone
content.SetStatus(message.ContentStatusDone)
res, hookErr := ast.HookDone(c, ctx, messages, content.String(), msg.Type == "tool_calls")
res, hookErr := ast.HookDone(c, ctx, messages, content.String(), content.Type == "function")
if hookErr == nil && res != nil {
if res.Output != "" {
chatMessage.New().

View file

@ -1,6 +1,8 @@
package message
import "fmt"
import (
jsoniter "github.com/json-iterator/go"
)
const (
// ContentStatusPending the content status pending
@ -36,7 +38,32 @@ func NewContent(typ string) *Content {
// String the content string
func (c *Content) String() string {
if c.Type == "function" {
return fmt.Sprintf(`{"id":"%s","type": "function", "function": {"name": "%s", "arguments": "%s"}}`, c.ID, c.Name, c.Bytes)
var arguments interface{} = string(c.Bytes)
if c.Status == ContentStatusDone {
var vv interface{} = nil
err := jsoniter.Unmarshal(c.Bytes, &vv)
if err != nil {
return ""
}
arguments = vv
}
data := map[string]interface{}{
"id": c.ID,
"type": "function",
"function": map[string]interface{}{
"name": c.Name,
"arguments": arguments,
},
}
raw, err := jsoniter.MarshalToString(data)
if err != nil {
return ""
}
return raw
}
return string(c.Bytes)
}

View file

@ -97,6 +97,7 @@ func NewOpenAI(data []byte) *Message {
return msg
}
msg.Type = "text"
if len(message.Choices) > 0 {
msg.Text = message.Choices[0].Delta.Content
}