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

provider: SambaNova FastAPI implementation. #548

Merged
merged 16 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export const GITHUB: string = 'github';
export const DEEPBRICKS: string = 'deepbricks';
export const SILICONFLOW: string = 'siliconflow';
export const CEREBRAS: string = 'cerebras';
export const SAMBANOVA: string = 'sambanova';

export const VALID_PROVIDERS = [
ANTHROPIC,
Expand Down Expand Up @@ -108,6 +109,7 @@ export const VALID_PROVIDERS = [
SILICONFLOW,
HUGGING_FACE,
CEREBRAS,
SAMBANOVA,
];

export const CONTENT_TYPES = {
Expand Down
2 changes: 2 additions & 0 deletions src/providers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import DeepbricksConfig from './deepbricks';
import SiliconFlowConfig from './siliconflow';
import HuggingfaceConfig from './huggingface';
import { cerebrasProviderAPIConfig } from './cerebras';
import SambaNovaConfig from './sambanova';

const Providers: { [key: string]: ProviderConfigs } = {
openai: OpenAIConfig,
Expand Down Expand Up @@ -81,6 +82,7 @@ const Providers: { [key: string]: ProviderConfigs } = {
deepbricks: DeepbricksConfig,
siliconflow: SiliconFlowConfig,
cerebras: cerebrasProviderAPIConfig,
sambanova: SambaNovaConfig,
};

export default Providers;
19 changes: 19 additions & 0 deletions src/providers/sambanova/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ProviderAPIConfig } from '../types';

const SambaNovaAPIConfig: ProviderAPIConfig = {
getBaseURL: ({ providerOptions }) =>
providerOptions.urlToFetch || 'https://fast-api.snova.ai/v1',
ilsubyeega marked this conversation as resolved.
Show resolved Hide resolved
headers: ({ providerOptions }) => {
return { Authorization: `Basic ${providerOptions.apiKey}` };
},
getEndpoint: ({ fn }) => {
switch (fn) {
case 'chatComplete':
return '/chat/completions';
default:
return '';
}
},
};

export default SambaNovaAPIConfig;
114 changes: 114 additions & 0 deletions src/providers/sambanova/chatComplete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { SAMBANOVA } from '../../globals';
import {
ChatCompletionResponse,
ErrorResponse,
ProviderConfig,
} from '../types';

export const SamvaNovaChatCompleteConfig: ProviderConfig = {
model: {
param: 'model',
required: true,
default: 'Meta-Llama-3.1-8B-Instruct',
},
messages: {
param: 'messages',
default: '',
},
max_tokens: {
param: 'max_tokens',
default: 100,
min: 0,
},
// Currently FastAPI supports stream responses only.
stream: {
param: 'stream',
default: true,
},
stream_options: {
param: 'stream_options',
},
stop: {
param: 'stop',
},
};

export interface SamvaNovaChatCompleteResponse extends ChatCompletionResponse {}

export interface SamvaNovaErrorResponse extends ErrorResponse {}

export interface SamvaNovaStreamChunk {
id: string;
object: string;
created: number;
model: string;
system_fingerprint: string;
choices: {
delta: {
content?: string;
};
index: number;
finish_reason: string | null;
logprobs: object | null;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we just do Record instead of object here?

}[];
usage: {
is_last_response: boolean;
total_tokens: number;
prompt_tokens: number;
completion_tokens: number;
time_to_first_token: number;
end_time: number;
start_time: number;
total_latency: number;
total_tokens_per_sec: number;
completion_tokens_per_sec: number;
completion_tokens_after_first_per_sec: number;
completion_tokens_after_first_per_sec_first_ten: number;
} | null;
}

export const SamvaNovaChatCompleteStreamChunkTransform: (
response: string
) => string = (responseChunk) => {
let chunk = responseChunk.trim();
chunk = chunk.replace(/^data: /, '');
chunk = chunk.trim();
if (chunk === '[DONE]') {
return `data: ${chunk}\n\n`;
}

const parsedChunk: SamvaNovaStreamChunk = JSON.parse(chunk);
if (parsedChunk.usage) {
return `data: ${JSON.stringify({
id: parsedChunk.id,
object: parsedChunk.object,
created: parsedChunk.created,
model: parsedChunk.model,
provider: SAMBANOVA,
choices: [],
ilsubyeega marked this conversation as resolved.
Show resolved Hide resolved
usage: {
prompt_tokens: parsedChunk.usage.prompt_tokens || 0,
completion_tokens: parsedChunk.usage.completion_tokens || 0,
total_tokens: parsedChunk.usage.total_tokens || 0,
},
})}\n\n`;
}
return `data: ${JSON.stringify({
id: parsedChunk.id,
object: parsedChunk.object,
created: parsedChunk.created,
model: parsedChunk.model,
provider: SAMBANOVA,
choices: [
ilsubyeega marked this conversation as resolved.
Show resolved Hide resolved
{
index: parsedChunk.choices[0].index || 0,
delta: {
role: 'assistant',
content: parsedChunk.choices[0].delta.content,
},
logprobs: null,
ilsubyeega marked this conversation as resolved.
Show resolved Hide resolved
finish_reason: parsedChunk.choices[0].finish_reason || null,
},
],
})}\n\n`;
};
16 changes: 16 additions & 0 deletions src/providers/sambanova/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ProviderConfigs } from '../types';
import SambaNovaAPIConfig from './api';
import {
SamvaNovaChatCompleteConfig,
SamvaNovaChatCompleteStreamChunkTransform,
} from './chatComplete';

const SambaNovaConfig: ProviderConfigs = {
chatComplete: SamvaNovaChatCompleteConfig,
api: SambaNovaAPIConfig,
responseTransforms: {
'stream-chatComplete': SamvaNovaChatCompleteStreamChunkTransform,
},
};

export default SambaNovaConfig;
Loading