44
55namespace OpenAi {
66
7- struct ImageUrl {
8- // The external URL of the image, must be a supported image types: jpeg, jpg, png, gif, webp.
7+ struct ImageUrl : public JsonSerializable {
8+ /* *
9+ * The external URL of the image, must be a supported image types:
10+ * jpeg, jpg, png, gif, webp.
11+ */
912 std::string url;
1013
11- // Specifies the detail level of the image. low uses fewer tokens, you can opt in to high resolution using high. Default value is auto
14+ /* *
15+ * Specifies the detail level of the image. low uses fewer tokens, you
16+ * can opt in to high resolution using high. Default value is auto
17+ */
1218 std::string detail;
1319
14- ImageUrl () = default ;
20+ ImageUrl (const std::string& url, const std::string& detail = " auto" )
21+ : url{url}, detail{detail} {}
1522
1623 ImageUrl (ImageUrl&&) noexcept = default ;
1724
@@ -20,13 +27,25 @@ struct ImageUrl {
2027 ImageUrl (const ImageUrl&) = delete ;
2128
2229 ImageUrl& operator =(const ImageUrl&) = delete ;
30+
31+ cpp::result<Json::Value, std::string> ToJson () override {
32+ try {
33+ Json::Value root;
34+ root[" url" ] = url;
35+ root[" detail" ] = detail;
36+ return root;
37+ } catch (const std::exception& e) {
38+ return cpp::fail (std::string (" ToJson failed: " ) + e.what ());
39+ }
40+ }
2341};
2442
2543// References an image URL in the content of a message.
2644struct ImageUrlContent : Content {
2745
2846 // The type of the content part.
29- ImageUrlContent (const std::string& type) : Content(type) {}
47+ explicit ImageUrlContent (const std::string& type, ImageUrl&& image_url)
48+ : Content(type), image_url{std::move (image_url)} {}
3049
3150 ImageUrlContent (ImageUrlContent&&) noexcept = default ;
3251
@@ -38,18 +57,18 @@ struct ImageUrlContent : Content {
3857
3958 ImageUrl image_url;
4059
60+ ~ImageUrlContent () override = default ;
61+
4162 static cpp::result<ImageUrlContent, std::string> FromJson (
4263 Json::Value&& json) {
4364 if (json.empty ()) {
4465 return cpp::fail (" Json string is empty" );
4566 }
4667
4768 try {
48- ImageUrlContent content{" image_url" };
49- ImageUrl image_url;
50- image_url.url = std::move (json[" image_url" ][" url" ].asString ());
51- image_url.detail = std::move (json[" image_url" ][" detail" ].asString ());
52- content.image_url = std::move (image_url);
69+ auto image_url = ImageUrl (json[" image_url" ][" url" ].asString (),
70+ json[" image_url" ][" detail" ].asString ());
71+ ImageUrlContent content{" image_url" , std::move (image_url)};
5372 return content;
5473 } catch (const std::exception& e) {
5574 return cpp::fail (std::string (" FromJson failed: " ) + e.what ());
@@ -60,8 +79,7 @@ struct ImageUrlContent : Content {
6079 try {
6180 Json::Value json;
6281 json[" type" ] = type;
63- json[" image_url" ][" url" ] = image_url.url ;
64- json[" image_url" ][" detail" ] = image_url.detail ;
82+ json[" image_url" ] = image_url.ToJson ().value ();
6583 return json;
6684 } catch (const std::exception& e) {
6785 return cpp::fail (std::string (" ToJson failed: " ) + e.what ());
0 commit comments