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
+ }
0 commit comments