-
Notifications
You must be signed in to change notification settings - Fork 17
[AI] Windows Copilot Runtime (Phi Silica) support #178
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
Draft
mattleibow
wants to merge
8
commits into
main
Choose a base branch
from
mattleibow/ai-windows-phi-silica
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.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
911b100
[AI] Windows Copilot Runtime (Phi Silica) support
mattleibow 7375d09
[AI] Fix PR #178 review comments
mattleibow e4607db
[AI] Add BuildTools.WinApp for MSIX dotnet run, fix MODE_XHARNESS define
mattleibow e29400b
[AI] Fix namespace after EssentialsAI.Sample rename to EssentialsAISa…
mattleibow 544443e
[AI] Skip Windows TFM on non-Windows builds to fix macOS CI
mattleibow b4c0008
Add nuget.org source for WindowsAppSDK.Search transitive dependency
jfversluis 428e9dd
Update Microsoft.Agents.AI packages to 1.5.0 to fix OpenTelemetry vul…
jfversluis f27c320
Bump Extensions packages to satisfy Agents.AI 1.5.0 dependencies
jfversluis 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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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
94 changes: 94 additions & 0 deletions
94
samples/EssentialsAISample/Services/AppContentIndexerSearchService.cs
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,94 @@ | ||
| #if WINDOWS | ||
| using System.Collections.Concurrent; | ||
| using Microsoft.Windows.Search.AppContentIndex; | ||
|
|
||
| namespace EssentialsAISample.Services; | ||
|
|
||
| /// <summary> | ||
| /// Semantic search using the Windows AppContentIndexer API. | ||
| /// The OS handles embedding generation, chunking, and search internally. | ||
| /// Each collection maps to a separate index. | ||
| /// </summary> | ||
| public sealed class AppContentIndexerSearchService : ISemanticSearchService, IDisposable | ||
| { | ||
| const string IndexPrefix = "maui-ai-sample"; | ||
|
|
||
| readonly object _indexersLock = new(); | ||
| readonly Dictionary<string, AppContentIndexer> _indexers = new(); | ||
|
jfversluis marked this conversation as resolved.
|
||
|
|
||
| AppContentIndexer GetOrCreateIndexer(string collection) | ||
| { | ||
| lock (_indexersLock) | ||
| { | ||
| if (_indexers.TryGetValue(collection, out var indexer)) | ||
| return indexer; | ||
|
|
||
| var indexName = $"{IndexPrefix}-{collection}"; | ||
| var result = AppContentIndexer.GetOrCreateIndex(indexName); | ||
| if (!result.Succeeded) | ||
| throw new InvalidOperationException($"Failed to create index '{indexName}': {result.Status}"); | ||
|
|
||
| _indexers[collection] = result.Indexer; | ||
| return result.Indexer; | ||
| } | ||
| } | ||
|
|
||
| public Task IndexAsync(string collection, string id, string text, CancellationToken cancellationToken = default) | ||
| { | ||
| return Task.Run(() => | ||
| { | ||
| var indexer = GetOrCreateIndexer(collection); | ||
| var content = AppManagedIndexableAppContent.CreateFromString(id, text); | ||
| indexer.AddOrUpdate(content); | ||
| }, cancellationToken); | ||
| } | ||
|
|
||
| public async Task<IReadOnlyList<SemanticSearchResult>> SearchAsync(string collection, string query, int maxResults, CancellationToken cancellationToken = default) | ||
| { | ||
| var indexer = GetOrCreateIndexer(collection); | ||
|
|
||
| // Run on background thread — GetNextMatches can block while the indexer processes | ||
| return await Task.Run(() => | ||
| { | ||
| // Request extra matches since multiple regions can match per item | ||
| var textQuery = indexer.CreateTextQuery(query); | ||
| var matches = textQuery.GetNextMatches(maxResults * 4); | ||
|
|
||
| // Group by ContentId, take the best rank (lowest index = highest relevance) | ||
| return matches | ||
| .Select((m, i) => (Id: m.ContentId, Rank: i)) | ||
| .GroupBy(m => m.Id) | ||
| .Select(g => new SemanticSearchResult( | ||
| g.Key, | ||
| // Best rank score + small boost for multiple matches | ||
| (float)(matches.Count - g.Min(m => m.Rank)) / matches.Count + g.Count() * 0.01f)) | ||
| .OrderByDescending(r => r.Score) | ||
| .Take(maxResults) | ||
| .ToList() as IReadOnlyList<SemanticSearchResult>; | ||
| }, cancellationToken); | ||
| } | ||
|
|
||
| public async Task WaitUntilReadyAsync(CancellationToken cancellationToken = default) | ||
| { | ||
| List<AppContentIndexer> snapshot; | ||
| lock (_indexersLock) | ||
| snapshot = [.. _indexers.Values]; | ||
|
|
||
| foreach (var indexer in snapshot) | ||
| await indexer.WaitForIndexingIdleAsync(TimeSpan.FromSeconds(60)); | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| List<AppContentIndexer> snapshot; | ||
| lock (_indexersLock) | ||
| { | ||
| snapshot = [.. _indexers.Values]; | ||
| _indexers.Clear(); | ||
| } | ||
|
|
||
| foreach (var indexer in snapshot) | ||
| indexer.Dispose(); | ||
| } | ||
| } | ||
| #endif | ||
Oops, something went wrong.
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.