repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | import importlib |
| #2 | import os |
| #3 | |
| #4 | import pytest |
| #5 | |
| #6 | from embedchain.config import BaseLlmConfig |
| #7 | from embedchain.llm.huggingface import HuggingFaceLlm |
| #8 | |
| #9 | |
| #10 | @pytest.fixture |
| #11 | def huggingface_llm_config(): |
| #12 | os.environ["HUGGINGFACE_ACCESS_TOKEN"] = "test_access_token" |
| #13 | config = BaseLlmConfig(model="google/flan-t5-xxl", max_tokens=50, temperature=0.7, top_p=0.8) |
| #14 | yield config |
| #15 | os.environ.pop("HUGGINGFACE_ACCESS_TOKEN") |
| #16 | |
| #17 | |
| #18 | @pytest.fixture |
| #19 | def huggingface_endpoint_config(): |
| #20 | os.environ["HUGGINGFACE_ACCESS_TOKEN"] = "test_access_token" |
| #21 | config = BaseLlmConfig(endpoint="https://api-inference.huggingface.co/models/gpt2", model_kwargs={"device": "cpu"}) |
| #22 | yield config |
| #23 | os.environ.pop("HUGGINGFACE_ACCESS_TOKEN") |
| #24 | |
| #25 | |
| #26 | def test_init_raises_value_error_without_api_key(mocker): |
| #27 | mocker.patch.dict(os.environ, clear=True) |
| #28 | with pytest.raises(ValueError): |
| #29 | HuggingFaceLlm() |
| #30 | |
| #31 | |
| #32 | def test_get_llm_model_answer_raises_value_error_for_system_prompt(huggingface_llm_config): |
| #33 | llm = HuggingFaceLlm(huggingface_llm_config) |
| #34 | llm.config.system_prompt = "system_prompt" |
| #35 | with pytest.raises(ValueError): |
| #36 | llm.get_llm_model_answer("prompt") |
| #37 | |
| #38 | |
| #39 | def test_top_p_value_within_range(): |
| #40 | config = BaseLlmConfig(top_p=1.0) |
| #41 | with pytest.raises(ValueError): |
| #42 | HuggingFaceLlm._get_answer("test_prompt", config) |
| #43 | |
| #44 | |
| #45 | def test_dependency_is_imported(): |
| #46 | importlib_installed = True |
| #47 | try: |
| #48 | importlib.import_module("huggingface_hub") |
| #49 | except ImportError: |
| #50 | importlib_installed = False |
| #51 | assert importlib_installed |
| #52 | |
| #53 | |
| #54 | def test_get_llm_model_answer(huggingface_llm_config, mocker): |
| #55 | mocker.patch("embedchain.llm.huggingface.HuggingFaceLlm._get_answer", return_value="Test answer") |
| #56 | |
| #57 | llm = HuggingFaceLlm(huggingface_llm_config) |
| #58 | answer = llm.get_llm_model_answer("Test query") |
| #59 | |
| #60 | assert answer == "Test answer" |
| #61 | |
| #62 | |
| #63 | def test_hugging_face_mock(huggingface_llm_config, mocker): |
| #64 | mock_llm_instance = mocker.Mock(return_value="Test answer") |
| #65 | mock_hf_hub = mocker.patch("embedchain.llm.huggingface.HuggingFaceHub") |
| #66 | mock_hf_hub.return_value.invoke = mock_llm_instance |
| #67 | |
| #68 | llm = HuggingFaceLlm(huggingface_llm_config) |
| #69 | answer = llm.get_llm_model_answer("Test query") |
| #70 | assert answer == "Test answer" |
| #71 | mock_llm_instance.assert_called_once_with("Test query") |
| #72 | |
| #73 | |
| #74 | def test_custom_endpoint(huggingface_endpoint_config, mocker): |
| #75 | mock_llm_instance = mocker.Mock(return_value="Test answer") |
| #76 | mock_hf_endpoint = mocker.patch("embedchain.llm.huggingface.HuggingFaceEndpoint") |
| #77 | mock_hf_endpoint.return_value.invoke = mock_llm_instance |
| #78 | |
| #79 | llm = HuggingFaceLlm(huggingface_endpoint_config) |
| #80 | answer = llm.get_llm_model_answer("Test query") |
| #81 | |
| #82 | assert answer == "Test answer" |
| #83 | mock_llm_instance.assert_called_once_with("Test query") |
| #84 |