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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
"""
Test HuggingFace LLM
"""

from base_llm_unit_tests import BaseLLMChatTest
import json
import os
import sys
from unittest.mock import patch, MagicMock, AsyncMock

sys.path.insert(
    0, os.path.abspath("../..")
)  # Adds the parent directory to the system path

import litellm
import pytest
from litellm.types.utils import ModelResponseStream, ModelResponse

MOCK_COMPLETION_RESPONSE = {
    "id": "9115d3daeab10608",
    "object": "chat.completion",
    "created": 11111,
    "model": "meta-llama/Meta-Llama-3-8B-Instruct",
    "prompt": [],
    "choices": [
        {
            "finish_reason": "stop",
            "seed": 3629048360264764400,
            "logprobs": None,
            "index": 0,
            "message": {
                "role": "assistant",
                "content": "This is a test response from the mocked HuggingFace API.",
                "tool_calls": [],
            },
        }
    ],
    "usage": {"prompt_tokens": 10, "completion_tokens": 20, "total_tokens": 30},
}

MOCK_STREAMING_CHUNKS = [
    {
        "id": "id1",
        "object": "chat.completion.chunk",
        "created": 1111,
        "choices": [
            {
                "index": 0,
                "text": "Deep",
                "logprobs": None,
                "finish_reason": None,
                "seed": None,
                "delta": {
                    "token_id": 34564,
                    "role": "assistant",
                    "content": "Deep",
                    "tool_calls": None,
                },
            }
        ],
        "model": "meta-llama/Meta-Llama-3-8B-Instruct-Turbo",
        "usage": None,
    },
    {
        "id": "id2",
        "object": "chat.completion.chunk",
        "created": 1111,
        "choices": [
            {
                "index": 0,
                "text": " learning",
                "logprobs": None,
                "finish_reason": None,
                "seed": None,
                "delta": {
                    "token_id": 6975,
                    "role": "assistant",
                    "content": " learning",
                    "tool_calls": None,
                },
            }
        ],
        "model": "meta-llama/Meta-Llama-3-8B-Instruct-Turbo",
        "usage": None,
    },
    {
        "id": "id3",
        "object": "chat.completion.chunk",
        "created": 1111,
        "choices": [
            {
                "index": 0,
                "text": " is",
                "logprobs": None,
                "finish_reason": None,
                "seed": None,
                "delta": {
                    "token_id": 374,
                    "role": "assistant",
                    "content": " is",
                    "tool_calls": None,
                },
            }
        ],
        "model": "meta-llama/Meta-Llama-3-8B-Instruct-Turbo",
        "usage": None,
    },
    {
        "id": "sid4",
        "object": "chat.completion.chunk",
        "created": 1111,
        "choices": [
            {
                "index": 0,
                "text": " response",
                "logprobs": None,
                "finish_reason": "length",
                "seed": 2853637492034609700,
                "delta": {
                    "token_id": 323,
                    "role": "assistant",
                    "content": " response",
                    "tool_calls": None,
                },
            }
        ],
        "model": "meta-llama/Meta-Llama-3-8B-Instruct-Turbo",
        "usage": {"prompt_tokens": 26, "completion_tokens": 20, "total_tokens": 46},
    },
]


PROVIDER_MAPPING_RESPONSE = {
    "fireworks-ai": {
        "status": "live",
        "providerId": "accounts/fireworks/models/llama-v3-8b-instruct",
        "task": "conversational",
    },
    "together": {
        "status": "live",
        "providerId": "meta-llama/Meta-Llama-3-8B-Instruct-Turbo",
        "task": "conversational",
    },
    "hf-inference": {
        "status": "live",
        "providerId": "meta-llama/Meta-Llama-3-8B-Instruct",
        "task": "conversational",
    },
}


@pytest.fixture
def mock_provider_mapping():
    with patch(
        "litellm.llms.huggingface.chat.transformation._fetch_inference_provider_mapping"
    ) as mock:
        mock.return_value = PROVIDER_MAPPING_RESPONSE
        yield mock


@pytest.fixture(autouse=True)
def clear_lru_cache():
    from litellm.llms.huggingface.common_utils import _fetch_inference_provider_mapping

    _fetch_inference_provider_mapping.cache_clear()
    yield
    _fetch_inference_provider_mapping.cache_clear()


@pytest.fixture
def mock_http_handler():
    """Fixture to mock the HTTP handler"""
    with patch("litellm.llms.custom_httpx.http_handler.HTTPHandler.post") as mock:
        print(f"Creating mock HTTP handler: {mock}")  # noqa: T201

        mock_response = MagicMock()
        mock_response.raise_for_status.return_value = None
        mock_response.status_code = 200

        def mock_side_effect(*args, **kwargs):
            if kwargs.get("stream", True):
                mock_response.iter_lines.return_value = iter(
                    [
                        f"data: {json.dumps(chunk)}".encode("utf-8")
                        for chunk in MOCK_STREAMING_CHUNKS
                    ]
                    + [b"data: [DONE]"]
                )
            else:
                mock_response.json.return_value = MOCK_COMPLETION_RESPONSE
            return mock_response

        mock.side_effect = mock_side_effect
        yield mock


@pytest.fixture
def mock_http_async_handler():
    """Fixture to mock the async HTTP handler"""
    with patch(
        "litellm.llms.custom_httpx.http_handler.AsyncHTTPHandler.post",
        new_callable=AsyncMock,
    ) as mock:
        print(f"Creating mock async HTTP handler: {mock}")  # noqa: T201

        mock_response = MagicMock()
        mock_response.raise_for_status.return_value = None
        mock_response.status_code = 200
        mock_response.headers = {"content-type": "application/json"}

        mock_response.json.return_value = MOCK_COMPLETION_RESPONSE
        mock_response.text = json.dumps(MOCK_COMPLETION_RESPONSE)

        async def mock_side_effect(*args, **kwargs):
            if kwargs.get("stream", True):

                async def mock_aiter():
                    for chunk in MOCK_STREAMING_CHUNKS:
                        yield f"data: {json.dumps(chunk)}".encode("utf-8")
                    yield b"data: [DONE]"

                mock_response.aiter_lines = mock_aiter
            return mock_response

        mock.side_effect = mock_side_effect
        yield mock


class TestHuggingFace(BaseLLMChatTest):
    @pytest.fixture(autouse=True)
    def setup(self, mock_provider_mapping, mock_http_handler, mock_http_async_handler):
        self.mock_provider_mapping = mock_provider_mapping
        self.mock_http = mock_http_handler
        self.mock_http_async = mock_http_async_handler
        self.model = "huggingface/together/meta-llama/Meta-Llama-3-8B-Instruct"
        litellm.set_verbose = False

    def get_base_completion_call_args(self) -> dict:
        """Implementation of abstract method from BaseLLMChatTest"""
        return {"model": self.model}

    def test_completion_non_streaming(self):
        messages = [{"role": "user", "content": "This is a dummy message"}]

        response = litellm.completion(model=self.model, messages=messages, stream=False)
        assert isinstance(response, ModelResponse)
        assert (
            response.choices[0].message.content
            == "This is a test response from the mocked HuggingFace API."
        )
        assert response.usage is not None
        assert response.model == self.model.split("/", 2)[2]

    def test_completion_streaming(self):
        messages = [{"role": "user", "content": "This is a dummy message"}]

        response = litellm.completion(model=self.model, messages=messages, stream=True)

        chunks = list(response)
        assert len(chunks) > 0

        assert self.mock_http.called
        call_args = self.mock_http.call_args
        assert call_args is not None

        kwargs = call_args[1]
        data = json.loads(kwargs["data"])
        assert data["stream"] is True
        assert data["messages"] == messages

        assert isinstance(chunks, list)
        assert isinstance(chunks[0], ModelResponseStream)
        assert isinstance(chunks[0].id, str)
        assert chunks[0].model == self.model.split("/", 1)[1]

    @pytest.mark.asyncio
    async def test_async_completion_streaming(self):
        """Test async streaming completion"""
        messages = [{"role": "user", "content": "This is a dummy message"}]
        response = await litellm.acompletion(
            model=self.model, messages=messages, stream=True
        )

        chunks = []
        async for chunk in response:
            chunks.append(chunk)

        assert self.mock_http_async.called
        assert len(chunks) > 0
        assert isinstance(chunks[0], ModelResponseStream)
        assert isinstance(chunks[0].id, str)
        assert chunks[0].model == self.model.split("/", 1)[1]

    @pytest.mark.asyncio
    async def test_async_completion_non_streaming(self):
        """Test async non-streaming completion"""
        messages = [{"role": "user", "content": "This is a dummy message"}]
        response = await litellm.acompletion(
            model=self.model, messages=messages, stream=False
        )

        assert self.mock_http_async.called
        assert isinstance(response, ModelResponse)
        assert (
            response.choices[0].message.content
            == "This is a test response from the mocked HuggingFace API."
        )
        assert response.usage is not None
        assert response.model == self.model.split("/", 2)[2]

    def test_tool_call_no_arguments(self, tool_call_no_arguments):
        mock_tool_response = {
            **MOCK_COMPLETION_RESPONSE,
            "choices": [
                {
                    "finish_reason": "tool_calls",
                    "index": 0,
                    "message": tool_call_no_arguments,
                }
            ],
        }

        with patch.object(
            self.mock_http,
            "side_effect",
            lambda *args, **kwargs: MagicMock(
                status_code=200,
                json=lambda: mock_tool_response,
                raise_for_status=lambda: None,
            ),
        ):
            messages = [{"role": "user", "content": "Get the FAQ"}]
            tools = [
                {
                    "type": "function",
                    "function": {
                        "name": "Get-FAQ",
                        "description": "Get FAQ information",
                        "parameters": {
                            "type": "object",
                            "properties": {},
                            "required": [],
                        },
                    },
                }
            ]

            response = litellm.completion(
                model=self.model, messages=messages, tools=tools, tool_choice="auto"
            )

            assert response.choices[0].message.tool_calls is not None
            assert len(response.choices[0].message.tool_calls) == 1
            assert (
                response.choices[0].message.tool_calls[0].function.name
                == tool_call_no_arguments["tool_calls"][0]["function"]["name"]
            )
            assert (
                response.choices[0].message.tool_calls[0].function.arguments
                == tool_call_no_arguments["tool_calls"][0]["function"]["arguments"]
            )

    @pytest.mark.parametrize(
        "model, provider, expected_url",
        [
            (
                "meta-llama/Llama-3-8B-Instruct",
                None,
                "https://router.huggingface.co/hf-inference/models/meta-llama/Llama-3-8B-Instruct/v1/chat/completions",
            ),
            (
                "together/meta-llama/Llama-3-8B-Instruct",
                None,
                "https://router.huggingface.co/together/v1/chat/completions",
            ),
            (
                "novita/meta-llama/Llama-3-8B-Instruct",
                None,
                "https://router.huggingface.co/novita/v3/openai/chat/completions",
            ),
            (
                "http://custom-endpoint.com/v1/chat/completions",
                None,
                "http://custom-endpoint.com/v1/chat/completions",
            ),
        ],
    )
    def test_get_complete_url(self, model, provider, expected_url):
        """Test that the complete URL is constructed correctly for different providers"""
        from litellm.llms.huggingface.chat.transformation import HuggingFaceChatConfig

        config = HuggingFaceChatConfig()
        url = config.get_complete_url(
            api_base=None,
            model=model,
            optional_params={},
            stream=False,
            api_key="test_api_key",
            litellm_params={},
        )
        assert url == expected_url

    @pytest.mark.parametrize(
        "api_base, model, expected_url",
        [
            (
                "https://abcd123.us-east-1.aws.endpoints.huggingface.cloud",
                "huggingface/tgi",
                "https://abcd123.us-east-1.aws.endpoints.huggingface.cloud/v1/chat/completions",
            ),
            (
                "https://abcd123.us-east-1.aws.endpoints.huggingface.cloud/",
                "huggingface/tgi",
                "https://abcd123.us-east-1.aws.endpoints.huggingface.cloud/v1/chat/completions",
            ),
            (
                "https://abcd123.us-east-1.aws.endpoints.huggingface.cloud/v1/chat/completions",
                "huggingface/tgi",
                "https://abcd123.us-east-1.aws.endpoints.huggingface.cloud/v1/chat/completions",
            ),
            (
                "https://example.com/custom/path",
                "huggingface/tgi",
                "https://example.com/custom/path/v1/chat/completions",
            ),
            (
                "https://example.com/custom/path/v1/chat/completions",
                "huggingface/tgi",
                "https://example.com/custom/path/v1/chat/completions",
            ),
            (
                "https://example.com/v1",
                "huggingface/tgi",
                "https://example.com/v1/chat/completions",
            ),
        ],
    )
    def test_get_complete_url_inference_endpoints(self, api_base, model, expected_url):
        from litellm.llms.huggingface.chat.transformation import HuggingFaceChatConfig

        config = HuggingFaceChatConfig()
        url = config.get_complete_url(
            api_base=api_base,
            model=model,
            optional_params={},
            stream=False,
            api_key="test_api_key",
            litellm_params={},
        )
        assert url == expected_url

    def test_completion_with_api_base(self):
        messages = [{"role": "user", "content": "This is a test message"}]
        api_base = "https://abcd123.us-east-1.aws.endpoints.huggingface.cloud"

        response = litellm.completion(
            model="huggingface/tgi", messages=messages, api_base=api_base, stream=False
        )

        assert isinstance(response, ModelResponse)
        assert (
            response.choices[0].message.content
            == "This is a test response from the mocked HuggingFace API."
        )

        assert self.mock_http.called
        call_args = self.mock_http.call_args
        assert call_args is not None

        called_url = call_args[1]["url"]
        assert called_url == f"{api_base}/v1/chat/completions"

    @pytest.mark.asyncio
    async def test_async_completion_with_api_base(self):
        messages = [{"role": "user", "content": "This is a test message"}]
        api_base = "https://abcd123.us-east-1.aws.endpoints.huggingface.cloud"

        response = await litellm.acompletion(
            model="huggingface/tgi", messages=messages, api_base=api_base, stream=False
        )

        assert isinstance(response, ModelResponse)
        assert (
            response.choices[0].message.content
            == "This is a test response from the mocked HuggingFace API."
        )

        assert self.mock_http_async.called
        call_args = self.mock_http_async.call_args
        assert call_args is not None

        called_url = call_args[1]["url"]
        assert called_url == f"{api_base}/v1/chat/completions"

    def test_completion_streaming_with_api_base(self):
        """Test streaming completion with api_base parameter"""
        messages = [{"role": "user", "content": "This is a test message"}]
        api_base = "https://abcd123.us-east-1.aws.endpoints.huggingface.cloud"

        response = litellm.completion(
            model="huggingface/tgi", messages=messages, api_base=api_base, stream=True
        )

        chunks = list(response)
        assert len(chunks) > 0
        assert isinstance(chunks[0], ModelResponseStream)

        # Check that the correct URL was called
        assert self.mock_http.called
        call_args = self.mock_http.call_args
        assert call_args is not None

        called_url = call_args[1]["url"]
        assert called_url == f"{api_base}/v1/chat/completions"

    def test_build_chat_completion_url_function(self):
        """Test the _build_chat_completion_url helper function"""
        from litellm.llms.huggingface.chat.transformation import (
            _build_chat_completion_url,
        )

        test_cases = [
            ("https://example.com", "https://example.com/v1/chat/completions"),
            ("https://example.com/", "https://example.com/v1/chat/completions"),
            ("https://example.com/v1", "https://example.com/v1/chat/completions"),
            ("https://example.com/v1/", "https://example.com/v1/chat/completions"),
            (
                "https://example.com/v1/chat/completions",
                "https://example.com/v1/chat/completions",
            ),
            (
                "https://example.com/custom/path",
                "https://example.com/custom/path/v1/chat/completions",
            ),
            (
                "https://example.com/custom/path/",
                "https://example.com/custom/path/v1/chat/completions",
            ),
        ]

        for input_url, expected_url in test_cases:
            result = _build_chat_completion_url(input_url)
            assert (
                result == expected_url
            ), f"Failed for input: {input_url}, expected: {expected_url}, got: {result}"

    def test_validate_environment(self):
        """Test that the environment is validated correctly"""
        from litellm.llms.huggingface.chat.transformation import HuggingFaceChatConfig

        config = HuggingFaceChatConfig()

        headers = config.validate_environment(
            headers={},
            model="huggingface/fireworks-ai/meta-llama/Meta-Llama-3-8B-Instruct",
            messages=[{"role": "user", "content": "Hello"}],
            optional_params={},
            api_key="test_api_key",
            litellm_params={},
        )

        assert headers["Authorization"] == "Bearer test_api_key"
        assert headers["content-type"] == "application/json"

    @pytest.mark.parametrize(
        "model, expected_model",
        [
            (
                "together/meta-llama/Llama-3-8B-Instruct",
                "meta-llama/Meta-Llama-3-8B-Instruct-Turbo",
            ),
            (
                "meta-llama/Meta-Llama-3-8B-Instruct",
                "meta-llama/Meta-Llama-3-8B-Instruct",
            ),
        ],
    )
    def test_transform_request(self, model, expected_model):
        from litellm.llms.huggingface.chat.transformation import HuggingFaceChatConfig

        config = HuggingFaceChatConfig()
        messages = [{"role": "user", "content": "Hello"}]

        transformed_request = config.transform_request(
            model=model,
            messages=messages,
            optional_params={},
            litellm_params={},
            headers={},
        )

        assert transformed_request["model"] == expected_model
        assert transformed_request["messages"] == messages

    @pytest.mark.asyncio
    async def test_completion_cost(self):
        pass