Skip to content

Provided session based tokens. #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 53 additions & 5 deletions blazor/SyncfusionAISamples/Components/Service/AzureAIService.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
using Microsoft.Extensions.AI;
using Syncfusion.Blazor.SmartComponents;
using System.Collections.Generic;
using System.Threading.Tasks;
using SyncfusionAISamples.Service;
using Syncfusion.Blazor.Charts.Chart.Internal;
using Azure.AI.OpenAI;
using System.ClientModel;
using Microsoft.JSInterop;

namespace SyncfusionAISamples.Service
{
public class AzureAIService
{
private OpenAIConfiguration _openAIConfiguration;
//private readonly OpenAIConfiguration _openAIConfiguration;
private readonly UserTokenService _userTokenService;
private ChatParameters chatParameters_history = new ChatParameters();
private IChatClient _azureAIClient;

public AzureAIService(OpenAIConfiguration openAIConfiguration)
public AzureAIService(UserTokenService userTokenService)
{
_openAIConfiguration = openAIConfiguration;
string apiKey = "your-api-key";
string deploymentName = "your-deployment-name";
// your-azure-endpoint-url
string endpoint = "";
//_openAIConfiguration = openAIConfiguration;
_userTokenService = userTokenService;
_azureAIClient = new AzureOpenAIClient(new Uri(endpoint), new ApiKeyCredential(apiKey)).AsChatClient(deploymentName);
}


/// <summary>
/// Gets a text completion from the Azure OpenAI service.
/// </summary>
Expand All @@ -23,6 +39,14 @@ public AzureAIService(OpenAIConfiguration openAIConfiguration)
/// <returns>The AI-generated completion as a string.</returns>
public async Task<string> GetCompletionAsync(string prompt, bool returnAsJson = true, bool appendPreviousResponse = false, string systemRole = null)
{
string userCode = await _userTokenService.GetUserFingerprintAsync();
int remainingTokens = await _userTokenService.GetRemainingTokensAsync(userCode);

if (remainingTokens <= 0)
{
await _userTokenService.ShowAlert();
return string.Empty;
}
string systemMessage = returnAsJson ? "You are a helpful assistant that only returns and replies with valid, iterable RFC8259 compliant JSON in your responses unless I ask for any other format. Do not provide introductory words such as 'Here is your result' or '```json', etc. in the response" : !string.IsNullOrEmpty(systemRole) ? systemRole : "You are a helpful assistant";
try
{
Expand All @@ -44,18 +68,42 @@ public async Task<string> GetCompletionAsync(string prompt, bool returnAsJson =
new ChatMessage(ChatRole.User,prompt)
};
}
var completion = await _openAIConfiguration.GetChatResponseAsync(chatParameters);
var completion = await GetChatResponseAsync(chatParameters);
if (appendPreviousResponse)
{
chatParameters_history?.Messages?.Add(new ChatMessage(ChatRole.Assistant, completion.ToString()));
}
return completion.ToString();
await _userTokenService.UpdateTokensAsync(userCode, (int)(remainingTokens - completion.Usage.TotalTokenCount));
return completion.Message.Text.ToString();
}
catch (Exception ex)
{
Console.WriteLine($"An exception has occurred: {ex.Message}");
return "";
}
}

public async Task<ChatCompletion> GetChatResponseAsync(ChatParameters options)
{
// Create a completion request with the provided parameters
var completionRequest = new ChatOptions
{
Temperature = options.Temperature ?? 0.5f,
TopP = options.TopP ?? 1.0f,
MaxOutputTokens = options.MaxTokens ?? 2000,
FrequencyPenalty = options.FrequencyPenalty ?? 0.0f,
PresencePenalty = options.PresencePenalty ?? 0.0f,
StopSequences = options.StopSequences
};
try
{
ChatCompletion completion = await _azureAIClient.CompleteAsync(options.Messages, completionRequest);
return completion;
}
catch (Exception ex)
{
throw;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Azure.AI.OpenAI;
using Microsoft.Extensions.AI;
using Microsoft.JSInterop;
using OpenAI;
using Syncfusion.Blazor.SmartComponents;
using SyncfusionAISamples.Service;
using System.ClientModel;

namespace SyncfusionAISamples.Components.Service
{
public class CustomInferenceBackend : IInferenceBackend
{
private IChatClient client;
private readonly UserTokenService _userTokenService;
public CustomInferenceBackend(UserTokenService userTokenService)
{
string apiKey = "your-api-key";
string deploymentName = "your-deployment-name";
// your-azure-endpoint-url
string endpoint = "";
client = new AzureOpenAIClient(new Uri(endpoint), new ApiKeyCredential(apiKey)).AsChatClient(deploymentName);
_userTokenService = userTokenService;
}

public async Task<string> GetChatResponseAsync(ChatParameters options)
{
//string userCode = await _userTokenService.GetUserFingerprintAsync();
//int remainingTokens = await _userTokenService.GetRemainingTokensAsync(userCode);

//if (remainingTokens <= 0)
//{
// await _userTokenService.ShowAlert();
// return string.Empty;
//}
// Create a completion request with the provided parameters
var completionRequest = new ChatOptions
{
Temperature = options.Temperature ?? 0.5f,
TopP = options.TopP ?? 1.0f,
MaxOutputTokens = options.MaxTokens ?? 2000,
FrequencyPenalty = options.FrequencyPenalty ?? 0.0f,
PresencePenalty = options.PresencePenalty ?? 0.0f,
StopSequences = options.StopSequences
};
try
{
ChatCompletion completion = await client.CompleteAsync(options.Messages, completionRequest);
//await _userTokenService.UpdateTokensAsync(userCode, (int)(remainingTokens - completion.Usage.TotalTokenCount));
return completion.Message.Text;
}
catch (Exception ex)
{
throw;
}
}
}
}
102 changes: 102 additions & 0 deletions blazor/SyncfusionAISamples/Components/Service/UserTokenService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.JSInterop;

namespace SyncfusionAISamples.Service
{
public class UserTokenService
{
private readonly IJSRuntime _jsRuntime;
private const string TokenFilePath = "user_tokens.json";
private static readonly TimeZoneInfo IndianStandardTime = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");

public UserTokenService(IJSRuntime jsRuntime)
{
_jsRuntime = jsRuntime;
}

public async Task<string> GetUserFingerprintAsync()
{
return await _jsRuntime.InvokeAsync<string>("fingerPrint");
}

public async Task<int> GetRemainingTokensAsync(string userCode)
{
var tokens = await CheckAndResetTokensAsync(userCode);
return tokens.ContainsKey(userCode) ? tokens[userCode].RemainingTokens : 10000;
}

public async Task UpdateTokensAsync(string userCode, int tokens)
{
var tokenData = await ReadTokensFromFileAsync();
if (tokenData.ContainsKey(userCode))
{
tokenData[userCode].RemainingTokens = tokens;
}
else
{
tokenData[userCode] = new UserTokenInfo
{
UserId = userCode,
DateOfLogin = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, IndianStandardTime),
RemainingTokens = tokens
};
}
await WriteTokensToFileAsync(tokenData);
}

public async Task<Dictionary<string, UserTokenInfo>> CheckAndResetTokensAsync(string userCode)
{
var tokenData = await ReadTokensFromFileAsync();
if (tokenData.ContainsKey(userCode))
{
var userTokenInfo = tokenData[userCode];
var currentTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, IndianStandardTime);
var timeDifference = currentTime - userTokenInfo.DateOfLogin;

if (timeDifference.TotalHours > 1)
{
userTokenInfo.RemainingTokens = 10000; // Reset tokens
userTokenInfo.DateOfLogin = currentTime; // Update login time
await WriteTokensToFileAsync(tokenData);
}
}
return tokenData;
}

private async Task<Dictionary<string, UserTokenInfo>> ReadTokensFromFileAsync()
{
if (!File.Exists(TokenFilePath))
{
var initialData = new Dictionary<string, UserTokenInfo>();
await WriteTokensToFileAsync(initialData);
return initialData;
}

var json = await File.ReadAllTextAsync(TokenFilePath);
return JsonSerializer.Deserialize<Dictionary<string, UserTokenInfo>>(json) ?? new Dictionary<string, UserTokenInfo>();
}

private async Task WriteTokensToFileAsync(Dictionary<string, UserTokenInfo> tokenData)
{
var json = JsonSerializer.Serialize(tokenData, new JsonSerializerOptions { WriteIndented = true });
await File.WriteAllTextAsync(TokenFilePath, json);
}

public async Task ShowAlert()
{
await _jsRuntime.InvokeVoidAsync("showAlert", "You have no remaining tokens.");
}
}

public class UserTokenInfo
{
public string UserId { get; set; }
public DateTime DateOfLogin { get; set; }
public int RemainingTokens { get; set; }
}
}
18 changes: 7 additions & 11 deletions blazor/SyncfusionAISamples/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using FileManagerAI.Services;
using Microsoft.AspNetCore.DataProtection.KeyManagement;
using SmartComponents.LocalEmbeddings;
using Syncfusion.Blazor;
using Syncfusion.Blazor.SmartComponents;
using SyncfusionAISamples.Components;
using SyncfusionAISamples.Components.Service;
using SyncfusionAISamples.Service;

var builder = WebApplication.CreateBuilder(args);
Expand All @@ -23,19 +25,13 @@
// Local Embeddings
builder.Services.AddSingleton<LocalEmbedder>();

// OpenAI Service
string apiKey = "your-api-key";
string deploymentName = "your-deployment-name";
// your-azure-endpoint-url
string endpoint = "";

//Injecting smart components
builder.Services.AddSyncfusionSmartComponents()
.ConfigureCredentials(new AIServiceCredentials(apiKey, deploymentName, endpoint)) //Configuring credentials for AI functionality to work
.InjectOpenAIInference(); // Injecting OpenAI Services

builder.Services.AddSingleton<OpenAIConfiguration>();
builder.Services.AddSingleton<AzureAIService>();
//builder.Services.AddSingleton<OpenAIConfiguration>();
builder.Services.AddScoped<UserTokenService>();
builder.Services.AddScoped<AzureAIService>();
builder.Services.AddScoped<IInferenceBackend, CustomInferenceBackend>();
builder.Services.AddSyncfusionSmartComponents();
#endregion

var app = builder.Build();
Expand Down
1 change: 1 addition & 0 deletions blazor/SyncfusionAISamples/SyncfusionAISamples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.AI.OpenAI" Version="2.1.0" />
<PackageReference Include="Markdig" Version="0.23.*" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.*" />
<PackageReference Include="Syncfusion.Blazor.SmartComponents" Version="*" />
Expand Down
21 changes: 20 additions & 1 deletion blazor/SyncfusionAISamples/wwwroot/scripts/smart-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,4 +435,23 @@ window.preventTabDefault = function (textareaId, dotnetRef) {
});
}

/* Diagram scripts end */
/* Diagram scripts end */

async function fingerPrint() {
try {
// Import FingerprintJS and load the agent
const FingerprintJS = await import('https://openfpcdn.io/fingerprintjs/v4');
const fp = await FingerprintJS.load();

// Get the visitor identifier
const result = await fp.get();
return result.visitorId;
} catch (error) {
console.error(error);
return null;
}
}

function showAlert(message) {
alert(message);
}