-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
writer.ts
172 lines (135 loc) Β· 4.36 KB
/
writer.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
import { Writer as WriterClient } from "@writerai/writer-sdk";
import { type BaseLLMParams, LLM } from "@langchain/core/language_models/llms";
import { getEnvironmentVariable } from "@langchain/core/utils/env";
/**
* Interface for the input parameters specific to the Writer model.
*/
export interface WriterInput extends BaseLLMParams {
/** Writer API key */
apiKey?: string;
/** Writer organization ID */
orgId?: string | number;
/** Model to use */
model?: string;
/** Sampling temperature to use */
temperature?: number;
/** Minimum number of tokens to generate. */
minTokens?: number;
/** Maximum number of tokens to generate in the completion. */
maxTokens?: number;
/** Generates this many completions server-side and returns the "best"." */
bestOf?: number;
/** Penalizes repeated tokens according to frequency. */
frequencyPenalty?: number;
/** Whether to return log probabilities. */
logprobs?: number;
/** Number of completions to generate. */
n?: number;
/** Penalizes repeated tokens regardless of frequency. */
presencePenalty?: number;
/** Total probability mass of tokens to consider at each step. */
topP?: number;
}
/**
* Class representing a Writer Large Language Model (LLM). It interacts
* with the Writer API to generate text completions.
*/
export class Writer extends LLM implements WriterInput {
static lc_name() {
return "Writer";
}
get lc_secrets(): { [key: string]: string } | undefined {
return {
apiKey: "WRITER_API_KEY",
orgId: "WRITER_ORG_ID",
};
}
get lc_aliases(): { [key: string]: string } | undefined {
return {
apiKey: "writer_api_key",
orgId: "writer_org_id",
};
}
lc_serializable = true;
apiKey: string;
orgId: number;
model = "palmyra-instruct";
temperature?: number;
minTokens?: number;
maxTokens?: number;
bestOf?: number;
frequencyPenalty?: number;
logprobs?: number;
n?: number;
presencePenalty?: number;
topP?: number;
constructor(fields?: WriterInput) {
super(fields ?? {});
const apiKey = fields?.apiKey ?? getEnvironmentVariable("WRITER_API_KEY");
const orgId = fields?.orgId ?? getEnvironmentVariable("WRITER_ORG_ID");
if (!apiKey) {
throw new Error(
"Please set the WRITER_API_KEY environment variable or pass it to the constructor as the apiKey field."
);
}
if (!orgId) {
throw new Error(
"Please set the WRITER_ORG_ID environment variable or pass it to the constructor as the orgId field."
);
}
this.apiKey = apiKey;
this.orgId = typeof orgId === "string" ? parseInt(orgId, 10) : orgId;
this.model = fields?.model ?? this.model;
this.temperature = fields?.temperature ?? this.temperature;
this.minTokens = fields?.minTokens ?? this.minTokens;
this.maxTokens = fields?.maxTokens ?? this.maxTokens;
this.bestOf = fields?.bestOf ?? this.bestOf;
this.frequencyPenalty = fields?.frequencyPenalty ?? this.frequencyPenalty;
this.logprobs = fields?.logprobs ?? this.logprobs;
this.n = fields?.n ?? this.n;
this.presencePenalty = fields?.presencePenalty ?? this.presencePenalty;
this.topP = fields?.topP ?? this.topP;
}
_llmType() {
return "writer";
}
/** @ignore */
async _call(
prompt: string,
options: this["ParsedCallOptions"]
): Promise<string> {
const sdk = new WriterClient({
security: {
apiKey: this.apiKey,
},
organizationId: this.orgId,
});
return this.caller.callWithOptions({ signal: options.signal }, async () => {
try {
const res = await sdk.completions.create({
completionRequest: {
prompt,
stop: options.stop,
temperature: this.temperature,
minTokens: this.minTokens,
maxTokens: this.maxTokens,
bestOf: this.bestOf,
n: this.n,
frequencyPenalty: this.frequencyPenalty,
logprobs: this.logprobs,
presencePenalty: this.presencePenalty,
topP: this.topP,
},
modelId: this.model,
});
return (
res.completionResponse?.choices?.[0].text ?? "No completion found."
);
} catch (e) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(e as any).response = (e as any).rawResponse;
throw e;
}
});
}
}