Skip to content

Commit

Permalink
Merge pull request #76 from weni-ai/FLOWS-95/weniGPT
Browse files Browse the repository at this point in the history
Add handle for weniGPTCalled type events
  • Loading branch information
Robi9 authored Jan 31, 2024
2 parents 7661ee6 + f11f703 commit 6a0fb41
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 11 deletions.
3 changes: 3 additions & 0 deletions core/goflow/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/engine"
"github.com/nyaruka/goflow/services/webhooks"
"github.com/nyaruka/goflow/services/wenigpt"
"github.com/nyaruka/mailroom/runtime"

"github.com/shopspring/decimal"
Expand Down Expand Up @@ -71,6 +72,7 @@ func Engine(c *runtime.Config) flows.Engine {

eng = engine.NewBuilder().
WithWebhookServiceFactory(webhooks.NewServiceFactory(httpClient, httpRetries, httpAccess, webhookHeaders, c.WebhooksMaxBodyBytes)).
WithWeniGPTServiceFactory(wenigpt.NewServiceFactory(httpClient, httpRetries, httpAccess, webhookHeaders, c.WebhooksMaxBodyBytes, c.WenigptAuthToken, c.WenigptBaseURL)).
WithClassificationServiceFactory(classificationFactory(c)).
WithEmailServiceFactory(emailFactory(c)).
WithTicketServiceFactory(ticketFactory(c)).
Expand Down Expand Up @@ -98,6 +100,7 @@ func Simulator(c *runtime.Config) flows.Engine {

simulator = engine.NewBuilder().
WithWebhookServiceFactory(webhooks.NewServiceFactory(httpClient, nil, httpAccess, webhookHeaders, c.WebhooksMaxBodyBytes)).
WithWeniGPTServiceFactory(wenigpt.NewServiceFactory(httpClient, nil, httpAccess, webhookHeaders, c.WebhooksMaxBodyBytes, c.WenigptAuthToken, c.WenigptBaseURL)).
WithClassificationServiceFactory(classificationFactory(c)). // simulated sessions do real classification
WithExternalServiceServiceFactory(externalServiceFactory((c))). // and real external services
WithMsgCatalogServiceFactory(msgCatalogFactory((c))). // msg catalog
Expand Down
36 changes: 36 additions & 0 deletions core/handlers/wenigpt_called.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package handlers

import (
"context"

"github.com/jmoiron/sqlx"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/events"
"github.com/nyaruka/mailroom/core/hooks"
"github.com/nyaruka/mailroom/core/models"
"github.com/nyaruka/mailroom/runtime"
"github.com/sirupsen/logrus"
)

func init() {
models.RegisterEventHandler(events.TypeWeniGPTCalled, handleWeniGPTCalled)
}

func handleWeniGPTCalled(ctx context.Context, rt *runtime.Runtime, tx *sqlx.Tx, oa *models.OrgAssets, scene *models.Scene, e flows.Event) error {
event := e.(*events.WebhookCalledEvent)
logrus.WithFields(logrus.Fields{
"contact_uuid": scene.ContactUUID(),
"session_id": scene.SessionID(),
"url": event.URL,
"status": event.Status,
"elapsed_ms": event.ElapsedMS,
"extraction": event.Extraction,
}).Debug("wenigpt called")

_, step := scene.Session().FindStep(e.StepUUID())

// pass node and response time to the hook that monitors webhook health
scene.AppendToEventPreCommitHook(hooks.MonitorWebhooks, &hooks.WebhookCall{NodeUUID: step.NodeUUID(), Event: event})

return nil
}
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.5.2-goflow-0.144.3
replace github.com/nyaruka/goflow => github.com/weni-ai/goflow v0.6.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.5.2-goflow-0.144.3 h1:SnTRuxtzIHIt6U4yfdrY6sJAA2w9jicKyZOGOsZUxvA=
github.com/weni-ai/goflow v0.5.2-goflow-0.144.3/go.mod h1:o0xaVWP9qNcauBSlcNLa79Fm2oCPV+BDpheFRa/D40c=
github.com/weni-ai/goflow v0.6.0-goflow-0.144.3 h1:SalpB3qAqFvZkp0lsHkSJLnr6FbuKf0sivOGkfSHbk8=
github.com/weni-ai/goflow v0.6.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
6 changes: 2 additions & 4 deletions mailroom.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,10 @@ func NewMailroom(config *runtime.Config) *Mailroom {
mr.handlerForeman = NewForeman(mr.rt, mr.wg, queue.HandlerQueue, config.HandlerWorkers)

// set authentication token for zeroshot requests in goflow
apiToken := mr.rt.Config.ZeroshotAPIToken
routers.SetZeroshotToken(apiToken)
routers.SetZeroshotToken(mr.rt.Config.ZeroshotAPIToken)

// set base url for zeroshot requests
baseURL := mr.rt.Config.ZeroshotAPIUrl
routers.SetZeroshotAPIURL(baseURL)
routers.SetZeroshotAPIURL(mr.rt.Config.ZeroshotAPIUrl)

return mr
}
Expand Down
10 changes: 6 additions & 4 deletions runtime/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ type Config struct {

ZeroshotAPIToken string `help:"secret token for zeroshot API authentication and authorization"`
ZeroshotAPIUrl string `help:"zeroshot API base url"`
ChatgptKey string `help:"chat gpt api key"`
ChatgptBaseURL string `help:"chat gpt base url"`

ChatgptKey string `help:"chat gpt api key"`
ChatgptBaseURL string `help:"chat gpt base url"`

WenigptAuthToken string `help:"wenigpt authorization token"`
WenigptCookie string `help:"wenigpt cookie"`
Expand Down Expand Up @@ -144,8 +145,9 @@ func NewDefaultConfig() *Config {

ZeroshotAPIToken: "",
ZeroshotAPIUrl: "http://engine-ai.dev.cloud.weni.ai",
ChatgptBaseURL: "https://api.openai.com",
SentenxBaseURL: "https://sentenx.weni.ai",

ChatgptBaseURL: "https://api.openai.com",
SentenxBaseURL: "https://sentenx.weni.ai",
}
}

Expand Down

0 comments on commit 6a0fb41

Please sign in to comment.