Skip to content

Commit

Permalink
🚀 [Code] Updated architecture to "Chat Completion"
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmonettelli committed Oct 15, 2024
1 parent 69def9b commit 516774d
Show file tree
Hide file tree
Showing 16 changed files with 246 additions and 225 deletions.
15 changes: 7 additions & 8 deletions ChatGPT/Constants/APIConstants.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
namespace ChatGPT.Constants
namespace ChatGPT.Constants;

public static class APIConstants
{
public static class APIConstants
{
public const string OpenAIUrl = "https://api.openai.com/";
public const string OpenAIToken = "OPENAI_API_KEY_HERE";
public const string OpenAIUrl = "https://api.openai.com/";
public const string OpenAIToken = "OPENAI_API_KEY_HERE";

public const string OpenAIEndpoint_Completions = "v1/completions";
public const string OpenAIEndpoint_Generations = "v1/images/generations";
}
public const string OpenAIEndpoint_Completions = "v1/chat/completions";
public const string OpenAIEndpoint_Generations = "v1/images/generations";
}
17 changes: 11 additions & 6 deletions ChatGPT/Models/Choice.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
namespace ChatGPT.Models
namespace ChatGPT.Models;

public class Choice
{
public class Choice
{
public string Text { get; set; }
public int Index { get; set; }
}
public Message Message { get; set; }
}

public class Message
{
public string Role { get; set; } = "assistant";

public string Content { get; set; }
}
28 changes: 15 additions & 13 deletions ChatGPT/Models/CompletionRequest.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
using System.Text.Json.Serialization;

namespace ChatGPT.Models
namespace ChatGPT.Models;

public class CompletionRequest
{
public class CompletionRequest
{
[JsonPropertyName("model")]
public string Model { get; set; } = "gpt-3.5-turbo-instruct";
[JsonPropertyName("model")]
public string Model { get; set; }

[JsonPropertyName("prompt")]
public string Prompt { get; set; }
[JsonPropertyName("messages")]
public List<MessageRequest> Messages { get; set; }
}

[JsonPropertyName("temperature")]
public double Temperature { get; set; } = 0;
public class MessageRequest
{
[JsonPropertyName("role")]
public string Role { get; set; }

[JsonPropertyName("max_tokens")]
public int MaxTokens { get; set; } = 100;
}
}
[JsonPropertyName("content")]
public string Content { get; set; }
}
23 changes: 17 additions & 6 deletions ChatGPT/Models/CompletionResponse.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
namespace ChatGPT.Models
namespace ChatGPT.Models;

public class CompletionResponse
{
public class CompletionResponse
{
public string Id { get; set; }
public List<Choice> Choices { get; set; }
}
public string Id { get; set; }

public List<Choice> Choices { get; set; }

public Usage Usage { get; set; }
}

public class Usage
{
public int Prompt_Tokens { get; set; } = 9;

public int Completion_Tokens { get; set; } = 12;

public int Total_Tokens { get; set; } = 21;
}
9 changes: 4 additions & 5 deletions ChatGPT/Models/GeneratedImage.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace ChatGPT.Models
namespace ChatGPT.Models;

public class GeneratedImage
{
public class GeneratedImage
{
public string Url { get; set; }
}
public string Url { get; set; }
}
19 changes: 9 additions & 10 deletions ChatGPT/Models/GenerationRequest.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
using System.Text.Json.Serialization;

namespace ChatGPT.Models
namespace ChatGPT.Models;

public class GenerationRequest
{
public class GenerationRequest
{
[JsonPropertyName("prompt")]
public string Prompt { get; set; }
[JsonPropertyName("prompt")]
public string Prompt { get; set; }

Check warning on line 8 in ChatGPT/Models/GenerationRequest.cs

View workflow job for this annotation

GitHub Actions / Build_Windows

Non-nullable property 'Prompt' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

[JsonPropertyName("n")]
public int N { get; set; } = 1;
[JsonPropertyName("n")]
public int N { get; set; } = 1;

[JsonPropertyName("size")]
public string Size { get; set; } = "512x512";
}
[JsonPropertyName("size")]
public string Size { get; set; } = "512x512";
}
9 changes: 4 additions & 5 deletions ChatGPT/Models/GenerationResponse.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace ChatGPT.Models
namespace ChatGPT.Models;

public class GenerationResponse
{
public class GenerationResponse
{
public List<GeneratedImage> Data { get; set; }
}
public List<GeneratedImage> Data { get; set; }

Check warning on line 5 in ChatGPT/Models/GenerationResponse.cs

View workflow job for this annotation

GitHub Actions / Build_Windows

Non-nullable property 'Data' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
}
9 changes: 9 additions & 0 deletions ChatGPT/Models/LocalMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace ChatGPT.Models;

public class LocalMessage
{
public string Text { get; set; }

Check warning on line 5 in ChatGPT/Models/LocalMessage.cs

View workflow job for this annotation

GitHub Actions / Build_Windows

Non-nullable property 'Text' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
public bool IsUserMessage { get; set; }
public bool IsTextActive { get; set; }
public bool IsImageActive { get; set; }
}
10 changes: 0 additions & 10 deletions ChatGPT/Models/Message.cs

This file was deleted.

11 changes: 5 additions & 6 deletions ChatGPT/Services/IOpenAIService.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
namespace ChatGPT.Services
namespace ChatGPT.Services;

public interface IOpenAIService
{
public interface IOpenAIService
{
Task<string> AskQuestion(string query);
Task<string> AskQuestion(string query);

Task<string> CreateImage(string query);
}
Task<string> CreateImage(string query);
}
94 changes: 53 additions & 41 deletions ChatGPT/Services/OpenAIService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,61 +5,73 @@
using System.Text;
using System.Text.Json;

namespace ChatGPT.Services
namespace ChatGPT.Services;

public class OpenAIService : IOpenAIService
{
public class OpenAIService : IOpenAIService
{
HttpClient client;
JsonSerializerOptions options = new() { PropertyNameCaseInsensitive = true };
HttpClient client;
JsonSerializerOptions options = new() { PropertyNameCaseInsensitive = true };

public OpenAIService()
{
client = new HttpClient();
client.BaseAddress = new Uri(APIConstants.OpenAIUrl);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", APIConstants.OpenAIToken);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
public OpenAIService()
{
client = new HttpClient();
client.BaseAddress = new Uri(APIConstants.OpenAIUrl);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", APIConstants.OpenAIToken);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}

public async Task<string> AskQuestion(string query)
public async Task<string> AskQuestion(string query)
{
var completion = new CompletionRequest
{
var completion = new CompletionRequest()
Model = "gpt-4o-mini",
Messages = new List<MessageRequest>
{
Prompt = query
};

var body = JsonSerializer.Serialize(completion);
var content = new StringContent(body, Encoding.UTF8, "application/json");
new MessageRequest
{
Role = "system",
Content = "You are a helpful assistant."
},
new MessageRequest
{
Role = "user",
Content = query
}
}
};

var response = await client.PostAsync(APIConstants.OpenAIEndpoint_Completions, content);
var body = JsonSerializer.Serialize(completion);
var content = new StringContent(body, Encoding.UTF8, "application/json");

if (response.IsSuccessStatusCode)
{
var data = await response.Content.ReadFromJsonAsync<CompletionResponse>(options);
return data?.Choices?.FirstOrDefault().Text;
}
var response = await client.PostAsync(APIConstants.OpenAIEndpoint_Completions, content);

return default;
if (response.IsSuccessStatusCode)
{
var data = await response.Content.ReadFromJsonAsync<CompletionResponse>(options);
return data?.Choices?.FirstOrDefault().Message.Content;
}

public async Task<string> CreateImage(string query)
{
var generation = new GenerationRequest()
{
Prompt = query
};
return default;
}

var body = JsonSerializer.Serialize(generation);
var content = new StringContent(body, Encoding.UTF8, "application/json");
public async Task<string> CreateImage(string query)
{
var generation = new GenerationRequest()
{
Prompt = query
};

var response = await client.PostAsync(APIConstants.OpenAIEndpoint_Generations, content);
var body = JsonSerializer.Serialize(generation);
var content = new StringContent(body, Encoding.UTF8, "application/json");

if (response.IsSuccessStatusCode)
{
var data = await response.Content.ReadFromJsonAsync<GenerationResponse>(options);
return data.Data?.FirstOrDefault()?.Url;
}
var response = await client.PostAsync(APIConstants.OpenAIEndpoint_Generations, content);

return default;
if (response.IsSuccessStatusCode)
{
var data = await response.Content.ReadFromJsonAsync<GenerationResponse>(options);
return data.Data?.FirstOrDefault()?.Url;
}

return default;
}
}
11 changes: 5 additions & 6 deletions ChatGPT/ViewModels/BaseViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using CommunityToolkit.Mvvm.ComponentModel;

namespace ChatGPT.ViewModels
namespace ChatGPT.ViewModels;

public partial class BaseViewModel : ObservableObject
{
public partial class BaseViewModel : ObservableObject
{
[ObservableProperty]
bool isBusy;
}
[ObservableProperty]
bool isBusy;
}
Loading

0 comments on commit 516774d

Please sign in to comment.