diff --git a/.env.example b/.env.example index 4d4e2433a..6c1cbbfe3 100644 --- a/.env.example +++ b/.env.example @@ -3,6 +3,7 @@ REACT_APP_SENTRY_DSN='' REACT_APP_SENTRY_ENV='local' PUBLIC_URL='http://localhost:3011' ASSETS_URL='http://localhost:3011' +REACT_APP_GEMINI_API_KEY='' REACT_APP_GOOGLE_TAG_MANAGER_ID='' REACT_APP_API_ENDPOINT='http://localhost:3009' REACT_APP_PLAUSIBLE_DATA_DOMAIN='' diff --git a/src/utils/aiClient.js b/src/utils/aiClient.js new file mode 100644 index 000000000..b5ce33cce --- /dev/null +++ b/src/utils/aiClient.js @@ -0,0 +1,53 @@ +/** + * List available Gemini models for the given API key. + * @param {string} apiKey - Your Gemini API key + * @returns {Promise} The response from the ListModels endpoint + */ +export async function listGeminiModels(apiKey) { + const url = "https://generativelanguage.googleapis.com/v1beta/models"; + const response = await fetch(url, { + method: "GET", + headers: { + "Content-Type": "application/json", + "x-goog-api-key": apiKey, + }, + }); + if (!response.ok) { + throw new Error( + `ListModels failed: ${response.status} ${response.statusText}`, + ); + } + return response.json(); +} +// src/utils/aiClient.js + +// Helper utility to connect to Gemini (Google AI) and send a request. Easily swappable for other models. +export async function sendGeminiRequest({ + prompt, + apiKey, + model = "gemini-2.5-pro", + options = {}, +}) { + const url = `https://generativelanguage.googleapis.com/v1beta/models/${model}:generateContent`; + const body = { + contents: [{ parts: [{ text: prompt }] }], + ...options, + }; + const response = await fetch(url, { + method: "POST", + headers: { + "Content-Type": "application/json", + "x-goog-api-key": apiKey, + }, + body: JSON.stringify(body), + }); + if (!response.ok) { + throw new Error( + `AI request failed: ${response.status} ${response.statusText}`, + ); + } + const data = await response.json(); + return data; +} + +// To switch models, just change the 'model' argument or the API URL logic above. diff --git a/src/utils/prompts/propose_solution_prompt.txt b/src/utils/prompts/propose_solution_prompt.txt new file mode 100644 index 000000000..01b52fe97 --- /dev/null +++ b/src/utils/prompts/propose_solution_prompt.txt @@ -0,0 +1,15 @@ +You are an expert computer science teacher and will be given a student task and piece of student python code written in response to the task. + +You will return a correct solution to the task based on the student's code in the form of a git diff, making as few changes to the student code as possible. + +Don't change the whitespace unless necessary for the code to run. + +You will also provide an explanation of why the student needs to make the changes to fulfil the task. The explanation must not be longer than 30 words. + +Task: + +{{task}} + +Student code: + +{{student_code}} \ No newline at end of file