Skip to content

chagne default temperature. #333

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

Merged
merged 1 commit into from
Mar 7, 2024
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using BotSharp.Abstraction.Functions.Models;

namespace BotSharp.Abstraction.Conversations;

public abstract class ConversationHookBase : IConversationHook
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ public class MessageConfig : TruncateMessageRequest
/// <summary>
/// The sampling temperature to use that controls the apparent creativity of generated completions.
/// </summary>
public float Temperature { get; set; } = 0.5f;
public float Temperature { get; set; } = 0f;

/// <summary>
/// An alternative value to Temperature, called nucleus sampling, that causes
/// the model to consider the results of the tokens with probability mass.
/// </summary>
public float SamplingFactor { get; set; } = 0.5f;
public float SamplingFactor { get; set; } = 0f;

/// <summary>
/// Conversation states from input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public TemplateRender(IServiceProvider services, ILogger<TemplateRender> logger)
_options.MemberAccessStrategy.Register<Agent>();
_options.MemberAccessStrategy.Register<RoutableAgent>();
_options.MemberAccessStrategy.Register<RoutingHandlerDef>();
_options.MemberAccessStrategy.Register<UserIdentity>();
}

public string Render(string template, Dictionary<string, object> dict)
Expand Down
17 changes: 16 additions & 1 deletion src/Infrastructure/BotSharp.Core/Users/Services/UserIdentity.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Http;
using System.Security.Claims;
using System.Text.Json.Serialization;

namespace BotSharp.Core.Users.Services;

Expand All @@ -17,12 +18,14 @@ public UserIdentity(IHttpContextAccessor contextAccessor)
public string Id
=> _claims?.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value!;

[JsonPropertyName("user_name")]
public string UserName
=> _claims?.FirstOrDefault(x => x.Type == ClaimTypes.Name)?.Value!;

public string Email
=> _claims?.FirstOrDefault(x => x.Type == ClaimTypes.Email)?.Value!;

[JsonPropertyName("first_name")]
public string FirstName
{
get
Expand All @@ -36,9 +39,21 @@ public string FirstName
}
}

[JsonPropertyName("last_name")]
public string LastName
=> _claims?.FirstOrDefault(x => x.Type == ClaimTypes.Surname)?.Value!;

[JsonPropertyName("full_name")]
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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ public async Task<bool> GetChatCompletionsStreamingAsync(Agent agent, List<RoleD

// 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 temperature = float.Parse(state.GetState("temperature", "0.5"));
var samplingFactor = float.Parse(state.GetState("sampling_factor", "0.5"));
var temperature = float.Parse(state.GetState("temperature", "0.0"));
var samplingFactor = float.Parse(state.GetState("sampling_factor", "0.0"));
chatCompletionsOptions.Temperature = temperature;
chatCompletionsOptions.NucleusSamplingFactor = samplingFactor;
// chatCompletionsOptions.FrequencyPenalty = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public async Task<string> GetCompletion(string text, string agentId, string mess
completionsOptions.StopSequences.Add($"{AgentRole.Assistant}:");

var state = _services.GetRequiredService<IConversationStateService>();
var temperature = float.Parse(state.GetState("temperature", "0.5"));
var samplingFactor = float.Parse(state.GetState("sampling_factor", "0.5"));
var temperature = float.Parse(state.GetState("temperature", "0.0"));
var samplingFactor = float.Parse(state.GetState("sampling_factor", "0.0"));
completionsOptions.Temperature = temperature;
completionsOptions.NucleusSamplingFactor = samplingFactor;
completionsOptions.DeploymentName = _model;
Expand Down
3 changes: 2 additions & 1 deletion src/Plugins/BotSharp.Plugin.ChatHub/ChatHubPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
{
// Register hooks
services.AddScoped<IConversationHook, ChatHubConversationHook>();
services.AddScoped<IContentGeneratingHook, StreamingLogHook>();
services.AddScoped<IConversationHook, StreamingLogHook>();
services.AddScoped<IConversationHook, WelcomeHook>();
services.AddScoped<IRoutingHook, StreamingLogHook>();
services.AddScoped<IContentGeneratingHook, StreamingLogHook>();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using BotSharp.Abstraction.Messaging;
using BotSharp.Abstraction.Messaging.Enums;
using BotSharp.Abstraction.Messaging.JsonConverters;
using BotSharp.Abstraction.Messaging.Models.RichContent;
using Microsoft.AspNetCore.SignalR;

namespace BotSharp.Plugin.ChatHub.Hooks;
Expand Down Expand Up @@ -31,42 +29,6 @@ public ChatHubConversationHook(IServiceProvider services,
};
}

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)
{
var userService = _services.GetRequiredService<IUserService>();
Expand Down
18 changes: 17 additions & 1 deletion src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public async Task OnAgentQueueEmptied(string agentId, string? reason = null)
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 agent = await _agentService.LoadAgent(message.CurrentAgentId);
Expand All @@ -249,6 +249,22 @@ public async Task OnRoutingInstructionRevised(FunctionCallFromLlm instruct, Role
};
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


Expand Down
85 changes: 85 additions & 0 deletions src/Plugins/BotSharp.Plugin.ChatHub/Hooks/WelcomeHook.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}