Skip to content

Commit a1c2888

Browse files
authored
Merge pull request #6 from samestrin/v2.0.8
V2.0.8
2 parents 7cdc7cd + 6d7a489 commit a1c2888

10 files changed

+48
-299
lines changed

README.md

+5-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![Star on GitHub](https://img.shields.io/github/stars/samestrin/llm-interface?style=social)](https://github.com/samestrin/llm-interface/stargazers) [![Fork on GitHub](https://img.shields.io/github/forks/samestrin/llm-interface?style=social)](https://github.com/samestrin/llm-interface/network/members) [![Watch on GitHub](https://img.shields.io/github/watchers/samestrin/llm-interface?style=social)](https://github.com/samestrin/llm-interface/watchers)
44

5-
![Version 2.0.7](https://img.shields.io/badge/Version-2.0.7-blue) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![Built with Node.js](https://img.shields.io/badge/Built%20with-Node.js-green)](https://nodejs.org/)
5+
![Version 2.0.8](https://img.shields.io/badge/Version-2.0.8-blue) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![Built with Node.js](https://img.shields.io/badge/Built%20with-Node.js-green)](https://nodejs.org/)
66

77
## Introduction
88

@@ -23,6 +23,10 @@ This goal of `llm-interface` is to provide a single, simple, unified interface f
2323

2424
## Updates
2525

26+
**v2.0.8**
27+
28+
- **Removing Dependencies**: The removal of OpenAI and Groq SDKs results in a smaller bundle, faster installs, and reduced complexity.
29+
2630
**v2.0.7**
2731

2832
- **New LLM Providers**: Added support for DeepInfra, FriendliAI, Monster API, Octo AI, Together AI, and NVIDIA.
@@ -33,19 +37,13 @@ This goal of `llm-interface` is to provide a single, simple, unified interface f
3337

3438
- **New LLM Provider**: Added support for watsonx.ai.
3539

36-
**v2.0.3**
37-
38-
- **New LLM Providers Functions**: `LLMInterface.getAllModelNames()` and `LLMInterface.getModelConfigValue(provider, configValueKey)`.
39-
4040
## Dependencies
4141

4242
The project relies on several npm packages and APIs. Here are the primary dependencies:
4343

4444
- `axios`: For making HTTP requests (used for various HTTP AI APIs).
4545
- `@anthropic-ai/sdk`: SDK for interacting with the Anthropic API.
4646
- `@google/generative-ai`: SDK for interacting with the Google Gemini API.
47-
- `groq-sdk`: SDK for interacting with the Groq API.
48-
- `openai`: SDK for interacting with the OpenAI API.
4947
- `dotenv`: For managing environment variables. Used by test cases.
5048
- `flat-cache`: For optionally caching API responses to improve performance and reduce redundant requests.
5149
- `jsonrepair`: Used to repair invalid JSON responses.

package-lock.json

+3-39
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,8 @@
4040
"axios": "^1.7.2",
4141
"dotenv": "^16.4.5",
4242
"flat-cache": "^5.0.0",
43-
"groq-sdk": "^0.5.0",
4443
"jsonrepair": "^3.8.0",
45-
"loglevel": "^1.9.1",
46-
"openai": "^4.52.0"
44+
"loglevel": "^1.9.1"
4745
},
4846
"devDependencies": {
4947
"@babel/core": "^7.24.7",

src/config/llmProviders.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
"default": { "name": "gpt-3.5-turbo", "tokens": 16385 },
66
"large": { "name": "gpt-4o", "tokens": 128000 },
77
"small": { "name": "davinci-002", "tokens": 16384 }
8-
},
9-
"note": "api"
8+
}
109
},
1110
"ai21": {
1211
"url": "https://api.ai21.com/studio/v1/chat/completions",
@@ -61,7 +60,7 @@
6160
"note": "url value is partial"
6261
},
6362
"groq": {
64-
"url": "https://api.groq.com/v1/request_manager/text_completion",
63+
"url": "https://api.groq.com/openai/v1/chat/completions",
6564
"model": {
6665
"default": { "name": "llama3-8b-8192", "tokens": 8192 },
6766
"large": { "name": "llama3-70b-8192", "tokens": 8192 },

src/interfaces/baseInterface.js

+17-7
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ class BaseInterface {
5151
);
5252
}
5353

54+
/**
55+
* Method to update the message object if needed.
56+
* Can be overridden by derived classes to transform the message object.
57+
* @param {object} messageObject - The message object to be updated.
58+
* @returns {object} The updated message object.
59+
*/
60+
updateMessageObject(messageObject) {
61+
return messageObject; // Default implementation does nothing
62+
}
63+
5464
/**
5565
* Method to construct the request URL, can be overridden by derived classes.
5666
* @param {string} model - The model to use for the request.
@@ -68,7 +78,11 @@ class BaseInterface {
6878
* @returns {string} The response content from the API.
6979
*/
7080
async sendMessage(message, options = {}, interfaceOptions = {}) {
71-
const messageObject = this.createMessageObject(message);
81+
let messageObject =
82+
typeof message === 'string' ? this.createMessageObject(message) : message;
83+
84+
// Update the message object if needed
85+
messageObject = this.updateMessageObject(messageObject);
7286

7387
const cacheTimeoutSeconds =
7488
typeof interfaceOptions === 'number'
@@ -78,12 +92,7 @@ class BaseInterface {
7892
const { model, messages } = messageObject;
7993
const selectedModel = getModelByAlias(this.interfaceName, model);
8094

81-
const {
82-
temperature = 0.7,
83-
max_tokens = 150,
84-
stop_sequences = [''],
85-
response_format = '',
86-
} = options;
95+
const { max_tokens = 150, response_format = '' } = options;
8796

8897
const requestBody = {
8998
model:
@@ -119,6 +128,7 @@ class BaseInterface {
119128
while (retryAttempts >= 0) {
120129
try {
121130
const response = await this.client.post(url, requestBody);
131+
122132
let responseContent = null;
123133
if (
124134
response &&

src/interfaces/groq.js

+6-110
Original file line numberDiff line numberDiff line change
@@ -5,124 +5,20 @@
55
* @param {string} apiKey - The API key for the Groq API.
66
*/
77

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');
129
const { groqApiKey } = require('../config/config.js');
10+
const { getMessageObject } = require('../utils/utils.js');
1311
const { getConfig } = require('../utils/configManager.js');
1412
const config = getConfig();
15-
const log = require('loglevel');
1613

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 {
2315
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);
2917
}
3018

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;
12421
}
12522
}
12623

127-
Groq.prototype.adjustModelAlias = adjustModelAlias;
12824
module.exports = Groq;

0 commit comments

Comments
 (0)