| | |
| |
|
| | import os |
| | import sys |
| | from qwen_api import Qwen |
| | from qwen_api.client import QwenAPIError |
| | from qwen_api.core.exceptions import AuthError, RateLimitError |
| | from qwen_api.core.types.chat import ChatMessage, ChatResponse, MessageRole |
| |
|
| | LLM_LOGIN_USER = os.getenv('LLM_LOGIN_USER','') |
| | LLM_LOGIN_PASSWORD = os.getenv('LLM_LOGIN_PASSWORD','') |
| | LLM_MODEL = os.getenv('LLM_MODEL','qwen-max-latest') |
| |
|
| | def qwen_chat(prompt: str) -> tuple[bool, str]: |
| | |
| | response_status = False |
| |
|
| | try: |
| | |
| | |
| | client = Qwen( |
| | |
| | email=LLM_LOGIN_USER, |
| | password=LLM_LOGIN_PASSWORD, |
| | |
| | |
| | |
| | base_url = "https://chat.qwen.ai", |
| | timeout = 600, |
| | log_level = "ERROR", |
| | save_logs = False |
| | ) |
| |
|
| | |
| | messages = [ChatMessage( |
| | role = MessageRole.USER, |
| | content = prompt, |
| | web_search = False, |
| | thinking = False |
| | )] |
| |
|
| | |
| | response: ChatResponse = client.chat.create( |
| | messages=messages, |
| | model=LLM_MODEL, |
| | |
| | stream=True |
| | ) |
| | |
| | response_status = True |
| |
|
| | content = "" |
| | |
| | for chunk in response: |
| | delta = chunk.choices[0].delta |
| | content += delta.content |
| |
|
| | return response_status, content |
| | |
| | except AuthError as e: |
| | print(f"Authentication failed: {e}") |
| | except RateLimitError as e: |
| | print(f"Rate limit exceeded: {e}") |
| | except QwenAPIError as e: |
| | print(f"API error: {e}") |
| | except Exception as e: |
| | print(f"Unexpected error: {e}") |
| | |
| | return response_status, "Error" |
| |
|
| | if __name__ == "__main__": |
| | if (len(sys.argv) <= 1): |
| | print("No message provided") |
| | sys.exit(1) |
| | |
| | role = "" |
| |
|
| | prompt = role + ' '.join(sys.argv[1:]) |
| |
|
| | response_ok, result = qwen_chat(prompt) |
| | |
| | if response_ok: |
| | print(result) |
| |
|