-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support Together AI in OpenAIEmbeddings wrapper (#304)
- Loading branch information
1 parent
e0a3651
commit ddc761d
Showing
8 changed files
with
152 additions
and
8 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
25 changes: 18 additions & 7 deletions
25
docs/modules/retrieval/text_embedding/integrations/openai.md
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 |
---|---|---|
@@ -1,11 +1,22 @@ | ||
# OpenAI | ||
# OpenAIEmbeddings | ||
|
||
Let's load the OpenAI Embedding class. | ||
You can use the `OpenAIEmbeddings` wrapper to consume OpenAI embedding models. | ||
|
||
```dart | ||
final openaiApiKey = Platform.environment['OPENAI_API_KEY']; | ||
final embeddings = OpenAIEmbeddings(apiKey: openaiApiKey); | ||
const text = 'This is a test document.'; | ||
final res = await embeddings.embedQuery(text); | ||
final res = await embeddings.embedDocuments([text]); | ||
final openAiApiKey = Platform.environment['OPENAI_API_KEY']; | ||
final embeddings = OpenAIEmbeddings(apiKey: openAiApiKey); | ||
// Embedding a document | ||
const doc = Document(pageContent: 'This is a test document.'); | ||
final res1 = await embeddings.embedDocuments([doc]); | ||
print(res1); | ||
// [[-0.003105443, 0.011136302, -0.0040295827, -0.011749065, ...]] | ||
// Embedding a retrieval query | ||
const text = 'This is a test query.'; | ||
final res2 = await embeddings.embedQuery(text); | ||
print(res2); | ||
// [-0.005047946, 0.0050882488, -0.0051957234, -0.019143905, ...] | ||
embeddings.close(); | ||
``` |
30 changes: 30 additions & 0 deletions
30
docs/modules/retrieval/text_embedding/integrations/together_ai.md
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,30 @@ | ||
# Together AI Embeddings | ||
|
||
[Together AI](https://www.together.ai/) offers several leading [embedding models](https://docs.together.ai/docs/embedding-models#embedding-models) through its OpenAI compatible API. | ||
|
||
You can consume Together AI API using the `OpenAIEmbeddings` wrapper in the same way you would use the OpenAI API. | ||
|
||
The only difference is that you need to change the base URL to `https://api.together.xyz/v1`: | ||
|
||
```dart | ||
final togetherAiApiKey = Platform.environment['TOGETHER_AI_API_KEY']; | ||
final embeddings = OpenAIEmbeddings( | ||
apiKey: togetherAiApiKey, | ||
baseUrl: 'https://api.together.xyz/v1', | ||
model: 'togethercomputer/m2-bert-80M-32k-retrieval', | ||
); | ||
// Embedding a document | ||
const doc = Document(pageContent: 'This is a test document.'); | ||
final res1 = await embeddings.embedDocuments([doc]); | ||
print(res1); | ||
// [[-0.038838703, 0.0580902, 0.022614542, 0.0078403875, ...]] | ||
// Embedding a retrieval query | ||
const text = 'This is a test query.'; | ||
final res2 = await embeddings.embedQuery(text); | ||
print(res2); | ||
// [-0.019722218, 0.04656633, -0.0074559706, 0.005712764, ...] | ||
embeddings.close(); | ||
``` |
24 changes: 24 additions & 0 deletions
24
examples/docs_examples/bin/modules/retrieval/text_embedding/integrations/openai.dart
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,24 @@ | ||
// ignore_for_file: avoid_print | ||
import 'dart:io'; | ||
|
||
import 'package:langchain/langchain.dart'; | ||
import 'package:langchain_openai/langchain_openai.dart'; | ||
|
||
void main(final List<String> arguments) async { | ||
final openAiApiKey = Platform.environment['OPENAI_API_KEY']; | ||
final embeddings = OpenAIEmbeddings(apiKey: openAiApiKey); | ||
|
||
// Embedding a document | ||
const doc = Document(pageContent: 'This is a test document.'); | ||
final res1 = await embeddings.embedDocuments([doc]); | ||
print(res1); | ||
// [[-0.003105443, 0.011136302, -0.0040295827, -0.011749065, ...]] | ||
|
||
// Embedding a retrieval query | ||
const text = 'This is a test query.'; | ||
final res2 = await embeddings.embedQuery(text); | ||
print(res2); | ||
// [-0.005047946, 0.0050882488, -0.0051957234, -0.019143905, ...] | ||
|
||
embeddings.close(); | ||
} |
28 changes: 28 additions & 0 deletions
28
examples/docs_examples/bin/modules/retrieval/text_embedding/integrations/together_ai.dart
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,28 @@ | ||
// ignore_for_file: avoid_print | ||
import 'dart:io'; | ||
|
||
import 'package:langchain/langchain.dart'; | ||
import 'package:langchain_openai/langchain_openai.dart'; | ||
|
||
void main(final List<String> arguments) async { | ||
final togetherAiApiKey = Platform.environment['TOGETHER_AI_API_KEY']; | ||
final embeddings = OpenAIEmbeddings( | ||
apiKey: togetherAiApiKey, | ||
baseUrl: 'https://api.together.xyz/v1', | ||
model: 'togethercomputer/m2-bert-80M-32k-retrieval', | ||
); | ||
|
||
// Embedding a document | ||
const doc = Document(pageContent: 'This is a test document.'); | ||
final res1 = await embeddings.embedDocuments([doc]); | ||
print(res1); | ||
// [[-0.038838703, 0.0580902, 0.022614542, 0.0078403875, ...]] | ||
|
||
// Embedding a retrieval query | ||
const text = 'This is a test query.'; | ||
final res2 = await embeddings.embedQuery(text); | ||
print(res2); | ||
// [-0.019722218, 0.04656633, -0.0074559706, 0.005712764, ...] | ||
|
||
embeddings.close(); | ||
} |
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
File renamed without changes.
43 changes: 43 additions & 0 deletions
43
packages/langchain_openai/test/embeddings/together_ai_embeddings_test.dart
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,43 @@ | ||
@TestOn('vm') | ||
library; // Uses dart:io | ||
|
||
import 'dart:io'; | ||
|
||
import 'package:langchain_openai/langchain_openai.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group('Together AI Embeddings tests', () { | ||
late OpenAIEmbeddings embeddings; | ||
|
||
setUp(() async { | ||
embeddings = OpenAIEmbeddings( | ||
apiKey: Platform.environment['TOGETHER_AI_API_KEY'], | ||
baseUrl: 'https://api.together.xyz/v1', | ||
); | ||
}); | ||
|
||
tearDown(() { | ||
embeddings.close(); | ||
}); | ||
|
||
test('Test AI Embeddings models', () async { | ||
final models = [ | ||
'togethercomputer/m2-bert-80M-2k-retrieval', | ||
'togethercomputer/m2-bert-80M-8k-retrieval', | ||
'togethercomputer/m2-bert-80M-32k-retrieval', | ||
'WhereIsAI/UAE-Large-V1', | ||
'BAAI/bge-large-en-v1.5', | ||
'BAAI/bge-base-en-v1.5', | ||
'sentence-transformers/msmarco-bert-base-dot-v5', | ||
'bert-base-uncased', | ||
]; | ||
for (final model in models) { | ||
embeddings.model = model; | ||
final res = await embeddings.embedQuery('Hello world'); | ||
expect(res.length, greaterThan(0)); | ||
await Future<void>.delayed(const Duration(seconds: 1)); // Rate limit | ||
} | ||
}); | ||
}); | ||
} |