cohere-ai / cohere-typescript

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

replace cohere.init() ui with a client creator #15

Closed kipply closed 1 year ago

kipply commented 3 years ago

It's better for

elliottsj commented 2 years ago

Related / to expand on this issue: the current default export is a singleton instance of the Cohere class: https://github.com/cohere-ai/cohere-node/blob/b27234a427c1082b706520af69bfe897bdb072b7/cohere.ts#L117

This means that it's not possible for a user to create multiple distinct instances with their own API keys. i.e. something like this is not possible:

import { Client } from 'cohere-ai';

const client1 = new Client();
client1.init('key1');

const client2 = new Client();
client2.init('key2');

Instead, we only have the singleton instance:

import cohere from 'cohere-ai';

cohere.init('key1');

An example use case when the former is useful is implementing a wrapping service for the Cohere API, e.g. a GraphQL server: https://github.com/elliottsj/cohere-graphql/blob/d3b616ac3b3b3a420a3a14a39c019ff444251774/src/routes/api/graphql.ts#L190

When using the singleton instance, there's a greater risk that users' API keys are shared between requests.