Update llm/apimodels/hf_model.py
Browse files- llm/apimodels/hf_model.py +72 -0
llm/apimodels/hf_model.py
CHANGED
@@ -520,4 +520,76 @@ class HF_Qwen2(HFInterface, ABC):
|
|
520 |
`HF_Qwen2(llm=not initialized)`, indicating the state.
|
521 |
"""
|
522 |
llm_info = f"llm={self.llm}" if hasattr(self, 'llm') else 'llm=not initialized'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
523 |
return f"{self.__class__.__name__}({llm_info})"
|
|
|
520 |
`HF_Qwen2(llm=not initialized)`, indicating the state.
|
521 |
"""
|
522 |
llm_info = f"llm={self.llm}" if hasattr(self, 'llm') else 'llm=not initialized'
|
523 |
+
return f"{self.__class__.__name__}({llm_info})"
|
524 |
+
|
525 |
+
class FreeThinker3B(HFInterface, ABC):
|
526 |
+
"""
|
527 |
+
This class represents an interface for the FreeThinker3B small language model from Hugging Face.
|
528 |
+
It inherits from `HFInterface` (likely an interface from a Hugging Face library)
|
529 |
+
and `ABC` (for abstract base class) to enforce specific functionalities.
|
530 |
+
"""
|
531 |
+
|
532 |
+
def __init__(self):
|
533 |
+
"""
|
534 |
+
Initializer for the `FreeThinker3B` class.
|
535 |
+
- Retrieves configuration values for the FreeThinker3B model from a `config` dictionary:
|
536 |
+
- `repo_id`: The ID of the repository containing the FreeThinker3B model on Hugging Face.
|
537 |
+
- `max_length`: Maximum length of the generated text.
|
538 |
+
- `temperature`: Controls randomness in the generation process.
|
539 |
+
- `top_k`: Restricts the vocabulary used for generation.
|
540 |
+
- Raises a `ValueError` if the `api` key (presumably stored elsewhere) is missing.
|
541 |
+
- Creates an instance of `HuggingFaceEndpoint` using the retrieved configuration
|
542 |
+
and the `api` key.
|
543 |
+
"""
|
544 |
+
|
545 |
+
repo_id = config["FreeThinker3B"]["model"]
|
546 |
+
max_length = config["FreeThinker3B"]["max_new_tokens"]
|
547 |
+
temperature = config["FreeThinker3B"]["temperature"]
|
548 |
+
top_k = config["FreeThinker3B"]["top_k"]
|
549 |
+
|
550 |
+
if not _api:
|
551 |
+
raise ValueError(f"API key not provided {_api}")
|
552 |
+
|
553 |
+
self.llm = HuggingFaceEndpoint(
|
554 |
+
repo_id=repo_id, max_length=max_length, temperature=temperature, top_k=top_k, token=_api
|
555 |
+
)
|
556 |
+
|
557 |
+
def execution(self) -> Any:
|
558 |
+
"""
|
559 |
+
This method attempts to return the underlying `llm` (likely a language model object).
|
560 |
+
It wraps the retrieval in a `try-except` block to catch potential exceptions.
|
561 |
+
On success, it returns the `llm` object.
|
562 |
+
On failure, it logs an error message with the exception details using a logger
|
563 |
+
(assumed to be available elsewhere).
|
564 |
+
"""
|
565 |
+
try:
|
566 |
+
return self.llm # `invoke()`
|
567 |
+
except Exception as e:
|
568 |
+
print(f"Something wrong with API or HuggingFaceEndpoint: {e}")
|
569 |
+
|
570 |
+
def model_name(self):
|
571 |
+
"""
|
572 |
+
Simple method that returns the FreeThinker3B model name from the configuration.
|
573 |
+
This can be useful for identifying the specific model being used.
|
574 |
+
"""
|
575 |
+
return config["HF_Qwen2"]["model"]
|
576 |
+
|
577 |
+
def __str__(self):
|
578 |
+
"""
|
579 |
+
Defines the string representation of the `FreeThinker3B` object for human readability.
|
580 |
+
It combines the class name and the model name retrieved from the `model_name` method
|
581 |
+
with an underscore separator.
|
582 |
+
"""
|
583 |
+
return f"{self.__class__.__name__}_{self.model_name()}"
|
584 |
+
|
585 |
+
def __repr__(self):
|
586 |
+
"""
|
587 |
+
Defines the representation of the `FreeThinker3B` object for debugging purposes.
|
588 |
+
It uses `hasattr` to check if the `llm` attribute is set.
|
589 |
+
- If `llm` exists, it returns a string like `FreeThinker3B(llm=HuggingFaceEndpoint(...))`,
|
590 |
+
showing the class name and the `llm` object information.
|
591 |
+
- If `llm` is not yet set (during initialization), it returns
|
592 |
+
`FreeThinker3B(llm=not initialized)`, indicating the state.
|
593 |
+
"""
|
594 |
+
llm_info = f"llm={self.llm}" if hasattr(self, 'llm') else 'llm=not initialized'
|
595 |
return f"{self.__class__.__name__}({llm_info})"
|