chore(api/tests): apply ruff reformat #7590 (#7591)

Co-authored-by: -LAN- <laipz8200@outlook.com>
This commit is contained in:
Bowen Liang
2024-08-23 23:52:25 +08:00
committed by GitHub
parent 2da63654e5
commit b035c02f78
155 changed files with 4279 additions and 5925 deletions

View File

@@ -10,87 +10,75 @@ from core.model_runtime.model_providers.huggingface_hub.llm.llm import Huggingfa
from tests.integration_tests.model_runtime.__mock.huggingface import setup_huggingface_mock
@pytest.mark.parametrize('setup_huggingface_mock', [['none']], indirect=True)
@pytest.mark.parametrize("setup_huggingface_mock", [["none"]], indirect=True)
def test_hosted_inference_api_validate_credentials(setup_huggingface_mock):
model = HuggingfaceHubLargeLanguageModel()
with pytest.raises(CredentialsValidateFailedError):
model.validate_credentials(
model='HuggingFaceH4/zephyr-7b-beta',
credentials={
'huggingfacehub_api_type': 'hosted_inference_api',
'huggingfacehub_api_token': 'invalid_key'
}
model="HuggingFaceH4/zephyr-7b-beta",
credentials={"huggingfacehub_api_type": "hosted_inference_api", "huggingfacehub_api_token": "invalid_key"},
)
with pytest.raises(CredentialsValidateFailedError):
model.validate_credentials(
model='fake-model',
credentials={
'huggingfacehub_api_type': 'hosted_inference_api',
'huggingfacehub_api_token': 'invalid_key'
}
model="fake-model",
credentials={"huggingfacehub_api_type": "hosted_inference_api", "huggingfacehub_api_token": "invalid_key"},
)
model.validate_credentials(
model='HuggingFaceH4/zephyr-7b-beta',
model="HuggingFaceH4/zephyr-7b-beta",
credentials={
'huggingfacehub_api_type': 'hosted_inference_api',
'huggingfacehub_api_token': os.environ.get('HUGGINGFACE_API_KEY')
}
"huggingfacehub_api_type": "hosted_inference_api",
"huggingfacehub_api_token": os.environ.get("HUGGINGFACE_API_KEY"),
},
)
@pytest.mark.parametrize('setup_huggingface_mock', [['none']], indirect=True)
@pytest.mark.parametrize("setup_huggingface_mock", [["none"]], indirect=True)
def test_hosted_inference_api_invoke_model(setup_huggingface_mock):
model = HuggingfaceHubLargeLanguageModel()
response = model.invoke(
model='HuggingFaceH4/zephyr-7b-beta',
model="HuggingFaceH4/zephyr-7b-beta",
credentials={
'huggingfacehub_api_type': 'hosted_inference_api',
'huggingfacehub_api_token': os.environ.get('HUGGINGFACE_API_KEY')
"huggingfacehub_api_type": "hosted_inference_api",
"huggingfacehub_api_token": os.environ.get("HUGGINGFACE_API_KEY"),
},
prompt_messages=[
UserPromptMessage(
content='Who are you?'
)
],
prompt_messages=[UserPromptMessage(content="Who are you?")],
model_parameters={
'temperature': 1.0,
'top_k': 2,
'top_p': 0.5,
"temperature": 1.0,
"top_k": 2,
"top_p": 0.5,
},
stop=['How'],
stop=["How"],
stream=False,
user="abc-123"
user="abc-123",
)
assert isinstance(response, LLMResult)
assert len(response.message.content) > 0
@pytest.mark.parametrize('setup_huggingface_mock', [['none']], indirect=True)
@pytest.mark.parametrize("setup_huggingface_mock", [["none"]], indirect=True)
def test_hosted_inference_api_invoke_stream_model(setup_huggingface_mock):
model = HuggingfaceHubLargeLanguageModel()
response = model.invoke(
model='HuggingFaceH4/zephyr-7b-beta',
model="HuggingFaceH4/zephyr-7b-beta",
credentials={
'huggingfacehub_api_type': 'hosted_inference_api',
'huggingfacehub_api_token': os.environ.get('HUGGINGFACE_API_KEY')
"huggingfacehub_api_type": "hosted_inference_api",
"huggingfacehub_api_token": os.environ.get("HUGGINGFACE_API_KEY"),
},
prompt_messages=[
UserPromptMessage(
content='Who are you?'
)
],
prompt_messages=[UserPromptMessage(content="Who are you?")],
model_parameters={
'temperature': 1.0,
'top_k': 2,
'top_p': 0.5,
"temperature": 1.0,
"top_k": 2,
"top_p": 0.5,
},
stop=['How'],
stop=["How"],
stream=True,
user="abc-123"
user="abc-123",
)
assert isinstance(response, Generator)
@@ -101,86 +89,81 @@ def test_hosted_inference_api_invoke_stream_model(setup_huggingface_mock):
assert isinstance(chunk.delta.message, AssistantPromptMessage)
assert len(chunk.delta.message.content) > 0 if chunk.delta.finish_reason is None else True
@pytest.mark.parametrize('setup_huggingface_mock', [['none']], indirect=True)
@pytest.mark.parametrize("setup_huggingface_mock", [["none"]], indirect=True)
def test_inference_endpoints_text_generation_validate_credentials(setup_huggingface_mock):
model = HuggingfaceHubLargeLanguageModel()
with pytest.raises(CredentialsValidateFailedError):
model.validate_credentials(
model='openchat/openchat_3.5',
model="openchat/openchat_3.5",
credentials={
'huggingfacehub_api_type': 'inference_endpoints',
'huggingfacehub_api_token': 'invalid_key',
'huggingfacehub_endpoint_url': os.environ.get('HUGGINGFACE_TEXT_GEN_ENDPOINT_URL'),
'task_type': 'text-generation'
}
"huggingfacehub_api_type": "inference_endpoints",
"huggingfacehub_api_token": "invalid_key",
"huggingfacehub_endpoint_url": os.environ.get("HUGGINGFACE_TEXT_GEN_ENDPOINT_URL"),
"task_type": "text-generation",
},
)
model.validate_credentials(
model='openchat/openchat_3.5',
model="openchat/openchat_3.5",
credentials={
'huggingfacehub_api_type': 'inference_endpoints',
'huggingfacehub_api_token': os.environ.get('HUGGINGFACE_API_KEY'),
'huggingfacehub_endpoint_url': os.environ.get('HUGGINGFACE_TEXT_GEN_ENDPOINT_URL'),
'task_type': 'text-generation'
}
"huggingfacehub_api_type": "inference_endpoints",
"huggingfacehub_api_token": os.environ.get("HUGGINGFACE_API_KEY"),
"huggingfacehub_endpoint_url": os.environ.get("HUGGINGFACE_TEXT_GEN_ENDPOINT_URL"),
"task_type": "text-generation",
},
)
@pytest.mark.parametrize('setup_huggingface_mock', [['none']], indirect=True)
@pytest.mark.parametrize("setup_huggingface_mock", [["none"]], indirect=True)
def test_inference_endpoints_text_generation_invoke_model(setup_huggingface_mock):
model = HuggingfaceHubLargeLanguageModel()
response = model.invoke(
model='openchat/openchat_3.5',
model="openchat/openchat_3.5",
credentials={
'huggingfacehub_api_type': 'inference_endpoints',
'huggingfacehub_api_token': os.environ.get('HUGGINGFACE_API_KEY'),
'huggingfacehub_endpoint_url': os.environ.get('HUGGINGFACE_TEXT_GEN_ENDPOINT_URL'),
'task_type': 'text-generation'
"huggingfacehub_api_type": "inference_endpoints",
"huggingfacehub_api_token": os.environ.get("HUGGINGFACE_API_KEY"),
"huggingfacehub_endpoint_url": os.environ.get("HUGGINGFACE_TEXT_GEN_ENDPOINT_URL"),
"task_type": "text-generation",
},
prompt_messages=[
UserPromptMessage(
content='Who are you?'
)
],
prompt_messages=[UserPromptMessage(content="Who are you?")],
model_parameters={
'temperature': 1.0,
'top_k': 2,
'top_p': 0.5,
"temperature": 1.0,
"top_k": 2,
"top_p": 0.5,
},
stop=['How'],
stop=["How"],
stream=False,
user="abc-123"
user="abc-123",
)
assert isinstance(response, LLMResult)
assert len(response.message.content) > 0
@pytest.mark.parametrize('setup_huggingface_mock', [['none']], indirect=True)
@pytest.mark.parametrize("setup_huggingface_mock", [["none"]], indirect=True)
def test_inference_endpoints_text_generation_invoke_stream_model(setup_huggingface_mock):
model = HuggingfaceHubLargeLanguageModel()
response = model.invoke(
model='openchat/openchat_3.5',
model="openchat/openchat_3.5",
credentials={
'huggingfacehub_api_type': 'inference_endpoints',
'huggingfacehub_api_token': os.environ.get('HUGGINGFACE_API_KEY'),
'huggingfacehub_endpoint_url': os.environ.get('HUGGINGFACE_TEXT_GEN_ENDPOINT_URL'),
'task_type': 'text-generation'
"huggingfacehub_api_type": "inference_endpoints",
"huggingfacehub_api_token": os.environ.get("HUGGINGFACE_API_KEY"),
"huggingfacehub_endpoint_url": os.environ.get("HUGGINGFACE_TEXT_GEN_ENDPOINT_URL"),
"task_type": "text-generation",
},
prompt_messages=[
UserPromptMessage(
content='Who are you?'
)
],
prompt_messages=[UserPromptMessage(content="Who are you?")],
model_parameters={
'temperature': 1.0,
'top_k': 2,
'top_p': 0.5,
"temperature": 1.0,
"top_k": 2,
"top_p": 0.5,
},
stop=['How'],
stop=["How"],
stream=True,
user="abc-123"
user="abc-123",
)
assert isinstance(response, Generator)
@@ -191,86 +174,81 @@ def test_inference_endpoints_text_generation_invoke_stream_model(setup_huggingfa
assert isinstance(chunk.delta.message, AssistantPromptMessage)
assert len(chunk.delta.message.content) > 0 if chunk.delta.finish_reason is None else True
@pytest.mark.parametrize('setup_huggingface_mock', [['none']], indirect=True)
@pytest.mark.parametrize("setup_huggingface_mock", [["none"]], indirect=True)
def test_inference_endpoints_text2text_generation_validate_credentials(setup_huggingface_mock):
model = HuggingfaceHubLargeLanguageModel()
with pytest.raises(CredentialsValidateFailedError):
model.validate_credentials(
model='google/mt5-base',
model="google/mt5-base",
credentials={
'huggingfacehub_api_type': 'inference_endpoints',
'huggingfacehub_api_token': 'invalid_key',
'huggingfacehub_endpoint_url': os.environ.get('HUGGINGFACE_TEXT2TEXT_GEN_ENDPOINT_URL'),
'task_type': 'text2text-generation'
}
"huggingfacehub_api_type": "inference_endpoints",
"huggingfacehub_api_token": "invalid_key",
"huggingfacehub_endpoint_url": os.environ.get("HUGGINGFACE_TEXT2TEXT_GEN_ENDPOINT_URL"),
"task_type": "text2text-generation",
},
)
model.validate_credentials(
model='google/mt5-base',
model="google/mt5-base",
credentials={
'huggingfacehub_api_type': 'inference_endpoints',
'huggingfacehub_api_token': os.environ.get('HUGGINGFACE_API_KEY'),
'huggingfacehub_endpoint_url': os.environ.get('HUGGINGFACE_TEXT2TEXT_GEN_ENDPOINT_URL'),
'task_type': 'text2text-generation'
}
"huggingfacehub_api_type": "inference_endpoints",
"huggingfacehub_api_token": os.environ.get("HUGGINGFACE_API_KEY"),
"huggingfacehub_endpoint_url": os.environ.get("HUGGINGFACE_TEXT2TEXT_GEN_ENDPOINT_URL"),
"task_type": "text2text-generation",
},
)
@pytest.mark.parametrize('setup_huggingface_mock', [['none']], indirect=True)
@pytest.mark.parametrize("setup_huggingface_mock", [["none"]], indirect=True)
def test_inference_endpoints_text2text_generation_invoke_model(setup_huggingface_mock):
model = HuggingfaceHubLargeLanguageModel()
response = model.invoke(
model='google/mt5-base',
model="google/mt5-base",
credentials={
'huggingfacehub_api_type': 'inference_endpoints',
'huggingfacehub_api_token': os.environ.get('HUGGINGFACE_API_KEY'),
'huggingfacehub_endpoint_url': os.environ.get('HUGGINGFACE_TEXT2TEXT_GEN_ENDPOINT_URL'),
'task_type': 'text2text-generation'
"huggingfacehub_api_type": "inference_endpoints",
"huggingfacehub_api_token": os.environ.get("HUGGINGFACE_API_KEY"),
"huggingfacehub_endpoint_url": os.environ.get("HUGGINGFACE_TEXT2TEXT_GEN_ENDPOINT_URL"),
"task_type": "text2text-generation",
},
prompt_messages=[
UserPromptMessage(
content='Who are you?'
)
],
prompt_messages=[UserPromptMessage(content="Who are you?")],
model_parameters={
'temperature': 1.0,
'top_k': 2,
'top_p': 0.5,
"temperature": 1.0,
"top_k": 2,
"top_p": 0.5,
},
stop=['How'],
stop=["How"],
stream=False,
user="abc-123"
user="abc-123",
)
assert isinstance(response, LLMResult)
assert len(response.message.content) > 0
@pytest.mark.parametrize('setup_huggingface_mock', [['none']], indirect=True)
@pytest.mark.parametrize("setup_huggingface_mock", [["none"]], indirect=True)
def test_inference_endpoints_text2text_generation_invoke_stream_model(setup_huggingface_mock):
model = HuggingfaceHubLargeLanguageModel()
response = model.invoke(
model='google/mt5-base',
model="google/mt5-base",
credentials={
'huggingfacehub_api_type': 'inference_endpoints',
'huggingfacehub_api_token': os.environ.get('HUGGINGFACE_API_KEY'),
'huggingfacehub_endpoint_url': os.environ.get('HUGGINGFACE_TEXT2TEXT_GEN_ENDPOINT_URL'),
'task_type': 'text2text-generation'
"huggingfacehub_api_type": "inference_endpoints",
"huggingfacehub_api_token": os.environ.get("HUGGINGFACE_API_KEY"),
"huggingfacehub_endpoint_url": os.environ.get("HUGGINGFACE_TEXT2TEXT_GEN_ENDPOINT_URL"),
"task_type": "text2text-generation",
},
prompt_messages=[
UserPromptMessage(
content='Who are you?'
)
],
prompt_messages=[UserPromptMessage(content="Who are you?")],
model_parameters={
'temperature': 1.0,
'top_k': 2,
'top_p': 0.5,
"temperature": 1.0,
"top_k": 2,
"top_p": 0.5,
},
stop=['How'],
stop=["How"],
stream=True,
user="abc-123"
user="abc-123",
)
assert isinstance(response, Generator)
@@ -286,18 +264,14 @@ def test_get_num_tokens():
model = HuggingfaceHubLargeLanguageModel()
num_tokens = model.get_num_tokens(
model='google/mt5-base',
model="google/mt5-base",
credentials={
'huggingfacehub_api_type': 'inference_endpoints',
'huggingfacehub_api_token': os.environ.get('HUGGINGFACE_API_KEY'),
'huggingfacehub_endpoint_url': os.environ.get('HUGGINGFACE_TEXT2TEXT_GEN_ENDPOINT_URL'),
'task_type': 'text2text-generation'
"huggingfacehub_api_type": "inference_endpoints",
"huggingfacehub_api_token": os.environ.get("HUGGINGFACE_API_KEY"),
"huggingfacehub_endpoint_url": os.environ.get("HUGGINGFACE_TEXT2TEXT_GEN_ENDPOINT_URL"),
"task_type": "text2text-generation",
},
prompt_messages=[
UserPromptMessage(
content='Hello World!'
)
]
prompt_messages=[UserPromptMessage(content="Hello World!")],
)
assert num_tokens == 7

View File

@@ -14,19 +14,19 @@ def test_hosted_inference_api_validate_credentials():
with pytest.raises(CredentialsValidateFailedError):
model.validate_credentials(
model='facebook/bart-base',
model="facebook/bart-base",
credentials={
'huggingfacehub_api_type': 'hosted_inference_api',
'huggingfacehub_api_token': 'invalid_key',
}
"huggingfacehub_api_type": "hosted_inference_api",
"huggingfacehub_api_token": "invalid_key",
},
)
model.validate_credentials(
model='facebook/bart-base',
model="facebook/bart-base",
credentials={
'huggingfacehub_api_type': 'hosted_inference_api',
'huggingfacehub_api_token': os.environ.get('HUGGINGFACE_API_KEY'),
}
"huggingfacehub_api_type": "hosted_inference_api",
"huggingfacehub_api_token": os.environ.get("HUGGINGFACE_API_KEY"),
},
)
@@ -34,15 +34,12 @@ def test_hosted_inference_api_invoke_model():
model = HuggingfaceHubTextEmbeddingModel()
result = model.invoke(
model='facebook/bart-base',
model="facebook/bart-base",
credentials={
'huggingfacehub_api_type': 'hosted_inference_api',
'huggingfacehub_api_token': os.environ.get('HUGGINGFACE_API_KEY'),
"huggingfacehub_api_type": "hosted_inference_api",
"huggingfacehub_api_token": os.environ.get("HUGGINGFACE_API_KEY"),
},
texts=[
"hello",
"world"
]
texts=["hello", "world"],
)
assert isinstance(result, TextEmbeddingResult)
@@ -55,25 +52,25 @@ def test_inference_endpoints_validate_credentials():
with pytest.raises(CredentialsValidateFailedError):
model.validate_credentials(
model='all-MiniLM-L6-v2',
model="all-MiniLM-L6-v2",
credentials={
'huggingfacehub_api_type': 'inference_endpoints',
'huggingfacehub_api_token': 'invalid_key',
'huggingface_namespace': 'Dify-AI',
'huggingfacehub_endpoint_url': os.environ.get('HUGGINGFACE_EMBEDDINGS_ENDPOINT_URL'),
'task_type': 'feature-extraction'
}
"huggingfacehub_api_type": "inference_endpoints",
"huggingfacehub_api_token": "invalid_key",
"huggingface_namespace": "Dify-AI",
"huggingfacehub_endpoint_url": os.environ.get("HUGGINGFACE_EMBEDDINGS_ENDPOINT_URL"),
"task_type": "feature-extraction",
},
)
model.validate_credentials(
model='all-MiniLM-L6-v2',
model="all-MiniLM-L6-v2",
credentials={
'huggingfacehub_api_type': 'inference_endpoints',
'huggingfacehub_api_token': os.environ.get('HUGGINGFACE_API_KEY'),
'huggingface_namespace': 'Dify-AI',
'huggingfacehub_endpoint_url': os.environ.get('HUGGINGFACE_EMBEDDINGS_ENDPOINT_URL'),
'task_type': 'feature-extraction'
}
"huggingfacehub_api_type": "inference_endpoints",
"huggingfacehub_api_token": os.environ.get("HUGGINGFACE_API_KEY"),
"huggingface_namespace": "Dify-AI",
"huggingfacehub_endpoint_url": os.environ.get("HUGGINGFACE_EMBEDDINGS_ENDPOINT_URL"),
"task_type": "feature-extraction",
},
)
@@ -81,18 +78,15 @@ def test_inference_endpoints_invoke_model():
model = HuggingfaceHubTextEmbeddingModel()
result = model.invoke(
model='all-MiniLM-L6-v2',
model="all-MiniLM-L6-v2",
credentials={
'huggingfacehub_api_type': 'inference_endpoints',
'huggingfacehub_api_token': os.environ.get('HUGGINGFACE_API_KEY'),
'huggingface_namespace': 'Dify-AI',
'huggingfacehub_endpoint_url': os.environ.get('HUGGINGFACE_EMBEDDINGS_ENDPOINT_URL'),
'task_type': 'feature-extraction'
"huggingfacehub_api_type": "inference_endpoints",
"huggingfacehub_api_token": os.environ.get("HUGGINGFACE_API_KEY"),
"huggingface_namespace": "Dify-AI",
"huggingfacehub_endpoint_url": os.environ.get("HUGGINGFACE_EMBEDDINGS_ENDPOINT_URL"),
"task_type": "feature-extraction",
},
texts=[
"hello",
"world"
]
texts=["hello", "world"],
)
assert isinstance(result, TextEmbeddingResult)
@@ -104,18 +98,15 @@ def test_get_num_tokens():
model = HuggingfaceHubTextEmbeddingModel()
num_tokens = model.get_num_tokens(
model='all-MiniLM-L6-v2',
model="all-MiniLM-L6-v2",
credentials={
'huggingfacehub_api_type': 'inference_endpoints',
'huggingfacehub_api_token': os.environ.get('HUGGINGFACE_API_KEY'),
'huggingface_namespace': 'Dify-AI',
'huggingfacehub_endpoint_url': os.environ.get('HUGGINGFACE_EMBEDDINGS_ENDPOINT_URL'),
'task_type': 'feature-extraction'
"huggingfacehub_api_type": "inference_endpoints",
"huggingfacehub_api_token": os.environ.get("HUGGINGFACE_API_KEY"),
"huggingface_namespace": "Dify-AI",
"huggingfacehub_endpoint_url": os.environ.get("HUGGINGFACE_EMBEDDINGS_ENDPOINT_URL"),
"task_type": "feature-extraction",
},
texts=[
"hello",
"world"
]
texts=["hello", "world"],
)
assert num_tokens == 2