Building a C# and ChatGPT Translator


Language translation is a common requirement in various applications. This article will explore how to build a simple translator using C# and ChatGPT with minimal code. We'll leverage the OpenAI API to interact with the ChatGPT model and create a C# console application that can translate text from one language to another. Let's dive into the steps involved in building this translator.


Before we begin, ensure you have the following,

  • OpenAI API key: Sign in to your OpenAI account and generate an API key. You'll need this key to authenticate requests to the ChatGPT model.
  • C# development environment: Set up a C# development environment such as Visual Studio or Visual Studio Code.

Step 1. Set up the project

Create a new C# console application project in your preferred development environment.

Step 2. Install the required packages

To make HTTP requests and handle JSON responses, install the 'System.Net.Http' and 'System.Text.Json' packages. You can install them via NuGet Package Manager or using the .NET CLI:

  • dotnet add package System.Net.Http
  • dotnet add package System.Text.Json

Step 3. Write the translation code

Replace the contents of the 'Program.cs' file with the following code.

using OpenAI_API;
using System.Text;

Console.WriteLine("Enter you message: ");
string input = Console.ReadLine() + "\n";

var apiKey = "API KEY";
var apiModel = "text-davinci-003";
List<string> rq = new List<string>();
string rs = "";

OpenAIAPI api = new OpenAIAPI(new APIAuthentication(apiKey));
var completionRequest = new OpenAI_API.Completions.CompletionRequest()
    Prompt = "Translate this into 1. French and 2. Armenian:\r\n" + input,
    Model = apiModel,
    Temperature = 0.3,
    MaxTokens = 100,
    TopP = 1.0,
    FrequencyPenalty = 0.0,
    PresencePenalty = 0.0,


var result = await api.Completions.CreateCompletionsAsync(completionRequest);
foreach (var choice in result.Completions)
    rs = choice.Text;

Console.OutputEncoding = Encoding.UTF8;


Step 4. Replace the API key

Replace `"YOUR_API_KEY"` in the code with your actual OpenAI API key obtained earlier.

Step 5. Run the application

Build and run the C# application. You'll be prompted to enter the text you want to translate. After entering the text, the application will output the translated text.

The output shows different language conversions from "Hello" in English. 


This article taught us how to build a simple language translator using C# and the ChatGPT model with minimal code. By leveraging the OpenAI API, we were able to send translation requests and receive the translated text in our C# console application. This approach can be extended and customized to suit more complex translation scenarios and integrated into various C# applications.

Similar Articles