### Install OmiAI SDK using npm Source: https://github.com/jigsawstack/omiai/blob/main/README.md This snippet demonstrates how to install the OmiAI SDK package using the npm package manager. It's the foundational step to integrate OmiAI into your project, making its functionalities available for use. ```bash npm i omiai ``` -------------------------------- ### Generate Text with OmiAI (Basic Usage) Source: https://github.com/jigsawstack/omiai/blob/main/README.md This example illustrates the fundamental usage of OmiAI to generate text based on a simple prompt. It involves importing `createOmiAI`, initializing an OmiAI instance, and then calling the `generate` method to obtain a text response from the underlying LLM. ```typescript import { createOmiAI } from "omiai"; const omi = createOmiAI(); const result = await omi.generate({ prompt: "What is the meaning of life?", }); console.log(result?.text); ``` -------------------------------- ### Use Message Array for Prompts Source: https://github.com/jigsawstack/omiai/blob/main/README.md This example shows how to provide prompts to OmiAI using a message array format, similar to conversational AI interfaces. This allows for defining roles (e.g., 'user', 'assistant') and content for more complex, multi-turn interactions with the LLM. ```typescript const result = await omi.generate({ prompt: [{ role: "user", content: "What is the meaning of life?" }], }); console.log(result?.text); ``` -------------------------------- ### Stream Text Output from OmiAI Source: https://github.com/jigsawstack/omiai/blob/main/README.md This example shows how to stream text responses from the OmiAI SDK for real-time updates. By setting `stream: true`, the `textStream` iterable is used to process chunks of text as they become available, enabling dynamic display of generated content. ```typescript const result = await omi.generate({ prompt: "Tell me a story of a person who discovered the meaning of life.", stream: true, }); let text = ""; for await (const chunk of result?.textStream) { text += chunk; console.clear(); console.log(text); } ``` -------------------------------- ### Force or Disable Reasoning in OmiAI Source: https://github.com/jigsawstack/omiai/blob/main/README.md This example shows how to explicitly control OmiAI's built-in reasoning feature. By setting the `reasoning` parameter to `true` or `false`, users can override the automatic reasoning decision for specific prompts and retrieve the reasoning text for analysis. ```typescript const result = await omi.generate({ prompt: "How many r's are there in the text: 'strawberry'?", reasoning: true, schema: z.object({ answer: z.number(), }), }); console.log("reason: ", result?.reasoningText); console.log("result: ", result?.object); ``` -------------------------------- ### Configure OmiAI with API Keys Source: https://github.com/jigsawstack/omiai/blob/main/README.md This code shows how to initialize the OmiAI SDK by passing API keys for various LLM providers directly to the `createOmiAI` function. This method provides an alternative to using environment variables for sensitive credentials, allowing for explicit configuration. ```typescript const omi = createOmiAI({ openaiProviderConfig: { apiKey: process.env.OPENAI_API_KEY, }, anthropicProviderConfig: { apiKey: process.env.ANTHROPIC_API_KEY, }, ... }); ``` -------------------------------- ### Omiai Generate Method Parameters (APIDOC) Source: https://github.com/jigsawstack/omiai/blob/main/README.md Defines the parameters for the `omi.generate` method, detailing options for streaming, reasoning, multi-LLM, system prompts, structured output schemas, context tools (like web search), auto-tooling, temperature, topK, topP, and custom tools. It also describes the structure for `GeneratePromptObj` for message-based prompts. ```APIDOC interface GenerateParams { stream?: boolean; // Optional: Enable streaming output. reasoning?: boolean; // Auto turns on depending on the prompt. Set to true to force reasoning. Set to false to disable auto-reasoning. multiLLM?: boolean; // Turn on if you want to run your prompt across all models then merge the results. system?: string; // Optional: System prompt. prompt: string | GeneratePromptObj[]; // String prompt or array which will treated as messages. schema?: z.ZodSchema; // Optional: Schema to use for structured output. contextTool?: { web?: boolean; // Auto turns on depending on the prompt. Set to true to force web-search. Set to false to disable web search. }; // Optional: Configuration for context tools. autoTool?: boolean; // Auto turns on depending on the prompt. Set to true to force tool-calling. Set to false to disable tool-calling. temperature?: number; // Optional: Sampling temperature. topK?: number; // Optional: Top-K sampling. topP?: number; // Optional: Top-P sampling. tools?: { // Optional: Custom tools to pass to the SDK. [key: string]: ReturnType; }; } interface GeneratePromptObj { role: CoreMessage["role"]; // Role of the message (e.g., 'user', 'assistant'). content: | string // Text content. | { // Array of content parts. type: "text" | "image" | "file"; // Type of content. data: string; // URL or base64 data. mimeType?: string; // Optional: MIME type of the file. }[]; } ``` -------------------------------- ### Generate with Multi-LLM Source: https://github.com/jigsawstack/omiai/blob/main/README.md Demonstrates how to use the Multi-LLM feature to run prompts across multiple LLMs for more accurate and cross-checked results. Be aware that this can increase costs due to parallel execution across several models. ```ts const result = await omi.generate({ prompt: "What is the meaning of life?", multiLLM: true, }); ``` -------------------------------- ### Implement Custom Tool Calling Source: https://github.com/jigsawstack/omiai/blob/main/README.md Illustrates how to define and pass custom tools to the Omiai SDK using the `tools` parameter, enabling the LLM to interact with external functionalities like a weather service. The tool function parameters are based on Vercel's AI SDK. ```ts import { createOmiAI, tool } from "omiai"; const omi = createOmiAI(); const result = await omi.generate({ prompt: "What is the weather in San Francisco?", tools: { weather: tool({ description: "Get the weather in a location", parameters: z.object({ location: z.string().describe("The location to get the weather for"), }), execute: async ({ location }) => ({ location, temperature: 72 + Math.floor(Math.random() * 21) - 10, }), }), }, }); ``` -------------------------------- ### Attach Images and Files to Prompts Source: https://github.com/jigsawstack/omiai/blob/main/README.md This snippet demonstrates OmiAI's multimodal capabilities by attaching an image URL to a prompt. It combines text and image input to perform tasks like extracting information from an image, leveraging built-in tools such as OCR for advanced processing. ```typescript const result = await omi.generate({ prompt: [ { role: "user", content: [ { type: "text", data: "Extract the total price of the items in the image", //will tool call OCR tool }, { type: "image", data: "https://media.snopes.com/2021/08/239918331_10228097135359041_3825446756894757753_n.jpg", mimeType: "image/jpg", }, ], }, ], schema: z.object({ total_price: z .number() .describe("The total price of the items in the image"), }), }); console.log(result?.object); ``` -------------------------------- ### Generate Image with Omiai Source: https://github.com/jigsawstack/omiai/blob/main/README.md Demonstrates how to generate an image using the `omi.generate` method by providing a prompt. The generated image data is accessible as a Blob from the result. ```ts const result = await omi.generate({ prompt: "Generate an image of a cat", }); const blob: Blob = result?.files?.[0].data; ``` -------------------------------- ### Force Web Search in Generation Source: https://github.com/jigsawstack/omiai/blob/main/README.md Illustrates how to explicitly enable web search for a generation prompt using the `contextTool.web` parameter. By default, web search is automatically decided based on the prompt, but it can be forced on or off. ```ts const result = await omi.generate({ prompt: "What won the US presidential election in 2025?", contextTool: { web: true, }, }); ``` -------------------------------- ### Generate Structured Output with Zod Schema Source: https://github.com/jigsawstack/omiai/blob/main/README.md This snippet demonstrates how to enforce a structured output from the LLM using Zod schemas. The `generate` method is called with a prompt and a Zod object schema, ensuring the response conforms to the defined structure and type, which is useful for reliable data parsing. ```typescript import { z } from "zod"; const result = await omi.generate({ prompt: "How many r's are there in the word 'strawberries'?", schema: z.object({ answer: z.number().describe("The answer to the question"), }), }); console.log(result?.object); ``` -------------------------------- ### Omiai Embedding Method Parameters (APIDOC) Source: https://github.com/jigsawstack/omiai/blob/main/README.md Defines the parameters for the `omi.embedding` method, specifying the type of content to embed (audio, image, pdf, text, text-other) and providing options for text content, URL, or file content. ```APIDOC interface EmbeddingParams { type: "audio" | "image" | "pdf" | "text" | "text-other"; // Type of content to embed. text?: string; // Optional: Text content for embedding. url?: string; // Optional: URL of the content for embedding. fileContent?: string; // Optional: Base64 encoded file content for embedding. } ``` -------------------------------- ### Generate Embeddings from Text or PDF Source: https://github.com/jigsawstack/omiai/blob/main/README.md Shows how to generate embeddings using the `omi.embedding` method for different input types like text and PDF. The embedding model is powered by JigsawStack. ```ts const result = await omi.embedding({ type: "text", text: "Hello, world!", }); console.log(result.embeddings); ``` ```ts const result = await omi.embedding({ type: "pdf", url: "https://example.com/file.pdf", }); console.log(result.embeddings); ``` -------------------------------- ### Stream Structured Object Output Source: https://github.com/jigsawstack/omiai/blob/main/README.md This snippet demonstrates streaming structured object output from OmiAI using a Zod schema. It allows for partial object updates as the LLM generates the response, which is useful for displaying progress or incomplete data in applications that require real-time structured information. ```typescript const result = await omi.generate({ prompt: "Tell me a story of a person who discovered the meaning of life.", schema: z.object({ story: z.string().max(1000).describe("The story"), character_names: z .array(z.string()) .describe("The names of the characters in the story"), }), stream: true, }); for await (const chunk of result?.partialObjectStream ?? []) { console.log(chunk); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.