diff --git a/.devcontainer/install-dependencies.sh b/.devcontainer/install-dependencies.sh old mode 100644 new mode 100755 diff --git a/.env.example b/.env.example index 35db1dd..1ee433e 100644 --- a/.env.example +++ b/.env.example @@ -63,3 +63,6 @@ AWS_BUCKET= AWS_USE_PATH_STYLE_ENDPOINT=false VITE_APP_NAME="${APP_NAME}" + +# OpenAI API Key for Chat Helper +OPENAI_API_KEY= diff --git a/CHAT_HELPER_README.md b/CHAT_HELPER_README.md new file mode 100644 index 0000000..caa6541 --- /dev/null +++ b/CHAT_HELPER_README.md @@ -0,0 +1,41 @@ +# Chat Helper Feature + +This feature adds a simple chat interface where users can ask questions about PHP and web development, and get responses from an AI assistant. + +## Features + +- Simple chat interface with plain HTML and JavaScript +- No authentication required - all messages are public +- Integration with OpenAI's GPT model for intelligent responses +- Focused on PHP and web development questions + +## Setup + +1. Make sure you have an OpenAI API key. If you don't have one, you can get it from [OpenAI's website](https://platform.openai.com/). + +2. Add your OpenAI API key to the `.env` file: + ``` + OPENAI_API_KEY=your_api_key_here + ``` + +3. The chat helper is available at the `/chat-helper` route. + +## How It Works + +1. Users can visit the `/chat-helper` page without authentication +2. They can type their PHP or web development questions in the input field +3. The question is sent to the server, which forwards it to OpenAI's API +4. The AI response is displayed in the chat interface + +## Technical Implementation + +- The feature uses a simple Blade template for the frontend (no React) +- JavaScript fetch API is used to communicate with the backend +- The backend uses Laravel's HTTP client to communicate with OpenAI +- The AI is instructed to only answer PHP and web development questions + +## Limitations + +- The AI will respond with "I can't help you" for questions not related to PHP or web development +- All messages are public and visible to everyone +- There's no message history persistence (messages are lost on page refresh) diff --git a/app/Http/Controllers/ChatHelperController.php b/app/Http/Controllers/ChatHelperController.php new file mode 100644 index 0000000..c441395 --- /dev/null +++ b/app/Http/Controllers/ChatHelperController.php @@ -0,0 +1,74 @@ +validate([ + 'message' => 'required|string|max:1000', + ]); + + $userMessage = $request->input('message'); + + // Prepare the prompt for OpenAI + $prompt = "You are PHP developer assistant. Answer only to questions related to PHP and Web development. Answer shortly. If question is not relevant answer with one phrase \"I can't help you\". There is the user message: \n$userMessage"; + + try { + // Get API key from environment + $apiKey = env('OPENAI_API_KEY'); + + if (!$apiKey) { + return response()->json([ + 'error' => 'OpenAI API key not configured' + ], 500); + } + + // Call OpenAI API + $response = Http::withHeaders([ + 'Authorization' => 'Bearer ' . $apiKey, + 'Content-Type' => 'application/json', + ])->post('https://api.openai.com/v1/chat/completions', [ + 'model' => 'gpt-3.5-turbo', + 'messages' => [ + [ + 'role' => 'system', + 'content' => 'You are PHP developer assistant. Answer only to questions related to PHP and Web development. Answer shortly. If question is not relevant answer with one phrase "I can\'t help you".' + ], + [ + 'role' => 'user', + 'content' => $userMessage + ] + ], + 'temperature' => 0.7, + ]); + + if ($response->successful()) { + $aiResponse = $response->json()['choices'][0]['message']['content'] ?? 'Sorry, I could not generate a response.'; + + return response()->json([ + 'message' => $userMessage, + 'response' => $aiResponse + ]); + } else { + return response()->json([ + 'error' => 'Failed to get response from OpenAI: ' . ($response->json()['error']['message'] ?? 'Unknown error') + ], $response->status()); + } + } catch (\Exception $e) { + return response()->json([ + 'error' => 'An error occurred: ' . $e->getMessage() + ], 500); + } + } +} diff --git a/resources/views/chat-helper.blade.php b/resources/views/chat-helper.blade.php new file mode 100644 index 0000000..41909f7 --- /dev/null +++ b/resources/views/chat-helper.blade.php @@ -0,0 +1,218 @@ + + +
+ + + +