-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added chat.js, removed key from static site
- Loading branch information
Showing
3 changed files
with
34 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
async function getChatGPTResponse(prompt) { | ||
const url = `https://mooncalendar.glitch.me/api/chatgpt`; | ||
|
||
try { | ||
const response = await fetch(url, { | ||
method: "POST", | ||
headers: { "Content-Type": "application/json" }, | ||
body: JSON.stringify({ prompt }) | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Error: ${response.statusText}`); | ||
} | ||
|
||
const data = await response.json(); | ||
console.log("ChatGPT response:", data.response); | ||
|
||
return data.response; // Return the chatbot's response | ||
} catch (error) { | ||
console.error("Failed to get response from ChatGPT:", error); | ||
} | ||
|
||
return null; // Default to null if fetching fails | ||
} | ||
|
||
// Example Usage | ||
getChatGPTResponse("Tell me a joke about programming.").then(response => { | ||
if (response) { | ||
console.log("ChatGPT says:", response); | ||
} | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters