美文网首页
chatgpt apikey-简单调用

chatgpt apikey-简单调用

作者: 火卫控 | 来源:发表于2023-04-08 21:25 被阅读0次

    生成文字对话

    import openai
    import json
    
    # 设置API密钥
    openai.api_key = "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"
    def chat(prompt):
    
        response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[
            {"role": "user", "content":prompt}
        ]
    )
        answer = response.choices[0].message.content
        return answer
    
    if __name__=='__main__':
        # prompt='人口最多的国家'
        prompt=input("请输入你的问题: ")
        result = chat(prompt)
        print(result)
    

    生成图片

    import openai
    import json
    
    # 设置API密钥
    # openai.api_key = "sk-Wljk3BVhN0VieGCwAzEXT3BlbkFJ*******"
    openai.api_key = "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"
    
    def image_genaration(prompt):
        response = openai.Image.create(
        prompt=prompt,
        n=1,
        size="1024x1024"
        )
        image_url = response['data'][0]['url']
        return image_url
    
    if __name__=='__main__':
        prompt='a delicious dessert'
        result = image_genaration(prompt)
        print(result)
    
    
    

    相关文章

      网友评论

          本文标题:chatgpt apikey-简单调用

          本文链接:https://www.haomeiwen.com/subject/jiuvddtx.html