python3用chatGPT和AWS API网关发送提问请求

btxsgosb  于 2023-01-14  发布在  Python
关注(0)|答案(1)|浏览(231)

当我尝试设置python3脚本从api网关向lambda发送请求时,即使我在post请求的正文中有请求消息,我还是收到错误。我不确定是什么导致了这个问题。
我的 Postman 请求中的json主体是这样的:
{ "message": "what is the capital of Texas" }
有人能帮帮我吗?

import openai
import json


def lambda_handler(event, context):
    request_body = None
    if "body" in event:
        request_body = json.loads(event["body"])
    else:
        # Handle the case where the event does not have a "body" key
        return {
            "statusCode": 400,
            "body": json.dumps({ "error": "Request must contain a message in the body." })
        }
    # Get the message from the request body
    message = request_body["message"]
        # Check if the request body contains a message
    if request_body is None or "message" not in request_body:
        # Return an error message in the HTTP response
        return {
            "statusCode": 400,
            "body": json.dumps({ "error": "Request must contain a message in the body." })
        }
    # print body api gets
    print(request_body)
    # set API key env
    OPENAI_API_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
    openai.api_key = api_key

# Use GPT-3 to generate a response
response = openai.Completion.create(
    engine="davinci",
    prompt=message,
    temperature=0.5,
    max_tokens=1024,
    top_p=1,
    frequency_penalty=1,
    presence_penalty=1,
    api_key=api_key
)

# Extract the response message from the response
response_message = response["choices"][0]["text"]

# Return the response message in the HTTP response
return {
    "statusCode": 200,
    "body": json.dumps({ "message": response_message })
}`

我已经阅读了所有的博客和信息在线使用api门的方式和错误,我得到的是以下。
回复:
{ "statusCode": 400, "body": "{\"error\": \"Request must contain a message in the body.\"}" }

yduiuuwa

yduiuuwa1#

正如官方OpenAI website所述:

**主要GPT-3型号旨在与text completion终端配合使用。**我们还提供专门用于其他终端的型号。

我们的GPT-3型号的较早版本有davincicuriebabbageada。这些版本旨在与我们的fine-tuning端点配合使用。Learn more
尝试改变这一点...

response = openai.Completion.create(
    engine = "davinci",
    prompt = message,
    temperature = 0.5,
    max_tokens = 1024,
    top_p = 1,
    frequency_penalty = 1,
    presence_penalty = 1,
    api_key = api_key
)

......到这个。

response = openai.Completion.create(
    engine = "text-davinci-003",
    prompt = message,
    temperature = 0.5,
    max_tokens = 1024,
    top_p = 1,
    frequency_penalty = 1,
    presence_penalty = 1,
    api_key = api_key
)

相关问题