Skip to content

Commit

Permalink
Merge branch 'feature/chat-span' of private:sdcb/chats into feature/c…
Browse files Browse the repository at this point in the history
…hat-span
  • Loading branch information
greywen committed Dec 26, 2024
2 parents ef06e95 + d269c4a commit b1def27
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/BE/Controllers/Chats/Chats/ChatController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public async Task<IActionResult> ChatPrivate(
MessageLiteDto[] systemMessages = GetSystemMessages(chat, req.Spans, existingMessages, isEmptyChat);

// ensure chat.ChatSpan contains all span ids that in request, otherwise return error
if (req.Spans!.Any(x => !chat.ChatSpans.Any(y => y.SpanId == x.Id)))
if (req.Spans.Any(x => !chat.ChatSpans.Any(y => y.SpanId == x.Id)))
{
return BadRequest("Invalid span id");
}
Expand Down
44 changes: 30 additions & 14 deletions src/BE/Controllers/Chats/Chats/ChatSpanController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ public async Task<ActionResult<ChatSpanDto>> CreateChatSpan(string encryptedChat
ChatSpan toAdd = null!;
if (chat.ChatSpans.Count == 0)
{
UserModel? um = await userModelManager.GetValidModelsByUserId(currentUser.Id).FirstOrDefaultAsync(cancellationToken);
IQueryable<UserModel> query = userModelManager.GetValidModelsByUserId(currentUser.Id);

if (request.ModelId != null)
{
query = query.Where(x => x.ModelId == request.ModelId.Value);
}

UserModel? um = await query.FirstOrDefaultAsync(cancellationToken);
if (um == null)
{
return BadRequest("No models available");
Expand All @@ -42,8 +49,8 @@ public async Task<ActionResult<ChatSpanDto>> CreateChatSpan(string encryptedChat
SpanId = 0,
ModelId = um.ModelId,
Model = um.Model,
Temperature = ChatService.DefaultTemperature,
EnableSearch = true,
Temperature = request.SetsTemperature ? request.Temperature : null,
EnableSearch = request.EnableSearch ?? false,
};
}
else
Expand All @@ -55,8 +62,7 @@ public async Task<ActionResult<ChatSpanDto>> CreateChatSpan(string encryptedChat

ChatSpan refSpan = chat.ChatSpans.First();
short modelId = request.ModelId ?? refSpan.ModelId;
UserModel? um = await userModelManager.GetValidModelsByUserId(currentUser.Id)
.FirstOrDefaultAsync(x => x.ModelId == modelId, cancellationToken);
UserModel? um = await userModelManager.GetValidModelsByUserId(currentUser.Id).FirstOrDefaultAsync(x => x.ModelId == modelId, cancellationToken);
if (um == null)
{
return BadRequest("Model not available");
Expand All @@ -68,17 +74,33 @@ public async Task<ActionResult<ChatSpanDto>> CreateChatSpan(string encryptedChat
SpanId = FindAvailableSpanId(chat.ChatSpans),
ModelId = request.ModelId ?? refSpan.ModelId,
Model = um.Model,
Temperature = request.SetTemperature ? request.Temperature : refSpan.Temperature,
Temperature = request.SetsTemperature ? request.Temperature : refSpan.Temperature,
EnableSearch = request.EnableSearch ?? refSpan.EnableSearch,
};
}

chat.ChatSpans.Add(toAdd);
await db.SaveChangesAsync(cancellationToken);
return Created(default(string), ChatSpanDto.FromDB(toAdd));
}

static byte FindAvailableSpanId(ICollection<ChatSpan> spans)
/// <summary>
/// Finds the next available SpanId for a new ChatSpan.
/// </summary>
/// <param name="spans">The SpanId asc ordered collection of existing ChatSpans.</param>
/// <returns>The next available SpanId.</returns>
static byte FindAvailableSpanId(ICollection<ChatSpan> spans)
{
// Suggest the next SpanId based on the last SpanId in the collection
byte suggested = spans.Last().SpanId;
if (suggested < 255)
{
// If the suggested SpanId is less than 255, increment it by 1
return (byte)(suggested + 1);
}
else
{
// If the suggested SpanId is 255, find the first available SpanId starting from 0
byte spanId = 0;
while (spans.Any(x => x.SpanId == spanId))
{
Expand Down Expand Up @@ -114,7 +136,7 @@ public async Task<ActionResult<ChatSpanDto>> UpdateChatSpan(string encryptedChat
span.Model = um.Model;
}

if (request.SetTemperature)
if (request.SetsTemperature)
{
span.Temperature = request.Temperature;
}
Expand All @@ -139,12 +161,6 @@ public async Task<IActionResult> DeleteChatSpan(string encryptedChatId, byte spa
return NotFound();
}

bool hasMessages = await db.Messages.AnyAsync(x => x.ChatId == chatId && x.SpanId == spanId, cancellationToken);
if (hasMessages)
{
return BadRequest("Cannot delete span with messages");
}

db.ChatSpans.Remove(span);
await db.SaveChangesAsync(cancellationToken);
return NoContent();
Expand Down
13 changes: 10 additions & 3 deletions src/BE/Controllers/Chats/Chats/Dtos/CreateChatSpanRequest.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;

namespace Chats.BE.Controllers.Chats.Chats.Dtos;

public record CreateChatSpanRequest
{
[JsonPropertyName("modelId")]
public short? ModelId { get; init; }
public bool SetTemperature { get; init; } = false;

[JsonPropertyName("setsTemperature")]
public bool SetsTemperature { get; init; } = false;

[JsonPropertyName("temperature")]
public float? Temperature { get; init; }

[JsonPropertyName("enableSearch")]
public bool? EnableSearch { get; init; }
}
}
2 changes: 1 addition & 1 deletion src/BE/Services/ChatServices/ChatService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Chats.BE.Services.ChatServices;

public abstract partial class ChatService : IDisposable
{
public const float DefaultTemperature = 0.5f;
public const float DefaultTemperature = 0.4f;
public const string DefaultPrompt = "你是{{MODEL_NAME}},请仔细遵循用户指令并认真回复,当前日期: {{CURRENT_DATE}}";

internal protected Model Model { get; }
Expand Down

0 comments on commit b1def27

Please sign in to comment.