본문 바로가기
AI/ChatGPT

ChatGPT API를 사용한 영어 대화하기 sample

by wenect 2023. 3. 6.

ChatGPT에게 영어대화하기 c# 샘플을 만들어 달라고 했더니 ...

Here is a sample C# console application that demonstrates how to use the OpenAI API to generate an English conversation

using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

class Program
{
    static async Task Main(string[] args)
    {
        string prompt = "Hey, how are you doing today?";
        string apiKey = "your-api-key-here";
        string apiUrl = "https://api.openai.com/v1/engines/davinci-codex/completions";

        using var httpClient = new HttpClient();
        httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
        httpClient.DefaultRequestHeaders.Add("Content-Type", "application/json");

        var requestBody = new
        {
            prompt = prompt,
            temperature = 0.5,
            max_tokens = 50,
            top_p = 1,
            frequency_penalty = 0,
            presence_penalty = 0
        };

        var requestJson = JsonConvert.SerializeObject(requestBody);
        var response = await httpClient.PostAsync(apiUrl, new StringContent(requestJson, Encoding.UTF8, "application/json"));
        var responseJson = await response.Content.ReadAsStringAsync();
        dynamic responseObject = JsonConvert.DeserializeObject(responseJson);

        Console.WriteLine($"Bot: {responseObject.choices[0].text}");
        Console.Write($"You: ");
        var userInput = Console.ReadLine();

        while (!string.IsNullOrEmpty(userInput))
        {
            prompt = $"{prompt}\n{userInput}";

            requestBody.prompt = prompt;
            requestJson = JsonConvert.SerializeObject(requestBody);
            response = await httpClient.PostAsync(apiUrl, new StringContent(requestJson, Encoding.UTF8, "application/json"));
            responseJson = await response.Content.ReadAsStringAsync();
            responseObject = JsonConvert.DeserializeObject(responseJson);

            Console.WriteLine($"Bot: {responseObject.choices[0].text}");
            Console.Write($"You: ");
            userInput = Console.ReadLine();
        }
    }
}

 

 

 

'AI > ChatGPT' 카테고리의 다른 글

chatGPT 설정 바꾸기  (0) 2023.03.24
ChatGPT4 - 신청  (1) 2023.03.15
VSCode with ChatGPT  (0) 2023.03.11
Bing 사용하기  (0) 2023.03.05
ChatGPT - chrome extensions  (0) 2023.02.26

댓글