Skip to content

Commit

Permalink
feat: Add copyWith method to all RunnableOptions subclasses (#531)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmigloz authored Aug 21, 2024
1 parent ec82c76 commit 42c8d48
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 7 deletions.
13 changes: 13 additions & 0 deletions packages/langchain_core/lib/src/chat_models/fake.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:collection/collection.dart';

import '../../language_models.dart';
import '../prompts/types.dart';
import '../tools/base.dart';
import 'base.dart';
import 'types.dart';

Expand Down Expand Up @@ -85,6 +86,8 @@ class FakeChatModelOptions extends ChatModelOptions {
const FakeChatModelOptions({
super.model,
this.metadata,
super.tools,
super.toolChoice,
super.concurrencyLimit,
});

Expand All @@ -95,11 +98,15 @@ class FakeChatModelOptions extends ChatModelOptions {
FakeChatModelOptions copyWith({
final String? model,
final Map<String, dynamic>? metadata,
final List<ToolSpec>? tools,
final ChatToolChoice? toolChoice,
final int? concurrencyLimit,
}) {
return FakeChatModelOptions(
model: model ?? this.model,
metadata: metadata ?? this.metadata,
tools: tools ?? this.tools,
toolChoice: toolChoice ?? this.toolChoice,
concurrencyLimit: concurrencyLimit ?? this.concurrencyLimit,
);
}
Expand Down Expand Up @@ -223,6 +230,8 @@ class FakeEchoChatModelOptions extends ChatModelOptions {
super.model,
this.metadata,
this.throwRandomError = false,
super.tools,
super.toolChoice,
super.concurrencyLimit,
});

Expand All @@ -237,12 +246,16 @@ class FakeEchoChatModelOptions extends ChatModelOptions {
final String? model,
final Map<String, dynamic>? metadata,
final bool? throwRandomError,
final List<ToolSpec>? tools,
final ChatToolChoice? toolChoice,
final int? concurrencyLimit,
}) {
return FakeEchoChatModelOptions(
model: model ?? this.model,
metadata: metadata ?? this.metadata,
throwRandomError: throwRandomError ?? this.throwRandomError,
tools: tools ?? this.tools,
toolChoice: toolChoice ?? this.toolChoice,
concurrencyLimit: concurrencyLimit ?? this.concurrencyLimit,
);
}
Expand Down
8 changes: 8 additions & 0 deletions packages/langchain_core/lib/src/chat_models/types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ abstract class ChatModelOptions extends LanguageModelOptions {

/// Controls which (if any) tool is called by the model.
final ChatToolChoice? toolChoice;

@override
ChatModelOptions copyWith({
final String? model,
final List<ToolSpec>? tools,
final ChatToolChoice? toolChoice,
final int? concurrencyLimit,
});
}

/// {@template chat_result}
Expand Down
6 changes: 6 additions & 0 deletions packages/langchain_core/lib/src/language_models/types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ abstract class LanguageModelOptions extends BaseLangChainOptions {
/// ID of the language model to use.
/// Check the provider's documentation for available models.
final String? model;

@override
LanguageModelOptions copyWith({
final String? model,
final int? concurrencyLimit,
});
}

/// {@template language_model}
Expand Down
34 changes: 28 additions & 6 deletions packages/langchain_core/lib/src/llms/fake.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import 'types.dart';
/// Fake LLM for testing.
/// You can pass in a list of responses to return in order when called.
/// {@endtemplate}
class FakeLLM extends SimpleLLM {
class FakeLLM extends SimpleLLM<FakeLLMOptions> {
/// {@macro fake_list_llm}
FakeLLM({
required this.responses,
}) : super(defaultOptions: const LLMOptions());
}) : super(defaultOptions: const FakeLLMOptions());

/// Responses to return in order when called.
final List<String> responses;
Expand Down Expand Up @@ -60,13 +60,35 @@ class FakeLLM extends SimpleLLM {
}
}

/// {@template fake_llm_options}
/// Fake LLM options for testing.
/// {@endtemplate}
class FakeLLMOptions extends LLMOptions {
/// {@macro fake_llm_options}
const FakeLLMOptions({
super.model,
super.concurrencyLimit,
});

@override
FakeLLMOptions copyWith({
final String? model,
final int? concurrencyLimit,
}) {
return FakeLLMOptions(
model: model ?? this.model,
concurrencyLimit: concurrencyLimit ?? this.concurrencyLimit,
);
}
}

/// {@template fake_echo_llm}
/// Fake LLM for testing.
/// It just returns the prompt or streams it char by char.
/// {@endtemplate}
class FakeEchoLLM extends BaseLLM {
class FakeEchoLLM extends BaseLLM<FakeLLMOptions> {
/// {@macro fake_echo_llm}
const FakeEchoLLM() : super(defaultOptions: const LLMOptions());
const FakeEchoLLM() : super(defaultOptions: const FakeLLMOptions());

@override
String get modelType => 'fake-echo';
Expand Down Expand Up @@ -122,11 +144,11 @@ class FakeEchoLLM extends BaseLLM {
/// Fake LLM for testing.
/// It returns the string returned by the [handler] function.
/// {@endtemplate}
class FakeHandlerLLM extends SimpleLLM {
class FakeHandlerLLM extends SimpleLLM<FakeLLMOptions> {
/// {@macro fake_handler_llm}
FakeHandlerLLM({
required this.handler,
}) : super(defaultOptions: const LLMOptions());
}) : super(defaultOptions: const FakeLLMOptions());

/// Function called to generate the response.
final String Function(
Expand Down
2 changes: 1 addition & 1 deletion packages/langchain_core/lib/src/llms/types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import '../language_models/types.dart';
/// Options to pass into the LLM.
/// {@endtemplate}
@immutable
class LLMOptions extends LanguageModelOptions {
abstract class LLMOptions extends LanguageModelOptions {
/// {@macro llm_options}
const LLMOptions({
super.model,
Expand Down
12 changes: 12 additions & 0 deletions packages/langchain_core/lib/src/retrievers/types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,22 @@ class VectorStoreRetrieverOptions extends RetrieverOptions {
/// {@macro vector_store_retriever_options}
const VectorStoreRetrieverOptions({
this.searchType = const VectorStoreSimilaritySearch(),
super.concurrencyLimit,
});

/// The type of search to perform, either:
/// - [VectorStoreSearchType.similarity] (default)
/// - [VectorStoreSearchType.mmr]
final VectorStoreSearchType searchType;

@override
VectorStoreRetrieverOptions copyWith({
final VectorStoreSearchType? searchType,
final int? concurrencyLimit,
}) {
return VectorStoreRetrieverOptions(
searchType: searchType ?? this.searchType,
concurrencyLimit: concurrencyLimit ?? this.concurrencyLimit,
);
}
}
11 changes: 11 additions & 0 deletions packages/langchain_core/test/runnables/binding_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:langchain_core/language_models.dart';
import 'package:langchain_core/output_parsers.dart';
import 'package:langchain_core/prompts.dart';
import 'package:langchain_core/runnables.dart';
import 'package:langchain_core/tools.dart';
import 'package:test/test.dart';

void main() {
Expand Down Expand Up @@ -123,4 +124,14 @@ class _FakeOptionsChatModelOptions extends ChatModelOptions {
const _FakeOptionsChatModelOptions(this.stop);

final String stop;

@override
ChatModelOptions copyWith({
final String? model,
final List<ToolSpec>? tools,
final ChatToolChoice? toolChoice,
final int? concurrencyLimit,
}) {
return _FakeOptionsChatModelOptions(stop);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:collection/collection.dart';
import 'package:langchain_core/chat_models.dart';
import 'package:langchain_core/tools.dart';
import 'package:meta/meta.dart';

/// {@template chat_vertex_ai_options}
Expand Down Expand Up @@ -108,6 +109,8 @@ class ChatVertexAIOptions extends ChatModelOptions {
final List<String>? stopSequences,
final int? candidateCount,
final List<ChatExample>? examples,
final List<ToolSpec>? tools,
final ChatToolChoice? toolChoice,
final int? concurrencyLimit,
}) {
return ChatVertexAIOptions(
Expand Down
3 changes: 3 additions & 0 deletions packages/langchain_mistralai/lib/src/chat_models/types.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:langchain_core/chat_models.dart';
import 'package:langchain_core/tools.dart';
import 'package:meta/meta.dart';

/// {@template chat_mistral_ai_options}
Expand Down Expand Up @@ -54,6 +55,8 @@ class ChatMistralAIOptions extends ChatModelOptions {
final int? maxTokens,
final bool? safePrompt,
final int? randomSeed,
final List<ToolSpec>? tools,
final ChatToolChoice? toolChoice,
final int? concurrencyLimit,
}) {
return ChatMistralAIOptions(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:collection/collection.dart';
import 'package:langchain_core/chat_models.dart';
import 'package:langchain_core/tools.dart';
import 'package:meta/meta.dart';

import '../../../langchain_ollama.dart';
Expand Down Expand Up @@ -247,6 +248,8 @@ class ChatOllamaOptions extends ChatModelOptions {
final bool? useMmap,
final bool? useMlock,
final int? numThread,
final List<ToolSpec>? tools,
final ChatToolChoice? toolChoice,
final int? concurrencyLimit,
}) {
return ChatOllamaOptions(
Expand Down Expand Up @@ -283,6 +286,8 @@ class ChatOllamaOptions extends ChatModelOptions {
useMmap: useMmap ?? this.useMmap,
useMlock: useMlock ?? this.useMlock,
numThread: numThread ?? this.numThread,
tools: tools ?? this.tools,
toolChoice: toolChoice ?? this.toolChoice,
concurrencyLimit: concurrencyLimit ?? this.concurrencyLimit,
);
}
Expand Down

0 comments on commit 42c8d48

Please sign in to comment.