Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 2x 2x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x | import { generateInvalidProviderResponseError } from '@api/utils/ai-provider';
import type {
AIProviderFunctionConfig,
ResponseChunkStreamTransformFunction,
ResponseTransformFunction,
} from '@shared/types/ai-providers/config';
import type { ChatCompletionRequestBody } from '@shared/types/api/routes/chat-completions-api';
import { ChatCompletionMessageRole } from '@shared/types/api/routes/shared/messages';
import { AIProvider } from '@shared/types/constants';
import type { OllamaChatCompleteResponse, OllamaStreamChunk } from './types';
import { ollamaErrorResponseTransform } from './utils';
export const ollamaChatCompleteConfig: AIProviderFunctionConfig = {
model: {
param: 'model',
required: true,
default: 'llama3.2:latest',
},
messages: {
param: 'messages',
default: '',
transform: (params: ChatCompletionRequestBody) => {
return params.messages?.map((message) => {
if (message.role === ChatCompletionMessageRole.DEVELOPER)
return { ...message, role: ChatCompletionMessageRole.SYSTEM };
return message;
});
},
},
frequency_penalty: {
param: 'frequency_penalty',
min: -2,
max: 2,
},
presence_penalty: {
param: 'presence_penalty',
min: -2,
max: 2,
},
response_format: {
param: 'response_format',
},
seed: {
param: 'seed',
},
stop: {
param: 'stop',
},
stream: {
param: 'stream',
default: false,
},
temperature: {
param: 'temperature',
default: 1,
min: 0,
max: 2,
},
top_p: {
param: 'top_p',
default: 1,
min: 0,
max: 1,
},
max_tokens: {
param: 'max_tokens',
default: 100,
min: 0,
},
max_completion_tokens: {
param: 'max_tokens',
default: 100,
min: 0,
},
tools: {
param: 'tools',
},
};
export const ollamaChatCompleteResponseTransform: ResponseTransformFunction = (
aiProviderResponseBody,
aiProviderResponseStatus,
_aiProviderResponseHeaders,
_strictOpenAiCompliance,
_raRequestData,
) => {
if (aiProviderResponseStatus !== 200 && 'error' in aiProviderResponseBody) {
return ollamaErrorResponseTransform(
aiProviderResponseBody,
aiProviderResponseStatus,
);
}
const response =
aiProviderResponseBody as unknown as OllamaChatCompleteResponse;
if ('choices' in response) {
// Construct usage object from Ollama's token counts if usage is not provided
const usage = response.usage ?? {
prompt_tokens: response.prompt_eval_count ?? 0,
completion_tokens: response.eval_count ?? 0,
total_tokens:
(response.prompt_eval_count ?? 0) + (response.eval_count ?? 0),
};
return {
id: response.id,
object: response.object,
created: response.created,
model: response.model,
provider: AIProvider.OLLAMA,
choices: response.choices,
usage,
};
}
return generateInvalidProviderResponseError(response, AIProvider.OLLAMA);
};
export const ollamaChatCompleteStreamChunkTransform: ResponseChunkStreamTransformFunction =
(
responseChunk,
_fallbackId,
_streamState,
_strictOpenAiCompliance,
_raRequestData,
) => {
const chunk = responseChunk
.trim()
.replace(/^data: /, '')
.trim();
if (chunk === '[DONE]') {
return `data: ${chunk}\n\n`;
}
const parsedChunk: OllamaStreamChunk = JSON.parse(chunk);
return `data: ${JSON.stringify({
id: parsedChunk.id,
object: parsedChunk.object,
created: parsedChunk.created,
model: parsedChunk.model,
provider: AIProvider.OLLAMA,
choices: parsedChunk.choices,
})}\n\n`;
};
|