Translator with C# and ChatGPT

In the recent few months, ChatGPT has become very famous and everyone is talking about it. So, I finally decided to play with the API they provide and see what I can build with this new and cool technology. This seems like a new door for some cool possibilities. In this article, I want to show how you can use ChatGPT in C# and make a small console app that can translate your text to French and Armenian languages at the same time. After reading this article, you should be able to use ChatGPT API in your C# projects; however, this example is going to be very simple but it’s a very good example because it won’t have complicated code and also I can show you how easily we can use ChatGPT to add some fancy feature in our projects.

Get an API Key from ChatGPT

First of all, you need to generate your ChatGPT’s API key. Go here and create your API Key and copy that to a safe place.

Then I’m going to create a console app and name it Translator, this is going to use .NET 7.0 and all my code is going to be in the Programs.cs file. The next thing we are going to need is the OpenAI NuGet package to be able to connect the ChatGPT.

Looking at the OpenAI website documentation, the following code is the given example for doing a language translation in Python. So, basically, I’m going to use the same model and settings in C# to be able to create my little translator app.

import os
import openai

openai.api_key = os.getenv("OPENAI_API_KEY")

response = openai.Completion.create(
  model="text-davinci-003",
  prompt="Translate this into 1. French, 2. Spanish and 3. Japanese:\n\nWhat rooms do you have available?\n\n1.",
  temperature=0.3,
  max_tokens=100,
  top_p=1.0,
  frequency_penalty=0.0,
  presence_penalty=0.0
)

Finally, this is the C# code I put in my Program.cs and that is all I needed to do for making an AI translator using ChatGPT. Just don’t forget to use your API KEY in the code.

using OpenAI_API;
using System.Text;

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

var apiKey = "YOUR 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;
    rq.Add(choice.Text);
}


Console.OutputEncoding = Encoding.UTF8;
Console.WriteLine(rq.FirstOrDefault());

In the image below, you can see the working app and how I was able to translate my English sentence into France and Armenian.

In this article, we saw how easy it can be to use ChatGPT in our projects, but to be fair there was nothing new on our side since we just used a NuGet package to make the call and everything else is happening on the ChatGPT side. There is more advanced stuff like training ChatGPT with your custom data, but I don’t know how to do that yet and I will share as soon as I learn more about the different stuff I can do with ChatGPT. This was only an example to help people new to programming to start playing with ChatGPT in C#. I hope you enjoyed it.