Skip to content

Instantly share code, notes, and snippets.

@xsuchy
Created April 17, 2025 09:52
Show Gist options
  • Save xsuchy/4d888912fecd4da63b06262b271a3b4b to your computer and use it in GitHub Desktop.
Save xsuchy/4d888912fecd4da63b06262b271a3b4b to your computer and use it in GitHub Desktop.
Kontrola gramatiky
#!/usr/bin/python3
"""
AI prompt:
You are Python and ChatGPT expert. Can you write a Python script that reads a text from STDIN and submits it to ChatGPT to find any grammatical errors and correct them? No stylistic changes should be made to ensure the differences are minimal. The resulting text will be printed.
"""
import os
import sys
from openai import OpenAI
# Retrieve your API key from the environment
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
sys.exit("Error: please set the OPENAI_API_KEY environment variable.")
client = OpenAI(api_key=api_key)
text = sys.stdin.read()
if not text.strip():
sys.exit("Error: no input detected on stdin.")
# Call the ChatCompletion endpoint
response = client.chat.completions.create(
model="gpt-4.1", # see https://platform.openai.com/settings/organization/limits
temperature=0.0, # deterministically only fixes grammar
messages=[
{
"role": "system",
"content": (
"Jsi asistent pro opravu gramatiky. "
"Opravuješ pouze gramatické chyby a překlepy."
"NEprováděj žádné další stylistické ani formulační změny."
)
},
{
"role": "user",
"content": (
"V následujícím textu opravte pouze gramatické chyby ("
"minimální změny). Nepřeformulovávej ani neměň styl::\n\n"
+ text
)
}
]
)
# Extract and print the corrected text
corrected = response.choices[0].message.content.strip()
print(corrected)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment