mirror of https://github.com/velour/catbase.git
rest: bypass jq when it's convenient
Adding a custom parser for GPT-2 queries because it's easier this way.
This commit is contained in:
parent
da69df36d4
commit
646282b233
|
@ -28,6 +28,24 @@ type RestPlugin struct {
|
|||
handlers bot.HandlerTable
|
||||
}
|
||||
|
||||
type postProcessor func(interface{}) string
|
||||
|
||||
var postProcessors = map[string]postProcessor{
|
||||
"gpt2": func(input interface{}) string {
|
||||
values := input.(map[string]interface{})
|
||||
text := values["text"].(string)
|
||||
lastStop := strings.LastIndexAny(text, ".!?")
|
||||
if lastStop > 0 {
|
||||
text = text[:lastStop+1]
|
||||
}
|
||||
eot := strings.LastIndex(text, "<|endoftext|>")
|
||||
if eot > 0 {
|
||||
text = text[:eot]
|
||||
}
|
||||
return text
|
||||
},
|
||||
}
|
||||
|
||||
func New(b bot.Bot) *RestPlugin {
|
||||
p := &RestPlugin{
|
||||
b: b,
|
||||
|
@ -298,6 +316,10 @@ func (p *RestPlugin) mkHandler(w wire) bot.ResponseHandler {
|
|||
var returnValues interface{}
|
||||
json.Unmarshal(body, &returnValues)
|
||||
|
||||
var msg string
|
||||
if pp, ok := postProcessors[w.ReturnField]; ok {
|
||||
msg = pp(returnValues)
|
||||
} else {
|
||||
query, err := gojq.Parse(w.ReturnField)
|
||||
if err != nil {
|
||||
msg := fmt.Sprintf("Wire handler did not find return value: %s => `%s`", w.URL, w.ReturnField)
|
||||
|
@ -305,7 +327,6 @@ func (p *RestPlugin) mkHandler(w wire) bot.ResponseHandler {
|
|||
return true
|
||||
}
|
||||
|
||||
var msg string
|
||||
iter := query.Run(returnValues) // or query.RunWithContext
|
||||
|
||||
for {
|
||||
|
@ -318,6 +339,7 @@ func (p *RestPlugin) mkHandler(w wire) bot.ResponseHandler {
|
|||
}
|
||||
msg += fmt.Sprintf("%s\n", v)
|
||||
}
|
||||
}
|
||||
|
||||
msg = strings.TrimSpace(msg)
|
||||
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, msg)
|
||||
|
@ -327,7 +349,7 @@ func (p *RestPlugin) mkHandler(w wire) bot.ResponseHandler {
|
|||
|
||||
func (p *RestPlugin) handleErr(err error, r bot.Request) bool {
|
||||
if err != nil {
|
||||
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "Error: %s", err)
|
||||
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("Error: %s", err))
|
||||
return true
|
||||
}
|
||||
return false
|
||||
|
|
Loading…
Reference in New Issue