Wikipedie:GPT urgentní upravovač

Z Wikipedie, otevřené encyklopedie

Následující program v Pythonu 3 je založený na umělé inteligenci. Projde zadaný hrubě nekvalitní článek české Wikipedie (jeho jméno zapište přímo do programu, jde o "holý" program bez uživatelského rozhraní) a program vypíše jeho zdrojový kód po úpravách k lepšímu. Nezapomeňte po něm vše zkontrolovat.

Protože program přes API provolává model řady ChatGPT, je potřeba mít na stránkách firmy OpenAI zakoupeno právo model takto používat (https://openai.com/api/).

"""Vezme článek na úrovni k urgentní úpravě a pokusí se ho dostat na přijatelnější tvar"""
import requests
from openai import OpenAI
client = OpenAI(api_key=my_api_key)   # za my_api_key dosadit klíč od firmy OpenAI - lze ho zakoupit na jejich webu

# Vstup:
article_name = "Crush 40"  # jméno článku v uvozovkách  


def get_wikipedia_source(article_title, language="cs"):
    """Gets the source code of a Wikipedia article"""
    # Define the endpoint and parameters
    endpoint = f"https://{language}.wikipedia.org/w/api.php"
    params = {
        "action": "query",
        "format": "json",
        "prop": "revisions",
        "titles": article_title,
        "rvprop": "content",
        "rvslots": "main"
    }

    # Make the request to the Wikipedia API
    response = requests.get(endpoint, params=params)
    
    if response.status_code != 200:
        raise Exception(f"Error fetching data from Wikipedia API: {response.status_code}")

    data = response.json()

    # Extract the page ID, as the structure of the response contains dynamic page IDs
    pages = data.get("query", {}).get("pages", {})
    
    if not pages:
        raise Exception("No pages found or an error occurred.")

    page_id = next(iter(pages))  # Get the first (and likely only) page ID key

    # Extract the content of the page
    page = pages[page_id]
    revisions = page.get("revisions", [])
    
    if not revisions:
        raise Exception("No revisions found for this page.")

    content = revisions[0].get("slots", {}).get("main", {}).get("*", "")
    
    return content

 
def uu(article_name):
    """Urgentně upraví článek"""
    
    vzor = get_wikipedia_source("Johan Thomas Lundbye")
    
    prompt = f"The goal is to improve a Czech Wikipedia article about {article_name}"
    prompt += """ so that the basic criteria of quality are met.
             * Do not change things that are acceptable
             * Use Wikimedia markup
             * Create the usual structure of Wikipedia article (chapters, infobox,...)
             * The article has an introduction which starts with its title (bold) and definition.
             * In articles with more chapters, the introduction summarizes the main facts explained later in the text 
             * The chapters are correctly marked by == Chapter name ==, possibly having === Subsections === (do
             not use another marking)
             * Sparsely add internal links, e.g. [[Praha|Prahy]], where appropriate
             * The article has at least one appropriate Kategorie (category), e.g. [[Kategorie:Norští spisovatelé]]
[[Kategorie:Wikipedie:Umělá inteligence]]
             in an article about a Norwegian author
             * Do not use ''italics'' and '''bold''' without reason
             * Keep the references and/or literature, but place and format them correctly
             * Correct the gross language errors but do not change the wording without strong reason
             * Do not omit relevant information, do not drop references
             * If the article or one of its chapters is too short or lacks an important information,
             it may be expanded
             An example of an acceptable short article may be this about Johan Thomas Lundbye, whose code looks like this:\n"""
    prompt += vzor
    prompt += f"\nAnd here is the code of the article about {article_name}, which should be improved:\n"
    prompt += get_wikipedia_source(article_name)
    
    response = client.chat.completions.create(
        model="gpt-4o",    
        max_tokens=4096,
        messages=[
                {
                  "role": "user",
                  "content": prompt 
                },]
              )
    msgtext = response.choices[0].message.content
    print(msgtext)
    return msgtext
    
    
uu(article_name)