Getting Started with the OpenAI API and Node.js/JavaScript

generally we used davinci,curie and other engines but you use ```
content-filter-alpha-c4

where it mentions

You can find more details in the OpenAI docs here

I’m working on a system for generating technical tutorial videos. So, I thought I’d use it to generate some tutorials on using the OpenAI API. I’d love to know what you think. Is this helpful at all?

3 Likes

Thanks, and, that’s a great point. I need to generate the step-by-step for the README.md also.

1 Like

Steve, this is outstandingly helpful for a non-technical person. I really appreciate it. (And the book is outstanding too - I’m on page 37 so far.)

I used Replit and followed your step-by-step instructions for a file upload and I succeeded in uploading a simple json lines file, and changing the purpose to search. My file-upload.js includes the following (slightly modified for search and for the name of my json lines file):

// 2. Get the data file ready for the http post
const data = new FormData();
data.append(‘purpose’, ‘search’);
data.append(‘file’, fs.createReadStream(‘les-search-test.jsonl’));
// 3. Set http request parameters for axios
const params = {
method: ‘post’,
url: ‘https://api.openai.com/v1/files’,
headers: {
‘Authorization’: 'Bearer ’ + process.env.OPENAI_API_KEY,
…data.getHeaders()
},
query: ‘where do fish go’,
max_rerank: ‘5’,
search_model: ‘davinci’,
file: ‘les-search-test.jsonl’,
data : data
};

As you can see, I added to your sample code a query, the search_model, the name of my json lines file, and max_rerank. I don’t know for sure if my changes to the code are correct, but I am getting the following run results:

node file-upload.js
{“id”:“file-1nZxVbD6uvjjsAb5RR3tuWVn”,“object”:“file”,“bytes”:275,“created_at”:1626806729,“filename”:“les-search-test.jsonl”,“purpose”:“search”,“status”:“uploaded”,“status_details”:null}

So far so good?

Your next instructions say “the id value is what you’ll need from the response to use the file with the search, classifications, or answers endpoint.”

I’m now stuck. I also followed your instructions for simple search and that worked really well. (And GPT-3’s results were brilliant, as they’ve been in the playground so far). On Replit, it’s the combination of using the search endpoint and uploading a json lines file that I can’t seem to figure out. Your help would be greatly appreciated. Thanks very much Steve.

Hi @lmccallum, I’m really happy to hear this post and the book are helpful. Thanks so much for the feedback!

If I understand your question correctly, I think you just need to make sure to pass the file parameter, rather than the documents parameter when you call the search endpoint. The value for the file parameter should be the file name that was returned after running file-upload.js. Does that make sense?

Here is a link to the OpenAI documentation on the file parameter. I hope this helps!

2 Likes

I am getting additional help from your book. I’m on page 101 so far. Do I get a prize if I’m the first person to read it cover to cover? :rofl:

Of course you get a prize! It’s knowledge. :wink:

2 Likes

Great work! Is it possible const axios = require(‘axios’); is missing in this example?

Hi @tobias.laemmle, it looks like the code you’re showing is from the .replit file. You don’t need to include require('axios') in that file. Just the code you have is all that is needed. Are you running into an issue getting the example to execute correctly?

This is awesome @stevet! I was actually trying to figure out how to implement axios into the Django webserver I’m developing specifically for GPT-3 and was hitting some roadblocks until I came across this!

This helped since the desired behavior of the system is to post the data without refreshing the page. Axios is still fairly new to me but I’m getting the hang of it. :smile:

Once the project is complete, I’ll be releasing it to the OpenAI community for all to enjoy. It’s a Dockerized full-stack web server with all the gadgets and gizmos, so it’ll be quite the showcase to add to my portfolio once it’s complete! If you’ve got a feature that you’d like to have added to it, definitely message me with you idea(s) and I’ll be more than happy to try and put it in!

Thanks again @stevet for your help!!

2 Likes

Thanks @DutytoDevelop! I’m glad to hear this was helpful. Keep me posted on your project, I’d love to check it out when it’s ready. Thanks again!

1 Like

@stevet Do we have to run node twice? First time for uploading the file and getting the file parameter, then again to run .replit ?

That’s how I got it working and results are satisfactory.

Quick question: why not a python app for those last chapters in your book?

Hi there @lmccallum

Were you able to successfully run the app? I did. but I am new to replit.

So I went like,

node upload.js

Then, I get the file id.

console.log(response.data['id']);

Then I copy-paste that value here (that’s what I understood from @stevet reply to you).

const data = {
    "file": "file-RJZYs4rA2doCD0MuCaLT1bHr",
    //"documents": documents,
    "question": req.body.question,
    "search_model": "ada",
    ...

And then RUN .replit.

However, I thought I could assign a value right in upload.js.

process.env.ANSWERS_FILE = response.data['id'];

So that,

const data = {
    "file": process.env.ANSWERS_FILE,
    ...

If I can make it work, perhaps upload.js could be executed on page load?

1 Like

Never mind. I am reading the book, and did it the proper way hahaha

2 Likes

Hey @jose glad to hear you got through it with the book! Let me know if you have any other questions as you work through it. Thanks!

@dandrade.jose you can use the process.env.ANSWERS_FILE aproach but in replit you need to use the ‘run’ button to run the code rather than using node upload.js from the console. So, you’d update the file named .replit to:

run = "node upload.js"

and then when you click the run button the environment variables will work with the code. Does that make sense?

1 Like

Hi, i found your Answer tutorial on youtube and followed the instruction - which I must thank you for providing an UI for our trying out.

I used the upload.js for answers.jsonl and added a secret Key as instructed in github - where it says to use ‘file name’ as value.

I got a 404.
I followed the tutorial here, and tried to use file id as value for the secret key.
got a 404 too.

Can u please tell me what i am doing wrong?

Also, should I activate the line in answers.js?

const data = {
// “file”: process.env.ANSWERS_FILE,
“documents”: documents,

Thank you very much in advance.

1 Like

I was on the same boat, just learn that the answers endpoint has been deprecated, this is the new suggested approach - Question answering using embeddings-based search | OpenAI Cookbook