Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Add langchain_ollama example #661

Merged
merged 1 commit into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading