Spaces:
Running
Running
Gregor Betz
commited on
langchain_huggingface
Browse files- backend/models.py +21 -9
- requirements.txt +2 -2
backend/models.py
CHANGED
|
@@ -1,11 +1,14 @@
|
|
| 1 |
from typing import Any, Dict
|
| 2 |
from enum import Enum
|
| 3 |
|
| 4 |
-
from langchain_community.chat_models.huggingface import ChatHuggingFace
|
| 5 |
-
from langchain_community.llms.huggingface_endpoint import HuggingFaceEndpoint
|
| 6 |
from langchain_core import pydantic_v1
|
| 7 |
from langchain_core.language_models.chat_models import BaseChatModel
|
| 8 |
from langchain_core.utils import get_from_dict_or_env
|
|
|
|
|
|
|
|
|
|
| 9 |
from langchain_openai import ChatOpenAI
|
| 10 |
|
| 11 |
|
|
@@ -80,24 +83,33 @@ def get_chat_model_wrapper(
|
|
| 80 |
model_id: str,
|
| 81 |
inference_server_url: str,
|
| 82 |
token: str,
|
| 83 |
-
backend: str =
|
| 84 |
**model_init_kwargs
|
| 85 |
):
|
| 86 |
|
| 87 |
backend = LLMBackends(backend)
|
| 88 |
|
| 89 |
if backend == LLMBackends.HFChat:
|
| 90 |
-
llm = LazyHuggingFaceEndpoint(
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
task="text-generation",
|
| 93 |
huggingfacehub_api_token=token,
|
| 94 |
**model_init_kwargs,
|
| 95 |
)
|
|
|
|
| 96 |
|
| 97 |
-
from transformers import AutoTokenizer
|
| 98 |
-
|
| 99 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id, token=token)
|
| 100 |
-
chat_model = LazyChatHuggingFace(llm=llm, model_id=model_id, tokenizer=tokenizer)
|
| 101 |
elif backend in [LLMBackends.VLLM, LLMBackends.Fireworks]:
|
| 102 |
chat_model = ChatOpenAI(
|
| 103 |
model=model_id,
|
|
|
|
| 1 |
from typing import Any, Dict
|
| 2 |
from enum import Enum
|
| 3 |
|
| 4 |
+
#from langchain_community.chat_models.huggingface import ChatHuggingFace
|
| 5 |
+
#from langchain_community.llms.huggingface_endpoint import HuggingFaceEndpoint
|
| 6 |
from langchain_core import pydantic_v1
|
| 7 |
from langchain_core.language_models.chat_models import BaseChatModel
|
| 8 |
from langchain_core.utils import get_from_dict_or_env
|
| 9 |
+
from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint
|
| 10 |
+
|
| 11 |
+
|
| 12 |
from langchain_openai import ChatOpenAI
|
| 13 |
|
| 14 |
|
|
|
|
| 83 |
model_id: str,
|
| 84 |
inference_server_url: str,
|
| 85 |
token: str,
|
| 86 |
+
backend: str = LLMBackends.HFChat,
|
| 87 |
**model_init_kwargs
|
| 88 |
):
|
| 89 |
|
| 90 |
backend = LLMBackends(backend)
|
| 91 |
|
| 92 |
if backend == LLMBackends.HFChat:
|
| 93 |
+
# llm = LazyHuggingFaceEndpoint(
|
| 94 |
+
# endpoint_url=inference_server_url,
|
| 95 |
+
# task="text-generation",
|
| 96 |
+
# huggingfacehub_api_token=token,
|
| 97 |
+
# **model_init_kwargs,
|
| 98 |
+
# )
|
| 99 |
+
|
| 100 |
+
# from transformers import AutoTokenizer
|
| 101 |
+
|
| 102 |
+
# tokenizer = AutoTokenizer.from_pretrained(model_id, token=token)
|
| 103 |
+
# chat_model = LazyChatHuggingFace(llm=llm, model_id=model_id, tokenizer=tokenizer)
|
| 104 |
+
|
| 105 |
+
llm = HuggingFaceEndpoint(
|
| 106 |
+
repo_id=model_id,
|
| 107 |
task="text-generation",
|
| 108 |
huggingfacehub_api_token=token,
|
| 109 |
**model_init_kwargs,
|
| 110 |
)
|
| 111 |
+
chat_model = ChatHuggingFace(llm=llm)
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
elif backend in [LLMBackends.VLLM, LLMBackends.Fireworks]:
|
| 114 |
chat_model = ChatOpenAI(
|
| 115 |
model=model_id,
|
requirements.txt
CHANGED
|
@@ -2,8 +2,8 @@ gradio==4.37.2
|
|
| 2 |
aiohttp
|
| 3 |
datasets
|
| 4 |
huggingface_hub
|
| 5 |
-
langchain
|
| 6 |
-
|
| 7 |
langchain_openai
|
| 8 |
sentencepiece
|
| 9 |
transformers
|
|
|
|
| 2 |
aiohttp
|
| 3 |
datasets
|
| 4 |
huggingface_hub
|
| 5 |
+
langchain
|
| 6 |
+
langchain_huggingface
|
| 7 |
langchain_openai
|
| 8 |
sentencepiece
|
| 9 |
transformers
|