繁体   English   中英

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

[英]python3 with chatGPT and AWS API gateway to send requests to ask questions

当我尝试设置 python3 脚本以将请求从 api 网关发送到 lambda 时,即使我在发布请求的正文中有请求消息,我也会收到错误消息。 我不确定是什么导致了这个问题。

我的 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.\"}" }

OpenAI 官方网站所述:

主要的 GPT-3 模型旨在与文本完成端点一起使用。 我们还提供专门用于其他端点的模型。

我们的 GPT-3 模型的旧版本有davincicuriebabbageada 这些旨在与我们的微调端点一起使用。 了解更多

试着改变这...

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
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM