Skip to content

Commit

Permalink
reduced export functions on groq-go and updated copyright
Browse files Browse the repository at this point in the history
  • Loading branch information
conneroisu committed Oct 31, 2024
1 parent d990844 commit e5f15c0
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 49 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2024 connero
Copyright (c) 2024 groq-go authors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
60 changes: 47 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ API Documentation: https://console.groq.com/docs/quickstart

Code generated by groq\-modeler DO NOT EDIT.

Created at: 2024\-10\-26 10:01:35
Created at: 2024\-10\-31 15:25:27

groq\-modeler Version 1.1.2

Expand Down Expand Up @@ -147,6 +147,9 @@ groq\-modeler Version 1.1.2
- [func \(e ErrContentFieldsMisused\) Error\(\) string](<#ErrContentFieldsMisused.Error>)
- [type ErrTooManyEmptyStreamMessages](<#ErrTooManyEmptyStreamMessages>)
- [func \(e ErrTooManyEmptyStreamMessages\) Error\(\) string](<#ErrTooManyEmptyStreamMessages.Error>)
- [type ErrToolNotFound](<#ErrToolNotFound>)
- [func \(e ErrToolNotFound\) Error\(\) string](<#ErrToolNotFound.Error>)
- [type ErrorResponse](<#ErrorResponse>)
- [type FinishReason](<#FinishReason>)
- [func \(r FinishReason\) MarshalJSON\(\) \(\[\]byte, error\)](<#FinishReason.MarshalJSON>)
- [type Format](<#Format>)
Expand Down Expand Up @@ -180,11 +183,11 @@ groq\-modeler Version 1.1.2

```go
const (
AudioResponseFormatJSON AudioResponseFormat = "json" // AudioResponseFormatJSON is the JSON format of some audio.
AudioResponseFormatText AudioResponseFormat = "text" // AudioResponseFormatText is the text format of some audio.
AudioResponseFormatSRT AudioResponseFormat = "srt" // AudioResponseFormatSRT is the SRT format of some audio.
AudioResponseFormatVerboseJSON AudioResponseFormat = "verbose_json" // AudioResponseFormatVerboseJSON is the verbose JSON format of some audio.
AudioResponseFormatVTT AudioResponseFormat = "vtt" // AudioResponseFormatVTT is the VTT format of some audio.
AudioResponseFormatJSON AudioResponseFormat = "json" // AudioResponseFormatJSON is the JSON response format of some audio.
AudioResponseFormatText AudioResponseFormat = "text" // AudioResponseFormatText is the text response format of some audio.
AudioResponseFormatSRT AudioResponseFormat = "srt" // AudioResponseFormatSRT is the SRT response format of some audio.
AudioResponseFormatVerboseJSON AudioResponseFormat = "verbose_json" // AudioResponseFormatVerboseJSON is the verbose JSON response format of some audio.
AudioResponseFormatVTT AudioResponseFormat = "vtt" // AudioResponseFormatVTT is the VTT response format of some audio.

TranscriptionTimestampGranularityWord TranscriptionTimestampGranularity = "word" // TranscriptionTimestampGranularityWord is the word timestamp granularity.
TranscriptionTimestampGranularitySegment TranscriptionTimestampGranularity = "segment" // TranscriptionTimestampGranularitySegment is the segment timestamp granularity.
Expand Down Expand Up @@ -455,7 +458,7 @@ type APIError struct {
```

<a name="APIError.Error"></a>
### func \(\*APIError\) [Error](<https://github.com/conneroisu/groq-go/blob/main/errors.go#L91>)
### func \(\*APIError\) [Error](<https://github.com/conneroisu/groq-go/blob/main/errors.go#L96>)

```go
func (e *APIError) Error() string
Expand All @@ -464,7 +467,7 @@ func (e *APIError) Error() string
Error method implements the error interface on APIError.

<a name="APIError.UnmarshalJSON"></a>
### func \(\*APIError\) [UnmarshalJSON](<https://github.com/conneroisu/groq-go/blob/main/errors.go#L103>)
### func \(\*APIError\) [UnmarshalJSON](<https://github.com/conneroisu/groq-go/blob/main/errors.go#L108>)

```go
func (e *APIError) UnmarshalJSON(data []byte) (err error)
Expand Down Expand Up @@ -920,7 +923,7 @@ type DefaultErrorAccumulator struct {
```

<a name="DefaultErrorAccumulator.Bytes"></a>
### func \(\*DefaultErrorAccumulator\) [Bytes](<https://github.com/conneroisu/groq-go/blob/main/errors.go#L82>)
### func \(\*DefaultErrorAccumulator\) [Bytes](<https://github.com/conneroisu/groq-go/blob/main/errors.go#L87>)

```go
func (e *DefaultErrorAccumulator) Bytes() (errBytes []byte)
Expand All @@ -929,7 +932,7 @@ func (e *DefaultErrorAccumulator) Bytes() (errBytes []byte)
Bytes method returns the bytes of the error accumulator.

<a name="DefaultErrorAccumulator.Write"></a>
### func \(\*DefaultErrorAccumulator\) [Write](<https://github.com/conneroisu/groq-go/blob/main/errors.go#L73>)
### func \(\*DefaultErrorAccumulator\) [Write](<https://github.com/conneroisu/groq-go/blob/main/errors.go#L78>)

```go
func (e *DefaultErrorAccumulator) Write(p []byte) error
Expand Down Expand Up @@ -958,7 +961,7 @@ type ErrContentFieldsMisused struct {
```

<a name="ErrContentFieldsMisused.Error"></a>
### func \(ErrContentFieldsMisused\) [Error](<https://github.com/conneroisu/groq-go/blob/main/errors.go#L55>)
### func \(ErrContentFieldsMisused\) [Error](<https://github.com/conneroisu/groq-go/blob/main/errors.go#L60>)

```go
func (e ErrContentFieldsMisused) Error() string
Expand All @@ -967,7 +970,7 @@ func (e ErrContentFieldsMisused) Error() string
Error implements the error interface.

<a name="ErrTooManyEmptyStreamMessages"></a>
## type [ErrTooManyEmptyStreamMessages](<https://github.com/conneroisu/groq-go/blob/main/errors.go#L31>)
## type [ErrTooManyEmptyStreamMessages](<https://github.com/conneroisu/groq-go/blob/main/errors.go#L35>)

ErrTooManyEmptyStreamMessages is returned when the stream has sent too many empty messages.

Expand All @@ -976,14 +979,45 @@ type ErrTooManyEmptyStreamMessages struct{}
```

<a name="ErrTooManyEmptyStreamMessages.Error"></a>
### func \(ErrTooManyEmptyStreamMessages\) [Error](<https://github.com/conneroisu/groq-go/blob/main/errors.go#L61>)
### func \(ErrTooManyEmptyStreamMessages\) [Error](<https://github.com/conneroisu/groq-go/blob/main/errors.go#L66>)

```go
func (e ErrTooManyEmptyStreamMessages) Error() string
```

Error returns the error message.

<a name="ErrToolNotFound"></a>
## type [ErrToolNotFound](<https://github.com/conneroisu/groq-go/blob/main/errors.go#L30-L32>)

ErrToolNotFound is returned when a tool is not found.

```go
type ErrToolNotFound struct {
ToolName string
}
```

<a name="ErrToolNotFound.Error"></a>
### func \(ErrToolNotFound\) [Error](<https://github.com/conneroisu/groq-go/blob/main/errors.go#L159>)

```go
func (e ErrToolNotFound) Error() string
```

Error implements the error interface.

<a name="ErrorResponse"></a>
## type [ErrorResponse](<https://github.com/conneroisu/groq-go/blob/main/errors.go#L54-L56>)

ErrorResponse is the response returned by the Groq API.

```go
type ErrorResponse struct {
Error *APIError `json:"error,omitempty"`
}
```

<a name="FinishReason"></a>
## type [FinishReason](<https://github.com/conneroisu/groq-go/blob/main/chat.go#L152>)

Expand Down
43 changes: 15 additions & 28 deletions audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (c *Client) callAudioAPI(
endpointSuffix Endpoint,
) (response AudioResponse, err error) {
var formBody bytes.Buffer
c.requestFormBuilder = c.createFormBuilder(&formBody)
c.requestFormBuilder = builders.NewFormBuilder(&formBody)
err = AudioMultipartForm(request, c.requestFormBuilder)
if err != nil {
return AudioResponse{}, err
Expand Down Expand Up @@ -160,9 +160,21 @@ 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 {
err := CreateFileField(request, b)
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 err
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)
}
err = b.WriteField("model", string(request.Model))
if err != nil {
Expand Down Expand Up @@ -201,28 +213,3 @@ func AudioMultipartForm(request AudioRequest, b builders.FormBuilder) error {
}
return b.Close()
}

// CreateFileField creates the "file" form field from either an existing file
// or by using the reader.
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
}
6 changes: 3 additions & 3 deletions audio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestCreateFileField(t *testing.T) {
return mockFailedErr
},
}
err := groq.CreateFileField(req, mockBuilder)
err := groq.AudioMultipartForm(req, mockBuilder)
a.ErrorIs(
err,
mockFailedErr,
Expand All @@ -116,7 +116,7 @@ func TestCreateFileField(t *testing.T) {
},
}

err := groq.CreateFileField(req, mockBuilder)
err := groq.AudioMultipartForm(req, mockBuilder)
a.ErrorIs(
err,
mockFailedErr,
Expand All @@ -130,7 +130,7 @@ func TestCreateFileField(t *testing.T) {
FilePath: "non_existing_file.wav",
}
mockBuilder := builders.NewFormBuilder(&test.FailingErrorBuffer{})
err := groq.CreateFileField(req, mockBuilder)
err := groq.AudioMultipartForm(req, mockBuilder)
a.Error(
err,
"createFileField using file should return error when open file fails",
Expand Down
4 changes: 0 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ type (

header builders.Header
requestFormBuilder builders.FormBuilder
createFormBuilder func(body io.Writer) builders.FormBuilder

client *http.Client // Client is the HTTP client to use
logger *slog.Logger // Logger is the logger for the client.
Expand Down Expand Up @@ -85,9 +84,6 @@ func NewClient(groqAPIKey string, opts ...Opts) (*Client, error) {
logger: slog.Default(),
baseURL: groqAPIURLv1,
emptyMessagesLimit: 10,
createFormBuilder: func(body io.Writer) builders.FormBuilder {
return builders.NewFormBuilder(body)
},
}
for _, opt := range opts {
opt(c)
Expand Down

0 comments on commit e5f15c0

Please sign in to comment.