Last active
May 15, 2023 15:00
-
-
Save caiomoura1994/be05ef6b859db8efb2f5339339a911bb to your computer and use it in GitHub Desktop.
Chatgpt Company History
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
// Import the necessary libraries | |
const express = require('express'); | |
const bodyParser = require('body-parser'); | |
const openai = require('openai'); | |
const axios = require('axios'); | |
// Configure the OpenAI library | |
openai.apiKey = 'your-openai-api-key'; | |
// Create an Express application object | |
const app = express(); | |
// Use the bodyParser middleware to parse JSON requests | |
app.use(bodyParser.json()); | |
// Create an endpoint to receive search requests | |
app.post('/search', async (req, res) => { | |
// Get the company history from the other API | |
const companyHistory = await getCompanyHistory(); | |
// Concatenate the employee's question with the company history context | |
const prompt = `${companyHistory}\n\n${req.body.question}`; | |
// Use the OpenAI API to process the text | |
const response = await openai.completion.create({ | |
engine: 'text-davinci-002', | |
prompt, | |
maxTokens: 1024, | |
n: 1, | |
stop: ['\n'] | |
}); | |
// Get the response generated by ChatGPT | |
const chatGPTResponse = response.choices[0].text; | |
// Send the final response to the employee | |
res.json({ response: chatGPTResponse }); | |
}); | |
// Function to get the company history from the other API | |
async function getCompanyHistory() { | |
const url = 'https://your-other-api.com/history'; | |
const response = await axios.get(url); | |
return response.data.history; | |
} | |
app.post('/generate-response', async (req, res) => { | |
const prompt = req.body.prompt; | |
const model = req.body.model; // Replace with your model identifier | |
const response = await openai.complete({ | |
prompt, | |
model | |
}); | |
const generatedResponse = response.choices[0].text; | |
res.json({ response: generatedResponse }); | |
}); | |
// Start the application server on port 3000 | |
app.listen(3000, () => { | |
console.log('Server started on port 3000'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment