Talking to ChatGPT coder style
DALL-E

Talking to ChatGPT coder style

When ChatGPT was announced, I was excited to test it immediately. I wouldn’t claim that I was among the first or anywhere near that, but I was definitely an early adopter. The excitement was real, even when I started noticing hallucinations in its responses. I still remember asking it how we translate "Eiffel Tower" into Bulgarian, and it kept using the French pronunciation written in Cyrillic letters. ChatGPT was not a quick learner at the time. After multiple attempts to teach it the correct word, it simply wasn’t able to remember it after two more prompts. But hey, we don’t leave anyone behind.

It took a lot of time for large language models (LLMs) to evolve. Meanwhile, dozens of new models were developed. We learned how to communicate with AI through prompts, leading to the birth of a new engineering discipline: prompt engineering. There are specific rules to follow if you want a well-polished result—whether you're generating text or writing a code snippet.

When I started coding with ChatGPT, I was amazed by the quality of the code it generated. But despite claims that it would replace software developers, we are far from that reality. Here’s why—read on!

I hadn’t been coding for over eight years, except for sporadically teaching Java to new joiners at companies where I worked. But that wasn’t real coding. At one point, I needed to open IntelliJ and write actual code. Of course, I needed help getting everything up and running, and this was the first time I realized that ChatGPT is great—but sometimes helpless.

I needed to start a project I had written, but for some reason, it wouldn’t run. I had never seen the error before, so I turned to ChatGPT for guidance. Unfortunately, it wasn’t able to help. It kept providing suggestions that were completely unrelated to the actual issue. Well, if ChatGPT couldn’t provide a solution, there was only one other way to tackle it—I simply searched Stack Overflow. And yes, I found the solution quite fast.

But I wasn’t giving up on ChatGPT just yet. I wanted to try something more exciting—how about sending API calls and generating some content using Python? For some of you, this might be an easy-peasy task, but for someone who knows nothing about Python, it’s a big deal. I followed a book’s instructions, but, as always in IT, things change fast. A book published last year is already outdated today. So what did I do? Of course, I asked ChatGPT about the changes I needed to make in order to successfully call the API and get responses from my Python code.

It did an amazing job. It even pointed out every single account setting I needed to configure so that my requests would be successful.




Some code will follow soon...

But before that, let’s add a few helpful hints: As mentioned above, I’m not a Python person. I might even say that I don’t really like it. And let’s not even talk about the naming conventions! Yes, I come from the Java world, and Uncle Bob’s Clean Code principles clash with Python’s style (no offense to Python fans!).

I used ChatGPT not just to fix bugs but also to understand what was actually happening in the code.

I won’t waste space explaining how to set up an account—you can follow the tutorial below. Instead, let’s focus on the actual code needed to start “talking” to ChatGPT using Python. It might not be the greatest code out there, but keep in mind that this is for learning purposes and will be improved over time. Let’s start with a simple “Hello, World!” just to check if your setup is working properly.

How to use ChatGPT API in Python – GeeksforGeeks

Code Example:

import openai

openai.api_key = 'you_enter_your_own_key_here'

try:

    response = openai.ChatCompletion.create(

        model="gpt-3.5-turbo-0125",

        messages=[{"role": "user", "content": "I need your help?"}]

    )

    print(response["choices"][0]["message"]["content"])

except Exception as e:

    print(f"An error occurred: {e}")        


Let’s Discuss the Code

As you can imagine, not much is happening yet, but it’s a start! The excitement of seeing the response “Of course! What do you need help with?” for the first time is remarkable.

Here’s what’s going on in the code:

  1. Importing the openai library – You need this to communicate with the language model. Follow the tutorial above to set it up properly. (I promise to write my own soon!)
  2. Getting an API key – You need an OpenAI account and an API key. And yes, never share your API key with anyone.
  3. Establishing a connection – We use a try block (I instinctively wrote try-catch, Java habits die hard!) to handle potential errors.
  4. Choosing a model – Here, we’re using "gpt-3.5-turbo-0125", mainly because of pricing. Yes, every single API request costs money, so choose wisely.
  5. Defining the request – The messages array defines the conversation. We set the role as "user", which means ChatGPT will respond in a generic way. This doesn’t give us much flexibility, but we’ll explore roles in a future article.
  6. Processing the response – The code retrieves and prints the first message from the response list. If you run the same request multiple times, you’ll get the same answer—try it!
  7. Handling errors – If anything goes wrong (e.g., connection issues, authentication errors, API downtime), we catch the error and display the message in the console.




What’s Next?

In our next publication, we’ll dive deeper into roles and fine-tuning, which can help us get more precise and tailored responses.

Why Bother With All This?

Well, we’re software developers. We don’t just want to use ChatGPT’s chat interface—we want to integrate it into our software and explore how it can enhance our products.

Enjoy these first lines of code! More fun is coming soon. ??

要查看或添加评论,请登录

Milena Georgieva的更多文章

  • Roles in ChatGPT

    Roles in ChatGPT

    Here is the deal. This time, we will cover the topic of roles and fine-tuning our API calls, and next time we will have…

社区洞察