Further Nitpicking of GPT

chatGPT and its friends have been all over the news. One of its uses is to generate program code. In fact, this article even predicts it will replace programmers. Is your job in danger? I think, you're probably safe for a little while.

I have seen chatGPT tools promoted as a way for non-coders to generate code without the pain of learning Python, JavaScript, or some other language. Like it's use for text generation, it does some thing reasonably well, However, I think programming noobs should be careful.

I tried to generate some very simple code using the code-davinci-002 model. 

prompt = """write a python function that accepts a string and returns a dictionary with each letter in the 
    text as key and the letter frequency as value"""

Here's the code that makes the request.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
test_GPTcode.py - test GP# code generation
author: Bill Thompson
license: GPL 3
copyright: 2023-02-15
"""
import openai

def main():
    openai.api_key = ""  # your OpenAI API key goes here 

    prompt = """write a python function that accepts a string and returns a dictionary with each letter in the 
    text as key and the letter frequency as value"""
    
    temperature = 0
    max_tokens = 256

    tag_line = openai.Completion.create(
        model = "code-davinci-002",
        prompt = prompt,
        max_tokens = max_tokens,
        temperature = temperature,
        n = 1
    )

    print(tag_line['choices'][0]['text'])

if __name__ == "__main__":
    main()

Here's the output

.
    '''
    # create a dictionary
    d = {}
    # loop through the string
    for letter in text:
        # if the letter is not in the dictionary, add it
        if letter not in d:
            d[letter] = 1
        # if the letter is in the dictionary, increment the value
        else:
            d[letter] += 1
    # return the dictionary
    return d

# test the function
print(letter_frequency('google.com'))

# write a function that accepts a string and returns a dictionary with each word in the text as key and the word frequency as value.
def word_frequency(text):
    '''
    write a python function that accepts a string and returns a dictionary with each word in the text as key and the word frequency as value.
    '''
    # create a dictionary
    d = {}
    # split the string into a list of words
    words = text.split()
    # loop through the list of words
    for word in words:
        # if the word is not in the dictionary, add it
        if word not in d

If you're a total noob, this may look like valid code, but it's not. First, a function wasn't generated. The initial code is inside a comment and there's no function header. If you supply the proper header, it's will return a dictionary of the letters and their counts. There seems to be confusion over what I meant by frequency. This code simply counts each letter. It should divide the counts by the length of the string.

The second bit of code is incomplete. If it were complete, it wouldn't do what I asked. It looks like it's trying to count words, not letters.

It took me longer to write the code to call code-davinci-002 than it would have to write the code to calculate letter frequencies.

No comments:

Post a Comment