Skip to content

Commit

Permalink
Merge branch 'dev' of private:sdcb/chats into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
greywen committed Dec 6, 2024
2 parents 371b435 + d36d5e5 commit 220b444
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/BE/Chats.BE.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Aliyun.OSS.SDK.NetCore" Version="2.14.1" />
<PackageReference Include="AWSSDK.S3" Version="3.7.409" />
<PackageReference Include="Azure.AI.OpenAI" Version="2.0.0" />
<PackageReference Include="Azure.Identity" Version="1.13.1" /> <!-- fix security issue from Azure.AI.OpenAI -->
Expand Down
10 changes: 5 additions & 5 deletions src/BE/Controllers/Chats/Conversations/ConversationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,17 @@ public async Task<IActionResult> StartConversationStreamed(

thisChat.Title = request.UserMessage.Text[..Math.Min(50, request.UserMessage.Text.Length)];
thisChat.ModelId = request.ModelId;
thisChat.EnableSearch = request.UserModelConfig.EnableSearch;
thisChat.Temperature = request.UserModelConfig.Temperature;
}
else
{
request = request with
{
UserModelConfig = new JsonUserModelConfig
{
EnableSearch = thisChat.EnableSearch,
Temperature = thisChat.Temperature,
EnableSearch = request.UserModelConfig.EnableSearch ?? thisChat.EnableSearch,
Temperature = request.UserModelConfig.Temperature ?? thisChat.Temperature,
}
};
}
Expand Down Expand Up @@ -156,9 +158,7 @@ ..await GetMessageTree(existingMessages, messageId).ToAsyncEnumerable().SelectAw
using ConversationService s = conversationFactory.CreateConversationService(userModel.Model);
ChatCompletionOptions cco = new()
{
Temperature = request.UserModelConfig.Temperature != null
? Math.Clamp(request.UserModelConfig.Temperature.Value, (float)userModel.Model.ModelReference.MinTemperature, (float)userModel.Model.ModelReference.MaxTemperature)
: null,
Temperature = request.UserModelConfig.Temperature,
EndUserId = currentUser.Id.ToString(),
};
await foreach (InternalChatSegment seg in icc.Run(userBalance.Balance, userModel, s.ChatStreamedFEProcessed(messageToSend, cco, cancellationToken)))
Expand Down
10 changes: 10 additions & 0 deletions src/BE/DB/Extensions/ModelReference.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Chats.BE.DB;

public partial class ModelReference
{
public float? UnnormalizeTemperature(float? temperature)
{
if (temperature == null) return null;
return temperature * (float)(MaxTemperature - MinTemperature) + (float)MinTemperature;
}
}
12 changes: 12 additions & 0 deletions src/BE/Services/Common/MaskedKeyUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public static string ToMasked(this string key)
return null;
}

if (!IsProbablyValidJson(jsonKey))
{
return ToMaskedNull(jsonKey);
}

try
{
using JsonDocument document = JsonDocument.Parse(jsonKey);
Expand Down Expand Up @@ -75,6 +80,13 @@ public static string ToMasked(this string key)
}
}

private static bool IsProbablyValidJson(string json)
{
json = json.Trim();
return (json.StartsWith('{') && json.EndsWith('}')) ||
(json.StartsWith('[') && json.EndsWith(']'));
}

public static bool SeemsMasked(this string? key)
{
return key is not null && key.Contains("****");
Expand Down
2 changes: 1 addition & 1 deletion src/BE/Services/Conversations/ConversationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Chats.BE.Services.Conversations;

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

internal protected Model Model { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ private ChatMessage[] FEProcessMessages(IReadOnlyList<ChatMessage> messages, Cha
{
options.RemoveAllowSearch();
}
options.Temperature = Model.ModelReference.UnnormalizeTemperature(options.Temperature);

return filteredMessage;
}
Expand Down
22 changes: 21 additions & 1 deletion src/BE/Services/FileServices/FileUploadRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,39 @@

namespace Chats.BE.Services.FileServices;

/// <summary>
/// Represents a request to upload a file.
/// </summary>
/// <remarks>
/// The <see cref="Stream"/> property will not be disposed by the file service.
/// </remarks>
public record FileUploadRequest
{
/// <summary>
/// Gets the name of the file.
/// </summary>
public required string FileName { get; init; }

/// <summary>
/// Gets the content type of the file.
/// </summary>
public required string ContentType { get; init; }

/// <summary>
/// Gets the stream containing the file data.
/// </summary>
/// <remarks>
/// The stream will not be disposed by the file service.
/// </remarks>
public required Stream Stream { get; init; }
}

public record CreateDownloadUrlRequest
{
public required int FileId { get; init; }
public required string StorageKey { get; init; }

public TimeSpan ValidPeriod { get; init; } = TimeSpan.FromHours(2);
public DateTime ValidEnd => DateTime.UtcNow + ValidPeriod;

public static CreateDownloadUrlRequest FromFile(DB.File file)
{
Expand Down
28 changes: 28 additions & 0 deletions src/BE/Services/FileServices/IFileService.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,36 @@
namespace Chats.BE.Services.FileServices;

/// <summary>
/// Interface for file service operations such as upload, download, and creating download URLs.
/// </summary>
public interface IFileService
{
/// <summary>
/// Uploads a file to the storage. The caller must dispose the stream.
/// </summary>
/// <param name="request">The file upload request containing file details.</param>
/// <param name="cancellationToken">Token to monitor for cancellation requests.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the storage key of the uploaded file.</returns>
/// <remarks>
/// The <see cref="Stream"/> inside <see cref="FileUploadRequest"/> will not be disposed by the file service.
/// </remarks>
Task<string> Upload(FileUploadRequest request, CancellationToken cancellationToken);

/// <summary>
/// Downloads a file from the storage.
/// </summary>
/// <param name="storageKey">The storage key of the file to download.</param>
/// <param name="cancellationToken">Token to monitor for cancellation requests.</param>
/// <returns>
/// A task that represents the asynchronous operation. The task result contains the file stream.
/// The caller must dispose the stream.
/// </returns>
Task<Stream> Download(string storageKey, CancellationToken cancellationToken);

/// <summary>
/// Creates a download URL for a file.
/// </summary>
/// <param name="request">The request containing details for creating the download URL.</param>
/// <returns>The URI of the created download URL.</returns>
Uri CreateDownloadUrl(CreateDownloadUrlRequest request);
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@

using Aliyun.OSS;

namespace Chats.BE.Services.FileServices.Implementations.AliyunOSS;

public class AliyunOSSFileService(AliyunOssConfig aliyunOssConfig) : IFileService
public class AliyunOSSFileService(AliyunOssConfig config) : IFileService
{
private readonly OssClient _oss = new(config.Endpoint, config.AccessKeyId, config.AccessKeySecret);

public Uri CreateDownloadUrl(CreateDownloadUrlRequest req)
{
throw new NotImplementedException();
return _oss.GeneratePresignedUri(config.Bucket, req.StorageKey, req.ValidEnd, SignHttpMethod.Get);
}

public Task<Stream> Download(string storageKey, CancellationToken cancellationToken)
{
throw new NotImplementedException();
cancellationToken.ThrowIfCancellationRequested();
OssObject obj = _oss.GetObject(config.Bucket, storageKey);
return Task.FromResult(obj.ResponseStream);
}

public Task<string> Upload(FileUploadRequest request, CancellationToken cancellationToken)
{
throw new NotImplementedException();
cancellationToken.ThrowIfCancellationRequested();
SuggestedStorageInfo ssi = SuggestedStorageInfo.FromFileName(request.FileName);
_ = _oss.PutObject(config.Bucket, ssi.StorageKey, request.Stream);
return Task.FromResult(ssi.StorageKey);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public Uri CreateDownloadUrl(CreateDownloadUrlRequest req)
{
BucketName = _config.Bucket,
Key = req.StorageKey,
Expires = DateTime.UtcNow + req.ValidPeriod,
Expires = req.ValidEnd,
Verb = HttpVerb.GET
});
return new Uri(url);
Expand Down
5 changes: 3 additions & 2 deletions src/FE/components/Admin/Files/FileServiceModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ export const FileServiceModal = (props: IProps) => {

const formatConfigs = (config: string) => {
try {
JSON.parse(config);
return config;
const parsed = JSON.parse(config);
return JSON.stringify(parsed, null, 2);
} catch {
return config;
}
Expand Down Expand Up @@ -182,6 +182,7 @@ export const FileServiceModal = (props: IProps) => {
getFileServiceTypeInitialConfig(fileServiceTypeId).then((res) => {
form.setValue('configs', formatConfigs(res));
});
form.setValue('name', t(feFileServiceTypes[fileServiceTypeId].name));
}
});
return () => subscription.unsubscribe();
Expand Down

0 comments on commit 220b444

Please sign in to comment.