-
Notifications
You must be signed in to change notification settings - Fork 452
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #548 from ilsubyeega/provider-sambanova
provider: SambaNova FastAPI implementation.
- Loading branch information
Showing
6 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { ProviderAPIConfig } from '../types'; | ||
|
||
const SambaNovaAPIConfig: ProviderAPIConfig = { | ||
getBaseURL: ({ providerOptions }) => | ||
providerOptions.customHost || 'https://fast-api.snova.ai', | ||
headers: ({ providerOptions }) => { | ||
return { Authorization: `Bearer ${providerOptions.apiKey}` }; | ||
}, | ||
getEndpoint: ({ fn }) => { | ||
switch (fn) { | ||
case 'chatComplete': | ||
return '/v1/chat/completions'; | ||
default: | ||
return ''; | ||
} | ||
}, | ||
}; | ||
|
||
export default SambaNovaAPIConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { SAMBANOVA } from '../../globals'; | ||
|
||
export interface SambaNovaStreamChunk { | ||
id: string; | ||
object: string; | ||
created: number; | ||
model: string; | ||
system_fingerprint: string; | ||
choices: { | ||
delta: { | ||
content?: string; | ||
}; | ||
index: number; | ||
finish_reason: string | null; | ||
logprobs: object | null; | ||
}[]; | ||
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; | ||
}; | ||
} | ||
|
||
export const SambaNovaChatCompleteStreamChunkTransform: ( | ||
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: SambaNovaStreamChunk = 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: [ | ||
{ | ||
index: 0, | ||
delta: {}, | ||
logprobs: null, | ||
finish_reason: 'stop', | ||
}, | ||
], | ||
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: [ | ||
{ | ||
index: parsedChunk.choices[0].index || 0, | ||
delta: { | ||
role: 'assistant', | ||
content: parsedChunk.choices[0].delta.content, | ||
}, | ||
logprobs: parsedChunk.choices[0].logprobs, | ||
finish_reason: parsedChunk.choices[0].finish_reason || null, | ||
}, | ||
], | ||
})}\n\n`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { SAMBANOVA } from '../../globals'; | ||
import { chatCompleteParams, responseTransformers } from '../open-ai-base'; | ||
import { ProviderConfigs } from '../types'; | ||
import SambaNovaAPIConfig from './api'; | ||
import { SambaNovaChatCompleteStreamChunkTransform } from './chatComplete'; | ||
|
||
const SambaNovaConfig: ProviderConfigs = { | ||
chatComplete: chatCompleteParams( | ||
[ | ||
'functions', | ||
'function_call', | ||
'presence_penalty', | ||
'frequency_penalty', | ||
'logit_bias', | ||
'user', | ||
'seed', | ||
'tools', | ||
'tool_choice', | ||
'response_format', | ||
'logprobs', | ||
], | ||
{ | ||
model: 'Meta-Llama-3.1-8B-Instruct', | ||
} | ||
), | ||
api: SambaNovaAPIConfig, | ||
responseTransforms: { | ||
...responseTransformers(SAMBANOVA, { | ||
chatComplete: (response, isError) => { | ||
if (isError || 'choices' in response === false) return response; | ||
|
||
return { | ||
...response, | ||
provider: SAMBANOVA, | ||
choices: response.choices.map((choice) => ({ | ||
...choice, | ||
message: { | ||
role: 'assistant', | ||
...(choice.message as any), | ||
}, | ||
})), | ||
usage: { | ||
prompt_tokens: response.usage?.prompt_tokens || 0, | ||
completion_tokens: response.usage?.completion_tokens || 0, | ||
total_tokens: response.usage?.total_tokens || 0, | ||
}, | ||
}; | ||
}, | ||
}), | ||
'stream-chatComplete': SambaNovaChatCompleteStreamChunkTransform, | ||
}, | ||
}; | ||
|
||
export default SambaNovaConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters