-
Notifications
You must be signed in to change notification settings - Fork 151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
integration: Add Hugging Face local models; ST for embeddings #402
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"create-llama": patch | ||
--- | ||
|
||
Add local models via Hugging Face; use Sentence Transformers w. ONNX instead of FastEmbed (support for more models, etc) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import prompts from "prompts"; | ||
import { ModelConfigParams } from "."; | ||
import { questionHandlers, toChoice } from "../../questions/utils"; | ||
|
||
const MODELS = ["HuggingFaceH4/zephyr-7b-alpha"]; | ||
type ModelData = { | ||
dimensions: number; | ||
}; | ||
const EMBEDDING_MODELS: Record<string, ModelData> = { | ||
"all-MiniLM-L6-v2": { dimensions: 384 }, | ||
}; | ||
|
||
const DEFAULT_MODEL = MODELS[0]; | ||
const DEFAULT_EMBEDDING_MODEL = Object.keys(EMBEDDING_MODELS)[0]; | ||
const DEFAULT_DIMENSIONS = Object.values(EMBEDDING_MODELS)[0].dimensions; | ||
|
||
type HuggingfaceQuestionsParams = { | ||
askModels: boolean; | ||
}; | ||
|
||
export async function askHuggingfaceQuestions({ | ||
askModels, | ||
}: HuggingfaceQuestionsParams): Promise<ModelConfigParams> { | ||
const config: ModelConfigParams = { | ||
model: DEFAULT_MODEL, | ||
embeddingModel: DEFAULT_EMBEDDING_MODEL, | ||
dimensions: DEFAULT_DIMENSIONS, | ||
isConfigured(): boolean { | ||
return true; | ||
}, | ||
}; | ||
|
||
if (askModels) { | ||
const { model } = await prompts( | ||
{ | ||
type: "select", | ||
name: "model", | ||
message: "Which Hugging Face model would you like to use?", | ||
choices: MODELS.map(toChoice), | ||
initial: 0, | ||
}, | ||
questionHandlers, | ||
); | ||
config.model = model; | ||
|
||
const { embeddingModel } = await prompts( | ||
{ | ||
type: "select", | ||
name: "embeddingModel", | ||
message: "Which embedding model would you like to use?", | ||
choices: Object.keys(EMBEDDING_MODELS).map(toChoice), | ||
initial: 0, | ||
}, | ||
questionHandlers, | ||
); | ||
config.embeddingModel = embeddingModel; | ||
config.dimensions = EMBEDDING_MODELS[embeddingModel].dimensions; | ||
} | ||
|
||
return config; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,8 @@ def init_settings(): | |
init_mistral() | ||
case "azure-openai": | ||
init_azure_openai() | ||
case "huggingface": | ||
init_huggingface() | ||
case "t-systems": | ||
from .llmhub import init_llmhub | ||
|
||
|
@@ -113,29 +115,40 @@ def init_azure_openai(): | |
) | ||
|
||
|
||
def init_fastembed(): | ||
def init_huggingface_embedding(): | ||
try: | ||
from llama_index.embeddings.fastembed import FastEmbedEmbedding | ||
from llama_index.embeddings.huggingface import HuggingFaceEmbedding | ||
except ImportError: | ||
raise ImportError( | ||
"FastEmbed support is not installed. Please install it with `poetry add llama-index-embeddings-fastembed`" | ||
"Hugging Face support is not installed. Please install it with `poetry add llama-index-embeddings-huggingface`" | ||
) | ||
|
||
embed_model_map: Dict[str, str] = { | ||
# Small and multilingual | ||
"all-MiniLM-L6-v2": "sentence-transformers/all-MiniLM-L6-v2", | ||
# Large and multilingual | ||
"paraphrase-multilingual-mpnet-base-v2": "sentence-transformers/paraphrase-multilingual-mpnet-base-v2", | ||
} | ||
embedding_model = os.getenv("EMBEDDING_MODEL", "all-MiniLM-L6-v2") | ||
backend = os.getenv("EMBEDDING_BACKEND", "onnx") # "torch", "onnx", or "openvino" | ||
trust_remote_code = ( | ||
os.getenv("EMBEDDING_TRUST_REMOTE_CODE", "false").lower() == "true" | ||
) | ||
|
||
Settings.embed_model = HuggingFaceEmbedding( | ||
model_name=embedding_model, | ||
trust_remote_code=trust_remote_code, | ||
backend=backend, | ||
) | ||
|
||
|
||
embedding_model = os.getenv("EMBEDDING_MODEL") | ||
if embedding_model is None: | ||
raise ValueError("EMBEDDING_MODEL environment variable is not set") | ||
def init_huggingface(): | ||
try: | ||
from llama_index.llms.huggingface import HuggingFaceLLM | ||
except ImportError: | ||
raise ImportError( | ||
"Hugging Face support is not installed. Please install it with `poetry add llama-index-llms-huggingface` and `poetry add llama-index-embeddings-huggingface`" | ||
) | ||
|
||
# This will download the model automatically if it is not already downloaded | ||
Settings.embed_model = FastEmbedEmbedding( | ||
model_name=embed_model_map[embedding_model] | ||
Settings.llm = HuggingFaceLLM( | ||
model_name=os.getenv("MODEL"), | ||
tokenizer_name=os.getenv("MODEL"), | ||
) | ||
init_huggingface_embedding() | ||
marcusschiesser marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def init_groq(): | ||
|
@@ -147,8 +160,8 @@ def init_groq(): | |
) | ||
|
||
Settings.llm = Groq(model=os.getenv("MODEL")) | ||
# Groq does not provide embeddings, so we use FastEmbed instead | ||
init_fastembed() | ||
# Groq does not provide embeddings, so we use open Sentence Transformer models instead | ||
init_huggingface_embedding() | ||
|
||
|
||
def init_anthropic(): | ||
|
@@ -168,8 +181,8 @@ def init_anthropic(): | |
} | ||
|
||
Settings.llm = Anthropic(model=model_map[os.getenv("MODEL")]) | ||
# Anthropic does not provide embeddings, so we use FastEmbed instead | ||
init_fastembed() | ||
# Anthropic does not provide embeddings, so we use open Sentence Transformer models instead | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tomaarsen we actually had a PR that was replacing sentence transformers (
|
||
init_huggingface_embedding() | ||
|
||
|
||
def init_gemini(): | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tomaarsen please add other models that are running well locally. (I added a model selector that you can call with
create-llama --ask-models
)