feat: support GLM-4V (#2124)

This commit is contained in:
Yeuoly 2024-01-22 11:56:37 +08:00 committed by GitHub
parent 14a2eeba0c
commit 8394bbd47f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 95 additions and 11 deletions

View File

@ -11,7 +11,8 @@ class _CommonZhipuaiAI:
:return:
"""
credentials_kwargs = {
"api_key": credentials['api_key'],
"api_key": credentials['api_key'] if 'api_key' in credentials else
credentials['zhipuai_api_key'] if 'zhipuai_api_key' in credentials else None,
}
return credentials_kwargs

View File

@ -0,0 +1,44 @@
model: glm-4v
label:
en_US: glm-4v
model_type: llm
model_properties:
mode: chat
features:
- vision
parameter_rules:
- name: temperature
use_template: temperature
default: 0.95
min: 0.0
max: 1.0
help:
zh_Hans: 采样温度,控制输出的随机性,必须为正数取值范围是:(0.0,1.0],不能等于 0,默认值为 0.95 值越大,会使输出更随机,更具创造性;值越小,输出会更加稳定或确定建议您根据应用场景调整 top_p 或 temperature 参数,但不要同时调整两个参数。
en_US: Sampling temperature, controls the randomness of the output, must be a positive number. The value range is (0.0,1.0], which cannot be equal to 0. The default value is 0.95. The larger the value, the more random and creative the output will be; the smaller the value, The output will be more stable or certain. It is recommended that you adjust the top_p or temperature parameters according to the application scenario, but do not adjust both parameters at the same time.
- name: top_p
use_template: top_p
default: 0.7
help:
zh_Hans: 用温度取样的另一种方法,称为核取样取值范围是:(0.0, 1.0) 开区间,不能等于 0 或 1默认值为 0.7 模型考虑具有 top_p 概率质量tokens的结果例如0.1 意味着模型解码器只考虑从前 10% 的概率的候选集中取 tokens 建议您根据应用场景调整 top_p 或 temperature 参数,但不要同时调整两个参数。
en_US: Another method of temperature sampling is called kernel sampling. The value range is (0.0, 1.0) open interval, which cannot be equal to 0 or 1. The default value is 0.7. The model considers the results with top_p probability mass tokens. For example 0.1 means The model decoder only considers tokens from the candidate set with the top 10% probability. It is recommended that you adjust the top_p or temperature parameters according to the application scenario, but do not adjust both parameters at the same time.
- name: incremental
label:
zh_Hans: 增量返回
en_US: Incremental
type: boolean
help:
zh_Hans: SSE接口调用时用于控制每次返回内容方式是增量还是全量不提供此参数时默认为增量返回true 为增量返回false 为全量返回。
en_US: When the SSE interface is called, it is used to control whether the content is returned incrementally or in full. If this parameter is not provided, the default is incremental return. true means incremental return, false means full return.
required: false
- name: return_type
label:
zh_Hans: 回复类型
en_US: Return Type
type: string
help:
zh_Hans: 用于控制每次返回内容的类型,空或者没有此字段时默认按照 json_string 返回json_string 返回标准的 JSON 字符串text 返回原始的文本内容。
en_US: Used to control the type of content returned each time. When it is empty or does not have this field, it will be returned as json_string by default. json_string returns a standard JSON string, and text returns the original text content.
required: false
options:
- text
- json_string

View File

@ -3,7 +3,8 @@ from typing import Any, Dict, Generator, List, Optional, Union
from core.model_runtime.entities.llm_entities import LLMResult, LLMResultChunk, LLMResultChunkDelta
from core.model_runtime.entities.message_entities import (AssistantPromptMessage, PromptMessage, PromptMessageRole,
PromptMessageTool, SystemPromptMessage, UserPromptMessage)
PromptMessageTool, SystemPromptMessage, UserPromptMessage,
TextPromptMessageContent, ImagePromptMessageContent, PromptMessageContentType)
from core.model_runtime.errors.validate import CredentialsValidateFailedError
from core.model_runtime.model_providers.__base.large_language_model import LargeLanguageModel
from core.model_runtime.model_providers.zhipuai._client import ZhipuModelAPI
@ -108,10 +109,21 @@ class ZhipuAILargeLanguageModel(_CommonZhipuaiAI, LargeLanguageModel):
prompt_messages = prompt_messages[1:]
# resolve zhipuai model not support system message and user message, assistant message must be in sequence
new_prompt_messages = []
new_prompt_messages: List[PromptMessage] = []
for prompt_message in prompt_messages:
copy_prompt_message = prompt_message.copy()
if copy_prompt_message.role in [PromptMessageRole.USER, PromptMessageRole.SYSTEM, PromptMessageRole.TOOL]:
if isinstance(copy_prompt_message.content, list):
# check if model is 'glm-4v'
if model != 'glm-4v':
# not support list message
continue
# get image and
if not isinstance(copy_prompt_message, UserPromptMessage):
# not support system message
continue
new_prompt_messages.append(copy_prompt_message)
if not isinstance(copy_prompt_message.content, str):
# not support image message
continue
@ -130,11 +142,38 @@ class ZhipuAILargeLanguageModel(_CommonZhipuaiAI, LargeLanguageModel):
else:
new_prompt_messages.append(copy_prompt_message)
if model == 'glm-4v':
params = {
'model': model,
'prompt': [{
'role': prompt_message.role.value,
'content': prompt_message.content
'content':
[
{
'type': 'text',
'text': prompt_message.content
}
] if isinstance(prompt_message.content, str) else
[
{
'type': 'image',
'image_url': {
'url': content.data
}
} if content.type == PromptMessageContentType.IMAGE else {
'type': 'text',
'text': content.data
} for content in prompt_message.content
],
} for prompt_message in new_prompt_messages],
**model_parameters
}
else:
params = {
'model': model,
'prompt': [{
'role': prompt_message.role.value,
'content': prompt_message.content,
} for prompt_message in new_prompt_messages],
**model_parameters
}