Model Runtime (#1858)
Co-authored-by: StyleZhang <jasonapring2015@outlook.com> Co-authored-by: Garfield Dai <dai.hai@foxmail.com> Co-authored-by: chenhe <guchenhe@gmail.com> Co-authored-by: jyong <jyong@dify.ai> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: Yeuoly <admin@srmxy.cn>
This commit is contained in:
119
api/tests/integration_tests/model_runtime/replicate/test_llm.py
Normal file
119
api/tests/integration_tests/model_runtime/replicate/test_llm.py
Normal file
@@ -0,0 +1,119 @@
|
||||
import os
|
||||
from typing import Generator
|
||||
|
||||
import pytest
|
||||
|
||||
from core.model_runtime.entities.message_entities import SystemPromptMessage, UserPromptMessage, AssistantPromptMessage
|
||||
from core.model_runtime.entities.llm_entities import LLMResult, LLMResultChunk, \
|
||||
LLMResultChunkDelta
|
||||
from core.model_runtime.errors.validate import CredentialsValidateFailedError
|
||||
from core.model_runtime.model_providers.replicate.llm.llm import ReplicateLargeLanguageModel
|
||||
|
||||
|
||||
def test_validate_credentials():
|
||||
model = ReplicateLargeLanguageModel()
|
||||
|
||||
with pytest.raises(CredentialsValidateFailedError):
|
||||
model.validate_credentials(
|
||||
model='meta/llama-2-13b-chat',
|
||||
credentials={
|
||||
'replicate_api_token': 'invalid_key',
|
||||
'model_version': 'f4e2de70d66816a838a89eeeb621910adffb0dd0baba3976c96980970978018d'
|
||||
}
|
||||
)
|
||||
|
||||
model.validate_credentials(
|
||||
model='meta/llama-2-13b-chat',
|
||||
credentials={
|
||||
'replicate_api_token': os.environ.get('REPLICATE_API_KEY'),
|
||||
'model_version': 'f4e2de70d66816a838a89eeeb621910adffb0dd0baba3976c96980970978018d'
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def test_invoke_model():
|
||||
model = ReplicateLargeLanguageModel()
|
||||
|
||||
response = model.invoke(
|
||||
model='meta/llama-2-13b-chat',
|
||||
credentials={
|
||||
'replicate_api_token': os.environ.get('REPLICATE_API_KEY'),
|
||||
'model_version': 'f4e2de70d66816a838a89eeeb621910adffb0dd0baba3976c96980970978018d'
|
||||
},
|
||||
prompt_messages=[
|
||||
SystemPromptMessage(
|
||||
content='You are a helpful AI assistant.',
|
||||
),
|
||||
UserPromptMessage(
|
||||
content='Who are you?'
|
||||
)
|
||||
],
|
||||
model_parameters={
|
||||
'temperature': 1.0,
|
||||
'top_k': 2,
|
||||
'top_p': 0.5,
|
||||
},
|
||||
stop=['How'],
|
||||
stream=False,
|
||||
user="abc-123"
|
||||
)
|
||||
|
||||
assert isinstance(response, LLMResult)
|
||||
assert len(response.message.content) > 0
|
||||
|
||||
|
||||
def test_invoke_stream_model():
|
||||
model = ReplicateLargeLanguageModel()
|
||||
|
||||
response = model.invoke(
|
||||
model='mistralai/mixtral-8x7b-instruct-v0.1',
|
||||
credentials={
|
||||
'replicate_api_token': os.environ.get('REPLICATE_API_KEY'),
|
||||
'model_version': '2b56576fcfbe32fa0526897d8385dd3fb3d36ba6fd0dbe033c72886b81ade93e'
|
||||
},
|
||||
prompt_messages=[
|
||||
SystemPromptMessage(
|
||||
content='You are a helpful AI assistant.',
|
||||
),
|
||||
UserPromptMessage(
|
||||
content='Who are you?'
|
||||
)
|
||||
],
|
||||
model_parameters={
|
||||
'temperature': 1.0,
|
||||
'top_k': 2,
|
||||
'top_p': 0.5,
|
||||
},
|
||||
stop=['How'],
|
||||
stream=True,
|
||||
user="abc-123"
|
||||
)
|
||||
|
||||
assert isinstance(response, Generator)
|
||||
|
||||
for chunk in response:
|
||||
assert isinstance(chunk, LLMResultChunk)
|
||||
assert isinstance(chunk.delta, LLMResultChunkDelta)
|
||||
assert isinstance(chunk.delta.message, AssistantPromptMessage)
|
||||
|
||||
|
||||
def test_get_num_tokens():
|
||||
model = ReplicateLargeLanguageModel()
|
||||
|
||||
num_tokens = model.get_num_tokens(
|
||||
model='',
|
||||
credentials={
|
||||
'replicate_api_token': os.environ.get('REPLICATE_API_KEY'),
|
||||
'model_version': '2b56576fcfbe32fa0526897d8385dd3fb3d36ba6fd0dbe033c72886b81ade93e'
|
||||
},
|
||||
prompt_messages=[
|
||||
SystemPromptMessage(
|
||||
content='You are a helpful AI assistant.',
|
||||
),
|
||||
UserPromptMessage(
|
||||
content='Hello World!'
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
assert num_tokens == 14
|
@@ -0,0 +1,151 @@
|
||||
import os
|
||||
|
||||
import pytest
|
||||
|
||||
from core.model_runtime.entities.text_embedding_entities import TextEmbeddingResult
|
||||
from core.model_runtime.errors.validate import CredentialsValidateFailedError
|
||||
from core.model_runtime.model_providers.replicate.text_embedding.text_embedding import ReplicateEmbeddingModel
|
||||
|
||||
|
||||
def test_validate_credentials_one():
|
||||
model = ReplicateEmbeddingModel()
|
||||
|
||||
with pytest.raises(CredentialsValidateFailedError):
|
||||
model.validate_credentials(
|
||||
model='replicate/all-mpnet-base-v2',
|
||||
credentials={
|
||||
'replicate_api_token': 'invalid_key',
|
||||
'model_version': 'b6b7585c9640cd7a9572c6e129c9549d79c9c31f0d3fdce7baac7c67ca38f305'
|
||||
}
|
||||
)
|
||||
|
||||
model.validate_credentials(
|
||||
model='replicate/all-mpnet-base-v2',
|
||||
credentials={
|
||||
'replicate_api_token': os.environ.get('REPLICATE_API_KEY'),
|
||||
'model_version': 'b6b7585c9640cd7a9572c6e129c9549d79c9c31f0d3fdce7baac7c67ca38f305'
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def test_validate_credentials_two():
|
||||
model = ReplicateEmbeddingModel()
|
||||
|
||||
with pytest.raises(CredentialsValidateFailedError):
|
||||
model.validate_credentials(
|
||||
model='nateraw/bge-large-en-v1.5',
|
||||
credentials={
|
||||
'replicate_api_token': 'invalid_key',
|
||||
'model_version': '9cf9f015a9cb9c61d1a2610659cdac4a4ca222f2d3707a68517b18c198a9add1'
|
||||
}
|
||||
)
|
||||
|
||||
model.validate_credentials(
|
||||
model='nateraw/bge-large-en-v1.5',
|
||||
credentials={
|
||||
'replicate_api_token': os.environ.get('REPLICATE_API_KEY'),
|
||||
'model_version': '9cf9f015a9cb9c61d1a2610659cdac4a4ca222f2d3707a68517b18c198a9add1'
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def test_invoke_model_one():
|
||||
model = ReplicateEmbeddingModel()
|
||||
|
||||
result = model.invoke(
|
||||
model='nateraw/bge-large-en-v1.5',
|
||||
credentials={
|
||||
'replicate_api_token': os.environ.get('REPLICATE_API_KEY'),
|
||||
'model_version': '9cf9f015a9cb9c61d1a2610659cdac4a4ca222f2d3707a68517b18c198a9add1'
|
||||
},
|
||||
texts=[
|
||||
"hello",
|
||||
"world"
|
||||
],
|
||||
user="abc-123"
|
||||
)
|
||||
|
||||
assert isinstance(result, TextEmbeddingResult)
|
||||
assert len(result.embeddings) == 2
|
||||
assert result.usage.total_tokens == 2
|
||||
|
||||
|
||||
def test_invoke_model_two():
|
||||
model = ReplicateEmbeddingModel()
|
||||
|
||||
result = model.invoke(
|
||||
model='andreasjansson/clip-features',
|
||||
credentials={
|
||||
'replicate_api_token': os.environ.get('REPLICATE_API_KEY'),
|
||||
'model_version': '75b33f253f7714a281ad3e9b28f63e3232d583716ef6718f2e46641077ea040a'
|
||||
},
|
||||
texts=[
|
||||
"hello",
|
||||
"world"
|
||||
],
|
||||
user="abc-123"
|
||||
)
|
||||
|
||||
assert isinstance(result, TextEmbeddingResult)
|
||||
assert len(result.embeddings) == 2
|
||||
assert result.usage.total_tokens == 2
|
||||
|
||||
|
||||
def test_invoke_model_three():
|
||||
model = ReplicateEmbeddingModel()
|
||||
|
||||
result = model.invoke(
|
||||
model='replicate/all-mpnet-base-v2',
|
||||
credentials={
|
||||
'replicate_api_token': os.environ.get('REPLICATE_API_KEY'),
|
||||
'model_version': 'b6b7585c9640cd7a9572c6e129c9549d79c9c31f0d3fdce7baac7c67ca38f305'
|
||||
},
|
||||
texts=[
|
||||
"hello",
|
||||
"world"
|
||||
],
|
||||
user="abc-123"
|
||||
)
|
||||
|
||||
assert isinstance(result, TextEmbeddingResult)
|
||||
assert len(result.embeddings) == 2
|
||||
assert result.usage.total_tokens == 2
|
||||
|
||||
|
||||
def test_invoke_model_four():
|
||||
model = ReplicateEmbeddingModel()
|
||||
|
||||
result = model.invoke(
|
||||
model='nateraw/jina-embeddings-v2-base-en',
|
||||
credentials={
|
||||
'replicate_api_token': os.environ.get('REPLICATE_API_KEY'),
|
||||
'model_version': 'f8367a1c072ba2bc28af549d1faeacfe9b88b3f0e475add7a75091dac507f79e'
|
||||
},
|
||||
texts=[
|
||||
"hello",
|
||||
"world"
|
||||
],
|
||||
user="abc-123"
|
||||
)
|
||||
|
||||
assert isinstance(result, TextEmbeddingResult)
|
||||
assert len(result.embeddings) == 2
|
||||
assert result.usage.total_tokens == 2
|
||||
|
||||
|
||||
def test_get_num_tokens():
|
||||
model = ReplicateEmbeddingModel()
|
||||
|
||||
num_tokens = model.get_num_tokens(
|
||||
model='nateraw/jina-embeddings-v2-base-en',
|
||||
credentials={
|
||||
'replicate_api_token': os.environ.get('REPLICATE_API_KEY'),
|
||||
'model_version': 'f8367a1c072ba2bc28af549d1faeacfe9b88b3f0e475add7a75091dac507f79e'
|
||||
},
|
||||
texts=[
|
||||
"hello",
|
||||
"world"
|
||||
]
|
||||
)
|
||||
|
||||
assert num_tokens == 2
|
Reference in New Issue
Block a user