Refactor error handling and logging in chat streaming and API methods
- Update error handling in handleChat to return and log errors - Modify streamChat to remove explicit error logging and improve retry mechanism - Adjust HookRetry method to return nil for more flexible error handling - Improve error handling in retryMessages method with better index tracking - Remove redundant error logging in requestMessages method
This commit is contained in:
parent
e59dacefa2
commit
b89a39b192
4 changed files with 33 additions and 20 deletions
|
|
@ -232,7 +232,14 @@ func (neo *DSL) handleChat(c *gin.Context) {
|
||||||
defer cancel()
|
defer cancel()
|
||||||
defer ctx.Release() // Release the context after the request is done
|
defer ctx.Release() // Release the context after the request is done
|
||||||
|
|
||||||
neo.Answer(ctx, content, c)
|
err := neo.Answer(ctx, content, c)
|
||||||
|
|
||||||
|
// Error handling
|
||||||
|
if err != nil {
|
||||||
|
message.New().Done().Error(err).Write(c.Writer)
|
||||||
|
c.Done()
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// handleChatList handles the chat list request
|
// handleChatList handles the chat list request
|
||||||
|
|
|
||||||
|
|
@ -323,10 +323,6 @@ func (ast *Assistant) handleChatStream(c *gin.Context, ctx chatctx.Context, mess
|
||||||
go func() {
|
go func() {
|
||||||
var res interface{} = nil
|
var res interface{} = nil
|
||||||
res, err = ast.streamChat(c, ctx, messages, options, clientBreak, contents, callback...)
|
res, err = ast.streamChat(c, ctx, messages, options, clientBreak, contents, callback...)
|
||||||
if err != nil {
|
|
||||||
chatMessage.New().Error(err).Done().Write(c.Writer)
|
|
||||||
err = fmt.Errorf("stream chat error %s", err.Error())
|
|
||||||
}
|
|
||||||
result = res
|
result = res
|
||||||
done <- true
|
done <- true
|
||||||
}()
|
}()
|
||||||
|
|
@ -622,16 +618,22 @@ func (ast *Assistant) streamChat(
|
||||||
ctx.RetryTimes = ctx.RetryTimes + 1 // Increment the retry times
|
ctx.RetryTimes = ctx.RetryTimes + 1 // Increment the retry times
|
||||||
ctx.Retry = true // Set the retry mode
|
ctx.Retry = true // Set the retry mode
|
||||||
|
|
||||||
|
// The maximum retry times is 9
|
||||||
|
if ctx.RetryTimes > 9 {
|
||||||
|
color.Red("Maximum retry times is 9, please check the error and fix it")
|
||||||
|
// chatMessage.New().Error(retry.Error()).Done().Callback(cb).Write(c.Writer)
|
||||||
|
return nil, retry
|
||||||
|
}
|
||||||
|
|
||||||
// Hook retry
|
// Hook retry
|
||||||
promptAny, retryErr := ast.HookRetry(c, ctx, messages, contents, exception.Trim(retry))
|
promptAny, retryErr := ast.HookRetry(c, ctx, messages, contents, exception.Trim(retry))
|
||||||
if retryErr != nil {
|
if retryErr != nil {
|
||||||
color.Red("%s, try to fix the error %d times, but failed with %s", exception.Trim(retry), ctx.RetryTimes, exception.Trim(retryErr))
|
color.Red("%s, try to fix the error %d times, but failed with %s", exception.Trim(retry), ctx.RetryTimes, exception.Trim(retryErr))
|
||||||
chatMessage.New().Error(retry.Error()).Done().Callback(cb).Write(c.Writer)
|
// chatMessage.New().Error(retry.Error()).Done().Callback(cb).Write(c.Writer)
|
||||||
return nil, retry
|
return nil, retry
|
||||||
}
|
}
|
||||||
|
|
||||||
if promptAny == nil {
|
if promptAny == nil {
|
||||||
chatMessage.New().Error(retry.Error()).Done().Callback(cb).Write(c.Writer)
|
|
||||||
return nil, retry
|
return nil, retry
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -640,7 +642,7 @@ func (ast *Assistant) streamChat(
|
||||||
case NextAction:
|
case NextAction:
|
||||||
result, err := v.Execute(c, ctx, contents, cb)
|
result, err := v.Execute(c, ctx, contents, cb)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
chatMessage.New().Error(err.Error()).Done().Callback(cb).Write(c.Writer)
|
// chatMessage.New().Error(err.Error()).Done().Callback(cb).Write(c.Writer)
|
||||||
return nil, retry
|
return nil, retry
|
||||||
}
|
}
|
||||||
return result, nil
|
return result, nil
|
||||||
|
|
@ -653,7 +655,7 @@ func (ast *Assistant) streamChat(
|
||||||
retryMessages, retryErr := ast.retryMessages(messages, prompt)
|
retryMessages, retryErr := ast.retryMessages(messages, prompt)
|
||||||
if retryErr != nil {
|
if retryErr != nil {
|
||||||
color.Red("%s, try to fix the error %d times, but failed with %s", exception.Trim(retry), ctx.RetryTimes, exception.Trim(retryErr))
|
color.Red("%s, try to fix the error %d times, but failed with %s", exception.Trim(retry), ctx.RetryTimes, exception.Trim(retryErr))
|
||||||
chatMessage.New().Error(retry.Error()).Done().Callback(cb).Write(c.Writer)
|
// chatMessage.New().Error(retry.Error()).Done().Callback(cb).Write(c.Writer)
|
||||||
return nil, retry
|
return nil, retry
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -690,7 +692,7 @@ func (ast *Assistant) streamChat(
|
||||||
func (ast *Assistant) retryMessages(messages []chatMessage.Message, prompt string) ([]chatMessage.Message, error) {
|
func (ast *Assistant) retryMessages(messages []chatMessage.Message, prompt string) ([]chatMessage.Message, error) {
|
||||||
|
|
||||||
// Get the last user message
|
// Get the last user message
|
||||||
var lastIndex int
|
var lastIndex int = -1
|
||||||
for i := len(messages) - 1; i >= 0; i-- {
|
for i := len(messages) - 1; i >= 0; i-- {
|
||||||
if messages[i].Role == "user" {
|
if messages[i].Role == "user" {
|
||||||
messages[i].Text = prompt
|
messages[i].Text = prompt
|
||||||
|
|
@ -699,7 +701,7 @@ func (ast *Assistant) retryMessages(messages []chatMessage.Message, prompt strin
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if lastIndex == 0 {
|
if lastIndex == -1 {
|
||||||
return nil, fmt.Errorf("no user message found")
|
return nil, fmt.Errorf("no user message found")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1089,7 +1091,12 @@ func (ast *Assistant) requestMessages(ctx context.Context, messages []chatMessag
|
||||||
|
|
||||||
content := message.String()
|
content := message.String()
|
||||||
if content == "" {
|
if content == "" {
|
||||||
return nil, fmt.Errorf("content must be string")
|
// fmt.Println("--------------------------------")
|
||||||
|
// fmt.Println("Request Message Error")
|
||||||
|
// utils.Dump(message)
|
||||||
|
// fmt.Println("--------------------------------")
|
||||||
|
// return nil, fmt.Errorf("content must be string")
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
newMessage := map[string]interface{}{
|
newMessage := map[string]interface{}{
|
||||||
|
|
|
||||||
|
|
@ -246,7 +246,7 @@ func (obj *objectCall) run(info *v8go.FunctionCallbackInfo) *v8go.Value {
|
||||||
var chatCtx chatctx.Context = global.ChatContext
|
var chatCtx chatctx.Context = global.ChatContext
|
||||||
chatCtx.AssistantID = assistantID
|
chatCtx.AssistantID = assistantID
|
||||||
chatCtx.ChatID = fmt.Sprintf("call_%s", uuid.New().String()) // New chat id
|
chatCtx.ChatID = fmt.Sprintf("call_%s", uuid.New().String()) // New chat id
|
||||||
chatCtx.Silent = options.Silent // Check the silent mode
|
chatCtx.Silent = options.Silent
|
||||||
|
|
||||||
// Define the callback function
|
// Define the callback function
|
||||||
var cb func(msg *chatMessage.Message) = nil
|
var cb func(msg *chatMessage.Message) = nil
|
||||||
|
|
@ -346,7 +346,7 @@ func (obj *objectCall) retry(jsArgs []v8go.Valuer, err error, input interface{},
|
||||||
delay = options.Retry.DelayMax
|
delay = options.Retry.DelayMax
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retry delay (millisecond)
|
// Wait for the delay
|
||||||
if delay > 0 {
|
if delay > 0 {
|
||||||
time.Sleep(time.Duration(delay) * time.Millisecond)
|
time.Sleep(time.Duration(delay) * time.Millisecond)
|
||||||
}
|
}
|
||||||
|
|
@ -448,7 +448,6 @@ func (obj *objectCall) retry(jsArgs []v8go.Valuer, err error, input interface{},
|
||||||
return nil, fmt.Errorf("%s occurred but failed to get the run function: %s", errmsg, fnErr.Error())
|
return nil, fmt.Errorf("%s occurred but failed to get the run function: %s", errmsg, fnErr.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call the run function
|
|
||||||
result, resErr := fn.Call(this, jsArgs...)
|
result, resErr := fn.Call(this, jsArgs...)
|
||||||
if resErr != nil {
|
if resErr != nil {
|
||||||
return nil, fmt.Errorf("%s (%d)", exception.Trim(resErr), times-1)
|
return nil, fmt.Errorf("%s (%d)", exception.Trim(resErr), times-1)
|
||||||
|
|
|
||||||
|
|
@ -153,9 +153,9 @@ func (ast *Assistant) HookRetry(c *gin.Context, context chatctx.Context, input [
|
||||||
v, err := ast.call(ctx, "Retry", c, contents, context, lastInput.String(), output, errmsg)
|
v, err := ast.call(ctx, "Retry", c, contents, context, lastInput.String(), output, errmsg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err.Error() == HookErrorMethodNotFound {
|
if err.Error() == HookErrorMethodNotFound {
|
||||||
return "", nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
return "", err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
switch v := v.(type) {
|
switch v := v.(type) {
|
||||||
|
|
@ -166,12 +166,12 @@ func (ast *Assistant) HookRetry(c *gin.Context, context chatctx.Context, input [
|
||||||
raw, _ := jsoniter.MarshalToString(v)
|
raw, _ := jsoniter.MarshalToString(v)
|
||||||
err := jsoniter.UnmarshalFromString(raw, &next)
|
err := jsoniter.UnmarshalFromString(raw, &next)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return nil, err
|
||||||
}
|
}
|
||||||
return next, nil
|
return &next, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return "", nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// HookDone Handle completion of assistant response
|
// HookDone Handle completion of assistant response
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue