Preconditions
1. You need to have an openAI account.
2. You need to have a magic tool.
These two will not be taught. There are tutorials on the first one online, but I am afraid to go into the second one. . .
process
Enter the openAI homepage, find personal information, click View API keys
, click API Keys, create a key, remember to copy it, otherwise you will not be able to open it when you put it away .
After you have the key, you need to write a program.
the code
import openai
import gradio as gr
openai.api_key = "你的api_key"
def chatgpt(content, temperature=0.8):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "user", "content": content}
],
temperature=temperature,
max_tokens=1000,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
)
# print(response)
return response.choices[0].message.content
if __name__ == "__main__":
def send_chatgpt(text):
output = chatgpt(text,0.8)
return output
interface = gr.Interface(fn=send_chatgpt, inputs="text", outputs="text")
interface.launch()
Remember to pip openai and gradio (UI interface), then run the code and click the link
You can go here. Note that the apikey has a quota, which is basically enough for a single person.
If you have any questions, you can ask them in the comment area and answer them when you see them.