Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add anthropic provider and refactor api field of publishJSON #205

Merged
merged 5 commits into from
Nov 1, 2024

Conversation

CahidArda
Copy link
Contributor

@CahidArda CahidArda commented Oct 29, 2024

Anthropic Provider

You can now publish requests to Anthropic API with

import { anthropic, Client } from "@upstash/qstash"

const client = new Client({ token: ... })
await client.publishJSON({
  api: {
    name: "llm",
    provider: anthropic()
  },
  body: {
    model,
  },
  callback,
});

Refactor

This PR is mainly concerned with how we handle the api field in publishJSON, batchJSON and enqueue:

await client.publishJSON({
  api: {
    name: "llm",
    provider: upstash()
  },
  body: {
    model,
  },
  callback,
});

Previously, the providers (upstash, openai, custom) returned the following object:

export type ProviderReturnType<
  TOwner extends Owner = Owner,
  TBaseUrl extends string = string,
  TToken extends string = string,
  TOrganization extends string | undefined = string | undefined,
> = {
  owner: TOwner;
  baseUrl: TBaseUrl;
  token: TToken;
  organization: TOrganization;
};

resend provider returned:

export type EmailProviderReturnType = {
  owner: "resend";
  baseUrl: "https://api.resend.com/emails" | "https://api.resend.com/emails/batch";
  token: string;
};

The publish request would be updated with this provider in a series of methods. Now there is a single method.

Now, the providers return LLMProvider and EmailProvider classes which extend BaseProvider. BaseProvider has some methods we utilize in getProviderInfo method.

getProviderInfo transform the api field of PublishRequest into ProviderInfo which is similar to the original ProviderReturnType:

export type ProviderInfo = {
  url: string;
  baseUrl: string;
  route: string[];
  appendHeaders: Record<string, string>;
  owner: Owner;
};

Notive the appendHeaders. Now, the headers are created in the BaseProvider and it's subclasses, instead of appendLLMOptionsIfNeeded and appendAPIOptions .

Copy link

linear bot commented Oct 29, 2024

@CahidArda CahidArda force-pushed the DX-1323-anthropic-and-refactor-api branch from 27f324b to 3fa2ec1 Compare October 30, 2024 16:38
};

export type ApiKind = "llm" | "email";
export type Owner = EmailOwner | LLMOwner;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like Owner is not the correct keyword here, because EmailOwner or LLMOwner feels like referring to a user, not a company. I was thinking of Provider at first, but we already use that for classes. Can we think of sth else?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took the owner from the old api. It's not the best name but I couldn't think of a better alternative. Something like ProviderOwner would be too much I think

const headerValue = this.owner === "anthropic" ? this.token : `Bearer ${this.token}`;

const headers = { [header]: headerValue };
if (this.organization) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we check if the owner is openai?


// UTILS

function updateWithAnalytics(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we seperate the utilties?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you mean different files right?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved them to utils.ts

@CahidArda CahidArda merged commit 7c9db73 into main Nov 1, 2024
6 checks passed
@CahidArda CahidArda deleted the DX-1323-anthropic-and-refactor-api branch November 1, 2024 14:32
@CahidArda CahidArda changed the title Add anthropic provider and refactor publish api Add anthropic provider and refactor api field of publishJSON Nov 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants