Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Thread and Message for Assistant v2 #735

Open
ZhihanWei opened this issue May 6, 2024 · 10 comments
Open

Support Thread and Message for Assistant v2 #735

ZhihanWei opened this issue May 6, 2024 · 10 comments
Labels
enhancement New feature or request

Comments

@ZhihanWei
Copy link

ZhihanWei commented May 6, 2024

Is your feature request related to a problem? Please describe.
OpenAI has released their new version of assistant(v2) and the old version of assistant(v1) will be deprecated by the end of 2024. There are some incompatibilities between the two version in terms of API request body.
For example, MessageRequest in v1

type MessageRequest struct {
	Role     string         `json:"role"`
	Content  string         `json:"content"`
	FileIds  []string       `json:"file_ids,omitempty"` //nolint:revive // backwards-compatibility
	Metadata map[string]any `json:"metadata,omitempty"`
}

MessageRequest in v2, which introduced attachments

type MessageRequest struct {
	Role        string         `json:"role"`
	Content     string         `json:"content"`
	Attachments []Attachment   `json:"attachments,omitempty"`
	Metadata    map[string]any `json:"metadata,omitempty"`
}

We let the user configure the assistant version in the client configuration but the new assistant version will cause trouble at runtime when making calls to Thread and Message.

Describe the solution you'd like
Support both assistant v1 and v2.

Additional context
There are different implementations I can think of that could make the enhancement, but most likely the compatibility will be checked at runtime. Would be good to see an implementation to support both assistant versions and check errors at compile time while maintaining the configured assistantVersion.

@ZhihanWei ZhihanWei added the enhancement New feature or request label May 6, 2024
@Trojan295
Copy link

There are more changes, because the Content can be now a string, but also an array of objects. https://platform.openai.com/docs/api-reference/messages/createMessage

@ZhihanWei
Copy link
Author

In the new version, seems like assistant v1 cannot usegpt-4o.

@nadeemc
Copy link

nadeemc commented Jun 9, 2024

A workaround for now is to use a custom config to set the version:

config := openai.DefaultConfig(key)
config.AssistantVersion = "v2"

return openai.NewClientWithConfig(config)

@dezchai
Copy link

dezchai commented Jun 18, 2024

Was working on a pull request for this but don't know how to handle content being either a string or array in Go.

@dezchai
Copy link

dezchai commented Jun 19, 2024

@sashabaranov thoughts?

@sashabaranov
Copy link
Owner

https://github.com/sashabaranov/go-openai/releases/tag/v1.26.0 contains assistant API v2 by default

@dezchai
Copy link

dezchai commented Jun 19, 2024

@sashabaranov It's still missing attachments and content being a string or object.
dezchai@f4149b4
See: https://platform.openai.com/docs/api-reference/messages/object

@sashabaranov
Copy link
Owner

@dezchai we're open for your contributions to add those! 🥰

@dezchai
Copy link

dezchai commented Jun 19, 2024

@sashabaranov Sounds good, but I'm new to Go and am not sure how content being a string or array* in https://platform.openai.com/docs/api-reference/runs/createThreadAndRun
image

@sashabaranov
Copy link
Owner

sashabaranov commented Jun 19, 2024

@dezchai there are a couple of ways we can go here:

  1. Use any as a type and document that it's either a string or array (slice in Go's terms). Then, it would be possible to use a type switch with it https://go.dev/tour/methods/16
  2. Use json.RawMessage for it, see it's usage for errors
  3. Add a custom type with json.Unmarshaler implemented for it (i.e. having a UnmarshalJSON method) errors examples applies here as well

UPD: note that with the first approach you'll get []any by default, not []string — see example https://go.dev/play/p/WTFANwhyJBT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants