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

Fix for issue #19933: langchain_huggingface #25136

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -1,43 +1,44 @@
"""Hugging Face Chat Wrapper."""

from dataclasses import dataclass
from transformers.pipelines.text_generation import Chat
from typing import (
Any,
Callable,
Dict,
List,
Literal,
Optional,
Sequence,
Type,
Union,
cast,
)

from langchain_core.callbacks.manager import (
AsyncCallbackManagerForLLMRun,
CallbackManagerForLLMRun,
)
from langchain_core.language_models import LanguageModelInput
from langchain_core.language_models.chat_models import BaseChatModel
from langchain_core.messages import (
AIMessage,
BaseMessage,
ChatMessage,
HumanMessage,
SystemMessage,
ToolMessage,
)
from langchain_core.outputs import ChatGeneration, ChatResult, LLMResult
from langchain_core.pydantic_v1 import root_validator
from langchain_core.runnables import Runnable
from langchain_core.tools import BaseTool
from langchain_core.utils.function_calling import convert_to_openai_tool

from langchain_huggingface.llms.huggingface_endpoint import HuggingFaceEndpoint
from langchain_huggingface.llms.huggingface_pipeline import HuggingFacePipeline

DEFAULT_SYSTEM_PROMPT = """You are a helpful, respectful, and honest assistant."""

Check failure on line 41 in libs/partners/huggingface/langchain_huggingface/chat_models/huggingface.py

View workflow job for this annotation

GitHub Actions / cd libs/partners/huggingface / make lint #3.12

Ruff (I001)

langchain_huggingface/chat_models/huggingface.py:3:1: I001 Import block is un-sorted or un-formatted


@dataclass
Expand Down Expand Up @@ -356,7 +357,7 @@
def _generate(
self,
messages: List[BaseMessage],
stop: Optional[List[str]] = None,
stop: Optional[List[Chat]] = None,
run_manager: Optional[CallbackManagerForLLMRun] = None,
**kwargs: Any,
) -> ChatResult:
Expand Down Expand Up @@ -406,9 +407,7 @@

messages_dicts = [self._to_chatml_format(m) for m in messages]

return self.tokenizer.apply_chat_template(
messages_dicts, tokenize=False, add_generation_prompt=True
)
return messages_dicts

def _to_chatml_format(self, message: BaseMessage) -> dict:
"""Convert LangChain message to ChatML format."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from langchain_core.language_models.llms import BaseLLM
from langchain_core.outputs import Generation, GenerationChunk, LLMResult
from langchain_core.pydantic_v1 import Extra
from transformers.pipelines.text_generation import ReturnType

Check failure on line 11 in libs/partners/huggingface/langchain_huggingface/llms/huggingface_pipeline.py

View workflow job for this annotation

GitHub Actions / cd libs/partners/huggingface / make lint #3.12

Ruff (F401)

langchain_huggingface/llms/huggingface_pipeline.py:11:52: F401 `transformers.pipelines.text_generation.ReturnType` imported but unused

DEFAULT_MODEL_ID = "gpt2"
DEFAULT_TASK = "text-generation"
Expand Down Expand Up @@ -253,7 +254,7 @@

def _generate(
self,
prompts: List[str],
prompts: List[List[dict[str, str]]], # List of prompts in the ChatML format e.g {"role": "user", "content": "Hello, how are you?"}

Check failure on line 257 in libs/partners/huggingface/langchain_huggingface/llms/huggingface_pipeline.py

View workflow job for this annotation

GitHub Actions / cd libs/partners/huggingface / make lint #3.12

Ruff (E501)

langchain_huggingface/llms/huggingface_pipeline.py:257:89: E501 Line too long (138 > 88)
stop: Optional[List[str]] = None,
run_manager: Optional[CallbackManagerForLLMRun] = None,
**kwargs: Any,
Expand All @@ -269,6 +270,7 @@
# Process batch of prompts
responses = self.pipeline(
batch_prompts,
return_full_text=False,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do this in a separate issue / PR?

**pipeline_kwargs,
)

Expand All @@ -294,7 +296,8 @@
if skip_prompt:
text = text[len(batch_prompts[j]) :]
# Append the processed text to results
text_generations.append(text)
# The 'text' variable is in the ChatMl format so we get the last message (just gerenated by the model) and access the text content

Check failure on line 299 in libs/partners/huggingface/langchain_huggingface/llms/huggingface_pipeline.py

View workflow job for this annotation

GitHub Actions / cd libs/partners/huggingface / make lint #3.12

Ruff (E501)

langchain_huggingface/llms/huggingface_pipeline.py:299:89: E501 Line too long (146 > 88)
text_generations.append(text)

return LLMResult(
generations=[[Generation(text=text)] for text in text_generations]
Expand Down
Loading