test_llm.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import os
  2. from typing import Generator
  3. import pytest
  4. from core.model_runtime.entities.llm_entities import LLMResult, LLMResultChunk, LLMResultChunkDelta
  5. from core.model_runtime.entities.message_entities import (AssistantPromptMessage, SystemPromptMessage,
  6. UserPromptMessage, PromptMessageTool)
  7. from core.model_runtime.errors.validate import CredentialsValidateFailedError
  8. from core.model_runtime.model_providers.zhipuai.llm.llm import ZhipuAILargeLanguageModel
  9. def test_validate_credentials():
  10. model = ZhipuAILargeLanguageModel()
  11. with pytest.raises(CredentialsValidateFailedError):
  12. model.validate_credentials(
  13. model='chatglm_turbo',
  14. credentials={
  15. 'api_key': 'invalid_key'
  16. }
  17. )
  18. model.validate_credentials(
  19. model='chatglm_turbo',
  20. credentials={
  21. 'api_key': os.environ.get('ZHIPUAI_API_KEY')
  22. }
  23. )
  24. def test_invoke_model():
  25. model = ZhipuAILargeLanguageModel()
  26. response = model.invoke(
  27. model='chatglm_turbo',
  28. credentials={
  29. 'api_key': os.environ.get('ZHIPUAI_API_KEY')
  30. },
  31. prompt_messages=[
  32. UserPromptMessage(
  33. content='Who are you?'
  34. )
  35. ],
  36. model_parameters={
  37. 'temperature': 0.9,
  38. 'top_p': 0.7
  39. },
  40. stop=['How'],
  41. stream=False,
  42. user="abc-123"
  43. )
  44. assert isinstance(response, LLMResult)
  45. assert len(response.message.content) > 0
  46. def test_invoke_stream_model():
  47. model = ZhipuAILargeLanguageModel()
  48. response = model.invoke(
  49. model='chatglm_turbo',
  50. credentials={
  51. 'api_key': os.environ.get('ZHIPUAI_API_KEY')
  52. },
  53. prompt_messages=[
  54. UserPromptMessage(
  55. content='Hello World!'
  56. )
  57. ],
  58. model_parameters={
  59. 'temperature': 0.9,
  60. 'top_p': 0.7
  61. },
  62. stream=True,
  63. user="abc-123"
  64. )
  65. assert isinstance(response, Generator)
  66. for chunk in response:
  67. assert isinstance(chunk, LLMResultChunk)
  68. assert isinstance(chunk.delta, LLMResultChunkDelta)
  69. assert isinstance(chunk.delta.message, AssistantPromptMessage)
  70. assert len(chunk.delta.message.content) > 0 if chunk.delta.finish_reason is None else True
  71. def test_get_num_tokens():
  72. model = ZhipuAILargeLanguageModel()
  73. num_tokens = model.get_num_tokens(
  74. model='chatglm_turbo',
  75. credentials={
  76. 'api_key': os.environ.get('ZHIPUAI_API_KEY')
  77. },
  78. prompt_messages=[
  79. SystemPromptMessage(
  80. content='You are a helpful AI assistant.',
  81. ),
  82. UserPromptMessage(
  83. content='Hello World!'
  84. )
  85. ]
  86. )
  87. assert num_tokens == 14
  88. def test_get_tools_num_tokens():
  89. model = ZhipuAILargeLanguageModel()
  90. num_tokens = model.get_num_tokens(
  91. model='tools',
  92. credentials={
  93. 'api_key': os.environ.get('ZHIPUAI_API_KEY')
  94. },
  95. tools=[
  96. PromptMessageTool(
  97. name='get_current_weather',
  98. description='Get the current weather in a given location',
  99. parameters={
  100. "type": "object",
  101. "properties": {
  102. "location": {
  103. "type": "string",
  104. "description": "The city and state e.g. San Francisco, CA"
  105. },
  106. "unit": {
  107. "type": "string",
  108. "enum": [
  109. "c",
  110. "f"
  111. ]
  112. }
  113. },
  114. "required": [
  115. "location"
  116. ]
  117. }
  118. )
  119. ],
  120. prompt_messages=[
  121. SystemPromptMessage(
  122. content='You are a helpful AI assistant.',
  123. ),
  124. UserPromptMessage(
  125. content='Hello World!'
  126. )
  127. ]
  128. )
  129. assert num_tokens == 108