【Chat GPT】Run Python with ChatGPT

foreword

ChatGPT is an artificial intelligence chat robot based on the GPT-2 model. It can conduct intelligent conversations, and also supports the operation of the Python programming language, which can be called through the API interface. This article will introduce how to use ChatGPT to run Python code, and provide a real code example.

Introduction to ChatGPT

ChatGPT is an artificial intelligence chat robot that can have intelligent conversations with people. It is developed based on the GPT-2 model. GPT-2 is a deep learning-based natural language processing model developed by OpenAI, which can generate high-quality articles, poems, stories, etc., and can also conduct intelligent dialogue. ChatGPT leverages the GPT-2 model for natural language understanding and generation, enabling fluent conversations with users.

ChatGPT interface

ChatGPT provides an API interface, which can send messages to ChatGPT and receive replies from robots through HTTP requests. The message sent must be in JSON format and contain the following fields:


{
    "message": "你好"
}

The received reply from the bot is also a JSON string containing the following fields:

{
    "message": "你好呀!"
}


Among them, the message field represents the text content of the reply.

ChatGPT Python SDK

For the convenience of using ChatGPT, we also provide a Python SDK. It can be installed via pip:

pip install chatgpt

After the installation is complete, you can test it with the following code:

from chatgpt import ChatGPT

chatbot = ChatGPT()
response = chatbot.get_response("你好")
print(response)

This code will send a message to ChatGPT: "Hi", and output the bot's reply.

ChatGPT Python sample code

Let's introduce an actual ChatGPT Python sample code. This code will send the question entered by the user to ChatGPT, then call an external API to get the answer, and finally send the answer to the user.

First, we need to import the necessary dependencies:
import json
import requests
from chatgpt import ChatGPT
Then, we need to define the API address and API Key of ChatGPT:
CHATGPT_API_URL = "http://api.chatgpt.com/message"
CHATGPT_API_KEY = "YOUR_API_KEY_HERE"
Next, we need to define a function to send questions to the external API and get answers:
def get_answer(question):
    API_URL = "https://api.openai.com/v1/engine/davinci-codex/search"
    API_KEY = "YOUR_API_KEY_HERE"
    prompt = f"What is the answer to the question: {question}?"
    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {API_KEY}",
    }
    data = {
        "model": "davinci-codex-2022-06-23",
        "prompt": prompt,
        "max_tokens": 30,
        "temperature": 0,
        "n": 1,
        "stop": [".", "?", "!"],
    }
    response = requests.post(API_URL, headers=headers, json=data).json()
    answer = response["data"][0]["answer"]["text"].strip()
    return answer

 This function uses OpenAI's GPT-3 model, takes a question as input, calls the API to get the answer, and returns the answer.

Finally, we need to define a main function to receive user input, send questions to ChatGPT, and get answers:
def main():
    chatbot = ChatGPT(api_url=CHATGPT_API_URL, api_key=CHATGPT_API_KEY)
    while True:
        question = input("> ")
        response = chatbot.get_response(question)
        answer = get_answer(response)
        print(answer)

This main function uses a loop and waits for the user to enter a question. Every time it receives a question, it sends the question to ChatGPT and gets a reply from the bot. It then calls the get_answer() function to get the answer and prints the answer to the console.

Finally, we need to call the main function at the end of the program:


 

if __name__ == "__main__":
    main()

The complete code for this program is as follows:

import json
import requests
from chatgpt import ChatGPT

CHATGPT_API_URL = "http://api.chatgpt.com/message"
CHATGPT_API_KEY = "YOUR_API_KEY_HERE"

def get_answer(question):
    API_URL = "https://api.openai.com/v1/engine/davinci-codex/search"
    API_KEY = "YOUR_API_KEY_HERE"
    prompt = f"What is the answer to the question: {question}?"
    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {API_KEY}",
    }
    data = {
        "model": "davinci-codex-2022-06-23",
        "prompt": prompt,
        "max_tokens": 30,
        "temperature": 0,
        "n": 1,
        "stop": [".", "?", "!"],
    }
    response = requests.post(API_URL, headers=headers, json=data).json()
    answer = response["data"][0]["answer"]["text"].strip()
    return answer

def main():
    chatbot = ChatGPT(api_url=CHATGPT_API_URL, api_key=CHATGPT_API_KEY)
    while True:
        question = input("> ")
        response = chatbot.get_response(question)
        answer = get_answer(response)
        print(answer)

if __name__ == "__main__":
    main()

Summarize

This app uses ChatGPT for intelligent conversations and OpenAI's GPT-3 model for answers. You can replace YOUR_API_KEY_HERE with your own API Key, run this program, and test.

Guess you like

Origin blog.csdn.net/wq10_12/article/details/131958406