UNPKG

gemini-ai-sdk

Version:
141 lines (136 loc) 4.72 kB
import { HarmCategory, HarmBlockThreshold, DynamicRetrievalMode, Part, Content, GenerationConfig, SafetySetting, Tool, GenerateContentResult, GenerateContentStreamResult } from '@google/generative-ai'; declare const safetyDisabledSettings: { category: HarmCategory; threshold: HarmBlockThreshold; }[]; declare const defaultTools: { webSearch: { googleSearchRetrieval: { dynamicRetrievalConfig: { mode: DynamicRetrievalMode; dynamicThreshold: number; }; }; }; codeExecution: { codeExecution: {}; }; }; declare const constants_defaultTools: typeof defaultTools; declare const constants_safetyDisabledSettings: typeof safetyDisabledSettings; declare namespace constants { export { constants_defaultTools as defaultTools, constants_safetyDisabledSettings as safetyDisabledSettings }; } /** * Represents a file to be uploaded. */ type FileUpload = { buffer: ArrayBuffer; filePath: string; }; /** * Checks if the given data is a FileUpload object. * @param data - The data to check. * @returns True if the data is a FileUpload, false otherwise. */ declare function isFileUpload(data: any): data is FileUpload; /** * Determines the file type of a buffer. * @param buffer - The buffer to analyze. * @param filePath - Optional file path for fallback. * @param options - Options for strict checking. * @returns The detected file type. */ declare const getFileType: (buffer: Uint8Array | ArrayBuffer, filePath?: string | undefined, { strict }?: { strict?: boolean; }) => Promise<string>; /** * Options for initializing a Gemini instance. */ interface GeminiOptions { apiVersion?: string; fetch?: typeof fetch; } /** * Options for the `ask` method, as well as the `Chat` class. */ interface AskOptions { stream?: boolean; model?: string; history?: Content[]; generationConfig?: GenerationConfig; safetySettings?: SafetySetting[]; systemInstruction?: Content; tools?: Tool[]; } /** * Types for various methods. */ type GenerateResult = GenerateContentResult | GenerateContentStreamResult; /** * Represents a chat session with Gemini. */ declare class Chat { private gemini; private history; private options; /** * Creates a new Chat instance. * @param gemini - The Gemini instance to use. * @param options - Optional parameters for the chat. */ constructor(gemini: Gemini, options?: Partial<AskOptions>); /** * Appends a message to the chat history. * @param message - The message to append. */ appendMessage(message: Content): void; /** * Sends a message to Gemini and returns the response. * @param message - The message to send. * @param options - Optional parameters for the request. * @returns The response from Gemini. If options.stream = false, returns a single object. * Otherwise, returns an AsyncGenerator of results. */ ask(message: string | Part[], options?: Partial<AskOptions>): Promise<GenerateResult>; } /** * Main class for interacting with the Gemini API. */ declare class Gemini { private genAI; private options; /** * Creates a new Gemini instance. * @param apiKey - Your API key for the Gemini API. * @param options - Optional parameters for initializing the instance. */ constructor(apiKey: string, options?: Partial<GeminiOptions>); /** * Uploads a file to the Gemini API. * @param options - Options for the file upload. * @returns The URI of the uploaded file. */ private uploadFile; /** * Converts a list of messages or files to Gemini API parts. * @param messages - The messages or files to convert. * @returns An array of Gemini API parts. */ messageToParts(messages: (string | Uint8Array | ArrayBuffer | FileUpload)[]): Promise<Part[]>; /** * Sends a request to the Gemini API and returns the response. * @param message - The message to send. * @param options - Optional parameters for the request. * @returns The response from the Gemini API. If options.stream = false, returns a single object. * Otherwise, returns an AsyncGenerator of results. */ ask(message: string | Part[], options?: Partial<AskOptions>): Promise<GenerateResult>; /** * Creates a new chat session. * @param options - Optional parameters for the chat. * @returns A new Chat instance. */ createChat(options?: Partial<AskOptions>): Chat; } export { type AskOptions, Chat, type FileUpload, Gemini, type GeminiOptions, type GenerateResult, constants, Gemini as default, getFileType, isFileUpload };