-
Notifications
You must be signed in to change notification settings - Fork 507
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 NovitaAI Provider #303
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,20 @@ | ||
import { ProviderAPIConfig } from '../types'; | ||
|
||
const NovitaAIApiConfig: ProviderAPIConfig = { | ||
getBaseURL: () => 'https://api.novita.ai/v3/openai', | ||
headers: ({ providerOptions }) => { | ||
return { Authorization: `Bearer ${providerOptions.apiKey}` }; | ||
}, | ||
getEndpoint: ({ fn }) => { | ||
switch (fn) { | ||
case 'complete': | ||
return '/v1/completions'; | ||
case 'chatComplete': | ||
return '/v1/chat/completions'; | ||
AnyISalIn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
default: | ||
return ''; | ||
} | ||
}, | ||
}; | ||
|
||
export default NovitaAIApiConfig; |
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,220 @@ | ||
import { NOVITA_AI } from '../../globals'; | ||
import { | ||
ChatCompletionResponse, | ||
ErrorResponse, | ||
ProviderConfig, | ||
} from '../types'; | ||
import { | ||
generateErrorResponse, | ||
generateInvalidProviderResponseError, | ||
} from '../utils'; | ||
|
||
// TODOS: this configuration does not enforce the maximum token limit for the input parameter. If you want to enforce this, you might need to add a custom validation function or a max property to the ParameterConfig interface, and then use it in the input configuration. However, this might be complex because the token count is not a simple length check, but depends on the specific tokenization method used by the model. | ||
|
||
export const NovitaAIChatCompleteConfig: ProviderConfig = { | ||
AnyISalIn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
model: { | ||
param: 'model', | ||
required: true, | ||
default: 'lzlv_70b', | ||
}, | ||
messages: { | ||
param: 'messages', | ||
required: true, | ||
default: '', | ||
}, | ||
max_tokens: { | ||
param: 'max_tokens', | ||
required: true, | ||
default: 128, | ||
min: 1, | ||
}, | ||
stop: { | ||
param: 'stop', | ||
}, | ||
temperature: { | ||
param: 'temperature', | ||
}, | ||
top_p: { | ||
param: 'top_p', | ||
}, | ||
n: { | ||
param: 'n', | ||
}, | ||
top_k: { | ||
param: 'top_k', | ||
}, | ||
presence_penalty: { | ||
param: 'presence_penalty', | ||
min: -2, | ||
max: 2, | ||
}, | ||
frequency_penalty: { | ||
param: 'frequency_penalty', | ||
min: -2, | ||
max: 2, | ||
}, | ||
stream: { | ||
param: 'stream', | ||
default: false, | ||
}, | ||
logprobs: { | ||
param: 'logprobs', | ||
}, | ||
tools: { | ||
param: 'tools', | ||
}, | ||
tool_choice: { | ||
param: 'tool_choice', | ||
}, | ||
response_format: { | ||
param: 'response_format', | ||
}, | ||
}; | ||
|
||
export interface NovitaAIChatCompleteResponse extends ChatCompletionResponse { | ||
usage: { | ||
prompt_tokens: number; | ||
completion_tokens: number; | ||
total_tokens: number; | ||
}; | ||
} | ||
|
||
export interface NovitaAIErrorResponse { | ||
model: string; | ||
job_id: string; | ||
request_id: string; | ||
error: string; | ||
message?: string; | ||
type?: string; | ||
} | ||
|
||
export interface NovitaAIOpenAICompatibleErrorResponse extends ErrorResponse {} | ||
|
||
export interface NovitaAIChatCompletionStreamChunk { | ||
id: string; | ||
request_id: string; | ||
object: string; | ||
choices: { | ||
index: number; | ||
delta: { | ||
content: string; | ||
}; | ||
}[]; | ||
} | ||
|
||
export const NovitaAIErrorResponseTransform: ( | ||
response: NovitaAIErrorResponse | NovitaAIOpenAICompatibleErrorResponse | ||
) => ErrorResponse | false = (response) => { | ||
if ('error' in response && typeof response.error === 'string') { | ||
return generateErrorResponse( | ||
{ message: response.error, type: null, param: null, code: null }, | ||
NOVITA_AI | ||
); | ||
} | ||
|
||
if ('error' in response && typeof response.error === 'object') { | ||
return generateErrorResponse( | ||
{ | ||
message: response.error?.message || '', | ||
type: response.error?.type || null, | ||
param: response.error?.param || null, | ||
code: response.error?.code || null, | ||
}, | ||
NOVITA_AI | ||
); | ||
} | ||
|
||
if ('message' in response && response.message) { | ||
return generateErrorResponse( | ||
{ | ||
message: response.message, | ||
type: response.type || null, | ||
param: null, | ||
code: null, | ||
}, | ||
NOVITA_AI | ||
); | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
export const NovitaAIChatCompleteResponseTransform: ( | ||
response: | ||
| NovitaAIChatCompleteResponse | ||
| NovitaAIErrorResponse | ||
| NovitaAIOpenAICompatibleErrorResponse, | ||
responseStatus: number | ||
) => ChatCompletionResponse | ErrorResponse = (response, responseStatus) => { | ||
if (responseStatus !== 200) { | ||
const errorResponse = NovitaAIErrorResponseTransform( | ||
response as NovitaAIErrorResponse | ||
); | ||
if (errorResponse) return errorResponse; | ||
} | ||
|
||
if ('choices' in response) { | ||
return { | ||
id: response.id, | ||
object: response.object, | ||
created: response.created, | ||
model: response.model, | ||
provider: NOVITA_AI, | ||
choices: response.choices.map((choice) => { | ||
return { | ||
message: { | ||
role: 'assistant', | ||
content: choice.message.content, | ||
tool_calls: choice.message.tool_calls | ||
? choice.message.tool_calls.map((toolCall: any) => ({ | ||
id: toolCall.id, | ||
type: toolCall.type, | ||
function: toolCall.function, | ||
})) | ||
: null, | ||
}, | ||
index: 0, | ||
logprobs: null, | ||
finish_reason: choice.finish_reason, | ||
}; | ||
}), | ||
usage: { | ||
prompt_tokens: response.usage?.prompt_tokens, | ||
completion_tokens: response.usage?.completion_tokens, | ||
total_tokens: response.usage?.total_tokens, | ||
}, | ||
}; | ||
} | ||
|
||
return generateInvalidProviderResponseError(response, NOVITA_AI); | ||
}; | ||
|
||
export const NovitaAIChatCompleteStreamChunkTransform: ( | ||
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: NovitaAIChatCompletionStreamChunk = JSON.parse(chunk); | ||
return ( | ||
`data: ${JSON.stringify({ | ||
id: parsedChunk.id, | ||
object: parsedChunk.object, | ||
created: Math.floor(Date.now() / 1000), | ||
model: '', | ||
AnyISalIn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
provider: NOVITA_AI, | ||
choices: [ | ||
{ | ||
delta: { | ||
content: parsedChunk.choices[0]?.delta.content, | ||
}, | ||
index: 0, | ||
finish_reason: '', | ||
}, | ||
], | ||
})}` + '\n\n' | ||
); | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
end point seems to be incorrect.
please refer to https://novita.ai/get-started/llm.html#example-with-curl-client
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi it's correct, we use the
/v3/openai
prefix, so the complete URL will be /v3/openai/v1/completion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The final url as mentioned in the documentation is
https://api.novita.ai/v3/openai/completions
But the above configuration will result in final url being
https://api.novita.ai/v3/openai/v1/completions
Nothing in the documentation mentions
/v1
being neededThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hey! I noticed that both
https://api.novita.ai/v3/openai/v1/completions
andhttps://api.novita.ai/v3/openai/completions
are behaving similarly. But the novita's official documentation does not mentionhttps://api.novita.ai/v3/openai/v1/completions
being the end point as mentioned here https://novita.ai/get-started/llm.html#example-with-curl-client. Can you remove the/v1
so that it is compliant with their official documentation?