in API, is there any way to list the available/applicable models specific to a capability (chat, embeddings etc)? #1400
Unanswered
joshuamosesb
asked this question in
Q&A
Replies: 1 comment
-
Could maybe do something like this to fallback to another endpoint if the first try fails (ignore the display var): async def query_openai(text, display, retries=3):
# Load settings from settings.json
settings = load_settings()
max_tokens = settings.get("max_tokens")
temperature = settings.get("temperature")
for i in range(retries):
try:
response = openai.ChatCompletion.create(
model=settings.get("model"),
messages=[
{"role": "system", "content": f"You are a helpful assistant. {settings.get('custom_instructions')}"},
{"role": "user", "content": f"Human: {text}\nAI:"}
],
max_tokens=max_tokens,
temperature=temperature
)
response_content = response['choices'][0]['message']['content'].strip()
if response_content: # Check if the response is not empty
message = response_content
return message
else:
logger.warning(f"Retry {i+1}: Received empty response from OpenAI.")
except Exception as e:
if 'Did you mean to use v1/completions?' in str(e):
# Re-query using v1/completions
prompt = f"You are a helpful assistant. {settings.get('custom_instructions')}\nHuman: {text}"
response = openai.Completion.create(
model=settings.get("model"),
prompt=prompt,
max_tokens=max_tokens,
temperature=temperature
)
response_content = response['choices'][0]['text'].strip()
if response_content:
message = response_content
return message
else:
logger.error(f"Error on try {i+1}: {e}")
if i == retries - 1: # If this was the last retry
error_message = f"Something went wrong after {retries} retries: {e}"
await handle_error(error_message, None, display)
await asyncio.sleep(0.5) # Wait before retrying This function could be modified to filter whatever models you want to use: async def available_models():
try:
# Get available models from OpenAI
openai.api_key = os.getenv("OPENAI_API_KEY")
model_list = openai.Model.list()
# Filter to only keep supported models.
supported_models = [model['id'] for model in model_list['data'] if "gpt" in model['id'].lower()]
return JSONResponse(content={"models": supported_models})
except Exception as e:
return HTTPException(status_code=500, detail=f"An error occurred: {str(e)}") Check out my project GPT Home for more context: https://github.com/judahpaul16/gpt-home. First function is from |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
this snippet lists all available models for the
client
instance.But, is there any way to list available/valid models at respective capability level like
client.chat.models.list()
orclient.embeddings.models.list()
?this will help in passing a suitable model name while calling the respective
create
method (e.g.,client.chat.completion.create
)if we pass
model="text-embedding-ada-002"
not suitable forchat.completion.create
we get an error:
Beta Was this translation helpful? Give feedback.
All reactions