Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions UI/ChatGPT/src/ChatGPT/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,15 @@ protected override void OnLaunched(LaunchActivatedEventArgs args)
{
var section = context.Configuration.GetSection(nameof(AppConfig));
var apiKey = section[nameof(AppConfig.ApiKey)];
var useMockService = apiKey is null or { Length: 0 };

if (useMockService)
if (apiKey is null or { Length: 0 })
{
services.AddSingleton<IChatService, MockChatService>();
}
else
{
services
.AddSingleton<OpenAiOptions, ChatAiOptions>()
.AddSingleton<IChatCompletionService, OpenAIService>()
.AddSingleton(new ChatClient("gpt-3.5-turbo", apiKey))
.AddSingleton<IChatService, ChatService>();
}

Expand Down
11 changes: 0 additions & 11 deletions UI/ChatGPT/src/ChatGPT/Business/Models/ChatAiOptions.cs

This file was deleted.

7 changes: 3 additions & 4 deletions UI/ChatGPT/src/ChatGPT/ChatGPT.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
Configuration;
</UnoFeatures>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Betalgo.OpenAI" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="OpenAI" />
</ItemGroup>
</Project>
7 changes: 3 additions & 4 deletions UI/ChatGPT/src/ChatGPT/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
global using ChatGPT.Business.Models;
global using ChatGPT.Presentation;
global using ChatGPT.Services;
global using ChatGPT.Business;
global using OpenAI.Chat;
global using ApplicationExecutionState = Windows.ApplicationModel.Activation.ApplicationExecutionState;
global using Color = Windows.UI.Color;
global using OpenAI;
global using OpenAI.Interfaces;
global using OpenAI.Managers;
global using Color = Windows.UI.Color;
5 changes: 1 addition & 4 deletions UI/ChatGPT/src/ChatGPT/Presentation/MainModel.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using ChatGPT.Business;
using ChatGPT.Services;

namespace ChatGPT.Presentation;
namespace ChatGPT.Presentation;

public partial record MainModel
{
Expand Down
4 changes: 1 addition & 3 deletions UI/ChatGPT/src/ChatGPT/Presentation/Message.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using ChatGPT.Business;

namespace ChatGPT.Presentation;
namespace ChatGPT.Presentation;

public partial record Message(Guid Id, Source Source, Status Status, string? Content)
{
Expand Down
88 changes: 38 additions & 50 deletions UI/ChatGPT/src/ChatGPT/Services/ChatService.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,37 @@
using ChatGPT.Business;
using OpenAI.Interfaces;
using OpenAI.ObjectModels;
using OpenAI.ObjectModels.RequestModels;
using OpenAI.ObjectModels.ResponseModels;
using System.ClientModel;
using System.Runtime.CompilerServices;
using System.Text;

namespace ChatGPT.Services;
public class ChatService(IChatCompletionService client) : IChatService
public class ChatService : IChatService
{
private readonly IChatCompletionService _client = client;

private const string systemPrompt = "You are Uno ChatGPT Sample, a helpful assistant helping users to learn more about how to develop using Uno Platform.";

private readonly ChatClient _client;

public ChatService(ChatClient client)
{
_client = client;
}

public async ValueTask<ChatResponse> AskAsync(ChatRequest chatRequest, CancellationToken ct = default)
{
try
{
var request = ToCompletionRequest(chatRequest);
var result = await _client.CreateCompletion(request, cancellationToken: ct);
ChatCompletion result = await _client.CompleteChatAsync(request);

if (result.Successful)
return result.FinishReason switch
{
var response = result.Choices.Select(choice => choice.Message.Content);

return new ChatResponse(string.Join("", response));
}
else
{
return new ChatResponse(result.Error?.Message, IsError: true);
}
ChatFinishReason.Stop => new ChatResponse(result.ToString()),
ChatFinishReason.Length => new ChatResponse("Incomplete model output due to MaxTokens parameter or token limit exceeded.", IsError: true),
ChatFinishReason.ContentFilter => new ChatResponse("Omitted content due to a content filter flag.", IsError: true),
_ => new ChatResponse(result.FinishReason.ToString())
};
}
catch (Exception)
catch (Exception ex)
{
return new ChatResponse(IsError: true);
return new ChatResponse($"Something went wrong: {ex.Message}", IsError: true);
}
}

Expand All @@ -43,56 +41,46 @@ public async IAsyncEnumerable<ChatResponse> AskAsStream(ChatRequest chatRequest,
var response = new ChatResponse();
var content = new StringBuilder();

IAsyncEnumerator<ChatCompletionCreateResponse>? responseStream = default;
IAsyncEnumerator<StreamingChatCompletionUpdate>? responseStream = default;

while (!response.IsError)
{
try
{
responseStream ??= _client.CreateCompletionAsStream(request).GetAsyncEnumerator(ct);
responseStream ??= _client.CompleteChatStreamingAsync(request).GetAsyncEnumerator(ct);

if (await responseStream.MoveNextAsync())
{
if (responseStream.Current.Successful)
foreach (var updatePart in responseStream.Current.ContentUpdate)
{
foreach (var choice in responseStream.Current.Choices)
{
content.Append(choice.Message.Content);
}
response = response with { Message = content.ToString() };
}
else
{
response = response with { Message = responseStream.Current.Error?.Message, IsError = true };
content.Append(updatePart.Text);
}

response = response with { Message = content.ToString() };
}
else
{
yield break;
}
}
catch (Exception)
catch (Exception ex)
{
response = response with { IsError = true };
response = response with { Message = $"Something went wrong: {ex.Message}", IsError = true };
}

yield return response;
}
}

private ChatCompletionCreateRequest ToCompletionRequest(ChatRequest request)
{
var history = request.History;
var requestMessages = new List<ChatMessage>(history.Count + 1)
{
ChatMessage.FromSystem(systemPrompt)
};
requestMessages.AddRange(history.Select(entry => entry.IsUser
? ChatMessage.FromUser(entry.Message)
: ChatMessage.FromAssistant(entry.Message)));
private ChatMessage[] ToCompletionRequest(ChatRequest request)
=> request.History
.Select(ConvertMessage)
.Prepend(ChatMessage.CreateSystemMessage(systemPrompt))
.ToArray();


return new ChatCompletionCreateRequest()
{
Messages = requestMessages,
Model = Models.Gpt_3_5_Turbo
};
}
private ChatMessage ConvertMessage(ChatEntry entry)
=> entry.IsUser
? ChatMessage.CreateUserMessage(entry.Message)
: ChatMessage.CreateAssistantMessage(entry.Message);
}
4 changes: 1 addition & 3 deletions UI/ChatGPT/src/ChatGPT/Services/IChatService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using ChatGPT.Business;

namespace ChatGPT.Services;
namespace ChatGPT.Services;
public interface IChatService
{
ValueTask<ChatResponse> AskAsync(ChatRequest request, CancellationToken ct = default);
Expand Down
3 changes: 1 addition & 2 deletions UI/ChatGPT/src/ChatGPT/Services/MockChatService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using ChatGPT.Business;
using System.Runtime.CompilerServices;
using System.Runtime.CompilerServices;
using System.Text;

namespace ChatGPT.Services;
Expand Down
6 changes: 4 additions & 2 deletions UI/ChatGPT/src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
See https://aka.platform.uno/using-uno-sdk for more information.
-->
<ItemGroup>
<PackageVersion Include="Betalgo.OpenAI" Version="7.4.3" />
</ItemGroup>
</Project>
<ItemGroup>
<PackageVersion Include="OpenAI" Version="2.0.0-beta.7" />
</ItemGroup>
</Project>