Skip to content

Commit 997a86f

Browse files
authored
Merge pull request #7 from samestrin/2.0.9
2.0.9
2 parents 6400dcb + e5962f6 commit 997a86f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+3833
-663
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,5 @@ dist
132132
/src/cache
133133
.prettier*
134134

135+
.DS_STORE
136+
cache/

README.md

+19-19
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,40 @@
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.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/)
5+
![Version 2.0.9](https://img.shields.io/badge/Version-2.0.9-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

9-
`llm-interface` is a wrapper designed to interact with multiple Large Language Model (LLM) APIs. `llm-interface` simplifies integrating various LLM providers, including **OpenAI, AI21 Studio, Anthropic, Cloudflare AI, Cohere, DeepInfra, Fireworks AI, Friendli AI, Google Gemini, Goose AI, Groq, Hugging Face, Mistral AI, Monster API, Octo AI, Perplexity, Reka AI, watsonx.ai, and LLaMA.cpp**, into your applications. It is available as an [NPM package](https://www.npmjs.com/package/llm-interface).
9+
`llm-interface` is a wrapper designed to interact with multiple Large Language Model (LLM) APIs. `llm-interface` simplifies integrating various LLM providers, including **OpenAI, AI21 Studio, AIML API, Anthropic, Cloudflare AI, Cohere, DeepInfra, Fireworks AI, Forefront, Friendli AI, Google Gemini, Goose AI, Groq, Hugging Face, Mistral AI, Monster API, Octo AI, Ollama, Perplexity, Reka AI, Replicate, watsonx.ai, Writer, and LLaMA.cpp**, into your applications. It is available as an [NPM package](https://www.npmjs.com/package/llm-interface).
1010

1111
This goal of `llm-interface` is to provide a single, simple, unified interface for sending messages and receiving responses from different LLM services. This will make it easier for developers to work with multiple LLMs without worrying about the specific intricacies of each API.
1212

1313
## Features
1414

15-
- **Unified Interface**: `LLMInterfaceSendMessage` is a single, consistent interface to interact with **19 different LLM APIs**.
15+
- **Unified Interface**: `LLMInterfaceSendMessage` is a single, consistent interface to interact with **24 different LLM APIs** (22 hosted LLM providers and 2 local LLM providers).
1616
- **Dynamic Module Loading**: Automatically loads and manages LLM interfaces only when they are invoked, minimizing resource usage.
1717
- **Error Handling**: Robust error handling mechanisms to ensure reliable API interactions.
1818
- **Extensible**: Easily extendable to support additional LLM providers as needed.
1919
- **Response Caching**: Efficiently caches LLM responses to reduce costs and enhance performance.
2020
- **Graceful Retries**: Automatically retry failed prompts with increasing delays to ensure successful responses.
21-
- **JSON Output**: Simple to use native JSON output for OpenAI, Fireworks AI, and Gemini responses.
22-
- **JSON Repair**: Detect and repair invalid JSON responses.
21+
- **JSON Output**: Simple to use native JSON output for various LLM providers including OpenAI, Fireworks AI, Google Gemini, and more.
22+
- **JSON Repair**: Detect and repair invalid JSON responses.
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.
26+
**v2.0.9**
2927

30-
**v2.0.7**
28+
- **New LLM Providers**: Added support for AIML API (_currently not respecting option values_), DeepSeek, Forefront, Ollama, Replicate, and Writer.
29+
- **New LLMInterface Methods**: `LLMInterface.setApiKey`, `LLMInterface.sendMesage`, and `LLMInterface.streamMessage`.
30+
- **Streaming**: Streaming support available for: AI21 Studio, AIML API, DeepInfra, DeepSeek, Fireworks AI, FriendliAI, Groq, Hugging Face, LLaMa.CPP, Mistral AI, Monster API, NVIDIA,
31+
Octo AI, Ollama, OpenAI, Perplexity, Together AI, and Writer.
32+
- **New Interface Function**: `LLMInterfaceStreamMessage`
33+
- **Test Coverage**: 100% test coverage for all interface classes.
34+
- **Examples**: New usage [examples](/examples).
3135

32-
- **New LLM Providers**: Added support for DeepInfra, FriendliAI, Monster API, Octo AI, Together AI, and NVIDIA.
33-
- **Improved Test Coverage**: New DeepInfra, FriendliAI, Monster API, NVIDIA, Octo AI, Together AI, and watsonx.ai test cases.
34-
- **Refactor**: Improved support for OpenAI compatible APIs using new BaseInterface class.
35-
36-
**v2.0.6**
36+
**v2.0.8**
3737

38-
- **New LLM Provider**: Added support for watsonx.ai.
38+
- **Removing Dependencies**: The removal of OpenAI and Groq SDKs results in a smaller bundle, faster installs, and reduced complexity.
3939

4040
## Dependencies
4141

@@ -111,13 +111,13 @@ The project includes tests for each LLM handler. To run the tests, use the follo
111111
npm test
112112
```
113113

114-
#### Current Test Results
114+
#### Current Test Results
115115

116116
```bash
117-
Test Suites: 52 passed, 52 total
118-
Tests: 2 skipped, 215 passed, 217 total
117+
Test Suites: 1 skipped, 65 passed, 65 of 66 total
118+
Tests: 2 skipped, 291 passed, 293 total
119119
Snapshots: 0 total
120-
Time: 76.236 s
120+
Time: 103.293 s, estimated 121 s
121121
```
122122

123123
_Note: Currently skipping NVIDIA test cases due to API key limits._

docs/APIKEYS.md

+24-2
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,30 @@ The Cohere API offers trial keys. Trial keys are rate-limited, and cannot be use
3232

3333
- https://dashboard.cohere.com/api-keys
3434

35-
## Deepinfra
35+
## DeepInfra
3636

37-
The Deepinfra API is commercial but new accounts will start with a $1.80 credit.
37+
The DeepInfra API is commercial but new accounts will start with a $1.80 credit.
38+
39+
- https://deepinfra.com/dash/api_keys
40+
41+
## DeepSeek
42+
43+
The DeepSeek API is commercial and required a credit card or debit card to get started.
44+
45+
- https://platform.deepseek.com/api_keys
3846

3947
## Fireworks AI
4048

4149
The Fireworks AI API offers a free developer tier and commercial accounts. A Credit is not required for the free developer tier.
4250

4351
- https://fireworks.ai/api-keys
4452

53+
## Forefront
54+
55+
The Forefront API is commercial but it comes with $20 free credit.
56+
57+
- https://platform.forefront.ai/app/api-keys
58+
4559
## Friendli AI
4660

4761
The Friendli AI API is commercial but it comes with a $5.00 credit.
@@ -110,6 +124,14 @@ The Reka AI API requires a credit card, but currently comes with a $5.00 credit.
110124

111125
- https://platform.reka.ai/apikeys
112126

127+
## Replicate
128+
129+
The Replicate API is commercial but it does offer a free tier that you can use without providing a credit card.
130+
131+
- https://replicate.com/
132+
133+
After you login, you will need to click "Dashboard", then "Run a model".
134+
113135
## Together AI
114136

115137
The Together API is commercial, but it did not require a credit card, and it came with a $5.00 credit.

env

+7-4
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@ AI21_API_KEY=
1111
FIREWORKSAI_API_KEY=
1212
CLOUDFLARE_API_KEY=
1313
CLOUDFLARE_ACCOUNT_ID=
14-
LLAMACPP_URL=http://localhost:8080/completions
15-
CLOUDFLARE_API_KEY=
16-
CLOUDFLARE_ACCOUNT_ID=
1714
WATSONXSAI_API_KEY=
1815
WATSONXSAI_SPACE_ID=
1916
FRIENDLIAI_API_KEY=
2017
NVIDIA_API_KEY=
2118
DEEPINFRA_API_KEY=
2219
TOGETHERAI_API_KEY=
2320
MONSTERAPI_API_KEY=
24-
OCTOAI_API_KEY=
21+
OCTOAI_API_KEY=
22+
AIMLAPI_API_KEY=
23+
FOREFRONT_API_KEY=
24+
DEEPSEEK_API_KEY=
25+
26+
REPLICATE_API_KEY=
27+
LLAMACPP_URL=http://localhost:8080/completions

examples/json-output.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @file examples/json-output.js
3+
* @description Example showing JSON output. To do this, I will specify my JSON output requirements through my prompt.
4+
*/
5+
const { LLMInterface } = require('llm-interface');
6+
const { simplePrompt, options } = require('../src/utils/defaults.js');
7+
8+
require('dotenv').config({ path: '../.env' });
9+
10+
// Setup your key and interface
11+
const interface = 'huggingface';
12+
const apiKey = process.env.HUGGINGFACE_API_KEY;
13+
14+
/**
15+
* Main exampleUsage() function.
16+
*/
17+
async function exampleUsage() {
18+
let prompt = `${simplePrompt} Return 5 results.\n\nProvide the response as a JSON object.\n\nFollow this output format, only responding with the JSON object and nothing else:\n\n{title, reason}`;
19+
20+
console.log('JSON Output (Prompt Based):');
21+
console.log();
22+
console.log('Prompt:');
23+
console.log(`> ${prompt.replaceAll('\n\n', '\n>\n> ')}`);
24+
console.log();
25+
26+
LLMInterface.setApiKey(interface, apiKey);
27+
28+
try {
29+
const response = await LLMInterface.sendMessage(interface, prompt, {
30+
max_tokens: 1024,
31+
});
32+
33+
console.log('Repaired JSON Result:');
34+
console.log(response.results);
35+
console.log();
36+
} catch (error) {
37+
console.error('Error processing LLMInterface.sendMessage:', error);
38+
}
39+
}
40+
41+
exampleUsage();

examples/json-repair.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @file examples/native-json-output.js
3+
* @description Example showing JSON repair. To do this, I will specify my JSON output requirements through my prompt, and I will request a
4+
* larger result set then can be returned based on token size using a prompt, this will result in a response containing an invalid JSON object. I
5+
* will then repair the response using the attemptJsonRepair interfaceOption.
6+
*/
7+
const { LLMInterface } = require('llm-interface');
8+
const { simplePrompt, options } = require('../src/utils/defaults.js');
9+
10+
require('dotenv').config({ path: '../.env' });
11+
12+
// Setup your key and interface
13+
const interface = 'groq';
14+
const apiKey = process.env.GROQ_API_KEY;
15+
16+
/**
17+
* Main exampleUsage() function.
18+
*/
19+
async function exampleUsage() {
20+
let prompt = `${simplePrompt} Return 5 results.\n\nProvide the response as a JSON object.\n\nFollow this output format, only responding with the JSON object and nothing else:\n\n{title, reason}`;
21+
22+
console.log('JSON Repair:');
23+
console.log();
24+
console.log('Prompt:');
25+
console.log(`> ${prompt.replaceAll('\n\n', '\n>\n> ')}`);
26+
console.log();
27+
28+
LLMInterface.setApiKey(interface, apiKey);
29+
30+
try {
31+
const response = await LLMInterface.sendMessage(
32+
interface,
33+
prompt,
34+
{
35+
max_tokens: 100,
36+
},
37+
{ attemptJsonRepair: true },
38+
);
39+
40+
console.log('Repaired JSON Result:');
41+
console.log(response.results);
42+
console.log();
43+
} catch (error) {
44+
console.error('Error processing LLMInterface.sendMessage:', error);
45+
}
46+
}
47+
48+
exampleUsage();

examples/native-json-output.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @file examples/native-json-output.js
3+
* @description Example showing native JSON output. I will specify my JSON requirements in my prompt, and also specify native JSON mode. This will have
4+
* the added benefit of server side JSON validation, however this can return a null response when the result set is too large for the response token.
5+
*/
6+
const { LLMInterface } = require('llm-interface');
7+
const { simplePrompt, options } = require('../src/utils/defaults.js');
8+
9+
require('dotenv').config({ path: '../.env' });
10+
11+
// Setup your key and interface
12+
const interface = 'gemini';
13+
const apiKey = process.env.GEMINI_API_KEY;
14+
15+
/**
16+
* Main exampleUsage() function.
17+
*/
18+
async function exampleUsage() {
19+
let prompt = `${simplePrompt} Return 5 results.\n\nProvide the response as a valid JSON object; validate the object before responding.\n\nJSON Output Format: [{title, reason}]`;
20+
21+
console.log('Native JSON Output:');
22+
console.log();
23+
console.log('Prompt:');
24+
console.log(`> ${prompt.replaceAll('\n\n', '\n>\n> ')}`);
25+
console.log();
26+
27+
LLMInterface.setApiKey(interface, apiKey);
28+
29+
try {
30+
const response = await LLMInterface.sendMessage(interface, prompt, {
31+
max_tokens: 1024,
32+
response_format: 'json_object',
33+
});
34+
35+
console.log('JSON Result:');
36+
console.log(response.results);
37+
console.log();
38+
} catch (error) {
39+
console.error('Error processing LLMInterface.sendMessage:', error);
40+
}
41+
}
42+
43+
exampleUsage();

0 commit comments

Comments
 (0)