Skip to content

Commit

Permalink
refactor: Always await or explicitly discard Futures (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmigloz committed Aug 9, 2023
1 parent 4f4350b commit 989e93d
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 57 deletions.
4 changes: 4 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ linter:
- deprecated_consistency
# - diagnostic_describe_all_properties # Disabled because it's very verbose
- directives_ordering
- discarded_futures
- empty_catches
- empty_constructor_bodies
- empty_statements
Expand All @@ -87,12 +88,14 @@ linter:
- library_prefixes
- library_private_types_in_public_api
- list_remove_unrelated_type
- matching_super_parameters
- missing_whitespace_between_adjacent_strings
- no_adjacent_strings_in_list
- no_default_cases
- no_duplicate_case_values
- no_leading_underscores_for_library_prefixes
- no_leading_underscores_for_local_identifiers
- no_literal_bool_comparisons
- no_logic_in_create_state
- no_runtimeType_toString
- non_constant_identifier_names
Expand Down Expand Up @@ -151,6 +154,7 @@ linter:
- type_annotate_public_apis
- type_init_formals
- type_literal_in_constant_pattern
- unawaited_futures
- unnecessary_await_in_return
- unnecessary_brace_in_string_interps
- unnecessary_breaks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ class PopUpScreenCubit extends Cubit<PopUpScreenState> {
emit(state.copyWith(openAiKey: openAIKey));
}

void onSaveOpenAiKeyPressed() {
Future<void> onSaveOpenAiKeyPressed() async {
final openAIKey = state.openAiKey;
if (openAIKey == null) {
return;
}

settingsRepository.saveOpenAiKey(openAIKey);
await settingsRepository.saveOpenAiKey(openAIKey);
emit(state.copyWith(status: PopUpScreenStatus.idle));
}

Expand Down
27 changes: 14 additions & 13 deletions packages/langchain/lib/src/documents/vector_stores/memory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,23 @@ class MemoryVectorStore extends VectorStore {
/// - [texts] is a list of texts to add to the vector store.
/// - [metadatas] is a list of metadata to add to the vector store.
/// - [embeddings] is the embeddings model to use to embed the texts.
factory MemoryVectorStore.fromText({
static Future<MemoryVectorStore> fromText({
required final List<String> texts,
required final List<Map<String, dynamic>> metadatas,
required final Embeddings embeddings,
}) {
return MemoryVectorStore(embeddings: embeddings)
..addDocuments(
documents: texts
.mapIndexed(
(final i, final text) => Document(
pageContent: text,
metadata: i < metadatas.length ? metadatas[i] : const {},
),
)
.toList(growable: false),
);
}) async {
final vs = MemoryVectorStore(embeddings: embeddings);
await vs.addDocuments(
documents: texts
.mapIndexed(
(final i, final text) => Document(
pageContent: text,
metadata: i < metadatas.length ? metadatas[i] : const {},
),
)
.toList(growable: false),
);
return vs;
}

/// Creates a vector store from a list of documents.
Expand Down
4 changes: 2 additions & 2 deletions packages/langchain/test/agents/tools/base_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:test/test.dart';

void main() {
group('BaseTool tests', () {
test('StructuredTool.fromFunction', () {
test('StructuredTool.fromFunction', () async {
final echoTool = BaseTool.fromFunction(
name: 'echo-int',
description: 'echo-int',
Expand All @@ -17,7 +17,7 @@ void main() {
expect(echoTool.run({'input': 1}), '1');
});

test('Tool.fromFunction', () {
test('Tool.fromFunction', () async {
final echoTool = Tool.fromFunction(
name: 'echo',
description: 'echo',
Expand Down
8 changes: 4 additions & 4 deletions packages/langchain/test/chains/retrieval_qa_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ void main() {
const Document(pageContent: "what's this"),
];
final embeddings = _FakeEmbeddings();
final vectorStore = MemoryVectorStore(embeddings: embeddings)
..addDocuments(documents: documents);
final vectorStore = MemoryVectorStore(embeddings: embeddings);
await vectorStore.addDocuments(documents: documents);
final retriever = VectorStoreRetriever(vectorStore: vectorStore);

const model = FakeEchoLLM();
Expand Down Expand Up @@ -47,8 +47,8 @@ void main() {
const Document(pageContent: "what's this"),
];
final embeddings = _FakeEmbeddings();
final vectorStore = MemoryVectorStore(embeddings: embeddings)
..addDocuments(documents: documents);
final vectorStore = MemoryVectorStore(embeddings: embeddings);
await vectorStore.addDocuments(documents: documents);
final retriever = VectorStoreRetriever(vectorStore: vectorStore);

const llm = FakeEchoLLM();
Expand Down
30 changes: 15 additions & 15 deletions packages/langchain/test/documents/vector_stores/memory_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ void main() {
group('MemoryVectorStore tests', () {
test('Test MemoryVectorStore search', () async {
final embeddings = _FakeEmbeddings(vectors: [_chaoVector]);
final store = MemoryVectorStore(embeddings: embeddings)
..addVectors(
vectors: [
_helloVector,
_hiVector,
_byeVector,
_whatsThisVector,
],
documents: [
const Document(pageContent: 'hello'),
const Document(pageContent: 'hi'),
const Document(pageContent: 'bye'),
const Document(pageContent: "what's this"),
],
);
final store = MemoryVectorStore(embeddings: embeddings);
await store.addVectors(
vectors: [
_helloVector,
_hiVector,
_byeVector,
_whatsThisVector,
],
documents: [
const Document(pageContent: 'hello'),
const Document(pageContent: 'hi'),
const Document(pageContent: 'bye'),
const Document(pageContent: "what's this"),
],
);

final results = await store.similaritySearch(query: 'chao', k: 1);

Expand Down
2 changes: 1 addition & 1 deletion packages/langchain/test/memory/buffer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void main() {
final result1 = await memory.loadMemoryVariables();
expect(result1, {BaseMemory.defaultMemoryKey: expectedString});

memory.clear();
await memory.clear();
final result2 = await memory.loadMemoryVariables();
expect(result2, {BaseMemory.defaultMemoryKey: ''});
});
Expand Down
2 changes: 1 addition & 1 deletion packages/langchain/test/memory/buffer_window_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void main() {
final result1 = await memory.loadMemoryVariables();
expect(result1, {BaseMemory.defaultMemoryKey: expectedString});

memory.clear();
await memory.clear();
final result2 = await memory.loadMemoryVariables();
expect(result2, {BaseMemory.defaultMemoryKey: ''});
});
Expand Down
4 changes: 2 additions & 2 deletions packages/langchain/test/memory/simple_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ void main() {
};
const memory = SimpleMemory(memories: memories);
expect(await memory.loadMemoryVariables(), memories);
memory.saveContext(
await memory.saveContext(
inputValues: {
'foo': 'bar2',
'bar': 'foo2',
Expand All @@ -20,7 +20,7 @@ void main() {
},
);
expect(await memory.loadMemoryVariables(), memories);
memory.clear();
await memory.clear();
expect(await memory.loadMemoryVariables(), memories);
});
});
Expand Down
25 changes: 11 additions & 14 deletions packages/langchain/test/memory/stores/message/in_memory_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ void main() {
test('Test addMessage and getMessages', () async {
final history = ChatMessageHistory();
final message = ChatMessage.human('This is a test');
history.addChatMessage(message);
await history.addChatMessage(message);
expect(await history.getChatMessages(), [message]);
});

test('Test addUserMessage', () async {
final history = ChatMessageHistory()
..addHumanChatMessage('This is a human msg');
final history = ChatMessageHistory();
await history.addHumanChatMessage('This is a human msg');
final messages = await history.getChatMessages();
expect(messages.first, isA<HumanChatMessage>());
expect(messages.first.content, 'This is a human msg');
});

test('Test addAIChatMessage', () async {
final history = ChatMessageHistory()
..addAIChatMessage('This is an AI msg');
final history = ChatMessageHistory();
await history.addAIChatMessage('This is an AI msg');
final messages = await history.getChatMessages();
expect(messages.first, isA<AIChatMessage>());
expect(messages.first.content, 'This is an AI msg');
Expand All @@ -31,9 +31,8 @@ void main() {
final history = ChatMessageHistory();
final message = ChatMessage.human('This is a test');
final message2 = ChatMessage.ai('This is an AI msg');
history
..addChatMessage(message)
..addChatMessage(message2);
await history.addChatMessage(message);
await history.addChatMessage(message2);
final oldestMessage = await history.removeLast();
expect(oldestMessage, isA<AIChatMessage>());
expect(oldestMessage.content, 'This is an AI msg');
Expand All @@ -47,9 +46,8 @@ void main() {
final history = ChatMessageHistory();
final message = ChatMessage.human('This is a test');
final message2 = ChatMessage.ai('This is an AI msg');
history
..addChatMessage(message)
..addChatMessage(message2);
await history.addChatMessage(message);
await history.addChatMessage(message2);
final oldestMessage = await history.removeFirst();
expect(oldestMessage, isA<HumanChatMessage>());
expect(oldestMessage.content, 'This is a test');
Expand All @@ -62,9 +60,8 @@ void main() {
test('Test clear', () async {
final history = ChatMessageHistory();
final message = ChatMessage.human('This is a test');
history
..addChatMessage(message)
..clear();
await history.addChatMessage(message);
await history.clear();
expect(await history.getChatMessages(), <ChatMessage>[]);
});
});
Expand Down
2 changes: 1 addition & 1 deletion packages/langchain/test/memory/token_buffer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void main() {
final result1 = await memory.loadMemoryVariables();
expect(result1, {BaseMemory.defaultMemoryKey: expectedString});

memory.clear();
await memory.clear();
final result2 = await memory.loadMemoryVariables();
expect(result2, {BaseMemory.defaultMemoryKey: ''});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import 'package:langchain_openai/langchain_openai.dart';

void main() async {
// Uncomment the example you want to run:
_example1();
// _example2();
await _example1();
// await _example2();
}

/// The most basic building block of LangChain is calling an LLM on some input.
Expand Down

0 comments on commit 989e93d

Please sign in to comment.