Skip to content

Commit 0679f32

Browse files
committed
Merge branch 'master' of github.com:MattDavisRV/go-openai
2 parents 008f597 + 74d6449 commit 0679f32

26 files changed

+639
-324
lines changed

.github/workflows/pr.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ jobs:
1313
- name: Setup Go
1414
uses: actions/setup-go@v5
1515
with:
16-
go-version: '1.21'
16+
go-version: '1.24'
1717
- name: Run vet
1818
run: |
1919
go vet .
2020
- name: Run golangci-lint
21-
uses: golangci/golangci-lint-action@v4
21+
uses: golangci/golangci-lint-action@v6
2222
with:
23-
version: latest
23+
version: v1.64.5
2424
- name: Run tests
2525
run: go test -race -covermode=atomic -coverprofile=coverage.out -v .
2626
- name: Upload coverage reports to Codecov

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@
1616

1717
# Auth token for tests
1818
.openai-token
19-
.idea
19+
.idea
20+
21+
# Generated by tests
22+
test.mp3

.golangci.yml

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ linters-settings:
5757
# Default: true
5858
skipRecvDeref: false
5959

60-
gomnd:
60+
mnd:
6161
# List of function patterns to exclude from analysis.
6262
# Values always ignored: `time.Date`
6363
# Default: []
@@ -139,11 +139,6 @@ linters-settings:
139139
# Default: false
140140
all: true
141141

142-
varcheck:
143-
# Check usage of exported fields and variables.
144-
# Default: false
145-
exported-fields: false # default false # TODO: enable after fixing false positives
146-
147142

148143
linters:
149144
disable-all: true
@@ -167,9 +162,7 @@ linters:
167162
- durationcheck # check for two durations multiplied together
168163
- errname # Checks that sentinel errors are prefixed with the Err and error types are suffixed with the Error.
169164
- errorlint # errorlint is a linter for that can be used to find code that will cause problems with the error wrapping scheme introduced in Go 1.13.
170-
- execinquery # execinquery is a linter about query string checker in Query function which reads your Go src files and warning it finds
171165
- exhaustive # check exhaustiveness of enum switch statements
172-
- exportloopref # checks for pointers to enclosing loop variables
173166
- forbidigo # Forbids identifiers
174167
- funlen # Tool for detection of long functions
175168
# - gochecknoglobals # check that no global variables exist
@@ -180,14 +173,14 @@ linters:
180173
- gocyclo # Computes and checks the cyclomatic complexity of functions
181174
- godot # Check if comments end in a period
182175
- goimports # In addition to fixing imports, goimports also formats your code in the same style as gofmt.
183-
- gomnd # An analyzer to detect magic numbers.
184176
- gomoddirectives # Manage the use of 'replace', 'retract', and 'excludes' directives in go.mod.
185177
- gomodguard # Allow and block list linter for direct Go module dependencies. This is different from depguard where there are different block types for example version constraints and module recommendations.
186178
- goprintffuncname # Checks that printf-like functions are named with f at the end
187179
- gosec # Inspects source code for security problems
188180
- lll # Reports long lines
189181
- makezero # Finds slice declarations with non-zero initial length
190182
# - nakedret # Finds naked returns in functions greater than a specified function length
183+
- mnd # An analyzer to detect magic numbers.
191184
- nestif # Reports deeply nested if statements
192185
- nilerr # Finds the code that returns nil even if it checks that the error is not nil.
193186
- nilnil # Checks that there is no simultaneous return of nil error and an invalid value.
@@ -201,11 +194,11 @@ linters:
201194
- rowserrcheck # checks whether Err of rows is checked successfully
202195
- sqlclosecheck # Checks that sql.Rows and sql.Stmt are closed.
203196
- stylecheck # Stylecheck is a replacement for golint
204-
- tenv # tenv is analyzer that detects using os.Setenv instead of t.Setenv since Go1.17
205197
- testpackage # linter that makes you use a separate _test package
206198
- tparallel # tparallel detects inappropriate usage of t.Parallel() method in your Go test codes
207199
- unconvert # Remove unnecessary type conversions
208200
- unparam # Reports unused function parameters
201+
- usetesting # Reports uses of functions with replacement inside the testing package
209202
- wastedassign # wastedassign finds wasted assignment statements.
210203
- whitespace # Tool for detection of leading and trailing whitespace
211204
## you may want to enable
@@ -238,12 +231,6 @@ linters:
238231
#- tagliatelle # Checks the struct tags.
239232
#- thelper # thelper detects golang test helpers without t.Helper() call and checks the consistency of test helpers
240233
#- wsl # [too strict and mostly code is not more readable] Whitespace Linter - Forces you to use empty lines!
241-
## deprecated
242-
#- exhaustivestruct # [deprecated, replaced by exhaustruct] Checks if all struct's fields are initialized
243-
#- golint # [deprecated, replaced by revive] Golint differs from gofmt. Gofmt reformats Go source code, whereas golint prints out style mistakes
244-
#- interfacer # [deprecated] Linter that suggests narrower interface types
245-
#- maligned # [deprecated, replaced by govet fieldalignment] Tool to detect Go structs that would take less memory if their fields were sorted
246-
#- scopelint # [deprecated, replaced by exportloopref] Scopelint checks for unpinned variables in go programs
247234

248235

249236
issues:

audio_api_test.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,9 @@ func TestAudio(t *testing.T) {
4040

4141
ctx := context.Background()
4242

43-
dir, cleanup := test.CreateTestDirectory(t)
44-
defer cleanup()
45-
4643
for _, tc := range testcases {
4744
t.Run(tc.name, func(t *testing.T) {
48-
path := filepath.Join(dir, "fake.mp3")
45+
path := filepath.Join(t.TempDir(), "fake.mp3")
4946
test.CreateTestFile(t, path)
5047

5148
req := openai.AudioRequest{
@@ -90,12 +87,9 @@ func TestAudioWithOptionalArgs(t *testing.T) {
9087

9188
ctx := context.Background()
9289

93-
dir, cleanup := test.CreateTestDirectory(t)
94-
defer cleanup()
95-
9690
for _, tc := range testcases {
9791
t.Run(tc.name, func(t *testing.T) {
98-
path := filepath.Join(dir, "fake.mp3")
92+
path := filepath.Join(t.TempDir(), "fake.mp3")
9993
test.CreateTestFile(t, path)
10094

10195
req := openai.AudioRequest{

audio_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ import (
1313
)
1414

1515
func TestAudioWithFailingFormBuilder(t *testing.T) {
16-
dir, cleanup := test.CreateTestDirectory(t)
17-
defer cleanup()
18-
path := filepath.Join(dir, "fake.mp3")
16+
path := filepath.Join(t.TempDir(), "fake.mp3")
1917
test.CreateTestFile(t, path)
2018

2119
req := AudioRequest{
@@ -63,9 +61,7 @@ func TestAudioWithFailingFormBuilder(t *testing.T) {
6361

6462
func TestCreateFileField(t *testing.T) {
6563
t.Run("createFileField failing file", func(t *testing.T) {
66-
dir, cleanup := test.CreateTestDirectory(t)
67-
defer cleanup()
68-
path := filepath.Join(dir, "fake.mp3")
64+
path := filepath.Join(t.TempDir(), "fake.mp3")
6965
test.CreateTestFile(t, path)
7066

7167
req := AudioRequest{

batch_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ func TestUploadBatchFileRequest_AddEmbedding(t *testing.T) {
211211
Input: []string{"Hello", "World"},
212212
},
213213
},
214-
}, []byte("{\"custom_id\":\"req-1\",\"body\":{\"input\":[\"Hello\",\"World\"],\"model\":\"gpt-3.5-turbo\",\"user\":\"\"},\"method\":\"POST\",\"url\":\"/v1/embeddings\"}\n{\"custom_id\":\"req-2\",\"body\":{\"input\":[\"Hello\",\"World\"],\"model\":\"text-embedding-ada-002\",\"user\":\"\"},\"method\":\"POST\",\"url\":\"/v1/embeddings\"}")}, //nolint:lll
214+
}, []byte("{\"custom_id\":\"req-1\",\"body\":{\"input\":[\"Hello\",\"World\"],\"model\":\"gpt-3.5-turbo\"},\"method\":\"POST\",\"url\":\"/v1/embeddings\"}\n{\"custom_id\":\"req-2\",\"body\":{\"input\":[\"Hello\",\"World\"],\"model\":\"text-embedding-ada-002\"},\"method\":\"POST\",\"url\":\"/v1/embeddings\"}")}, //nolint:lll
215215
}
216216
for _, tt := range tests {
217217
t.Run(tt.name, func(t *testing.T) {

chat.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const (
1414
ChatMessageRoleAssistant = "assistant"
1515
ChatMessageRoleFunction = "function"
1616
ChatMessageRoleTool = "tool"
17+
ChatMessageRoleDeveloper = "developer"
1718
)
1819

1920
const chatCompletionsSuffix = "/chat/completions"
@@ -93,7 +94,7 @@ type ChatMessagePart struct {
9394

9495
type ChatCompletionMessage struct {
9596
Role string `json:"role"`
96-
Content string `json:"content"`
97+
Content string `json:"content,omitempty"`
9798
Refusal string `json:"refusal,omitempty"`
9899
MultiContent []ChatMessagePart
99100

@@ -132,7 +133,7 @@ func (m ChatCompletionMessage) MarshalJSON() ([]byte, error) {
132133

133134
msg := struct {
134135
Role string `json:"role"`
135-
Content string `json:"content"`
136+
Content string `json:"content,omitempty"`
136137
Refusal string `json:"refusal,omitempty"`
137138
MultiContent []ChatMessagePart `json:"-"`
138139
Name string `json:"name,omitempty"`
@@ -146,7 +147,7 @@ func (m ChatCompletionMessage) MarshalJSON() ([]byte, error) {
146147
func (m *ChatCompletionMessage) UnmarshalJSON(bs []byte) error {
147148
msg := struct {
148149
Role string `json:"role"`
149-
Content string `json:"content"`
150+
Content string `json:"content,omitempty"`
150151
Refusal string `json:"refusal,omitempty"`
151152
MultiContent []ChatMessagePart
152153
Name string `json:"name,omitempty"`
@@ -179,7 +180,7 @@ func (m *ChatCompletionMessage) UnmarshalJSON(bs []byte) error {
179180
type ToolCall struct {
180181
// Index is not nil only in chat completion chunk object
181182
Index *int `json:"index,omitempty"`
182-
ID string `json:"id"`
183+
ID string `json:"id,omitempty"`
183184
Type ToolType `json:"type"`
184185
Function FunctionCall `json:"function"`
185186
}
@@ -258,6 +259,8 @@ type ChatCompletionRequest struct {
258259
// Store can be set to true to store the output of this completion request for use in distillations and evals.
259260
// https://platform.openai.com/docs/api-reference/chat/create#chat-create-store
260261
Store bool `json:"store,omitempty"`
262+
// Controls effort on reasoning for reasoning models. It can be set to "low", "medium", or "high".
263+
ReasoningEffort string `json:"reasoning_effort,omitempty"`
261264
// Metadata to store with the completion.
262265
Metadata map[string]string `json:"metadata,omitempty"`
263266
}
@@ -390,7 +393,8 @@ func (c *Client) CreateChatCompletion(
390393
return
391394
}
392395

393-
if err = validateRequestForO1Models(request); err != nil {
396+
reasoningValidator := NewReasoningValidator()
397+
if err = reasoningValidator.Validate(request); err != nil {
394398
return
395399
}
396400

chat_stream.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ func (c *Client) CreateChatCompletionStream(
8080
}
8181

8282
request.Stream = true
83-
if err = validateRequestForO1Models(request); err != nil {
83+
reasoningValidator := NewReasoningValidator()
84+
if err = reasoningValidator.Validate(request); err != nil {
8485
return
8586
}
8687

0 commit comments

Comments
 (0)