Closed robertkozin closed 1 year ago
Here is our current error handling behaviour:
const cohere = require('cohere-ai');
cohere.init('invalid-key');
(async () => {
const response = await cohere.generate({
model: '...',
prompt: "...",
max_tokens: 300,
});
if (response.statusCode === 200) {
console.log(`Prediction: ${response.body.generations[0].text}`);
} else {
console.error(`Error: ${JSON.stringify(response)}`);
}
})();
Error: {"statusCode":401,"body":{"message":"invalid api token"}}
const cohere = require('cohere-ai');
cohere.init('invalid-key');
(async () => {
const response = await cohere.embed({
model: '...',
texts: [...],
});
if (response.statusCode === 200) {
console.log(`Embeddings: ${response.body.embeddings}`);
} else {
console.error(`Error: ${JSON.stringify(response)}`);
}
})();
Error: {"statusCode":401,"body":{"embeddings":[null,null,null,null,null,null,null]}}
const cohere = require('cohere-ai');
cohere.init('invalid-key');
(async () => {
const response = await cohere.classify({
model: '...',
inputs: [...],
examples: [...],
});
if (response.statusCode === 200) {
console.log(
`The confidence levels of the labels are ${JSON.stringify(response.body.classifications)}`
);
} else {
console.error(`Error: ${JSON.stringify(response)}`);
}
})();
Error: {"statusCode":401,"body":{"message":"invalid api token"}}
const cohere = require('cohere-ai');
cohere.init('invalid-key');
(async () => {
const response = await cohere.summarize({
text: '...',
length: 'medium',
format: 'paragraph',
model: 'summarize-xlarge',
additional_command: '',
temperature: 0.3,
});
if (response.statusCode === 200) {
console.log('Summary:', response.body.summary);
} else {
console.error(`Error: ${JSON.stringify(response)}`);
}
})();
Error: {"statusCode":401,"body":{"message":"invalid api token"}}
response.statusCode
works, but is unconventional in Node.js; we should probably reject the promise as suggested.cohere.embed()
is not returning the error message in the response.
Related: https://github.com/cohere-ai/blobheart/issues/6527
It's hard to write clean code when the SDK does not throw an error on non successful response. Maybe change it up?