I have copied my api key from playground and copied my assistants ID from the website so a spelling erorr is out of the question, ive even tried to make a new assitant and it had the same error for me. I use OpenAI version 1.37
The problem is it acts as if the ai dosent exist. and it definiteley does. there is money on the account so it should work.
My code:
from openai import OpenAI
client = OpenAI(api_key=“api key”)
def ask_my_assistant(prompt):
response = client.completions.create(model=“my assistant”, # Replace with your assistant’s ID
prompt=prompt,
max_tokens=150)
return response.choices[0].text.strip()
Example usage
response = ask_my_assistant(“Hello, how can I help you today?”)
print(response)
I get this error:
openai.NotFoundError: Error code: 404 - {‘error’: {‘message’: ‘The model asst_xxxxxxxxxxxxxxxx does not exist’, ‘type’: ‘invalid_request_error’, ‘param’: None, ‘code’: ‘model_not_found’}}
I have the exact same issue: I can create a client. The chat completion API works fine. But when I try to create an assistant, it gives me a “NotFoundError: Error code: 404 - {‘error’: {‘code’: ‘404’, ‘message’: ‘Resource not found’}}”
I am currently utilizing Flask with the request module, and all IDs appear to be functioning correctly. However, when attempting to make a request via a client (such as a website) to the OpenAI assistant, it consistently fails:
2024-03-10 16:00:55,794 - INFO - Response from OpenAI API for thread creation: 200 - {‘id’: ‘thread_ViOf8KzAoSLI7CGyjEfmBPOG’, ‘object’: ‘thread’, ‘created_at’: 1710079255, ‘metadata’: {}}
2024-03-10 16:00:56,161 - INFO - Response from OpenAI API for adding message to thread: 404 - {‘error’: {‘type’: ‘invalid_request_error’, ‘code’: ‘unknown_url’, ‘message’: ‘Unknown request URL: POST /v1/2024-02-15-preview/assistants/OPENAI_ASSISTANT_ID/messages. Please check the URL for typos, or see the docs at ht…://platform.openai.com/docs/api-reference/.’, ‘param’: None}}
2024-03-10 16:00:56,162 - ERROR - Error from OpenAI API: 404 Client Error: Not Found for url: ht…://api.openai.com/v1/2024-02-15-preview/assistants/OPENAI_ASSISTANT_ID/messages
I am facing the same issue with a fine-tuning job. Everything works fine and even says it was a success. But when I try to generate response it give the same error. Does anyone know how to solve this? I also tried updating the version but doesn’t work.
The first question is if you are using chat.completions or the assistants API. Then, if you have ensured the API key (typically set as environment variable OPENAI_API_KEY) is one for the training organization.
Then the model name. The ability to use fine-tuning models in assistants has only just been enabled. The only model that they will allow is gpt-3.5-turbo-0125, however.
You can list your fine tune models with this endpoint Python script I wrote up that shows all fine-tunes, and then for you, only those compatible with assistants, using the latest openai Python module. That will show you your true model name with created date if it is ready for use.
from typing import List, Optional
from openai import OpenAI
import datetime
import locale
def filter_models(starts_with: Optional[List[str]] = ["gpt", "ft:gpt"], blacklist: Optional[List[str]] = ["instruct"]) -> List[tuple]:
"""
Fetches and filters model names and creation dates from the OpenAI API based on specified criteria.
Parameters:
starts_with (List[str]): A list of prefixes to include models that start with any of them.
blacklist (List[str]): A list of substrings to exclude models that contain any of them.
Returns:
List[tuple]: A list of filtered model tuples (model name, creation date) that meet the criteria.
"""
client = OpenAI()
try:
model_obj = client.models.list() # API call
except Exception as err:
raise ValueError(f"Model listing API call failed: {err}") from None
model_list = sorted([(model.id, model.created) for model in model_obj.data], key=lambda x: x[0])
def starts_with_any(model_tuple: tuple, starts_with_list: List[str]) -> bool:
return any(model_tuple[0].startswith(prefix) for prefix in starts_with_list)
def contains_blacklisted(model_tuple: tuple, blacklist: List[str]) -> bool:
return any(blacklisted in model_tuple[0] for blacklisted in blacklist)
filtered_models = [model for model in model_list
if starts_with_any(model, starts_with)
and not contains_blacklisted(model, blacklist)]
return filtered_models
locale.setlocale(locale.LC_ALL, '')
default_filtered_models = filter_models(["ft:"])
print("-- All fine-tune models --")
if default_filtered_models:
print("\n".join(f"{model[0]}, Created: {datetime.datetime.fromtimestamp(model[1]).strftime('%c')}" for model in default_filtered_models))
else:
print("No models meeting criteria")
default_filtered_models = filter_models(["ft:gpt-3.5-turbo-0125"])
print("-- Fine-tune models compatible with Assistants --")
if default_filtered_models:
print("\n".join(f"{model[0]}, Created: {datetime.datetime.fromtimestamp(model[1]).strftime('%c')}" for model in default_filtered_models))
else:
print("No models meeting criteria")
Example output:
-- All fine-tune models --
ft:gpt-3.5-turbo-0613:my_org::83govGjy, Created: 9/28/2023 1:37:58 AM
ft:gpt-3.5-turbo-1106:my_org::8IkEWw8r, Created: 11/8/2023 1:18:36 PM
-- Fine-tune models compatible with Assistants --
No models meeting criteria
Hi, thank you for your response. I ran the code snippet that you attached and it is showing the fine-tune models that I created. But I still cannot generate response. I am attaching my code below. Can tell me what I am doing wrong?
import pandas as pd
df = pd.read_csv("/content/machine screws.csv") # enter the location of your data path
df.head()
def convert_to_gpt35_format(dataset):
fine_tuning_data = []
for _, row in dataset.iterrows():
fine_tuning_data.append({
"messages": [
{"role": "user", "content": row['Input']},
{"role": "assistant", "content": row['Output']} #json_response}
]
})
return fine_tuning_data
converted_data = convert_to_gpt35_format(df)
from sklearn.model_selection import train_test_split
# Stratified splitting. Assuming 'Top Category' can be used for stratification
train_data, val_data = train_test_split(
converted_data,
test_size=0.2,
random_state=42 # for reproducibility
)
import json
def write_to_jsonl(data, file_path):
with open(file_path, 'w') as file:
for entry in data:
json.dump(entry, file)
file.write('\n')
training_file_name = "train_a.jsonl"
validation_file_name = "val_a.jsonl"
write_to_jsonl(train_data, training_file_name)
write_to_jsonl(val_data, validation_file_name)
from openai import OpenAI
client = OpenAI(api_key="MY_KEY")
# Upload Training and Validation Files
training_file = client.files.create(
file=open(training_file_name, "rb"), purpose="fine-tune"
)
validation_file = client.files.create(
file=open(validation_file_name, "rb"), purpose="fine-tune"
)
# Create Fine-Tuning Job
response = client.fine_tuning.jobs.create(
training_file=training_file.id,
validation_file=validation_file.id,
model="gpt-3.5-turbo-0125",
)
# Print the result
print("Fine-Tuning Job ID:", response.id)
print("Status:", response.status)
print("Created at:", response.created_at)
job_id = response.id
prompt = " 1-8 X 15 FLAT SLTD MACH SCREW 3 THRD HDG "
response = client.chat.completions.create(
model=job_id, messages=prompt, temperature=0, max_tokens=50
)
# Access the generated text from the response
prediction_text = response.choices[0].text.strip()
print(f"Predicted code: {prediction_text}")
The next thing to investigate is the new “projects” system for managing API keys.
Since it allows model-level granularity in assigning rights, the new model may not have been populated into an existing API key automatically. OpenAI dumped this system on us without much documentation.
A project has a list of models under settings->“limits”, where you can either allow or block models that are checked. This should have your new fine-tune model.
Generate a new key within a project that is unrestricted, and then employ that new API key, and see if that solves all your problems.
Also, you provided code that both does fine tune operations and also does a completions inference.
These should be quite separate. A fine-tuning job can take hours of queue to complete, and then you should validate it independently.