UNPKG

genkitx-ollama

Version:

Genkit AI framework plugin for Ollama APIs.

1 lines 4.8 kB
{"version":3,"sources":["../src/types.ts"],"sourcesContent":["/**\n * Copyright 2024 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { z, type GenerateRequest } from 'genkit';\nimport type { EmbedRequest } from 'ollama';\n// Define possible API types\nexport type ApiType = 'chat' | 'generate';\n\n// Standard model definition\nexport interface ModelDefinition {\n name: string;\n type?: ApiType;\n supports?: {\n tools?: boolean;\n };\n}\n\n// Definition for embedding models\nexport interface EmbeddingModelDefinition {\n name: string;\n dimensions: number;\n}\n\nexport const OllamaEmbeddingPredictionSchema = z.object({\n embedding: z.array(z.number()),\n});\n\nexport type OllamaEmbeddingPrediction = z.infer<\n typeof OllamaEmbeddingPredictionSchema\n>;\n\nexport interface DefineOllamaEmbeddingParams {\n name: string;\n modelName: string;\n dimensions: number;\n options: OllamaPluginParams;\n}\n\n/**\n * Parameters for the Ollama plugin configuration.\n */\nexport interface OllamaPluginParams {\n /**\n * Array of models to be defined.\n *\n * ```ts\n * const ai = genkit({\n * plugins: [\n * ollama({\n * models: [{ name: 'gemma' }],\n * serverAddress: 'http://127.0.0.1:11434', // default local address\n * }),\n * ],\n * });\n * ```\n */\n models?: ModelDefinition[];\n\n /**\n * Array of embedding models to be defined.\n *\n * ```ts\n * const ai = genkit({\n * plugins: [\n * ollama({\n * serverAddress: 'http://localhost:11434',\n * embedders: [{ name: 'nomic-embed-text', dimensions: 768 }],\n * }),\n * ],\n * });\n * ```\n */\n embedders?: EmbeddingModelDefinition[];\n\n /**\n * The address of the Ollama server. Default: http://localhost:11434\n */\n serverAddress?: string;\n\n /**\n * Optional request headers, which can be either static or dynamically generated.\n *\n * ```ts\n * const ai = genkit({\n * plugins: [\n * ollama({\n * models: [...],\n * serverAddress: 'https://my-deployment.server.address',\n * requestHeaders: async (params) => {\n * const headers = await fetchAuthHeaders(params.serverAddress);\n * return { Authorization: headers['Authorization'] };\n * },\n * }),\n * ],\n * });\n * ```\n */\n requestHeaders?: RequestHeaders;\n}\n\n// Function type for generating request headers dynamically\nexport type RequestHeaderFunction = (\n params: {\n serverAddress: string;\n model?: ModelDefinition | EmbeddingModelDefinition;\n modelRequest?: GenerateRequest;\n embedRequest?: EmbedRequest;\n },\n // @deprecated -- moved into params, here for backwards compatibility reasons.\n modelRequest?: GenerateRequest\n) => Promise<Record<string, string> | void>;\n\n// Union type for request headers, supporting both static and dynamic options\nexport type RequestHeaders = Record<string, string> | RequestHeaderFunction;\n\nexport type OllamaRole = 'assistant' | 'tool' | 'system' | 'user';\n\n// Tool definition from Ollama Chat API\nexport interface OllamaTool {\n type: string;\n function: {\n name: string;\n description: string;\n // `parameters` should be valid JSON Schema\n parameters: Record<string, any>;\n };\n}\n\n// Tool Call from Ollama Chat API\nexport interface OllamaToolCall {\n function: {\n index?: number;\n name: string;\n arguments: Record<string, any>;\n };\n}\n\n// Message format as defined by Ollama API\nexport interface Message {\n role: string;\n content: string;\n images?: string[];\n tool_calls?: any[];\n}\n\n// Ollama local model definition\nexport interface LocalModel {\n name: string;\n model: string;\n // ISO 8601 format date\n modified_at: string;\n size: number;\n digest: string;\n details?: {\n parent_model?: string;\n format?: string;\n family?: string;\n families?: string[];\n parameter_size?: string;\n quantization_level?: string;\n };\n}\n\n// Ollama list local models response\nexport interface ListLocalModelsResponse {\n models: LocalModel[];\n}\n"],"mappings":"AAgBA,SAAS,SAA+B;AAoBjC,MAAM,kCAAkC,EAAE,OAAO;AAAA,EACtD,WAAW,EAAE,MAAM,EAAE,OAAO,CAAC;AAC/B,CAAC;","names":[]}