From 8e9495d3500921ef54f1b3733fe2f13663e505be Mon Sep 17 00:00:00 2001 From: David Miguel Date: Thu, 27 Feb 2025 18:12:56 +0100 Subject: [PATCH] docs: Add langchain_ollama example --- .../example/langchain_ollama_example.dart | 42 +++++++++++++++++++ packages/langchain_ollama/pubspec.yaml | 1 + 2 files changed, 43 insertions(+) create mode 100644 packages/langchain_ollama/example/langchain_ollama_example.dart diff --git a/packages/langchain_ollama/example/langchain_ollama_example.dart b/packages/langchain_ollama/example/langchain_ollama_example.dart new file mode 100644 index 00000000..8616a728 --- /dev/null +++ b/packages/langchain_ollama/example/langchain_ollama_example.dart @@ -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 _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 _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(); + } +} diff --git a/packages/langchain_ollama/pubspec.yaml b/packages/langchain_ollama/pubspec.yaml index 1ba6b667..e52a0fb9 100644 --- a/packages/langchain_ollama/pubspec.yaml +++ b/packages/langchain_ollama/pubspec.yaml @@ -29,4 +29,5 @@ dependencies: uuid: ^4.5.1 dev_dependencies: + langchain: ^0.7.7+2 test: ^1.25.8