12.5 特权权限更改
This commit is contained in:
parent
efabca13d6
commit
804186a45f
@ -1,33 +1,29 @@
|
||||
# flask_app/general/清除file_id.py
|
||||
import os
|
||||
from openai import OpenAI
|
||||
import openai
|
||||
import json
|
||||
|
||||
# 初始化 OpenAI 客户端
|
||||
client = OpenAI(
|
||||
api_key=os.getenv("DASHSCOPE_API_KEY"),
|
||||
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
|
||||
)
|
||||
|
||||
|
||||
openai.api_key = os.getenv("DASHSCOPE_API_KEY")
|
||||
openai.api_base = "https://dashscope.aliyuncs.com/compatible-mode/v1"
|
||||
def delete_all_files():
|
||||
"""
|
||||
查询所有文件并删除。
|
||||
"""
|
||||
try:
|
||||
# 获取文件列表
|
||||
file_stk = client.files.list()
|
||||
file_stk = openai.File.list()
|
||||
|
||||
# 将文件信息解析为 JSON 格式
|
||||
file_data = json.loads(file_stk.model_dump_json())
|
||||
# 将文件信息解析为字典格式
|
||||
file_data = file_stk.to_dict()
|
||||
|
||||
# 提取所有文件的 id
|
||||
file_ids = [file["id"] for file in file_data["data"]]
|
||||
|
||||
# 循环删除每个文件
|
||||
for file_id in file_ids:
|
||||
file_object = client.files.delete(file_id)
|
||||
print(f"Deleted file with id: {file_id} - {file_object.model_dump_json()}")
|
||||
file_object = openai.File.delete(file_id)
|
||||
print(f"Deleted file with id: {file_id} - {file_object}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"An error occurred while deleting files: {e}")
|
||||
@ -69,9 +65,9 @@ def delete_file_by_ids(file_ids):
|
||||
# 删除指定文件
|
||||
for file_id in file_ids:
|
||||
try:
|
||||
# 假设 client.files.delete 会返回一个文件对象
|
||||
file_object = client.files.delete(file_id)
|
||||
# print(f"Deleted file with id: {file_id} - {file_object.model_dump_json()}")
|
||||
# 假设 openai.File.delete 会返回一个文件对象
|
||||
file_object = openai.File.delete(file_id)
|
||||
# print(f"Deleted file with id: {file_id} - {file_object}")
|
||||
except Exception as e:
|
||||
# 处理删除单个文件时的异常
|
||||
print(f"Failed to delete file with id {file_id}: {e}")
|
||||
@ -79,5 +75,6 @@ def delete_file_by_ids(file_ids):
|
||||
except Exception as e:
|
||||
print(f"An error occurred while processing the file_ids: {e}")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
delete_all_files()
|
Loading…
x
Reference in New Issue
Block a user