diff --git a/src/core.ts b/src/core.ts index 6e6b634..d707fe1 100644 --- a/src/core.ts +++ b/src/core.ts @@ -3,6 +3,7 @@ import { BaseConfig } from "../types"; import AudioApis from "./audio/audio"; import Classification from "./classification/index"; import General from "./general"; +import PromptEngine from "./prompt_engine"; import { RequestClient } from "./request"; import { File } from "./store/file"; import Validate from "./validate"; @@ -29,6 +30,7 @@ export const JigsawStack = (config?: BaseConfig) => { const audio = new AudioApis(client); const file = new File(client); const validate = new Validate(client); + const promptengine = new PromptEngine(client); const store = { upload: file.upload, retrieve: file.retrieve, @@ -59,6 +61,7 @@ export const JigsawStack = (config?: BaseConfig) => { }, store, validate, + prompt_engine: promptengine, classification: classification.classify, }; }; diff --git a/src/prompt_engine/index.ts b/src/prompt_engine/index.ts new file mode 100644 index 0000000..811e635 --- /dev/null +++ b/src/prompt_engine/index.ts @@ -0,0 +1,50 @@ +import { Stream } from "../lib/streaming"; +import { RequestClient } from "../request"; +import { + PromptCreateParams, + PromptExecuteParams, + PromptGetResponse, + PromptListParams, + PromptListResponse, + PromptRunParams, + RunPromptDirectResponse, + RunPromptResponse, + RunPromptResponseStream, +} from "./interfaces"; + +class PromptEngine { + constructor(private readonly client: RequestClient) {} + create = async (params: PromptCreateParams): Promise<{ prompt_engine_id: string }> => { + return await this.client.fetchJSS("/prompt_engine", "POST", params); + }; + + run_prompt_direct(params: PromptRunParams & { stream: true }): Promise>; + run_prompt_direct(params: PromptRunParams & { stream?: false }): Promise; + async run_prompt_direct(params: PromptRunParams): Promise> { + const resp = await this.client.fetchJSS(`/prompt_engine/run`, "POST", params); + if (!params.stream) { + return resp as RunPromptDirectResponse; + } + return Stream.fromReadableStream(resp.body) as RunPromptResponseStream; + } + get = async (id: string): Promise => { + return await this.client.fetchJSS(`/prompt_engine/${id}`, "GET", {}); + }; + list = async (params: PromptListParams = { limit: 20, page: 0 }): Promise => { + return await this.client.fetchJSS("/prompt_engine", "GET", {}, params); + }; + delete = async (id: string): Promise<{ prompt_engine_id: string }> => { + return await this.client.fetchJSS(`/prompt_engine/${id}`, "DELETE", {}); + }; + run(params: PromptExecuteParams & { stream: true }): Promise>; + run(params: PromptExecuteParams & { stream?: false }): Promise; + async run(params: PromptExecuteParams): Promise | RunPromptResponse> { + const resp = await this.client.fetchJSS(`/prompt_engine/${params.id}`, "POST", params); + if (!params.stream) { + return resp as RunPromptResponse; + } + return Stream.fromReadableStream(resp.body) as RunPromptResponseStream; + } +} + +export default PromptEngine; diff --git a/src/prompt_engine/interfaces.ts b/src/prompt_engine/interfaces.ts new file mode 100644 index 0000000..bb450f9 --- /dev/null +++ b/src/prompt_engine/interfaces.ts @@ -0,0 +1,69 @@ +import { Stream } from "../lib/streaming"; + +export interface PromptCreateParams { + prompt: string; + return_prompt?: string | Array> | Record; + inputs?: Array<{ + key: string; + optional?: boolean; + initial_value?: string; + }>; + use_internet?: boolean; + optimize_prompt?: boolean; + prompt_guard?: Array< + | "defamation" + | "specialized_advice" + | "privacy" + | "intellectual_property" + | "indiscriminate_weapons" + | "hate" + | "sexual_content" + | "elections" + | "code_interpreter_abuse" + >; +} + +export interface PromptRunParams extends Omit { + input_values?: Record; + stream?: boolean; +} + +export interface PromptExecuteParams { + id: string; + input_values?: Record; + stream?: boolean; +} + +export interface PromptListParams { + page?: number; + limit?: number; +} + +export interface PromptResult { + id: string; + prompt: string; + inputs: Array<{ + key: string; + optional: boolean; + }>; + return_prompt: string; + created_at: string; +} + +export interface PromptGetResponse extends PromptResult { + success: boolean; +} + +export interface PromptListResponse extends PromptResult { + prompt_engines: PromptResult[]; +} + +export interface RunPromptResponse { + result: any; + success: boolean; + message?: string; +} + +export interface RunPromptResponseStream extends Stream {} + +export interface RunPromptDirectResponse extends RunPromptResponse {}