-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathcall-groq.ts
More file actions
57 lines (53 loc) · 1.41 KB
/
call-groq.ts
File metadata and controls
57 lines (53 loc) · 1.41 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
import OpenAI from 'openai';
import { dlog } from '../utils/dlog';
import { GROQ } from '../data/models';
import transformToProviderRequest from '../utils/provider-handlers/transfrom-to-provider-request';
import { applyJsonModeIfEnabled, handleLlmError } from './utils';
import type { ModelParams } from 'types/providers';
import type { Message } from 'types/pipe';
import { addToolsToParams } from '../utils/add-tools-to-params';
export async function callGroq({
pipe,
messages,
llmApiKey,
stream
}: {
pipe: any;
llmApiKey: string;
stream: boolean;
messages: Message[];
}) {
try {
const modelParams = buildModelParams(pipe, stream, messages);
const groq = new OpenAI({
apiKey: llmApiKey,
baseURL: 'https://api.groq.com/openai/v1'
});
applyJsonModeIfEnabled(modelParams, pipe);
addToolsToParams(modelParams, pipe);
// Transform params according to provider's format
const transformedRequestParams = transformToProviderRequest({
provider: GROQ,
params: modelParams as ModelParams,
fn: 'chatComplete'
});
dlog('Groq request params', transformedRequestParams);
return await groq.chat.completions.create(
transformedRequestParams as any
);
} catch (error: any) {
handleLlmError({ error, provider: GROQ });
}
}
function buildModelParams(
pipe: any,
stream: boolean,
messages: Message[]
): ModelParams {
return {
messages,
stream,
model: pipe.model.name,
...pipe.model.params
};
}