-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add project OpenAI.ChatGpt.EntityFrameworkCore; Add PersistentChatMes…
…sage class
- Loading branch information
Showing
12 changed files
with
227 additions
and
50 deletions.
There are no files selected for viewing
This file contains 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,14 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using OpenAI.ChatGpt.Models; | ||
|
||
namespace OpenAI.ChatGpt.EntityFrameworkCore; | ||
|
||
public class ChatGptDbContext : DbContext | ||
{ | ||
public DbSet<Topic> Topics => Set<Topic>(); | ||
public DbSet<PersistentChatMessage> Messages => Set<PersistentChatMessage>(); | ||
|
||
public ChatGptDbContext(DbContextOptions<ChatGptDbContext> options) : base(options) | ||
{ | ||
} | ||
} |
This file contains 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,70 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using OpenAI.ChatGpt.Models; | ||
|
||
namespace OpenAI.ChatGpt.EntityFrameworkCore; | ||
|
||
public class EfMessageStore : IMessageStore | ||
{ | ||
private readonly ChatGptDbContext _dbContext; | ||
|
||
public EfMessageStore(ChatGptDbContext dbContext) | ||
{ | ||
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext)); | ||
} | ||
|
||
public async Task<IEnumerable<Topic>> GetTopics(string userId, CancellationToken cancellationToken) | ||
{ | ||
if (userId == null) throw new ArgumentNullException(nameof(userId)); | ||
return await _dbContext.Topics.ToListAsync(cancellationToken: cancellationToken); | ||
} | ||
|
||
public Task<Topic> GetTopic(string userId, Guid topicId, CancellationToken cancellationToken) | ||
{ | ||
if (userId == null) throw new ArgumentNullException(nameof(userId)); | ||
return _dbContext.Topics.FirstAsync( | ||
it => it.Id == topicId && it.UserId == userId, | ||
cancellationToken: cancellationToken); | ||
} | ||
|
||
public async Task AddTopic(Topic topic, CancellationToken cancellationToken) | ||
{ | ||
if (topic == null) throw new ArgumentNullException(nameof(topic)); | ||
await _dbContext.Topics.AddAsync(topic, cancellationToken); | ||
await _dbContext.SaveChangesAsync(cancellationToken); | ||
} | ||
|
||
public async Task SaveMessages( | ||
string userId, | ||
Guid topicId, | ||
IEnumerable<PersistentChatMessage> messages, | ||
CancellationToken cancellationToken) | ||
{ | ||
if (userId == null) throw new ArgumentNullException(nameof(userId)); | ||
if (messages == null) throw new ArgumentNullException(nameof(messages)); | ||
await _dbContext.AddRangeAsync(messages, cancellationToken); | ||
await _dbContext.SaveChangesAsync(cancellationToken); | ||
} | ||
|
||
public async Task<IEnumerable<PersistentChatMessage>> GetMessages( | ||
string userId, Guid topicId, CancellationToken cancellationToken) | ||
{ | ||
if (userId == null) throw new ArgumentNullException(nameof(userId)); | ||
return await _dbContext.Messages | ||
.Where(it => it.TopicId == topicId && it.UserId == userId) | ||
.ToListAsync(cancellationToken: cancellationToken); | ||
} | ||
|
||
public Task<Topic?> GetLastTopicOrNull(string userId, CancellationToken cancellationToken) | ||
{ | ||
if (userId == null) throw new ArgumentNullException(nameof(userId)); | ||
return _dbContext.Topics | ||
.Where(it => it.UserId == userId) | ||
.OrderByDescending(it => it.CreatedAt) | ||
.FirstOrDefaultAsync(cancellationToken: cancellationToken); | ||
} | ||
|
||
public Task EnsureStorageCreated() | ||
{ | ||
return _dbContext.Database.EnsureCreatedAsync(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
OpenAI.ChatGpt.EntityFrameworkCore/OpenAI.ChatGpt.EntityFrameworkCore.csproj
This file contains 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,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<TargetFrameworks>net6.0;net7.0</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.4" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.4"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\OpenAI.ChatGpt\OpenAI.ChatGpt.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains 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 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 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 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 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 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,7 @@ | ||
namespace OpenAI.ChatGpt.Models; | ||
|
||
public class ChatGptCredentials | ||
{ | ||
public string ApiKey { get; set; } | ||
public string? ApiHost { get; set; } | ||
} |
Oops, something went wrong.