embeddings method
Represents the parameters for generating text using the OpenAI API.
The input parameter is the prompt or starting text for text generation.
The encodingFormat parameter specifies the format of the input text, with the default value being 'float'.
The dimensions parameter specifies the number of dimensions for the generated text.
The user parameter is an optional identifier for the user.
The maxTokens parameter specifies the maximum number of tokens to generate.
The temperature parameter controls the randomness of the generated text, with lower values producing more focused output.
The topP parameter controls the diversity of the generated text, with lower values producing more focused output.
The n parameter specifies the number of text completions to generate.
Implementation
Future<OpenAIEmbeddings> embeddings({
required String input,
String encodingFormat = 'float',
int? dimensions,
String? user,
double temperature = 0.3,
double topP = 1.0,
int n = 1,
}) async {
/// Sets the API URL for OpenAI embeddings.
///
/// The [apiUrl] parameter should be a valid URL for the OpenAI embeddings API.
/// By default, it is set to "https://api.openai.com/v1/embeddings".
///
/// Example:
/// ```dart
/// apiUrl = "https://api.openai.com/v1/embeddings";
/// ```
///
/// Throws an [ArgumentError] if the specified [model] is not supported.
/// The [model] parameter should be one of the supported model values:
/// - "text-embedding-ada-002"
/// - "text-embedding-3-small"
/// - "text-embedding-3-large"
///
/// Example:
/// ```dart
/// List<String> modelValues = [
/// "text-embedding-ada-002",
/// "text-embedding-3-small",
/// "text-embedding-3-large",
/// ];
///
/// if (!modelValues.contains(model)) {
/// throw ArgumentError('Embeddings application does not accept model $model');
/// }
/// ```
///
/// Note: This code snippet assumes that the `apiUrl` and `model` variables are defined.
apiUrl = "https://api.openai.com/v1/embeddings";
List<String> modelValues = [
"text-embedding-ada-002",
"text-embedding-3-small",
"text-embedding-3-large",
];
if (!modelValues.contains(model)) {
throw ArgumentError(
'Embeddings application does not accept model $model');
}
/// Creates the request body with the specified parameters.
///
/// The [model] parameter specifies the model to use.
/// The [input] parameter specifies the input text.
/// The [maxTokens] parameter specifies the maximum number of tokens to generate.
/// The [temperature] parameter controls the randomness of the output.
/// The [topP] parameter controls the diversity of the output.
/// The [n] parameter specifies the number of completions to generate.
/// The [encodingFormat] parameter specifies the encoding format of the input text.
/// The [dimensions] parameter specifies the dimensions of the input text.
/// The [user] parameter specifies the user ID.
///
/// Returns a map representing the request body.
// Parametreleri oluştur
Map<String, dynamic> requestBody = {
"model": model,
"input": input,
"temperature": temperature,
"top_p": topP,
"n": n,
"encoding_format": encodingFormat,
};
if (dimensions != null) {
requestBody['dimensions'] = dimensions;
}
if (user != null) {
requestBody['user'] = user;
}
/// Sends a POST request to the specified API endpoint with the given request body.
///
/// The [requestBody] is encoded as JSON and added to the request body.
/// The [apiUrl] is the URL of the API endpoint.
/// The [apiKey] is the authorization token used for authentication.
///
/// Returns the response body as a string.
final encodedBody = utf8.encode(jsonEncode(requestBody));
// Creates a new HttpClient object
var httpClient = HttpClient();
// Sets the connection timeout in seconds (defaults to 30 seconds)
httpClient.connectionTimeout = Duration(seconds: connectionTimeout);
final request = await httpClient.postUrl(Uri.parse(apiUrl));
request.headers.set('Content-Type', 'application/json; charset=utf-8');
request.headers.set('Authorization', 'Bearer $apiKey');
if (organization != '') {
request.headers.set('OpenAI-Organization', organization);
}
request.add(encodedBody);
final httpResponse = await request.close();
String responseBody = await utf8.decodeStream(httpResponse);
Future<OpenAIEmbeddings> formatEmbeddings =
OpenAIEmbeddings.fetchData(responseBody);
return formatEmbeddings;
}