-
-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
beb0444
commit 1892e46
Showing
16 changed files
with
915 additions
and
651 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
//----------------------------------------------- | ||
// | ||
// This file is part of the Siv3D Engine. | ||
// | ||
// Copyright (c) 2008-2023 Ryo Suzuki | ||
// Copyright (c) 2016-2023 OpenSiv3D Project | ||
// | ||
// Licensed under the MIT License. | ||
// | ||
//----------------------------------------------- | ||
|
||
# pragma once | ||
# include "../Common.hpp" | ||
# include "../String.hpp" | ||
# include "../Array.hpp" | ||
# include "../AsyncHTTPTask.hpp" | ||
|
||
namespace s3d | ||
{ | ||
namespace OpenAI | ||
{ | ||
namespace Chat | ||
{ | ||
namespace Model | ||
{ | ||
/// @brief GPT-3.5 モデル | GPT-3.5 model | ||
/// @see https://platform.openai.com/docs/models/gpt-3-5 | ||
/// @remark Chat API 用のモデルです。 | This is a model for the Chat API. | ||
inline constexpr StringView GPT3_5_Turbo{ U"gpt-3.5-turbo" }; | ||
|
||
/// @brief GPT-3.5 モデル(16k トークン) | GPT-3.5 model (16k tokens) | ||
/// @see https://platform.openai.com/docs/models/gpt-3-5 | ||
/// @remark Chat API 用のモデルです。 | This is a model for the Chat API. | ||
inline constexpr StringView GPT3_5_Turbo_16K{ U"gpt-3.5-turbo-16k" }; | ||
|
||
/// @brief GPT-4 モデル | GPT-4 model | ||
/// @see https://platform.openai.com/docs/models/gpt-4 | ||
/// @remark Chat API 用のモデルです。 | This is a model for the Chat API. | ||
inline constexpr StringView GPT4{ U"gpt-4" }; | ||
|
||
/// @brief GPT-4 モデル(32k トークン) | GPT-4 model (32k tokens) | ||
/// @see https://platform.openai.com/docs/models/gpt-4 | ||
/// @remark Chat API 用のモデルです。 | This is a model for the Chat API. | ||
inline constexpr StringView GPT4_32K{ U"gpt-4-32k" }; | ||
} | ||
|
||
/// @brief ChatGPT にメッセージを送り、その返答メッセージを取得します。 | Sends a message to ChatGPT and retrieves the response message. | ||
/// @param apiKey OpenAI API キー | OpenAI API key | ||
/// @param message メッセージ | Message | ||
/// @param model 使用するモデル | The model to be used (default: Model::GPT3_5_Turbo) | ||
/// @return 返答メッセージ。取得に失敗した場合は空の文字列 | Response message. An empty string if the retrieval fails. | ||
/// @remark インターネットアクセスが必要です。 | Internet access is required. | ||
[[nodiscard]] | ||
String Complete(StringView apiKey, StringView message, StringView model = Model::GPT3_5_Turbo); | ||
|
||
/// @brief ChatGPT にメッセージを送り、その返答メッセージを取得します。 | Sends a message to ChatGPT and retrieves the response message. | ||
/// @param apiKey OpenAI API キー | OpenAI API key | ||
/// @param message メッセージ | Message | ||
/// @param error エラーメッセージの格納先。エラーが無い場合は空の文字列になる | Destination for the error message. Will be an empty string if no error occurs. | ||
/// @param model 使用するモデル | The model to be used (default: Model::GPT3_5_Turbo) | ||
/// @return 返答メッセージ。取得に失敗した場合は空の文字列 | Response message. An empty string if the retrieval fails. | ||
/// @remark インターネットアクセスが必要です。 | Internet access is required. | ||
[[nodiscard]] | ||
String Complete(StringView apiKey, StringView message, String& error, StringView model = Model::GPT3_5_Turbo); | ||
|
||
/// @brief ChatGPT にメッセージを送り、その返答メッセージを取得します。 | Sends messages to ChatGPT and retrieves the response message. | ||
/// @param apiKey OpenAI API キー | OpenAI API key | ||
/// @param messages メッセージ(ロールとメッセージのペアの配列) | Array of message pairs (role and message) | ||
/// @return 返答メッセージ。取得に失敗した場合は空の文字列 | Response message. An empty string if the retrieval fails. | ||
/// @param model 使用するモデル | The model to be used (default: Model::GPT3_5_Turbo) | ||
/// @remark ロールは U"system", U"user", U"assistant" の 3 種類です。 | Roles are U"system", U"user", and U"assistant". | ||
/// @remark インターネットアクセスが必要です。 | Internet access is required. | ||
[[nodiscard]] | ||
String Complete(StringView apiKey, const Array<std::pair<String, String>>& messages, StringView model = Model::GPT3_5_Turbo); | ||
|
||
/// @brief ChatGPT にメッセージを送り、その返答メッセージを取得します。 | Sends messages to ChatGPT and retrieves the response message. | ||
/// @param apiKey OpenAI API キー | OpenAI API key | ||
/// @param messages メッセージ(ロールとメッセージのペアの配列) | Array of message pairs (role and message) | ||
/// @param error エラーメッセージの格納先。エラーが無い場合は空の文字列になる | Destination for the error message. Will be an empty string if no error occurs. | ||
/// @param model 使用するモデル | The model to be used (default: Model::GPT3_5_Turbo) | ||
/// @return 返答メッセージ。取得に失敗した場合は空の文字列 | Response message. An empty string if the retrieval fails. | ||
/// @remark ロールは U"system", U"user", U"assistant" の 3 種類です。 | Roles are U"system", U"user", and U"assistant". | ||
/// @remark インターネットアクセスが必要です。 | Internet access is required. | ||
[[nodiscard]] | ||
String Complete(StringView apiKey, const Array<std::pair<String, String>>& messages, String& error, StringView model = Model::GPT3_5_Turbo); | ||
|
||
/// @brief ChatGPT にメッセージを送り、レスポンス(JSON)を取得する非同期タスクを返します。 | Returns an asynchronous task for sending a message to ChatGPT and retrieving the response (JSON). | ||
/// @param apiKey OpenAI API キー | OpenAI API key | ||
/// @param message メッセージ | Message | ||
/// @param model 使用するモデル | The model to be used (default: Model::GPT3_5_Turbo) | ||
/// @return 非同期タスク | Asynchronous task | ||
/// @remark 戻り値の task が `(task.isReady() == true) && (task.getResponse().isOK() == true)` になれば結果を取得できます。 | The result can be retrieved if `(task.isReady() == true) && (task.getResponse().isOK() == true)`. | ||
/// @remark インターネットアクセスが必要です。 | Internet access is required. | ||
[[nodiscard]] | ||
AsyncHTTPTask CompleteAsync(StringView apiKey, StringView message, StringView model = Model::GPT3_5_Turbo); | ||
|
||
/// @brief ChatGPT にメッセージを送り、レスポンス(JSON)を取得する非同期タスクを返します。 | Returns an asynchronous task for sending messages to ChatGPT and retrieving the response (JSON). | ||
/// @param apiKey OpenAI API キー | OpenAI API key | ||
/// @param messages メッセージ(ロールとメッセージのペアの配列) | Array of message pairs (role and message) | ||
/// @param model 使用するモデル | The model to be used (default: Model::GPT3_5_Turbo) | ||
/// @return 非同期タスク | Asynchronous task | ||
/// @remark ロールは U"system", U"user", U"assistant" の 3 種類です。 | Roles are U"system", U"user", and U"assistant". | ||
/// @remark 戻り値の task が `(task.isReady() == true) && (task.getResponse().isOK() == true)` になれば結果を取得できます。 | The result can be retrieved if `(task.isReady() == true) && (task.getResponse().isOK() == true)`. | ||
/// @remark インターネットアクセスが必要です。 | Internet access is required. | ||
[[nodiscard]] | ||
AsyncHTTPTask CompleteAsync(StringView apiKey, const Array<std::pair<String, String>>& messages, StringView model = Model::GPT3_5_Turbo); | ||
|
||
/// @brief ChatGPT のレスポンス(JSON)から、返答メッセージを抽出して返します。 | Extracts and returns the response message from the ChatGPT response (JSON). | ||
/// @param response JSON レスポンス | JSON response | ||
/// @return 返答メッセージ。抽出に失敗した場合は空の文字列 | Response message. An empty string if extraction fails. | ||
/// @remark 通常は `AsyncHTTPTask::getAsJSON()` の戻り値を渡します。 | Typically, pass the return value of `AsyncHTTPTask::getAsJSON()`. | ||
[[nodiscard]] | ||
String GetContent(const JSON& response); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
//----------------------------------------------- | ||
// | ||
// This file is part of the Siv3D Engine. | ||
// | ||
// Copyright (c) 2008-2023 Ryo Suzuki | ||
// Copyright (c) 2016-2023 OpenSiv3D Project | ||
// | ||
// Licensed under the MIT License. | ||
// | ||
//----------------------------------------------- | ||
|
||
# pragma once | ||
# include "../Common.hpp" | ||
# include "../String.hpp" | ||
# include "../Array.hpp" | ||
# include "../AsyncHTTPTask.hpp" | ||
|
||
namespace s3d | ||
{ | ||
class JSON; | ||
|
||
namespace OpenAI | ||
{ | ||
namespace Embedding | ||
{ | ||
namespace Model | ||
{ | ||
/// @brief 埋め込みモデル text-embedding-ada-002 | Embedding Model text-embedding-ada-002 | ||
/// @see https://platform.openai.com/docs/models/embeddings | ||
/// @remark Embeddings API 用のモデルです。 | This is a model for the Embeddings API. | ||
inline constexpr StringView EmbeddingAda002{ U"text-embedding-ada-002" }; | ||
} | ||
|
||
/// @brief 文章の埋め込みベクトルを返します。 | Returns the embedding vector of the text. | ||
/// @param apiKey OpenAI API キー | OpenAI API key | ||
/// @param text 文章 | Text | ||
/// @param model 使用するモデル | The model to be used (default: Model::EmbeddingAda002) | ||
/// @return 埋め込みベクトル。取得に失敗した場合空の配列 | Embedding vector. Empty array if the retrieval fails. | ||
/// @remark インターネットアクセスが必要です。 | Internet access is required. | ||
[[nodiscard]] | ||
Array<float> Create(StringView apiKey, StringView text, StringView model = Model::EmbeddingAda002); | ||
|
||
/// @brief 文章の埋め込みベクトルを返します。 | Returns the embedding vector of the text. | ||
/// @param apiKey OpenAI API キー | OpenAI API key | ||
/// @param text 文章 | Text | ||
/// @param error エラーメッセージの格納先。エラーが無い場合は空の文字列になる | Destination for the error message. Will be an empty string if no error occurs. | ||
/// @param model 使用するモデル | The model to be used (default: Model::EmbeddingAda002) | ||
/// @return 埋め込みベクトル。取得に失敗した場合空の配列 | Embedding vector. Empty array if the retrieval fails. | ||
/// @remark インターネットアクセスが必要です。 | Internet access is required. | ||
[[nodiscard]] | ||
Array<float> Create(StringView apiKey, StringView text, String& error, StringView model = Model::EmbeddingAda002); | ||
|
||
/// @brief 文章の埋め込みベクトルを含むレスポンス(JSON)を取得する非同期タスクを返します。 | Returns an asynchronous task that retrieves a response (JSON) containing the embedding vector of the text. | ||
/// @param apiKey OpenAI API キー | OpenAI API key | ||
/// @param text 文章 | Text | ||
/// @param model 使用するモデル | The model to be used (default: Model::EmbeddingAda002) | ||
/// @return 非同期タスク | Asynchronous task | ||
/// @remark インターネットアクセスが必要です。 | Internet access is required. | ||
[[nodiscard]] | ||
AsyncHTTPTask CreateAsync(StringView apiKey, StringView text, StringView model = Model::EmbeddingAda002); | ||
|
||
/// @brief レスポンス(JSON)から、文章の埋め込みベクトルを抽出して返します。 | Extracts and returns the embedding vector of the text from the response (JSON). | ||
/// @param response JSON レスポンス | JSON response | ||
/// @return 埋め込みベクトル。抽出に失敗した場合は空の文字列 | Embedding vector. Empty array if the extraction fails. | ||
[[nodiscard]] | ||
Array<float> GetVector(const JSON& response); | ||
|
||
/// @brief 2 つの埋め込みベクトルのコサイン類似度を返します。 | Returns the cosine similarity of two embedding vectors. | ||
/// @param a 一方の埋め込みベクトル(正規化済み) | One of the embedding vectors (normalized) | ||
/// @param b もう一方の埋め込みベクトル(正規化済み) | The other embedding vector (normalized) | ||
/// @return コサイン類似度 | Cosine similarity | ||
[[nodiscard]] | ||
float CosineSimilarity(const Array<float>& a, const Array<float>& b); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
//----------------------------------------------- | ||
// | ||
// This file is part of the Siv3D Engine. | ||
// | ||
// Copyright (c) 2008-2023 Ryo Suzuki | ||
// Copyright (c) 2016-2023 OpenSiv3D Project | ||
// | ||
// Licensed under the MIT License. | ||
// | ||
//----------------------------------------------- | ||
|
||
# pragma once | ||
# include "../Common.hpp" | ||
# include "../String.hpp" | ||
# include "../Array.hpp" | ||
# include "../Image.hpp" | ||
|
||
namespace s3d | ||
{ | ||
namespace OpenAI | ||
{ | ||
namespace Image | ||
{ | ||
/// @brief 256x256 ピクセルの画像サイズ | Image size of 256x256 pixels | ||
inline constexpr Size ImageSize256{ 256, 256 }; | ||
|
||
/// @brief 512x512 ピクセルの画像サイズ | Image size of 512x512 pixels | ||
inline constexpr Size ImageSize512{ 512, 512 }; | ||
|
||
/// @brief 1024x1024 ピクセルの画像サイズ | Image size of 1024x1024 pixels | ||
inline constexpr Size ImageSize1024{ 1024, 1024 }; | ||
|
||
/// @brief 生成できる最大の画像の個数 | Maximum number of images that can be generated | ||
inline constexpr int32 MaxImageCount = 10; | ||
|
||
/// @brief テキストに基づいて DALL-E モデルを使用して生成された画像を取得します。 | Get an image generated by the DALL-E model based on the given text. | ||
/// @param apiKey OpenAI API キー | OpenAI API key | ||
/// @param prompt 画像を説明するテキスト。英語で 1000 文字以下 | Text describing the image, in English and up to 1000 characters | ||
/// @param size 生成する画像のサイズ。OpenAI::Image::ImageSize256, OpenAI::Image::ImageSize512, OpenAI::Image::ImageSize1024 のいずれか | Size of the generated image. One of OpenAI::Image::ImageSize256, OpenAI::Image::ImageSize512, OpenAI::Image::ImageSize1024 | ||
/// @return 生成された画像。生成に失敗した場合は空の画像 | Generated image. An empty image if the generation fails. | ||
/// @remark インターネットアクセスが必要です。 | Internet access is required. | ||
[[nodiscard]] | ||
s3d::Image Create(StringView apiKey, StringView prompt, const Size& size = OpenAI::Image::ImageSize256); | ||
|
||
/// @brief テキストに基づいて DALL-E モデルを使用して生成された 1 つまたは複数の画像を取得します。 | Get one or more images generated by the DALL-E model based on the given text. | ||
/// @param apiKey OpenAI API キー | OpenAI API key | ||
/// @param prompt 画像を説明するテキスト。英語で 1000 文字以下 | Text describing the image, in English and up to 1000 characters | ||
/// @param n 生成する画像の個数。1 以上 OpenAI::MaxImageCount 以下 | Number of images to generate, between 1 and OpenAI::MaxImageCount | ||
/// @param size 生成する画像のサイズ。OpenAI::Image::ImageSize256, OpenAI::Image::ImageSize512, OpenAI::Image::ImageSize1024 のいずれか | Size of the generated images. One of OpenAI::Image::ImageSize256, OpenAI::Image::ImageSize512, OpenAI::Image::ImageSize1024 | ||
/// @return 生成された画像の配列。生成に失敗した場合は空の配列 | Array of generated images. An empty array if the generation fails. | ||
/// @remark インターネットアクセスが必要です。 | Internet access is required. | ||
[[nodiscard]] | ||
Array<s3d::Image> Create(StringView apiKey, StringView prompt, int32 n, const Size& size = OpenAI::Image::ImageSize256); | ||
|
||
/// @brief テキストに基づいて DALL-E モデルを使用して画像を生成する非同期タスクを返します。 | Returns an asynchronous task that generates an image using the DALL-E model based on the given text. | ||
/// @param apiKey OpenAI API キー | OpenAI API key | ||
/// @param prompt 画像を説明するテキスト。英語で 1000 文字以下 | Text describing the image, in English and up to 1000 characters | ||
/// @param size 生成する画像のサイズ。OpenAI::Image::ImageSize256, OpenAI::Image::ImageSize512, OpenAI::Image::ImageSize1024 のいずれか | Size of the generated image. One of OpenAI::Image::ImageSize256, OpenAI::Image::ImageSize512, OpenAI::Image::ImageSize1024 | ||
/// @return 生成された画像を返す非同期タスク | Asynchronous task that returns the generated image | ||
/// @remark 戻り値の task が `task.isReady() == true` になれば `task.get()` で結果を取得できます。 | The result can be retrieved if `task.isReady() == true`. | ||
/// @remark インターネットアクセスが必要です。 | Internet access is required. | ||
[[nodiscard]] | ||
AsyncTask<s3d::Image> CreateAsync(StringView apiKey, StringView prompt, const Size& size = OpenAI::Image::ImageSize256); | ||
|
||
/// @brief テキストに基づいて DALL-E モデルを使用して 1 つまたは複数の画像を生成する非同期タスクを返します。 | Returns an asynchronous task that generates one or more images using the DALL-E model based on the given text. | ||
/// @param apiKey OpenAI API キー | OpenAI API key | ||
/// @param prompt 画像を説明するテキスト。英語で 1000 文字以下 | Text describing the image, in English and up to 1000 characters | ||
/// @param n 生成する画像の個数。1 以上 OpenAI::MaxImageCount 以下 | Number of images to generate, between 1 and OpenAI::MaxImageCount | ||
/// @param size 生成する画像のサイズ。OpenAI::Image::ImageSize256, OpenAI::Image::ImageSize512, OpenAI::Image::ImageSize1024 のいずれか | Size of the generated image. One of OpenAI::Image::ImageSize256, OpenAI::Image::ImageSize512, OpenAI::Image::ImageSize1024 | ||
/// @return 生成された画像の配列を返す非同期タスク | Asynchronous task that returns an array of generated images | ||
/// @remark 戻り値の task が `task.isReady() == true` になれば `task.get()` で結果を取得できます。 | The result can be retrieved if `task.isReady() == true`. | ||
/// @remark インターネットアクセスが必要です。 | Internet access is required. | ||
[[nodiscard]] | ||
AsyncTask<Array<s3d::Image>> CreateAsync(StringView apiKey, StringView prompt, int32 n, const Size& size = OpenAI::Image::ImageSize256); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
//----------------------------------------------- | ||
// | ||
// This file is part of the Siv3D Engine. | ||
// | ||
// Copyright (c) 2008-2023 Ryo Suzuki | ||
// Copyright (c) 2016-2023 OpenSiv3D Project | ||
// | ||
// Licensed under the MIT License. | ||
// | ||
//----------------------------------------------- | ||
|
||
# pragma once | ||
# include "../Common.hpp" | ||
# include "../String.hpp" | ||
|
||
namespace s3d | ||
{ | ||
namespace OpenAI | ||
{ | ||
namespace Speech | ||
{ | ||
struct Request | ||
{ | ||
String model = U"tts-1"; | ||
|
||
String input; | ||
|
||
String voice = U"alloy"; | ||
|
||
String responseFormat = U"mp3"; | ||
|
||
double speed = 1.0; | ||
}; | ||
|
||
bool Create(const Request& request, FilePathView path); | ||
} | ||
} | ||
} |
Oops, something went wrong.