Python calls the API of chatGPT

Foreword:

Recently, I have been studying GPT and LLM, and I have abandoned other things. I just update one and make up a word count.

1. python standard interface use

Python is connected to chatGPT, and it is packaged into an API interface with flask, so that you can connect it to WeChat, applets, official accounts or various places by yourself.

First upload the code:

import openai

#openai.api_key = "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"
openai.api_key = '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'

def generate_response(user_input):
    prompt = "The following is a conversation with an AI assistant. The assistant helps with various tasks. User: " + user_input + " AI:"
    response = openai.Completion.create(
        engine="text-davinci-003",
        prompt=prompt,
        max_tokens=1024,
        n=1,
        stop=None,
        temperature=0.7,
    )
    message = response.choices[0].text.strip()
    return message



if __name__ == "__main__":
    user_input = "我很失望怎么办"
    #prompt = "The following is a conversation with an AI assistant. The assistant helps with various tasks. User: " + user_input + " AI:"
    response = generate_response(user_input)
    print( response)

There are a few points to note.

1. It is the acquisition of APIkey.

First log in to OpenAI , log in to your own account, here.

 2. Next click

 That's it.

3. I have packaged the code and used it directly.

2. Python standard interface usage results

Question: What should I do if I am disappointed

The result of the dialogue is as follows 

Some fans reported that this APIkey is not easy to handle. I will share the recently available ones on the planet every day.

However, it is only for learning and testing purposes. If the request frequency is too high within a unit hour, it will be blocked.

Please do not use it commercially or maliciously. Thanks.

AI Polymath Alliance: AI Polymath Alliance

Guess you like

Origin blog.csdn.net/qq_33083551/article/details/130581538
Recommended