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 markdown cleaner and yaml linter #3

Merged
merged 2 commits into from
Aug 16, 2023
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
2 changes: 1 addition & 1 deletion config/sample.cfg.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
userId: $USERID
apiKey: $APIKEY
- provider: openai
modelId: chatgpt-3.5-turbo
modelId: gpt-3.5-turbo
url: https://api.openai.com
apiKey: $APIKEY
7 changes: 1 addition & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ go 1.19

require (
github.com/gorilla/mux v1.8.0
github.com/gorilla/sessions v1.2.1
github.com/labstack/echo/v4 v4.11.1
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.7.0
golang.org/x/oauth2 v0.11.0
github.com/writeas/go-strip-markdown v2.0.1+incompatible
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/gorilla/securecookie v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/labstack/gommon v0.4.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
Expand All @@ -26,6 +23,4 @@ require (
golang.org/x/net v0.14.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.31.0 // indirect
)
27 changes: 2 additions & 25 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyCS8BvQ=
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
github.com/gorilla/sessions v1.2.1 h1:DHd3rPN5lE3Ts3D8rKkQ8x/0kqfeNmBAaiSi+o7FsgI=
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/labstack/echo/v4 v4.11.1 h1:dEpLU2FLg4UVmvCGPuk/APjlH6GDpbEPti61srUUUs4=
Expand Down Expand Up @@ -44,15 +34,12 @@ github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyC
github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
github.com/writeas/go-strip-markdown v2.0.1+incompatible h1:IIqxTM5Jr7RzhigcL6FkrCNfXkvbR+Nbu1ls48pXYcw=
github.com/writeas/go-strip-markdown v2.0.1+incompatible/go.mod h1:Rsyu10ZhbEK9pXdk8V6MVnZmTzRG0alMNLMwa0J01fE=
golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI=
golang.org/x/oauth2 v0.11.0 h1:vPL4xzxBM4niKCW6g9whtaWVXTJf1U5e4aZxxFx/gbU=
golang.org/x/oauth2 v0.11.0/go.mod h1:LdF7O/8bLR/qWK9DrpXmbHLTouvRHK0SgJl0GmDBchk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand All @@ -61,18 +48,8 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
Expand Down
7 changes: 4 additions & 3 deletions pkg/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type Model interface {
type ModelInput struct {
UserId string `json:"userid"`
APIKey string `json:"apikey"`
ModelId string `json:"modelid"`
ModelId string `json:"modelId"`
Provider string `json:"provider"`
Prompt string `json:"prompt"`
Context string `json:"context"`
Expand All @@ -18,10 +18,11 @@ type ModelInput struct {
type ModelResponse struct {
Input string `json:"input_tokens"`
Status string `json:"status"`
RequestID string `json:"request_id"`
ConversationID string `json:"conversation_id"`
RequestID string `json:"requestId"`
ConversationID string `json:"conversationId"`
Output string `json:"output"`
RawOutput string `json:"raw_output"`
ErrorMessage string `json:"error"`
}

// FeedbackPayload represents the payload for the feedback endpoint.
Expand Down
16 changes: 3 additions & 13 deletions pkg/filters/filters.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package filters

import (
"gopkg.in/yaml.v2"

"github.com/openshift/wisdom/pkg/api"
"github.com/openshift/wisdom/pkg/filters/markdown"
"github.com/openshift/wisdom/pkg/filters/yaml"
)

type Filter struct {
Expand All @@ -15,7 +15,7 @@ type ResponseFilter func(response *api.ModelResponse) (*api.ModelResponse, error

func NewFilter() Filter {
filter := Filter{}
//filter.responseFilterChain = append(filter.responseFilterChain, YamlLinter)
filter.responseFilterChain = append(filter.responseFilterChain, markdown.MarkdownStripper, yaml.YamlLinter)
return filter
}

Expand All @@ -42,13 +42,3 @@ func (f *Filter) FilterResponse(response *api.ModelResponse) (*api.ModelResponse
}
return output, err
}

func YamlLinter(response *api.ModelResponse) (*api.ModelResponse, error) {
return response, isValidYAML(response.Output)
}

func isValidYAML(yamlString string) error {
var data interface{}
err := yaml.Unmarshal([]byte(yamlString), &data)
return err
}
41 changes: 41 additions & 0 deletions pkg/filters/markdown/markdown.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package markdown

import (
"fmt"
"regexp"

//gomarkdown "github.com/gomarkdown/markdown"
log "github.com/sirupsen/logrus"

"github.com/openshift/wisdom/pkg/api"
)

var (
markdownRegex = regexp.MustCompile("(?s)`{3}.*?\n(.*)`{3}")
)

func MarkdownStripper(response *api.ModelResponse) (*api.ModelResponse, error) {

if response.Output == "" {
return response, fmt.Errorf("response output is empty")
}
log.Debugf("Stripping markdown from response:\n %s\n", response.Output)

//response.Output = markdownRegex.ReplaceAllString(response.Output, "")
matches := markdownRegex.FindStringSubmatch(response.Output)
response.Output = matches[1]
/*
node := gomarkdown.Parse([]byte(response.Output), nil)

fmt.Printf("%#v\n", node.GetChildren()[0].GetChildren()[0])
//fmt.Printf("%#v\n", node.GetChildren()[0].GetChildren()[1])
//fmt.Printf("%s\n", node.GetChildren()[0].GetChildren()[1].AsLeaf().Literal)

response.Output = string(node.GetChildren()[0].GetChildren()[0].AsLeaf().Literal)
*/

//response.Output = stripmd.Strip(response.Output)

log.Debugf("Stripped markdown from response:\n %s\n", response.Output)
return response, nil
}
27 changes: 27 additions & 0 deletions pkg/filters/yaml/yaml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package yaml

import (
"fmt"

"gopkg.in/yaml.v2"

log "github.com/sirupsen/logrus"

"github.com/openshift/wisdom/pkg/api"
)

func YamlLinter(response *api.ModelResponse) (*api.ModelResponse, error) {
if err := isValidYAML(response.Output); err != nil {
return response, fmt.Errorf("response output is not valid YAML: %s", err)
}
return response, nil
}

func isValidYAML(yamlString string) error {
var data interface{}

log.Debugf("Validating YAML:\n%s\n", yamlString)
err := yaml.Unmarshal([]byte(yamlString), &data)

return err
}
7 changes: 7 additions & 0 deletions pkg/model/invoker.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@ import (
func InvokeModel(input api.ModelInput, model api.Model, filter filters.Filter) (*api.ModelResponse, error) {

response, err := model.Invoke(input)
if response == nil {
response = &api.ModelResponse{}
}
if err != nil {
response.ErrorMessage = err.Error()
return response, err
}
output, err := filter.FilterResponse(response)
if err != nil {
response.ErrorMessage = err.Error()
}
return output, err
}
1 change: 1 addition & 0 deletions pkg/model/openai/model_openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,6 @@ func (m *OpenAIModel) Invoke(input api.ModelInput) (*api.ModelResponse, error) {
response := api.ModelResponse{}
response.Input = input.Prompt
response.Output = apiResp.Choices[0].Message.Content
response.RawOutput = apiResp.Choices[0].Message.Content
return &response, err
}
23 changes: 12 additions & 11 deletions pkg/server/inference_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,22 @@ func (h *Handler) InferHandler(w http.ResponseWriter, r *http.Request) {
return
}
response, err := model.InvokeModel(payload, m, h.Filter)
if err != nil {
w.WriteHeader(http.StatusExpectationFailed)
w.Write([]byte(err.Error()))
return
}

buf := bytes.Buffer{}
err = json.NewEncoder(&buf).Encode(response)
if err != nil {
w.WriteHeader(http.StatusExpectationFailed)
w.Write([]byte(err.Error()))
return
if response != nil {
err = json.NewEncoder(&buf).Encode(response)
if err != nil {
w.WriteHeader(http.StatusExpectationFailed)
w.Write([]byte(err.Error()))
return
}
}

w.Header().Set("Content-Type", "text/json")
w.WriteHeader(http.StatusOK)
if err != nil || (response != nil && response.ErrorMessage != "") {
w.WriteHeader(http.StatusExpectationFailed)
} else {
w.WriteHeader(http.StatusOK)
}
w.Write(buf.Bytes())
}
3 changes: 0 additions & 3 deletions vendor/github.com/golang/protobuf/AUTHORS

This file was deleted.

3 changes: 0 additions & 3 deletions vendor/github.com/golang/protobuf/CONTRIBUTORS

This file was deleted.

28 changes: 0 additions & 28 deletions vendor/github.com/golang/protobuf/LICENSE

This file was deleted.

Loading