Skip to content

Commit 5737cb2

Browse files
Adding more files for communication with ChatGPT
1 parent 9425bd1 commit 5737cb2

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

CSharp/OpenAi/ChatGpt.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System.Net.Http.Headers;
2+
using System.Text;
3+
using System.Text.Json;
4+
5+
namespace Temp.OpenAi;
6+
7+
public sealed class ChatGpt
8+
{
9+
private readonly HttpClient _httpClient;
10+
private const string OpenAiSecret = "your secret api";
11+
private const string ApplicationJsonMediaTypeRequest = "application/json";
12+
private const string AcceptHeaderRequest = "Accept";
13+
private const string OpenAiApiBaseUrl = "https://api.openai.com/v1/";
14+
private readonly JsonSerializerOptions _serializerOptions = new()
15+
{
16+
PropertyNameCaseInsensitive = true
17+
};
18+
public ChatGpt()
19+
{
20+
_httpClient = new HttpClient();
21+
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", OpenAiSecret);
22+
_httpClient.BaseAddress = new Uri(OpenAiApiBaseUrl);
23+
_httpClient.DefaultRequestHeaders.Add(AcceptHeaderRequest, ApplicationJsonMediaTypeRequest);
24+
}
25+
26+
public async Task<IEnumerable<string>?> GetReasonsToMyBitch()
27+
{
28+
const string prompt = "Return only a CSV list separated by semicolons, of phrases with various reasons that justify " +
29+
"my delay in leaving work, to my wife. Do not repeat this question in your response. " +
30+
"Only the raw CSV. No double quotes. Just raw CSV";
31+
32+
return await DoRequest(prompt);
33+
}
34+
35+
public async Task<IEnumerable<string>?> GetExcusesToMyMates()
36+
{
37+
const string prompt = "Return only a CSV list separated by semicolons, of phrases with various reasons that " +
38+
"justify why I can't go out for a drink with my friends. Do not repeat this question in " +
39+
"your response. Only the raw CSV. No double quotes. Just raw CSV";
40+
41+
return await DoRequest(prompt);
42+
}
43+
44+
private async Task<IEnumerable<string>?> DoRequest(string prompt)
45+
{
46+
var promptJson = new CompletionChatRequest
47+
{
48+
Messages = new List<CompletionChatMessage>
49+
{
50+
new() { Content = prompt }
51+
}
52+
};
53+
54+
var content = new StringContent(JsonSerializer.Serialize(promptJson), Encoding.UTF8, ApplicationJsonMediaTypeRequest);
55+
var responseMessage =
56+
await _httpClient.PostAsync("chat/completions", content).ConfigureAwait(false);
57+
58+
var responseContent = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
59+
60+
var response = JsonSerializer.Deserialize<CompletionChatResponse>(responseContent, _serializerOptions);
61+
return response?.Content?.Split(";");
62+
}
63+
}

CSharp/OpenAi/CompletionChatChoice.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Temp.OpenAi;
2+
3+
public record struct CompletionChatChoice
4+
{
5+
public CompletionChatMessage Message { get; set; }
6+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Temp.OpenAi;
2+
3+
public record CompletionChatResponse
4+
{
5+
public CompletionChatChoice[] Choices { get; set; }
6+
public string? Content => Choices.FirstOrDefault().Message.Content;
7+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Runtime.Serialization;
2+
3+
namespace Temp.OpenAi;
4+
5+
public class CompletionChatRequest
6+
{
7+
[DataMember(Name="model")]
8+
public readonly string Model = "gpt-3.5-turbo";
9+
10+
[DataMember(Name="temperature")]
11+
public readonly float Temperature = 1f;
12+
13+
[DataMember(Name="max_tokens")]
14+
public readonly int MaxTokens = 256;
15+
16+
[DataMember(Name="messages")]
17+
public IEnumerable<CompletionChatMessage>? Messages { get; set; }
18+
}

0 commit comments

Comments
 (0)