chagne default temperature.

This commit is contained in:
Haiping Chen 2024-03-07 12:27:29 -06:00
parent 2ce526dd2e
commit 86b905ca7c
10 changed files with 127 additions and 49 deletions

View file

@ -1,5 +1,3 @@
using BotSharp.Abstraction.Functions.Models;
namespace BotSharp.Abstraction.Conversations; namespace BotSharp.Abstraction.Conversations;
public abstract class ConversationHookBase : IConversationHook public abstract class ConversationHookBase : IConversationHook

View file

@ -17,13 +17,13 @@ public class MessageConfig : TruncateMessageRequest
/// <summary> /// <summary>
/// The sampling temperature to use that controls the apparent creativity of generated completions. /// The sampling temperature to use that controls the apparent creativity of generated completions.
/// </summary> /// </summary>
public float Temperature { get; set; } = 0.5f; public float Temperature { get; set; } = 0f;
/// <summary> /// <summary>
/// An alternative value to Temperature, called nucleus sampling, that causes /// An alternative value to Temperature, called nucleus sampling, that causes
/// the model to consider the results of the tokens with probability mass. /// the model to consider the results of the tokens with probability mass.
/// </summary> /// </summary>
public float SamplingFactor { get; set; } = 0.5f; public float SamplingFactor { get; set; } = 0f;
/// <summary> /// <summary>
/// Conversation states from input /// Conversation states from input

View file

@ -27,6 +27,7 @@ public class TemplateRender : ITemplateRender
_options.MemberAccessStrategy.Register<Agent>(); _options.MemberAccessStrategy.Register<Agent>();
_options.MemberAccessStrategy.Register<RoutableAgent>(); _options.MemberAccessStrategy.Register<RoutableAgent>();
_options.MemberAccessStrategy.Register<RoutingHandlerDef>(); _options.MemberAccessStrategy.Register<RoutingHandlerDef>();
_options.MemberAccessStrategy.Register<UserIdentity>();
} }
public string Render(string template, Dictionary<string, object> dict) public string Render(string template, Dictionary<string, object> dict)

View file

@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using System.Security.Claims; using System.Security.Claims;
using System.Text.Json.Serialization;
namespace BotSharp.Core.Users.Services; namespace BotSharp.Core.Users.Services;
@ -17,12 +18,14 @@ public class UserIdentity : IUserIdentity
public string Id public string Id
=> _claims?.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value!; => _claims?.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value!;
[JsonPropertyName("user_name")]
public string UserName public string UserName
=> _claims?.FirstOrDefault(x => x.Type == ClaimTypes.Name)?.Value!; => _claims?.FirstOrDefault(x => x.Type == ClaimTypes.Name)?.Value!;
public string Email public string Email
=> _claims?.FirstOrDefault(x => x.Type == ClaimTypes.Email)?.Value!; => _claims?.FirstOrDefault(x => x.Type == ClaimTypes.Email)?.Value!;
[JsonPropertyName("first_name")]
public string FirstName public string FirstName
{ {
get get
@ -36,9 +39,21 @@ public class UserIdentity : IUserIdentity
} }
} }
[JsonPropertyName("last_name")]
public string LastName public string LastName
=> _claims?.FirstOrDefault(x => x.Type == ClaimTypes.Surname)?.Value!; => _claims?.FirstOrDefault(x => x.Type == ClaimTypes.Surname)?.Value!;
[JsonPropertyName("full_name")]
public string FullName public string FullName
=> $"{FirstName} {LastName}".Trim(); {
get
{
var fullName = _claims?.FirstOrDefault(x => x.Type == "full_name")?.Value;
if (!string.IsNullOrEmpty(fullName))
{
return fullName;
}
return $"{FirstName} {LastName}".Trim();
}
}
} }

View file

@ -254,8 +254,8 @@ public class ChatCompletionProvider : IChatCompletion
// https://community.openai.com/t/cheat-sheet-mastering-temperature-and-top-p-in-chatgpt-api-a-few-tips-and-tricks-on-controlling-the-creativity-deterministic-output-of-prompt-responses/172683 // https://community.openai.com/t/cheat-sheet-mastering-temperature-and-top-p-in-chatgpt-api-a-few-tips-and-tricks-on-controlling-the-creativity-deterministic-output-of-prompt-responses/172683
var state = _services.GetRequiredService<IConversationStateService>(); var state = _services.GetRequiredService<IConversationStateService>();
var temperature = float.Parse(state.GetState("temperature", "0.5")); var temperature = float.Parse(state.GetState("temperature", "0.0"));
var samplingFactor = float.Parse(state.GetState("sampling_factor", "0.5")); var samplingFactor = float.Parse(state.GetState("sampling_factor", "0.0"));
chatCompletionsOptions.Temperature = temperature; chatCompletionsOptions.Temperature = temperature;
chatCompletionsOptions.NucleusSamplingFactor = samplingFactor; chatCompletionsOptions.NucleusSamplingFactor = samplingFactor;
// chatCompletionsOptions.FrequencyPenalty = 0; // chatCompletionsOptions.FrequencyPenalty = 0;

View file

@ -63,8 +63,8 @@ public class TextCompletionProvider : ITextCompletion
completionsOptions.StopSequences.Add($"{AgentRole.Assistant}:"); completionsOptions.StopSequences.Add($"{AgentRole.Assistant}:");
var state = _services.GetRequiredService<IConversationStateService>(); var state = _services.GetRequiredService<IConversationStateService>();
var temperature = float.Parse(state.GetState("temperature", "0.5")); var temperature = float.Parse(state.GetState("temperature", "0.0"));
var samplingFactor = float.Parse(state.GetState("sampling_factor", "0.5")); var samplingFactor = float.Parse(state.GetState("sampling_factor", "0.0"));
completionsOptions.Temperature = temperature; completionsOptions.Temperature = temperature;
completionsOptions.NucleusSamplingFactor = samplingFactor; completionsOptions.NucleusSamplingFactor = samplingFactor;
completionsOptions.DeploymentName = _model; completionsOptions.DeploymentName = _model;

View file

@ -19,8 +19,9 @@ public class ChatHubPlugin : IBotSharpPlugin
{ {
// Register hooks // Register hooks
services.AddScoped<IConversationHook, ChatHubConversationHook>(); services.AddScoped<IConversationHook, ChatHubConversationHook>();
services.AddScoped<IContentGeneratingHook, StreamingLogHook>();
services.AddScoped<IConversationHook, StreamingLogHook>(); services.AddScoped<IConversationHook, StreamingLogHook>();
services.AddScoped<IConversationHook, WelcomeHook>();
services.AddScoped<IRoutingHook, StreamingLogHook>(); services.AddScoped<IRoutingHook, StreamingLogHook>();
services.AddScoped<IContentGeneratingHook, StreamingLogHook>();
} }
} }

View file

@ -1,7 +1,5 @@
using BotSharp.Abstraction.Messaging;
using BotSharp.Abstraction.Messaging.Enums; using BotSharp.Abstraction.Messaging.Enums;
using BotSharp.Abstraction.Messaging.JsonConverters; using BotSharp.Abstraction.Messaging.JsonConverters;
using BotSharp.Abstraction.Messaging.Models.RichContent;
using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR;
namespace BotSharp.Plugin.ChatHub.Hooks; namespace BotSharp.Plugin.ChatHub.Hooks;
@ -31,42 +29,6 @@ public class ChatHubConversationHook : ConversationHookBase
}; };
} }
public override async Task OnUserAgentConnectedInitially(Conversation conversation)
{
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");
if (welcomeTemplate != null)
{
var richContentService = _services.GetRequiredService<IRichContentService>();
var messages = richContentService.ConvertToMessages(welcomeTemplate.Content);
foreach (var message in messages)
{
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);
}
}
await base.OnUserAgentConnectedInitially(conversation);
}
public override async Task OnConversationInitialized(Conversation conversation) public override async Task OnConversationInitialized(Conversation conversation)
{ {
var userService = _services.GetRequiredService<IUserService>(); var userService = _services.GetRequiredService<IUserService>();

View file

@ -233,7 +233,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", BuildContentLog(input)); await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", BuildContentLog(input));
} }
public async Task OnRoutingInstructionRevised(FunctionCallFromLlm instruct, RoleDialogModel message) public async Task OnRoutingInstructionReceived(FunctionCallFromLlm instruct, RoleDialogModel message)
{ {
var conversationId = _state.GetConversationId(); var conversationId = _state.GetConversationId();
var agent = await _agentService.LoadAgent(message.CurrentAgentId); var agent = await _agentService.LoadAgent(message.CurrentAgentId);
@ -249,6 +249,22 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
}; };
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", BuildContentLog(input)); await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", BuildContentLog(input));
} }
public async Task OnRoutingInstructionRevised(FunctionCallFromLlm instruct, RoleDialogModel message)
{
var conversationId = _state.GetConversationId();
var agent = await _agentService.LoadAgent(message.CurrentAgentId);
var log = $"Revised user goal agent to: {agent?.Name}";
var input = new ContentLogInputModel(conversationId, message)
{
Name = agent?.Name,
AgentId = agent?.Id,
Source = ContentLogSource.HardRule,
Log = log
};
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", BuildContentLog(input));
}
#endregion #endregion

View file

@ -0,0 +1,85 @@
using BotSharp.Abstraction.Messaging.Models.RichContent;
using BotSharp.Abstraction.Messaging;
using BotSharp.Abstraction.Templating;
using BotSharp.Abstraction.Messaging.JsonConverters;
using Microsoft.AspNetCore.SignalR;
namespace BotSharp.Plugin.ChatHub.Hooks;
public class WelcomeHook : ConversationHookBase
{
private readonly IServiceProvider _services;
private readonly IHubContext<SignalRHub> _chatHub;
private readonly IUserIdentity _user;
private readonly IConversationStorage _storage;
private readonly JsonSerializerOptions _serializerOptions;
public WelcomeHook(IServiceProvider services,
IHubContext<SignalRHub> chatHub,
IUserIdentity user,
IConversationStorage storage)
{
_services = services;
_chatHub = chatHub;
_user = user;
_storage = storage;
_serializerOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Converters =
{
new RichContentJsonConverter(),
new TemplateMessageJsonConverter(),
}
};
}
public override async Task OnUserAgentConnectedInitially(Conversation conversation)
{
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");
if (welcomeTemplate != null)
{
// Render template
var templating = _services.GetRequiredService<ITemplateRender>();
var user = _services.GetRequiredService<IUserIdentity>();
var richContent = templating.Render(welcomeTemplate.Content, new Dictionary<string, object>
{
{ "user", user }
});
var richContentService = _services.GetRequiredService<IRichContentService>();
var messages = richContentService.ConvertToMessages(richContent);
foreach (var message in messages)
{
var json = JsonSerializer.Serialize(new ChatResponseModel()
{
ConversationId = conversation.Id,
Text = message.Text,
RichContent = new RichContent<IRichMessage>(message),
Sender = new UserViewModel()
{
FirstName = agent.Name,
LastName = "",
Role = AgentRole.Assistant
}
}, _serializerOptions);
await Task.Delay(300);
_storage.Append(conversation.Id, new RoleDialogModel(AgentRole.Assistant, message.Text)
{
MessageId = conversation.Id,
CurrentAgentId = agent.Id,
});
await _chatHub.Clients.User(_user.Id).SendAsync("OnMessageReceivedFromAssistant", json);
}
}
await base.OnUserAgentConnectedInitially(conversation);
}
}