Skip to content
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

add huggingface model #2624

Merged
merged 9 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions api/apps/llm_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ def apikey_json(keys):
elif factory == "LocalAI":
llm_name = req["llm_name"]+"___LocalAI"
api_key = "xxxxxxxxxxxxxxx"

elif factory == "HuggingFace":
llm_name = req["llm_name"]+"___HuggingFace"
api_key = "xxxxxxxxxxxxxxx"

elif factory == "OpenAI-API-Compatible":
llm_name = req["llm_name"]+"___OpenAI-API"
Expand Down
9 changes: 8 additions & 1 deletion conf/llm_factories.json
Original file line number Diff line number Diff line change
Expand Up @@ -2344,6 +2344,13 @@
"tags": "LLM",
"status": "1",
"llm": []
}
},
{
"name": "HuggingFace",
"logo": "",
"tags": "TEXT EMBEDDING",
"status": "1",
"llm": []
}
]
}
5 changes: 3 additions & 2 deletions rag/llm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from .cv_model import *
from .rerank_model import *
from .sequence2txt_model import *
from .tts_model import *
from .tts_model import *

EmbeddingModel = {
"Ollama": OllamaEmbed,
Expand Down Expand Up @@ -46,7 +46,8 @@
"SILICONFLOW": SILICONFLOWEmbed,
"Replicate": ReplicateEmbed,
"BaiduYiyan": BaiduYiyanEmbed,
"Voyage AI": VoyageEmbed
"Voyage AI": VoyageEmbed,
"HuggingFace":HuggingFaceEmbed,
}


Expand Down
1 change: 1 addition & 0 deletions rag/llm/chat_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1414,3 +1414,4 @@ def chat_streamly(self, system, history, gen_conf):
yield ans + "\n**ERROR**: " + str(e)

yield response._chunks[-1].usage_metadata.total_token_count

35 changes: 35 additions & 0 deletions rag/llm/embedding_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,3 +674,38 @@ def encode_queries(self, text):
texts=text, model=self.model_name, input_type="query"
)
return np.array(res.embeddings), res.total_tokens
class HuggingFaceEmbed(Base):
KevinHuSh marked this conversation as resolved.
Show resolved Hide resolved
KevinHuSh marked this conversation as resolved.
Show resolved Hide resolved
def __init__(self, key, model_name, base_url=None):
if not model_name:
raise ValueError("Model name cannot be None")
self.key = key
self.model_name = model_name
self.base_url = base_url or "http://127.0.0.1:8080"

def encode(self, texts: list, batch_size=32):
embeddings = []
for text in texts:
response = requests.post(
f"{self.base_url}/embed",
json={"inputs": text},
headers={'Content-Type': 'application/json'}
)
if response.status_code == 200:
embedding = response.json()
embeddings.append(embedding[0])
else:
raise Exception(f"Error: {response.status_code} - {response.text}")
return np.array(embeddings), sum([num_tokens_from_string(text) for text in texts])

def encode_queries(self, text):
response = requests.post(
f"{self.base_url}/embed",
json={"inputs": text},
headers={'Content-Type': 'application/json'}
)
if response.status_code == 200:
embedding = response.json()
return np.array(embedding[0]), num_tokens_from_string(text)
else:
raise Exception(f"Error: {response.status_code} - {response.text}")

37 changes: 37 additions & 0 deletions web/src/assets/svg/llm/huggingface.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions web/src/pages/user-setting/constants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ export const LocalLlmFactories = [
'TogetherAI',
'Replicate',
'OpenRouter',
'HuggingFace',
];
1 change: 1 addition & 0 deletions web/src/pages/user-setting/setting-model/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const IconMap = {
Anthropic: 'anthropic',
'Voyage AI': 'voyage',
'Google Cloud': 'google-cloud',
HuggingFace: 'huggingface',
};

export const BedrockRegionList = [
Expand Down
38 changes: 28 additions & 10 deletions web/src/pages/user-setting/setting-model/ollama-modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ type FieldType = IAddLlmRequestBody & { vision: boolean };

const { Option } = Select;

const llmFactoryToUrlMap = {
Ollama: 'https://huggingface.co/docs/text-embeddings-inference/quick_tour',
Xinference: 'https://inference.readthedocs.io/en/latest/user_guide',
LocalAI: 'https://localai.io/docs/getting-started/models/',
'LM-Studio': 'https://lmstudio.ai/docs/basics',
'OpenAI-API-Compatible': 'https://platform.openai.com/docs/models/gpt-4',
TogetherAI: 'https://docs.together.ai/docs/deployment-options',
Replicate: 'https://replicate.com/docs/topics/deployments',
OpenRouter: 'https://openrouter.ai/docs',
HuggingFace:
'https://huggingface.co/docs/text-embeddings-inference/quick_tour',
};
type LlmFactory = keyof typeof llmFactoryToUrlMap;

const OllamaModal = ({
visible,
hideModal,
Expand Down Expand Up @@ -35,7 +49,9 @@ const OllamaModal = ({

onOk?.(data);
};

const url =
llmFactoryToUrlMap[llmFactory as LlmFactory] ||
'https://huggingface.co/docs/text-embeddings-inference/quick_tour';
return (
<Modal
title={t('addLlmTitle', { name: llmFactory })}
Expand All @@ -46,11 +62,7 @@ const OllamaModal = ({
footer={(originNode: React.ReactNode) => {
return (
<Flex justify={'space-between'}>
<a
href={`https://github.com/infiniflow/ragflow/blob/main/docs/guides/deploy_local_llm.mdx`}
target="_blank"
rel="noreferrer"
>
<a href={url} target="_blank" rel="noreferrer">
{t('ollamaLink', { name: llmFactory })}
</a>
<Space>{originNode}</Space>
Expand All @@ -72,10 +84,16 @@ const OllamaModal = ({
rules={[{ required: true, message: t('modelTypeMessage') }]}
>
<Select placeholder={t('modelTypeMessage')}>
<Option value="chat">chat</Option>
<Option value="embedding">embedding</Option>
<Option value="rerank">rerank</Option>
<Option value="image2text">image2text</Option>
{llmFactory === 'HuggingFace' ? (
<Option value="embedding">embedding</Option>
) : (
<>
<Option value="chat">chat</Option>
<Option value="embedding">embedding</Option>
<Option value="rerank">rerank</Option>
<Option value="image2text">image2text</Option>
</>
)}
</Select>
</Form.Item>
<Form.Item<FieldType>
Expand Down