Open Source Web Application Framework for ASP.NET Core
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

61 lines
1.7 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;
using Microsoft.Extensions.AI;
namespace Volo.Abp.AI.Mocks;
public class MockChatClient : IChatClient
{
public const int StreamingResponseParts = 4;
public const string MockResponse = "This is a mock response.";
public void Dispose()
{
}
public Task<ChatResponse> GetResponseAsync(
IEnumerable<ChatMessage> messages,
ChatOptions options = null,
CancellationToken cancellationToken = default)
{
var responseMessages = messages.ToList();
responseMessages.Add(new ChatMessage(ChatRole.Assistant, MockResponse));
return Task.FromResult(new ChatResponse
{
Messages = responseMessages,
RawRepresentation = MockResponse
});
}
public object GetService(Type serviceType, object serviceKey = null)
{
return null;
}
public async IAsyncEnumerable<ChatResponseUpdate> GetStreamingResponseAsync(
IEnumerable<ChatMessage> messages,
ChatOptions options = null,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
for (var i = 0; i < StreamingResponseParts; i++)
{
await Task.Delay(25, cancellationToken);
if (cancellationToken.IsCancellationRequested)
{
break;
}
yield return new ChatResponseUpdate
{
Role = ChatRole.Assistant,
RawRepresentation = MockResponse + " " + (i + 1),
};
}
}
}