Skip to content

Commit

Permalink
feat: simplify content union (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-app[bot] committed Aug 12, 2024
1 parent 4af9109 commit b228103
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 178 deletions.
2 changes: 1 addition & 1 deletion .stats.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
configured_endpoints: 68
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/openai-97797a9363b9960b5f2fbdc84426a2b91e75533ecd409fe99e37c231180a4339.yml
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/openai-285bce7dcdae7eea5fe84a8d6e5af2c1473d65ea193109370fb2257851eef7eb.yml
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import (

"github.com/openai/openai-go"
"github.com/openai/openai-go/option"
"github.com/openai/openai-go/shared"
)

func main() {
Expand All @@ -53,7 +52,7 @@ func main() {
chatCompletion, err := client.Chat.Completions.New(context.TODO(), openai.ChatCompletionNewParams{
Messages: openai.F([]openai.ChatCompletionMessageParamUnion{openai.ChatCompletionUserMessageParam{
Role: openai.F(openai.ChatCompletionUserMessageParamRoleUser),
Content: openai.F[openai.ChatCompletionUserMessageParamContentUnion](shared.UnionString("Say this is a test")),
Content: openai.F([]openai.ChatCompletionContentPartUnionParam{openai.ChatCompletionContentPartTextParam{Text: openai.F("text"), Type: openai.F(openai.ChatCompletionContentPartTextTypeText)}}),
}}),
Model: openai.F(openai.ChatModelGPT4o),
})
Expand Down Expand Up @@ -239,7 +238,7 @@ client.Chat.Completions.New(
openai.ChatCompletionNewParams{
Messages: openai.F([]openai.ChatCompletionMessageParamUnion{openai.ChatCompletionUserMessageParam{
Role: openai.F(openai.ChatCompletionUserMessageParamRoleUser),
Content: openai.F[openai.ChatCompletionUserMessageParamContentUnion](shared.UnionString("How can I list all files in a directory using Python?")),
Content: openai.F([]openai.ChatCompletionContentPartUnionParam{openai.ChatCompletionContentPartTextParam{Text: openai.F("text"), Type: openai.F(openai.ChatCompletionContentPartTextTypeText)}}),
}}),
Model: openai.F(openai.ChatModelGPT4o),
},
Expand Down Expand Up @@ -302,7 +301,7 @@ client.Chat.Completions.New(
openai.ChatCompletionNewParams{
Messages: openai.F([]openai.ChatCompletionMessageParamUnion{openai.ChatCompletionUserMessageParam{
Role: openai.F(openai.ChatCompletionUserMessageParamRoleUser),
Content: openai.F[openai.ChatCompletionUserMessageParamContentUnion](shared.UnionString("How can I get the name of the current day in Node.js?")),
Content: openai.F([]openai.ChatCompletionContentPartUnionParam{openai.ChatCompletionContentPartTextParam{Text: openai.F("text"), Type: openai.F(openai.ChatCompletionContentPartTextTypeText)}}),
}}),
Model: openai.F(openai.ChatModelGPT4o),
},
Expand Down
40 changes: 10 additions & 30 deletions betathread.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,11 @@ func (r BetaThreadNewParams) MarshalJSON() (data []byte, err error) {
}

type BetaThreadNewParamsMessage struct {
// The text contents of the message.
Content param.Field[BetaThreadNewParamsMessagesContentUnion] `json:"content,required"`
// An array of content parts with a defined type, each can be of type `text` or
// images can be passed with `image_url` or `image_file`. Image types are only
// supported on
// [Vision-compatible models](https://platform.openai.com/docs/models/overview).
Content param.Field[[]MessageContentPartParamUnion] `json:"content,required"`
// The role of the entity that is creating the message. Allowed values include:
//
// - `user`: Indicates the message is sent by an actual user and should be used in
Expand All @@ -474,19 +477,6 @@ func (r BetaThreadNewParamsMessage) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}

// The text contents of the message.
//
// Satisfied by [shared.UnionString],
// [BetaThreadNewParamsMessagesContentArrayOfContentParts].
type BetaThreadNewParamsMessagesContentUnion interface {
ImplementsBetaThreadNewParamsMessagesContentUnion()
}

type BetaThreadNewParamsMessagesContentArrayOfContentParts []MessageContentPartParamUnion

func (r BetaThreadNewParamsMessagesContentArrayOfContentParts) ImplementsBetaThreadNewParamsMessagesContentUnion() {
}

// The role of the entity that is creating the message. Allowed values include:
//
// - `user`: Indicates the message is sent by an actual user and should be used in
Expand Down Expand Up @@ -900,8 +890,11 @@ func (r BetaThreadNewAndRunParamsThread) MarshalJSON() (data []byte, err error)
}

type BetaThreadNewAndRunParamsThreadMessage struct {
// The text contents of the message.
Content param.Field[BetaThreadNewAndRunParamsThreadMessagesContentUnion] `json:"content,required"`
// An array of content parts with a defined type, each can be of type `text` or
// images can be passed with `image_url` or `image_file`. Image types are only
// supported on
// [Vision-compatible models](https://platform.openai.com/docs/models/overview).
Content param.Field[[]MessageContentPartParamUnion] `json:"content,required"`
// The role of the entity that is creating the message. Allowed values include:
//
// - `user`: Indicates the message is sent by an actual user and should be used in
Expand All @@ -922,19 +915,6 @@ func (r BetaThreadNewAndRunParamsThreadMessage) MarshalJSON() (data []byte, err
return apijson.MarshalRoot(r)
}

// The text contents of the message.
//
// Satisfied by [shared.UnionString],
// [BetaThreadNewAndRunParamsThreadMessagesContentArrayOfContentParts].
type BetaThreadNewAndRunParamsThreadMessagesContentUnion interface {
ImplementsBetaThreadNewAndRunParamsThreadMessagesContentUnion()
}

type BetaThreadNewAndRunParamsThreadMessagesContentArrayOfContentParts []MessageContentPartParamUnion

func (r BetaThreadNewAndRunParamsThreadMessagesContentArrayOfContentParts) ImplementsBetaThreadNewAndRunParamsThreadMessagesContentUnion() {
}

// The role of the entity that is creating the message. Allowed values include:
//
// - `user`: Indicates the message is sent by an actual user and should be used in
Expand Down
13 changes: 6 additions & 7 deletions betathread_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/openai/openai-go"
"github.com/openai/openai-go/internal/testutil"
"github.com/openai/openai-go/option"
"github.com/openai/openai-go/shared"
)

func TestBetaThreadNewWithOptionalParams(t *testing.T) {
Expand All @@ -28,7 +27,7 @@ func TestBetaThreadNewWithOptionalParams(t *testing.T) {
)
_, err := client.Beta.Threads.New(context.TODO(), openai.BetaThreadNewParams{
Messages: openai.F([]openai.BetaThreadNewParamsMessage{{
Content: openai.F[openai.BetaThreadNewParamsMessagesContentUnion](shared.UnionString("string")),
Content: openai.F([]openai.MessageContentPartParamUnion{openai.ImageFileContentBlockParam{ImageFile: openai.F(openai.ImageFileParam{FileID: openai.F("file_id"), Detail: openai.F(openai.ImageFileDetailAuto)}), Type: openai.F(openai.ImageFileContentBlockTypeImageFile)}}),
Role: openai.F(openai.BetaThreadNewParamsMessagesRoleUser),
Attachments: openai.F([]openai.BetaThreadNewParamsMessagesAttachment{{
FileID: openai.F("file_id"),
Expand Down Expand Up @@ -60,7 +59,7 @@ func TestBetaThreadNewWithOptionalParams(t *testing.T) {
}}),
Metadata: openai.F[any](map[string]interface{}{}),
}, {
Content: openai.F[openai.BetaThreadNewParamsMessagesContentUnion](shared.UnionString("string")),
Content: openai.F([]openai.MessageContentPartParamUnion{openai.ImageFileContentBlockParam{ImageFile: openai.F(openai.ImageFileParam{FileID: openai.F("file_id"), Detail: openai.F(openai.ImageFileDetailAuto)}), Type: openai.F(openai.ImageFileContentBlockTypeImageFile)}}),
Role: openai.F(openai.BetaThreadNewParamsMessagesRoleUser),
Attachments: openai.F([]openai.BetaThreadNewParamsMessagesAttachment{{
FileID: openai.F("file_id"),
Expand Down Expand Up @@ -92,7 +91,7 @@ func TestBetaThreadNewWithOptionalParams(t *testing.T) {
}}),
Metadata: openai.F[any](map[string]interface{}{}),
}, {
Content: openai.F[openai.BetaThreadNewParamsMessagesContentUnion](shared.UnionString("string")),
Content: openai.F([]openai.MessageContentPartParamUnion{openai.ImageFileContentBlockParam{ImageFile: openai.F(openai.ImageFileParam{FileID: openai.F("file_id"), Detail: openai.F(openai.ImageFileDetailAuto)}), Type: openai.F(openai.ImageFileContentBlockTypeImageFile)}}),
Role: openai.F(openai.BetaThreadNewParamsMessagesRoleUser),
Attachments: openai.F([]openai.BetaThreadNewParamsMessagesAttachment{{
FileID: openai.F("file_id"),
Expand Down Expand Up @@ -253,7 +252,7 @@ func TestBetaThreadNewAndRunWithOptionalParams(t *testing.T) {
Temperature: openai.F(1.000000),
Thread: openai.F(openai.BetaThreadNewAndRunParamsThread{
Messages: openai.F([]openai.BetaThreadNewAndRunParamsThreadMessage{{
Content: openai.F[openai.BetaThreadNewAndRunParamsThreadMessagesContentUnion](shared.UnionString("string")),
Content: openai.F([]openai.MessageContentPartParamUnion{openai.ImageFileContentBlockParam{ImageFile: openai.F(openai.ImageFileParam{FileID: openai.F("file_id"), Detail: openai.F(openai.ImageFileDetailAuto)}), Type: openai.F(openai.ImageFileContentBlockTypeImageFile)}}),
Role: openai.F(openai.BetaThreadNewAndRunParamsThreadMessagesRoleUser),
Attachments: openai.F([]openai.BetaThreadNewAndRunParamsThreadMessagesAttachment{{
FileID: openai.F("file_id"),
Expand Down Expand Up @@ -285,7 +284,7 @@ func TestBetaThreadNewAndRunWithOptionalParams(t *testing.T) {
}}),
Metadata: openai.F[any](map[string]interface{}{}),
}, {
Content: openai.F[openai.BetaThreadNewAndRunParamsThreadMessagesContentUnion](shared.UnionString("string")),
Content: openai.F([]openai.MessageContentPartParamUnion{openai.ImageFileContentBlockParam{ImageFile: openai.F(openai.ImageFileParam{FileID: openai.F("file_id"), Detail: openai.F(openai.ImageFileDetailAuto)}), Type: openai.F(openai.ImageFileContentBlockTypeImageFile)}}),
Role: openai.F(openai.BetaThreadNewAndRunParamsThreadMessagesRoleUser),
Attachments: openai.F([]openai.BetaThreadNewAndRunParamsThreadMessagesAttachment{{
FileID: openai.F("file_id"),
Expand Down Expand Up @@ -317,7 +316,7 @@ func TestBetaThreadNewAndRunWithOptionalParams(t *testing.T) {
}}),
Metadata: openai.F[any](map[string]interface{}{}),
}, {
Content: openai.F[openai.BetaThreadNewAndRunParamsThreadMessagesContentUnion](shared.UnionString("string")),
Content: openai.F([]openai.MessageContentPartParamUnion{openai.ImageFileContentBlockParam{ImageFile: openai.F(openai.ImageFileParam{FileID: openai.F("file_id"), Detail: openai.F(openai.ImageFileDetailAuto)}), Type: openai.F(openai.ImageFileContentBlockTypeImageFile)}}),
Role: openai.F(openai.BetaThreadNewAndRunParamsThreadMessagesRoleUser),
Attachments: openai.F([]openai.BetaThreadNewAndRunParamsThreadMessagesAttachment{{
FileID: openai.F("file_id"),
Expand Down
20 changes: 5 additions & 15 deletions betathreadmessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -1945,8 +1945,11 @@ func (r TextDeltaBlockType) IsKnown() bool {
}

type BetaThreadMessageNewParams struct {
// The text contents of the message.
Content param.Field[BetaThreadMessageNewParamsContentUnion] `json:"content,required"`
// An array of content parts with a defined type, each can be of type `text` or
// images can be passed with `image_url` or `image_file`. Image types are only
// supported on
// [Vision-compatible models](https://platform.openai.com/docs/models/overview).
Content param.Field[[]MessageContentPartParamUnion] `json:"content,required"`
// The role of the entity that is creating the message. Allowed values include:
//
// - `user`: Indicates the message is sent by an actual user and should be used in
Expand All @@ -1967,19 +1970,6 @@ func (r BetaThreadMessageNewParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}

// The text contents of the message.
//
// Satisfied by [shared.UnionString],
// [BetaThreadMessageNewParamsContentArrayOfContentParts].
type BetaThreadMessageNewParamsContentUnion interface {
ImplementsBetaThreadMessageNewParamsContentUnion()
}

type BetaThreadMessageNewParamsContentArrayOfContentParts []MessageContentPartParamUnion

func (r BetaThreadMessageNewParamsContentArrayOfContentParts) ImplementsBetaThreadMessageNewParamsContentUnion() {
}

// The role of the entity that is creating the message. Allowed values include:
//
// - `user`: Indicates the message is sent by an actual user and should be used in
Expand Down
3 changes: 1 addition & 2 deletions betathreadmessage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/openai/openai-go"
"github.com/openai/openai-go/internal/testutil"
"github.com/openai/openai-go/option"
"github.com/openai/openai-go/shared"
)

func TestBetaThreadMessageNewWithOptionalParams(t *testing.T) {
Expand All @@ -30,7 +29,7 @@ func TestBetaThreadMessageNewWithOptionalParams(t *testing.T) {
context.TODO(),
"thread_id",
openai.BetaThreadMessageNewParams{
Content: openai.F[openai.BetaThreadMessageNewParamsContentUnion](shared.UnionString("string")),
Content: openai.F([]openai.MessageContentPartParamUnion{openai.ImageFileContentBlockParam{ImageFile: openai.F(openai.ImageFileParam{FileID: openai.F("file_id"), Detail: openai.F(openai.ImageFileDetailAuto)}), Type: openai.F(openai.ImageFileContentBlockTypeImageFile)}}),
Role: openai.F(openai.BetaThreadMessageNewParamsRoleUser),
Attachments: openai.F([]openai.BetaThreadMessageNewParamsAttachment{{
FileID: openai.F("file_id"),
Expand Down
20 changes: 5 additions & 15 deletions betathreadrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -727,8 +727,11 @@ func (r BetaThreadRunNewParams) MarshalJSON() (data []byte, err error) {
}

type BetaThreadRunNewParamsAdditionalMessage struct {
// The text contents of the message.
Content param.Field[BetaThreadRunNewParamsAdditionalMessagesContentUnion] `json:"content,required"`
// An array of content parts with a defined type, each can be of type `text` or
// images can be passed with `image_url` or `image_file`. Image types are only
// supported on
// [Vision-compatible models](https://platform.openai.com/docs/models/overview).
Content param.Field[[]MessageContentPartParamUnion] `json:"content,required"`
// The role of the entity that is creating the message. Allowed values include:
//
// - `user`: Indicates the message is sent by an actual user and should be used in
Expand All @@ -749,19 +752,6 @@ func (r BetaThreadRunNewParamsAdditionalMessage) MarshalJSON() (data []byte, err
return apijson.MarshalRoot(r)
}

// The text contents of the message.
//
// Satisfied by [shared.UnionString],
// [BetaThreadRunNewParamsAdditionalMessagesContentArrayOfContentParts].
type BetaThreadRunNewParamsAdditionalMessagesContentUnion interface {
ImplementsBetaThreadRunNewParamsAdditionalMessagesContentUnion()
}

type BetaThreadRunNewParamsAdditionalMessagesContentArrayOfContentParts []MessageContentPartParamUnion

func (r BetaThreadRunNewParamsAdditionalMessagesContentArrayOfContentParts) ImplementsBetaThreadRunNewParamsAdditionalMessagesContentUnion() {
}

// The role of the entity that is creating the message. Allowed values include:
//
// - `user`: Indicates the message is sent by an actual user and should be used in
Expand Down
7 changes: 3 additions & 4 deletions betathreadrun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/openai/openai-go"
"github.com/openai/openai-go/internal/testutil"
"github.com/openai/openai-go/option"
"github.com/openai/openai-go/shared"
)

func TestBetaThreadRunNewWithOptionalParams(t *testing.T) {
Expand All @@ -33,7 +32,7 @@ func TestBetaThreadRunNewWithOptionalParams(t *testing.T) {
AssistantID: openai.F("assistant_id"),
AdditionalInstructions: openai.F("additional_instructions"),
AdditionalMessages: openai.F([]openai.BetaThreadRunNewParamsAdditionalMessage{{
Content: openai.F[openai.BetaThreadRunNewParamsAdditionalMessagesContentUnion](shared.UnionString("string")),
Content: openai.F([]openai.MessageContentPartParamUnion{openai.ImageFileContentBlockParam{ImageFile: openai.F(openai.ImageFileParam{FileID: openai.F("file_id"), Detail: openai.F(openai.ImageFileDetailAuto)}), Type: openai.F(openai.ImageFileContentBlockTypeImageFile)}}),
Role: openai.F(openai.BetaThreadRunNewParamsAdditionalMessagesRoleUser),
Attachments: openai.F([]openai.BetaThreadRunNewParamsAdditionalMessagesAttachment{{
FileID: openai.F("file_id"),
Expand Down Expand Up @@ -65,7 +64,7 @@ func TestBetaThreadRunNewWithOptionalParams(t *testing.T) {
}}),
Metadata: openai.F[any](map[string]interface{}{}),
}, {
Content: openai.F[openai.BetaThreadRunNewParamsAdditionalMessagesContentUnion](shared.UnionString("string")),
Content: openai.F([]openai.MessageContentPartParamUnion{openai.ImageFileContentBlockParam{ImageFile: openai.F(openai.ImageFileParam{FileID: openai.F("file_id"), Detail: openai.F(openai.ImageFileDetailAuto)}), Type: openai.F(openai.ImageFileContentBlockTypeImageFile)}}),
Role: openai.F(openai.BetaThreadRunNewParamsAdditionalMessagesRoleUser),
Attachments: openai.F([]openai.BetaThreadRunNewParamsAdditionalMessagesAttachment{{
FileID: openai.F("file_id"),
Expand Down Expand Up @@ -97,7 +96,7 @@ func TestBetaThreadRunNewWithOptionalParams(t *testing.T) {
}}),
Metadata: openai.F[any](map[string]interface{}{}),
}, {
Content: openai.F[openai.BetaThreadRunNewParamsAdditionalMessagesContentUnion](shared.UnionString("string")),
Content: openai.F([]openai.MessageContentPartParamUnion{openai.ImageFileContentBlockParam{ImageFile: openai.F(openai.ImageFileParam{FileID: openai.F("file_id"), Detail: openai.F(openai.ImageFileDetailAuto)}), Type: openai.F(openai.ImageFileContentBlockTypeImageFile)}}),
Role: openai.F(openai.BetaThreadRunNewParamsAdditionalMessagesRoleUser),
Attachments: openai.F([]openai.BetaThreadRunNewParamsAdditionalMessagesAttachment{{
FileID: openai.F("file_id"),
Expand Down
Loading

0 comments on commit b228103

Please sign in to comment.