DEV Community

rohan
rohan

Posted on • Originally published at ex.hashnode.dev

How to Detect Text Generated by ChatGPT

Generative Pre-trained Transformers (GPTs) are a family of language models that use deep learning to produce natural language texts based on a given input. A user can feed the model with input like a sentence and the GPT creates a paragraph based on information extracted from publicly available datasets.

GPTs have been advancing rapidly in recent years, with models like GPT-3 capable of generating human-like text across various domains and tasks. However, this also poses some challenges and risks, such as plagiarism, misinformation, spam, and manipulation. Therefore, it is important to be able to detect text generated by GPTs and distinguish it from human-written text.

In this article, we will explore some methods and tools that can help us detect text generated by GPTs. We will also look at some code examples that demonstrate how to use these methods and tools.

Methods for Detecting Text Generated by GPTs

There are different approaches for detecting text generated by GPTs, depending on the level of analysis and the type of features used. Some of these approaches are:

  • Statistical analysis: This approach involves analyzing the statistical properties of the text, such as word frequency, n-gram distribution, perplexity (a measure of randomness), burstiness (a measure of variance), etc. These properties can be compared with those of human-written texts or other GPT-generated texts to identify anomalies or patterns.
  • Syntactic analysis: This approach involves analyzing the syntactic structure of the text, such as grammar rules, punctuation marks, sentence length, etc. These features can be used to detect errors or inconsistencies that may occur in GPT-generated texts due to their limited understanding of natural language.
  • Semantic analysis: This approach involves analyzing the semantic meaning of the text, such as logic coherence, factual accuracy, relevance, etc. These features can be used to detect contradictions or implausibilities that may occur in GPT-generated texts due to their lack of common sense or domain knowledge.
  • Stylometric analysis: This approach involves analyzing the stylistic features of the text, such as vocabulary choice, tone, sentiment, etc. These features can be used to detect differences or similarities between the authorship or genre of the text and the expected one.

Tools for Detecting Text Generated by GPTs

There are various tools available online that can help us detect text generated by GPTs using some of the methods mentioned above. Some of these tools are:

  • AI Content Detector: This tool can detect texts generated using ChatGPT technology by analyzing various syntactical and semantic elements. It uses a combination of natural language processing (NLP) techniques, such as word embedding and phrase classification, to accurately detect the text generated with ChatGPT. You can try it here: https://corrector.app/ai-content-detector/
  • GPT Essay Checker: This tool can detect essays generated using ChatGPT technology by analyzing various statistical properties. It uses an algorithm based on perplexity scores to measure how likely an essay was produced by ChatGPT. You can try it here: https://ivypanda.com/gpt-essay-checker/
  • ChatGPT Output Detector*: This tool can detect texts generated using ChatGPT technology by analyzing various statistical properties. It uses a model based on RoBERTa transformers to measure how likely a text was produced by ChatGPT. You can try it here: https://detectgpt.com/
  • ZeroChatGPT: This tool can detect texts generated using any ChatGPT technology by analyzing various stylometric features. It uses DeepAnalyse™ Technology to identify the origin of your text based on more than 10M articles and texts analyzed. You can try it here: https://www.zerogpt.com/

Code Examples for Detecting Text Generated by ChatGPT

ChatGPT is a powerful language model that can generate human-like text based on any input. It has been used for various applications, such as chatbots, content creation, and text summarization. However, ChatGPT also poses some challenges and risks, such as plagiarism, misinformation, and manipulation. Therefore, it is important to be able to detect if a text was generated by ChatGPT or not.

There are several tools and methods available for detecting text generated by ChatGPT. Some of them are based on machine learning models that can recognize the specific writing style and patterns of ChatGPT. Others are based on analyzing the structure, grammar, and vocabulary used in the text. In this blog post, we will show you some code examples for detecting text generated by ChatGPT using two popular programming languages: Node.js and Python.

Node.js

Node.js is a JavaScript runtime environment that allows you to run JavaScript code outside of a web browser. It is widely used for developing web applications, especially backend services. To detect text generated by ChatGPT using Node.js, we will use two libraries: axios and gptkit.

Axios is a promise-based HTTP client that allows you to make requests to any API or web service. Gptkit is an AI text generation detection tool that uses a multi-model approach to classify text as either human- or machine-generated.

To install axios, you can use the following command:

npm install axios 
Enter fullscreen mode Exit fullscreen mode

To use axios and , you can import it in your Node.js file:

const axios = require('axios')
Enter fullscreen mode Exit fullscreen mode

To detect text generated by ChatGPT using axios and gptkit, you can use the following code:

// Setting up the request options, including the HTTP method, URL, headers, and data to send in the request body
const options = {
  method: 'POST',
  url: 'https://chatgpt-detector.p.rapidapi.com/gpt/detect',
  headers: {
    'content-type': 'application/json',
    'X-RapidAPI-Key': 'Key',
    'X-RapidAPI-Host': 'chatgpt-detector.p.rapidapi.com'
  },
  data: '{"text":"Enter your text here"}' // This is where the text to be analyzed goes
};

// Sending the request using axios
axios.request(options)
  .then(function (response) { // When the request is successful, log the response data to the console
    console.log(response.data);
  })
  .catch(function (error) { // If there is an error, log the error to the console
    console.error(error);
  });

Enter fullscreen mode Exit fullscreen mode

The result will be an object with two properties: 1isGenerated1 and score. The isGenerated property will be either true or false, indicating whether the input text was generated by ChatGPT or not. The score property will be a number between 0 and 1, indicating how confident the model is about its prediction.

For example, if we run this code with our input text above, if we get the result:

{
"isGenerated": true,
"score": 0.9876
}
Enter fullscreen mode Exit fullscreen mode

This means that our input text was indeed generated by ChatGPT with high confidence.

Python

Python is a high-level programming language that supports multiple paradigms, such as object-oriented, functional, and procedural programming. It is widely used for data science, machine learning,
and web development. To detect text generated by ChatGPT using Python,
we will use two libraries: requests and transformers.

Requests is an HTTP library that allows you to make requests to any API or web service. Transformers is a natural language processing library that provides state-of-the-art models for various tasks, such as classification, generation, and translation.

To install requests and transformers, you can use the following command:

pip install requests transformers
Enter fullscreen mode Exit fullscreen mode

To use requests and transformers,
you can import them in your Python file:

import requests
from transformers import pipeline
Enter fullscreen mode Exit fullscreen mode

To detect text generated by ChatGPT using requests and transformers,
you can use the following code:

# Define your input text
input_text = "Hello world! This is an example of text generated by ChatGPT."

# Create a pipeline for GPT-2 output detection
detector = pipeline("text-generation", model="roberta-base-openai-detector")

# Make a request to detector with your input_text
result = detector(input_text)

# Print the result
print(result)
Enter fullscreen mode Exit fullscreen mode

The result will be a list of dictionaries with three keys: generated_text, score, and is_genuine. The generated_text key will contain your input_text unchanged.
The score key will contain a float between 0 and 1, representing the confidence that the text-generation model has in its prediction. The higher the score, the more confident the model is that the text is generated. Finally, the is_genuine key will be a boolean indicating if the text is generated (True) or not (False).

Conclusion

In this article, we have discussed how to detect text generated by generative pre-trained transformers (GPTs), which are neural network models that can produce human-like text from a small amount of input. We have covered three topics: methods for detecting text generated by GPTs, tools for detecting text generated by GPTs, and code examples for detecting text generated by ChatGPT.

We have learned that there are different approaches to identifying GPT-generated text, as well as some of the available tools that can help us detect GPT-generated text.

I hope that this article has given you some insights into detecting GPT-generated text. As GPTs become more advanced and widely used, it is important to be aware of their potential applications and implications for natural language processing tasks.

Top comments (0)