Skip to content

Commit a314f5f

Browse files
authored
Merge pull request #1108 from Utkarsh-0304/feat-hyperbolic
provider: added support for Hyperbolic AI
2 parents 29d25fb + 74a2061 commit a314f5f

File tree

6 files changed

+170
-0
lines changed

6 files changed

+170
-0
lines changed

src/globals.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ export const REPLICATE: string = 'replicate';
9494
export const LEPTON: string = 'lepton';
9595
export const KLUSTER_AI: string = 'kluster-ai';
9696
export const NSCALE: string = 'nscale';
97+
export const HYPERBOLIC: string = 'hyperbolic';
9798

9899
export const VALID_PROVIDERS = [
99100
ANTHROPIC,
@@ -153,6 +154,7 @@ export const VALID_PROVIDERS = [
153154
LEPTON,
154155
KLUSTER_AI,
155156
NSCALE,
157+
HYPERBOLIC,
156158
];
157159

158160
export const CONTENT_TYPES = {

src/providers/hyperbolic/api.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { ProviderAPIConfig } from '../types';
2+
3+
const HYPERBOLIC_API_URL = 'https://api.hyperbolic.xyz';
4+
5+
const HyperbolicAPIConfig: ProviderAPIConfig = {
6+
getBaseURL: () => HYPERBOLIC_API_URL,
7+
headers: ({ providerOptions, fn }) => {
8+
const headersObj: Record<string, string> = {
9+
Authorization: `Bearer ${providerOptions.apiKey}`,
10+
};
11+
return headersObj;
12+
},
13+
getEndpoint: ({ fn }) => {
14+
switch (fn) {
15+
case 'chatComplete':
16+
return '/v1/chat/completions';
17+
case 'imageGenerate':
18+
return '/v1/image/generation';
19+
default:
20+
return '';
21+
}
22+
},
23+
};
24+
25+
export default HyperbolicAPIConfig;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { HYPERBOLIC } from '../../globals';
2+
interface HyperbolicStreamChunk {
3+
id: string;
4+
object: string;
5+
created: number;
6+
model: string;
7+
choices: {
8+
index: number;
9+
message: {
10+
role?: string | null;
11+
content?: string;
12+
};
13+
finish_reason: string | null;
14+
}[];
15+
usage: {
16+
prompt_tokens: number;
17+
total_tokens: number;
18+
completion_tokens: number;
19+
};
20+
}
21+
22+
export const HyperbolicChatCompleteStreamChunkTransform = (
23+
responseChunk: string
24+
) => {
25+
let chunk = responseChunk.trim();
26+
chunk = chunk.replace(/^data: /, '');
27+
chunk = chunk.trim();
28+
29+
if (chunk === '[DONE]') {
30+
return `data: ${chunk}\n\n`;
31+
}
32+
33+
try {
34+
const parsedChunk: HyperbolicStreamChunk = JSON.parse(chunk);
35+
return (
36+
`data: ${JSON.stringify({
37+
id: parsedChunk.id,
38+
object: parsedChunk.object,
39+
created: parsedChunk.created,
40+
model: parsedChunk.model,
41+
provider: HYPERBOLIC,
42+
choices: [
43+
{
44+
index: parsedChunk.choices[0].index,
45+
message: parsedChunk.choices[0].message,
46+
finish_reason: parsedChunk.choices[0].finish_reason,
47+
},
48+
],
49+
usage: {
50+
prompt_tokens: parsedChunk.usage.prompt_tokens,
51+
total_tokens: parsedChunk.usage.total_tokens,
52+
completion_tokens: parsedChunk.usage.completion_tokens,
53+
},
54+
})}` + '\n\n'
55+
);
56+
} catch (error) {
57+
console.error('Error parsing Hyperbolic stream chunk:', error);
58+
return `data: ${chunk}\n\n`;
59+
}
60+
};
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { HYPERBOLIC } from '../../globals';
2+
import { ErrorResponse, ProviderConfig } from '../types';
3+
import { OpenAIErrorResponseTransform } from '../openai/utils';
4+
5+
export const HyperbolicImageGenerateConfig: ProviderConfig = {
6+
model: {
7+
param: 'model',
8+
required: true,
9+
},
10+
prompt: {
11+
param: 'prompt',
12+
required: true,
13+
},
14+
height: {
15+
param: 'height',
16+
},
17+
width: {
18+
param: 'width',
19+
},
20+
backend: {
21+
param: 'backend',
22+
},
23+
};
24+
25+
interface HyperbolicImageObject {
26+
url?: string;
27+
b64_json?: string;
28+
seed?: number;
29+
}
30+
31+
interface HyperbolicImageGenerateResponse {
32+
images: HyperbolicImageObject[];
33+
model: string;
34+
prompt: string;
35+
}
36+
37+
export const HyperbolicImageGenerateResponseTransform: (
38+
response: HyperbolicImageGenerateResponse | ErrorResponse,
39+
responseStatus: number
40+
) => HyperbolicImageGenerateResponse | ErrorResponse = (
41+
response,
42+
responseStatus
43+
) => {
44+
if (responseStatus !== 200 && 'error' in response) {
45+
return OpenAIErrorResponseTransform(response, HYPERBOLIC);
46+
}
47+
48+
return response;
49+
};

src/providers/hyperbolic/index.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { HYPERBOLIC } from '../../globals';
2+
import { chatCompleteParams, responseTransformers } from '../open-ai-base';
3+
import { ProviderConfigs } from '../types';
4+
import HyperbolicAPIConfig from './api';
5+
import { HyperbolicChatCompleteStreamChunkTransform } from './chatComplete';
6+
import {
7+
HyperbolicImageGenerateConfig,
8+
HyperbolicImageGenerateResponseTransform,
9+
} from './imageGenerate';
10+
11+
const HyperbolicConfig: ProviderConfigs = {
12+
chatComplete: chatCompleteParams(
13+
[],
14+
{},
15+
{
16+
top_k: { param: 'top_k', default: -1 },
17+
min_p: { param: 'min_p', default: 0, min: 0, max: 1 },
18+
repetition_penalty: { param: 'repetition_penalty', default: 1 },
19+
}
20+
),
21+
imageGenerate: HyperbolicImageGenerateConfig,
22+
api: HyperbolicAPIConfig,
23+
responseTransforms: {
24+
...responseTransformers(HYPERBOLIC, {
25+
chatComplete: true,
26+
}),
27+
'stream-chatComplete': HyperbolicChatCompleteStreamChunkTransform,
28+
imageGenerate: HyperbolicImageGenerateResponseTransform,
29+
},
30+
};
31+
32+
export default HyperbolicConfig;

src/providers/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import ReplicateConfig from './replicate';
5858
import LeptonConfig from './lepton';
5959
import KlusterAIConfig from './kluster-ai';
6060
import NscaleConfig from './nscale';
61+
import HyperbolicConfig from './hyperbolic';
6162

6263
const Providers: { [key: string]: ProviderConfigs } = {
6364
openai: OpenAIConfig,
@@ -116,6 +117,7 @@ const Providers: { [key: string]: ProviderConfigs } = {
116117
lepton: LeptonConfig,
117118
'kluster-ai': KlusterAIConfig,
118119
nscale: NscaleConfig,
120+
hyperbolic: HyperbolicConfig,
119121
};
120122

121123
export default Providers;

0 commit comments

Comments
 (0)