cohere-ai / cohere-typescript

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

Allow `baseUrl` and `defaultHeaders` override #182

Closed huytool157 closed 4 months ago

huytool157 commented 4 months ago

Is there a way to do this in Cohere?


new Anthropic({
  apiKey,
  baseUrl, // string
  defaultHeaders, // Record<string, unknown>
})

My use case is that we're re-routing requests to a proxy first (observability and security purposes) before sending to Cohere API
dsinghvi commented 4 months ago

@huytool157 you should be able to pass the baseUrl in the environment key

new Cohere({
  environment: "YOUR_BASE_URL"
});
huytool157 commented 4 months ago

@dsinghvi thanks, any way to do headers?

billytrend-cohere commented 4 months ago

Hey @huytool157 sorry for delay on this. One way you could do this is to pass a custom fetcher to the constructor.

import { CohereClient } from "cohere-ai";
import { Fetcher, fetcher } from "cohere-ai/core";

const cohere = new CohereClient({
    token: '...',
    fetcher: (args: Fetcher.Args) => fetcher({ ...args, headers: { ...args.headers, myHeader: "hello!" } })
});

Let me know if that solves your problem. We will track a better fix for this though

huytool157 commented 4 months ago

@billytrend-cohere Is this fetcher option new? What's the minimum version that I need to upgrade to? Currently in 7.9.5.

Screenshot 2024-07-09 at 11 19 00 AM
billytrend-cohere commented 4 months ago

yeah should be in 7.10.2 and up but would recommend latest version if possible! Should be a smooth update but let me know if you need assistance

huytool157 commented 4 months ago

@billytrend-cohere did you guys add some @aws-sdk deps recently? I'm getting some peer dependency issues trying to upgrade.

backend
└─┬ @aws-sdk/client-sso-oidc 3.609.0
  ├── ✕ unmet peer @aws-sdk/client-sts@^3.609.0: found 3.347.1
  └─┬ @aws-sdk/credential-provider-node 3.609.0
    └─┬ @aws-sdk/credential-provider-ini 3.609.0
      ├── ✕ unmet peer @aws-sdk/client-sts@^3.609.0: found 3.347.1
      └─┬ @aws-sdk/credential-provider-web-identity 3.609.0
        └── ✕ unmet peer @aws-sdk/client-sts@^3.609.0: found 3.347.1
billytrend-cohere commented 4 months ago

we did, will look into this issue asap

huytool157 commented 4 months ago

Thanks, preferably we should not have to deal with this because we are using @aws-sdk/client-sts in different parts of the codebase and we only use the native cohere integrations without AWS Bedrock

billytrend-cohere commented 4 months ago

We suspect it might be related to https://github.com/aws/aws-sdk-js-v3/issues/6258 but will keep you posted, meantime 7.10.2 shouldn't contain the problematic dependency!

huytool157 commented 4 months ago

I actually think it starts from 7.10.2. These dependencies have @aws-sdk/client-sts under the hood. Kinda unfortunate that it's the same version as the fetcher option and now I'm stucked.

https://github.com/cohere-ai/cohere-typescript/pull/162/files#diff-7ae45ad102eab3b6d7e7896acd08c427a9b25b346470d7bc6507b6481575d519R22-R25

huytool157 commented 4 months ago

@billytrend-cohere following up on this!

huytool157 commented 4 months ago

I ended up upgrading @aws-sdk/client-sts to 3.616.0. Not ideal since you shouldn't have to upgrade other packages to get cohere-ai working but we passed that hoop, going to close the issue.