Skip to content

Instantly share code, notes, and snippets.

@davidmigloz
Last active January 4, 2024 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidmigloz/2fb55eeea4ff20f0e1fc46a6bc43e764 to your computer and use it in GitHub Desktop.
Save davidmigloz/2fb55eeea4ff20f0e1fc46a6bc43e764 to your computer and use it in GitHub Desktop.
LangChain.dart streaming
import 'dart:io';
import 'package:langchain/langchain.dart';
import 'package:langchain_openai/langchain_openai.dart';
import 'package:langchain_ollama/langchain_ollama.dart';
Future<void> option1() async {
final openaiApiKey = Platform.environment['OPENAI_API_KEY'];
final chatModel = ChatOpenAI(apiKey: openaiApiKey);
final messages = [
ChatMessage.system(
'You are a helpful assistant that replies only with numbers '
'in order without any spaces or commas',
),
ChatMessage.humanText('List the numbers from 1 to 9'),
];
final stream = chatModel.stream(PromptValue.chat(messages));
await for (final ChatResult res in stream) {
print(res.firstOutputAsString);
}
// 123
// 456
// 789
}
Future<void> option2() async {
final openaiApiKey = Platform.environment['OPENAI_API_KEY'];
final chatModel = ChatOpenAI(apiKey: openaiApiKey);
final chain = chatModel.pipe(StringOutputParser());
final messages = [
ChatMessage.system(
'You are a helpful assistant that replies only with numbers '
'in order without any spaces or commas',
),
ChatMessage.humanText('List the numbers from 1 to 9'),
];
final stream = chain.stream(PromptValue.chat(messages));
await for (final String res in stream) {
print(res);
}
// 123
// 456
// 789
}
Future<void> ollama() async {
final chatModel = ChatOllama(
defaultOptions: ChatOllamaOptions(
model: 'llama2:latest',
),
);
final messages = [
ChatMessage.system(
'You are a helpful assistant that replies only with numbers '
'in order without any spaces or commas',
),
ChatMessage.humanText('List the numbers from 1 to 9'),
];
final stream = chatModel.stream(PromptValue.chat(messages));
await for (final ChatResult res in stream) {
print(res.firstOutputAsString);
}
// 1
// 2
// 3
// 4
// 5
// 6
// 7
// 8
// 9
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment