Skip to content

Commit

Permalink
Fix v1 environment issue (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
billytrend-cohere authored Aug 20, 2024
1 parent fb98f0a commit 3068c78
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/CustomClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { CohereClient } from "./Client";

export class CustomClient extends CohereClient {
constructor(protected readonly _options: CohereClient.Options = {}) {
try {
// if url ends with /v1, drop it for back compat
const match = /\/v1\/?$/
const fixed = _options.environment?.toString().replace(match, "");
if (fixed !== _options.environment?.toString()) {
_options.environment = fixed
}
} catch { }

super(_options)
}
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export * as Cohere from "./api";
export { BedrockClient } from "./BedrockClient";
export { CohereClient } from "./Client";
export { CohereClientV2 } from "./ClientV2";
export { CustomClient as CohereClient } from "./CustomClient";
export { CohereEnvironment } from "./environments";
export { CohereError, CohereTimeoutError } from "./errors";
export { SagemakerClient } from "./SagemakerClient";
Expand Down
11 changes: 11 additions & 0 deletions src/test/__snapshots__/client.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`v1 back compat https://api.cohere.com/ 1`] = `"https://api.cohere.com/v1/chat"`;

exports[`v1 back compat https://api.cohere.com/v1 1`] = `"https://api.cohere.com/v1/chat"`;

exports[`v1 back compat https://api.cohere.com/v1/ 1`] = `"https://api.cohere.com/v1/chat"`;

exports[`v1 back compat https://api.cohere.com/v1// 1`] = `"https://api.cohere.com/v1/v1/chat"`;

exports[`v1 back compat https://api.cohere.com/v2 1`] = `"https://api.cohere.com/v2/v1/chat"`;
29 changes: 29 additions & 0 deletions src/test/client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { describe, test } from "@jest/globals";
import { CohereClient } from "../index";

describe("v1 back compat", () => {
test.each([
"https://api.cohere.com/",
"https://api.cohere.com/v1",
"https://api.cohere.com/v1/",
"https://api.cohere.com/v1//",
"https://api.cohere.com/v2",
])("%s", async (environment) => {
let url = ""

const cohere = new CohereClient({
token: "token",
environment,
fetcher: (async (opts) => {
url = opts.url
throw "we're done"
})
})

try {
await cohere.chat({ message: "hello" })
} catch { }

expect(url).toMatchSnapshot()
})
});
2 changes: 1 addition & 1 deletion src/test/tests.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, test } from "@jest/globals";
import { StreamedChatResponse } from "api";
import { CohereClient } from "../Client";
import { CohereClient } from "../index";

const cohere = new CohereClient({
token: process.env.COHERE_API_KEY!,
Expand Down

0 comments on commit 3068c78

Please sign in to comment.