MetaGLM / zhipuai-sdk-python-v4

MIT License
163 stars 20 forks source link

我遇到服务端返回不稳定的问题,看看官方能不能诊断 #18

Closed arcstep closed 5 months ago

arcstep commented 7 months ago

以下问题在我的笔记本上可以重现

首先,我定义了一个工具

from langchain_zhipu import ZhipuAI

client = ZhipuAI()

tools = [
        {
            "type": "function",
            "function": {
                "name": "search",
                "description": "search(query: str) -> str - 查询 langchan 资料; args: query 类型为字符串,描述用户的问题.'",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "query": {
                            "type": "string"
                        },
                    },
                    "required": ["query"]
                }
            }
        }
    ]

正常返回的情况:

client.chat.completions.create(
    model="glm-4",
    messages = [
        {
            "role": "user",
            "content": "langchain是什么?请回答。"
        }
    ],
    tools = tools,
    tool_choice="auto",
)

此时会正确生成回调消息:

content=None role='assistant' tool_calls=[CompletionMessageToolCall(id='call_8513520561194426201', function=Function(arguments='{"query":"langchain是什么?"}', name='search'), type='function')]

服务端报错的情况:

两者差别仅仅在于,提示语后面多个“请回答”或“请查询本地服务”等字眼,因此不像是格式问题,更像是服务端的某种要求。**

client.chat.completions.create(
    model="glm-4",
    messages = [
        {
            "role": "user",
            "content": "langchain是什么?"
        }
    ],
    tools = tools,
    tool_choice="auto",
)

报错如下:

{
    "name": "APIInternalError",
    "message": "Error code: 500, with error text {\"error\":{\"code\":\"500\",\"message\":\"Internal Error\"}}",
    "stack": "---------------------------------------------------------------------------
APIInternalError                          Traceback (most recent call last)
Cell In[97], line 24
      3 client = ZhipuAI()
      5 tools = [
      6         {
      7             \"type\": \"function\",
   (...)
     21         }
     22     ]
---> 24 response = client.chat.completions.create(
     25     model=\"glm-4\",
     26     messages = [
     27         {
     28             \"role\": \"user\",
     29             \"content\": \"langchain是什么?\"
     30         }
     31     ],
     32     tools = tools,
     33     tool_choice=\"auto\",
     34 )
     35 print(response.choices[0].message)

File ~/github/langchain_zhipuai/langchain_zhipu/api_resource/chat/completions.py:48, in Completions.create(self, model, request_id, do_sample, stream, temperature, top_p, max_tokens, seed, messages, stop, sensitive_word_check, tools, tool_choice, extra_headers, disable_strict_validation, timeout)
     46     _cast_type = object
     47     _stream_cls = StreamResponse[object]
---> 48 return self._post(
     49     \"/chat/completions\",
     50     body={
     51         \"model\": model,
     52         \"request_id\": request_id,
     53         \"temperature\": temperature,
     54         \"top_p\": top_p,
     55         \"do_sample\": do_sample,
     56         \"max_tokens\": max_tokens,
     57         \"seed\": seed,
     58         \"messages\": messages,
     59         \"stop\": stop,
     60         \"sensitive_word_check\": sensitive_word_check,
     61         \"stream\": stream,
     62         \"tools\": tools,
     63         \"tool_choice\": tool_choice,
     64     },
     65     options=make_user_request_input(
     66         extra_headers=extra_headers,
     67     ),
     68     cast_type=_cast_type,
     69     enable_stream=stream or False,
     70     stream_cls=_stream_cls,
     71 )

File ~/github/langchain_zhipuai/langchain_zhipu/core/_http_client.py:290, in HttpClient.post(self, path, body, cast_type, options, files, enable_stream, stream_cls)
    276 def post(
    277         self,
    278         path: str,
   (...)
    285         stream_cls: type[StreamResponse[Any]] | None = None,
    286 ) -> ResponseT | StreamResponse:
    287     opts = ClientRequestParam.construct(method=\"post\", json_data=body, files=make_httpx_files(files), url=path,
    288                                         **options)
--> 290     return self.request(
    291         cast_type=cast_type, params=opts,
    292         enable_stream=enable_stream,
    293         stream_cls=stream_cls
    294     )

File ~/github/langchain_zhipuai/langchain_zhipu/core/_http_client.py:249, in HttpClient.request(self, cast_type, params, enable_stream, stream_cls)
    247     err.response.read()
    248     # raise err
--> 249     raise self._make_status_error(err.response) from None
    251 except Exception as err:
    252     raise err

APIInternalError: Error code: 500, with error text {\"error\":{\"code\":\"500\",\"message\":\"Internal Error\"}}"
}
zRzRzRzRzRzRzR commented 7 months ago

收到反馈,与你取得联系

glide-the commented 5 months ago

此问题已修复,您可以通过如下参数控制一些传输层的问题

客户端网络链接配置

在core/_http_client.py中,可以配置网络链接的超时时间,重试次数,限制等参数


# 通过 `Timeout` 控制接口`connect` 和 `read` 超时时间,默认为`timeout=300.0, connect=8.0`
ZHIPUAI_DEFAULT_TIMEOUT = httpx.Timeout(timeout=300.0, connect=8.0)
# 通过 `retry` 参数控制重试次数,默认为3次
ZHIPUAI_DEFAULT_MAX_RETRIES = 3
# 通过 `Limits` 控制最大连接数和保持连接数,默认为`max_connections=50, max_keepalive_connections=10`
ZHIPUAI_DEFAULT_LIMITS = httpx.Limits(max_connections=50, max_keepalive_connections=10)

同样在ZhipuAI入参中可以配置

client = ZhipuAI(
    timeout= httpx.Timeout(timeout=300.0, connect=8.0),
    max_retries=3,
)