Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(chat-models): Support Together AI API in ChatOpenAI wrapper #297

Merged
merged 1 commit into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
- [Ollama](/modules/model_io/models/chat_models/integrations/ollama.md)
- [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)
- [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 Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# OpenRouter

[OpenRouter](https://openrouter.ai/) offers a unified OpenAI-compatible API for a broad range of models.
[OpenRouter](https://openrouter.ai/) offers a unified OpenAI-compatible API for a broad range of [models](https://openrouter.ai/models).

You can also let users pay for their own models via their [OAuth PKCE](https://openrouter.ai/docs#oauth) flow.

Expand Down
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
```
6 changes: 3 additions & 3 deletions examples/browser_summarizer/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,14 @@ packages:
path: "../../packages/langchain"
relative: true
source: path
version: "0.3.1+1"
version: "0.3.2"
langchain_openai:
dependency: "direct main"
description:
path: "../../packages/langchain_openai"
relative: true
source: path
version: "0.3.1+1"
version: "0.3.2"
langchain_tiktoken:
dependency: transitive
description:
Expand Down Expand Up @@ -287,7 +287,7 @@ packages:
path: "../../packages/openai_dart"
relative: true
source: path
version: "0.1.3"
version: "0.1.4"
path:
dependency: transitive
description:
Expand Down
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
}
20 changes: 10 additions & 10 deletions examples/docs_examples/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ packages:
path: "../../packages/googleai_dart"
relative: true
source: path
version: "0.0.2"
version: "0.0.2+1"
googleapis:
dependency: transitive
description:
Expand Down Expand Up @@ -205,42 +205,42 @@ packages:
path: "../../packages/langchain"
relative: true
source: path
version: "0.3.1+1"
version: "0.3.2"
langchain_chroma:
dependency: "direct main"
description:
path: "../../packages/langchain_chroma"
relative: true
source: path
version: "0.1.0+11"
version: "0.1.0+12"
langchain_google:
dependency: "direct main"
description:
path: "../../packages/langchain_google"
relative: true
source: path
version: "0.2.2+1"
version: "0.2.3"
langchain_mistralai:
dependency: "direct main"
description:
path: "../../packages/langchain_mistralai"
relative: true
source: path
version: "0.0.1+4"
version: "0.0.2"
langchain_ollama:
dependency: "direct main"
description:
path: "../../packages/langchain_ollama"
relative: true
source: path
version: "0.0.2+1"
version: "0.0.3"
langchain_openai:
dependency: "direct main"
description:
path: "../../packages/langchain_openai"
relative: true
source: path
version: "0.3.1+1"
version: "0.3.2"
langchain_tiktoken:
dependency: transitive
description:
Expand Down Expand Up @@ -279,7 +279,7 @@ packages:
path: "../../packages/mistralai_dart"
relative: true
source: path
version: "0.0.2"
version: "0.0.2+1"
ollama_dart:
dependency: "direct overridden"
description:
Expand All @@ -293,7 +293,7 @@ packages:
path: "../../packages/openai_dart"
relative: true
source: path
version: "0.1.3"
version: "0.1.4"
path:
dependency: transitive
description:
Expand Down Expand Up @@ -388,7 +388,7 @@ packages:
path: "../../packages/vertex_ai"
relative: true
source: path
version: "0.0.7+2"
version: "0.0.8"
web:
dependency: transitive
description:
Expand Down
6 changes: 3 additions & 3 deletions examples/hello_world_backend/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,14 @@ packages:
path: "../../packages/langchain"
relative: true
source: path
version: "0.3.1+1"
version: "0.3.2"
langchain_openai:
dependency: "direct main"
description:
path: "../../packages/langchain_openai"
relative: true
source: path
version: "0.3.1+1"
version: "0.3.2"
langchain_tiktoken:
dependency: transitive
description:
Expand Down Expand Up @@ -205,7 +205,7 @@ packages:
path: "../../packages/openai_dart"
relative: true
source: path
version: "0.1.3"
version: "0.1.4"
path:
dependency: transitive
description:
Expand Down
6 changes: 3 additions & 3 deletions examples/hello_world_cli/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,14 @@ packages:
path: "../../packages/langchain"
relative: true
source: path
version: "0.3.1+1"
version: "0.3.2"
langchain_openai:
dependency: "direct main"
description:
path: "../../packages/langchain_openai"
relative: true
source: path
version: "0.3.1+1"
version: "0.3.2"
langchain_tiktoken:
dependency: transitive
description:
Expand Down Expand Up @@ -197,7 +197,7 @@ packages:
path: "../../packages/openai_dart"
relative: true
source: path
version: "0.1.3"
version: "0.1.4"
path:
dependency: transitive
description:
Expand Down
6 changes: 3 additions & 3 deletions examples/hello_world_flutter/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,14 @@ packages:
path: "../../packages/langchain"
relative: true
source: path
version: "0.3.1+1"
version: "0.3.2"
langchain_openai:
dependency: "direct main"
description:
path: "../../packages/langchain_openai"
relative: true
source: path
version: "0.3.1+1"
version: "0.3.2"
langchain_tiktoken:
dependency: transitive
description:
Expand Down Expand Up @@ -242,7 +242,7 @@ packages:
path: "../../packages/openai_dart"
relative: true
source: path
version: "0.1.3"
version: "0.1.4"
path:
dependency: transitive
description:
Expand Down
2 changes: 1 addition & 1 deletion examples/vertex_ai_matching_engine_setup/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ packages:
path: "../../packages/vertex_ai"
relative: true
source: path
version: "0.0.7+2"
version: "0.0.8"
web:
dependency: transitive
description:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import 'models/models.dart';
/// - [Completions API docs](https://platform.openai.com/docs/api-reference/chat)
///
/// You can also use this wrapper to consume OpenAI-compatible APIs like
/// [OpenRouter](https://openrouter.ai) or [One API](https://github.com/songquanpeng/one-api).
/// [OpenRouter](https://openrouter.ai), [Together AI](https://www.together.ai),
/// [One API](https://github.com/songquanpeng/one-api), etc.
///
/// ### Call options
///
Expand Down
Loading