题意:在 OpenAI 的助手(assistants)中,“Assistants” 没有 “files” 这个属性。
问题背景:
assistant_file = client.beta.assistants.files.create(
assistant_id = st.session_state.assistant_id,
file_id = st.session_state.file_id
)
I have this code which I have to integrate but it's showing up that "Assistants" have no attribute "file", this code is around 8 months old, and I understand that the API probably changed since then, but I cannot find any alternatives to this in the documentation. Does anyone have any experience in migrating this code ?
我有这段代码需要集成,但是它显示“Assistants”没有“file”这个属性。这段代码大约有8个月的历史了,我理解从那时起API可能已经发生了变化,但我在文档中找不到关于这个问题的替代方案。有人有迁移这段代码的经验吗?
问题解决:
The method you're trying to use (i.e., .files.create
) doesn't exist.
你试图使用的方法(即 .files.create)不存在。
Moreover, this code wouldn't work even with the OpenAI Assistants API v1
.
此外,即使使用 OpenAI Assistants API v1,这段代码也无法工作。
If you want to create an assistant, use the following code (works with the OpenAI Assistants API v2
):
如果你想要创建一个助手,请使用以下代码(适用于 OpenAI Assistants API v2):
from openai import OpenAI
client = OpenAI()
my_assistant = client.beta.assistants.create(
instructions="You are a personal math tutor. When asked a question, write and run Python code to answer the question.",
name="Math Tutor",
tools=[{"type": "code_interpreter"}],
model="gpt-4o",
)
print(my_assistant)
If you want to create a file for an assistant, use the following code (works with the OpenAI Assistants API v2
):
如果你想要为助手创建一个文件,请使用以下代码(适用于 OpenAI Assistants API v2):
from openai import OpenAI
client = OpenAI()
my_file = client.files.create(
file=open("mydata.jsonl", "rb"),
purpose="assistants"
)
print(my_file)