-
-
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(chat-models): Support Together AI API in ChatOpenAI wrapper (#297)
- Loading branch information
1 parent
ca6f23d
commit 28ab56a
Showing
12 changed files
with
297 additions
and
25 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
2 changes: 1 addition & 1 deletion
2
docs/modules/model_io/models/chat_models/integrations/open_router.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/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,84 @@ | ||
# Together AI | ||
|
||
[Together AI](https://www.together.ai) offers a unified OpenAI-compatible API for a broad range of [models](https://api.together.xyz/playground) 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 Together AI 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.together.xyz/v1`: | ||
|
||
```dart | ||
final chatModel = ChatOpenAI( | ||
apiKey: togetherAiApiKey, | ||
baseUrl: 'https://api.together.xyz/v1', | ||
defaultOptions: const ChatOpenAIOptions( | ||
model: 'mistralai/Mistral-7B-Instruct-v0.2', | ||
), | ||
); | ||
``` | ||
|
||
## Invoke | ||
|
||
```dart | ||
final togetherAiApiKey = Platform.environment['TOGETHER_AI_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: togetherAiApiKey, | ||
baseUrl: 'https://api.together.xyz/v1', | ||
defaultOptions: const ChatOpenAIOptions( | ||
model: 'mistralai/Mistral-7B-Instruct-v0.2', | ||
), | ||
); | ||
final chain = promptTemplate | chatModel | const StringOutputParser(); | ||
final res = await chain.invoke({ | ||
'input_language': 'English', | ||
'output_language': 'French', | ||
'text': 'I love programming.', | ||
}); | ||
print(res); | ||
// -> 'J'aime programmer' | ||
``` | ||
|
||
## Stream | ||
|
||
```dart | ||
final togetherAiApiKey = Platform.environment['TOGETHER_AI_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: togetherAiApiKey, | ||
baseUrl: 'https://api.together.xyz/v1', | ||
defaultOptions: const ChatOpenAIOptions( | ||
model: 'mistralai/Mistral-7B-Instruct-v0.2', | ||
), | ||
); | ||
final chain = promptTemplate.pipe(chatModel).pipe(const StringOutputParser()); | ||
final stream = chain.stream({'max_num': '9'}); | ||
await stream.forEach(print); | ||
// 1 | ||
// 2 | ||
// 3 | ||
// ... | ||
// 9 | ||
``` |
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
71 changes: 71 additions & 0 deletions
71
examples/docs_examples/bin/modules/model_io/models/chat_models/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,71 @@ | ||
// 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 { | ||
await _togetherAiInvoke(); | ||
await _togetherAiStreaming(); | ||
} | ||
|
||
Future<void> _togetherAiInvoke() async { | ||
final togetherAiApiKey = Platform.environment['TOGETHER_AI_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: togetherAiApiKey, | ||
baseUrl: 'https://api.together.xyz/v1', | ||
defaultOptions: const ChatOpenAIOptions( | ||
model: 'mistralai/Mistral-7B-Instruct-v0.2', | ||
), | ||
); | ||
|
||
final chain = promptTemplate | chatModel | const StringOutputParser(); | ||
|
||
final res = await chain.invoke({ | ||
'input_language': 'English', | ||
'output_language': 'French', | ||
'text': 'I love programming.', | ||
}); | ||
print(res); | ||
// -> 'J'aime programmer' | ||
} | ||
|
||
Future<void> _togetherAiStreaming() async { | ||
final togetherAiApiKey = Platform.environment['TOGETHER_AI_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: togetherAiApiKey, | ||
baseUrl: 'https://api.together.xyz/v1', | ||
defaultOptions: const ChatOpenAIOptions( | ||
model: 'mistralai/Mistral-7B-Instruct-v0.2', | ||
), | ||
); | ||
|
||
final chain = promptTemplate.pipe(chatModel).pipe(const StringOutputParser()); | ||
|
||
final stream = chain.stream({'max_num': '9'}); | ||
await stream.forEach(print); | ||
// 1 | ||
// 2 | ||
// 3 | ||
// ... | ||
// 9 | ||
} |
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
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
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
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
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
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
Oops, something went wrong.