You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python脚本未调用GPT-3.5-turbo API问题排查求助

问题场景

Python脚本编译运行正常,能生成输出文件edited.txt,但文件内无内容。API控制台显示无调用记录,推测脚本未成功调用API,但未收到任何错误码。

目标

编写Python脚本读取文件中的文本块(每个文本块包含提示词和约2000个token的文本内容),将文本块传入OpenAI API,并将响应写入edited.txt文件。

用途

协助100 Devs训练营将视频转写文本转换为HTML文档,方便盲人和视障人士浏览。已编写三个Python脚本,分别用于:

  • 去除时间戳
  • 基于token数量拆分文本(使用TikToken)
  • 为每个段落添加相同的提示词
    本脚本用于对原始转写文本进行第一轮编辑,之后会手动编辑并添加HTML格式。

示例输入

(以下是传入脚本的文本文件示例)
格式:
prompt-text: raw-transcript-text
prompt-text: raw-transcript-text

示例:
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. ...

源代码

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)

备注

首次调用OpenAI API,本人是盲人,使用调试工具存在困难。目前能获取到GET和POST请求返回200状态码,但可能有遗漏的信息。

已尝试的解决方法

  • 阅读所有OpenAI官方文档
  • 检查终端中的Flask事件(均返回200状态码)
  • 尝试使用VS Code中的Copilot工具
  • 确认.env文件与脚本同目录,且API密钥格式正确
  • 在API控制台验证密钥已激活、有权限且无调用记录
  • 确认脚本能正常生成输出文件,但未写入内容

内容的提问来源于stack exchange,提问作者Tre Scinta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 23:35:27