Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement changes to preserve the insertion order in the product list #86

Merged
merged 5 commits into from
Feb 16, 2024
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
5 changes: 0 additions & 5 deletions core/handlers/msg_catalog_created_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,6 @@ func TestMsgCatalogCreated(t *testing.T) {
testdata.Cathy: msg1,
},
SQLAssertions: []handlers.SQLAssertion{
{
SQL: "SELECT COUNT(*) FROM msgs_msg WHERE contact_id = $1 AND metadata = $2 AND high_priority = TRUE",
Args: []interface{}{testdata.Cathy.ID, `{"action":"View Products","body":"Some products","products":{"product_retailer_id":["9f526c6f-b2cb-4457-8048-a7f1dc101e50","eb2305cc-bf39-43ad-a069-bbbfb6401acc"]},"send_catalog":false}`},
Count: 2,
},
{
SQL: "SELECT COUNT(*) FROM msgs_msg WHERE contact_id = $1 AND status = 'Q' AND high_priority = FALSE",
Args: []interface{}{testdata.George.ID},
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ go 1.17

replace github.com/nyaruka/gocommon => github.com/Ilhasoft/gocommon v1.16.2-weni

replace github.com/nyaruka/goflow => github.com/weni-ai/goflow v0.6.3-goflow-0.144.3
replace github.com/nyaruka/goflow => github.com/weni-ai/goflow v0.7.0-goflow-0.144.3
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ github.com/tj/assert v0.0.0-20171129193455-018094318fb0/go.mod h1:mZ9/Rh9oLWpLLD
github.com/tj/go-elastic v0.0.0-20171221160941-36157cbbebc2/go.mod h1:WjeM0Oo1eNAjXGDx2yma7uG2XoyRZTq1uv3M/o7imD0=
github.com/tj/go-kinesis v0.0.0-20171128231115-08b17f58cb1b/go.mod h1:/yhzCV0xPfx6jb1bBgRFjl5lytqVqZXEaeqWP8lTEao=
github.com/tj/go-spin v1.1.0/go.mod h1:Mg1mzmePZm4dva8Qz60H2lHwmJ2loum4VIrLgVnKwh4=
github.com/weni-ai/goflow v0.6.3-goflow-0.144.3 h1:YnuWafX2GlipcJxalqg8CLbLuiKXCOTpP30US93HY7g=
github.com/weni-ai/goflow v0.6.3-goflow-0.144.3/go.mod h1:o0xaVWP9qNcauBSlcNLa79Fm2oCPV+BDpheFRa/D40c=
github.com/weni-ai/goflow v0.7.0-goflow-0.144.3 h1:hqrUH+n3XSoJDQoMc4rn+z9kXWVAMCph0nFC1NSx0Y0=
github.com/weni-ai/goflow v0.7.0-goflow-0.144.3/go.mod h1:o0xaVWP9qNcauBSlcNLa79Fm2oCPV+BDpheFRa/D40c=
go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down
21 changes: 17 additions & 4 deletions services/external/weni/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,10 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log
return callResult, err
}

productRetailerIDS := map[string][]string{}
productRetailerIDS := []string{}
productRetailerIDMap := make(map[string]struct{})
var productEntries []flows.ProductEntry
var productEntry flows.ProductEntry
searchResult := []string{}
var trace *httpx.Trace
var traces []*httpx.Trace
Expand All @@ -127,13 +129,20 @@ func (s *service) Call(session flows.Session, params assets.MsgCatalogParam, log
productRetailerID := prod
_, exists := productRetailerIDMap[productRetailerID]
if !exists {
productRetailerIDS[product] = append(productRetailerIDS[product], productRetailerID)
productRetailerIDS = append(productRetailerIDS, productRetailerID)
productRetailerIDMap[productRetailerID] = struct{}{}
}
}

productEntry = flows.ProductEntry{
Product: product,
ProductRetailerIDs: productRetailerIDS,
}
productEntries = append(productEntries, productEntry)
productRetailerIDS = nil
}

callResult.ProductRetailerIDS = productRetailerIDS
callResult.ProductRetailerIDS = productEntries

return callResult, nil
}
Expand Down Expand Up @@ -210,11 +219,15 @@ func GetProductListFromChatGPT(ctx context.Context, rtConfig *runtime.Config, co
Role: chatgpt.ChatMessageRoleSystem,
Content: "Ensure that no product names are repeated, and each product should be in singular form without any numbers or quantities.",
}
prompt5 := chatgpt.ChatCompletionMessage{
Role: chatgpt.ChatMessageRoleSystem,
Content: "Preserve the order of products as they appear in the user prompt.",
}
question := chatgpt.ChatCompletionMessage{
Role: chatgpt.ChatMessageRoleUser,
Content: content,
}
completionRequest := chatgpt.NewChatCompletionRequest([]chatgpt.ChatCompletionMessage{prompt1, prompt2, prompt3, prompt4, question})
completionRequest := chatgpt.NewChatCompletionRequest([]chatgpt.ChatCompletionMessage{prompt1, prompt2, prompt3, prompt4, prompt5, question})
response, trace, err := chatGPTClient.CreateChatCompletion(completionRequest)
if err != nil {
return nil, trace, errors.Wrapf(err, "error on chatgpt call for list products")
Expand Down
6 changes: 3 additions & 3 deletions services/external/weni/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func TestService(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, call)
fmt.Println(call.ProductRetailerIDS)
assert.Equal(t, "p1", call.ProductRetailerIDS["banana"])
assert.Equal(t, "p1", call.ProductRetailerIDS[0].ProductRetailerIDs[0])
assert.NotNil(t, call.Traces)

params = assets.MsgCatalogParam{
Expand All @@ -134,7 +134,7 @@ func TestService(t *testing.T) {
call, err = svc.Call(session, params, logger.Log)
assert.NoError(t, err)
assert.NotNil(t, call)
assert.Equal(t, "1236", call.ProductRetailerIDS["banana"])
assert.Equal(t, "1236", call.ProductRetailerIDS[0].ProductRetailerIDs[0])
assert.NotNil(t, call.Traces)

params = assets.MsgCatalogParam{
Expand All @@ -148,6 +148,6 @@ func TestService(t *testing.T) {
call, err = svc.Call(session, params, logger.Log)
assert.NoError(t, err)
assert.NotNil(t, call)
assert.Equal(t, "1234", call.ProductRetailerIDS["banana"])
assert.Equal(t, "1234", call.ProductRetailerIDS[0].ProductRetailerIDs[0])
assert.NotNil(t, call.Traces)
}
Loading