一直在尝试使用openai API。在意识到我使用的是旧版API的语法后,我将其切换到了3. 5版本。现在似乎得到一个未定义的错误。不确定,但我可能又做错了语法方面的事情?对这个问题有什么想法吗?
错误代码:
PS C:\Demo> node index.js
undefined
字符串
代码:
import { Configuration, OpenAIApi } from "openai";
const configuration = new Configuration({
organization: "Placeholder",
apiKey: "Placeholder"
});
const openai = new OpenAIApi(configuration);
const completion = await openai.createChatCompletion({ // Functions must be /v1/chat/completions compatible:
model: "gpt-3.5-turbo",
messages: [
{role: "user", content: "How are you today?"},
]
});
console.log(completion.data.choices[0].text); // print text response from response object
型
UPDATE ADDED完成对象:
{
status: 200,
statusText: 'OK',
headers: {
date: 'Sat, 29 Jul 2023 04:47:38 GMT',
'content-type': 'application/json',
'content-length': '492',
connection: 'close',
'access-control-allow-origin': '*',
'cache-control': 'no-cache, must-revalidate',
'openai-model': 'gpt-3.5-turbo-0613',
'openai-organization': 'user-kkvsk8yyp8b4vxwiot1omana',
'openai-processing-ms': '1666',
'openai-version': '2020-10-01',
'strict-transport-security': 'max-age=15724800; includeSubDomains',
'x-ratelimit-limit-requests': '200',
'x-ratelimit-limit-tokens': '40000',
'x-ratelimit-remaining-requests': '197',
'x-ratelimit-remaining-tokens': '39978',
'x-ratelimit-reset-requests': '15m0.985s',
'x-ratelimit-reset-tokens': '33ms',
'x-request-id': '97ffedd744caa7b206f878d2200c90a5',
'cf-cache-status': 'DYNAMIC',
server: 'cloudflare',
'cf-ray': '7ee2b98bc8b0d9d1-MIA',
'alt-svc': 'h3=":443"; ma=86400'
},
config: {
transitional: {
silentJSONParsing: true,
forcedJSONParsing: true,
clarifyTimeoutError: false
},
adapter: [Function: httpAdapter],
transformRequest: [ [Function: transformRequest] ],
transformResponse: [ [Function: transformResponse] ],
timeout: 0,
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
maxBodyLength: -1,
validateStatus: [Function: validateStatus],
headers: {
Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/json',
'User-Agent': 'OpenAI/NodeJS/3.3.0',
Authorization: 'Bearer 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',
'OpenAI-Organization': 'org-UfIKVGWEJyAhjwRVK9wrMBRZ',
'Content-Length': 85
},
method: 'post',
data: '{"model":"gpt-3.5-turbo","messages":[{"role":"user","content":"How are you today?"}]}',
url: 'https://api.openai.com/v1/chat/completions'
},
request: <ref *1> ClientRequest {
_events: [Object: null prototype] {
abort: [Function (anonymous)],
aborted: [Function (anonymous)],
connect: [Function (anonymous)],
error: [Function (anonymous)],
socket: [Function (anonymous)],
timeout: [Function (anonymous)],
finish: [Function: requestOnFinish]
},
_eventsCount: 7,
_maxListeners: undefined,
outputData: [],
outputSize: 0,
writable: true,
destroyed: false,
_last: true,
chunkedEncoding: false,
shouldKeepAlive: false,
maxRequestsOnConnectionReached: false,
_defaultKeepAlive: true,
useChunkedEncodingByDefault: true,
sendDate: false,
_removedConnection: false,
_removedContLen: false,
_removedTE: false,
strictContentLength: false,
_contentLength: 85,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
_closed: false,
socket: TLSSocket {
_tlsOptions: [Object],
_secureEstablished: true,
_securePending: false,
_newSessionPending: false,
_controlReleased: true,
secureConnecting: false,
_SNICallback: null,
servername: 'api.openai.com',
alpnProtocol: false,
authorized: true,
authorizationError: null,
encrypted: true,
_events: [Object: null prototype],
_eventsCount: 10,
connecting: false,
_hadError: false,
_parent: null,
_host: 'api.openai.com',
_closeAfterHandlingError: false,
_readableState: [ReadableState],
_maxListeners: undefined,
_writableState: [WritableState],
allowHalfOpen: false,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: undefined,
_server: null,
ssl: [TLSWrap],
_requestCert: true,
_rejectUnauthorized: true,
parser: null,
_httpMessage: [Circular *1],
[Symbol(res)]: [TLSWrap],
[Symbol(verified)]: true,
[Symbol(pendingSession)]: null,
[Symbol(async_id_symbol)]: 10,
[Symbol(kHandle)]: [TLSWrap],
[Symbol(lastWriteQueueSize)]: 0,
[Symbol(timeout)]: null,
[Symbol(kBuffer)]: null,
[Symbol(kBufferCb)]: null,
[Symbol(kBufferGen)]: null,
[Symbol(kCapture)]: false,
[Symbol(kSetNoDelay)]: false,
[Symbol(kSetKeepAlive)]: true,
[Symbol(kSetKeepAliveInitialDelay)]: 60,
[Symbol(kBytesRead)]: 0,
[Symbol(kBytesWritten)]: 0,
[Symbol(connect-options)]: [Object]
},
_header: 'POST /v1/chat/completions HTTP/1.1\r\n' +
'Accept: application/json, text/plain, */*\r\n' +
'Content-Type: application/json\r\n' +
'User-Agent: OpenAI/NodeJS/3.3.0\r\n' +
'Authorization: Bearer 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\r\n' +
'OpenAI-Organization: org-UfIKVGWEJyAhjwRVK9wrMBRZ\r\n' +
'Content-Length: 85\r\n' +
'Host: api.openai.com\r\n' +
'Connection: close\r\n' +
'\r\n',
_keepAliveTimeout: 0,
_onPendingData: [Function: nop],
agent: Agent {
_events: [Object: null prototype],
_eventsCount: 2,
_maxListeners: undefined,
defaultPort: 443,
protocol: 'https:',
options: [Object: null prototype],
requests: [Object: null prototype] {},
sockets: [Object: null prototype],
freeSockets: [Object: null prototype] {},
keepAliveMsecs: 1000,
keepAlive: false,
maxSockets: Infinity,
maxFreeSockets: 256,
scheduling: 'lifo',
maxTotalSockets: Infinity,
totalSocketCount: 1,
maxCachedSessions: 100,
_sessionCache: [Object],
[Symbol(kCapture)]: false
},
socketPath: undefined,
method: 'POST',
maxHeaderSize: undefined,
insecureHTTPParser: undefined,
joinDuplicateHeaders: undefined,
path: '/v1/chat/completions',
_ended: true,
res: IncomingMessage {
_readableState: [ReadableState],
_events: [Object: null prototype],
_eventsCount: 4,
_maxListeners: undefined,
socket: [TLSSocket],
httpVersionMajor: 1,
httpVersionMinor: 1,
httpVersion: '1.1',
complete: true,
rawHeaders: [Array],
rawTrailers: [],
joinDuplicateHeaders: undefined,
aborted: false,
upgrade: false,
url: '',
method: null,
statusCode: 200,
statusMessage: 'OK',
client: [TLSSocket],
_consuming: false,
_dumped: false,
req: [Circular *1],
responseUrl: 'https://api.openai.com/v1/chat/completions',
redirects: [],
[Symbol(kCapture)]: false,
[Symbol(kHeaders)]: [Object],
[Symbol(kHeadersCount)]: 44,
[Symbol(kTrailers)]: null,
[Symbol(kTrailersCount)]: 0
},
aborted: false,
timeoutCb: null,
upgradeOrConnect: false,
parser: null,
maxHeadersCount: null,
reusedSocket: false,
host: 'api.openai.com',
protocol: 'https:',
_redirectable: Writable {
_writableState: [WritableState],
_events: [Object: null prototype],
_eventsCount: 3,
_maxListeners: undefined,
_options: [Object],
_ended: true,
_ending: true,
_redirectCount: 0,
_redirects: [],
_requestBodyLength: 85,
_requestBodyBuffers: [],
_onNativeResponse: [Function (anonymous)],
_currentRequest: [Circular *1],
_currentUrl: 'https://api.openai.com/v1/chat/completions',
[Symbol(kCapture)]: false
},
[Symbol(kCapture)]: false,
[Symbol(kBytesWritten)]: 0,
[Symbol(kNeedDrain)]: false,
[Symbol(corked)]: 0,
[Symbol(kOutHeaders)]: [Object: null prototype] {
accept: [Array],
'content-type': [Array],
'user-agent': [Array],
authorization: [Array],
'openai-organization': [Array],
'content-length': [Array],
host: [Array]
},
[Symbol(errored)]: null,
[Symbol(kHighWaterMark)]: 16384,
[Symbol(kRejectNonStandardBodyWrites)]: false,
[Symbol(kUniqueHeaders)]: null
},
data: {
id: 'chatcmpl-7hW9YTnN96BXLqCsurIMqzCpdD9EJ',
object: 'chat.completion',
created: 1690606056,
model: 'gpt-3.5-turbo-0613',
choices: [ [Object] ],
usage: { prompt_tokens: 12, completion_tokens: 27, total_tokens: 39 }
}
}
型
1条答案
按热度按时间ebdffaop1#
的确是语法的问题。忘记为以下对象切换旧版gpt语法:
字符串
的gpt 3.5版本语法:
型