| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  | import os | 
					
						
							| 
									
										
										
										
											2024-04-18 20:24:05 +08:00
										 |  |  | from collections.abc import Generator | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-12 12:34:01 +08:00
										 |  |  | import pytest | 
					
						
							| 
									
										
										
										
											2024-04-18 20:24:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-12 12:34:01 +08:00
										 |  |  | from core.model_runtime.entities.llm_entities import LLMResult, LLMResultChunk, LLMResultChunkDelta | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  | from core.model_runtime.entities.message_entities import AssistantPromptMessage, UserPromptMessage | 
					
						
							|  |  |  | from core.model_runtime.errors.validate import CredentialsValidateFailedError | 
					
						
							|  |  |  | from core.model_runtime.model_providers.openllm.llm.llm import OpenLLMLargeLanguageModel | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-12 12:34:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  | def test_validate_credentials_for_chat_model(): | 
					
						
							|  |  |  |     model = OpenLLMLargeLanguageModel() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with pytest.raises(CredentialsValidateFailedError): | 
					
						
							|  |  |  |         model.validate_credentials( | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |             model="NOT IMPORTANT", | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |             credentials={ | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |                 "server_url": "invalid_key", | 
					
						
							|  |  |  |             }, | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     model.validate_credentials( | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |         model="NOT IMPORTANT", | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |         credentials={ | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |             "server_url": os.environ.get("OPENLLM_SERVER_URL"), | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  | def test_invoke_model(): | 
					
						
							|  |  |  |     model = OpenLLMLargeLanguageModel() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     response = model.invoke( | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |         model="NOT IMPORTANT", | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |         credentials={ | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |             "server_url": os.environ.get("OPENLLM_SERVER_URL"), | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |         prompt_messages=[UserPromptMessage(content="Hello World!")], | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |         model_parameters={ | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |             "temperature": 0.7, | 
					
						
							|  |  |  |             "top_p": 1.0, | 
					
						
							|  |  |  |             "top_k": 1, | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |         stop=["you"], | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |         user="abc-123", | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |         stream=False, | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert isinstance(response, LLMResult) | 
					
						
							|  |  |  |     assert len(response.message.content) > 0 | 
					
						
							|  |  |  |     assert response.usage.total_tokens > 0 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  | def test_invoke_stream_model(): | 
					
						
							|  |  |  |     model = OpenLLMLargeLanguageModel() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     response = model.invoke( | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |         model="NOT IMPORTANT", | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |         credentials={ | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |             "server_url": os.environ.get("OPENLLM_SERVER_URL"), | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |         prompt_messages=[UserPromptMessage(content="Hello World!")], | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |         model_parameters={ | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |             "temperature": 0.7, | 
					
						
							|  |  |  |             "top_p": 1.0, | 
					
						
							|  |  |  |             "top_k": 1, | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |         stop=["you"], | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |         stream=True, | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |         user="abc-123", | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert isinstance(response, Generator) | 
					
						
							|  |  |  |     for chunk in response: | 
					
						
							|  |  |  |         assert isinstance(chunk, LLMResultChunk) | 
					
						
							|  |  |  |         assert isinstance(chunk.delta, LLMResultChunkDelta) | 
					
						
							|  |  |  |         assert isinstance(chunk.delta.message, AssistantPromptMessage) | 
					
						
							|  |  |  |         assert len(chunk.delta.message.content) > 0 if chunk.delta.finish_reason is None else True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  | def test_get_num_tokens(): | 
					
						
							|  |  |  |     model = OpenLLMLargeLanguageModel() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     response = model.get_num_tokens( | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |         model="NOT IMPORTANT", | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |         credentials={ | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |             "server_url": os.environ.get("OPENLLM_SERVER_URL"), | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |         prompt_messages=[UserPromptMessage(content="Hello World!")], | 
					
						
							|  |  |  |         tools=[], | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert isinstance(response, int) | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |     assert response == 3 |