Skip to content

Commit

Permalink
qianfan now can search
Browse files Browse the repository at this point in the history
  • Loading branch information
sdcb committed Feb 24, 2025
1 parent 7c382a8 commit 8c75390
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 59 deletions.
12 changes: 8 additions & 4 deletions src/BE/Controllers/Chats/Chats/ChatController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,6 @@ private async Task<IActionResult> ChatPrivate(
GetMessageTree(existingMessages, req.MessageId),
systemMessages.Where(x => x.Role == DBChatRole.System && x.SpanId == span.Id || x.SpanId == null).ToArray(),
dbUserMessage,
new ChatExtraDetails() { TimezoneOffset = req.TimezoneOffset },
userBalance,
clientInfoTask,
channels[index].Writer,
Expand Down Expand Up @@ -378,7 +377,6 @@ private static async Task<ChatSpanResponse> ProcessChatSpan(
IEnumerable<MessageLiteDto> messageTree,
MessageLiteDto[] systemMessages,
Message? dbUserMessage,
ChatExtraDetails extraDetails,
UserBalance userBalance,
Task<ClientInfo> clientInfoTask,
ChannelWriter<SseResponseLine> writer,
Expand All @@ -394,7 +392,13 @@ private static async Task<ChatSpanResponse> ProcessChatSpan(
.SelectAwait(async x => await x.ToOpenAI(fup, cancellationToken))
.ToArrayAsync(cancellationToken);

ChatCompletionOptions cco = span.ToChatCompletionOptions(currentUser.Id, chat.ChatSpans.First(cs => cs.SpanId == span.Id));
ChatSpan chatSpan = chat.ChatSpans.First(cs => cs.SpanId == span.Id);
ChatCompletionOptions cco = span.ToChatCompletionOptions(currentUser.Id, chatSpan);
ChatExtraDetails ced = new()
{
TimezoneOffset = req.TimezoneOffset,
WebSearchEnabled = chatSpan.EnableSearch,
};

InChatContext icc = new(firstTick);

Expand All @@ -403,7 +407,7 @@ private static async Task<ChatSpanResponse> ProcessChatSpan(
{
using ChatService s = chatFactory.CreateChatService(userModel.Model);
bool responseStated = false, reasoningStarted = false;
await foreach (InternalChatSegment seg in icc.Run(userBalance.Balance, userModel, s.ChatStreamedFEProcessed(messageToSend, cco, extraDetails, cancellationToken)))
await foreach (InternalChatSegment seg in icc.Run(userBalance.Balance, userModel, s.ChatStreamedFEProcessed(messageToSend, cco, ced, cancellationToken)))
{
if (!string.IsNullOrEmpty(seg.ReasoningSegment))
{
Expand Down
27 changes: 0 additions & 27 deletions src/BE/Controllers/Chats/Chats/Dtos/ChatRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,6 @@

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

//public record ChatRequest
//{
// [JsonPropertyName("chatId")]
// public required string EncryptedChatId { get; init; }

// [JsonPropertyName("spans")]
// public required InternalChatSpanRequest[] Spans { get; init; }

// [JsonPropertyName("messageId")]
// public string? EncryptedMessageId { get; init; }

// [JsonPropertyName("userMessage")]
// public MessageContentRequest? UserMessage { get; init; }

// public DecryptedChatRequest Decrypt(IUrlEncryptionService idEncryption)
// {
// return new DecryptedChatRequest
// {
// ChatId = idEncryption.DecryptChatId(EncryptedChatId),
// Spans = Spans,
// MessageId = EncryptedMessageId == null ? null : idEncryption.DecryptMessageId(EncryptedMessageId),
// UserMessage = UserMessage,
// };
// }
//}

public record ChatSpanRequest : CreateChatSpanRequest
{
[JsonPropertyName("id")]
Expand All @@ -49,7 +23,6 @@ public ChatCompletionOptions ToChatCompletionOptions(int userId, ChatSpan span)
Temperature = span.Temperature,
EndUserId = userId.ToString(),
};
cco.SetAllowSearch(span.EnableSearch);
return cco;
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/BE/Services/Models/ChatExtraDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

public record ChatExtraDetails
{
public required short TimezoneOffset { get; init; }
public short TimezoneOffset { get; init; }

public bool WebSearchEnabled { get; init; }

public DateTime Now => DateTime.UtcNow.AddMinutes(TimezoneOffset);

public static ChatExtraDetails Default => new()
{
TimezoneOffset = 0
};
public static ChatExtraDetails Default => new();
}
2 changes: 1 addition & 1 deletion src/BE/Services/Models/ChatFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Chats.BE.DB.Enums;
using Chats.BE.Services.Models.ChatServices.DashScope;
using Chats.BE.Services.Models.ChatServices.OpenAI;
using Chats.BE.Services.Models.ChatServices.QianFan;
using Chats.BE.Services.Models.ChatServices.OpenAI.QianFan;
using Chats.BE.Services.Models.ChatServices.Test;
using Chats.BE.Services.Models.ModelLoaders;
using OpenAI.Chat;
Expand Down
4 changes: 0 additions & 4 deletions src/BE/Services/Models/ChatServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,6 @@ protected virtual async Task<ChatMessage[]> FEPreprocess(IReadOnlyList<ChatMessa
.ToAsyncEnumerable()
.SelectAwait(async m => await FilterVision(Model.ModelReference.AllowVision, m, cancellationToken))
.ToArrayAsync(cancellationToken);
if (!Model.ModelReference.AllowSearch)
{
options.RemoveAllowSearch();
}
options.Temperature = Model.ModelReference.UnnormalizeTemperature(options.Temperature);

return filteredMessage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ public DashScopeChatService(Model model) : base(model)
ChatClient = Client.TextGeneration;
}

protected override Task<OpenAIChatMessage[]> FEPreprocess(IReadOnlyList<OpenAIChatMessage> messages, ChatCompletionOptions options, ChatExtraDetails feOptions, CancellationToken cancellationToken)
{
if (feOptions.WebSearchEnabled && Model.ModelReference.AllowSearch)
{
options.SetWebSearchEnabled_QwenStyle(true);
}
return base.FEPreprocess(messages, options, feOptions, cancellationToken);
}

public override async IAsyncEnumerable<ChatSegment> ChatStreamed(IReadOnlyList<OpenAIChatMessage> messages, ChatCompletionOptions options, [EnumeratorCancellation] CancellationToken cancellationToken)
{
ChatParameters chatParameters = new()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ namespace Chats.BE.Services.Models.ChatServices.OpenAI.PipelinePolicies;

public class ReplaceSseContentPolicy(string SearchText, string ReplaceText) : PipelinePolicy
{
//private const string SearchText = "\"finish_reason\":\"normal\"";
//private const string ReplaceText = "\"finish_reason\":\"stop\"";

public override void Process(PipelineMessage message, IReadOnlyList<PipelinePolicy> pipeline, int currentIndex)
{
// 同步封装异步
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;

namespace Chats.BE.Services.Models.ChatServices.QianFan;
namespace Chats.BE.Services.Models.ChatServices.OpenAI.QianFan;

public record JsonQianFanApiConfig
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using Chats.BE.DB;
using Chats.BE.Services.Models.ChatServices.OpenAI;
using OpenAI.Chat;
using OpenAI;
using System.ClientModel.Primitives;
using System.ClientModel;
using Chats.BE.Services.Models.ChatServices.OpenAI.PipelinePolicies;
using System.Text.Json;
using Chats.BE.Services.Models.Extensions;

namespace Chats.BE.Services.Models.ChatServices.QianFan;
namespace Chats.BE.Services.Models.ChatServices.OpenAI.QianFan;

public class QianFanChatService(Model model) : OpenAIChatService(model, CreateChatClient(model, new Uri("https://qianfan.baidubce.com/v2")))
{
Expand All @@ -27,4 +27,13 @@ private static ChatClient CreateChatClient(Model model, Uri? suggestedApiUrl)
OpenAIClient api = new(new ApiKeyCredential(cfg.ApiKey), oaic);
return api.GetChatClient(model.ApiModelId);
}

protected override Task<ChatMessage[]> FEPreprocess(IReadOnlyList<ChatMessage> messages, ChatCompletionOptions options, ChatExtraDetails feOptions, CancellationToken cancellationToken)
{
if (feOptions.WebSearchEnabled && Model.ModelReference.AllowSearch)
{
options.SetWebSearchEnabled_QianFanStyle(true);
}
return base.FEPreprocess(messages, options, feOptions, cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static bool IsSearchEnabled(this ChatCompletionOptions options)
return false;
}

public static void SetAllowSearch(this ChatCompletionOptions options, bool value)
public static void SetWebSearchEnabled_QwenStyle(this ChatCompletionOptions options, bool value)
{
IDictionary<string, BinaryData>? rawData = GetSerializedAdditionalRawData(options);
if (rawData == null)
Expand All @@ -28,20 +28,21 @@ public static void SetAllowSearch(this ChatCompletionOptions options, bool value
rawData["enable_search"] = BinaryData.FromObjectAsJson(value);
}

public static void RemoveAllowSearch(this ChatCompletionOptions options)
public static void SetWebSearchEnabled_QianFanStyle(this ChatCompletionOptions options, bool value)
{
IDictionary<string, BinaryData>? rawData = GetSerializedAdditionalRawData(options);
rawData?.Remove("enable_search");
}
if (rawData == null)
{
rawData = new Dictionary<string, BinaryData>();
SetSerializedAdditionalRawData(options, rawData);
}

public static void SetModelName(this ChatCompletionOptions @this, string name)
{
Type internalCreateChatCompletionRequestModelType = typeof(ChatCompletionOptions).Assembly.GetType("OpenAI.Chat.InternalCreateChatCompletionRequestModel")
?? throw new InvalidOperationException("InternalCreateChatCompletionRequestModel type not found");
object modelValue = Activator.CreateInstance(internalCreateChatCompletionRequestModelType, [name])
?? throw new InvalidOperationException("Failed to create instance of InternalCreateChatCompletionRequestModel");
(typeof(ChatCompletionOptions).GetProperty("Model", BindingFlags.Instance | BindingFlags.NonPublic) ?? throw new InvalidOperationException("Model property not found"))
.SetValue(@this, modelValue);
rawData["web_search"] = BinaryData.FromObjectAsJson(new Dictionary<string, object>()
{
["enable"] = true,
["enable_citation"] = false,
["enable_trace"] = false,
});
}

public static ulong? GetDashScopeSeed(this ChatCompletionOptions options)
Expand Down

0 comments on commit 8c75390

Please sign in to comment.