Skip to content

Commit

Permalink
blazor sample
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Mar 9, 2024
1 parent 4a73dd5 commit fdf1731
Show file tree
Hide file tree
Showing 20 changed files with 462 additions and 6 deletions.
7 changes: 7 additions & 0 deletions Claudia.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{1B4BD6F6
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Claudia.Tests", "tests\Claudia.Tests\Claudia.Tests.csproj", "{B4A34AB7-FD1B-4CC4-9859-603D26D7D0A4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorApp1", "sandbox\BlazorApp1\BlazorApp1.csproj", "{8EEB0F69-132B-4887-959D-25531588FCD2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -33,6 +35,10 @@ Global
{B4A34AB7-FD1B-4CC4-9859-603D26D7D0A4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B4A34AB7-FD1B-4CC4-9859-603D26D7D0A4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B4A34AB7-FD1B-4CC4-9859-603D26D7D0A4}.Release|Any CPU.Build.0 = Release|Any CPU
{8EEB0F69-132B-4887-959D-25531588FCD2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8EEB0F69-132B-4887-959D-25531588FCD2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8EEB0F69-132B-4887-959D-25531588FCD2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8EEB0F69-132B-4887-959D-25531588FCD2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -41,6 +47,7 @@ Global
{DBACF901-2D17-4600-A424-52DE1597EFE8} = {B54A8855-F8F0-4015-80AA-86974E65AC2D}
{7D6C6A91-7B2E-4616-A40F-ACA1A0BDBEB4} = {E61BFC87-2B96-4699-9B69-EE4B008AE0A0}
{B4A34AB7-FD1B-4CC4-9859-603D26D7D0A4} = {1B4BD6F6-8528-4409-BA55-085DA5486D36}
{8EEB0F69-132B-4887-959D-25531588FCD2} = {E61BFC87-2B96-4699-9B69-EE4B008AE0A0}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B7CEBA02-BB0C-4102-AE58-DFD114C3192A}
Expand Down
15 changes: 15 additions & 0 deletions sandbox/BlazorApp1/BlazorApp1.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>48f52524-9f77-4194-bc43-b7e749c8e706</UserSecretsId>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Claudia\Claudia.csproj" />
</ItemGroup>

</Project>
34 changes: 34 additions & 0 deletions sandbox/BlazorApp1/Components/App.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<base href="/" />
<link rel="stylesheet" href="app.css" />
<link rel="stylesheet" href="BlazorApp1.styles.css" />
<link rel="icon" type="image/png" href="favicon.png" />

@*Using latest tailwind?*@
<script src="https://cdn.tailwindcss.com/3.0.18"></script>
<script>
tailwind.config = {
theme: {
extend: {
colors: {
clifford: '#da373d',
}
}
}
}
</script>
<script src="https://cdn.tailwindcss.com/[email protected],[email protected],[email protected],[email protected]"></script>
<HeadOutlet />
</head>

<body class="bg-gray-100 h-screen flex flex-col">
<Routes />
<script src="_framework/blazor.web.js"></script>
</body>

</html>
11 changes: 11 additions & 0 deletions sandbox/BlazorApp1/Components/Layout/MainLayout.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@inherits LayoutComponentBase

<main>
@Body
</main>

<div id="blazor-error-ui">
An unhandled error has occurred.
<a href="" class="reload">Reload</a>
<a class="dismiss">🗙</a>
</div>
96 changes: 96 additions & 0 deletions sandbox/BlazorApp1/Components/Layout/MainLayout.razor.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
.page {
position: relative;
display: flex;
flex-direction: column;
}

main {
flex: 1;
}

.sidebar {
background-image: linear-gradient(180deg, rgb(5, 39, 103) 0%, #3a0647 70%);
}

.top-row {
background-color: #f7f7f7;
border-bottom: 1px solid #d6d5d5;
justify-content: flex-end;
height: 3.5rem;
display: flex;
align-items: center;
}

.top-row ::deep a, .top-row ::deep .btn-link {
white-space: nowrap;
margin-left: 1.5rem;
text-decoration: none;
}

.top-row ::deep a:hover, .top-row ::deep .btn-link:hover {
text-decoration: underline;
}

.top-row ::deep a:first-child {
overflow: hidden;
text-overflow: ellipsis;
}

@media (max-width: 640.98px) {
.top-row {
justify-content: space-between;
}

.top-row ::deep a, .top-row ::deep .btn-link {
margin-left: 0;
}
}

@media (min-width: 641px) {
.page {
flex-direction: row;
}

.sidebar {
width: 250px;
height: 100vh;
position: sticky;
top: 0;
}

.top-row {
position: sticky;
top: 0;
z-index: 1;
}

.top-row.auth ::deep a:first-child {
flex: 1;
text-align: right;
width: 0;
}

.top-row, article {
padding-left: 2rem !important;
padding-right: 1.5rem !important;
}
}

#blazor-error-ui {
background: lightyellow;
bottom: 0;
box-shadow: 0 -1px 2px rgba(0, 0, 0, 0.2);
display: none;
left: 0;
padding: 0.6rem 1.25rem 0.7rem 1.25rem;
position: fixed;
width: 100%;
z-index: 1000;
}

#blazor-error-ui .dismiss {
cursor: pointer;
position: absolute;
right: 0.75rem;
top: 0.5rem;
}
36 changes: 36 additions & 0 deletions sandbox/BlazorApp1/Components/Pages/Error.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
@page "/Error"
@using System.Diagnostics

<PageTitle>Error</PageTitle>

<h1 class="text-danger">Error.</h1>
<h2 class="text-danger">An error occurred while processing your request.</h2>

@if (ShowRequestId)
{
<p>
<strong>Request ID:</strong> <code>@RequestId</code>
</p>
}

<h3>Development Mode</h3>
<p>
Swapping to <strong>Development</strong> environment will display more detailed information about the error that occurred.
</p>
<p>
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
It can result in displaying sensitive information from exceptions to end users.
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
and restarting the app.
</p>

@code{
[CascadingParameter]
private HttpContext? HttpContext { get; set; }

private string? RequestId { get; set; }
private bool ShowRequestId => !string.IsNullOrEmpty(RequestId);

protected override void OnInitialized() =>
RequestId = Activity.Current?.Id ?? HttpContext?.TraceIdentifier;
}
41 changes: 41 additions & 0 deletions sandbox/BlazorApp1/Components/Pages/Home.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
@page "/"
@using Claudia
@rendermode InteractiveServer

<PageTitle>Blazor Claude</PageTitle>

<div class="page">
<div class="w-11/12 md:w-1/2 bg-white shadow-lg rounded-lg p-6 mx-auto mt-6 mb-20">
<h1 class="text-2xl font-bold mb-4 text-center">Blazor Claude</h1>
<div class="border rounded-md p-4 h-96 overflow-y-auto">
@foreach (var item in chatMessages)
{
@if (item.Role == Roles.User)
{
<div class="mb-2">
<span class="font-bold text-blue-500">User:</span> @item.Content[0].Text
</div>
}
else
{
<div class="mb-2">
<span class="font-bold text-green-500">Assistant:</span> @item.Content[0].Text
</div>
}
}
</div>
</div>

Temperature: <input type="range" min="0.0" max="1.0" step=0.1 value=@temperature>

System: <textarea type="text" @bind="systemInput" />

<div class="fixed bottom-0 left-0 right-0 p-4 bg-white shadow-lg">
<div class="w-11/12 md:w-1/2 mx-auto">
<div class="flex">
<input type="text" id="chat-input" class="w-full px-3 py-2 text-gray-700 border rounded-l-md focus:outline-none" @bind="textInput" placeholder=" Enter Message...">
<button class="px-4 text-white bg-blue-500 border-l rounded-r-md hover:bg-blue-600 focus:outline-none" @onclick="SendClick">Send</button>
</div>
</div>
</div>
</div>
60 changes: 60 additions & 0 deletions sandbox/BlazorApp1/Components/Pages/Home.razor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using Claudia;
using Microsoft.AspNetCore.Components;

namespace BlazorApp1.Components.Pages;

public partial class Home
{
[Inject]
public required Anthropic Anthropic { get; init; }

double temperature = 1.0;
string textInput = "";
string systemInput = SystemPrompts.Claude3;
List<Message> chatMessages = new();

bool running = false;

async void SendClick()
{
if (running) return;

running = true;
try
{
chatMessages.Add(new() { Role = Roles.User, Content = textInput });

var stream = Anthropic.Messages.CreateStreamAsync(new()
{
Model = Models.Claude3Opus,
MaxTokens = 1024,
Temperature = temperature,
System = string.IsNullOrWhiteSpace(systemInput) ? null : systemInput,
Messages = chatMessages.ToArray()
});

chatMessages.Add(new Message { Role = Roles.Assistant, Content = "" });

textInput = ""; // clear input.
StateHasChanged();

await foreach (var messageStreamEvent in stream)
{
if (messageStreamEvent is ContentBlockDelta content)
{
var lastMessage = chatMessages[^1];
var newMessage = lastMessage with
{
Content = lastMessage.Content[0].Text + content.Delta.Text
};
chatMessages[^1] = newMessage;
StateHasChanged();
}
}
}
finally
{
running = false;
}
}
}
6 changes: 6 additions & 0 deletions sandbox/BlazorApp1/Components/Routes.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(Layout.MainLayout)" />
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
</Found>
</Router>
10 changes: 10 additions & 0 deletions sandbox/BlazorApp1/Components/_Imports.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@using System.Net.Http
@using System.Net.Http.Json
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using static Microsoft.AspNetCore.Components.Web.RenderMode
@using Microsoft.AspNetCore.Components.Web.Virtualization
@using Microsoft.JSInterop
@using BlazorApp1
@using BlazorApp1.Components
29 changes: 29 additions & 0 deletions sandbox/BlazorApp1/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using BlazorApp1.Components;
using Claudia;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();

// get ANTHROPIC_API_KEY from user secret
// https://learn.microsoft.com/en-us/aspnet/core/security/app-secrets
Environment.SetEnvironmentVariable("ANTHROPIC_API_KEY", builder.Configuration["ANTHROPIC_API_KEY"]);

// Add Anthropic Client
builder.Services.AddScoped<Anthropic>();

var app = builder.Build();

if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
}

app.UseStaticFiles();
app.UseAntiforgery();

app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode();

app.Run();
14 changes: 14 additions & 0 deletions sandbox/BlazorApp1/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:5276",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Loading

0 comments on commit fdf1731

Please sign in to comment.