#Keep on getting a 'TypeError response.json is not a function' Error
5 messages · Page 1 of 1 (latest)
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const path = require('path');
const app = express();
const port = 5000;
app.use(bodyParser.json());
app.use(cors());
app.use(express.static('public'));
process.env.NODE_ENV = 'production';
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: 'my api key',
});
const openai = new OpenAIApi(configuration);
app.post('/completions', async (req, res) => {
try {
const { prompt } = req.body;
const response = await openai.createCompletion({
model: "text-davinci-003",
prompt: "Say this is a test.",
max_tokens: 150,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0.6,
stop: [" Human:", " AI:"],
});
const data = await response.json();
response.json({ completion: data.choices[0].text });
}
catch (error) {
console.log(error);
res.status(500).json({ error: error.message });
}
});
app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
});
app.post('/completions', (req, res) => {
console.log('Received request to /completions');
// ...
});
This is my server.js file
And this is my app.js file
import React, { useState } from 'react';
import axios from 'axios';
const App = () => {
const [prompt, setPrompt] = useState('');
const [completion, setCompletion] = useState('');
const handleSubmit = async (event) => {
event.preventDefault();
const response = await axios.post('/completions', { prompt });
setCompletion(response.data.completion);
};
return (
<div>
<form onSubmit={handleSubmit}>
<label htmlFor="prompt">Prompt:</label>
<input
type="text"
id="prompt"
value={prompt}
onChange={(event) => setPrompt(event.target.value)}
/>
<button type="submit">Generate completion</button>
</form>
</div>);}
export default App;