-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathpipe.ts
212 lines (194 loc) · 4.39 KB
/
pipe.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { z } from 'zod';
import type {
AnthropicModels,
CohereModels,
FireworksAIModels,
GoogleModels,
GroqModels,
MistralAIModels,
OllamaModels,
OpenAIModels,
PerplexityModels,
TogetherModels
} from './model';
import type { PipeTool } from './tools';
const contentTypeSchema = z.object({
type: z.string(),
text: z.string().optional(),
image_url: z
.object({
url: z.string(),
detail: z.string().optional()
})
.optional()
});
export const schemaMessage = z
.object({
role: z.enum(['system', 'user', 'assistant', 'function', 'tool']),
content: z
.union([z.string(), z.array(contentTypeSchema), z.null()])
.optional(),
tool_call_id: z.string().optional(),
name: z.string().optional(),
tool_calls: z
.array(
z.object({
id: z.string(),
type: z.string(),
function: z.record(z.unknown())
})
)
.optional()
})
.refine(
({ content, role, tool_calls }) => {
// If content is null, role isn't assistant and tool_calls is not present.
// then the schema is invalid
// because the message content is null and its not an assistant tool call
const isSchemaInvalid =
content === null && role !== 'assistant' && !tool_calls;
if (isSchemaInvalid) return false;
return true;
},
{
message: 'Message content cannot be empty.'
}
);
export const schemaPipeMessage = z
.object({
role: z.enum(['system', 'user', 'assistant', 'function', 'tool']),
content: z.string().nullable(),
tool_call_id: z.string().optional(),
name: z.string().optional(),
tool_calls: z
.array(
z.object({
id: z.string(),
type: z.string(),
function: z.record(z.unknown())
})
)
.optional()
})
.refine(
({ content, role, tool_calls }) => {
// If content is null, role isn't assistant and tool_calls is not present.
// then the schema is invalid
// because the message content is null and its not an assistant tool call
const isSchemaInvalid =
content === null && role !== 'assistant' && !tool_calls;
if (isSchemaInvalid) return false;
return true;
},
{
message: 'Message content cannot be empty.'
}
);
export type Message = z.infer<typeof schemaMessage>;
export type PipeMessage = z.infer<typeof schemaPipeMessage>;
export const VariableSchema = z.object({
name: z.string(),
value: z.string()
});
export const VariablesSchema = z.array(VariableSchema).default([]);
export type VariablesI = z.infer<typeof VariablesSchema>;
export type VariableI = z.infer<typeof VariableSchema>;
interface ToolFunction {
name: string;
}
interface ToolChoiceFunction {
type: 'function';
function: ToolFunction;
}
export type ToolChoice = 'auto' | 'required' | 'none' | ToolChoiceFunction;
export type PipeModelT =
| OpenAIModels
| TogetherModels
| AnthropicModels
| GroqModels
| GoogleModels
| CohereModels
| FireworksAIModels
| PerplexityModels
| MistralAIModels
| OllamaModels;
export interface Pipe {
apiKey?: string;
name: string;
description: string;
status: 'public' | 'private';
model: PipeModelT;
stream: boolean;
json: boolean;
store: boolean;
moderate: boolean;
top_p: number;
max_tokens: number;
temperature: number;
presence_penalty: number;
frequency_penalty: number;
stop: string[];
tool_choice: ToolChoice;
parallel_tool_calls: boolean;
messages: PipeMessage[];
variables: VariablesI;
tools: PipeTool[];
memory: {
name: string;
}[];
}
export interface PipeOld {
name: string;
description: string;
status: string;
config: {
meta: {
stream: boolean;
json: boolean;
store: boolean;
moderate: boolean;
};
model: {
name: string;
provider: string;
params: {
top_p: number;
max_tokens: number;
temperature: number;
presence_penalty: number;
frequency_penalty: number;
stop: string[];
};
tool_choice: ToolChoice;
parallel_tool_calls: boolean;
};
prompt: {
system: string;
opening: string;
safety: string;
messages: { role: string; content: string }[];
variables: VariablesI;
json: string;
rag: string;
};
tools: any[];
memorysets: string[];
};
}
export interface ToolCall {
id: string;
type: string;
function: {
name: string;
arguments: string;
};
}
const functionNameRegex = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
export const toolChoiceSchema = z
.object({
type: z.enum(['function']).default('function'),
function: z.object({
name: z.string().refine(value => functionNameRegex.test(value))
})
})
.optional();