From f14120c446b673870e29993d983603dd20896731 Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 27 Jan 2025 11:54:39 +0800 Subject: [PATCH] Add assistant match functionality with RAG and store support - Implemented `processAssistantMatch` method to handle assistant matching requests - Added support for limit parameter with default and maximum values - Introduced `forceStore` flag to control matching method (RAG or store) - Created placeholder methods `assistantMatchRAG` and `assistantMatchStore` - Enhanced OpenAI message handling to parse error responses - Updated message type handling to include error messages This change adds flexible assistant matching capabilities with initial infrastructure for RAG and store-based matching. --- neo/message/message.go | 13 ++++++++ neo/process.go | 68 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/neo/message/message.go b/neo/message/message.go index 300aba1b..5de1d157 100644 --- a/neo/message/message.go +++ b/neo/message/message.go @@ -181,6 +181,15 @@ func NewOpenAI(data []byte) *Message { msg.Text = message.Choices[0].Delta.Content } + case strings.Contains(text, `{"error":{`): + var errorMessage openai.ErrorMessage + if err := jsoniter.Unmarshal(data, &errorMessage); err != nil { + msg.Text = err.Error() + "\n" + string(data) + return msg + } + msg.Type = "error" + msg.Text = errorMessage.Error.Message + case strings.Contains(text, `[DONE]`): msg.IsDone = true @@ -209,6 +218,10 @@ func (m *Message) String() string { switch typ { case "text": return m.Text + + case "error": + return m.Text + default: raw, _ := jsoniter.MarshalToString(map[string]interface{}{"type": m.Type, "props": m.Props}) return raw diff --git a/neo/process.go b/neo/process.go index 6ccd0a5d..547dfa03 100644 --- a/neo/process.go +++ b/neo/process.go @@ -27,6 +27,7 @@ func init() { "assistant.delete": processAssistantDelete, "assistant.search": processAssistantSearch, "assistant.find": processAssistantFind, + "assistant.match": processAssistantMatch, // Match assistant by content and params }) } @@ -110,6 +111,73 @@ func processAssistantDelete(process *process.Process) interface{} { return gin.H{"message": "ok"} } +// processAssistantMatch process the assistant match request +func processAssistantMatch(process *process.Process) interface{} { + process.ValidateArgNums(1) + content := process.Args[0] + params := map[string]interface{}{} + if len(process.Args) > 1 { + params = process.ArgsMap(1) + } + + // Limit default to 20 + if _, has := params["limit"]; !has { + params["limit"] = 20 + } + + // Max limit to 100 + if limit, has := params["limit"]; has { + switch v := limit.(type) { + case int: + if v > 100 { + params["limit"] = 100 + } + case string: + limitInt, err := strconv.Atoi(v) + if err != nil { + exception.New("Invalid limit type: %T", 500, limit).Throw() + } + + params["limit"] = limitInt + if limitInt > 100 { + params["limit"] = 100 + } + + default: + exception.New("Invalid limit type: %T", 500, limit).Throw() + } + } + + // Force Using sotre + forceStore := false + if store, has := params["store"]; has { + switch v := store.(type) { + case bool: + forceStore = v + case int: + forceStore = v == 1 + case string: + forceStore = v == "true" || v == "1" + } + } + + // Rag Support match using RAG + if Neo.RAG != nil && !forceStore { + return assistantMatchRAG(content, params) + } + + // Match using Store + return assistantMatchStore(content, params) +} + +func assistantMatchRAG(content interface{}, params map[string]interface{}) interface{} { + return nil +} + +func assistantMatchStore(content interface{}, params map[string]interface{}) interface{} { + return nil +} + // processAssistantSearch process the assistant search request func processAssistantSearch(process *process.Process) interface{} { params := process.ArgsMap(0)