Skip to content
Merged
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
Binary file modified docs/static/screenshots/web-live-chat.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class Agent
/// Instruction
/// </summary>
[JsonIgnore]
public string Instruction { get; set; }
public string? Instruction { get; set; }

/// <summary>
/// Templates
Expand Down Expand Up @@ -52,7 +52,7 @@ public class Agent
/// Domain knowledges
/// </summary>
[JsonIgnore]
public string Knowledges { get; set; }
public string? Knowledges { get; set; }

public bool IsPublic { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ namespace BotSharp.Abstraction.Messaging;
public interface IRichMessage
{
string Text { get; set; }

[JsonPropertyName("rich_type")]
string RichType => "text";
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text.Json;

namespace BotSharp.Abstraction.Messaging;
namespace BotSharp.Abstraction.Messaging.JsonConverters;

public class RichContentJsonConverter : JsonConverter<IRichMessage>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text.Json;

namespace BotSharp.Abstraction.Messaging;
namespace BotSharp.Abstraction.Messaging.JsonConverters;

public class TemplateMessageJsonConverter : JsonConverter<ITemplateMessage>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,14 @@ public class RichContent<T> where T : IRichMessage
[JsonPropertyName("messaging_type")]
public string MessagingType => "RESPONSE";
public T Message { get; set; }

public RichContent()
{

}

public RichContent(T message)
{
Message = message;
}
}
2 changes: 2 additions & 0 deletions src/Infrastructure/BotSharp.OpenAPI/BotSharp.OpenAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.16" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

<ItemGroup>
Expand Down
48 changes: 48 additions & 0 deletions src/Infrastructure/BotSharp.OpenAPI/BotSharpOpenApiExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using BotSharp.Abstraction.Messaging.JsonConverters;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Extensions.Configuration;
using Microsoft.IdentityModel.Tokens;

namespace BotSharp.OpenAPI;

public static class BotSharpOpenApiExtensions
{
public static IServiceCollection AddBotSharpOpenAPI(this IServiceCollection services, IConfiguration config)
{
// Add services to the container.
services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new RichContentJsonConverter());
options.JsonSerializerOptions.Converters.Add(new TemplateMessageJsonConverter());
});

// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
services.AddEndpointsApiExplorer();
services.AddSwaggerGen();

services.AddHttpContextAccessor();

// Add bearer authentication
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(o =>
{
o.TokenValidationParameters = new TokenValidationParameters
{
ValidIssuer = config["Jwt:Issuer"],
ValidAudience = config["Jwt:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(config["Jwt:Key"])),
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = false,
ValidateIssuerSigningKey = true
};
});

return services;
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Instructs.Models;
using BotSharp.OpenAPI.ViewModels.Users;
using BotSharp.Abstraction.Messaging;
using BotSharp.Abstraction.Messaging.Models.RichContent;
using System.Text.Json.Serialization;

namespace BotSharp.OpenAPI.ViewModels.Conversations;

public class ChatResponseModel : InstructResult
{
[JsonPropertyName("conversation_id")]
public string ConversationId { get; set; }
public string ConversationId { get; set; } = string.Empty;

public UserViewModel Sender { get; set; }
public UserViewModel Sender { get; set; } = new UserViewModel();

public string Function { get; set; }
public string? Function { get; set; }

/// <summary>
/// Planner instruction
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public FunctionCallFromLlm Instruction { get; set; }
public FunctionCallFromLlm? Instruction { get; set; }

/// <summary>
/// Rich message for UI rendering
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("rich_content")]
public object? RichContent { get; set; }
public RichContent<IRichMessage>? RichContent { get; set; }

[JsonPropertyName("created_at")]
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BotSharp.Abstraction.Messaging;
using BotSharp.Abstraction.Messaging.JsonConverters;
using BotSharp.Abstraction.Messaging.Models.RichContent;
using Microsoft.AspNetCore.SignalR;

Expand All @@ -9,43 +10,56 @@ public class ChatHubConversationHook : ConversationHookBase
private readonly IServiceProvider _services;
private readonly IHubContext<SignalRHub> _chatHub;
private readonly IUserIdentity _user;

private readonly JsonSerializerOptions _serializerOptions;
public ChatHubConversationHook(IServiceProvider services,
IHubContext<SignalRHub> chatHub,
IUserIdentity user)
{
_services = services;
_chatHub = chatHub;
_user = user;

_serializerOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Converters =
{
new RichContentJsonConverter(),
new TemplateMessageJsonConverter(),
}
};
}

public override async Task OnUserAgentConnectedInitially(Conversation conversation)
{
var agentService = _services.GetService<IAgentService>();
var agentService = _services.GetRequiredService<IAgentService>();
var agent = await agentService.LoadAgent(conversation.AgentId);

// Check if the Welcome template exists.
var welcomeTemplate = agent.Templates.FirstOrDefault(x => x.Name == "welcome");
var welcomeTemplate = agent.Templates?.FirstOrDefault(x => x.Name == "welcome");
if (welcomeTemplate != null)
{
var richContentService = _services.GetRequiredService<IRichContentService>();
var messages = richContentService.ConvertToMessages(welcomeTemplate.Content);

foreach (var message in messages)
{
await Task.Delay(300);

await _chatHub.Clients.User(_user.Id).SendAsync("OnMessageReceivedFromAssistant", new ChatResponseModel()
var json = JsonSerializer.Serialize(new ChatResponseModel()
{
ConversationId = conversation.Id,
Text = message.Text,
RichContent = new RichContent<IRichMessage>(message),
Sender = new UserViewModel()
{
FirstName = "AI",
LastName = "Assistant",
Role = AgentRole.Assistant
}
});
}, _serializerOptions);

await Task.Delay(300);

await _chatHub.Clients.User(_user.Id).SendAsync("OnMessageReceivedFromAssistant", json);
}
}

Expand Down Expand Up @@ -87,18 +101,20 @@ public override async Task OnResponseGenerated(RoleDialogModel message)
{
var conv = _services.GetRequiredService<IConversationService>();

await _chatHub.Clients.User(_user.Id).SendAsync("OnMessageReceivedFromAssistant", new ChatResponseModel()
var json = JsonSerializer.Serialize(new ChatResponseModel()
{
ConversationId = conv.ConversationId,
MessageId = message.MessageId,
Text = message.Content,
RichContent = message.RichContent,
Sender = new UserViewModel()
{
FirstName = "AI",
LastName = "Assistant",
Role = AgentRole.Assistant
}
});
}, _serializerOptions);
await _chatHub.Clients.User(_user.Id).SendAsync("OnMessageReceivedFromAssistant", json);

await base.OnResponseGenerated(message);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using BotSharp.Abstraction.Agents.Enums;
using BotSharp.Abstraction.Conversations.Enums;
using BotSharp.Abstraction.Messaging.JsonConverters;
using BotSharp.Abstraction.Messaging.Models.RichContent;
using BotSharp.Abstraction.Utilities;
using System.Text.Json.Serialization.Metadata;

namespace BotSharp.Plugin.MetaMessenger.Services;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<LangVersion>$(LangVersion)</LangVersion>
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<GenerateDocumentationFile>$(GenerateDocumentationFile)</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace BotSharp.Plugin.RoutingSpeeder.Providers.Models;

public class DialoguePredictionModel
{
public int Id { get; set; }
public string Text { get; set; }
public string Text { get; set; } = string.Empty;
public string? Label { get; set; }
public string? Prediction { get; set; }
}
40 changes: 2 additions & 38 deletions src/WebStarter/Program.cs
Original file line number Diff line number Diff line change
@@ -1,53 +1,17 @@
using BotSharp.Abstraction.Messaging;
using BotSharp.Abstraction.Users;
using BotSharp.Core;
using BotSharp.OpenAPI;
using BotSharp.Core.Users.Services;
using BotSharp.Logger;
using BotSharp.Plugin.ChatHub;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using System.Text;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new RichContentJsonConverter());
options.JsonSerializerOptions.Converters.Add(new TemplateMessageJsonConverter());
});

// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddHttpContextAccessor();

// Add bearer authentication
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(o =>
{
o.TokenValidationParameters = new TokenValidationParameters
{
ValidIssuer = builder.Configuration["Jwt:Issuer"],
ValidAudience = builder.Configuration["Jwt:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"])),
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = false,
ValidateIssuerSigningKey = true
};
});

builder.Services.AddScoped<IUserIdentity, UserIdentity>();

// Add BotSharp
builder.Services.AddBotSharpCore(builder.Configuration);
builder.Services.AddBotSharpOpenAPI(builder.Configuration);
builder.Services.AddBotSharpLogger(builder.Configuration);

builder.Services.AddCors(options =>
Expand Down
2 changes: 0 additions & 2 deletions src/WebStarter/WebStarter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@

<ItemGroup>
<PackageReference Include="LLamaSharp.Backend.Cuda11" Version="0.6.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.16" />
<PackageReference Include="SciSharp.TensorFlow.Redist" Version="2.11.4" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

<ItemGroup Condition="$(SolutionName)==BotSharp">
Expand Down
2 changes: 1 addition & 1 deletion src/WebStarter/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"MaxRecursiveDepth": 3,
"LlmConfig": {
"Provider": "azure-openai",
"Model": "one"
"Model": "gpt-35-turbo"
}
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,5 @@
"createdDateTime": "2023-08-18T14:39:32.2349685Z",
"updatedDateTime": "2023-08-18T14:39:32.2349686Z",
"id": "01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a",
"isPublic": true,
"llmConfig": {
"provider": "azure-openai",
"model": "one"
}
"isPublic": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
"text": "Hello, I'm an AI assistant that help you do variety of tasks."
},
{
"text": "How can I help you today?"
}
"rich_type": "quick_reply",
"text": "How can I help you today?",
"quick_replies": [
{
"title":"Order a pizza",
"payload":"order a pizza"
},
{
"title":"Who are you?",
"payload":"who are you?"
}
]
}
]
Loading