Last active
January 4, 2024 23:29
-
-
Save davidmigloz/2fb55eeea4ff20f0e1fc46a6bc43e764 to your computer and use it in GitHub Desktop.
LangChain.dart streaming
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
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