-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlangchain.ts
More file actions
187 lines (155 loc) · 6.06 KB
/
Copy pathlangchain.ts
File metadata and controls
187 lines (155 loc) · 6.06 KB
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
import "server-only";
import { ChatGoogleGenerativeAI } from "@langchain/google-genai";
import { ChatOpenAI } from "@langchain/openai";
import { HumanMessage, AIMessage, SystemMessage, type BaseMessage } from "langchain";
// ── Model constants ──────────────────────────────────────────────────────
export const GEMINI_MODEL = "gemini-2.5-flash" as const;
export const OPENAI_MODEL = "gpt-4o-mini" as const;
// ── Config ────────────────────────────────────────────────────────────────
export interface ChatModelConfig {
temperature?: number;
maxOutputTokens?: number;
topP?: number;
topK?: number;
stopSequences?: string[];
}
// ── Model factory ─────────────────────────────────────────────────────────
function createGemini(config?: ChatModelConfig) {
return new ChatGoogleGenerativeAI({
model: GEMINI_MODEL,
apiKey: process.env.GEMINI_API_KEY,
temperature: config?.temperature ?? 0.4,
maxOutputTokens: config?.maxOutputTokens ?? 1024,
topP: config?.topP ?? 0.92,
topK: config?.topK,
stopSequences: config?.stopSequences,
});
}
function createOpenAI(config?: ChatModelConfig) {
return new ChatOpenAI({
model: OPENAI_MODEL,
apiKey: process.env.OPENAI_API_KEY,
temperature: config?.temperature ?? 0.4,
maxTokens: config?.maxOutputTokens ?? 1024,
});
}
// ── Streaming (race both providers) ───────────────────────────────────────
/**
* Stream from Gemini AND OpenAI simultaneously — first to respond wins.
* No 12-second wait if Gemini hangs on 429.
*/
export async function* streamWithFallback(
messages: BaseMessage[],
config?: ChatModelConfig,
): AsyncGenerator<string> {
const hasOpenAI = Boolean(process.env.OPENAI_API_KEY);
if (!hasOpenAI) {
const model = createGemini(config);
const stream = await model.stream(messages);
for await (const chunk of stream) {
const t = typeof chunk.content === "string" ? chunk.content : "";
if (t) yield t;
}
return;
}
// Start both streams in parallel
const geminiStream = createGemini(config)
.stream(messages)
.catch(() => null);
const openaiStream = createOpenAI(config)
.stream(messages)
.catch(() => null);
const [gStream, oStream] = await Promise.all([geminiStream, openaiStream]);
if (!gStream && !oStream) {
throw new Error("Both Gemini and OpenAI failed to create stream");
}
// Race: first token from either provider wins
const gNext = gStream
? gStream[Symbol.asyncIterator]().next().then((r) => ({ ...r, stream: gStream }))
: new Promise<never>(() => {});
const oNext = oStream
? oStream[Symbol.asyncIterator]().next().then((r) => ({ ...r, stream: oStream }))
: new Promise<never>(() => {});
const first = await Promise.race([gNext, oNext]).catch(() => null);
if (!first || first.done) {
throw new Error("Both providers returned empty streams");
}
// Yield first token
const t = typeof first.value?.content === "string" ? first.value.content : "";
if (t) yield t;
// Drain the winning stream
for await (const chunk of first.stream) {
const text = typeof chunk.content === "string" ? chunk.content : "";
if (text) yield text;
}
}
// ── Non-streaming (race both providers) ───────────────────────────────────
/**
* Fire Gemini and OpenAI simultaneously — first response wins.
* Instant fallback without waiting for a hung provider.
*/
export async function invokeWithFallback(
messages: BaseMessage[],
config?: ChatModelConfig,
): Promise<string> {
const hasOpenAI = Boolean(process.env.OPENAI_API_KEY);
if (!hasOpenAI) {
const r = await createGemini(config).invoke(messages);
const t = typeof r.content === "string" ? r.content : "";
if (!t) throw new Error("Gemini returned empty response");
return t;
}
// Race both — first to respond wins
const race = Promise.race([
createGemini(config)
.invoke(messages)
.then((r) => ({ provider: "gemini" as const, text: typeof r.content === "string" ? r.content : "" })),
createOpenAI(config)
.invoke(messages)
.then((r) => ({ provider: "openai" as const, text: typeof r.content === "string" ? r.content : "" })),
]);
try {
const winner = await race;
if (winner.text) {
console.log(`[LangChain] Invoke winner: ${winner.provider}`);
return winner.text;
}
} catch {
// One failed — wait for the other
}
// Fallback: wait for both, take whichever succeeded
const [geminiResult, openaiResult] = await Promise.allSettled([
createGemini(config)
.invoke(messages)
.then((r) => typeof r.content === "string" ? r.content : ""),
createOpenAI(config)
.invoke(messages)
.then((r) => typeof r.content === "string" ? r.content : ""),
]);
for (const r of [geminiResult, openaiResult]) {
if (r.status === "fulfilled" && r.value) return r.value;
}
throw new Error("Both Gemini and OpenAI failed");
}
// ── Message helper ────────────────────────────────────────────────────────
export interface ChatMessage {
role: "user" | "assistant";
content: string;
}
export function toLangChainMessages(
messages: ChatMessage[],
systemInstruction?: string,
): BaseMessage[] {
const lcMessages: BaseMessage[] = [];
if (systemInstruction) {
lcMessages.push(new SystemMessage(systemInstruction));
}
for (const msg of messages) {
if (msg.role === "user") {
lcMessages.push(new HumanMessage(msg.content));
} else {
lcMessages.push(new AIMessage(msg.content));
}
}
return lcMessages;
}