| 
									
										
										
										
											2024-04-02 13:38:46 +03:00
										 |  |  | import os | 
					
						
							| 
									
										
										
										
											2024-04-18 20:24:05 +08:00
										 |  |  | from collections.abc import Generator | 
					
						
							| 
									
										
										
										
											2024-04-02 13:38:46 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | import pytest | 
					
						
							| 
									
										
										
										
											2024-04-18 20:24:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-02 13:38:46 +03:00
										 |  |  | from core.model_runtime.entities.llm_entities import LLMResult, LLMResultChunk, LLMResultChunkDelta | 
					
						
							| 
									
										
										
										
											2024-04-18 20:24:05 +08:00
										 |  |  | from core.model_runtime.entities.message_entities import ( | 
					
						
							|  |  |  |     AssistantPromptMessage, | 
					
						
							|  |  |  |     SystemPromptMessage, | 
					
						
							|  |  |  |     UserPromptMessage, | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2024-04-02 13:38:46 +03:00
										 |  |  | from core.model_runtime.errors.validate import CredentialsValidateFailedError | 
					
						
							|  |  |  | from core.model_runtime.model_providers.openrouter.llm.llm import OpenRouterLargeLanguageModel | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_validate_credentials(): | 
					
						
							|  |  |  |     model = OpenRouterLargeLanguageModel() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with pytest.raises(CredentialsValidateFailedError): | 
					
						
							|  |  |  |         model.validate_credentials( | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |             model="mistralai/mixtral-8x7b-instruct", credentials={"api_key": "invalid_key", "mode": "chat"} | 
					
						
							| 
									
										
										
										
											2024-04-02 13:38:46 +03:00
										 |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     model.validate_credentials( | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |         model="mistralai/mixtral-8x7b-instruct", | 
					
						
							|  |  |  |         credentials={"api_key": os.environ.get("TOGETHER_API_KEY"), "mode": "chat"}, | 
					
						
							| 
									
										
										
										
											2024-04-02 13:38:46 +03:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_invoke_model(): | 
					
						
							|  |  |  |     model = OpenRouterLargeLanguageModel() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     response = model.invoke( | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |         model="mistralai/mixtral-8x7b-instruct", | 
					
						
							|  |  |  |         credentials={"api_key": os.environ.get("TOGETHER_API_KEY"), "mode": "completion"}, | 
					
						
							| 
									
										
										
										
											2024-04-02 13:38:46 +03:00
										 |  |  |         prompt_messages=[ | 
					
						
							|  |  |  |             SystemPromptMessage( | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |                 content="You are a helpful AI assistant.", | 
					
						
							| 
									
										
										
										
											2024-04-02 13:38:46 +03:00
										 |  |  |             ), | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |             UserPromptMessage(content="Who are you?"), | 
					
						
							| 
									
										
										
										
											2024-04-02 13:38:46 +03:00
										 |  |  |         ], | 
					
						
							|  |  |  |         model_parameters={ | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |             "temperature": 1.0, | 
					
						
							|  |  |  |             "top_k": 2, | 
					
						
							|  |  |  |             "top_p": 0.5, | 
					
						
							| 
									
										
										
										
											2024-04-02 13:38:46 +03:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |         stop=["How"], | 
					
						
							| 
									
										
										
										
											2024-04-02 13:38:46 +03:00
										 |  |  |         stream=False, | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |         user="abc-123", | 
					
						
							| 
									
										
										
										
											2024-04-02 13:38:46 +03:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert isinstance(response, LLMResult) | 
					
						
							|  |  |  |     assert len(response.message.content) > 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_invoke_stream_model(): | 
					
						
							|  |  |  |     model = OpenRouterLargeLanguageModel() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     response = model.invoke( | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |         model="mistralai/mixtral-8x7b-instruct", | 
					
						
							|  |  |  |         credentials={"api_key": os.environ.get("TOGETHER_API_KEY"), "mode": "chat"}, | 
					
						
							| 
									
										
										
										
											2024-04-02 13:38:46 +03:00
										 |  |  |         prompt_messages=[ | 
					
						
							|  |  |  |             SystemPromptMessage( | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |                 content="You are a helpful AI assistant.", | 
					
						
							| 
									
										
										
										
											2024-04-02 13:38:46 +03:00
										 |  |  |             ), | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |             UserPromptMessage(content="Who are you?"), | 
					
						
							| 
									
										
										
										
											2024-04-02 13:38:46 +03:00
										 |  |  |         ], | 
					
						
							|  |  |  |         model_parameters={ | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |             "temperature": 1.0, | 
					
						
							|  |  |  |             "top_k": 2, | 
					
						
							|  |  |  |             "top_p": 0.5, | 
					
						
							| 
									
										
										
										
											2024-04-02 13:38:46 +03:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |         stop=["How"], | 
					
						
							| 
									
										
										
										
											2024-04-02 13:38:46 +03:00
										 |  |  |         stream=True, | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |         user="abc-123", | 
					
						
							| 
									
										
										
										
											2024-04-02 13:38:46 +03:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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 = OpenRouterLargeLanguageModel() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     num_tokens = model.get_num_tokens( | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |         model="mistralai/mixtral-8x7b-instruct", | 
					
						
							| 
									
										
										
										
											2024-04-02 13:38:46 +03:00
										 |  |  |         credentials={ | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |             "api_key": os.environ.get("TOGETHER_API_KEY"), | 
					
						
							| 
									
										
										
										
											2024-04-02 13:38:46 +03:00
										 |  |  |         }, | 
					
						
							|  |  |  |         prompt_messages=[ | 
					
						
							|  |  |  |             SystemPromptMessage( | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |                 content="You are a helpful AI assistant.", | 
					
						
							| 
									
										
										
										
											2024-04-02 13:38:46 +03:00
										 |  |  |             ), | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |             UserPromptMessage(content="Hello World!"), | 
					
						
							|  |  |  |         ], | 
					
						
							| 
									
										
										
										
											2024-04-02 13:38:46 +03:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert isinstance(num_tokens, int) | 
					
						
							|  |  |  |     assert num_tokens == 21 |