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

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

But if I have custom one?
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'])

Comments

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

Comments

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", 
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.