Skip to content

Commit a31a1c3

Browse files
authored
ChatGPT Api Implementation and Example (#28)
Added - Chat Completion API implemented - ChatGPT sample created - Messaging app like UI created for samples Changed - The previous ChatGPT sample moved into Text Completion Sample
1 parent 4236a2f commit a31a1c3

19 files changed

+3242
-89
lines changed

Runtime/DataTypes.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,47 @@ public class OpenAIModelResponse : OpenAIModel, IResponse
7777
}
7878
#endregion
7979

80+
#region Chat API Data Types
81+
public sealed class CreateChatCompletionRequest
82+
{
83+
public string Model { get; set; }
84+
public List<ChatMessage> Messages { get; set; }
85+
public float? Temperature { get; set; } = 1;
86+
public int? N { get; set; } = 1;
87+
public bool? Stream { get; set; } = false;
88+
public string Stop { get; set; }
89+
public int? MaxTokens { get; set; }
90+
public float? PresencePenalty { get; set; } = 0;
91+
public float? FrequencyPenalty { get; set; } = 0;
92+
public Dictionary<string, string> LogitBias { get; set; }
93+
public string User { get; set; }
94+
}
95+
96+
public struct CreateChatCompletionResponse : IResponse
97+
{
98+
public ApiError Error { get; set; }
99+
public string Model { get; set; }
100+
public string Id { get; set; }
101+
public string Object { get; set; }
102+
public long Created { get; set; }
103+
public List<ChatChoice> Choices { get; set; }
104+
public Usage Usage { get; set; }
105+
}
106+
107+
public struct ChatChoice
108+
{
109+
public ChatMessage Message { get; set; }
110+
public int? Index { get; set; }
111+
public string FinishReason { get; set; }
112+
}
113+
114+
public struct ChatMessage
115+
{
116+
public string Role { get; set; }
117+
public string Content { get; set; }
118+
}
119+
#endregion
120+
80121
#region Completions API Data Types
81122
public sealed class CreateCompletionRequest
82123
{

Runtime/OpenAIApi.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,13 @@ public async Task<CreateCompletionResponse> CreateCompletion(CreateCompletionReq
166166
return await DispatchRequest<CreateCompletionResponse>(path, UnityWebRequest.kHttpVerbPOST, payload);
167167
}
168168

169+
public async Task<CreateChatCompletionResponse> CreateChatCompletion(CreateChatCompletionRequest request)
170+
{
171+
var path = $"{BASE_PATH}/chat/completions";
172+
var payload = CreatePayload(request);
173+
return await DispatchRequest<CreateChatCompletionResponse>(path, UnityWebRequest.kHttpVerbPOST, payload);
174+
}
175+
169176
/// <summary>
170177
/// Creates a new edit for the provided input, instruction, and parameters.
171178
/// </summary>

0 commit comments

Comments
 (0)