forked from FAC29A/Oskar-Fran-Phoebe
-
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.
- Loading branch information
Showing
3 changed files
with
99 additions
and
55 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,49 @@ | ||
require("dotenv").config(); | ||
const openai = require('openai'); | ||
const openai = require("openai"); | ||
const fetch = require("node-fetch"); | ||
const openaikey = process.env.OPENAI; | ||
//const openai = new OpenAI(); | ||
|
||
//openai.ChatCompletion.create() | ||
|
||
function openaiResponse(message) { | ||
const endpoint = 'https://api.openai.com/v1/chat/completions'; | ||
|
||
fetch(endpoint, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Authorization': `Bearer ${openaikey}`, | ||
}, | ||
body: JSON.stringify({ | ||
model: 'gpt-3.5-turbo', | ||
messages: [{ "role": "user", "content": `${message}`}], | ||
temperature: 0.7, | ||
}), | ||
}) | ||
.then(response => | ||
response.json()) | ||
.then(data => { | ||
|
||
console.log(data.choices[0].message.content); | ||
|
||
}) | ||
.catch(error => { | ||
// Handle errors here | ||
console.error('Error:', error); | ||
}); | ||
} | ||
|
||
const endpoint = "https://api.openai.com/v1/chat/completions"; | ||
|
||
// openaiResponse("why do cats purr?") | ||
return fetch(endpoint, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${openaikey}`, | ||
}, | ||
body: JSON.stringify({ | ||
model: "gpt-3.5-turbo", | ||
messages: [{ role: "user", content: `${message}` }], | ||
temperature: 0.7, | ||
}), | ||
}) | ||
.then((response) => { | ||
if (!response.ok) { | ||
throw new Error( | ||
`OpenAI API request failed with status ${response.status}` | ||
); | ||
} | ||
return response.json(); | ||
}) | ||
.then((data) => { | ||
if ( | ||
data.choices && | ||
data.choices.length > 0 && | ||
data.choices[0].message && | ||
data.choices[0].message.content | ||
) { | ||
return data.choices[0].message.content; | ||
} else { | ||
throw new Error("Unexpected response format from OpenAI API"); | ||
} | ||
}) | ||
.catch((error) => { | ||
console.error("Error in openaiResponse:", error); | ||
throw error; | ||
}); | ||
} | ||
|
||
module.exports = { openaiResponse }; | ||
module.exports = { openaiResponse }; |