Team Ai
Apppublic

Underground-Digital/Workflow-Engine

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
test_llm.py173 linesDownload Raw Back to baichuan
1import os2from collections.abc import Generator3from time import sleep4 5import pytest6 7from core.model_runtime.entities.llm_entities import LLMResult, LLMResultChunk, LLMResultChunkDelta8from core.model_runtime.entities.message_entities import AssistantPromptMessage, SystemPromptMessage, UserPromptMessage9from core.model_runtime.entities.model_entities import AIModelEntity10from core.model_runtime.errors.validate import CredentialsValidateFailedError11from core.model_runtime.model_providers.baichuan.llm.llm import BaichuanLanguageModel12 13 14def test_predefined_models():15    model = BaichuanLanguageModel()16    model_schemas = model.predefined_models()17    assert len(model_schemas) >= 118    assert isinstance(model_schemas[0], AIModelEntity)19 20 21def test_validate_credentials_for_chat_model():22    sleep(3)23    model = BaichuanLanguageModel()24 25    with pytest.raises(CredentialsValidateFailedError):26        model.validate_credentials(27            model="baichuan2-turbo", credentials={"api_key": "invalid_key", "secret_key": "invalid_key"}28        )29 30    model.validate_credentials(31        model="baichuan2-turbo",32        credentials={33            "api_key": os.environ.get("BAICHUAN_API_KEY"),34            "secret_key": os.environ.get("BAICHUAN_SECRET_KEY"),35        },36    )37 38 39def test_invoke_model():40    sleep(3)41    model = BaichuanLanguageModel()42 43    response = model.invoke(44        model="baichuan2-turbo",45        credentials={46            "api_key": os.environ.get("BAICHUAN_API_KEY"),47            "secret_key": os.environ.get("BAICHUAN_SECRET_KEY"),48        },49        prompt_messages=[UserPromptMessage(content="Hello World!")],50        model_parameters={51            "temperature": 0.7,52            "top_p": 1.0,53            "top_k": 1,54        },55        stop=["you"],56        user="abc-123",57        stream=False,58    )59 60    assert isinstance(response, LLMResult)61    assert len(response.message.content) > 062    assert response.usage.total_tokens > 063 64 65def test_invoke_model_with_system_message():66    sleep(3)67    model = BaichuanLanguageModel()68 69    response = model.invoke(70        model="baichuan2-turbo",71        credentials={72            "api_key": os.environ.get("BAICHUAN_API_KEY"),73            "secret_key": os.environ.get("BAICHUAN_SECRET_KEY"),74        },75        prompt_messages=[76            SystemPromptMessage(content="请记住你是Kasumi。"),77            UserPromptMessage(content="现在告诉我你是谁?"),78        ],79        model_parameters={80            "temperature": 0.7,81            "top_p": 1.0,82            "top_k": 1,83        },84        stop=["you"],85        user="abc-123",86        stream=False,87    )88 89    assert isinstance(response, LLMResult)90    assert len(response.message.content) > 091    assert response.usage.total_tokens > 092 93 94def test_invoke_stream_model():95    sleep(3)96    model = BaichuanLanguageModel()97 98    response = model.invoke(99        model="baichuan2-turbo",100        credentials={101            "api_key": os.environ.get("BAICHUAN_API_KEY"),102            "secret_key": os.environ.get("BAICHUAN_SECRET_KEY"),103        },104        prompt_messages=[UserPromptMessage(content="Hello World!")],105        model_parameters={106            "temperature": 0.7,107            "top_p": 1.0,108            "top_k": 1,109        },110        stop=["you"],111        stream=True,112        user="abc-123",113    )114 115    assert isinstance(response, Generator)116    for chunk in response:117        assert isinstance(chunk, LLMResultChunk)118        assert isinstance(chunk.delta, LLMResultChunkDelta)119        assert isinstance(chunk.delta.message, AssistantPromptMessage)120        assert len(chunk.delta.message.content) > 0 if chunk.delta.finish_reason is None else True121 122 123def test_invoke_with_search():124    sleep(3)125    model = BaichuanLanguageModel()126 127    response = model.invoke(128        model="baichuan2-turbo",129        credentials={130            "api_key": os.environ.get("BAICHUAN_API_KEY"),131            "secret_key": os.environ.get("BAICHUAN_SECRET_KEY"),132        },133        prompt_messages=[UserPromptMessage(content="北京今天的天气怎么样")],134        model_parameters={135            "temperature": 0.7,136            "top_p": 1.0,137            "top_k": 1,138            "with_search_enhance": True,139        },140        stop=["you"],141        stream=True,142        user="abc-123",143    )144 145    assert isinstance(response, Generator)146    total_message = ""147    for chunk in response:148        assert isinstance(chunk, LLMResultChunk)149        assert isinstance(chunk.delta, LLMResultChunkDelta)150        assert isinstance(chunk.delta.message, AssistantPromptMessage)151        assert len(chunk.delta.message.content) > 0 if not chunk.delta.finish_reason else True152        total_message += chunk.delta.message.content153 154    assert "不" not in total_message155 156 157def test_get_num_tokens():158    sleep(3)159    model = BaichuanLanguageModel()160 161    response = model.get_num_tokens(162        model="baichuan2-turbo",163        credentials={164            "api_key": os.environ.get("BAICHUAN_API_KEY"),165            "secret_key": os.environ.get("BAICHUAN_SECRET_KEY"),166        },167        prompt_messages=[UserPromptMessage(content="Hello World!")],168        tools=[],169    )170 171    assert isinstance(response, int)172    assert response == 9173