Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions internal/callers/contentgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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{})

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down
5 changes: 3 additions & 2 deletions internal/callers/loadprompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions internal/config/structs/general_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
4 changes: 2 additions & 2 deletions internal/endpoints/websocket/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/listeners/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down