网站制作学习网Python→正文:Azure openai.error.InvalidRequestError: The completion operation does
字体:

Azure openai.error.InvalidRequestError: The completion operation does

Python 2024/2/26 20:03:54  点击:不统计

http://%77%77%77%2E%66%6F%72%61%73%70%2E%63%6E
 在使用Microsoft Azure openai服务时通过python,通过Api接口进行openai 的请求 接口文档https://learn.microsoft.com/zh-cn/azure/ai-services/openai/how-to/migration?tabs=python%2Cdalle-fix。 

掉用API接口出现错误:
 
openai.error.InvalidRequestError: The completion operation does not work with the specified model, gpt-4. Please choose different model and try again.
 
源案例如下:
 
import os
import openai
 
openai.api_key = os.getenv("AZURE_OPENAI_API_KEY")
openai.api_base = os.getenv("AZURE_OPENAI_ENDPOINT") # your endpoint should look like the following https://YOUR_RESOURCE_NAME.openai.azure.com/
openai.api_type = 'azure'
openai.api_version = '2023-05-15' # this might change in the future
 
deployment_name='REPLACE_WITH_YOUR_DEPLOYMENT_NAME' #This will correspond to the custom name you chose for your deployment when you deployed a model. 
 
# Send a completion call to generate an answer
print('Sending a test completion job')
start_phrase = 'Write a tagline for an ice cream shop. '
response = openai.Completion.create(engine=deployment_name, prompt=start_phrase, max_tokens=10)
text = response['choices'][0]['text'].replace('\n', '').replace(' .', '.').strip()
print(start_phrase+text)
 
后来查询到使用模型是 gpt 3.5 gpt 4等,不能用Completions模式, 需要用 Chat模式,否则会报错。
实际代码请求应该按照 文档中的 聊天模式完成
 
import os
import openai
openai.api_type = "azure"
openai.api_base = os.getenv("AZURE_OPENAI_ENDPOINT") 
openai.api_key = os.getenv("AZURE_OPENAI_API_KEY")
openai.api_version = "2023-05-15"
 
response = openai.ChatCompletion.create(
    engine="gpt-35-turbo", # engine = "deployment_name".
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Does Azure OpenAI support customer managed keys?"},
        {"role": "assistant", "content": "Yes, customer managed keys are supported by Azure OpenAI."},
        {"role": "user", "content": "Do other Azure AI services support this too?"}
    ]
)
 
print(response)
print(response['choices'][0]['message']['content'])
 
这样就解决了上述问腿 。

网站制作学习网Foasp.cn

·上一篇:GPT post Eventstream 数据流代码实现 >>    ·下一篇:ModuleNotFoundError: No module named '_ctypes' >>
推荐文章
最新文章