Skip to content

Commit

Permalink
Merge pull request #837 from trheyi/main
Browse files Browse the repository at this point in the history
Add assistant match functionality with RAG and store support
  • Loading branch information
trheyi authored Jan 27, 2025
2 parents 68d0f26 + f14120c commit 73d6f69
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
13 changes: 13 additions & 0 deletions neo/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
68 changes: 68 additions & 0 deletions neo/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func init() {
"assistant.delete": processAssistantDelete,
"assistant.search": processAssistantSearch,
"assistant.find": processAssistantFind,
"assistant.match": processAssistantMatch, // Match assistant by content and params
})
}

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 73d6f69

Please sign in to comment.