-
-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Add langchain_ollama example (#661)
- Loading branch information
1 parent
0be8aae
commit 0bba6cb
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
packages/langchain_ollama/example/langchain_ollama_example.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,42 @@ | ||
// ignore_for_file: avoid_print, unused_element | ||
import 'dart:io'; | ||
|
||
import 'package:langchain/langchain.dart'; | ||
import 'package:langchain_ollama/langchain_ollama.dart'; | ||
|
||
void main() async { | ||
// Uncomment the example you want to run: | ||
await _example1(); | ||
// await _example2(); | ||
} | ||
|
||
/// The most basic building block of LangChain is calling an LLM on some input. | ||
Future<void> _example1() async { | ||
final llm = Ollama( | ||
defaultOptions: const OllamaOptions(model: 'llama3.2'), | ||
); | ||
final LLMResult res = await llm.invoke( | ||
PromptValue.string('Tell me a joke'), | ||
); | ||
print(res); | ||
llm.close(); | ||
} | ||
|
||
/// The most frequent use case is to create a chat-bot. | ||
/// This is the most basic one. | ||
Future<void> _example2() async { | ||
final chatModel = ChatOllama( | ||
defaultOptions: const ChatOllamaOptions(model: 'llama3.2'), | ||
); | ||
|
||
try { | ||
while (true) { | ||
stdout.write('> '); | ||
final usrMsg = ChatMessage.humanText(stdin.readLineSync() ?? ''); | ||
final aiMsg = await chatModel([usrMsg]); | ||
print(aiMsg.content); | ||
} | ||
} finally { | ||
chatModel.close(); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -29,4 +29,5 @@ dependencies: | |
uuid: ^4.5.1 | ||
|
||
dev_dependencies: | ||
langchain: ^0.7.7+2 | ||
test: ^1.25.8 |