ネット上には、C#の例がなかったので、curlのサンプルをもとに、ChatGPTも活用しながら、コードをつくったら、うまくいった。
string apiKey = textBox4.Text;
string apiUrl = "https://api.openai.com/v1/chat/completions";
var requestData = new
{
model = "gpt-3.5-turbo",
messages = new[]
{
new { role = "system", content = "ウオーキング愛好者です" },
new { role = "user", content = "東京都内のおすすめな散歩コースは?" }
}
};
string requestDataJson = Newtonsoft.Json.JsonConvert.SerializeObject(requestData);
using (var httpClient = new HttpClient())
{
var request = new HttpRequestMessage(HttpMethod.Post, apiUrl);
request.Headers.Add("Authorization", $"Bearer {apiKey}");
request.Content = new StringContent(requestDataJson, Encoding.UTF8, "application/json");
var response = await httpClient.SendAsync(request);
string responseBody = await response.Content.ReadAsStringAsync();
MessageBox.Show(responseBody);
}
string apiUrl = "https://api.openai.com/v1/chat/completions";
var requestData = new
{
model = "gpt-3.5-turbo",
messages = new[]
{
new { role = "system", content = "ウオーキング愛好者です" },
new { role = "user", content = "東京都内のおすすめな散歩コースは?" }
}
};
string requestDataJson = Newtonsoft.Json.JsonConvert.SerializeObject(requestData);
using (var httpClient = new HttpClient())
{
var request = new HttpRequestMessage(HttpMethod.Post, apiUrl);
request.Headers.Add("Authorization", $"Bearer {apiKey}");
request.Content = new StringContent(requestDataJson, Encoding.UTF8, "application/json");
var response = await httpClient.SendAsync(request);
string responseBody = await response.Content.ReadAsStringAsync();
MessageBox.Show(responseBody);
}
あとは、responseBodyのパースを行う方法を実装する予定
0 件のコメント:
コメントを投稿