MCPcopy Create free account
hub / github.com/Superflows-AI/superflows / getLLMRequestChat

Function getLLMRequestChat

lib/queryLLM.ts:179–349  ·  view source on GitHub ↗
(
  messages: ChatGPTMessage[],
  params: ChatGPTParams = {},
  model: string,
)

Source from the content-addressed store, hash-verified

177}
178
179function getLLMRequestChat(
180 messages: ChatGPTMessage[],
181 params: ChatGPTParams = {},
182 model: string,
183): {
184 url: string;
185 options: { method: string; headers: HeadersInit; body: string };
186} {
187 const isOpenAIModel = model.includes("gpt");
188 const isMistralModel = model.includes("mistral");
189 const isPhindModel = model.includes("Phind");
190 const isAnthropicModel = model.includes("anthropic");
191 const isGroqModel = model.startsWith("groq");
192 const isOS = isOSModel(model);
193
194 let key: string, url: string;
195 if (isOpenAIModel) {
196 key = process.env.OPENAI_API_KEY!;
197 url = "https://api.openai.com/v1/chat/completions";
198 } else if (isGroqModel) {
199 key = process.env.GROQ_API_KEY!;
200 url = "https://api.groq.com/openai/v1/chat/completions";
201 model = model.split("/")[1];
202 } else if (isMistralModel) {
203 key = process.env.MISTRAL_API_KEY!;
204 url = "https://api.mistral.ai/v1/chat/completions";
205 delete defaultParams.frequency_penalty;
206 delete defaultParams.presence_penalty;
207 // Mistral doesn't know about function messages & is fussy about only replying to user messages!
208 // Below stringify-parse is deepcopy
209 messages = JSON.parse(JSON.stringify(messages)).map((m: ChatGPTMessage) =>
210 m.role !== "function"
211 ? { ...m }
212 : { role: "user", content: `${m.name} output: ${m.content}` },
213 );
214 } else if (isAnthropicModel && model.includes("claude-instant")) {
215 const prompt = GPTChatFormatToClaudeInstant(messages);
216 const max_tokens_to_sample = params.max_tokens;
217 const stop_sequences = params.stop;
218 const localParams = { ...params };
219 delete localParams.max_tokens;
220 delete localParams.stop;
221 return {
222 url: "https://api.anthropic.com/v1/complete",
223 options: {
224 method: "POST",
225 headers: {
226 "x-api-key": process.env.ANTHROPIC_API_KEY ?? "",
227 "Content-Type": "application/json",
228 "anthropic-version": "2023-06-01",
229 },
230 body: JSON.stringify({
231 ...localParams,
232 max_tokens_to_sample,
233 stop_sequences,
234 model: model.split("/")[1],
235 prompt: prompt,
236 }),

Callers 2

getLLMResponseFunction · 0.85
streamLLMResponseFunction · 0.85

Calls 3

isOSModelFunction · 0.85
GPTChatFormatToPhindFunction · 0.85

Tested by

no test coverage detected