Why is my Python script not calling GPT-3.5-turbo API?

58 Views Asked by At

Situation

My Python script compiles and runs successfully. It creates the output file (edited.txt), but doesn't write anything to the file. API dashboard shows no usage, so I'm guessing the script never successfully calls the API. However, I'm not receiving any error codes.

Objective:

I'm writing a Python script to read in blocks of text from a file. The text blocks consist of two parts; a prompt and then a 2,000 token (approximate) text body. The script is supposed to then pass that block of text to the OpenAI API and write the response to a file called "edited.txt"

Purpose:

I'm helping the 100 Devs boot camp transform their video transcripts into HTML docs so blind and visually-impaired people can navigate the video transcripts. I've already written three Python scripts that...

  • strip away time stamps
  • split the text into chunks based on token count (using TikToken)
  • appends the same prompt to the start of each paragraph

This script is intended to do the first editing pass on the raw transcript text before I manually edit and add HTMl formatting.

Sample Input:

(here's an example of what's on the text file being fed to this script)

Format:

prompt-text: raw-transcript-text

prompt-text: raw-transcript-text

Example:

Act as a software developer. Your job is to revise the following text to be more readable while maintaining any code syntax: We're back, did it again, did it again. Let's go. Fingers only. Hey, good morning. Good afternoon. Good evening. No matter where you're coming from. Hope you all are doing well. Welcome back everybody. ...

Act as a software developer. Your job is to revise the following text to be more readable while maintaining any code syntax: A lot of this stuff can just be a real torture in the beginning if you've never touched code. So we have folks from all ranges here. We've had folks that have like built full stack apps already and they're here to learn stuff that's going to help them get a job. ...

Source Code:

from flask import Flask, request, redirect, url_for, render_template
import os
from dotenv import load_dotenv
import openai
import json

load_dotenv()
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
MODEL_ENGINE = "gpt-3.5-turbo"

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        file = request.files['file']
        if file:
            data = file.read().decode('utf-8')
            blocks = data.split('\n\n')
            edited_text = ''

            for block in blocks:
                try:
                    response = openai.chat.completions.create(
                        model=MODEL_ENGINE,
                        messages=[{"role": "user", "content": block}],
                        max_tokens=4000,
                    )
                    edited_text += response.choices[0].text.strip() + '\n\n'
                except Exception as e:
                    print(f"An error occurred: {e}")

            with open('edited.txt', 'w', encoding='utf-8') as f:
                f.write(edited_text)
            

    return render_template('upload.html')

if __name__ == '__main__':
    app.run(debug=True, port=8080)

Notes:

This s my first time accessing the OpenAI API. Also I'm blind so accessing debuggers is tricky. From what I can access I hear a 200 response code for the GET and POST requests. But it's possible I'm not able to access something.

Any help you can offer is greatly appreciated!

What I've Tried:

  1. Read all available OpenAI documentation

  2. Checked Flask events in terminal (all came back 200)

  3. Tried using Copilot from within VS Code

  4. Verified .env file is in same directory with properly formatted API key.

  5. Checked API dashboard to verify key is active, has permissions and shows no usage

  6. Verified script is creating output file as expected, but not writing to it.

1

There are 1 best solutions below

1
Mudassar Farooq On

When I run your code on my side, I was receiving the error:

Choice object has no attribute text

edited_text += response.choices[0].text.strip() + '\n\n'

so i replaced the code with:

edited_text += response.choices[0].message.content.strip() + '\n\n'

I also didn't access the file from the request, files instead I did this:

file = "Hi I am the text if you read this message please respond with I red you message \n\n Hi I am the text if you read this message please respond with I red your 2nd Message"
    if file:
        data = file
        blocks = data.split('\n\n')
        edited_text = ''

The code is running fine now without any bugs.

openai.chat.completions.create method is tailored for chat-based interactions. It's designed to maintain context over a series of messages, making it suitable for creating conversational AI. So if you are not planning to build a conversation AI use the following code:

response = client.completions.create(
  model="gpt-3.5-turbo-instruct",
  prompt="Write a tagline for an ice cream shop."
)

This is good practice.