Skip to content

Commit

Permalink
feat: add audio model classes and clean up unused function (#46)
Browse files Browse the repository at this point in the history
* feat: enhance LLM response handling and add client function

- Updated `get_litellm_response` to include a default method for OpenAI style requests.
- Introduced a new function `get_llm_client` for LLM client initialization, currently a placeholder.

* feat: add audio model classes and clean up unused function

- Added `AudioModel` and `SpeechModel` to the `__init__.py` for better model organization.
- Removed the unused `get_llm_client` function from `utils.py` to streamline the codebase.

* chore: code format

* docs: update README with environment setup instructions and remove setup_guide.md

- Added instructions for setting environment variables `OPENAI_API_KEY`, `NERIF_DEFAULT_LLM_MODEL`, and `NERIF_DEFAULT_EMBEDDING_MODEL` in README.md.
- Provided an example of how to export these variables in a bash environment.
- Removed the outdated setup_guide.md file to streamline documentation.
  • Loading branch information
Chivier authored Jan 4, 2025
1 parent d6bbdea commit d443571
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 19 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ Our goal is to make LLMs easier to use for developers, turning complex AI capabi
### Pre-requisite

- Python >= 3.9
- Environment variable `OPENAI_API_KEY` or other LLM API keys, for more details, see [here](https://nerif-ai.com/docs/nerif-environment)
- Set default model and embedding model with `NERIF_DEFAULT_LLM_MODEL` and `NERIF_DEFAULT_EMBEDDING_MODEL`, for more details, see [here](https://nerif-ai.com/docs/nerif-environment)

Example:

```bash
export OPENAI_API_KEY=`your_api_key`
export NERIF_DEFAULT_LLM_MODEL=gpt-4o
export NERIF_DEFAULT_EMBEDDING_MODEL=text-embedding-3-small
```

### Install

Expand All @@ -33,6 +43,7 @@ from nerif.core import nerif
from nerif.model import SimpleChatModel

model = SimpleChatModel()
# Default model is `gpt-4o`

# Use nerif judge "natural language statement"
if nerif("the sky is blue"):
Expand Down
18 changes: 0 additions & 18 deletions docs/setup_guide.md

This file was deleted.

3 changes: 3 additions & 0 deletions nerif/model/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from .audio_model import AudioModel, SpeechModel
from .model import LogitsChatModel, SimpleChatModel, SimpleEmbeddingModel, VisionModel

__all__ = [
"LogitsChatModel",
"SimpleChatModel",
"SimpleEmbeddingModel",
"VisionModel",
"AudioModel",
"SpeechModel",
]
52 changes: 52 additions & 0 deletions nerif/model/audio_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import base64
from pathlib import Path
from typing import Any, List, Optional

from openai import OpenAI

from ..utils import (
LOGGER,
NERIF_DEFAULT_LLM_MODEL,
OPENAI_MODEL,
MessageType,
NerifTokenCounter,
get_litellm_embedding,
get_litellm_response,
get_ollama_response,
get_sllm_response,
get_vllm_response,
)


class AudioModel:
"""
A simple agent for audio tasks. (audio transcription, speech to text)
"""

def __init__(self):
self.client = OpenAI()

def transcribe(self, file: Path):
with open(file, "rb") as audio_file:
transcription = self.client.audio.transcriptions.create(
model="whisper-1",
file=audio_file,
)
return transcription


class SpeechModel:
"""
A simple agent for speech tasks. (speech model, text to speech)
"""

def __init__(self):
self.client = OpenAI()

def text_to_speech(self, text: str, voice: str = "alloy"):
response = self.client.audio.speech.create(
model="tts-1",
input=text,
voice=voice,
)
return response
8 changes: 7 additions & 1 deletion nerif/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,13 @@ def get_litellm_response(
"messages": messages,
}
else:
raise ValueError(f"Model {model} not supported")
# default method: use openai style
kargs = {
"model": model,
"messages": messages,
"api_key": api_key,
"base_url": base_url,
}

kargs["stream"] = stream
kargs["temperature"] = temperature
Expand Down

0 comments on commit d443571

Please sign in to comment.