|
5 | 5 | * @param {string} apiKey - The API key for the Groq API.
|
6 | 6 | */
|
7 | 7 |
|
8 |
| -const GroqSDK = require('groq-sdk'); |
9 |
| -const { adjustModelAlias, getModelByAlias } = require('../utils/config.js'); |
10 |
| -const { getFromCache, saveToCache } = require('../utils/cache.js'); |
11 |
| -const { getMessageObject } = require('../utils/utils.js'); |
| 8 | +const BaseInterface = require('./baseInterface.js'); |
12 | 9 | const { groqApiKey } = require('../config/config.js');
|
| 10 | +const { getMessageObject } = require('../utils/utils.js'); |
13 | 11 | const { getConfig } = require('../utils/configManager.js');
|
14 | 12 | const config = getConfig();
|
15 |
| -const log = require('loglevel'); |
16 | 13 |
|
17 |
| -// Groq class for interacting with the Groq API |
18 |
| -class Groq { |
19 |
| - /** |
20 |
| - * Constructor for the Groq class. |
21 |
| - * @param {string} apiKey - The API key for the Groq API. |
22 |
| - */ |
| 14 | +class Groq extends BaseInterface { |
23 | 15 | constructor(apiKey) {
|
24 |
| - this.interfaceName = 'groq'; |
25 |
| - this.apiKey = apiKey || groqApiKey; |
26 |
| - this.groq = new GroqSDK({ |
27 |
| - apiKey: this.apiKey, |
28 |
| - }); |
| 16 | + super('groq', apiKey || groqApiKey, config['groq'].url); |
29 | 17 | }
|
30 | 18 |
|
31 |
| - /** |
32 |
| - * Send a message to the Groq API. |
33 |
| - * @param {string|object} message - The message to send or a message object. |
34 |
| - * @param {object} options - Additional options for the API request. |
35 |
| - * @param {object} interfaceOptions - Options specific to the interface. |
36 |
| - * @returns {string} The response content from the Groq API. |
37 |
| - */ |
38 |
| - async sendMessage(message, options = {}, interfaceOptions = {}) { |
39 |
| - const messageObject = |
40 |
| - typeof message === 'string' ? getMessageObject(message) : message; |
41 |
| - const cacheTimeoutSeconds = |
42 |
| - typeof interfaceOptions === 'number' |
43 |
| - ? interfaceOptions |
44 |
| - : interfaceOptions.cacheTimeoutSeconds; |
45 |
| - |
46 |
| - let { model } = messageObject; |
47 |
| - const selectedModel = getModelByAlias(this.interfaceName, model); |
48 |
| - |
49 |
| - // Set the model and default values |
50 |
| - model = |
51 |
| - selectedModel || |
52 |
| - options.model || |
53 |
| - config[this.interfaceName].model.default.name; |
54 |
| - |
55 |
| - const { max_tokens = 150 } = options; |
56 |
| - |
57 |
| - // Prepare the parameters for the API call |
58 |
| - const params = { |
59 |
| - model, |
60 |
| - messages: messageObject.messages, |
61 |
| - max_tokens, |
62 |
| - }; |
63 |
| - |
64 |
| - // Generate a cache key based on the parameters |
65 |
| - const cacheKey = JSON.stringify(params); |
66 |
| - if (cacheTimeoutSeconds) { |
67 |
| - const cachedResponse = getFromCache(cacheKey); |
68 |
| - if (cachedResponse) { |
69 |
| - return cachedResponse; |
70 |
| - } |
71 |
| - } |
72 |
| - |
73 |
| - // Set up retry mechanism with exponential backoff |
74 |
| - let retryAttempts = interfaceOptions.retryAttempts || 0; |
75 |
| - let currentRetry = 0; |
76 |
| - while (retryAttempts >= 0) { |
77 |
| - try { |
78 |
| - // Send the request to the Groq API |
79 |
| - const chatCompletion = await this.groq.chat.completions.create(params); |
80 |
| - let responseContent = null; |
81 |
| - if ( |
82 |
| - chatCompletion && |
83 |
| - chatCompletion.choices && |
84 |
| - chatCompletion.choices[0] && |
85 |
| - chatCompletion.choices[0].message && |
86 |
| - chatCompletion.choices[0].message.content |
87 |
| - ) { |
88 |
| - responseContent = chatCompletion.choices[0].message.content; |
89 |
| - } |
90 |
| - // Attempt to repair the object if needed |
91 |
| - if (interfaceOptions.attemptJsonRepair) { |
92 |
| - responseContent = await parseJSON( |
93 |
| - responseContent, |
94 |
| - interfaceOptions.attemptJsonRepair, |
95 |
| - ); |
96 |
| - } |
97 |
| - // Build response object |
98 |
| - responseContent = { results: responseContent }; |
99 |
| - |
100 |
| - if (cacheTimeoutSeconds && responseContent) { |
101 |
| - saveToCache(cacheKey, responseContent, cacheTimeoutSeconds); |
102 |
| - } |
103 |
| - |
104 |
| - return responseContent; |
105 |
| - } catch (error) { |
106 |
| - retryAttempts--; |
107 |
| - if (retryAttempts < 0) { |
108 |
| - // Log any errors and throw the error |
109 |
| - log.error( |
110 |
| - 'Response data:', |
111 |
| - error.response ? error.response.data : null, |
112 |
| - ); |
113 |
| - throw error; |
114 |
| - } |
115 |
| - |
116 |
| - // Calculate the delay for the next retry attempt |
117 |
| - let retryMultiplier = interfaceOptions.retryMultiplier || 0.3; |
118 |
| - const delay = (currentRetry + 1) * retryMultiplier * 1000; |
119 |
| - |
120 |
| - await new Promise((resolve) => setTimeout(resolve, delay)); |
121 |
| - currentRetry++; |
122 |
| - } |
123 |
| - } |
| 19 | + createMessageObject(message) { |
| 20 | + return typeof message === 'string' ? getMessageObject(message) : message; |
124 | 21 | }
|
125 | 22 | }
|
126 | 23 |
|
127 |
| -Groq.prototype.adjustModelAlias = adjustModelAlias; |
128 | 24 | module.exports = Groq;
|
0 commit comments