generate_text
Generate AI text content using Pollinations.ai models. Provide a prompt to create text responses, customize with model selection, system instructions, and output formatting options.
Instructions
使用Pollinations.ai生成文本
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| prompt | Yes | 文本提示词 | |
| model | No | 要使用的模型(如openai、mistral等) | openai |
| seed | No | 随机种子值(用于生成一致的结果) | |
| system | No | 系统提示词(设置AI行为) | |
| json | No | 是否返回JSON格式的响应 | |
| private | No | 设置为true可使响应私有 |
Implementation Reference
- src/index.ts:572-616 (handler)The primary handler function for the 'generate_text' tool. Validates input arguments, calls the PollinationsTextAPI to generate text, formats the response, and handles errors appropriately.private async handleGenerateText(args: any) { try { if (!this.isValidGenerateTextArgs(args)) { throw this.handleValidationError('无效的文本生成参数'); } const { prompt, model = 'openai', seed, system, json = false, private: isPrivate = false } = args; const result = await this.textAPI.generateTextGet(prompt, { model, seed, json, system, private: isPrivate }); return { content: [ { type: 'text', text: typeof result === 'string' ? result : JSON.stringify(result, null, 2), }, ], }; } catch (error) { // 处理所有错误 let pollinationsError: PollinationsError; if (error instanceof PollinationsError) { pollinationsError = error; } else { pollinationsError = this.handleApiError(error); } return { content: [ { type: 'text', text: pollinationsError.toUserFriendlyMessage(), }, ], isError: true, }; } }
- src/index.ts:619-637 (schema)Type guard function that validates the input arguments for the generate_text tool, ensuring correct types for prompt, model, seed, system, json, and private parameters.private isValidGenerateTextArgs(args: any): args is { prompt: string; model?: string; seed?: number; system?: string; json?: boolean; private?: boolean; } { return ( typeof args === 'object' && args !== null && typeof args.prompt === 'string' && (args.model === undefined || typeof args.model === 'string') && (args.seed === undefined || typeof args.seed === 'number') && (args.system === undefined || typeof args.system === 'string') && (args.json === undefined || typeof args.json === 'boolean') && (args.private === undefined || typeof args.private === 'boolean') ); }
- src/index.ts:227-263 (registration)Tool registration in the ListToolsRequestSchema handler, defining the name, description, and input schema for the 'generate_text' tool.{ name: 'generate_text', description: '使用Pollinations.ai生成文本', inputSchema: { type: 'object', properties: { prompt: { type: 'string', description: '文本提示词', }, model: { type: 'string', description: '要使用的模型(如openai、mistral等)', default: 'openai', }, seed: { type: 'number', description: '随机种子值(用于生成一致的结果)', }, system: { type: 'string', description: '系统提示词(设置AI行为)', }, json: { type: 'boolean', description: '是否返回JSON格式的响应', default: false, }, private: { type: 'boolean', description: '设置为true可使响应私有', default: false, }, }, required: ['prompt'], }, },
- src/index.ts:48-81 (helper)Helper method in PollinationsTextAPI class that constructs the API URL and makes the HTTP GET request to Pollinations.ai text generation endpoint.async generateTextGet(prompt: string, options: { model?: string; seed?: number; json?: boolean; system?: string; private?: boolean; } = {}) { const { model = 'openai', seed, json = false, system, private: isPrivate = false } = options; let url = `${this.baseTextUrl}/${encodeURIComponent(prompt)}?model=${model}`; if (seed !== undefined) { url += `&seed=${seed}`; } if (json) { url += `&json=true`; } if (system) { url += `&system=${encodeURIComponent(system)}`; } if (isPrivate) { url += `&private=true`; } try { const response = await axios.get(url); return response.data; } catch (error) { throw new Error(`文本生成失败: ${error instanceof Error ? error.message : String(error)}`); } }
- src/index.ts:274-275 (registration)Dispatch case in the CallToolRequestSchema handler that routes 'generate_text' calls to the handleGenerateText method.case 'generate_text': return this.handleGenerateText(request.params.arguments);