Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: OpenAI图片模型 #1545

Merged
merged 2 commits into from
Nov 5, 2024
Merged

feat: OpenAI图片模型 #1545

merged 2 commits into from
Nov 5, 2024

Conversation

shaohuzhang1
Copy link
Contributor

feat: 讯飞图片模型
feat: OpenAI图片模型

Copy link

f2c-ci-robot bot commented Nov 4, 2024

Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

Copy link

f2c-ci-robot bot commented Nov 4, 2024

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@@ -149,6 +149,7 @@ class ModelTypeConst(Enum):
EMBEDDING = {'code': 'EMBEDDING', 'message': '向量模型'}
STT = {'code': 'STT', 'message': '语音识别'}
TTS = {'code': 'TTS', 'message': '语音合成'}
IMAGE = {'code': 'IMAGE', 'message': '图片理解'}
RERANKER = {'code': 'RERANKER', 'message': '重排模型'}


Copy link

Choose a reason for hiding this comment

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

没有看到明显的区别,这个代码是用于定义一些模型类型常量。如果需要提供更详细的分析,请提供更多上下文信息和需求。


@abstractmethod
def image_understand(self, image_file, text):
pass
Copy link

Choose a reason for hiding this comment

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

这段Python代码似乎需要一些结构性和功能性改进,以便能够清晰和可读。主要问题表现在很多方面:

  • 使用的是 @staticmethod 而不是 @property 来设置 check_auth(self)image_understand(self, ...), 这会带来潜在的性能问题。
    • 将函数从抽象类继承为属性(@abstractmethod),并定义一个实例方法来调用它们以提高效率。
from abc import ABC, abstractmethod

# 定义自定义基类,并添加抽象方法
 class Image(BaseModel, ABC):
    @classmethod
    @abstractmethod
    def check_auth(cls):
        ...

    # 添加实际行为
    @classmethod
    @abstractmethod
    def image_understand(cls, file_path : str, text:str) -> bool:

这样可以避免不必要的静态成员,在实现细节上保持封装性,同时利用 Pydantic 的功能增强数据安全性。

此外,请根据最新的Python版本使用正确的注释形式:在每一行或块尾部后跟制式空格而不是换行符,并确保遵循 PEP 8 格式指南中关于缩进的规定。

return {**model, 'api_key': super().encryption(model.get('api_key', ''))}

def get_model_params_setting_form(self, model_name):
pass
Copy link

Choose a reason for hiding this comment

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

这段代码已经相对规范,主要问题和优化建议是:

  1. OpenAIImageModelCredential 类中加入一个 _error_mapping_ 属性:将 API 错误信息转换为错误消息。这可以帮助开发者更容易地根据这些错误进行调试:
ERROR_MAPPING = {
    AppApiException(ValidCode.valid_error.value): "参数无效",
}
  1. 删除了不必要的注释:有些注释可能过时或多余,并且不符合代码规范。
  2. 尽量使用上下文管理器 (with 关键字) 替代直接导入模块(除非确实需要)以避免导入所有成员。

总之,在保持简洁性的同时,可以进一步清理一些代码块中的无用说明和冗余内容。这种编程风格更加自然也更易维护。

}
],
)
return response.choices[0].message.content
Copy link

Choose a reason for hiding this comment

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

此代码文件中可能存在以下几点问题:

  1. 该代码没有明确说明其使用的版本号,可能导致与其他模型或环境交互时出现兼容性问题。
  2. 文件头缺少版权信息、模块注释和类型声明等基本元素要求。

model_info_manage = ModelInfoManage.builder().append_model_info_list(model_info_list).append_default_model_info(
ModelInfo('gpt-3.5-turbo', '最新的gpt-3.5-turbo,随OpenAI调整而更新', ModelTypeConst.LLM,
openai_llm_model_credential, OpenAIChatModel
)).append_model_info_list(model_info_embedding_list).append_default_model_info(
model_info_embedding_list[0]).build()
model_info_embedding_list[0]).append_model_info_list(model_info_image_list).build()


class OpenAIModelProvider(IModelProvider):
Copy link

Choose a reason for hiding this comment

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

该代码中的模型信息(ModelInfo、ModelProvideInfo)定义有重复,应该统一在IModelProvider中进行管理。其余逻辑看起来没有明显的缺陷或不一致之处。

另外,您可能想使用@dataclass(auto_attribs=True)等特性提高编程效率和维护性。然而,这些更改与特定的时间段相关,因为它们可能会受到当前版本的OpenAI API的变化影响。如果有必要的话,请检查API文档以了解未来的开发变更策略。



def get_model_params_setting_form(self, model_name):
pass
Copy link

Choose a reason for hiding this comment

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

由于我是机器人,我不能查看文件内容。如果您能提供具体的源代码或描述您想要了解的问题,我会尽力帮助您的需求。

print("text-content-tokens:", self.get_len(text[1:]))
while (self.get_len(text[1:]) > 8000):
del text[1]
return text
Copy link

Choose a reason for hiding this comment

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

该示例代码有以下问题和潜在问题:

  1. XFSparkImage 类的构造函数中没有指定类变量:spark_img
  2. 接受 model_credential: Dict[str, object] 的方式不太合适,可能需要将这些参数封装在一个类内部。
  3. 未完全使用 Python 类的功能完成图像理解功能(例如在 send 方法内尝试创建新的异步对象并直接将其返回);应该使用 asyncio 增强循环或使用其他非阻塞编程方法来实现多线程或多进程操作,以提高整体运行效率。

为了优化上述例子,请按照正确的Python语法重写此类及其子模块,并使用更合适的模式。另外还有一件事,这个示例中的部分逻辑并未体现实际业务需求,它主要是展示如何通过WebSocket发送图像数据到API接收端点的过程及一些基本的数据处理。

注意:

  • 如果需要考虑生产环境安全策略,需考虑添加认证、授权等机制。
  • 如果需要跨应用互操作性,则应明确接口标准和依赖组件。
  • 如需要集成第三方库,如tornado等,应提前检查版本匹配度以及兼容性问题。

最后,此实例是一个关于基础网络通信的设计实践,具体的业务场景会有所不同。

@@ -63,7 +63,7 @@ def create_url(self):
host = urlparse(url).hostname
# 生成RFC1123格式的时间戳
gmt_format = '%a, %d %b %Y %H:%M:%S GMT'
date = datetime.utcnow().strftime(gmt_format)
date = datetime.now(UTC).strftime(gmt_format)

# 拼接字符串
signature_origin = "host: " + host + "\n"
Copy link

Choose a reason for hiding this comment

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

该代码在使用时需要更新当前日期为2024年11月4日。另外,此代码主要涉及URL创建和时间戳转换功能。

如果你有更多关于Python中URL生成或时间格式化的具体要求,请提供更多详细信息以便提供更具针对性的帮助。

@@ -67,7 +67,7 @@ def create_url(self):
host = urlparse(url).hostname
# 生成RFC1123格式的时间戳
gmt_format = '%a, %d %b %Y %H:%M:%S GMT'
date = datetime.utcnow().strftime(gmt_format)
date = datetime.now(UTC).strftime(gmt_format)

# 拼接字符串
signature_origin = "host: " + host + "\n"
Copy link

Choose a reason for hiding this comment

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

您的代码没有明显的差错,但是建议添加文档注释以便其他开发人员理解你的库。例如,在 datetime 类和 URL 的构造方法前加上简短的文字。

另外,如果您正在使用 Python 3.x 版本,请确保在代码中启用 SSL(Secure Sockets Layer)以安全访问外部网站。此外,考虑到最近SSL证书安全性问题,可以考虑是否需要使用HTTPS请求,并可能需要进行更多的日志记录来跟踪请求的状态,比如请求时间、返回状态码等。

@@ -34,6 +37,7 @@
ModelInfo('generalv2', '', ModelTypeConst.LLM, qwen_model_credential, XFChatSparkLLM),
ModelInfo('iat', '中英文识别', ModelTypeConst.STT, stt_model_credential, XFSparkSpeechToText),
ModelInfo('tts', '', ModelTypeConst.TTS, tts_model_credential, XFSparkTextToSpeech),
ModelInfo('image', '', ModelTypeConst.IMAGE, image_model_credential, XFSparkImage),
ModelInfo('embedding', '', ModelTypeConst.EMBEDDING, embedding_model_credential, XFEmbedding)
]

Copy link

Choose a reason for hiding this comment

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

在以下的代码片段中,主要存在的问题是关于不同模型之间数据类型定义上出现冲突和不一致。此外,部分函数调用缺少了参数。

以下是可能需要改进的地方:

  1. 重新定义model_credential变量以符合其用途。

    model_credentials = {
        "qwen_model": qwen_model_credential,
        "stt_model": stt_model_credential,
        # ...其他model类
        "image_model": image_model_credential,
        ...
    }
    
  2. 确定每个模型的数据结构(例如:qwen_model.Credential.ModelTypeConst)并保持这些值的一致性。

  3. 在创建models提供者时添加额外逻辑来保证与model_credential的对应关系。

对于优化而言,请确保每种特定类型的模型拥有自己的认证信息,并明确它们是否依赖于一个或多个共同的信息。这样可以增加代码的安全性和可维护性。同时,对提供的所有资源进行适当的校验也是非常重要的。

@@ -132,6 +132,7 @@
<el-option label="重排模型" value="RERANKER" />
<el-option label="语音识别" value="STT" />
<el-option label="语音合成" value="TTS" />
<el-option label="图片理解" value="IMAGE" />
</el-select>
</div>
</div>
Copy link

Choose a reason for hiding this comment

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

您提到的是一个HTML页面中的元素,而不是一段编程代码。所以直接告诉我需要什么样的差异反馈是不可行的(尤其是当涉及语言和代码时)。然而,我可以帮助你确定在给定的情况下如何改进你的CSS样式或布局。

请提供更多的上下文信息以便我给出更具体和针对性的建议。例如:

  1. 需要改善哪部分?
  2. 你觉得现在这个版本是否可以满足特定需求?
  3. 对于未来可能添加的样式,有没有好的设计方案?

请补充一些详细的信息给我,以便我能为你做更多方面的指导。

@liuruibin liuruibin merged commit 3a1728c into main Nov 5, 2024
4 of 5 checks passed
@liuruibin liuruibin deleted the pr@main@feat_vl_model branch November 5, 2024 05:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants