-
-
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.
- Loading branch information
Showing
20 changed files
with
462 additions
and
6 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
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,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> |
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,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> |
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,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> |
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,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; | ||
} |
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,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; | ||
} |
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,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> |
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,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; | ||
} | ||
} | ||
} |
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,6 @@ | ||
<Router AppAssembly="@typeof(Program).Assembly"> | ||
<Found Context="routeData"> | ||
<RouteView RouteData="@routeData" DefaultLayout="@typeof(Layout.MainLayout)" /> | ||
<FocusOnNavigate RouteData="@routeData" Selector="h1" /> | ||
</Found> | ||
</Router> |
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,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 |
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,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(); |
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 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/launchsettings.json", | ||
"profiles": { | ||
"http": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"applicationUrl": "http://localhost:5276", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.