promptgun 0.8.7 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -63,14 +63,12 @@ await ai
63
63
  )
64
64
  )
65
65
  )
66
- .onElement(restaurantStream => restaurantStream
67
- .onCompleteAs(restaurant => {
68
- // do stuff every time a restaurant has come through
69
- })
70
- )
66
+ .onElement(restaurantStream /* type safe */ => {
67
+ console.log(await restaurantStream)
68
+ })
71
69
  ```
72
- #### Streamed, "dumb"
73
- You can also simply get the partial data, "what has come in so far". The data is parsed for you, even as the underlying JSON that is received is incomplete.
70
+
71
+ And iterating (not awaiting) any streamed json element simply gives the stream of accumulated partial data, "what has come in so far" as it comes in. The data is parsed for you, even as the underlying JSON that is received is incomplete.
74
72
  ```typescript
75
73
  const parsedPartialJsonStream = ai
76
74
  .chat('What are the top 5 restaurants in New York City?')
@@ -87,6 +85,25 @@ for await (const parsedPartialJson of parsedPartialJsonStream) {
87
85
  }
88
86
  ```
89
87
 
88
+ ### Image out
89
+ ```typescript
90
+ await ai
91
+ .image('A black hole')
92
+ .imageSize('1024x1024') // optional
93
+ .model('gpt-image-1') // optional, default: gpt-image-1
94
+ .toFile('blackhole.png')
95
+ ```
96
+ This writes the file, but it also returns a reference to that file:
97
+ ```typescript
98
+ const file = await ai
99
+ .image('A black hole')
100
+ .toFile('blackhole.png')
101
+ ```
102
+ You can also avoid writing a file altogether and get the byte array directly:
103
+ ```typescript
104
+ const byteArray = await ai.image('A black hole')
105
+ ```
106
+
90
107
  ## Setup
91
108
  Before you do any prompts, do:
92
109
  ```typescript
package/build/index.d.ts CHANGED
@@ -18,6 +18,7 @@ export declare class AIClient {
18
18
  chat<T extends undefined>(toPrompt: (args?: T) => Prompt): BasicPrompt<T>;
19
19
  chat<T>(toPrompt: (args: T) => Prompt, args: T): BasicPrompt<T>;
20
20
  chat<T = undefined>(toPrompt: (args?: T) => Prompt, args?: T): BasicPrompt<T>;
21
+ image(prompt: string): ImagePrompt;
21
22
  }
22
23
 
23
24
  export declare const AiModel: {
@@ -885,6 +886,8 @@ export declare type DeepPartial<T> = T extends object ? {
885
886
  [P in keyof T]?: DeepPartial<T[P]>;
886
887
  } : T;
887
888
 
889
+ export declare type ElementType<ArrayType extends AnyJson> = ArrayType extends readonly (infer ElementType)[] ? ElementType : never;
890
+
888
891
  export declare class EmptyPropTypeSpec<T> extends PropTypeSpec<T> {
889
892
  array<T_arr = unknown>(element: (arg: EmptyTypeSpec<unknown>) => TypeSpec<T_arr>): FullPropTypeSpec<Combine<T, T_arr[]>>;
890
893
  object<T_obj extends {}>(properties: (arg: TypeMemberSpec<{}>) => TypeMemberSpec<T_obj>): FullPropTypeSpec<Combine<T, T_obj>>;
@@ -936,6 +939,52 @@ export declare type HandshakeData = {
936
939
  withinQuotum: boolean;
937
940
  };
938
941
 
942
+ export declare class ImageFilePrompt implements Promise<File> {
943
+ private prompt;
944
+ private _model;
945
+ private _imageSize;
946
+ private fileName;
947
+ private aiClient;
948
+ constructor(prompt: string, _model: OpenaiImageModelId | undefined, _imageSize: ImageSize | undefined, fileName: string, aiClient: AIClient);
949
+ then<TResult1 = File, TResult2 = never>(onfulfilled?: ((value: any) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined): Promise<TResult1 | TResult2>;
950
+ catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null | undefined): Promise<File | TResult>;
951
+ finally(onfinally?: (() => void) | null | undefined): Promise<File>;
952
+ get [Symbol.toStringTag](): string;
953
+ model(model: OpenaiImageModelId): this;
954
+ /**
955
+ * The size of the generated images. Must be one of `1024x1024`, `1536x1024`
956
+ * (landscape), `1024x1536` (portrait), or `auto` (default value) for
957
+ * `gpt-image-1`, one of `256x256`, `512x512`, or `1024x1024` for `dall-e-2`, and
958
+ * one of `1024x1024`, `1792x1024`, or `1024x1792` for `dall-e-3`.
959
+ */
960
+ size(imageSize: ImageSize): this;
961
+ private getResponseAsFile;
962
+ }
963
+
964
+ export declare class ImagePrompt implements Promise<Buffer> {
965
+ private prompt;
966
+ private _model;
967
+ private _imageSize;
968
+ private aiClient;
969
+ constructor(prompt: string, _model: OpenaiImageModelId | undefined, _imageSize: ImageSize | undefined, aiClient: AIClient);
970
+ then<TResult1 = Buffer, TResult2 = never>(onfulfilled?: ((value: any) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined): Promise<TResult1 | TResult2>;
971
+ catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null | undefined): Promise<Buffer | TResult>;
972
+ finally(onfinally?: (() => void) | null | undefined): Promise<Buffer>;
973
+ get [Symbol.toStringTag](): string;
974
+ model(model: OpenaiImageModelId): this;
975
+ /**
976
+ * The size of the generated images. Must be one of `1024x1024`, `1536x1024`
977
+ * (landscape), `1024x1536` (portrait), or `auto` (default value) for
978
+ * `gpt-image-1`, one of `256x256`, `512x512`, or `1024x1024` for `dall-e-2`, and
979
+ * one of `1024x1024`, `1792x1024`, or `1024x1792` for `dall-e-3`.
980
+ */
981
+ size(imageSize: ImageSize): this;
982
+ toFile(fileName: string): ImageFilePrompt;
983
+ private getResponseAsString;
984
+ }
985
+
986
+ export declare type ImageSize = '1024x1024' | '1536x1024' | '1024x1536' | '256x256' | '512x512' | '1792x1024' | '1024x1792';
987
+
939
988
  export declare type JsonArray = AnyJson[];
940
989
 
941
990
  export declare type JsonExcerpt = {
@@ -947,7 +996,7 @@ export declare type JsonObject = {
947
996
  [key: string]: AnyJson;
948
997
  };
949
998
 
950
- export declare class JsonPrompt<PSArgs, Json extends AnyJson = AnyJson> extends StreamedJsonChildElement implements AsyncGenerator<AnyJson>, Promise<Json> {
999
+ export declare class JsonPrompt<PSArgs, Json extends AnyJson = AnyJson> extends StreamedJsonChildElement<Json> implements AsyncGenerator<AnyJson>, Promise<Json> {
951
1000
  private aiClient;
952
1001
  private promptSupplier;
953
1002
  private promptSupplierArgs;
@@ -995,16 +1044,10 @@ export declare class JsonPrompt<PSArgs, Json extends AnyJson = AnyJson> extends
995
1044
 
996
1045
  export declare type Message = SystemMessage | UserMessage | AssistantMessage;
997
1046
 
998
- export declare class ModelNotSupportedError extends Error {
999
- }
1000
-
1001
1047
  export declare function newAIClient(promptGridApiKey: string): AIClient;
1002
1048
 
1003
1049
  export declare function newAIClient(config: PromptGunConfig): AIClient;
1004
1050
 
1005
- export declare class NoAiCredientialsError extends Error {
1006
- }
1007
-
1008
1051
  export declare type NullType = BaseType<'null'>;
1009
1052
 
1010
1053
  export declare type NumberType = BaseType<'number'>;
@@ -1022,6 +1065,14 @@ export declare function obtainAiClientData(config: PromptGunConfig): {
1022
1065
  openai: Promise<string | undefined>;
1023
1066
  };
1024
1067
 
1068
+ export declare const OpenaiImageModel: {
1069
+ readonly dall_e_2: "dall-e-2";
1070
+ readonly dall_e_3: "dall-e-3";
1071
+ readonly gpt_image_1: "gpt-image-1";
1072
+ };
1073
+
1074
+ export declare type OpenaiImageModelId = (typeof OpenaiImageModel)[keyof typeof OpenaiImageModel];
1075
+
1025
1076
  export declare type Opts = {
1026
1077
  model?: 'gpt-4o';
1027
1078
  };
@@ -1046,11 +1097,7 @@ export declare type PromptGunConfig = {
1046
1097
  apiKeys?: AccessTokens;
1047
1098
  };
1048
1099
 
1049
- export declare function promptLLM<T>(args: T, toPrompt: (args: T) => Prompt): Flux<string>;
1050
-
1051
- export declare function promptLLM<T>(args: T, toPrompt: (args: T) => Prompt, json: true): StreamedJsonRootElement;
1052
-
1053
- export declare function promptLLM<T>(args: T, toPrompt: (args: T) => Prompt, json?: boolean): Flux<string> | StreamedJsonRootElement;
1100
+ export declare type PropType<ObjectType extends AnyJson, PropName extends string> = ObjectType extends Record<PropName, infer PropType extends AnyJson> ? PropType : never;
1054
1101
 
1055
1102
  export declare abstract class PropTypeSpec<T> extends TypeSpec<T> {
1056
1103
  private _comments;
@@ -1063,11 +1110,11 @@ export declare abstract class PropTypeSpec<T> extends TypeSpec<T> {
1063
1110
 
1064
1111
  export declare function setupAI(arg: string | PromptGunConfig): void;
1065
1112
 
1066
- export declare class StreamedJsonChildElement extends StreamedJsonElement {
1113
+ export declare class StreamedJsonChildElement<T extends AnyJson> extends StreamedJsonElement<T> {
1067
1114
  constructor();
1068
1115
  }
1069
1116
 
1070
- export declare abstract class StreamedJsonElement {
1117
+ export declare abstract class StreamedJsonElement<T extends AnyJson> implements Promise<T> {
1071
1118
  protected readonly receivedJson: string[];
1072
1119
  private readonly propertyCallbacks;
1073
1120
  private onElementCallbacks;
@@ -1076,19 +1123,24 @@ export declare abstract class StreamedJsonElement {
1076
1123
  private currentToken?;
1077
1124
  private state;
1078
1125
  private receivedExcerpts;
1079
- onElement(callback: (value: StreamedJsonElement) => void): this;
1080
- onProperty(propertyName: string, callback: (element: StreamedJsonElement) => void): this;
1126
+ onElement(callback: (value: StreamedJsonElement<ElementType<T>>) => void): this;
1127
+ onProperty<P extends string>(propertyName: P, callback: (element: StreamedJsonElement<PropType<T, P>>) => void): this;
1081
1128
  /**
1082
1129
  * @deprecated Does not work right now because FIELD_NAME
1083
1130
  * token comes after string value is already streaming
1084
1131
  */
1085
- onJsonStringProperty(propertyName: string, callback: (element: StreamedJsonElement) => void): this;
1132
+ onJsonStringProperty<P extends string>(propertyName: P, callback: (element: StreamedJsonElement<PropType<T, P>>) => void): this;
1086
1133
  onEachExcerpt(callback: (value: JsonExcerpt) => void): this;
1134
+ then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined): Promise<TResult1 | TResult2>;
1135
+ catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null | undefined): Promise<T | TResult>;
1136
+ finally(onfinally?: (() => void) | null | undefined): Promise<T>;
1137
+ get [Symbol.toStringTag](): string;
1138
+ private getResponseAsPromise;
1087
1139
  /**
1088
1140
  * @param onComplete - a callback that will receive the complete JSON
1089
1141
  * string for this element once it's done streaming
1090
1142
  */
1091
- onComplete(onComplete: (value: string) => void): void;
1143
+ private onComplete;
1092
1144
  private _onComplete;
1093
1145
  onCompleteAsync(onComplete: (value: string) => Promise<void>): void;
1094
1146
  onCompleteAs<T>(onComplete: (value: T) => void): void;
@@ -1111,7 +1163,7 @@ export declare abstract class StreamedJsonElement {
1111
1163
  private toAsync;
1112
1164
  }
1113
1165
 
1114
- export declare class StreamedJsonRootElement extends StreamedJsonElement {
1166
+ export declare class StreamedJsonRootElement<T extends AnyJson> extends StreamedJsonElement<T> {
1115
1167
  protected readonly jsonTokenFlux: Flux<JsonExcerpt>;
1116
1168
  constructor(jsonTokenFlux: Flux<JsonExcerpt>);
1117
1169
  whenComplete(): Promise<string>;