-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
openai.ts
161 lines (138 loc) Β· 4.58 KB
/
openai.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
import {
ChatOpenAI,
type ChatOpenAICallOptions,
messageToOpenAIRole,
} from "@langchain/openai";
import { CallbackManagerForLLMRun } from "../callbacks/manager.js";
import { BaseMessage, ChatMessage, ChatResult } from "../schema/index.js";
import { promptLayerTrackRequest } from "../util/prompt-layer.js";
export {
type AzureOpenAIInput,
type OpenAICallOptions,
type OpenAIChatInput,
} from "@langchain/openai";
export { type ChatOpenAICallOptions, ChatOpenAI };
export class PromptLayerChatOpenAI extends ChatOpenAI {
promptLayerApiKey?: string;
plTags?: string[];
returnPromptLayerId?: boolean;
constructor(
fields?: ConstructorParameters<typeof ChatOpenAI>[0] & {
promptLayerApiKey?: string;
plTags?: string[];
returnPromptLayerId?: boolean;
}
) {
super(fields);
this.promptLayerApiKey =
fields?.promptLayerApiKey ??
(typeof process !== "undefined"
? // eslint-disable-next-line no-process-env
process.env?.PROMPTLAYER_API_KEY
: undefined);
this.plTags = fields?.plTags ?? [];
this.returnPromptLayerId = fields?.returnPromptLayerId ?? false;
}
async _generate(
messages: BaseMessage[],
options?: string[] | ChatOpenAICallOptions,
runManager?: CallbackManagerForLLMRun
): Promise<ChatResult> {
const requestStartTime = Date.now();
let parsedOptions: ChatOpenAICallOptions;
if (Array.isArray(options)) {
parsedOptions = { stop: options } as ChatOpenAICallOptions;
} else if (options?.timeout && !options.signal) {
parsedOptions = {
...options,
signal: AbortSignal.timeout(options.timeout),
};
} else {
parsedOptions = options ?? {};
}
const generatedResponses = await super._generate(
messages,
parsedOptions,
runManager
);
const requestEndTime = Date.now();
const _convertMessageToDict = (message: BaseMessage) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let messageDict: any;
if (message._getType() === "human") {
messageDict = { role: "user", content: message.content };
} else if (message._getType() === "ai") {
messageDict = { role: "assistant", content: message.content };
} else if (message._getType() === "function") {
messageDict = { role: "assistant", content: message.content };
} else if (message._getType() === "system") {
messageDict = { role: "system", content: message.content };
} else if (message._getType() === "generic") {
messageDict = {
role: (message as ChatMessage).role as
| "system"
| "assistant"
| "user"
| "function"
| "tool",
content: message.content,
};
} else {
throw new Error(`Got unknown type ${message}`);
}
return messageDict;
};
const _createMessageDicts = (
messages: BaseMessage[],
callOptions?: ChatOpenAICallOptions
) => {
const params = {
...this.invocationParams(),
model: this.modelName,
};
if (callOptions?.stop) {
if (Object.keys(params).includes("stop")) {
throw new Error("`stop` found in both the input and default params.");
}
}
const messageDicts = messages.map((message) =>
_convertMessageToDict(message)
);
return messageDicts;
};
for (let i = 0; i < generatedResponses.generations.length; i += 1) {
const generation = generatedResponses.generations[i];
const messageDicts = _createMessageDicts(messages, parsedOptions);
let promptLayerRequestId: string | undefined;
const parsedResp = [
{
content: generation.text,
role: messageToOpenAIRole(generation.message),
},
];
const promptLayerRespBody = await promptLayerTrackRequest(
this.caller,
"langchain.PromptLayerChatOpenAI",
{ ...this._identifyingParams(), messages: messageDicts, stream: false },
this.plTags,
parsedResp,
requestStartTime,
requestEndTime,
this.promptLayerApiKey
);
if (this.returnPromptLayerId === true) {
if (promptLayerRespBody.success === true) {
promptLayerRequestId = promptLayerRespBody.request_id;
}
if (
!generation.generationInfo ||
typeof generation.generationInfo !== "object"
) {
generation.generationInfo = {};
}
generation.generationInfo.promptLayerRequestId = promptLayerRequestId;
}
}
return generatedResponses;
}
}