From 61960f3a8678d8834180ff92109be791444cde53 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 2 Jul 2026 02:23:01 +0000 Subject: [PATCH 1/2] Add OpenAI env config, .gitignore update, and ChatGPT connection script --- .env.example | 2 ++ .gitignore | 3 +++ Dockerfile | 3 +++ README.md | 38 ++++++++++++++++++++++++++++++++---- chat.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 2 ++ 6 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 .env.example create mode 100644 chat.py create mode 100644 requirements.txt diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..90a7f44 --- /dev/null +++ b/.env.example @@ -0,0 +1,2 @@ +# Skopiuj ten plik jako .env i uzupełnij swój klucz API OpenAI +OPENAI_API_KEY=sk-...twój_klucz_tutaj... diff --git a/.gitignore b/.gitignore index a541ffe..62308b6 100644 --- a/.gitignore +++ b/.gitignore @@ -24,3 +24,6 @@ htmlcov/ # OS files .DS_Store Thumbs.db + +# Environment variables (secrets) +.env diff --git a/Dockerfile b/Dockerfile index 826307e..a952f85 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,4 +6,7 @@ RUN apt-get update \ && apt-get install -y --no-install-recommends git ca-certificates \ && rm -rf /var/lib/apt/lists/* +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt + CMD ["bash"] diff --git a/README.md b/README.md index c6b2958..3078a37 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,33 @@ # Repozytorium Bazowe -To repozytorium zawiera podstawową konfigurację startową: -- `.gitignore` dla typowych plików lokalnych i cache narzędzi. +To repozytorium zawiera podstawową konfigurację startową wraz z połączeniem do OpenAI ChatGPT API: +- `.gitignore` dla typowych plików lokalnych, cache narzędzi i pliku `.env`. +- `.env.example` — szablon zmiennych środowiskowych (skopiuj jako `.env` i uzupełnij klucz API). - `Dockerfile` do uruchamiania środowiska roboczego w kontenerze. - `.dockerignore` ograniczający kontekst buildu obrazu. +- `requirements.txt` z zależnościami (`openai`, `python-dotenv`). +- `chat.py` — prosty klient ChatGPT w trybie konwersacji. + +## Konfiguracja klucza API OpenAI + +1. Skopiuj plik przykładowy: + ```bash + cp .env.example .env + ``` +2. Otwórz `.env` i wstaw swój klucz API: + ``` + OPENAI_API_KEY=sk-...twój_prawdziwy_klucz... + ``` + Klucz możesz wygenerować na stronie: https://platform.openai.com/api-keys + +> **Uwaga:** plik `.env` jest dodany do `.gitignore` — nie zostanie przypadkowo opublikowany w repozytorium. + +## Uruchomienie czatu (lokalnie) + +```bash +pip install -r requirements.txt +python chat.py +``` ## Uruchomienie kontenera @@ -13,10 +37,16 @@ Zbuduj obraz: docker build -t codex-workspace . ``` -Uruchom kontener: +Uruchom kontener przekazując plik `.env`: + +```bash +docker run --rm -it --env-file .env -v "$(pwd):/workspace" codex-workspace +``` + +Następnie w kontenerze: ```bash -docker run --rm -it -v "$(pwd):/workspace" codex-workspace +python chat.py ``` ## Klonowanie zewnętrznego repozytorium (opcjonalnie) diff --git a/chat.py b/chat.py new file mode 100644 index 0000000..c0d9025 --- /dev/null +++ b/chat.py @@ -0,0 +1,50 @@ +""" +Prosty klient ChatGPT korzystający z OpenAI API. + +Użycie: + python chat.py + +Wymagania: + - Plik .env z ustawionym OPENAI_API_KEY (patrz .env.example) + - Zainstalowane zależności: pip install -r requirements.txt +""" + +import os +from openai import OpenAI +from dotenv import load_dotenv + +load_dotenv() + +api_key = os.getenv("OPENAI_API_KEY") +if not api_key: + raise EnvironmentError( + "Brak klucza API. Ustaw OPENAI_API_KEY w pliku .env (patrz .env.example)." + ) + +client = OpenAI(api_key=api_key) + + +def chat(message: str, model: str = "gpt-4o-mini") -> str: + """Wyślij wiadomość do ChatGPT i zwróć odpowiedź.""" + response = client.chat.completions.create( + model=model, + messages=[{"role": "user", "content": message}], + ) + return response.choices[0].message.content + + +def main() -> None: + print("Połączono z ChatGPT. Wpisz 'wyjście' aby zakończyć.\n") + while True: + user_input = input("Ty: ").strip() + if user_input.lower() in {"wyjście", "exit", "quit"}: + print("Do widzenia!") + break + if not user_input: + continue + reply = chat(user_input) + print(f"ChatGPT: {reply}\n") + + +if __name__ == "__main__": + main() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..75c8117 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +openai>=1.0.0 +python-dotenv>=1.0.0 From 8ff342fa3be25056f0c5a00075f1be9d75c1bef3 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 2 Jul 2026 02:23:52 +0000 Subject: [PATCH 2/2] Address code review: add API response validation and error handling in chat loop --- .env.example | 2 +- chat.py | 8 +++++++- requirements.txt | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/.env.example b/.env.example index 90a7f44..630be49 100644 --- a/.env.example +++ b/.env.example @@ -1,2 +1,2 @@ # Skopiuj ten plik jako .env i uzupełnij swój klucz API OpenAI -OPENAI_API_KEY=sk-...twój_klucz_tutaj... +OPENAI_API_KEY=sk-...twój_klucz_tutaj... \ No newline at end of file diff --git a/chat.py b/chat.py index c0d9025..f92dd3d 100644 --- a/chat.py +++ b/chat.py @@ -30,6 +30,8 @@ def chat(message: str, model: str = "gpt-4o-mini") -> str: model=model, messages=[{"role": "user", "content": message}], ) + if not response.choices or response.choices[0].message.content is None: + raise ValueError("API zwróciło pustą odpowiedź.") return response.choices[0].message.content @@ -42,7 +44,11 @@ def main() -> None: break if not user_input: continue - reply = chat(user_input) + try: + reply = chat(user_input) + except Exception as exc: + print(f"Błąd API: {exc}\n") + continue print(f"ChatGPT: {reply}\n") diff --git a/requirements.txt b/requirements.txt index 75c8117..a881125 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,2 @@ openai>=1.0.0 -python-dotenv>=1.0.0 +python-dotenv>=1.0.0 \ No newline at end of file