#Q: Incompatibility to use Embeddings and ChatCompletion (status code 405)

2 messages · Page 1 of 1 (latest)

eager aspen
#

I would like to know if it is possible to use openai.ChatCompletion.create and openai.Embedding.create in the same app.

I have seen that OpenRouter does not work for openai.Embedding.create with model text-embedding-ada-002.

Therefore I thought about trying to use OpenRouter key with the openai/gpt-4-32k model for openai.ChatCompletion.create and OpenAI key with the text-embedding-ada-002 model for openai.Embedding.create.

However, I keep getting HTTP code 405 from API when trying to create the Embedding.

My best guess is that since openai.api_base is set to use OpenRouter, then when trying to use openai key to create the embedding, the request is invalid

You can reproduce with the following code:

#openai.api_base = "https://openrouter.ai/api/v1" # not working in here
openai.api_key = os.environ.get("OPEN_AI_KEY") # using open ai key
response = openai.Embedding.create(
    input=text,
    model="text-embedding-ada-002",
)
# output: HTTP code 405 from API () 

In a different file:

openai.api_base = "https://openrouter.ai/api/v1"
openai.api_key = os.environ.get("OPENROUTER_API_KEY")

res = openai.ChatCompletion.create(
    model="openai/gpt-4-32k",
    messages=messages,
    temperature=temp_var,
    headers={
        'HTTP-Referer': ${OPENROUTER_REFERRER},
        "X-Title": ${OPENROUTER_TITLE},
        },
)

# ...
hybrid mulch
#

A few ways around this:

  1. set the API base each time you make a request/wrap it in a function/decorator
  2. replace the internal function with a modified one that only applies to chatcompletions
  3. use the OpenAI library for embeddings, and use requests for OpenRouter (or vice versa)