Skip to content

Commit

Permalink
docs: Add langchain_ollama example (#661)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmigloz authored Feb 27, 2025
1 parent 0be8aae commit 0bba6cb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
42 changes: 42 additions & 0 deletions packages/langchain_ollama/example/langchain_ollama_example.dart
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();
}
}
1 change: 1 addition & 0 deletions packages/langchain_ollama/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ dependencies:
uuid: ^4.5.1

dev_dependencies:
langchain: ^0.7.7+2
test: ^1.25.8

0 comments on commit 0bba6cb

Please sign in to comment.