Skip to content

Commit

Permalink
add huggingface model (infiniflow#2624)
Browse files Browse the repository at this point in the history
### What problem does this PR solve?

infiniflow#2469

### Type of change

- [x] New Feature (non-breaking change which adds functionality)

---------

Co-authored-by: Kevin Hu <[email protected]>
  • Loading branch information
JobSmithManipulation and KevinHuSh authored Sep 27, 2024
1 parent 1b2f66f commit 96f56a3
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 13 deletions.
4 changes: 4 additions & 0 deletions api/apps/llm_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,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

37 changes: 37 additions & 0 deletions rag/llm/embedding_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,3 +678,40 @@ 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):
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

0 comments on commit 96f56a3

Please sign in to comment.