Skip to content

Commit e222662

Browse files
Merge branch 'sashabaranov:master' into master
2 parents 5273861 + 74d6449 commit e222662

11 files changed

+170
-155
lines changed

.github/workflows/pr.yml

+3-3
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

.golangci.yml

-14
Original file line numberDiff line numberDiff line change
@@ -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-
# Removed execinquery (deprecated). 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
@@ -201,7 +194,6 @@ 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
@@ -239,12 +231,6 @@ linters:
239231
#- tagliatelle # Checks the struct tags.
240232
#- thelper # thelper detects golang test helpers without t.Helper() call and checks the consistency of test helpers
241233
#- wsl # [too strict and mostly code is not more readable] Whitespace Linter - Forces you to use empty lines!
242-
## deprecated
243-
#- exhaustivestruct # [deprecated, replaced by exhaustruct] Checks if all struct's fields are initialized
244-
#- golint # [deprecated, replaced by revive] Golint differs from gofmt. Gofmt reformats Go source code, whereas golint prints out style mistakes
245-
#- interfacer # [deprecated] Linter that suggests narrower interface types
246-
#- maligned # [deprecated, replaced by govet fieldalignment] Tool to detect Go structs that would take less memory if their fields were sorted
247-
#- scopelint # [deprecated, replaced by exportloopref] Scopelint checks for unpinned variables in go programs
248234

249235

250236
issues:

chat.go

+1
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"

chat_test.go

-34
Original file line numberDiff line numberDiff line change
@@ -106,40 +106,6 @@ func TestO1ModelsChatCompletionsBetaLimitations(t *testing.T) {
106106
},
107107
expectedError: openai.ErrReasoningModelLimitationsLogprobs,
108108
},
109-
{
110-
name: "message_type_unsupported",
111-
in: openai.ChatCompletionRequest{
112-
MaxCompletionTokens: 1000,
113-
Model: openai.O1Mini,
114-
Messages: []openai.ChatCompletionMessage{
115-
{
116-
Role: openai.ChatMessageRoleSystem,
117-
},
118-
},
119-
},
120-
expectedError: openai.ErrO1BetaLimitationsMessageTypes,
121-
},
122-
{
123-
name: "tool_unsupported",
124-
in: openai.ChatCompletionRequest{
125-
MaxCompletionTokens: 1000,
126-
Model: openai.O1Mini,
127-
Messages: []openai.ChatCompletionMessage{
128-
{
129-
Role: openai.ChatMessageRoleUser,
130-
},
131-
{
132-
Role: openai.ChatMessageRoleAssistant,
133-
},
134-
},
135-
Tools: []openai.Tool{
136-
{
137-
Type: openai.ToolTypeFunction,
138-
},
139-
},
140-
},
141-
expectedError: openai.ErrO1BetaLimitationsTools,
142-
},
143109
{
144110
name: "set_temperature_unsupported",
145111
in: openai.ChatCompletionRequest{

client.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,21 @@ func sendRequestStream[T streamable](client *Client, req *http.Request) (*stream
182182

183183
func (c *Client) setCommonHeaders(req *http.Request) {
184184
// https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference#authentication
185-
// Azure API Key authentication
186-
if c.config.APIType == APITypeAzure || c.config.APIType == APITypeCloudflareAzure {
185+
switch c.config.APIType {
186+
case APITypeAzure, APITypeCloudflareAzure:
187+
// Azure API Key authentication
187188
req.Header.Set(AzureAPIKeyHeader, c.config.authToken)
188-
} else if c.config.authToken != "" {
189-
// OpenAI or Azure AD authentication
190-
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.config.authToken))
189+
case APITypeAnthropic:
190+
// https://docs.anthropic.com/en/api/versioning
191+
req.Header.Set("anthropic-version", c.config.APIVersion)
192+
case APITypeOpenAI, APITypeAzureAD:
193+
fallthrough
194+
default:
195+
if c.config.authToken != "" {
196+
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.config.authToken))
197+
}
191198
}
199+
192200
if c.config.OrgID != "" {
193201
req.Header.Set("OpenAI-Organization", c.config.OrgID)
194202
}

client_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,21 @@ func TestClient(t *testing.T) {
3939
}
4040
}
4141

42+
func TestSetCommonHeadersAnthropic(t *testing.T) {
43+
config := DefaultAnthropicConfig("mock-token", "")
44+
client := NewClientWithConfig(config)
45+
req, err := http.NewRequest("GET", "http://example.com", nil)
46+
if err != nil {
47+
t.Fatalf("Failed to create request: %v", err)
48+
}
49+
50+
client.setCommonHeaders(req)
51+
52+
if got := req.Header.Get("anthropic-version"); got != AnthropicAPIVersion {
53+
t.Errorf("Expected anthropic-version header to be %q, got %q", AnthropicAPIVersion, got)
54+
}
55+
}
56+
4257
func TestDecodeResponse(t *testing.T) {
4358
stringInput := ""
4459

completion.go

+71-67
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,43 @@ import (
1010
// GPT3 Models are designed for text-based tasks. For code-specific
1111
// tasks, please refer to the Codex series of models.
1212
const (
13-
O1Mini = "o1-mini"
14-
O1Mini20240912 = "o1-mini-2024-09-12"
15-
O1Preview = "o1-preview"
16-
O1Preview20240912 = "o1-preview-2024-09-12"
17-
O1 = "o1"
18-
O120241217 = "o1-2024-12-17"
19-
O3Mini = "o3-mini"
20-
O3Mini20250131 = "o3-mini-2025-01-31"
21-
GPT432K0613 = "gpt-4-32k-0613"
22-
GPT432K0314 = "gpt-4-32k-0314"
23-
GPT432K = "gpt-4-32k"
24-
GPT40613 = "gpt-4-0613"
25-
GPT40314 = "gpt-4-0314"
26-
GPT4o = "gpt-4o"
27-
GPT4o20240513 = "gpt-4o-2024-05-13"
28-
GPT4o20240806 = "gpt-4o-2024-08-06"
29-
GPT4o20241120 = "gpt-4o-2024-11-20"
30-
GPT4oLatest = "chatgpt-4o-latest"
31-
GPT4oMini = "gpt-4o-mini"
32-
GPT4oMini20240718 = "gpt-4o-mini-2024-07-18"
33-
GPT4Turbo = "gpt-4-turbo"
34-
GPT4Turbo20240409 = "gpt-4-turbo-2024-04-09"
35-
GPT4Turbo0125 = "gpt-4-0125-preview"
36-
GPT4Turbo1106 = "gpt-4-1106-preview"
37-
GPT4TurboPreview = "gpt-4-turbo-preview"
38-
GPT4VisionPreview = "gpt-4-vision-preview"
39-
GPT4 = "gpt-4"
40-
GPT3Dot5Turbo0125 = "gpt-3.5-turbo-0125"
41-
GPT3Dot5Turbo1106 = "gpt-3.5-turbo-1106"
42-
GPT3Dot5Turbo0613 = "gpt-3.5-turbo-0613"
43-
GPT3Dot5Turbo0301 = "gpt-3.5-turbo-0301"
44-
GPT3Dot5Turbo16K = "gpt-3.5-turbo-16k"
45-
GPT3Dot5Turbo16K0613 = "gpt-3.5-turbo-16k-0613"
46-
GPT3Dot5Turbo = "gpt-3.5-turbo"
47-
GPT3Dot5TurboInstruct = "gpt-3.5-turbo-instruct"
13+
O1Mini = "o1-mini"
14+
O1Mini20240912 = "o1-mini-2024-09-12"
15+
O1Preview = "o1-preview"
16+
O1Preview20240912 = "o1-preview-2024-09-12"
17+
O1 = "o1"
18+
O120241217 = "o1-2024-12-17"
19+
O3Mini = "o3-mini"
20+
O3Mini20250131 = "o3-mini-2025-01-31"
21+
GPT432K0613 = "gpt-4-32k-0613"
22+
GPT432K0314 = "gpt-4-32k-0314"
23+
GPT432K = "gpt-4-32k"
24+
GPT40613 = "gpt-4-0613"
25+
GPT40314 = "gpt-4-0314"
26+
GPT4o = "gpt-4o"
27+
GPT4o20240513 = "gpt-4o-2024-05-13"
28+
GPT4o20240806 = "gpt-4o-2024-08-06"
29+
GPT4o20241120 = "gpt-4o-2024-11-20"
30+
GPT4oLatest = "chatgpt-4o-latest"
31+
GPT4oMini = "gpt-4o-mini"
32+
GPT4oMini20240718 = "gpt-4o-mini-2024-07-18"
33+
GPT4Turbo = "gpt-4-turbo"
34+
GPT4Turbo20240409 = "gpt-4-turbo-2024-04-09"
35+
GPT4Turbo0125 = "gpt-4-0125-preview"
36+
GPT4Turbo1106 = "gpt-4-1106-preview"
37+
GPT4TurboPreview = "gpt-4-turbo-preview"
38+
GPT4VisionPreview = "gpt-4-vision-preview"
39+
GPT4 = "gpt-4"
40+
GPT4Dot5Preview = "gpt-4.5-preview"
41+
GPT4Dot5Preview20250227 = "gpt-4.5-preview-2025-02-27"
42+
GPT3Dot5Turbo0125 = "gpt-3.5-turbo-0125"
43+
GPT3Dot5Turbo1106 = "gpt-3.5-turbo-1106"
44+
GPT3Dot5Turbo0613 = "gpt-3.5-turbo-0613"
45+
GPT3Dot5Turbo0301 = "gpt-3.5-turbo-0301"
46+
GPT3Dot5Turbo16K = "gpt-3.5-turbo-16k"
47+
GPT3Dot5Turbo16K0613 = "gpt-3.5-turbo-16k-0613"
48+
GPT3Dot5Turbo = "gpt-3.5-turbo"
49+
GPT3Dot5TurboInstruct = "gpt-3.5-turbo-instruct"
4850
// Deprecated: Model is shutdown. Use gpt-3.5-turbo-instruct instead.
4951
GPT3TextDavinci003 = "text-davinci-003"
5052
// Deprecated: Model is shutdown. Use gpt-3.5-turbo-instruct instead.
@@ -85,38 +87,40 @@ const (
8587

8688
var disabledModelsForEndpoints = map[string]map[string]bool{
8789
"/completions": {
88-
O1Mini: true,
89-
O1Mini20240912: true,
90-
O1Preview: true,
91-
O1Preview20240912: true,
92-
O3Mini: true,
93-
O3Mini20250131: true,
94-
GPT3Dot5Turbo: true,
95-
GPT3Dot5Turbo0301: true,
96-
GPT3Dot5Turbo0613: true,
97-
GPT3Dot5Turbo1106: true,
98-
GPT3Dot5Turbo0125: true,
99-
GPT3Dot5Turbo16K: true,
100-
GPT3Dot5Turbo16K0613: true,
101-
GPT4: true,
102-
GPT4o: true,
103-
GPT4o20240513: true,
104-
GPT4o20240806: true,
105-
GPT4o20241120: true,
106-
GPT4oLatest: true,
107-
GPT4oMini: true,
108-
GPT4oMini20240718: true,
109-
GPT4TurboPreview: true,
110-
GPT4VisionPreview: true,
111-
GPT4Turbo1106: true,
112-
GPT4Turbo0125: true,
113-
GPT4Turbo: true,
114-
GPT4Turbo20240409: true,
115-
GPT40314: true,
116-
GPT40613: true,
117-
GPT432K: true,
118-
GPT432K0314: true,
119-
GPT432K0613: true,
90+
O1Mini: true,
91+
O1Mini20240912: true,
92+
O1Preview: true,
93+
O1Preview20240912: true,
94+
O3Mini: true,
95+
O3Mini20250131: true,
96+
GPT3Dot5Turbo: true,
97+
GPT3Dot5Turbo0301: true,
98+
GPT3Dot5Turbo0613: true,
99+
GPT3Dot5Turbo1106: true,
100+
GPT3Dot5Turbo0125: true,
101+
GPT3Dot5Turbo16K: true,
102+
GPT3Dot5Turbo16K0613: true,
103+
GPT4: true,
104+
GPT4Dot5Preview: true,
105+
GPT4Dot5Preview20250227: true,
106+
GPT4o: true,
107+
GPT4o20240513: true,
108+
GPT4o20240806: true,
109+
GPT4o20241120: true,
110+
GPT4oLatest: true,
111+
GPT4oMini: true,
112+
GPT4oMini20240718: true,
113+
GPT4TurboPreview: true,
114+
GPT4VisionPreview: true,
115+
GPT4Turbo1106: true,
116+
GPT4Turbo0125: true,
117+
GPT4Turbo: true,
118+
GPT4Turbo20240409: true,
119+
GPT40314: true,
120+
GPT40613: true,
121+
GPT432K: true,
122+
GPT432K0314: true,
123+
GPT432K0613: true,
120124
},
121125
chatCompletionsSuffix: {
122126
CodexCodeDavinci002: true,

config.go

+21-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const (
1111

1212
azureAPIPrefix = "openai"
1313
azureDeploymentsPrefix = "deployments"
14+
15+
AnthropicAPIVersion = "2023-06-01"
1416
)
1517

1618
type APIType string
@@ -20,6 +22,7 @@ const (
2022
APITypeAzure APIType = "AZURE"
2123
APITypeAzureAD APIType = "AZURE_AD"
2224
APITypeCloudflareAzure APIType = "CLOUDFLARE_AZURE"
25+
APITypeAnthropic APIType = "ANTHROPIC"
2326
)
2427

2528
const AzureAPIKeyHeader = "api-key"
@@ -37,7 +40,7 @@ type ClientConfig struct {
3740
BaseURL string
3841
OrgID string
3942
APIType APIType
40-
APIVersion string // required when APIType is APITypeAzure or APITypeAzureAD
43+
APIVersion string // required when APIType is APITypeAzure or APITypeAzureAD or APITypeAnthropic
4144
AssistantVersion string
4245
AzureModelMapperFunc func(model string) string // replace model to azure deployment name func
4346
HTTPClient HTTPDoer
@@ -76,6 +79,23 @@ func DefaultAzureConfig(apiKey, baseURL string) ClientConfig {
7679
}
7780
}
7881

82+
func DefaultAnthropicConfig(apiKey, baseURL string) ClientConfig {
83+
if baseURL == "" {
84+
baseURL = "https://api.anthropic.com/v1"
85+
}
86+
return ClientConfig{
87+
authToken: apiKey,
88+
BaseURL: baseURL,
89+
OrgID: "",
90+
APIType: APITypeAnthropic,
91+
APIVersion: AnthropicAPIVersion,
92+
93+
HTTPClient: &http.Client{},
94+
95+
EmptyMessagesLimit: defaultEmptyMessagesLimit,
96+
}
97+
}
98+
7999
func (ClientConfig) String() string {
80100
return "<OpenAI API ClientConfig>"
81101
}

0 commit comments

Comments
 (0)