Skip to content

Commit 4b46d02

Browse files
yjp20stainless-app[bot]
authored andcommitted
chore: add back custom code that was reverted
1 parent af3e201 commit 4b46d02

File tree

10 files changed

+147
-36
lines changed

10 files changed

+147
-36
lines changed

README.md

+9-12
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,9 @@ func main() {
5050
option.WithAPIKey("My API Key"), // defaults to os.LookupEnv("OPENAI_API_KEY")
5151
)
5252
chatCompletion, err := client.Chat.Completions.New(context.TODO(), openai.ChatCompletionNewParams{
53-
Messages: openai.F([]openai.ChatCompletionMessageParamUnion{openai.ChatCompletionUserMessageParam{
54-
Role: openai.F(openai.ChatCompletionUserMessageParamRoleUser),
55-
Content: openai.F([]openai.ChatCompletionContentPartUnionParam{openai.ChatCompletionContentPartTextParam{Text: openai.F("text"), Type: openai.F(openai.ChatCompletionContentPartTextTypeText)}}),
56-
}}),
53+
Messages: openai.F([]openai.ChatCompletionMessageParamUnion{
54+
openai.UserMessage("Say this is a test"),
55+
}),
5756
Model: openai.F(openai.ChatModelGPT4o),
5857
})
5958
if err != nil {
@@ -236,10 +235,9 @@ defer cancel()
236235
client.Chat.Completions.New(
237236
ctx,
238237
openai.ChatCompletionNewParams{
239-
Messages: openai.F([]openai.ChatCompletionMessageParamUnion{openai.ChatCompletionUserMessageParam{
240-
Role: openai.F(openai.ChatCompletionUserMessageParamRoleUser),
241-
Content: openai.F([]openai.ChatCompletionContentPartUnionParam{openai.ChatCompletionContentPartTextParam{Text: openai.F("text"), Type: openai.F(openai.ChatCompletionContentPartTextTypeText)}}),
242-
}}),
238+
Messages: openai.F([]openai.ChatCompletionMessageParamUnion{
239+
openai.UserMessage("Say this is a test"),
240+
}),
243241
Model: openai.F(openai.ChatModelGPT4o),
244242
},
245243
// This sets the per-retry timeout
@@ -299,10 +297,9 @@ client := openai.NewClient(
299297
client.Chat.Completions.New(
300298
context.TODO(),
301299
openai.ChatCompletionNewParams{
302-
Messages: openai.F([]openai.ChatCompletionMessageParamUnion{openai.ChatCompletionUserMessageParam{
303-
Role: openai.F(openai.ChatCompletionUserMessageParamRoleUser),
304-
Content: openai.F([]openai.ChatCompletionContentPartUnionParam{openai.ChatCompletionContentPartTextParam{Text: openai.F("text"), Type: openai.F(openai.ChatCompletionContentPartTextTypeText)}}),
305-
}}),
300+
Messages: openai.F([]openai.ChatCompletionMessageParamUnion{
301+
openai.UserMessage("Say this is a test"),
302+
}),
306303
Model: openai.F(openai.ChatModelGPT4o),
307304
},
308305
option.WithMaxRetries(5),

azure/azure_test.go

+4-17
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,14 @@ import (
88

99
"github.com/openai/openai-go"
1010
"github.com/openai/openai-go/internal/apijson"
11-
"github.com/openai/openai-go/shared"
1211
)
1312

1413
func TestJSONRoute(t *testing.T) {
1514
chatCompletionParams := openai.ChatCompletionNewParams{
1615
Model: openai.F(openai.ChatModel("arbitraryDeployment")),
1716
Messages: openai.F([]openai.ChatCompletionMessageParamUnion{
18-
openai.ChatCompletionAssistantMessageParam{
19-
Role: openai.F(openai.ChatCompletionAssistantMessageParamRoleAssistant),
20-
Content: openai.F[openai.ChatCompletionAssistantMessageParamContentUnion](shared.UnionString("You are a helpful assistant")),
21-
},
22-
openai.ChatCompletionUserMessageParam{
23-
Role: openai.F(openai.ChatCompletionUserMessageParamRoleUser),
24-
Content: openai.F[openai.ChatCompletionUserMessageParamContentUnion](shared.UnionString("Can you tell me another word for the universe?")),
25-
},
17+
openai.AssistantMessage("You are a helpful assistant"),
18+
openai.UserMessage("Can you tell me another word for the universe?"),
2619
}),
2720
}
2821

@@ -95,14 +88,8 @@ func TestNoRouteChangeNeeded(t *testing.T) {
9588
chatCompletionParams := openai.ChatCompletionNewParams{
9689
Model: openai.F(openai.ChatModel("arbitraryDeployment")),
9790
Messages: openai.F([]openai.ChatCompletionMessageParamUnion{
98-
openai.ChatCompletionAssistantMessageParam{
99-
Role: openai.F(openai.ChatCompletionAssistantMessageParamRoleAssistant),
100-
Content: openai.F[openai.ChatCompletionAssistantMessageParamContentUnion](shared.UnionString("You are a helpful assistant")),
101-
},
102-
openai.ChatCompletionUserMessageParam{
103-
Role: openai.F(openai.ChatCompletionUserMessageParamRoleUser),
104-
Content: openai.F[openai.ChatCompletionUserMessageParamContentUnion](shared.UnionString("Can you tell me another word for the universe?")),
105-
},
91+
openai.AssistantMessage("You are a helpful assistant"),
92+
openai.UserMessage("Can you tell me another word for the universe?"),
10693
}),
10794
}
10895

chatcompletion.go

+98
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,79 @@ import (
1212
"github.com/openai/openai-go/option"
1313
"github.com/openai/openai-go/packages/ssestream"
1414
"github.com/openai/openai-go/shared"
15+
"github.com/tidwall/sjson"
1516
)
1617

18+
func UserMessage(content string) ChatCompletionMessageParamUnion {
19+
return UserMessageParts(TextPart(content))
20+
}
21+
22+
func UserMessageParts(parts ...ChatCompletionContentPartUnionParam) ChatCompletionUserMessageParam {
23+
return ChatCompletionUserMessageParam{
24+
Role: F(ChatCompletionUserMessageParamRoleUser),
25+
Content: F(parts),
26+
}
27+
}
28+
29+
func TextPart(content string) ChatCompletionContentPartTextParam {
30+
return ChatCompletionContentPartTextParam{
31+
Type: F(ChatCompletionContentPartTextTypeText),
32+
Text: F(content),
33+
}
34+
}
35+
36+
func RefusalPart(refusal string) ChatCompletionContentPartRefusalParam {
37+
return ChatCompletionContentPartRefusalParam{
38+
Type: F(ChatCompletionContentPartRefusalTypeRefusal),
39+
Refusal: F(refusal),
40+
}
41+
}
42+
43+
func ImagePart(url string) ChatCompletionContentPartImageParam {
44+
return ChatCompletionContentPartImageParam{
45+
Type: F(ChatCompletionContentPartImageTypeImageURL),
46+
ImageURL: F(ChatCompletionContentPartImageImageURLParam{
47+
URL: F(url),
48+
}),
49+
}
50+
}
51+
52+
func AssistantMessage(content string) ChatCompletionAssistantMessageParam {
53+
return ChatCompletionAssistantMessageParam{
54+
Role: F(ChatCompletionAssistantMessageParamRoleAssistant),
55+
Content: F([]ChatCompletionAssistantMessageParamContentUnion{
56+
TextPart(content),
57+
}),
58+
}
59+
}
60+
61+
func ToolMessage(toolCallID, content string) ChatCompletionToolMessageParam {
62+
return ChatCompletionToolMessageParam{
63+
Role: F(ChatCompletionToolMessageParamRoleTool),
64+
ToolCallID: F(toolCallID),
65+
Content: F([]ChatCompletionContentPartTextParam{
66+
TextPart(content),
67+
}),
68+
}
69+
}
70+
71+
func SystemMessage(content string) ChatCompletionMessageParamUnion {
72+
return ChatCompletionSystemMessageParam{
73+
Role: F(ChatCompletionSystemMessageParamRoleSystem),
74+
Content: F([]ChatCompletionContentPartTextParam{
75+
TextPart(content),
76+
}),
77+
}
78+
}
79+
80+
func FunctionMessage(name, content string) ChatCompletionMessageParamUnion {
81+
return ChatCompletionFunctionMessageParam{
82+
Role: F(ChatCompletionFunctionMessageParamRoleFunction),
83+
Name: F(name),
84+
Content: F(content),
85+
}
86+
}
87+
1788
// ChatCompletionService contains methods and other services that help with
1889
// interacting with the openai API.
1990
//
@@ -870,10 +941,35 @@ func (r *ChatCompletionMessage) UnmarshalJSON(data []byte) (err error) {
870941
return apijson.UnmarshalRoot(data, r)
871942
}
872943

944+
func (r ChatCompletionMessage) MarshalJSON() (data []byte, err error) {
945+
s := ""
946+
s, _ = sjson.Set(s, "role", r.Role)
947+
948+
if r.FunctionCall.Name != "" {
949+
b, err := apijson.Marshal(r.FunctionCall)
950+
if err != nil {
951+
return nil, err
952+
}
953+
s, _ = sjson.SetRaw(s, "function_call", string(b))
954+
} else if len(r.ToolCalls) > 0 {
955+
b, err := apijson.Marshal(r.ToolCalls)
956+
if err != nil {
957+
return nil, err
958+
}
959+
s, _ = sjson.SetRaw(s, "tool_calls", string(b))
960+
} else {
961+
s, _ = sjson.Set(s, "content", r.Content)
962+
}
963+
964+
return []byte(s), nil
965+
}
966+
873967
func (r chatCompletionMessageJSON) RawJSON() string {
874968
return r.raw
875969
}
876970

971+
func (r ChatCompletionMessage) implementsChatCompletionMessageParamUnion() {}
972+
877973
// The role of the author of this message.
878974
type ChatCompletionMessageRole string
879975

@@ -944,6 +1040,8 @@ func (r ChatCompletionMessageParam) implementsChatCompletionMessageParamUnion()
9441040
// [ChatCompletionUserMessageParam], [ChatCompletionAssistantMessageParam],
9451041
// [ChatCompletionToolMessageParam], [ChatCompletionFunctionMessageParam],
9461042
// [ChatCompletionMessageParam].
1043+
//
1044+
// This union is additionally satisfied by the return types [ChatCompletionMessage]
9471045
type ChatCompletionMessageParamUnion interface {
9481046
implementsChatCompletionMessageParamUnion()
9491047
}

examples/audio-text-to-speech/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func main() {
1313
ctx := context.Background()
1414

1515
res, err := client.Audio.Speech.New(ctx, openai.AudioSpeechNewParams{
16-
Model: openai.F(openai.AudioSpeechNewParamsModelTTS1),
16+
Model: openai.F(openai.AudioModelWhisper1),
1717
Input: openai.String(`Why did the chicken cross the road? To get to the other side.`),
1818
ResponseFormat: openai.F(openai.AudioSpeechNewParamsResponseFormatPCM),
1919
Voice: openai.F(openai.AudioSpeechNewParamsVoiceAlloy),

examples/audio-transcriptions/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func main() {
1818
}
1919

2020
transcription, err := client.Audio.Transcriptions.New(ctx, openai.AudioTranscriptionNewParams{
21-
Model: openai.F(openai.AudioTranscriptionNewParamsModelWhisper1),
21+
Model: openai.F(openai.AudioModelWhisper1),
2222
File: openai.F[io.Reader](file),
2323
})
2424
if err != nil {

examples/chat-completion-streaming/main.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ func main() {
2121
Messages: openai.F([]openai.ChatCompletionMessageParamUnion{
2222
openai.UserMessage(question),
2323
}),
24-
Seed: openai.Int(0),
24+
Seed: openai.Int(1),
2525
Model: openai.F(openai.ChatModelGPT4o),
2626
})
2727

2828
for stream.Next() {
2929
evt := stream.Current()
30-
print(evt.Choices[0].Delta.Content)
30+
if len(evt.Choices) > 0 {
31+
print(evt.Choices[0].Delta.Content)
32+
}
3133
}
3234
println()
3335

examples/chat-completion/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func main() {
2121
Messages: openai.F([]openai.ChatCompletionMessageParamUnion{
2222
openai.UserMessage(question),
2323
}),
24-
Seed: openai.Int(0),
24+
Seed: openai.Int(1),
2525
Model: openai.F(openai.ChatModelGPT4o),
2626
})
2727
if err != nil {

examples/image-generation/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func main() {
2323

2424
image, err := client.Images.Generate(ctx, openai.ImageGenerateParams{
2525
Prompt: openai.String(prompt),
26-
Model: openai.F(openai.ImageGenerateParamsModelDallE3),
26+
Model: openai.F(openai.ImageModelDallE3),
2727
ResponseFormat: openai.F(openai.ImageGenerateParamsResponseFormatURL),
2828
N: openai.Int(1),
2929
})
@@ -38,7 +38,7 @@ func main() {
3838

3939
image, err = client.Images.Generate(ctx, openai.ImageGenerateParams{
4040
Prompt: openai.String(prompt),
41-
Model: openai.F(openai.ImageGenerateParamsModelDallE3),
41+
Model: openai.F(openai.ImageModelDallE3),
4242
ResponseFormat: openai.F(openai.ImageGenerateParamsResponseFormatB64JSON),
4343
N: openai.Int(1),
4444
})

go.work

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
go 1.22.4
2+
3+
use ./examples

go.work.sum

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.13.0 h1:GJHeeA2N7xrG3q30L2UXDyuWRzDM900/65j70wcM4Ww=
2+
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.13.0/go.mod h1:l38EPgmsp71HHLq9j7De57JcKOWPyhrsW1Awm1JS6K0=
3+
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.14.0 h1:nyQWyZvwGTvunIMxi1Y9uXkcyr+I7TeNrr/foo4Kpk8=
4+
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.14.0/go.mod h1:l38EPgmsp71HHLq9j7De57JcKOWPyhrsW1Awm1JS6K0=
5+
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.7.0 h1:tfLQ34V6F7tVSwoTf/4lH5sE0o6eCJuNDTmH09nDpbc=
6+
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.7.0/go.mod h1:9kIvujWAA58nmPmWB1m23fyWic1kYZMxD9CxaWn4Qpg=
7+
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 h1:ywEEhmNahHBihViHepv3xPBn1663uRv2t2q/ESv9seY=
8+
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0/go.mod h1:iZDifYGJTIgIIkYRNWPENUnqx6bJ2xnSDFI2tjwZNuY=
9+
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2 h1:XHOnouVk1mxXfQidrMEnLlPk9UMeRtyBTnEFtxkV0kU=
10+
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2/go.mod h1:wP83P5OoQ5p6ip3ScPr0BAq0BvuPAvacpEuSzyouqAI=
11+
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
12+
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
13+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
14+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
15+
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
16+
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
17+
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ=
18+
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU=
19+
golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
20+
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
21+
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
22+
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
23+
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
24+
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=

0 commit comments

Comments
 (0)