The llm_api_access
crate provides a unified way to interact with different large language models (LLMs) like OpenAI, Gemini, and Anthropic.
Current Status
Currently I develop this because it is used to power an open-source coding assistant currently in active development. Gemini has been the main test target; OpenAI (including embeddings) and Anthropic are supported but have been exercised less. Development is self encouraged so updates can be far and few between, open an issue on github if you want something specific.
LLM Enum
This enum represents the supported LLM providers:
OpenAI
: Represents the OpenAI language model.Gemini
: Represents the Gemini language model.Anthropic
: Represents the Anthropic language model.
Access Trait
The Access
trait defines asynchronous methods for interacting with LLMs:
send_single_message
: Sends a single message and returns the generated response.send_convo_message
: Sends a list of messages as a conversation and returns the generated response.get_model_info
: Gets information about a specific LLM model.list_models
: Lists all available LLM models.count_tokens
: Counts the number of tokens in a given text.
The LLM
enum implements Access
, providing specific implementations for each method based on the chosen LLM provider.
Note: Currently, get_model_info
, list_models
, and count_tokens
only work for the Gemini LLM. Other providers return an error indicating this functionality is not yet supported.
Loading API Credentials with dotenv
The llm_api_access
crate uses the dotenv
library to securely load API credentials from a .env
file in your project's root directory. This file should contain key-value pairs for each LLM provider you want to use.
Example Structure:
OPEN_AI_ORG=your_openai_org
OPENAI_API_KEY=your_openai_api_key
GEMINI_API_KEY=your_gemini_api_key
ANTHROPIC_API_KEY=your_anthropic_api_key
Steps:
- Create
.env
File: Create a file named.env
at the root of your Rust project directory. - Add API Keys: Fill in the
.env
file with the following format, replacing placeholders with your actual API keys.
Important Note:
- Never commit your
.env
file to version control systems like Git. It contains sensitive information like API keys.
Example Usage
send_single_message
Example
use ;
async
This example creates an instance of the LLM::OpenAI
provider and sends a single message using the send_single_message
method. It then matches the result, printing the generated joke or an error message if an error occurred.
send_convo_message
Example
use ;
use Message;
async
Note: This example requires API keys and configuration for the Gemini LLM provider.
Embeddings
The crate provides support for generating text embeddings through the OpenAI API.
OpenAI Embeddings
The openai
module includes functionality to generate vector embeddings:
pub async
This function takes:
input
: The text to generate embeddings fordimensions
: Optional parameter to specify the number of dimensions (if omitted, uses the model default)
It returns a vector of floating point values representing the text embedding.
Example Usage:
use get_embedding;
async
The function uses the "text-embedding-3-small" model by default and requires the same environment variables as other OpenAI API calls (OPEN_AI_KEY
and OPEN_AI_ORG
).
Testing
The llm_api_access
crate includes unit tests for various methods in the Access
trait. These tests showcase usage and expected behavior with different LLM providers.