Skip to content

Commit

Permalink
simplified dependency graph and exported symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
conneroisu committed Oct 31, 2024
1 parent e5f15c0 commit f9cfcd8
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 24 deletions.
39 changes: 25 additions & 14 deletions audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,21 +160,9 @@ func (r AudioRequest) hasJSONResponse() bool {
// AudioMultipartForm creates a form with audio file contents and the name of
// the model to use for audio processing.
func AudioMultipartForm(request AudioRequest, b builders.FormBuilder) error {
if request.Reader != nil {
err := b.CreateFormFileReader("file", request.Reader, request.FilePath)
if err != nil {
return fmt.Errorf("creating form using reader: %w", err)
}
return nil
}
f, err := os.Open(request.FilePath)
err := createFileField(request, b)
if err != nil {
return fmt.Errorf("opening audio file: %w", err)
}
defer f.Close()
err = b.CreateFormFile("file", f)
if err != nil {
return fmt.Errorf("creating form file: %w", err)
return err
}
err = b.WriteField("model", string(request.Model))
if err != nil {
Expand Down Expand Up @@ -213,3 +201,26 @@ func AudioMultipartForm(request AudioRequest, b builders.FormBuilder) error {
}
return b.Close()
}

func createFileField(
request AudioRequest,
b builders.FormBuilder,
) (err error) {
if request.Reader != nil {
err := b.CreateFormFileReader("file", request.Reader, request.FilePath)
if err != nil {
return fmt.Errorf("creating form using reader: %w", err)
}
return nil
}
f, err := os.Open(request.FilePath)
if err != nil {
return fmt.Errorf("opening audio file: %w", err)
}
defer f.Close()
err = b.CreateFormFile("file", f)
if err != nil {
return fmt.Errorf("creating form file: %w", err)
}
return nil
}
2 changes: 1 addition & 1 deletion models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion models_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions scripts/generate-models/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@ module github.com/conneroisu/groq-go/cmd/models

go 1.23.2

require github.com/samber/lo v1.47.0

require golang.org/x/text v0.18.0 // indirect
require golang.org/x/text v0.18.0
2 changes: 0 additions & 2 deletions scripts/generate-models/go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
github.com/samber/lo v1.47.0 h1:z7RynLwP5nbyRscyvcD043DWYoOcYRv3mV8lBeqOCLc=
github.com/samber/lo v1.47.0/go.mod h1:RmDH9Ct32Qy3gduHQuKJ3gW1fMHAnE/fAzQuf6He5cU=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
55 changes: 52 additions & 3 deletions scripts/generate-models/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ import (
"log"
"net/http"
"os"
"regexp"
"sort"
"strings"
"text/template"
"time"
"unicode"

"github.com/samber/lo"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)

const (
Expand Down Expand Up @@ -102,7 +106,7 @@ func (r *Response) Categorize() (CategorizedModels, error) {
return models, nil
}

func isMultiModalModel(model ResponseModel) bool {
func isMultiModalModel(_ ResponseModel) bool {
return false
}

Expand Down Expand Up @@ -228,10 +232,55 @@ func fillModelsTemplate(w io.Writer, models CategorizedModels) (err error) {
return nil
}

var (
LowerCaseLettersCharset = []rune("abcdefghijklmnopqrstuvwxyz")

Check warning on line 236 in scripts/generate-models/main.go

View check run for this annotation

Codeac.io / Codeac Code Quality

exported

exported var LowerCaseLettersCharset should have comment or be unexported
UpperCaseLettersCharset = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
LettersCharset = append(LowerCaseLettersCharset, UpperCaseLettersCharset...)
NumbersCharset = []rune("0123456789")
AlphanumericCharset = append(LettersCharset, NumbersCharset...)
SpecialCharset = []rune("!@#$%^&*()_+-=[]{}|;':\",./<>?")
AllCharset = append(AlphanumericCharset, SpecialCharset...)

// bearer:disable go_lang_permissive_regex_validation
splitWordReg = regexp.MustCompile(`([a-z])([A-Z0-9])|([a-zA-Z])([0-9])|([0-9])([a-zA-Z])|([A-Z])([A-Z])([a-z])`)
// bearer:disable go_lang_permissive_regex_validation
splitNumberLetterReg = regexp.MustCompile(`([0-9])([a-zA-Z])`)
)

// Words splits string into an array of its words.
func Words(str string) []string {
str = splitWordReg.ReplaceAllString(str, `$1$3$5$7 $2$4$6$8$9`)
// example: Int8Value => Int 8Value => Int 8 Value
str = splitNumberLetterReg.ReplaceAllString(str, "$1 $2")
var result strings.Builder
for _, r := range str {
if unicode.IsLetter(r) || unicode.IsDigit(r) {
result.WriteRune(r)
} else {
result.WriteRune(' ')
}
}
return strings.Fields(result.String())
}

// Capitalize converts the first character of string to upper case and the remaining to lower case.
func Capitalize(str string) string {
return cases.Title(language.English).String(str)
}

// PascalCase converts string to pascal case.
func PascalCase(str string) string {
items := Words(str)
for i := range items {
items[i] = Capitalize(items[i])
}
return strings.Join(items, "")
}

func nameModels(models []ResponseModel) {
for i := range models {
if (models)[i].Name == "" {
models[i].Name = lo.PascalCase(models[i].ID)
models[i].Name = PascalCase(models[i].ID)
}
}
// sort models by name alphabetically
Expand Down

0 comments on commit f9cfcd8

Please sign in to comment.