Merge pull request #892 from trheyi/main
Refactor error handling and logging in chat streaming and API methods
This commit is contained in:
commit
902d63e7f2
4 changed files with 33 additions and 20 deletions
|
|
@ -232,7 +232,14 @@ func (neo *DSL) handleChat(c *gin.Context) {
|
|||
defer cancel()
|
||||
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
|
||||
|
|
|
|||
|
|
@ -323,10 +323,6 @@ func (ast *Assistant) handleChatStream(c *gin.Context, ctx chatctx.Context, mess
|
|||
go func() {
|
||||
var res interface{} = nil
|
||||
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
|
||||
done <- true
|
||||
}()
|
||||
|
|
@ -622,16 +618,22 @@ func (ast *Assistant) streamChat(
|
|||
ctx.RetryTimes = ctx.RetryTimes + 1 // Increment the retry times
|
||||
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
|
||||
promptAny, retryErr := ast.HookRetry(c, ctx, messages, contents, exception.Trim(retry))
|
||||
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))
|
||||
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
|
||||
}
|
||||
|
||||
if promptAny == nil {
|
||||
chatMessage.New().Error(retry.Error()).Done().Callback(cb).Write(c.Writer)
|
||||
return nil, retry
|
||||
}
|
||||
|
||||
|
|
@ -640,7 +642,7 @@ func (ast *Assistant) streamChat(
|
|||
case NextAction:
|
||||
result, err := v.Execute(c, ctx, contents, cb)
|
||||
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 result, nil
|
||||
|
|
@ -653,7 +655,7 @@ func (ast *Assistant) streamChat(
|
|||
retryMessages, retryErr := ast.retryMessages(messages, prompt)
|
||||
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))
|
||||
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
|
||||
}
|
||||
|
||||
|
|
@ -690,7 +692,7 @@ func (ast *Assistant) streamChat(
|
|||
func (ast *Assistant) retryMessages(messages []chatMessage.Message, prompt string) ([]chatMessage.Message, error) {
|
||||
|
||||
// Get the last user message
|
||||
var lastIndex int
|
||||
var lastIndex int = -1
|
||||
for i := len(messages) - 1; i >= 0; i-- {
|
||||
if messages[i].Role == "user" {
|
||||
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")
|
||||
}
|
||||
|
||||
|
|
@ -1089,7 +1091,12 @@ func (ast *Assistant) requestMessages(ctx context.Context, messages []chatMessag
|
|||
|
||||
content := message.String()
|
||||
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{}{
|
||||
|
|
|
|||
|
|
@ -246,7 +246,7 @@ func (obj *objectCall) run(info *v8go.FunctionCallbackInfo) *v8go.Value {
|
|||
var chatCtx chatctx.Context = global.ChatContext
|
||||
chatCtx.AssistantID = assistantID
|
||||
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
|
||||
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
|
||||
}
|
||||
|
||||
// Retry delay (millisecond)
|
||||
// Wait for the delay
|
||||
if delay > 0 {
|
||||
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())
|
||||
}
|
||||
|
||||
// Call the run function
|
||||
result, resErr := fn.Call(this, jsArgs...)
|
||||
if resErr != nil {
|
||||
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)
|
||||
if err != nil {
|
||||
if err.Error() == HookErrorMethodNotFound {
|
||||
return "", nil
|
||||
return nil, nil
|
||||
}
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
switch v := v.(type) {
|
||||
|
|
@ -166,12 +166,12 @@ func (ast *Assistant) HookRetry(c *gin.Context, context chatctx.Context, input [
|
|||
raw, _ := jsoniter.MarshalToString(v)
|
||||
err := jsoniter.UnmarshalFromString(raw, &next)
|
||||
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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue