import sys
from openai import OpenAI
# Read the lines from the text file
#with open(r'C:\Users\sirdni\Documents\GitHub\birthday-card\flashcards\testing.txt', 'r') as file:
with open(sys.argv[1], 'r') as file:
lines = file.readlines()
# Construct the prompt
prompt = "I'll give you lines that have commas in them. Everything to the left of the comma is a question and everything to the right of it is the correct answer to the question. Generate me 3 false answer choices not including the correct answer for a kahoot that are similar to the correct answers. Put it in the format 'Answer, Answer, Answer' for each line. You shouldn't have bullet points or anything else, just comma separated per line. Don't have the 'question' on the same line either. Also, don't have the explanation like 'Sure, here are the questions and answers with 3 similar but incorrect answer choices for each:' Just include the answer choices.\n\n"
# Append each line from the file to the prompt
for line in lines:
prompt += line.strip() + "\n"
client = OpenAI(
# This is the default and can be omitted
api_key=sys.argv[2],
)
chat_completion = client.chat.completions.create(
messages=[
{
"role": "user",
"content": prompt,
}
],
model="gpt-3.5-turbo",
)
generated_text = chat_completion.choices[0].message.content
# Print the generated text
print(generated_text)```
#How would I convert this to Javascript?
1 messages · Page 1 of 1 (latest)
whats the issue?
https://platform.openai.com/docs/quickstart?context=node
Select node.js
`import OpenAI from "openai";
const openai = new OpenAI();
async function main() {
const completion = await openai.chat.completions.create({
messages: [{ role: "system", content: "You are a helpful assistant." }],
model: "gpt-3.5-turbo",
});
console.log(completion.choices[0]);
}
main();`
I don't technically have an issue but it's a python script and I'm not sure how I would implement it in java
wait so you can just do that in js? like that?
I code in python, but I forget how to run it I always references https://platform.openai.com/docs/quickstart?context=node
It's a simple "how to start" tutorial
Always reference the doc and press node.js
chatpgt is unreliable since it is trained on old data
Even if I give it recent code, it changes my code to the old one that no longer works. But their doc is pretty good (except of some response json that hasn't been updated)
gpt-4 response
`import OpenAI from "openai";
import fs from "fs";
import process from "process";
// Make sure to replace 'YOUR_API_KEY' with your actual OpenAI API key
const openai = new OpenAI(process.argv[2]);
async function generateTextFromFile(filePath) {
try {
// Read lines from the text file
const lines = fs.readFileSync(filePath, 'utf8').split('\n');
// Construct the prompt
let prompt = "I'll give you lines that have commas in them. Everything to the left of the comma is a question and everything to the right of it is the correct answer to the question. Generate me 3 false answer choices not including the correct answer for a kahoot that are similar to the correct answers. Put it in the format 'Answer, Answer, Answer' for each line. You shouldn't have bullet points or anything else, just comma separated per line. Don't have the 'question' on the same line either. Also, don't have the explanation like 'Sure, here are the questions and answers with 3 similar but incorrect answer choices for each:' Just include the answer choices.\n\n";
lines.forEach(line => {
if (line.trim() !== '') {
prompt += line.trim() + "\n";
}
});
// Call OpenAI API with the constructed prompt
const completion = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
messages: [{
role: "user",
content: prompt,
}],
});
// Print the generated text
console.log(completion.choices[0].message.content);
} catch (error) {
console.error("An error occurred:", error);
}
}
// Make sure to pass the file path as the first argument and API key as the second argument when running this script
generateTextFromFile(process.argv[3]);
`
thank you! I'm trying it right now
could you possibly ask it how you would embed it within an html file and instead of specifying the file type and api key, select it via input fields?