Let an AI chatbot do the work

Translation at Your Command

Because users communicate with the AI back end via questions as text blocks instead of differently named API calls, you can now easily build more specialized tools that follow the pattern of Listing 2. How about a translation chatbot that translates texts from different languages into French? Listing 4 reads texts and sends them to the API with the instruction to "Translate to French" in the prompt.

Listing 4

french.go

package main
import (
 "bufio"
 "os"
)
func main() {
 ai := NewAI()
 ai.init()
 scanner := bufio.NewScanner(os.Stdin)
 text := ""
 for scanner.Scan() {
 text += scanner.Text()
 }
 ai.printResp("Translate to French:\n" + text)
}

Because the server can parse requests in many different languages, the API request does not even need to specify the source language of the provided text to be translated. The AI discovers this automatically on reading the question. Figure  7 shows that the electronic brain translates questions from German and English tourists concerning the route to the Eiffel Tower into very usable French. You can compile the french binary by typing

go build french.go openai.go
Figure 7: The electronic brain also translates from and to a wide variety of languages.

This links the new program to the library from Listing 1.

In the examples so far, we've used the el cheapo mode of chatbot with MaxTokens set to 1000, giving us relatively brief answers. For longer texts, however, Listing 1 needs to set MaxTokens to a higher value. At some point, though, this will lead to OpenAI wanting to be compensated for the work. You will then need to add a means of payment to the account in the form of a credit card.

Hard Limits

In summary, for the Davinci003 model that Listing 1 set as the electronic brain to use in line 30, the AI is surprisingly knowledgeable about real-world facts. However, this knowledge stops abruptly in the year 2021, because more recent events have not yet been fed in. For example, the server has to pass on questions about the Soccer World Cup 2022 in Qatar, and when asked about the Secretary of Defense, the server will respond with the appointee from the Trump administration. The next update to the model will hopefully get it up to speed with current events.

Disturbing Worlds

The AI behind ChatGPT can understand texts, analyze problems, answer questions, and produce output. But it is also capable of image processing. For example, using the API, it can analyze uploaded images, assign them to a category, or subject them to amazing transformations.

For example, Listing 5 runs a curl command to contact the OpenAI API endpoint images/variations. The back end then goes ahead and disturbingly modifies the original photo of a Wiener schnitzel that I cooked myself [7]. It is important to note that this particular end point only accepts square photos that are in PNG format and are no larger than 4MB.

Listing 5

var.sh

curl https://api.openai.com/v1/images/variations \
 -H "Authorization: Bearer sk-XXXXXXXXXXXXX" \
 -F image='@test.png' -F n=3 -F size="1024x1024"

To use it, I spun up the trusted convert tool from the ImageMagick collection to reformat my schnitzel photo to create a PNG image measuring 1000x1000 pixels, using the -crop and -resize options. When called with a valid API token, the curl command – which parses and uploads the test.png file – returns three different URLs in the JSON response. Each of them results in a photo variation generated from the original, which curl can subsequently retrieve from the server and store on the local drive.

Figure 8 shows the uploaded original, and Figure 9 shows one of the three variants OpenAI offered for download. You can see that the schnitzel is now on a different plate and surrounded by a light brown sauce. Next to the plate there is a beer glass full of salad. And instead of good old American India Pale Ale, you get what looks like a sake rice wine bottle top left.

Figure 8: From this schnitzel image, the AI generated …
Figure 9: … this rather disturbing variation.

On closer inspection, even the schnitzel is no longer the original. Instead, the algorithm used by the AI seems to have combined my photo with one from its archive, probably from a Japanese tonkatsu restaurant. We live in amazing times.

The Author

Mike Schilli works as a software engineer in the San Francisco Bay Area, California. Each month in his column, which has been running since 1997, he researches practical applications of various programming languages. If you email him at mailto:mschilli@perlmeister.com he will gladly answer any questions.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • ChatGPT Clients

    Do you think ChatGPT only works in your web browser? You can also access the global chat phenomenon from your desktop – or even from the Linux command line.

  • Welcome

    Books, academic journals, tech blogs, and social media posts have been trumpeting dire warnings about super-intelligent AI systems snuffing out civilization.

  • Bash PaLM Shell

    The PaLMShell.bash script lets you connect to the Google Pathways API Large Language Model (PaLM) from the command line.

  • New Release of Br OS Includes ChatGPT Integration

    Br OS 23.04 is now available and is geared specifically toward web content creation.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News