Skip to content

Commit

Permalink
feat(api): rename api key (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-app[bot] authored and stainless-bot committed May 29, 2024
1 parent a312c3b commit c9d1f45
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 47 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ The full API of this library can be found in [api.md](api.md).
import Together from 'together-ai';

const together = new Together({
accessToken: process.env['TOGETHER_API_KEY'], // This is the default and can be omitted
apiKey: process.env['TOGETHER_API_KEY'], // This is the default and can be omitted
});

async function main() {
Expand Down Expand Up @@ -69,7 +69,7 @@ This library includes TypeScript definitions for all request params and response
import Together from 'together-ai';

const together = new Together({
accessToken: process.env['TOGETHER_API_KEY'], // This is the default and can be omitted
apiKey: process.env['TOGETHER_API_KEY'], // This is the default and can be omitted
});

async function main() {
Expand Down
18 changes: 9 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface ClientOptions {
/**
* Defaults to process.env['TOGETHER_API_KEY'].
*/
accessToken?: string | undefined;
apiKey?: string | undefined;

/**
* Override the default base URL for the API, e.g., "https://api.example.com/v2/"
Expand Down Expand Up @@ -71,14 +71,14 @@ export interface ClientOptions {

/** API Client for interfacing with the Together API. */
export class Together extends Core.APIClient {
accessToken: string;
apiKey: string;

private _options: ClientOptions;

/**
* API Client for interfacing with the Together API.
*
* @param {string | undefined} [opts.accessToken=process.env['TOGETHER_API_KEY'] ?? undefined]
* @param {string | undefined} [opts.apiKey=process.env['TOGETHER_API_KEY'] ?? undefined]
* @param {string} [opts.baseURL=process.env['TOGETHER_BASE_URL'] ?? https://api.together.xyz/v1] - Override the default base URL for the API.
* @param {number} [opts.timeout=1 minute] - The maximum amount of time (in milliseconds) the client will wait for a response before timing out.
* @param {number} [opts.httpAgent] - An HTTP agent used to manage HTTP(s) connections.
Expand All @@ -89,17 +89,17 @@ export class Together extends Core.APIClient {
*/
constructor({
baseURL = Core.readEnv('TOGETHER_BASE_URL'),
accessToken = Core.readEnv('TOGETHER_API_KEY'),
apiKey = Core.readEnv('TOGETHER_API_KEY'),
...opts
}: ClientOptions = {}) {
if (accessToken === undefined) {
if (apiKey === undefined) {
throw new Errors.TogetherError(
"The TOGETHER_API_KEY environment variable is missing or empty; either provide it, or instantiate the Together client with an accessToken option, like new Together({ accessToken: 'My Access Token' }).",
"The TOGETHER_API_KEY environment variable is missing or empty; either provide it, or instantiate the Together client with an apiKey option, like new Together({ apiKey: 'My API Key' }).",
);
}

const options: ClientOptions = {
accessToken,
apiKey,
...opts,
baseURL: baseURL || `https://api.together.xyz/v1`,
};
Expand All @@ -113,7 +113,7 @@ export class Together extends Core.APIClient {
});
this._options = options;

this.accessToken = accessToken;
this.apiKey = apiKey;
}

chat: API.Chat = new API.Chat(this);
Expand All @@ -136,7 +136,7 @@ export class Together extends Core.APIClient {
}

protected override authHeaders(opts: Core.FinalRequestOptions): Core.Headers {
return { Authorization: `Bearer ${this.accessToken}` };
return { Authorization: `Bearer ${this.apiKey}` };
}

static Together = this;
Expand Down
2 changes: 1 addition & 1 deletion tests/api-resources/chat/completions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Together from 'together-ai';
import { Response } from 'node-fetch';

const together = new Together({
accessToken: 'My Access Token',
apiKey: 'My API Key',
baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010',
});

Expand Down
2 changes: 1 addition & 1 deletion tests/api-resources/completions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Together from 'together-ai';
import { Response } from 'node-fetch';

const together = new Together({
accessToken: 'My Access Token',
apiKey: 'My API Key',
baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010',
});

Expand Down
2 changes: 1 addition & 1 deletion tests/api-resources/embeddings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Together from 'together-ai';
import { Response } from 'node-fetch';

const together = new Together({
accessToken: 'My Access Token',
apiKey: 'My API Key',
baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010',
});

Expand Down
2 changes: 1 addition & 1 deletion tests/api-resources/files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Together from 'together-ai';
import { Response } from 'node-fetch';

const together = new Together({
accessToken: 'My Access Token',
apiKey: 'My API Key',
baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010',
});

Expand Down
2 changes: 1 addition & 1 deletion tests/api-resources/fine-tune.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Together from 'together-ai';
import { Response } from 'node-fetch';

const together = new Together({
accessToken: 'My Access Token',
apiKey: 'My API Key',
baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010',
});

Expand Down
2 changes: 1 addition & 1 deletion tests/api-resources/images.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Together from 'together-ai';
import { Response } from 'node-fetch';

const together = new Together({
accessToken: 'My Access Token',
apiKey: 'My API Key',
baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010',
});

Expand Down
2 changes: 1 addition & 1 deletion tests/api-resources/models.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Together from 'together-ai';
import { Response } from 'node-fetch';

const together = new Together({
accessToken: 'My Access Token',
apiKey: 'My API Key',
baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010',
});

Expand Down
52 changes: 23 additions & 29 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('instantiate client', () => {
const client = new Together({
baseURL: 'http://localhost:5000/',
defaultHeaders: { 'X-My-Default-Header': '2' },
accessToken: 'My Access Token',
apiKey: 'My API Key',
});

test('they are used in the request', () => {
Expand Down Expand Up @@ -55,7 +55,7 @@ describe('instantiate client', () => {
const client = new Together({
baseURL: 'http://localhost:5000/',
defaultQuery: { apiVersion: 'foo' },
accessToken: 'My Access Token',
apiKey: 'My API Key',
});
expect(client.buildURL('/foo', null)).toEqual('http://localhost:5000/foo?apiVersion=foo');
});
Expand All @@ -64,7 +64,7 @@ describe('instantiate client', () => {
const client = new Together({
baseURL: 'http://localhost:5000/',
defaultQuery: { apiVersion: 'foo', hello: 'world' },
accessToken: 'My Access Token',
apiKey: 'My API Key',
});
expect(client.buildURL('/foo', null)).toEqual('http://localhost:5000/foo?apiVersion=foo&hello=world');
});
Expand All @@ -73,7 +73,7 @@ describe('instantiate client', () => {
const client = new Together({
baseURL: 'http://localhost:5000/',
defaultQuery: { hello: 'world' },
accessToken: 'My Access Token',
apiKey: 'My API Key',
});
expect(client.buildURL('/foo', { hello: undefined })).toEqual('http://localhost:5000/foo');
});
Expand All @@ -82,7 +82,7 @@ describe('instantiate client', () => {
test('custom fetch', async () => {
const client = new Together({
baseURL: 'http://localhost:5000/',
accessToken: 'My Access Token',
apiKey: 'My API Key',
fetch: (url) => {
return Promise.resolve(
new Response(JSON.stringify({ url, custom: true }), {
Expand All @@ -99,7 +99,7 @@ describe('instantiate client', () => {
test('custom signal', async () => {
const client = new Together({
baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010',
accessToken: 'My Access Token',
apiKey: 'My API Key',
fetch: (...args) => {
return new Promise((resolve, reject) =>
setTimeout(
Expand All @@ -124,18 +124,12 @@ describe('instantiate client', () => {

describe('baseUrl', () => {
test('trailing slash', () => {
const client = new Together({
baseURL: 'http://localhost:5000/custom/path/',
accessToken: 'My Access Token',
});
const client = new Together({ baseURL: 'http://localhost:5000/custom/path/', apiKey: 'My API Key' });
expect(client.buildURL('/foo', null)).toEqual('http://localhost:5000/custom/path/foo');
});

test('no trailing slash', () => {
const client = new Together({
baseURL: 'http://localhost:5000/custom/path',
accessToken: 'My Access Token',
});
const client = new Together({ baseURL: 'http://localhost:5000/custom/path', apiKey: 'My API Key' });
expect(client.buildURL('/foo', null)).toEqual('http://localhost:5000/custom/path/foo');
});

Expand All @@ -144,55 +138,55 @@ describe('instantiate client', () => {
});

test('explicit option', () => {
const client = new Together({ baseURL: 'https://example.com', accessToken: 'My Access Token' });
const client = new Together({ baseURL: 'https://example.com', apiKey: 'My API Key' });
expect(client.baseURL).toEqual('https://example.com');
});

test('env variable', () => {
process.env['TOGETHER_BASE_URL'] = 'https://example.com/from_env';
const client = new Together({ accessToken: 'My Access Token' });
const client = new Together({ apiKey: 'My API Key' });
expect(client.baseURL).toEqual('https://example.com/from_env');
});

test('empty env variable', () => {
process.env['TOGETHER_BASE_URL'] = ''; // empty
const client = new Together({ accessToken: 'My Access Token' });
const client = new Together({ apiKey: 'My API Key' });
expect(client.baseURL).toEqual('https://api.together.xyz/v1');
});

test('blank env variable', () => {
process.env['TOGETHER_BASE_URL'] = ' '; // blank
const client = new Together({ accessToken: 'My Access Token' });
const client = new Together({ apiKey: 'My API Key' });
expect(client.baseURL).toEqual('https://api.together.xyz/v1');
});
});

test('maxRetries option is correctly set', () => {
const client = new Together({ maxRetries: 10, accessToken: 'My Access Token' });
const client = new Together({ maxRetries: 10, apiKey: 'My API Key' });
expect(client.maxRetries).toEqual(10);

// default
const client2 = new Together({ accessToken: 'My Access Token' });
const client2 = new Together({ apiKey: 'My API Key' });
expect(client2.maxRetries).toEqual(5);
});

test('with environment variable arguments', () => {
// set options via env var
process.env['TOGETHER_API_KEY'] = 'My Access Token';
process.env['TOGETHER_API_KEY'] = 'My API Key';
const client = new Together();
expect(client.accessToken).toBe('My Access Token');
expect(client.apiKey).toBe('My API Key');
});

test('with overriden environment variable arguments', () => {
// set options via env var
process.env['TOGETHER_API_KEY'] = 'another My Access Token';
const client = new Together({ accessToken: 'My Access Token' });
expect(client.accessToken).toBe('My Access Token');
process.env['TOGETHER_API_KEY'] = 'another My API Key';
const client = new Together({ apiKey: 'My API Key' });
expect(client.apiKey).toBe('My API Key');
});
});

describe('request building', () => {
const client = new Together({ accessToken: 'My Access Token' });
const client = new Together({ apiKey: 'My API Key' });

describe('Content-Length', () => {
test('handles multi-byte characters', () => {
Expand Down Expand Up @@ -234,7 +228,7 @@ describe('retries', () => {
return new Response(JSON.stringify({ a: 1 }), { headers: { 'Content-Type': 'application/json' } });
};

const client = new Together({ accessToken: 'My Access Token', timeout: 10, fetch: testFetch });
const client = new Together({ apiKey: 'My API Key', timeout: 10, fetch: testFetch });

expect(await client.request({ path: '/foo', method: 'get' })).toEqual({ a: 1 });
expect(count).toEqual(2);
Expand All @@ -261,7 +255,7 @@ describe('retries', () => {
return new Response(JSON.stringify({ a: 1 }), { headers: { 'Content-Type': 'application/json' } });
};

const client = new Together({ accessToken: 'My Access Token', fetch: testFetch });
const client = new Together({ apiKey: 'My API Key', fetch: testFetch });

expect(await client.request({ path: '/foo', method: 'get' })).toEqual({ a: 1 });
expect(count).toEqual(2);
Expand All @@ -288,7 +282,7 @@ describe('retries', () => {
return new Response(JSON.stringify({ a: 1 }), { headers: { 'Content-Type': 'application/json' } });
};

const client = new Together({ accessToken: 'My Access Token', fetch: testFetch });
const client = new Together({ apiKey: 'My API Key', fetch: testFetch });

expect(await client.request({ path: '/foo', method: 'get' })).toEqual({ a: 1 });
expect(count).toEqual(2);
Expand Down

0 comments on commit c9d1f45

Please sign in to comment.