-
-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support Anyscale in ChatOpenAI and OpenAIEmbeddings wrappers (#305
- Loading branch information
1 parent
ddc761d
commit 7daa3eb
Showing
9 changed files
with
376 additions
and
208 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
./packages/langchain/README.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
84 changes: 84 additions & 0 deletions
84
docs/modules/model_io/models/chat_models/integrations/anyscale.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,84 @@ | ||
# Anyscale | ||
|
||
[Anyscale](https://www.anyscale.com/) offers a unified OpenAI-compatible API for a broad range of [models](https://docs.endpoints.anyscale.com/guides/models/#chat-models) running serverless or on your own dedicated instances. | ||
|
||
It also allows to fine-tune models on your own data or train new models from scratch. | ||
|
||
You can consume Anyscale API using the `ChatOpenAI` 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.endpoints.anyscale.com/v1`: | ||
|
||
```dart | ||
final chatModel = ChatOpenAI( | ||
apiKey: togetherAiApiKey, | ||
baseUrl: 'https://api.endpoints.anyscale.com/v1', | ||
defaultOptions: const ChatOpenAIOptions( | ||
model: 'meta-llama/Llama-2-70b-chat-hf', | ||
), | ||
); | ||
``` | ||
|
||
## Invoke | ||
|
||
```dart | ||
final anyscaleApiKey = Platform.environment['ANYSCALE_API_KEY']; | ||
final promptTemplate = ChatPromptTemplate.fromTemplates(const [ | ||
( | ||
ChatMessageType.system, | ||
'You are a helpful assistant that translates {input_language} to {output_language}.', | ||
), | ||
(ChatMessageType.human, '{text}'), | ||
]); | ||
final chatModel = ChatOpenAI( | ||
apiKey: anyscaleApiKey, | ||
baseUrl: 'https://api.endpoints.anyscale.com/v1', | ||
defaultOptions: const ChatOpenAIOptions( | ||
model: 'meta-llama/Llama-2-70b-chat-hf', | ||
), | ||
); | ||
final chain = promptTemplate | chatModel | const StringOutputParser(); | ||
final res = await chain.invoke({ | ||
'input_language': 'English', | ||
'output_language': 'French', | ||
'text': 'I love programming.', | ||
}); | ||
print(res); | ||
// -> "I love programming" se traduit en français sous la forme "J'aime passionnément la programmation" | ||
``` | ||
|
||
## Stream | ||
|
||
```dart | ||
final anyscaleApiKey = Platform.environment['ANYSCALE_API_KEY']; | ||
final promptTemplate = ChatPromptTemplate.fromTemplates(const [ | ||
( | ||
ChatMessageType.system, | ||
'You are a helpful assistant that replies only with numbers ' | ||
'in order without any spaces or commas', | ||
), | ||
(ChatMessageType.human, 'List the numbers from 1 to {max_num}'), | ||
]); | ||
final chatModel = ChatOpenAI( | ||
apiKey: anyscaleApiKey, | ||
baseUrl: 'https://api.endpoints.anyscale.com/v1', | ||
defaultOptions: const ChatOpenAIOptions( | ||
model: 'mistralai/Mixtral-8x7B-Instruct-v0.1', | ||
), | ||
); | ||
final chain = promptTemplate.pipe(chatModel).pipe(const StringOutputParser()); | ||
final stream = chain.stream({'max_num': '9'}); | ||
await stream.forEach(print); | ||
// 1 | ||
// 2 | ||
// 3 | ||
// ... | ||
// 9 | ||
``` |
30 changes: 30 additions & 0 deletions
30
docs/modules/retrieval/text_embedding/integrations/anyscale.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 @@ | ||
# Anyscale Embeddings | ||
|
||
[Anyscale](https://www.anyscale.com/) offers several [embedding models](https://docs.endpoints.anyscale.com/guides/models/#embedding-models) through its OpenAI compatible API. | ||
|
||
You can consume Anyscale 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.endpoints.anyscale.com/v1`: | ||
|
||
```dart | ||
final anyscaleApiKey = Platform.environment['ANYSCALE_API_KEY']; | ||
final embeddings = OpenAIEmbeddings( | ||
apiKey: anyscaleApiKey, | ||
baseUrl: 'https://api.endpoints.anyscale.com/v1', | ||
model: 'thenlper/gte-large', | ||
); | ||
// Embedding a document | ||
const doc = Document(pageContent: 'This is a test document.'); | ||
final res1 = await embeddings.embedDocuments([doc]); | ||
print(res1); | ||
// [[-0.0011281073093414307, -0.013280618004500866, 0.02164546772837639, ...]] | ||
// Embedding a retrieval query | ||
const text = 'This is a test query.'; | ||
final res2 = await embeddings.embedQuery(text); | ||
print(res2); | ||
// [-0.027850965037941933, 0.00269310618750751, 0.008118202909827232, ...] | ||
embeddings.close(); | ||
``` |
Oops, something went wrong.