cohere-ai / cohere-typescript

The Cohere TypeScript SDK
https://docs.cohere.ai
MIT License
122 stars 18 forks source link

Throw error on non 200 response status #61

Closed robertkozin closed 1 year ago

robertkozin commented 1 year ago

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?

cohere.classify({ 
    model: 'large', 
    inputs: ["Am I still able to return my order?", "When can I expect my package?"], 
    examples: [{"text": "Do you offer same day shipping?", "label": "Shipping and handling policy"}, {"text": "Can you ship to Italy?", "label": "Shipping and handling policy"}, {"text": "How long does shipping take?", "label": "Shipping and handling policy"}, {"text": "Can I buy online and pick up in store?", "label": "Shipping and handling policy"}, {"text": "What are your shipping options?", "label": "Shipping and handling policy"}, {"text": "My order arrived damaged, can I get a refund?", "label": "Start return or exchange"}, {"text": "You sent me the wrong item", "label": "Start return or exchange"}, {"text": "I want to exchange my item for another colour", "label": "Start return or exchange"}, {"text": "I ordered something and it wasn’t what I expected. Can I return it?", "label": "Start return or exchange"}, {"text": "What’s your return policy?", "label": "Start return or exchange"}, {"text": "Where’s my package?", "label": "Track order"}, {"text": "When will my order arrive?", "label": "Track order"}, {"text": "What’s my shipping number?", "label": "Track order"}, {"text": "Which carrier is my package with?", "label": "Track order"}, {"text": "Is my package delayed?", "label": "Track order"}] 
}).then(response => {
    console.log(`The confidence levels of the labels are ${JSON.stringify(response.body.classifications)}`); 
}).catch(err => {
    console.log('Failed to classify', err)
})
elliottsj commented 1 year ago

Here is our current error handling behaviour:

generate

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"}}

embed

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]}}

classify

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"}}

summarize

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"}}

Observations