Skip to content

Commit

Permalink
feat: Support Anyscale in ChatOpenAI and OpenAIEmbeddings wrappers (#305
Browse files Browse the repository at this point in the history
)
  • Loading branch information
davidmigloz committed Jan 20, 2024
1 parent ddc761d commit 7daa3eb
Show file tree
Hide file tree
Showing 9 changed files with 376 additions and 208 deletions.
199 changes: 0 additions & 199 deletions README.md

This file was deleted.

1 change: 1 addition & 0 deletions README.md
2 changes: 2 additions & 0 deletions docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
- [Mistral AI](/modules/model_io/models/chat_models/integrations/mistralai.md)
- [OpenRouter](/modules/model_io/models/chat_models/integrations/open_router.md)
- [Together AI](/modules/model_io/models/chat_models/integrations/together_ai.md)
- [Anyscale](/modules/model_io/models/chat_models/integrations/anyscale.md)
- [Prem App](/modules/model_io/models/chat_models/integrations/prem.md)
- [Output parsers](/modules/model_io/output_parsers/output_parsers.md)
- [String output parser](/modules/model_io/output_parsers/string.md)
Expand All @@ -75,6 +76,7 @@
- [Ollama](/modules/retrieval/text_embedding/integrations/ollama.md)
- [Mistral AI](/modules/retrieval/text_embedding/integrations/mistralai.md)
- [Together AI](/modules/retrieval/text_embedding/integrations/together_ai.md)
- [Anyscale](/modules/retrieval/text_embedding/integrations/anyscale.md)
- [Prem App](/modules/retrieval/text_embedding/integrations/prem.md)
- [Vector stores](/modules/retrieval/vector_stores/vector_stores.md)
- Integrations
Expand Down
84 changes: 84 additions & 0 deletions docs/modules/model_io/models/chat_models/integrations/anyscale.md
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 docs/modules/retrieval/text_embedding/integrations/anyscale.md
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();
```
Loading

0 comments on commit 7daa3eb

Please sign in to comment.