test_chat.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # Copyright 2024 the LlamaFactory team.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os
  15. from llamafactory.chat import ChatModel
  16. TINY_LLAMA = os.environ.get("TINY_LLAMA", "llamafactory/tiny-random-Llama-3")
  17. INFER_ARGS = {
  18. "model_name_or_path": TINY_LLAMA,
  19. "finetuning_type": "lora",
  20. "template": "llama3",
  21. "infer_dtype": "float16",
  22. "do_sample": False,
  23. "max_new_tokens": 1,
  24. }
  25. MESSAGES = [
  26. {"role": "user", "content": "Hi"},
  27. ]
  28. EXPECTED_RESPONSE = "_rho"
  29. def test_chat():
  30. chat_model = ChatModel(INFER_ARGS)
  31. assert chat_model.chat(MESSAGES)[0].response_text == EXPECTED_RESPONSE
  32. def test_stream_chat():
  33. chat_model = ChatModel(INFER_ARGS)
  34. response = ""
  35. for token in chat_model.stream_chat(MESSAGES):
  36. response += token
  37. assert response == EXPECTED_RESPONSE