InvalidRequestError: Must provide an 'engine' parameter while invoking openAI API for text generation
Asked Answered
T

4

6

I was trying this code given in OpenAI.

Link:- API for text generation

Code

import openai

prompt = """We’re releasing an API for accessing new AI models developed by OpenAI. Unlike most AI systems which are designed for one use-case, the API today provides a general-purpose “text in, text out” interface, allowing users to try it on virtually any English language task. You can now request access in order to integrate the API into your product, develop an entirely new application, or help us explore the strengths and limits of this technology."""

response = openai.Completion.create(model="davinci", prompt=prompt, stop="\n", temperature=0.9, max_tokens=100)

print(response)

I'm getting an error

Error

"Must provide an 'engine' parameter to create a %s" % cls, "engine". openai.error.InvalidRequestError: Must provide an 'engine' parameter to create a <class 'openai.api_resources.completion.Completion'>

I'm using python 3.7.6

Tip answered 11/1, 2021 at 13:39 Comment(0)
M
10

It seems you have confused the engine parameter with the model parameter. Please have a look at this documentation for the correct way to call: https://beta.openai.com/docs/developer-quickstart/python-bindings

Please change model = "davinci" to engine = "davinci" and you should be good to go.

Mispleading answered 25/1, 2021 at 6:44 Comment(1)
But if I have custom one?Icaria
R
1

If you get the error code:

...
InvalidRequestError: Engine not found

One possible problem could be your account setting does not offer you access to the engine. For example, embedding engines are only available for "private beta." You may need to request access to it for your account. The following code may get you the available engines to your account:

import openai

openai.api_key = your_openai_api_key

data = openai.Engine.list() for eng in data['data']:
    print(eng['id'])
Rubidium answered 1/1, 2022 at 15:57 Comment(0)
M
1

Here is complete prompt in a function, for a successful query:

import os
import openai

openai.api_key = os.environ["openai_key"]

start = "Your are a AI Search Engine, answer the following query with a witty answer and include validated facts only."

def generate(prompt):
    start_sequence = "{}.{}".format(start,prompt)
    completions = openai.Completion.create(
      model="text-davinci-003",
      prompt=start_sequence,
      temperature=0.1,
      max_tokens=256,
      top_p=1,
      frequency_penalty=0.51,
      presence_penalty=0.5,
      #stream = False,
      #echo = True
    )
          
    
    message = completions.choices[0].text
    print(message)
    return message
Metatherian answered 13/2, 2023 at 19:9 Comment(0)
S
1

I was using langchain's AzureOpenAI object and what worked for me was replacing:

from langchain.llms import AzureOpenAI

llm = AzureOpenAI(
    deployment_name="td2",
    model_name="text-davinci-002", 
)

For

llm = AzureOpenAI(
    engine="td2",
    model_name="text-davinci-002", 
)
Sparky answered 13/6, 2023 at 14:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.