Skip to content

Commit

Permalink
feat: add results store (xbotter#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
xbotter committed Feb 21, 2024
2 parents b57d68e + 5a2820a commit fd97c3a
Show file tree
Hide file tree
Showing 8 changed files with 447 additions and 137 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace PromptPlayground.Migrations
{
/// <inheritdoc />
public partial class GenerationResultStore : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "GenerationResultStores",
columns: table => new
{
Id = table.Column<long>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
FunctionPath = table.Column<string>(type: "TEXT", nullable: false),
Text = table.Column<string>(type: "TEXT", nullable: false),
RenderedPrompt = table.Column<string>(type: "TEXT", nullable: false),
CreatedAt = table.Column<DateTime>(type: "TEXT", nullable: false),
Elapsed = table.Column<TimeSpan>(type: "TEXT", nullable: false),
Usage = table.Column<string>(type: "TEXT", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_GenerationResultStores", x => x.Id);
});
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "GenerationResultStores");
}
}
}
80 changes: 80 additions & 0 deletions PromptPlayground/Migrations/DbStoreModelSnapshot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using PromptPlayground.Services;

#nullable disable

namespace PromptPlayground.Migrations
{
[DbContext(typeof(DbStore))]
partial class DbStoreModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "8.0.2");

modelBuilder.Entity("PromptPlayground.Services.Models.GenerationResultStore", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<DateTime>("CreatedAt")
.HasColumnType("TEXT");
b.Property<TimeSpan>("Elapsed")
.HasColumnType("TEXT");
b.Property<string>("FunctionPath")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("RenderedPrompt")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Text")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("GenerationResultStores");
});

modelBuilder.Entity("PromptPlayground.Services.Models.GenerationResultStore", b =>
{
b.OwnsOne("PromptPlayground.Services.ResultTokenUsage", "Usage", b1 =>
{
b1.Property<long>("GenerationResultStoreId")
.HasColumnType("INTEGER");
b1.Property<int>("Completion")
.HasColumnType("INTEGER");
b1.Property<int>("Prompt")
.HasColumnType("INTEGER");
b1.Property<int>("Total")
.HasColumnType("INTEGER");
b1.HasKey("GenerationResultStoreId");
b1.ToTable("GenerationResultStores");
b1.ToJson("Usage");
b1.WithOwner()
.HasForeignKey("GenerationResultStoreId");
});
b.Navigation("Usage");
});
#pragma warning restore 612, 618
}
}
}
3 changes: 2 additions & 1 deletion PromptPlayground/PromptPlayground.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@
<PackageReference Include="Humanizer" Version="2.14.1" />
<!--<PackageReference Include="LLamaSharp.Backend.Cpu" Version="0.10.0" />-->
<PackageReference Include="LLamaSharp.semantic-kernel" Version="0.10.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.2" />
<PackageReference Include="Microsoft.SemanticKernel.Plugins.Core" Version="1.4.0-alpha" />
<PackageReference Include="Projektanker.Icons.Avalonia.MaterialDesign" Version="9.0.1" />
<PackageReference Include="Semi.Avalonia" Version="11.0.7" />

<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.0.9" />
<PackageReference Include="ERNIE-Bot.SemanticKernel" Version="0.14.1" />
<PackageReference Include="MessageBox.Avalonia" Version="3.1.5.1" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.4.0" />
Expand Down
67 changes: 67 additions & 0 deletions PromptPlayground/Services/DbStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using PromptPlayground.Services.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PromptPlayground.Services
{
internal class DbStore : DbContext
{
static string defaultConnectionString;
public static DbStore NewScoped
{
get
{
var options = new DbContextOptionsBuilder<DbStore>()
.UseSqlite(defaultConnectionString)
.Options;
return new DbStore(options);
}
}

static DbStore()
{
var path = new ProfileService<DbStore>("store.db").ProfilePath();
defaultConnectionString = $"Data Source={path};Mode=ReadWriteCreate;Cache=Shared";

var db = NewScoped;
db.Database.Migrate();
}

public DbStore(DbContextOptions<DbStore> options) : base(options)
{

}

public DbSet<GenerationResultStore> GenerationResultStores { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.Entity<GenerationResultStore>(
entity =>
{
entity.OwnsOne(_ => _.Usage, builder =>
{
builder.ToJson();
});
});
}
}

internal class DbStoreContextFactory : IDesignTimeDbContextFactory<DbStore>
{
public DbStore CreateDbContext(string[] args)
{
var options = new DbContextOptionsBuilder<DbStore>()
.UseSqlite("Data Source=design.db")
.Options;
return new DbStore(options);
}
}
}
19 changes: 19 additions & 0 deletions PromptPlayground/Services/Models/GenerationResultStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PromptPlayground.Services.Models
{
internal class GenerationResultStore
{
public long Id { get; set; }
public required string FunctionPath { get; set; }
public required string Text { get; set; }
public required string RenderedPrompt { get; set; }
public ResultTokenUsage? Usage { get; set; }
public DateTime CreatedAt { get; set; }
public TimeSpan Elapsed { get; set; }
}
}
Loading

0 comments on commit fd97c3a

Please sign in to comment.