-
Notifications
You must be signed in to change notification settings - Fork 2
refactor(llm): separate identity and provider resolution #350
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
11 changes: 11 additions & 0 deletions
11
TelegramSearchBot.LLM/Interface/AI/LLM/IBotIdentityProvider.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,11 @@ | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace TelegramSearchBot.Interface.AI.LLM { | ||
| public interface IBotIdentityProvider { | ||
| Task<BotIdentity> GetIdentityAsync(CancellationToken cancellationToken = default); | ||
| void SetIdentity(long botUserId, string botName); | ||
| } | ||
|
|
||
| public sealed record BotIdentity(long UserId, string UserName); | ||
| } |
11 changes: 11 additions & 0 deletions
11
TelegramSearchBot.LLM/Interface/AI/LLM/IGroupLlmSettingsService.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,11 @@ | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| #nullable enable | ||
|
|
||
| namespace TelegramSearchBot.Interface.AI.LLM { | ||
| public interface IGroupLlmSettingsService { | ||
| Task<string?> GetModelAsync(long chatId, CancellationToken cancellationToken = default); | ||
| Task<(string Previous, string Current)> SetModelAsync(long chatId, string modelName, CancellationToken cancellationToken = default); | ||
| } | ||
| } |
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
29 changes: 29 additions & 0 deletions
29
TelegramSearchBot.LLM/Service/AI/LLM/BotIdentityProvider.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,29 @@ | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using TelegramSearchBot.Attributes; | ||
| using TelegramSearchBot.Common; | ||
| using TelegramSearchBot.Interface; | ||
| using TelegramSearchBot.Interface.AI.LLM; | ||
|
|
||
| namespace TelegramSearchBot.Service.AI.LLM { | ||
| [Injectable(ServiceLifetime.Singleton)] | ||
| public class BotIdentityProvider : IService, IBotIdentityProvider { | ||
| private readonly object _lock = new(); | ||
| private BotIdentity _identity = new(0, string.Empty); | ||
|
|
||
| public string ServiceName => nameof(BotIdentityProvider); | ||
|
|
||
| public Task<BotIdentity> GetIdentityAsync(CancellationToken cancellationToken = default) { | ||
| lock (_lock) { | ||
| return Task.FromResult(_identity); | ||
| } | ||
| } | ||
|
|
||
| public void SetIdentity(long botUserId, string botName) { | ||
| lock (_lock) { | ||
| _identity = new BotIdentity(botUserId, botName ?? string.Empty); | ||
| Env.BotId = botUserId; | ||
| } | ||
| } | ||
| } | ||
| } |
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
71 changes: 71 additions & 0 deletions
71
TelegramSearchBot.LLM/Service/AI/LLM/GroupLlmSettingsService.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,71 @@ | ||
| using System; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.EntityFrameworkCore; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using TelegramSearchBot.Attributes; | ||
| using TelegramSearchBot.Interface; | ||
| using TelegramSearchBot.Interface.AI.LLM; | ||
| using TelegramSearchBot.Model; | ||
| using TelegramSearchBot.Model.Data; | ||
|
|
||
| #nullable enable | ||
|
|
||
| namespace TelegramSearchBot.Service.AI.LLM { | ||
| [Injectable(ServiceLifetime.Scoped)] | ||
| public class GroupLlmSettingsService : IService, IGroupLlmSettingsService { | ||
| private readonly DataDbContext _dbContext; | ||
|
|
||
| public GroupLlmSettingsService(DataDbContext dbContext) { | ||
| _dbContext = dbContext; | ||
| } | ||
|
|
||
| public string ServiceName => nameof(GroupLlmSettingsService); | ||
|
|
||
| public async Task<string?> GetModelAsync(long chatId, CancellationToken cancellationToken = default) { | ||
| var settings = await _dbContext.GroupSettings.AsNoTracking() | ||
| .FirstOrDefaultAsync(s => s.GroupId == chatId, cancellationToken); | ||
| return settings == null ? null : settings.LLMModelName; | ||
| } | ||
|
|
||
| public async Task<(string Previous, string Current)> SetModelAsync(long chatId, string modelName, CancellationToken cancellationToken = default) { | ||
| var normalizedModelName = modelName?.Trim(); | ||
| if (string.IsNullOrWhiteSpace(normalizedModelName)) { | ||
| throw new ArgumentException("Model name cannot be empty.", nameof(modelName)); | ||
| } | ||
|
|
||
| modelName = normalizedModelName; | ||
| var settings = await _dbContext.GroupSettings | ||
| .FirstOrDefaultAsync(s => s.GroupId == chatId, cancellationToken); | ||
| var previous = settings == null ? null : settings.LLMModelName; | ||
| GroupSettings? newSettings = null; | ||
|
|
||
| if (settings is null) { | ||
| newSettings = new GroupSettings { | ||
| GroupId = chatId, | ||
| LLMModelName = modelName | ||
| }; | ||
| await _dbContext.GroupSettings.AddAsync(newSettings, cancellationToken); | ||
| } else { | ||
| settings.LLMModelName = modelName; | ||
| } | ||
|
|
||
| try { | ||
| await _dbContext.SaveChangesAsync(cancellationToken); | ||
| } catch (DbUpdateException) when (newSettings != null) { | ||
| _dbContext.Entry(newSettings).State = EntityState.Detached; | ||
| settings = await _dbContext.GroupSettings | ||
| .FirstOrDefaultAsync(s => s.GroupId == chatId, cancellationToken); | ||
| if (settings is null) { | ||
| throw; | ||
| } | ||
|
|
||
| previous = settings.LLMModelName; | ||
| settings.LLMModelName = modelName; | ||
| await _dbContext.SaveChangesAsync(cancellationToken); | ||
| } | ||
|
|
||
| return (previous ?? "Default", modelName); | ||
| } | ||
| } | ||
| } | ||
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.