-
Notifications
You must be signed in to change notification settings - Fork 1
/
OpenAIService.cs
39 lines (31 loc) · 1.29 KB
/
OpenAIService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System.Text;
using Newtonsoft.Json;
namespace openai_csharp_example;
public class OpenAIService
{
private readonly HttpClient _httpClient;
private readonly string _apiKey;
public OpenAIService(HttpClient httpClient, string apiKey)
{
_httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
_apiKey = apiKey ?? throw new ArgumentNullException(nameof(apiKey));
}
public async Task<string> SendPromptAndGetResponse(IEnumerable<object> messages)
{
const string requestUri = "https://api.openai.com/v1/chat/completions";
var requestBody = new
{
temperature = 0.2,
model="gpt-3.5-turbo",
messages= messages.ToList()
};
_httpClient.DefaultRequestHeaders.Authorization =
new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", _apiKey);
var response = await _httpClient.PostAsync(
requestUri,
new StringContent(JsonConvert.SerializeObject(requestBody), Encoding.UTF8, "application/json"));
response.EnsureSuccessStatusCode();
var responseBody = JsonConvert.DeserializeObject<ResponseBody>(await response.Content.ReadAsStringAsync());
return responseBody.Choices[0].Message.Content.Trim();
}
}