diff --git a/internal/callers/contentgen.go b/internal/callers/contentgen.go index 0b18dac..4ae67b5 100644 --- a/internal/callers/contentgen.go +++ b/internal/callers/contentgen.go @@ -16,7 +16,7 @@ import ( ) // Loads the intent detection response & selects the appropriate workflow -func LoadIntentDetectionResponse(JSONData []byte) { +func LoadIntentDetectionResponse(JSONData []byte, originalInput string, numRetrys int) { // Read JSON data from intent detection var intentDetectionResponse structs.IntentDetectionResponse if err := json.Unmarshal(JSONData, &intentDetectionResponse); err != nil { @@ -32,10 +32,10 @@ func LoadIntentDetectionResponse(JSONData []byte) { } // Pass intent detection response & workflow to HandleWorkflow - HandleWorkflow(intentDetectionResponse, workflow) + HandleWorkflow(intentDetectionResponse, workflow, originalInput, numRetrys) } -func HandleWorkflow(intentDetectionResponse structs.IntentDetectionResponse, workflow structs.Workflow) { +func HandleWorkflow(intentDetectionResponse structs.IntentDetectionResponse, workflow structs.Workflow, originalInput string, numRetrys int) { // Storage for previous step results (ensures placeholders are accessible) dataStore := make(map[string]interface{}) @@ -67,11 +67,11 @@ func HandleWorkflow(intentDetectionResponse structs.IntentDetectionResponse, wor payload = deepReplace(step.Body, dataStore).(map[string]interface{}) // Replace placeholders for later steps } logrus.Debugf("\nšŸ“¦ API Call Configuration for Step '%s': \nURL: %s,\n Method: %s,\n Headers: %+s\n\n", step.Name, workflowConfig.Endpoint, workflowConfig.Method, workflowConfig.Headers) - logrus.Debugf("\nšŸ“¦ API Call Paylowd for Step '%s': %+s\n\n", step.Name, PrettyPrintJSON(payload)) + logrus.Debugf("\nšŸ“¦ API Call Payload for Step '%s': %+s\n\n", step.Name, PrettyPrintJSON(payload)) // Make the API call passing what we jsut created above var responseData map[string]interface{} - responseData, err := makeAPICall(workflowConfig, payload) + responseData, err := makeAPICall(workflowConfig, payload, originalInput, numRetrys) if err != nil { logrus.Errorf("\nāŒ Error in step '%s': %v\n", step.Name, err) return @@ -143,7 +143,7 @@ func buildPayload(intentDetectionResponse structs.IntentDetectionResponse) map[s } // Makes the API request & returns the response -func makeAPICall(apiConfig structs.APIConfig, payload map[string]interface{}) (map[string]interface{}, error) { +func makeAPICall(apiConfig structs.APIConfig, payload map[string]interface{}, originalInput string, numRetrys int) (map[string]interface{}, error) { client := &http.Client{} // Convert payload to JSON @@ -193,6 +193,16 @@ func makeAPICall(apiConfig structs.APIConfig, payload map[string]interface{}) (m var jsonResponse map[string]interface{} if err := json.Unmarshal(body, &jsonResponse); err == nil { logrus.Errorf("\nšŸ” API Response:\n%s\n", PrettyPrintJSON(jsonResponse)) + if numRetrys < config.General.NumIntentDetectionRetries { + // Retry intent detection + logrus.Warn("šŸ”„ Unnecessary data detected. Retrying intent detection...") + numRetrys++ + go StartIntentDetection(originalInput, numRetrys) + return nil, fmt.Errorf("retrying due to unnecessary data") + } else { + logrus.Error("\nāŒ Max retries reached. Unable to process the request. To Increase the number of retries, please update the general.yaml file.") + return nil, fmt.Errorf("max retries reached") + } } else { // If response is not JSON, print it as a raw string logrus.Errorf("\nšŸ” API Raw Response:\n%s\n", string(body)) diff --git a/internal/callers/loadprompt.go b/internal/callers/loadprompt.go index fc34c08..1caed20 100644 --- a/internal/callers/loadprompt.go +++ b/internal/callers/loadprompt.go @@ -13,14 +13,15 @@ import ( "github.com/sirupsen/logrus" ) -func StartIntentDetection(input string) { +func StartIntentDetection(input string, numRetrys int) { jsonData, err := LoadPrompt(input) if err != nil { logrus.Warn("Error running intent detection:", err) return } // Pass JSON data from intent detection to contentget.go for the call - LoadIntentDetectionResponse(jsonData) + // Also pass user text input through for potential retrying in makeAPICall in contentgen.go + LoadIntentDetectionResponse(jsonData, input, numRetrys) } // LoadPrompt sends the prompt to chat ai, then saves and returns the JSON response diff --git a/internal/config/structs/general_struct.go b/internal/config/structs/general_struct.go index 4a93757..2a34a43 100644 --- a/internal/config/structs/general_struct.go +++ b/internal/config/structs/general_struct.go @@ -3,7 +3,8 @@ package structs import _ "gopkg.in/yaml.v3" type GeneralSettings struct { - DataDir string `yaml:"dataDir"` - OpenWebsocket bool `yaml:"openWebsocket"` - Log_Level string `yaml:"log_level"` + DataDir string `yaml:"dataDir"` + OpenWebsocket bool `yaml:"openWebsocket"` + Log_Level string `yaml:"log_level"` + NumIntentDetectionRetries int `yaml:"numIntentDetectionRetries"` } diff --git a/internal/endpoints/websocket/websocket.go b/internal/endpoints/websocket/websocket.go index 70964fe..80e2680 100644 --- a/internal/endpoints/websocket/websocket.go +++ b/internal/endpoints/websocket/websocket.go @@ -51,7 +51,7 @@ func wsHandler(w http.ResponseWriter, r *http.Request) { select { case text := <-voskResponse: if keywordActive { - go callers.StartIntentDetection(text) + go callers.StartIntentDetection(text, 0) keywordActive = false err = conn.WriteMessage(websocket.TextMessage, []byte("Finished recording")) if err != nil { @@ -72,7 +72,7 @@ func wsHandler(w http.ResponseWriter, r *http.Request) { // Keep going } } else if messageType == websocket.TextMessage { - go callers.StartIntentDetection(string(message)) + go callers.StartIntentDetection(string(message), 0) } } diff --git a/internal/listeners/text.go b/internal/listeners/text.go index 1c95ae9..94f33f6 100644 --- a/internal/listeners/text.go +++ b/internal/listeners/text.go @@ -34,7 +34,7 @@ func StartTextListener() { config.LoadYaml() fmt.Println("Reloaded yaml...") } else { // Call intent detection - go callers.StartIntentDetection(input) + go callers.StartIntentDetection(input, 0) } fmt.Println()