-
Notifications
You must be signed in to change notification settings - Fork 276
feat: Add ReasoningBank for reusable reasoning strategies #702
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
Open
nebrass
wants to merge
1
commit into
google:main
Choose a base branch
from
nebrass:feature/reasoning-bank
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,164
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
core/src/main/java/com/google/adk/reasoning/BaseReasoningBankService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /* | ||
| * Copyright 2025 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.adk.reasoning; | ||
|
|
||
| import io.reactivex.rxjava3.core.Completable; | ||
| import io.reactivex.rxjava3.core.Single; | ||
|
|
||
| /** | ||
| * Base contract for reasoning bank services. | ||
| * | ||
| * <p>The service provides functionalities to store and retrieve reasoning strategies that can be | ||
| * used to augment LLM prompts with relevant problem-solving approaches. | ||
| * | ||
| * <p>Based on the ReasoningBank paper (arXiv:2509.25140). | ||
| */ | ||
| public interface BaseReasoningBankService { | ||
|
|
||
| /** | ||
| * Stores a reasoning strategy in the bank. | ||
| * | ||
| * @param appName The name of the application. | ||
| * @param strategy The strategy to store. | ||
| * @return A Completable that completes when the strategy is stored. | ||
| */ | ||
| Completable storeStrategy(String appName, ReasoningStrategy strategy); | ||
|
|
||
| /** | ||
| * Stores a reasoning trace for later distillation into strategies. | ||
| * | ||
| * @param appName The name of the application. | ||
| * @param trace The trace to store. | ||
| * @return A Completable that completes when the trace is stored. | ||
| */ | ||
| Completable storeTrace(String appName, ReasoningTrace trace); | ||
|
|
||
| /** | ||
| * Searches for reasoning strategies that match the given query. | ||
| * | ||
| * @param appName The name of the application. | ||
| * @param query The query to search for (typically a task description). | ||
| * @return A {@link SearchReasoningResponse} containing matching strategies. | ||
| */ | ||
| Single<SearchReasoningResponse> searchStrategies(String appName, String query); | ||
|
|
||
| /** | ||
| * Searches for reasoning strategies that match the given query with a limit. | ||
| * | ||
| * @param appName The name of the application. | ||
| * @param query The query to search for. | ||
| * @param maxResults Maximum number of strategies to return. | ||
| * @return A {@link SearchReasoningResponse} containing matching strategies. | ||
| */ | ||
| Single<SearchReasoningResponse> searchStrategies(String appName, String query, int maxResults); | ||
| } |
180 changes: 180 additions & 0 deletions
180
core/src/main/java/com/google/adk/reasoning/InMemoryReasoningBankService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| /* | ||
| * Copyright 2025 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.adk.reasoning; | ||
|
|
||
| import com.google.common.collect.ImmutableSet; | ||
| import io.reactivex.rxjava3.core.Completable; | ||
| import io.reactivex.rxjava3.core.Single; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * An in-memory reasoning bank service for prototyping purposes only. | ||
| * | ||
| * <p>Uses keyword matching instead of semantic search. For production use, consider implementing a | ||
| * service backed by vector embeddings for semantic similarity matching. | ||
| */ | ||
| public final class InMemoryReasoningBankService implements BaseReasoningBankService { | ||
|
|
||
| private static final int DEFAULT_MAX_RESULTS = 5; | ||
|
|
||
| // Pattern to extract words for keyword matching. | ||
| private static final Pattern WORD_PATTERN = Pattern.compile("[A-Za-z]+"); | ||
|
|
||
| /** Keys are app names, values are lists of strategies. */ | ||
| private final Map<String, List<ReasoningStrategy>> strategies; | ||
|
|
||
| /** Keys are app names, values are lists of traces. */ | ||
| private final Map<String, List<ReasoningTrace>> traces; | ||
|
|
||
| public InMemoryReasoningBankService() { | ||
| this.strategies = new ConcurrentHashMap<>(); | ||
| this.traces = new ConcurrentHashMap<>(); | ||
| } | ||
|
|
||
| @Override | ||
| public Completable storeStrategy(String appName, ReasoningStrategy strategy) { | ||
| return Completable.fromAction( | ||
| () -> { | ||
| List<ReasoningStrategy> appStrategies = | ||
| strategies.computeIfAbsent( | ||
| appName, k -> Collections.synchronizedList(new ArrayList<>())); | ||
| appStrategies.add(strategy); | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public Completable storeTrace(String appName, ReasoningTrace trace) { | ||
| return Completable.fromAction( | ||
| () -> { | ||
| List<ReasoningTrace> appTraces = | ||
| traces.computeIfAbsent(appName, k -> Collections.synchronizedList(new ArrayList<>())); | ||
| appTraces.add(trace); | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public Single<SearchReasoningResponse> searchStrategies(String appName, String query) { | ||
| return searchStrategies(appName, query, DEFAULT_MAX_RESULTS); | ||
| } | ||
|
|
||
| @Override | ||
| public Single<SearchReasoningResponse> searchStrategies( | ||
| String appName, String query, int maxResults) { | ||
| return Single.fromCallable( | ||
| () -> { | ||
| if (!strategies.containsKey(appName)) { | ||
| return SearchReasoningResponse.builder().build(); | ||
| } | ||
|
|
||
| List<ReasoningStrategy> appStrategies = strategies.get(appName); | ||
| ImmutableSet<String> queryWords = extractWords(query); | ||
|
|
||
| if (queryWords.isEmpty()) { | ||
| return SearchReasoningResponse.builder().build(); | ||
| } | ||
|
|
||
| List<ScoredStrategy> scoredStrategies = new ArrayList<>(); | ||
|
|
||
| for (ReasoningStrategy strategy : appStrategies) { | ||
| int score = calculateMatchScore(strategy, queryWords); | ||
| if (score > 0) { | ||
| scoredStrategies.add(new ScoredStrategy(strategy, score)); | ||
| } | ||
| } | ||
|
|
||
| // Sort by score descending | ||
| scoredStrategies.sort((a, b) -> Integer.compare(b.score, a.score)); | ||
|
|
||
| // Take top results | ||
| List<ReasoningStrategy> matchingStrategies = | ||
| scoredStrategies.stream() | ||
| .map(scoredStrategy -> scoredStrategy.strategy) | ||
| .limit(maxResults) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| return SearchReasoningResponse.builder().setStrategies(matchingStrategies).build(); | ||
| }); | ||
| } | ||
|
|
||
| private int calculateMatchScore(ReasoningStrategy strategy, Set<String> queryWords) { | ||
| int score = 0; | ||
|
|
||
| // Check problem pattern | ||
| Set<String> patternWords = extractWords(strategy.problemPattern()); | ||
| score += countOverlap(queryWords, patternWords) * 3; // Weight pattern matches higher | ||
|
|
||
| // Check name | ||
| Set<String> nameWords = extractWords(strategy.name()); | ||
| score += countOverlap(queryWords, nameWords) * 2; | ||
|
|
||
| // Check tags | ||
| for (String tag : strategy.tags()) { | ||
| Set<String> tagWords = extractWords(tag); | ||
| score += countOverlap(queryWords, tagWords); | ||
| } | ||
|
|
||
| // Check steps (lower weight) | ||
| for (String step : strategy.steps()) { | ||
| Set<String> stepWords = extractWords(step); | ||
| if (!Collections.disjoint(queryWords, stepWords)) { | ||
| score += 1; | ||
| } | ||
| } | ||
|
|
||
| return score; | ||
| } | ||
|
|
||
| private int countOverlap(Set<String> set1, Set<String> set2) { | ||
| Set<String> intersection = new HashSet<>(set1); | ||
| intersection.retainAll(set2); | ||
| return intersection.size(); | ||
| } | ||
|
|
||
| private ImmutableSet<String> extractWords(String text) { | ||
| if (text == null || text.isEmpty()) { | ||
| return ImmutableSet.of(); | ||
| } | ||
|
|
||
| Set<String> words = new HashSet<>(); | ||
| Matcher matcher = WORD_PATTERN.matcher(text); | ||
| while (matcher.find()) { | ||
| words.add(matcher.group().toLowerCase(Locale.ROOT)); | ||
| } | ||
| return ImmutableSet.copyOf(words); | ||
| } | ||
|
|
||
| /** Helper class for scoring strategies during search. */ | ||
| private static class ScoredStrategy { | ||
| final ReasoningStrategy strategy; | ||
| final int score; | ||
|
|
||
| ScoredStrategy(ReasoningStrategy strategy, int score) { | ||
| this.strategy = strategy; | ||
| this.score = score; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.