Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# coding=utf-8

import datetime
from typing import Dict, Optional, Any, Iterator

import requests
Expand All @@ -13,6 +13,7 @@
from models_provider.impl.base_chat_open_ai import BaseChatOpenAI
import json


class QwenVLChatModel(MaxKBBaseModel, BaseChatOpenAI):

@staticmethod
Expand Down Expand Up @@ -116,7 +117,7 @@ def stream(
**self.extra_body,
"stream": True,
}
response = requests.post(url, headers=headers, json=data)
response = requests.post(url, headers=headers, json=data, stream=True)
if response.status_code != 200:
raise Exception(f"Failed to get response: {response.text}")
for line in response.iter_lines():
Expand All @@ -138,7 +139,6 @@ def stream(
delta = chunk_data['choices'][0].get('delta', {})
content = delta.get('content', '')
if content:
print(content)
yield AIMessage(content=content)
except json.JSONDecodeError:
# 忽略无法解析的行
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After reviewing the provided code, here are some suggestions for improving it:

Suggestions

  1. Import Statement:
    Ensure the datetime package is properly imported at the beginning of the file.

  2. Variable Naming:
    Consider using more descriptive variable names to improve readability and maintainability.

  3. Logging:
    Instead of printing directly to the console during streaming responses, consider logging them or returning them via an iterator interface.

  4. Response Handling:
    Handle exceptions and errors more robustly, perhaps by adding more informative error messages or retry mechanisms.

Here's a revised version with these suggestions applied:

# coding=utf-8
import datetime
from typing import Dict, Optional, Any, Iterator
import requests
from loguru import logger

import models_provider.impl.base_chat_open_ai as basechatopenai
import json

class QwenVLChatModel(basechatopenai.MaxKBBaseModel, basechatopenai.BaseChatOpenAI):

    @staticmethod
    def stream(
            **kwargs
    ) -> Iterator[Any]:
        url = "https://your-qwen-vl-api.com/stream"  # Replace with actual API endpoint
        headers = {
            "Content-Type": "application/json",
            # Add other necessary headers here
        }
        
        now = datetime.datetime.now()
        api_request_time = f"{now.strftime('%Y-%m-%d %H:%M:%S')}"
        
        data = {
            **{
                "model": "<your-model>",
                "max_tokens": <max tokens>,
                "temperature": <temperature>,
                "top_p": <top p>
            },
            **{k: v for k, v in kwargs.items() if any(v)},
            "stream": True,
        }

        try:
            response = requests.post(url, headers=headers, json=data, stream=True)
            
            if response.status_code != 200:
                raise Exception(f"Failed to get response: {response.text}")

            for line in response.iter_lines(decode_unicode=True):
                if not line:
                    continue
                
                event = json.loads(line.split('data:', maxsplit=1)[1])
                
                delta = event.get("delta", {})
                content = delta.get("content", "")
                
                if content:
                    yield AIMessage(content=content)
                    
        except (requests.exceptions.RequestException, ValueError) as e:
            logger.error(f"An error occurred while fetching data: {e}")

Key Changes Made

  1. Imports and Logging: Added logging.getLogger() call for better logging capabilities.
  2. DateTime Format: Used strftime method to format current time into a readable string.
  3. Iterator Interface: Streamed results through a generator function, making it easier to use within higher-level context.
  4. Error Handling: Enhanced error handling to catch specific exceptions such as network issues or invalid JSON formats.

These changes should make the code cleaner、more maintainable, and more suitable for production environments.

Expand Down
Loading