Skip to content

Commit

Permalink
Merge pull request #61 from pavel-zhur/feature/image-traces
Browse files Browse the repository at this point in the history
Feature/image traces
  • Loading branch information
pavel-zhur authored Oct 10, 2024
2 parents 8830880 + 86533e8 commit 1e72ae5
Show file tree
Hide file tree
Showing 9 changed files with 256 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace OneShelf.Common.OpenAi.Models.Memory;

public class ChatBotMemoryPointWithDeserializableTraces : ChatBotMemoryPoint
{
public List<string> ImageTraces { get; init; } = new();

public List<string> ImageUrlTraces { get; init; } = new();
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
namespace OneShelf.Common.OpenAi.Models.Memory;

public class ChatBotMemoryPointWithTraces : ChatBotMemoryPoint
public class ChatBotMemoryPointWithTraces : ChatBotMemoryPointWithDeserializableTraces
{
public List<MemoryPointTrace> Traces { get; init; } = new();

public List<string> ImageTraces { get; init; } = new();

public List<string> ImageUrlTraces { get; init; } = new();
}
7 changes: 7 additions & 0 deletions OneShelf.Common/OneShelf.Common.OpenAi/Models/ValueHolder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace OneShelf.Common.OpenAi.Models;

public class ValueHolder<T>(T value = default)
where T : struct
{
public T Value { get; set; } = value;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public DialogRunner(IOptions<OpenAiOptions> options, ILogger<DialogRunner> logge
}

public async Task<(DialogResult result, ChatBotMemoryPointWithTraces newMessagePoint)> Execute(
IReadOnlyList<MemoryPoint> existingMemory, DialogConfiguration configuration, CancellationToken cancellationToken = default, DateTime? imagesUnavailableUntil = null)
IReadOnlyList<MemoryPoint> existingMemory, DialogConfiguration configuration, CancellationToken cancellationToken = default, DateTime? imagesUnavailableUntil = null, ValueHolder<bool>? isPhoto = null)
{
var lastTopicChange = existingMemory.WithIndices().LastOrDefault(x => x.x is ChatBotMemoryPoint
{
Expand Down Expand Up @@ -89,6 +89,8 @@ public DialogRunner(IOptions<OpenAiOptions> options, ILogger<DialogRunner> logge
if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException();
if (imagesDeserialized.Any() && !imagesUnavailableUntil.HasValue)
{
if (isPhoto != null) isPhoto.Value = true;

urlsTask = GenerateImages(imagesDeserialized, configuration, cancellationToken);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public AiDialogHandler(
_dogContext = dogContext;
}

protected override void OnInitializing(Update update)
protected override void OnInitializing(long userId, long chatId)
{
_dogDatabase.InitializeInteractionsRepositoryScope(_dogContext.DomainId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ public AiDialogHandler(
_options = options;
}

protected override void OnInitializing(Update update)
protected override void OnInitializing(long userId, long chatId)
{
_dragonDatabase.InitializeInteractionsRepositoryScope(update.Message!.From!.Id, update.Message.Chat.Id);
_dragonDatabase.InitializeInteractionsRepositoryScope(userId, chatId);
}

protected override bool TraceImages => _options.Value.IsAdmin(_dragonScope.UserId);
protected override bool TraceImages => true;

protected override IInteraction<InteractionType> CreateInteraction(Update update) => new Interaction
{
ChatId = update.Message!.Chat.Id,
ChatId = update.Message?.Chat.Id ?? update.CallbackQuery!.Message!.Chat.Id,
UpdateId = update.UpdateId,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected override async Task<bool> HandleSync(Update update)
_dragonDatabase.Updates.Add(dbUpdate);
await _dragonDatabase.SaveChangesAsync();

_scope.Initialize(dbUpdate.Id, update.Message?.Chat.Id, update.Message?.From?.Id);
_scope.Initialize(dbUpdate.Id, update.Message?.Chat.Id ?? update.CallbackQuery?.Message?.Chat.Id, update.Message?.From?.Id ?? update.CallbackQuery?.From.Id);
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@
<None Include="..\..\nuget readme.md" Pack="true" Link="readme.md" PackagePath="\readme.md" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Nito.AsyncEx.Coordination" Version="5.1.2" />
</ItemGroup>

</Project>
Loading

0 comments on commit 1e72ae5

Please sign in to comment.