Skip to content

Commit 73f9f6d

Browse files
authored
Merge pull request #333 from hchen2020/master
chagne default temperature.
2 parents 116b58f + 86b905c commit 73f9f6d

File tree

10 files changed

+127
-49
lines changed

10 files changed

+127
-49
lines changed

src/Infrastructure/BotSharp.Abstraction/Conversations/ConversationHookBase.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using BotSharp.Abstraction.Functions.Models;
2-
31
namespace BotSharp.Abstraction.Conversations;
42

53
public abstract class ConversationHookBase : IConversationHook

src/Infrastructure/BotSharp.Abstraction/Models/MessageConfig.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ public class MessageConfig : TruncateMessageRequest
1717
/// <summary>
1818
/// The sampling temperature to use that controls the apparent creativity of generated completions.
1919
/// </summary>
20-
public float Temperature { get; set; } = 0.5f;
20+
public float Temperature { get; set; } = 0f;
2121

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

2828
/// <summary>
2929
/// Conversation states from input

src/Infrastructure/BotSharp.Core/Templating/TemplateRender.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public TemplateRender(IServiceProvider services, ILogger<TemplateRender> logger)
2727
_options.MemberAccessStrategy.Register<Agent>();
2828
_options.MemberAccessStrategy.Register<RoutableAgent>();
2929
_options.MemberAccessStrategy.Register<RoutingHandlerDef>();
30+
_options.MemberAccessStrategy.Register<UserIdentity>();
3031
}
3132

3233
public string Render(string template, Dictionary<string, object> dict)

src/Infrastructure/BotSharp.Core/Users/Services/UserIdentity.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.AspNetCore.Http;
22
using System.Security.Claims;
3+
using System.Text.Json.Serialization;
34

45
namespace BotSharp.Core.Users.Services;
56

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

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

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

28+
[JsonPropertyName("first_name")]
2629
public string FirstName
2730
{
2831
get
@@ -36,9 +39,21 @@ public string FirstName
3639
}
3740
}
3841

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

46+
[JsonPropertyName("full_name")]
4247
public string FullName
43-
=> $"{FirstName} {LastName}".Trim();
48+
{
49+
get
50+
{
51+
var fullName = _claims?.FirstOrDefault(x => x.Type == "full_name")?.Value;
52+
if (!string.IsNullOrEmpty(fullName))
53+
{
54+
return fullName;
55+
}
56+
return $"{FirstName} {LastName}".Trim();
57+
}
58+
}
4459
}

src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,8 @@ public async Task<bool> GetChatCompletionsStreamingAsync(Agent agent, List<RoleD
254254

255255
// 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
256256
var state = _services.GetRequiredService<IConversationStateService>();
257-
var temperature = float.Parse(state.GetState("temperature", "0.5"));
258-
var samplingFactor = float.Parse(state.GetState("sampling_factor", "0.5"));
257+
var temperature = float.Parse(state.GetState("temperature", "0.0"));
258+
var samplingFactor = float.Parse(state.GetState("sampling_factor", "0.0"));
259259
chatCompletionsOptions.Temperature = temperature;
260260
chatCompletionsOptions.NucleusSamplingFactor = samplingFactor;
261261
// chatCompletionsOptions.FrequencyPenalty = 0;

src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/TextCompletionProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ public async Task<string> GetCompletion(string text, string agentId, string mess
6363
completionsOptions.StopSequences.Add($"{AgentRole.Assistant}:");
6464

6565
var state = _services.GetRequiredService<IConversationStateService>();
66-
var temperature = float.Parse(state.GetState("temperature", "0.5"));
67-
var samplingFactor = float.Parse(state.GetState("sampling_factor", "0.5"));
66+
var temperature = float.Parse(state.GetState("temperature", "0.0"));
67+
var samplingFactor = float.Parse(state.GetState("sampling_factor", "0.0"));
6868
completionsOptions.Temperature = temperature;
6969
completionsOptions.NucleusSamplingFactor = samplingFactor;
7070
completionsOptions.DeploymentName = _model;

src/Plugins/BotSharp.Plugin.ChatHub/ChatHubPlugin.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
1919
{
2020
// Register hooks
2121
services.AddScoped<IConversationHook, ChatHubConversationHook>();
22-
services.AddScoped<IContentGeneratingHook, StreamingLogHook>();
2322
services.AddScoped<IConversationHook, StreamingLogHook>();
23+
services.AddScoped<IConversationHook, WelcomeHook>();
2424
services.AddScoped<IRoutingHook, StreamingLogHook>();
25+
services.AddScoped<IContentGeneratingHook, StreamingLogHook>();
2526
}
2627
}

src/Plugins/BotSharp.Plugin.ChatHub/Hooks/ChatHubConversationHook.cs

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
using BotSharp.Abstraction.Messaging;
21
using BotSharp.Abstraction.Messaging.Enums;
32
using BotSharp.Abstraction.Messaging.JsonConverters;
4-
using BotSharp.Abstraction.Messaging.Models.RichContent;
53
using Microsoft.AspNetCore.SignalR;
64

75
namespace BotSharp.Plugin.ChatHub.Hooks;
@@ -31,42 +29,6 @@ public ChatHubConversationHook(IServiceProvider services,
3129
};
3230
}
3331

34-
public override async Task OnUserAgentConnectedInitially(Conversation conversation)
35-
{
36-
var agentService = _services.GetRequiredService<IAgentService>();
37-
var agent = await agentService.LoadAgent(conversation.AgentId);
38-
39-
// Check if the Welcome template exists.
40-
var welcomeTemplate = agent.Templates?.FirstOrDefault(x => x.Name == ".welcome");
41-
if (welcomeTemplate != null)
42-
{
43-
var richContentService = _services.GetRequiredService<IRichContentService>();
44-
var messages = richContentService.ConvertToMessages(welcomeTemplate.Content);
45-
46-
foreach (var message in messages)
47-
{
48-
var json = JsonSerializer.Serialize(new ChatResponseModel()
49-
{
50-
ConversationId = conversation.Id,
51-
Text = message.Text,
52-
RichContent = new RichContent<IRichMessage>(message),
53-
Sender = new UserViewModel()
54-
{
55-
FirstName = "AI",
56-
LastName = "Assistant",
57-
Role = AgentRole.Assistant
58-
}
59-
}, _serializerOptions);
60-
61-
await Task.Delay(300);
62-
63-
await _chatHub.Clients.User(_user.Id).SendAsync("OnMessageReceivedFromAssistant", json);
64-
}
65-
}
66-
67-
await base.OnUserAgentConnectedInitially(conversation);
68-
}
69-
7032
public override async Task OnConversationInitialized(Conversation conversation)
7133
{
7234
var userService = _services.GetRequiredService<IUserService>();

src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ public async Task OnAgentQueueEmptied(string agentId, string? reason = null)
233233
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", BuildContentLog(input));
234234
}
235235

236-
public async Task OnRoutingInstructionRevised(FunctionCallFromLlm instruct, RoleDialogModel message)
236+
public async Task OnRoutingInstructionReceived(FunctionCallFromLlm instruct, RoleDialogModel message)
237237
{
238238
var conversationId = _state.GetConversationId();
239239
var agent = await _agentService.LoadAgent(message.CurrentAgentId);
@@ -249,6 +249,22 @@ public async Task OnRoutingInstructionRevised(FunctionCallFromLlm instruct, Role
249249
};
250250
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", BuildContentLog(input));
251251
}
252+
253+
public async Task OnRoutingInstructionRevised(FunctionCallFromLlm instruct, RoleDialogModel message)
254+
{
255+
var conversationId = _state.GetConversationId();
256+
var agent = await _agentService.LoadAgent(message.CurrentAgentId);
257+
var log = $"Revised user goal agent to: {agent?.Name}";
258+
259+
var input = new ContentLogInputModel(conversationId, message)
260+
{
261+
Name = agent?.Name,
262+
AgentId = agent?.Id,
263+
Source = ContentLogSource.HardRule,
264+
Log = log
265+
};
266+
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", BuildContentLog(input));
267+
}
252268
#endregion
253269

254270

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using BotSharp.Abstraction.Messaging.Models.RichContent;
2+
using BotSharp.Abstraction.Messaging;
3+
using BotSharp.Abstraction.Templating;
4+
using BotSharp.Abstraction.Messaging.JsonConverters;
5+
using Microsoft.AspNetCore.SignalR;
6+
7+
namespace BotSharp.Plugin.ChatHub.Hooks;
8+
9+
public class WelcomeHook : ConversationHookBase
10+
{
11+
private readonly IServiceProvider _services;
12+
private readonly IHubContext<SignalRHub> _chatHub;
13+
private readonly IUserIdentity _user;
14+
private readonly IConversationStorage _storage;
15+
private readonly JsonSerializerOptions _serializerOptions;
16+
public WelcomeHook(IServiceProvider services,
17+
IHubContext<SignalRHub> chatHub,
18+
IUserIdentity user,
19+
IConversationStorage storage)
20+
{
21+
_services = services;
22+
_chatHub = chatHub;
23+
_user = user;
24+
_storage = storage;
25+
26+
_serializerOptions = new JsonSerializerOptions
27+
{
28+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
29+
Converters =
30+
{
31+
new RichContentJsonConverter(),
32+
new TemplateMessageJsonConverter(),
33+
}
34+
};
35+
}
36+
37+
public override async Task OnUserAgentConnectedInitially(Conversation conversation)
38+
{
39+
var agentService = _services.GetRequiredService<IAgentService>();
40+
var agent = await agentService.LoadAgent(conversation.AgentId);
41+
42+
// Check if the Welcome template exists.
43+
var welcomeTemplate = agent.Templates?.FirstOrDefault(x => x.Name == ".welcome");
44+
if (welcomeTemplate != null)
45+
{
46+
// Render template
47+
var templating = _services.GetRequiredService<ITemplateRender>();
48+
var user = _services.GetRequiredService<IUserIdentity>();
49+
var richContent = templating.Render(welcomeTemplate.Content, new Dictionary<string, object>
50+
{
51+
{ "user", user }
52+
});
53+
var richContentService = _services.GetRequiredService<IRichContentService>();
54+
var messages = richContentService.ConvertToMessages(richContent);
55+
56+
foreach (var message in messages)
57+
{
58+
var json = JsonSerializer.Serialize(new ChatResponseModel()
59+
{
60+
ConversationId = conversation.Id,
61+
Text = message.Text,
62+
RichContent = new RichContent<IRichMessage>(message),
63+
Sender = new UserViewModel()
64+
{
65+
FirstName = agent.Name,
66+
LastName = "",
67+
Role = AgentRole.Assistant
68+
}
69+
}, _serializerOptions);
70+
71+
await Task.Delay(300);
72+
73+
_storage.Append(conversation.Id, new RoleDialogModel(AgentRole.Assistant, message.Text)
74+
{
75+
MessageId = conversation.Id,
76+
CurrentAgentId = agent.Id,
77+
});
78+
79+
await _chatHub.Clients.User(_user.Id).SendAsync("OnMessageReceivedFromAssistant", json);
80+
}
81+
}
82+
83+
await base.OnUserAgentConnectedInitially(conversation);
84+
}
85+
}

0 commit comments

Comments
 (0)