Python调用GPT程序示例

demo代码如下:

import requests
import json


def send_prompt(prompt=''):
    API_KEY = '密钥'

    if not prompt:
        return

    headers = {
    
    
        'Content-Type': 'application/json',
        'Authorization': 'Bearer ' + API_KEY
    }

    data = {
    
    
        'model': 'text-davinci-003',
        'prompt': prompt,
        "max_tokens": 150,
        "temperature": 1,
        "top_p": 1,
        "frequency_penalty": 0.0,
        "presence_penalty": 0.6,
        "stop": [" Human:", " AI:"]
    }

    response = requests.post('https://api.openai.com/v1/completions', headers=headers, data=json.dumps(data))

    response_data = response.json()
    # print(response_data)
    print(response_data['choices'][0]['text'].strip())


prompt = '如何学习Python?'
send_prompt(prompt)

猜你喜欢

转载自blog.csdn.net/weixin_46211269/article/details/130297254
今日推荐