Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ollama-ai-provider

Package Overview
Dependencies
Maintainers
0
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ollama-ai-provider - npm Package Compare versions

Comparing version 0.10.0 to 0.11.0

4

dist/index.d.ts
import { LanguageModelV1, EmbeddingModelV1 } from '@ai-sdk/provider';
type OllamaChatModelId = 'codellama' | 'codellama:7b' | 'codellama:13b' | 'codellama:34b' | 'codellama:70b' | 'codellama:code' | 'codellama:python' | 'falcon2' | 'falcon2:11b' | 'gemma' | 'gemma:2b' | 'gemma:7b' | 'gemma2' | 'gemma:9b' | 'gemma:27b' | 'llama2' | 'llama2:7b' | 'llama2:13b' | 'llama2:70b' | 'llama3' | 'llama3:8b' | 'llama3:70b' | 'llama3-chatqa' | 'llama3-chatqa:8b' | 'llama3-chatqa:70b' | 'llama3-gradient' | 'llama3-gradient:8b' | 'llama3-gradient:70b' | 'llava' | 'llava:7b' | 'llava:13b' | 'llava:34b' | 'llava-llama3' | 'llava-llama3:8b' | 'llava-phi3' | 'llava-phi3:3.8b' | 'mistral' | 'mistral:7b' | 'mixtral' | 'mixtral:8x7b' | 'mixtral:8x22b' | 'moondream' | 'moondream:1.8b' | 'openhermes' | 'openhermes:v2.5' | 'qwen' | 'qwen:7b' | 'qwen:14b' | 'qwen:32b' | 'qwen:72b' | 'qwen:110b' | 'qwen2' | 'qwen2:0.5b' | 'qwen2:1.5b' | 'qwen2:7b' | 'qwen2:72b' | 'phi3' | 'phi3:3.8b' | (string & NonNullable<unknown>);
type OllamaChatModelId = 'codegemma' | 'codegemma:2b' | 'codegemma:7b' | 'codellama' | 'codellama:7b' | 'codellama:13b' | 'codellama:34b' | 'codellama:70b' | 'codellama:code' | 'codellama:python' | 'command-r' | 'command-r:35b' | 'command-r-plus' | 'command-r-plus:104b' | 'deepseek-coder-v2' | 'deepseek-coder-v2:16b' | 'deepseek-coder-v2:236b' | 'falcon2' | 'falcon2:11b' | 'gemma' | 'gemma:2b' | 'gemma:7b' | 'gemma2' | 'gemma2:2b' | 'gemma2:9b' | 'gemma2:27b' | 'llama2' | 'llama2:7b' | 'llama2:13b' | 'llama2:70b' | 'llama3' | 'llama3:8b' | 'llama3:70b' | 'llama3-chatqa' | 'llama3-chatqa:8b' | 'llama3-chatqa:70b' | 'llama3-gradient' | 'llama3-gradient:8b' | 'llama3-gradient:70b' | 'llama3.1' | 'llama3.1:8b' | 'llama3.1:70b' | 'llama3.1:405b' | 'llava' | 'llava:7b' | 'llava:13b' | 'llava:34b' | 'llava-llama3' | 'llava-llama3:8b' | 'llava-phi3' | 'llava-phi3:3.8b' | 'mistral' | 'mistral:7b' | 'mistral-large' | 'mistral-large:123b' | 'mistral-nemo' | 'mistral-nemo:12b' | 'mixtral' | 'mixtral:8x7b' | 'mixtral:8x22b' | 'moondream' | 'moondream:1.8b' | 'openhermes' | 'openhermes:v2.5' | 'qwen' | 'qwen:7b' | 'qwen:14b' | 'qwen:32b' | 'qwen:72b' | 'qwen:110b' | 'qwen2' | 'qwen2:0.5b' | 'qwen2:1.5b' | 'qwen2:7b' | 'qwen2:72b' | 'phi3' | 'phi3:3.8b' | 'phi3:14b' | (string & NonNullable<unknown>);
interface OllamaChatSettings {

@@ -61,2 +61,3 @@ /**

readonly defaultObjectGenerationMode = "json";
readonly supportsImageUrls = false;
constructor(modelId: OllamaChatModelId, settings: OllamaChatSettings, config: OllamaChatConfig);

@@ -72,2 +73,3 @@ get provider(): string;

maxEmbeddingsPerCall?: number;
truncate?: boolean;
}

@@ -74,0 +76,0 @@

@@ -30,8 +30,7 @@ "use strict";

// src/ollama-facade.ts
var import_provider_utils7 = require("@ai-sdk/provider-utils");
var import_provider_utils6 = require("@ai-sdk/provider-utils");
// src/ollama-chat-language-model.ts
var import_provider3 = require("@ai-sdk/provider");
var import_provider_utils6 = require("@ai-sdk/provider-utils");
var import_zod3 = require("zod");
var import_provider_utils5 = require("@ai-sdk/provider-utils");
var import_zod2 = require("zod");

@@ -41,44 +40,8 @@ // src/convert-to-ollama-chat-messages.ts

var import_provider_utils = require("@ai-sdk/provider-utils");
// src/generate-tool/inject-tools-schema-into-system.ts
var DEFAULT_SCHEMA_PREFIX = "You have access to the following tools:";
var DEFAULT_SCHEMA_SUFFIX = `To use a tool, you MUST answer with a JSON object with the following structure:
[
{
"name": <name of the called tool>,
"arguments": <arguments for the tool matching the above JSON schema>
}
]`;
function injectToolsSchemaIntoSystem({
schemaPrefix = DEFAULT_SCHEMA_PREFIX,
schemaSuffix = DEFAULT_SCHEMA_SUFFIX,
system,
toolChoice,
tools
}) {
const selectedTools = tools == null ? void 0 : tools.filter(
(tool) => !toolChoice || tool.name === toolChoice
);
if (!selectedTools) {
return system;
}
return [system, schemaPrefix, JSON.stringify(selectedTools), schemaSuffix].filter((line) => line !== null).join("\n");
}
// src/convert-to-ollama-chat-messages.ts
function convertToOllamaChatMessages(prompt, tools, toolChoice) {
function convertToOllamaChatMessages(prompt) {
const messages = [];
let hasSystem = false;
for (const { content, role } of prompt) {
switch (role) {
case "system": {
messages.push({
content: injectToolsSchemaIntoSystem({
system: content,
toolChoice,
tools
}),
role: "system"
});
hasSystem = true;
messages.push({ content, role: "system" });
break;

@@ -94,3 +57,3 @@ }

throw new import_provider.UnsupportedFunctionalityError({
functionality: "image-part"
functionality: "Image URLs in user messages"
});

@@ -110,14 +73,46 @@ } else if (current.type === "image" && current.image instanceof Uint8Array) {

case "assistant": {
const text = [];
const toolCalls = [];
for (const part of content) {
switch (part.type) {
case "text": {
text.push(part.text);
break;
}
case "tool-call": {
toolCalls.push({
function: {
arguments: part.args,
name: part.toolName
},
id: part.toolCallId,
type: "function"
});
break;
}
default: {
const _exhaustiveCheck = part;
throw new Error(`Unsupported part: ${_exhaustiveCheck}`);
}
}
}
messages.push({
content: content.map((part) => {
switch (part.type) {
case "text": {
return part.text;
}
}
}).join(""),
role: "assistant"
content: text.join(","),
role: "assistant",
tool_calls: toolCalls.length > 0 ? toolCalls : void 0
});
break;
}
case "tool": {
messages.push(
...content.map((part) => ({
// Non serialized contents are not accepted by ollama, triggering the following error:
// "json: cannot unmarshal array into Go struct field ChatRequest.messages of type string"
content: typeof part.result === "object" ? JSON.stringify(part.result) : `${part.result}`,
role: "tool",
tool_call_id: part.toolCallId
}))
);
break;
}
default: {

@@ -129,63 +124,7 @@ const _exhaustiveCheck = role;

}
if (!hasSystem && tools) {
messages.unshift({
content: injectToolsSchemaIntoSystem({
system: "",
toolChoice,
tools
}),
role: "system"
});
}
return messages;
}
// src/generate-tool/infer-tool-calls-from-response.ts
// src/generate-tool/infer-tool-calls-from-stream.ts
var import_provider_utils2 = require("@ai-sdk/provider-utils");
var import_zod = require("zod");
function inferToolCallsFromResponse(response) {
try {
const tool = JSON.parse(response.message.content);
let parsedTools = toolResponseSchema.parse(tool);
if (!Array.isArray(parsedTools)) {
parsedTools = [parsedTools];
}
return {
...response,
finish_reason: "tool-calls",
message: {
content: "",
role: "assistant",
tool_calls: parsedTools.map((parsedTool) => ({
function: {
arguments: JSON.stringify(parsedTool.arguments),
name: parsedTool.name
},
id: (0, import_provider_utils2.generateId)(),
type: "function"
}))
}
};
} catch (e) {
return {
...response,
finish_reason: "stop"
};
}
}
var toolResponseSchema = import_zod.z.union([
import_zod.z.array(
import_zod.z.object({
arguments: import_zod.z.record(import_zod.z.unknown()),
name: import_zod.z.string()
})
),
import_zod.z.object({
arguments: import_zod.z.record(import_zod.z.unknown()),
name: import_zod.z.string()
})
]);
// src/generate-tool/infer-tool-calls-from-stream.ts
var import_provider_utils3 = require("@ai-sdk/provider-utils");
var import_partial_json = require("partial-json");

@@ -221,3 +160,3 @@ var InferToolCallsFromStream = class {

for (const [index, parsedFunction] of parsedFunctions.entries()) {
const parsedArguments = (_a = JSON.stringify(parsedFunction == null ? void 0 : parsedFunction.arguments)) != null ? _a : "";
const parsedArguments = (_a = JSON.stringify(parsedFunction == null ? void 0 : parsedFunction.parameters)) != null ? _a : "";
if (parsedArguments === "") {

@@ -232,3 +171,3 @@ continue;

},
id: (0, import_provider_utils3.generateId)(),
id: (0, import_provider_utils2.generateId)(),
type: "function"

@@ -282,7 +221,9 @@ };

// src/map-ollama-finish-reason.ts
function mapOllamaFinishReason(finishReason) {
function mapOllamaFinishReason({
finishReason,
hasToolCalls
}) {
switch (finishReason) {
case "stop":
case "tool-calls": {
return finishReason;
case "stop": {
return hasToolCalls ? "tool-calls" : "stop";
}

@@ -296,13 +237,13 @@ default: {

// src/ollama-error.ts
var import_provider_utils4 = require("@ai-sdk/provider-utils");
var import_zod2 = require("zod");
var ollamaErrorDataSchema = import_zod2.z.object({
error: import_zod2.z.object({
code: import_zod2.z.string().nullable(),
message: import_zod2.z.string(),
param: import_zod2.z.any().nullable(),
type: import_zod2.z.string()
var import_provider_utils3 = require("@ai-sdk/provider-utils");
var import_zod = require("zod");
var ollamaErrorDataSchema = import_zod.z.object({
error: import_zod.z.object({
code: import_zod.z.string().nullable(),
message: import_zod.z.string(),
param: import_zod.z.any().nullable(),
type: import_zod.z.string()
})
});
var ollamaFailedResponseHandler = (0, import_provider_utils4.createJsonErrorResponseHandler)({
var ollamaFailedResponseHandler = (0, import_provider_utils3.createJsonErrorResponseHandler)({
errorSchema: ollamaErrorDataSchema,

@@ -321,3 +262,3 @@ errorToMessage: (data) => data.error.message

var import_provider2 = require("@ai-sdk/provider");
var import_provider_utils5 = require("@ai-sdk/provider-utils");
var import_provider_utils4 = require("@ai-sdk/provider-utils");

@@ -349,3 +290,3 @@ // src/utils/text-line-stream.ts

var createJsonStreamResponseHandler = (chunkSchema) => async ({ response }) => {
const responseHeaders = (0, import_provider_utils5.extractResponseHeaders)(response);
const responseHeaders = (0, import_provider_utils4.extractResponseHeaders)(response);
if (response.body === null) {

@@ -360,3 +301,3 @@ throw new import_provider2.EmptyResponseBodyError({});

controller.enqueue(
(0, import_provider_utils5.safeParseJSON)({
(0, import_provider_utils4.safeParseJSON)({
schema: chunkSchema,

@@ -380,2 +321,3 @@ text: chunkText

this.defaultObjectGenerationMode = "json";
this.supportsImageUrls = false;
}

@@ -424,3 +366,3 @@ get provider() {

...baseArguments,
messages: convertToOllamaChatMessages(prompt, tools),
messages: convertToOllamaChatMessages(prompt),
tools: tools == null ? void 0 : tools.map((tool) => ({

@@ -454,8 +396,3 @@ function: {

...baseArguments,
format: "json",
messages: convertToOllamaChatMessages(
prompt,
[mode.tool],
mode.tool.name
),
messages: convertToOllamaChatMessages(prompt),
tool_choice: {

@@ -480,7 +417,2 @@ function: { name: mode.tool.name },

}
case "object-grammar": {
throw new import_provider3.UnsupportedFunctionalityError({
functionality: "object-grammar mode"
});
}
default: {

@@ -495,3 +427,3 @@ const _exhaustiveCheck = type;

const { args, warnings } = this.getArguments(options);
const { responseHeaders, value } = await (0, import_provider_utils6.postJsonToApi)({
const { responseHeaders, value: response } = await (0, import_provider_utils5.postJsonToApi)({
abortSignal: options.abortSignal,

@@ -505,3 +437,3 @@ body: {

headers: this.config.headers(),
successfulResponseHandler: (0, import_provider_utils6.createJsonResponseHandler)(
successfulResponseHandler: (0, import_provider_utils5.createJsonResponseHandler)(
ollamaChatResponseSchema

@@ -511,15 +443,21 @@ ),

});
const response = inferToolCallsFromResponse(value);
const { messages: rawPrompt, ...rawSettings } = args;
const toolCalls = (_a = response.message.tool_calls) == null ? void 0 : _a.map((toolCall) => {
var _a2;
return {
args: JSON.stringify(toolCall.function.arguments),
toolCallId: (_a2 = toolCall.id) != null ? _a2 : (0, import_provider_utils5.generateId)(),
toolCallType: "function",
toolName: toolCall.function.name
};
});
return {
finishReason: mapOllamaFinishReason(response.finish_reason),
finishReason: mapOllamaFinishReason({
finishReason: response.done_reason,
hasToolCalls: toolCalls !== void 0 && toolCalls.length > 0
}),
rawCall: { rawPrompt, rawSettings },
rawResponse: { headers: responseHeaders },
text: (_a = response.message.content) != null ? _a : void 0,
toolCalls: (_b = response.message.tool_calls) == null ? void 0 : _b.map((toolCall) => ({
args: toolCall.function.arguments,
toolCallId: (0, import_provider_utils6.generateId)(),
toolCallType: "function",
toolName: toolCall.function.name
})),
text: (_b = response.message.content) != null ? _b : void 0,
toolCalls,
usage: {

@@ -534,3 +472,3 @@ completionTokens: response.eval_count || 0,

const { args, type, warnings } = this.getArguments(options);
const { responseHeaders, value: response } = await (0, import_provider_utils6.postJsonToApi)({
const { responseHeaders, value: response } = await (0, import_provider_utils5.postJsonToApi)({
abortSignal: options.abortSignal,

@@ -575,3 +513,3 @@ body: args,

completionTokens: value.eval_count,
promptTokens: Number.NaN
promptTokens: value.prompt_eval_count || 0
};

@@ -600,46 +538,47 @@ return;

};
var ollamaChatResponseSchema = import_zod3.z.object({
created_at: import_zod3.z.string(),
done: import_zod3.z.literal(true),
eval_count: import_zod3.z.number(),
eval_duration: import_zod3.z.number(),
finish_reason: import_zod3.z.string().optional().nullable(),
load_duration: import_zod3.z.number().optional(),
message: import_zod3.z.object({
content: import_zod3.z.string(),
role: import_zod3.z.string(),
tool_calls: import_zod3.z.array(
import_zod3.z.object({
function: import_zod3.z.object({
arguments: import_zod3.z.string(),
name: import_zod3.z.string()
})
var ollamaChatResponseSchema = import_zod2.z.object({
created_at: import_zod2.z.string(),
done: import_zod2.z.literal(true),
done_reason: import_zod2.z.string().optional().nullable(),
eval_count: import_zod2.z.number(),
eval_duration: import_zod2.z.number(),
load_duration: import_zod2.z.number().optional(),
message: import_zod2.z.object({
content: import_zod2.z.string(),
role: import_zod2.z.string(),
tool_calls: import_zod2.z.array(
import_zod2.z.object({
function: import_zod2.z.object({
arguments: import_zod2.z.record(import_zod2.z.any()),
name: import_zod2.z.string()
}),
id: import_zod2.z.string().optional()
})
).optional().nullable()
}),
model: import_zod3.z.string(),
prompt_eval_count: import_zod3.z.number().optional(),
prompt_eval_duration: import_zod3.z.number().optional(),
total_duration: import_zod3.z.number()
model: import_zod2.z.string(),
prompt_eval_count: import_zod2.z.number().optional(),
prompt_eval_duration: import_zod2.z.number().optional(),
total_duration: import_zod2.z.number()
});
var ollamaChatStreamChunkSchema = import_zod3.z.discriminatedUnion("done", [
import_zod3.z.object({
created_at: import_zod3.z.string(),
done: import_zod3.z.literal(false),
message: import_zod3.z.object({
content: import_zod3.z.string(),
role: import_zod3.z.string()
var ollamaChatStreamChunkSchema = import_zod2.z.discriminatedUnion("done", [
import_zod2.z.object({
created_at: import_zod2.z.string(),
done: import_zod2.z.literal(false),
message: import_zod2.z.object({
content: import_zod2.z.string(),
role: import_zod2.z.string()
}),
model: import_zod3.z.string()
model: import_zod2.z.string()
}),
import_zod3.z.object({
created_at: import_zod3.z.string(),
done: import_zod3.z.literal(true),
eval_count: import_zod3.z.number(),
eval_duration: import_zod3.z.number(),
load_duration: import_zod3.z.number().optional(),
model: import_zod3.z.string(),
prompt_eval_count: import_zod3.z.number().optional(),
prompt_eval_duration: import_zod3.z.number().optional(),
total_duration: import_zod3.z.number()
import_zod2.z.object({
created_at: import_zod2.z.string(),
done: import_zod2.z.literal(true),
eval_count: import_zod2.z.number(),
eval_duration: import_zod2.z.number(),
load_duration: import_zod2.z.number().optional(),
model: import_zod2.z.string(),
prompt_eval_count: import_zod2.z.number().optional(),
prompt_eval_duration: import_zod2.z.number().optional(),
total_duration: import_zod2.z.number()
})

@@ -652,3 +591,3 @@ ]);

var _a;
this.baseURL = (_a = (0, import_provider_utils7.withoutTrailingSlash)(options.baseURL)) != null ? _a : "http://127.0.0.1:11434/api";
this.baseURL = (_a = (0, import_provider_utils6.withoutTrailingSlash)(options.baseURL)) != null ? _a : "http://127.0.0.1:11434/api";
this.headers = options.headers;

@@ -673,8 +612,8 @@ }

// src/ollama-provider.ts
var import_provider_utils9 = require("@ai-sdk/provider-utils");
var import_provider_utils8 = require("@ai-sdk/provider-utils");
// src/ollama-embedding-model.ts
var import_provider4 = require("@ai-sdk/provider");
var import_provider_utils8 = require("@ai-sdk/provider-utils");
var import_zod4 = require("zod");
var import_provider3 = require("@ai-sdk/provider");
var import_provider_utils7 = require("@ai-sdk/provider-utils");
var import_zod3 = require("zod");
var OllamaEmbeddingModel = class {

@@ -692,3 +631,3 @@ constructor(modelId, settings, config) {

var _a;
return (_a = this.settings.maxEmbeddingsPerCall) != null ? _a : 1;
return (_a = this.settings.maxEmbeddingsPerCall) != null ? _a : 2048;
}

@@ -703,3 +642,3 @@ get supportsParallelCalls() {

if (values.length > this.maxEmbeddingsPerCall) {
throw new import_provider4.TooManyEmbeddingValuesForCallError({
throw new import_provider3.TooManyEmbeddingValuesForCallError({
maxEmbeddingsPerCall: this.maxEmbeddingsPerCall,

@@ -715,25 +654,23 @@ modelId: this.modelId,

};
for (const value of values) {
const { responseHeaders, value: response } = await (0, import_provider_utils8.postJsonToApi)({
abortSignal,
body: {
model: this.modelId,
prompt: value
},
failedResponseHandler: ollamaFailedResponseHandler,
fetch: this.config.fetch,
headers: this.config.headers(),
successfulResponseHandler: (0, import_provider_utils8.createJsonResponseHandler)(
ollamaTextEmbeddingResponseSchema
),
url: `${this.config.baseURL}/embeddings`
});
embeddings.embeddings.push(response.embedding);
embeddings.rawResponse = { headers: responseHeaders };
}
const { responseHeaders, value: response } = await (0, import_provider_utils7.postJsonToApi)({
abortSignal,
body: {
input: values,
model: this.modelId
},
failedResponseHandler: ollamaFailedResponseHandler,
fetch: this.config.fetch,
headers: this.config.headers(),
successfulResponseHandler: (0, import_provider_utils7.createJsonResponseHandler)(
ollamaTextEmbeddingResponseSchema
),
url: `${this.config.baseURL}/embed`
});
embeddings.embeddings = response.embeddings;
embeddings.rawResponse = { headers: responseHeaders };
return embeddings;
}
};
var ollamaTextEmbeddingResponseSchema = import_zod4.z.object({
embedding: import_zod4.z.array(import_zod4.z.number())
var ollamaTextEmbeddingResponseSchema = import_zod3.z.object({
embeddings: import_zod3.z.array(import_zod3.z.array(import_zod3.z.number()))
});

@@ -744,3 +681,3 @@

var _a;
const baseURL = (_a = (0, import_provider_utils9.withoutTrailingSlash)(options.baseURL)) != null ? _a : "http://127.0.0.1:11434/api";
const baseURL = (_a = (0, import_provider_utils8.withoutTrailingSlash)(options.baseURL)) != null ? _a : "http://127.0.0.1:11434/api";
const getHeaders = () => ({

@@ -747,0 +684,0 @@ ...options.headers

{
"name": "ollama-ai-provider",
"version": "0.10.0",
"version": "0.11.0",
"description": "Vercel AI Provider for running LLMs locally using Ollama",

@@ -18,5 +18,5 @@ "main": "./dist/index.js",

"dependencies": {
"@ai-sdk/provider": "0.0.11",
"@ai-sdk/provider-utils": "1.0.0",
"partial-json": "^0.1.7"
"@ai-sdk/provider": "0.0.14",
"@ai-sdk/provider-utils": "1.0.5",
"partial-json": "0.1.7"
},

@@ -23,0 +23,0 @@ "devDependencies": {

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc