Hey @Steph you’re right! Typo that slipped by me. I fixed it in the example code. Thanks so much for pointing that out!
Also if you are trying out this using a simple js file with no backend. Axion is globally defined once you add their cdn script on your html
You need to include this before the line where you are importing your js file
Then you really dont need to add this
const axios = require(‘axios’);
Just comment it out and all works well.
Hope this helps
Classifications Endpoint
This example show how to call the classifications endpoint using using Node.js/JavaScript. For details about the classifications endpoint see the OpenAI Docs.
NOTE: This tutorial is part of a series. If you have not read the getting started post you should check that one out first.
Steps
- log in to Replit.com
- Open the
openai-examples-noderepl that you created in the getting started tutorial. - Create a new file named
classifications-endpoint.js - Copy the following code into the classifications-endpoint.js file.
const axios = require('axios');
const examples = [
["The service was super quick. I love that.","Good"],
["Would not go back.","Poor"],
["I tried the chicken and cranberry pizza...mmmm!","Good"],
["There were no signs indicating cash only!","Poor"],
["I was disgusted. There was a hair in my food.","Poor"],
["The waitress was a little slow but friendly.","Neutral"]
]
const client = axios.create({
headers: {
'Authorization': 'Bearer ' + process.env.OPENAI_API_KEY
}
});
const endpoint = "https://api.openai.com/v1/classifications";
const params = {
"query": "I'm never going to this place again",
"examples": examples,
"model": "curie"
}
client.post(endpoint, params)
.then(result => {
console.log(params.query + '\nLABEL:' + result.data.label);
}).catch(err => {
console.log(err);
});
- Update the
.replitfile with the following
language = "nodejs"
run = "node classifications-endpoint.js"
- Review the results in the console pane. You should see a result similar to the following example.
I'm never going to this place again
LABEL:Poor
The examples array is being used for the example classifications. You can change the examples and labels in that array, along with the query to try different classifications.
Let me know if you have any questions!
File Upload
This example show how to upload a jsonl file to the Files endpoint using using Node.js/JavaScript. This example is posting a file for the classifications endpoint. You can change the file purpose in step 2 of the code below. The purpose can be, search, answers, or classifications. See the see the OpenAI Docs for details.
For the example code to work, you’ll also need to create a file named reviews.jsonl and add data for the classifications endpoint. Here is some sample data you can use for testing.
{"text": "i love this place", "label": "Good", "metadata": {"id":"1"}}
{"text": "i hate this place", "label": "Poor", "metadata": {"id":"2"}}
{"text": "i have no opinion", "label": "Neutral", "metadata": {"id":"3"}}
NOTE: This tutorial is part of a series. If you have not read the getting started post you should check that one out first.
Steps
-
log in to Replit.com
-
Open the
openai-examples-noderepl that you created in the getting started tutorial. -
Create a new file named
file-upload.js -
Copy the following code into the file-upload.js file.
// 1. Require a few libraries that will be used
const fs = require('fs');
const axios = require('axios');
const FormData = require('form-data');
// 2. Get the data file ready for the http post
const data = new FormData();
data.append('purpose', 'classifications');
data.append('file', fs.createReadStream('reviews.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()
},
data : data
};
// 4. Call the openai api and log results to the console
axios(params)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
});
- Update the
.replitfile with the following
language = "nodejs"
run = "node file-upload.js"
- Review the results in the console pane. You should see a result similar to the following example.
{
"id":"file-ZMHNItHRzrkYbIf19mutOvlR",
"object":"file",
"bytes":215,
"created_at":1618832808,
"filename":"reviews.jsonl",
"purpose":"classifications",
"status":"uploaded",
"status_details":null
}
The id value is what you’ll need from the response to use the file with the search, classifications, or answers endpoint.
Let me know if you have any questions!
Hi @m-a.schenk - that is a very good point and one I need to make clearer. The .replit file is used to create an entry point for the app with a run command for example:
run = "node index.js"
If your .replit file contained the previous command and your index.js file called the API using a key stored as an environment variable - YES the request will use your key.
However, only the file referenced in the run command within the .replit file can be run on a public REPL. So, I would recomend making sure that the run command in the .replit file dosen’t contain code that calls the API (other than when you’re testing), or sign up for a Replit.com paid account that lets you have private repls. I think that is like $7/month.
Thanks again for bringing this up - it’s a very important point and I’m going to clarify that in the post about using replit.
My pleasure and no worries at all. Having an input for the API key that doesn’t store the value might be a good way to go.
Looks like replit.com has changed the behavior on how the repl works now. Seems they don’t run with env variables you setup anymore unless you’re sharing an application.
thanks this helps a lot
generally we used davinci,curie and other engines but you use ```
content-filter-alpha-c4
where it mentions
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?
Thanks, and, that’s a great point. I need to generate the step-by-step for the README.md also.
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!
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? 
Of course you get a prize! It’s knowledge. 
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?