-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
prompt_values.ts
177 lines (139 loc) Β· 3.58 KB
/
prompt_values.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { Serializable } from "./load/serializable.js";
import {
type BaseMessage,
HumanMessage,
getBufferString,
} from "./messages/index.js";
export interface BasePromptValueInterface extends Serializable {
toString(): string;
toChatMessages(): BaseMessage[];
}
export interface StringPromptValueInterface extends BasePromptValueInterface {
value: string;
}
export interface ChatPromptValueInterface extends BasePromptValueInterface {
messages: BaseMessage[];
}
/**
* Base PromptValue class. All prompt values should extend this class.
*/
export abstract class BasePromptValue
extends Serializable
implements BasePromptValueInterface
{
abstract toString(): string;
abstract toChatMessages(): BaseMessage[];
}
/**
* Represents a prompt value as a string. It extends the BasePromptValue
* class and overrides the toString and toChatMessages methods.
*/
export class StringPromptValue
extends BasePromptValue
implements StringPromptValueInterface
{
static lc_name(): string {
return "StringPromptValue";
}
lc_namespace = ["langchain_core", "prompt_values"];
lc_serializable = true;
value: string;
constructor(value: string) {
super({ value });
this.value = value;
}
toString() {
return this.value;
}
toChatMessages() {
return [new HumanMessage(this.value)];
}
}
/**
* Interface for the fields of a ChatPromptValue.
*/
export interface ChatPromptValueFields {
messages: BaseMessage[];
}
/**
* Class that represents a chat prompt value. It extends the
* BasePromptValue and includes an array of BaseMessage instances.
*/
export class ChatPromptValue
extends BasePromptValue
implements ChatPromptValueInterface
{
lc_namespace = ["langchain_core", "prompt_values"];
lc_serializable = true;
static lc_name() {
return "ChatPromptValue";
}
messages: BaseMessage[];
constructor(messages: BaseMessage[]);
constructor(fields: ChatPromptValueFields);
constructor(fields: BaseMessage[] | ChatPromptValueFields) {
if (Array.isArray(fields)) {
// eslint-disable-next-line no-param-reassign
fields = { messages: fields };
}
super(fields);
this.messages = fields.messages;
}
toString() {
return getBufferString(this.messages);
}
toChatMessages() {
return this.messages;
}
}
export type ImageContent = {
/** Specifies the detail level of the image. */
detail?: "auto" | "low" | "high";
/** Either a URL of the image or the base64 encoded image data. */
url: string;
};
export interface ImagePromptValueFields {
imageUrl: ImageContent;
}
/**
* Class that represents an image prompt value. It extends the
* BasePromptValue and includes an ImageURL instance.
*/
export class ImagePromptValue extends BasePromptValue {
lc_namespace = ["langchain_core", "prompt_values"];
lc_serializable = true;
static lc_name() {
return "ImagePromptValue";
}
imageUrl: ImageContent;
/** @ignore */
value: string;
constructor(fields: ImagePromptValueFields);
constructor(fields: ImageContent);
constructor(fields: ImageContent | ImagePromptValueFields) {
if (!("imageUrl" in fields)) {
// eslint-disable-next-line no-param-reassign
fields = { imageUrl: fields };
}
super(fields);
this.imageUrl = fields.imageUrl;
}
toString() {
return this.imageUrl.url;
}
toChatMessages() {
return [
new HumanMessage({
content: [
{
type: "image_url",
image_url: {
detail: this.imageUrl.detail,
url: this.imageUrl.url,
},
},
],
}),
];
}
}