使用python调用chatgpt的api

333 阅读1分钟

首先需要按照openapi pip install openai


import openai
import os

openai.api_key = mykey
COMPLETION_MODEL = "text-davinci-003"

prompt = """
Consideration proudct : 工厂现货PVC充气青蛙夜市地摊热卖充气玩具发光蛙儿童水上玩具

1. Compose human readable product title used on Amazon in english within 20 words.
2. Write 5 selling points for the products in Amazon.
3. Evaluate a price range for this product in U.S.

Output the result in json format with three properties called title, selling_points and price_range
"""

def get_response(prompt):
    completions = openai.Completion.create (
        engine=COMPLETION_MODEL,
        prompt=prompt,
        max_tokens=512,
        n=1,
        stop=None,
        temperature=0.0,        
    )
    message = completions.choices[0].text
    return message

print(get_response(prompt)) 

但是,这段代码在自己的机器上无法有返回结果,因为大家都懂的,可以通过谷歌的这个编辑器colab.research.google.com/ 来使用。

此文章为3月Day24学习笔记,内容来源于极客时间《03|巧用提示语,说说话就能做个聊天机器人 (geekbang.org)