|
| 1 | +import { Stream } from "../lib/streaming"; |
| 2 | +import { RequestClient } from "../request"; |
| 3 | +import { |
| 4 | + PromptCreateParams, |
| 5 | + PromptExecuteParams, |
| 6 | + PromptGetResponse, |
| 7 | + PromptListParams, |
| 8 | + PromptListResponse, |
| 9 | + PromptRunParams, |
| 10 | + RunPromptDirectResponse, |
| 11 | + RunPromptResponse, |
| 12 | + RunPromptResponseStream, |
| 13 | +} from "./interfaces"; |
| 14 | + |
| 15 | +class PromptEngine { |
| 16 | + constructor(private readonly client: RequestClient) {} |
| 17 | + create = async (params: PromptCreateParams): Promise<{ prompt_engine_id: string }> => { |
| 18 | + return await this.client.fetchJSS("/prompt_engine", "POST", params); |
| 19 | + }; |
| 20 | + |
| 21 | + run_prompt_direct(params: PromptRunParams & { stream: true }): Promise<RunPromptResponseStream<string>>; |
| 22 | + run_prompt_direct(params: PromptRunParams & { stream?: false }): Promise<RunPromptDirectResponse>; |
| 23 | + async run_prompt_direct(params: PromptRunParams): Promise<RunPromptDirectResponse | RunPromptResponseStream<string>> { |
| 24 | + const resp = await this.client.fetchJSS(`/prompt_engine/run`, "POST", params); |
| 25 | + if (!params.stream) { |
| 26 | + return resp as RunPromptDirectResponse; |
| 27 | + } |
| 28 | + return Stream.fromReadableStream<string>(resp.body) as RunPromptResponseStream<string>; |
| 29 | + } |
| 30 | + get = async (id: string): Promise<PromptGetResponse> => { |
| 31 | + return await this.client.fetchJSS(`/prompt_engine/${id}`, "GET", {}); |
| 32 | + }; |
| 33 | + list = async (params: PromptListParams = { limit: 20, page: 0 }): Promise<PromptListResponse> => { |
| 34 | + return await this.client.fetchJSS("/prompt_engine", "GET", {}, params); |
| 35 | + }; |
| 36 | + delete = async (id: string): Promise<{ prompt_engine_id: string }> => { |
| 37 | + return await this.client.fetchJSS(`/prompt_engine/${id}`, "DELETE", {}); |
| 38 | + }; |
| 39 | + run(params: PromptExecuteParams & { stream: true }): Promise<RunPromptResponseStream<string>>; |
| 40 | + run(params: PromptExecuteParams & { stream?: false }): Promise<RunPromptResponse>; |
| 41 | + async run(params: PromptExecuteParams): Promise<RunPromptResponseStream<string> | RunPromptResponse> { |
| 42 | + const resp = await this.client.fetchJSS(`/prompt_engine/${params.id}`, "POST", params); |
| 43 | + if (!params.stream) { |
| 44 | + return resp as RunPromptResponse; |
| 45 | + } |
| 46 | + return Stream.fromReadableStream<string>(resp.body) as RunPromptResponseStream<string>; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +export default PromptEngine; |
0 commit comments