OpenAIEmbeddings class
Wrapper around OpenAI Embeddings API.
Example:
final embeddings = OpenAIEmbeddings(apiKey: openaiApiKey);
final res = await embeddings.embedQuery('Hello world');
You can also use this wrapper to consume OpenAI-compatible APIs like Anyscale, Together AI, etc.
Authentication
The OpenAI API uses API keys for authentication. Visit your API Keys page to retrieve the API key you'll use in your requests.
Organization (optional)
For users who belong to multiple organizations, you can specify which organization is used for an API request. Usage from these API requests will count against the specified organization's subscription quota.
final client = OpenAIEmbeddings(
apiKey: 'OPENAI_API_KEY',
organization: 'org-dtDDtkEGoFccn5xaP5W1p3Rr',
);
Advance
Azure OpenAI Service
OpenAI's models are also available as an Azure service.
Although the Azure OpenAI API is similar to the official OpenAI API, there are subtle differences between them. This client is intended to be used with the official OpenAI API, but most of the functionality should work with the Azure OpenAI API as well.
If you want to use this client with the Azure OpenAI API (at your own risk), you can do so by instantiating the client as follows:
final client = OpenAIEmbeddings(
baseUrl: 'https://YOUR_RESOURCE_NAME.openai.azure.com/openai/deployments/YOUR_DEPLOYMENT_NAME',
headers: { 'api-key': 'YOUR_API_KEY' },
queryParams: { 'api-version': 'API_VERSION' },
);
YOUR_RESOURCE_NAME
: This value can be found in the Keys & Endpoint section when examining your resource from the Azure portal.YOUR_DEPLOYMENT_NAME
: This value will correspond to the custom name you chose for your deployment when you deployed a model. This value can be found under Resource Management > Deployments in the Azure portal.YOUR_API_KEY
: This value can be found in the Keys & Endpoint section when examining your resource from the Azure portal.API_VERSION
: The Azure OpenAI API version to use (e.g.2023-05-15
). Try to use the latest version available, it will probably be the closest to the official OpenAI API.
Custom HTTP client
You can always provide your own implementation of http.Client
for further
customization:
final client = OpenAIEmbeddings(
apiKey: 'OPENAI_API_KEY',
client: MyHttpClient(),
);
Using a proxy
HTTP proxy
You can use your own HTTP proxy by overriding the baseUrl
and providing
your required headers
:
final client = OpenAIEmbeddings(
baseUrl: 'https://my-proxy.com',
headers: {'x-my-proxy-header': 'value'},
);
If you need further customization, you can always provide your own
http.Client
.
SOCKS5 proxy
To use a SOCKS5 proxy, you can use the
socks5_proxy
package and a
custom http.Client
.
Constructors
-
OpenAIEmbeddings.new({String? apiKey, String? organization, String baseUrl = 'https://api.openai.com/v1', Map<
String, String> ? headers, Map<String, dynamic> ? queryParams, Client? client, String model = 'text-embedding-3-small', int? dimensions, int batchSize = 512, String? user}) - Create a new OpenAIEmbeddings instance.
Properties
- apiKey ↔ String
-
Get the API key.
getter/setter pair
- batchSize ↔ int
-
The maximum number of documents to embed in a single request.
This is limited by max input tokens for the model
(e.g. 8191 tokens for text-embedding-3-small).
getter/setter pair
- dimensions ↔ int?
-
The number of dimensions the resulting output embeddings should have.
Only supported in
text-embedding-3
and later models.getter/setter pair - hashCode → int
-
The hash code for this object.
no setterinherited
- model ↔ String
-
ID of the model to use (e.g. 'text-embedding-3-small').
getter/setter pair
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- user ↔ String?
-
A unique identifier representing your end-user, which can help OpenAI to
monitor and detect abuse.
getter/setter pair
Methods
-
close(
) → void - Closes the client and cleans up any resources associated with it.
-
embedDocuments(
List< Document> documents) → Future<List< List< >double> > - Embed search docs.
-
embedQuery(
String query) → Future< List< double> > - Embed query text.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited