To use the OpenAI API in JavaScript, you can use the fetch
method or the axios
library to make HTTP requests and interact with the API.
Here's an example using fetch
:
index.tsx493 chars18 lines
In this example, we're using the Davinci Codex engine to generate text completions based on the prompt "Hello, my name is". We're setting the max_tokens
to 5 and the temperature
to 0.5 to control the length and randomness of the generated text. We're also including an Authorization
header with our API key.
Here's an example using axios
:
index.tsx414 chars15 lines
This example is similar to the previous one, but we're using axios
to make the HTTP request. We're again including an Authorization
header with our API key. Note that axios.post
takes the request body as its second argument rather than as part of the body
property.
gistlibby LogSnag