Skip to content

Commit

Permalink
Merge pull request #2 from samestrin/interfaces
Browse files Browse the repository at this point in the history
v0.0.10
  • Loading branch information
samestrin authored Jun 17, 2024
2 parents 166bc13 + 3a4b6ce commit 183b0c1
Show file tree
Hide file tree
Showing 28 changed files with 1,213 additions and 240 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ The LLM Interface project is a versatile and comprehensive wrapper designed to i

## Updates

**v0.0.10**

- **Hugging Face**: Added support for Hugging Face Inference (_over 150,000 publicly accessible machine learning models_)
- **Perplexity Labs**: Added support for Perplexity Labs
- **JSON Improvements**: The `json_object` mode for OpenAI and Gemini now guarantees the return a valid JSON object or null.
- **Graceful Retries**: Retry LLM queries upon failure.

**v0.0.9**

- **Response Caching**: Efficiently caches LLM responses to reduce costs, enhance performance and minimize redundant requests, with customizable cache timeout settings.
Expand All @@ -19,10 +26,6 @@ The LLM Interface project is a versatile and comprehensive wrapper designed to i
- **Mistral AI**: Added support for Mistral AI
- **Cohere**: Added support for Cohere

**v0.0.7**

- **Goose AI**: Added support for Goose AI

## Features

- **Unified Interface**: A single, consistent interface to interact with multiple LLM APIs.
Expand All @@ -36,7 +39,7 @@ The LLM Interface project is a versatile and comprehensive wrapper designed to i

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

- `axios`: For making HTTP requests (used for Cohere, Goose AI, LLaMA.cpp, Mistral, and Reka AI).
- `axios`: For making HTTP requests (used for various HTTP AI APIs).
- `@anthropic-ai/sdk`: SDK for interacting with the Anthropic API.
- `@google/generative-ai`: SDK for interacting with the Google Gemini API.
- `groq-sdk`: SDK for interacting with the Groq API.
Expand Down
3 changes: 3 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ module.exports = {
gooseApiKey: process.env.GOOSE_API_KEY,
cohereApiKey: process.env.COHERE_API_KEY,
mistralApiKey: process.env.MISTRAL_API_KEY,
huggingfaceApiKey: process.env.HUGGINGFACE_API_KEY,
perplexityApiKey: process.env.PERPLEXITY_API_KEY,
ai21ApiKey: process.env.AI21_API_KEY,
};
66 changes: 66 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,28 @@ openai
});
```

### AI21

#### `sendMessage(message, options, cacheTimeoutSeconds)`

- **Parameters:**
- `message`: An object containing the model and messages to send.
- `options`: An optional object containing `max_tokens` and `model`.
- `cacheTimeoutSeconds`: An optional number specifying the cache timeout in seconds. If set, caching is enabled.
- **Returns:** A promise that resolves to the response text.
- **Example:**

```javascript
ai21
.sendMessage(message, { max_tokens: 150 })
.then((response) => {
console.log(response);
})
.catch((error) => {
console.error(error);
});
```

### Anthropic

#### `sendMessage(message, options, cacheTimeoutSeconds)`
Expand Down Expand Up @@ -147,6 +169,28 @@ groq
});
```

### Hugging Face

#### `sendMessage(message, options, cacheTimeoutSeconds)`

- **Parameters:**
- `message`: An object containing the model and messages to send.
- `options`: An optional object containing `max_tokens` and `model`.
- `cacheTimeoutSeconds`: An optional number specifying the cache timeout in seconds. If set, caching is enabled.
- **Returns:** A promise that resolves to the response text.
- **Example:**

```javascript
huggingface
.sendMessage(message, { max_tokens: 100 })
.then((response) => {
console.log(response);
})
.catch((error) => {
console.error(error);
});
```

### Mistral AI

#### `sendMessage(message, options, cacheTimeoutSeconds)`
Expand All @@ -169,6 +213,28 @@ mistral
});
```

### Perplexity Labs

#### `sendMessage(message, options, cacheTimeoutSeconds)`

- **Parameters:**
- `message`: An object containing the model and messages to send.
- `options`: An optional object containing `max_tokens` and `model`.
- `cacheTimeoutSeconds`: An optional number specifying the cache timeout in seconds. If set, caching is enabled.
- **Returns:** A promise that resolves to the response text.
- **Example:**

```javascript
perplexity
.sendMessage(message, { max_tokens: 100 })
.then((response) => {
console.log(response);
})
.catch((error) => {
console.error(error);
});
```

### Reka AI

#### `sendMessage(message, options, cacheTimeoutSeconds)`
Expand Down
18 changes: 18 additions & 0 deletions docs/APIKEYS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ The OpenAI API requires a credit card.

- https://platform.openai.com/api-keys

## AI21 Studio

The AI21 API is a commercial product, but it currently does not require a credit card, and comes with a $90 credit.

- https://studio.ai21.com/account/api-key?source=docs

## Anthropic

The Anthropic API requires a credit card.
Expand Down Expand Up @@ -38,12 +44,24 @@ The Groq API is currently free.

- https://console.groq.com/keys

## Hugging Face

The Hugging Face Inference API is currently free for rate-limited, non-commercial use.

- https://huggingface.co/settings/tokens

## Mistral

The Mistral API is a commercial product, but it currently does not require a credit card, and comes with a $5.00 credit.

- https://console.mistral.ai/api-keys/

## Perplexity

The Perplexity API requires a credit cards.

- https://www.perplexity.ai/settings/api

## Reka AI

The Reka AI API requires a credit card, but currently comes with a $5 credit.
Expand Down
127 changes: 107 additions & 20 deletions docs/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@
- [Initializing llm-interface](#initializing-llm-interface)
- [Basic Usage Examples](#basic-usage-examples)
- [OpenAI Interface](#openai-interface)
- [AI21 Interface](#ai21-interface)
- [Anthropic Interface](#anthropic-interface)
- [Cohere Interface](#cohere-interface)
- [Gemini Interface](#gemini-interface)
- [Goose AI Interface](#goose-ai-interface)
- [Groq Interface](#groq-interface)
- [HuggingFace Interface](#huggingface-interface)
- [Mistral AI Interface](#mistral-ai-interface)
- [Perplexity Interface](#perplexity-interface)
- [Reka AI Interface](#reka-ai-interface)
- [LLaMA.cpp Interface](#llamacpp-interface)
- [Advanced Usage Examples](#advanced-usage-examples)
- [OpenAI Interface (JSON Output)](#openai-interface-json-output)
- [Gemini Interface (JSON Output)](#gemini-interface-json-output)
- [OpenAI Interface (Cached)](#openai-interface-cached)
- [OpenAI Interface (Graceful Retry)](#openai-interface-graceful-retry)

# Usage

Expand Down Expand Up @@ -99,6 +102,33 @@ anthropic
});
```

### AI21 Interface

The AI21 interface allows you to send messages to the AI21 API.

#### Example

```javascript
const ai21 = new LLMInterface.ai21(process.env.AI21_API_KEY);

const message = {
model: "jamba-instruct",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Explain the importance of low latency LLMs." },
],
};

ai21
.sendMessage(message, { max_tokens: 150 })
.then((response) => {
console.log(response);
})
.catch((error) => {
console.error(error);
});
```

### Cohere Interface

The Cohere interface allows you to send messages to the Cohere API.
Expand Down Expand Up @@ -207,6 +237,38 @@ groq
});
```

### HuggingFace Interface

The HuggingFace interface allows you to send messages to the HuggingFace API.

#### Example

```javascript
const huggingface = new LLMInterface.huggingface(process.env.ANTHROPIC_API_KEY);

const message = {
model: "claude-3-opus-20240229",
messages: [
{
role: "user",
content:
"You are a helpful assistant. Say OK if you understand and stop.",
},
{ role: "system", content: "OK" },
{ role: "user", content: "Explain the importance of low latency LLMs." },
],
};

huggingface
.sendMessage(message, { max_tokens: 150 })
.then((response) => {
console.log(response);
})
.catch((error) => {
console.error(error);
});
```

### Mistral AI Interface

The Mistral AI interface allows you to send messages to the Mistral AI API.
Expand Down Expand Up @@ -234,6 +296,38 @@ mistral
});
```

### Perplexity Interface

The Perplexity interface allows you to send messages to the Perplexity API.

#### Example

```javascript
const perplexity = new LLMInterface.perplexity(process.env.ANTHROPIC_API_KEY);

const message = {
model: "claude-3-opus-20240229",
messages: [
{
role: "user",
content:
"You are a helpful assistant. Say OK if you understand and stop.",
},
{ role: "system", content: "OK" },
{ role: "user", content: "Explain the importance of low latency LLMs." },
],
};

perplexity
.sendMessage(message, { max_tokens: 150 })
.then((response) => {
console.log(response);
})
.catch((error) => {
console.error(error);
});
```

### Reka AI Interface

The Reka AI interface allows you to send messages to the Reka AI REST API.
Expand Down Expand Up @@ -294,7 +388,7 @@ Then select the interface you'd like to use and initialize it with an API key or

### OpenAI Interface (JSON Output)

The OpenAI interface allows you to send messages to the OpenAI API and request the response back in JSON. To take advantage of this feature be sure to include text like "Return the results as a JSON object." and provide a desired output format like "Follow this format: [{reason, reasonDescription}]."
Some interfaces allows you request the response back in JSON, currently **OpenAI** and **Gemini** are supported. To take advantage of this feature be sure to include text like "Return the results as a JSON object." and provide a desired output format like "Follow this format: [{reason, reasonDescription}]." In this example we use OpenAI and request a valid JSON object.

#### Example

Expand Down Expand Up @@ -326,32 +420,25 @@ openai
});
```

### Gemini Interface (JSON Output)
### OpenAI Interface (Cached)

The Gemini interface allows you to send messages to the Google Gemini API. To take advantage of this feature be sure to include text like "Return the results as a JSON object." and provide a desired output format like "Follow this format: [{reason, reasonDescription}]."
To reduce operational costs and improve performance you can optionally specify a cache timeout in seconds. In this example we use OpenAI and store the results for 86400 seconds or one day.

#### Example

```javascript
const gemini = new LLMInterface.gemini(process.env.GEMINI_API_KEY);
const openai = new LLMInterface.openai(process.env.OPENAI_API_KEY);

const message = {
model: "gemini-1.5-flash",
model: "gpt-3.5-turbo",
messages: [
{
role: "system",
content: "You are a helpful assistant.",
},
{
role: "user",
content:
"Explain the importance of low latency LLMs. Return the results as a JSON object. Follow this format: [{reason, reasonDescription}].",
},
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Explain the importance of low latency LLMs." },
],
};

gemini
.sendMessage(message, { max_tokens: 100, response_format: "json_object" })
openai
.sendMessage(message, { max_tokens: 150 }, { cacheTimeoutSeconds: 86400 })
.then((response) => {
console.log(response);
})
Expand All @@ -360,9 +447,9 @@ gemini
});
```

### OpenAI Interface (Cached)
### OpenAI Interface (Graceful Retry)

The OpenAI interface allows you to send messages to the OpenAI API. To reduce operational costs and improve performance you can optionally specify a cache timeout in seconds. In this example we store the results for 86400 seconds or one day.
You can gracefully retry your requests. In this example we use OpenAI and up to 3 times if needed.

#### Example

Expand All @@ -378,7 +465,7 @@ const message = {
};

openai
.sendMessage(message, { max_tokens: 150 }, 86400)
.sendMessage(message, { max_tokens: 150 }, { retryAttempts: 3 })
.then((response) => {
console.log(response);
})
Expand Down
4 changes: 4 additions & 0 deletions env
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@
REKA_API_KEY=
GOOSE_API_KEY=
MISTRAL_API_KEY=
HUGGINGFACE_API_KEY=
PERPLEXITY_API_KEY=
AI21_API_KEY=
AZUREAI_API_KEY=
LLAMACPP_URL=http://localhost:8080/completions
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "llm-interface",
"version": "0.0.9",
"version": "0.0.10",
"main": "src/index.js",
"description": "A simple, unified interface for integrating and interacting with multiple Large Language Model (LLM) APIs, including OpenAI, Anthropic, Google Gemini, Groq, and LlamaCPP.",
"scripts": {
Expand Down
Loading

0 comments on commit 183b0c1

Please sign in to comment.