ollama_generate
Generate single-turn text completions from prompts using local Ollama models. Provide a model name and prompt to create structured outputs in JSON or Markdown format.
Instructions
Generate completion from a prompt. Simpler than chat, useful for single-turn completions.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| model | Yes | Name of the model to use | |
| prompt | Yes | The prompt to generate from | |
| options | No | Generation options (optional). Provide as JSON object with settings like temperature, top_p, etc. | |
| format | No | json |
Implementation Reference
- src/tools/generate.ts:11-27 (handler)Core handler logic: calls ollama.generate with the provided model, prompt, options, and formats the response using formatResponse.export async function generateWithModel( ollama: Ollama, model: string, prompt: string, options: GenerationOptions, format: ResponseFormat ): Promise<string> { const response = await ollama.generate({ model, prompt, options, format: format === ResponseFormat.JSON ? 'json' : undefined, stream: false, }); return formatResponse(response.response, format); }
- src/schemas.ts:103-109 (schema)Zod schema used for validating the input arguments in the tool handler.export const GenerateInputSchema = z.object({ model: z.string().min(1), prompt: z.string(), options: parseJsonOrDefault({}).pipe(GenerationOptionsSchema), format: ResponseFormatSchema.default('json'), stream: z.boolean().default(false), });
- src/tools/generate.ts:29-66 (registration)Tool registration: exports toolDefinition with name 'ollama_generate', description, input schema (for tool description), and handler that validates args and calls generateWithModel.export const toolDefinition: ToolDefinition = { name: 'ollama_generate', description: 'Generate completion from a prompt. Simpler than chat, useful for single-turn completions.', inputSchema: { type: 'object', properties: { model: { type: 'string', description: 'Name of the model to use', }, prompt: { type: 'string', description: 'The prompt to generate from', }, options: { type: 'string', description: 'Generation options (optional). Provide as JSON object with settings like temperature, top_p, etc.', }, format: { type: 'string', enum: ['json', 'markdown'], default: 'json', }, }, required: ['model', 'prompt'], }, handler: async (ollama: Ollama, args: Record<string, unknown>, format: ResponseFormat) => { const validated = GenerateInputSchema.parse(args); return generateWithModel( ollama, validated.model, validated.prompt, validated.options || {}, format ); }, };