UNPKG

ai-functions

Version:

A powerful TypeScript library for building AI-powered applications with template literals and structured outputs

131 lines 4.96 kB
import { z } from 'zod'; import { streamObject, streamText } from 'ai'; import { getProvider } from '../providers/config'; export async function* generateStreamingList(prompt, options) { const modelParams = { temperature: options.temperature, maxTokens: options.maxTokens, topP: options.topP, frequencyPenalty: options.frequencyPenalty, presencePenalty: options.presencePenalty, stopSequences: options.stop ? Array.isArray(options.stop) ? options.stop : [options.stop] : undefined, seed: options.seed }; const model = options.model || getProvider()('gpt-4o-mini'); try { if (options.signal?.aborted) { throw new Error('Stream was aborted'); } const { elementStream } = streamObject({ model, output: 'array', schema: z.string(), prompt: `Generate a list of items based on this prompt: ${prompt}`, system: options.system, ...modelParams, experimental_streamingFunction: true, onChunk: ({ chunk }) => { if (chunk.type === 'text-delta' && chunk.text && options.streaming?.onProgress) { options.streaming.onProgress(chunk.text); } if (options.streaming?.onChunk) { options.streaming.onChunk(chunk); } } }); try { for await (const item of elementStream) { if (options.signal?.aborted) { throw new Error('Stream was aborted'); } if (options.streaming?.onProgress) { options.streaming.onProgress(item); } yield item; } } catch (error) { if (error instanceof Error) { if (error.name === 'AbortError' || error.code === 'ABORT_ERR' || options.signal?.aborted) { throw new Error('Stream was aborted'); } throw error; } throw error; } } catch (error) { if (error instanceof Error) { if (error.name === 'AbortError' || error.code === 'ABORT_ERR' || options.signal?.aborted) { throw new Error('Stream was aborted'); } else if (error.name === 'TimeoutError') { throw new Error('Stream timed out'); } throw error; } throw new Error('Failed to generate list: Unknown error occurred'); } } export async function* generateStreamingText(prompt, options) { const model = options.model || getProvider()('gpt-4o-mini'); try { if (options.signal?.aborted) { throw new Error('Stream was aborted'); } const { textStream } = streamText({ model, prompt, system: options.system, temperature: options.temperature, maxTokens: options.maxTokens, topP: options.topP, frequencyPenalty: options.frequencyPenalty, presencePenalty: options.presencePenalty, stopSequences: options.stop ? Array.isArray(options.stop) ? options.stop : [options.stop] : undefined, seed: options.seed, experimental_streamingFunction: true, onChunk: ({ chunk }) => { if (chunk.type === 'text-delta' && chunk.text && options.streaming?.onProgress) { options.streaming.onProgress(chunk.text); } if (options.streaming?.onChunk) { options.streaming.onChunk(chunk); } } }); try { for await (const text of textStream) { if (options.signal?.aborted) { throw new Error('Stream was aborted'); } if (options.streaming?.onProgress) { options.streaming.onProgress(text); } yield text; } } catch (error) { if (error instanceof Error) { if (error.name === 'AbortError' || error.code === 'ABORT_ERR' || options.signal?.aborted) { throw new Error('Stream was aborted'); } throw error; } throw error; } } catch (error) { if (error instanceof Error) { if (error.name === 'AbortError' || error.code === 'ABORT_ERR' || options.signal?.aborted) { throw new Error('Stream was aborted'); } else if (error.name === 'TimeoutError') { throw new Error('Stream timed out'); } throw error; } throw new Error('Failed to generate text: Unknown error occurred'); } } //# sourceMappingURL=utils.js.map