dify 添加OpenAPI完成API错误

ecfsfe2w  于 2个月前  发布在  其他
关注(0)|答案(3)|浏览(24)

自检

  • 这仅用于提交错误报告,如果您想提问,请访问 Discussions
  • 我已搜索现有问题 search for existing issues ,包括已关闭的问题。
  • 我确认我使用英语提交此报告(我已阅读并同意 Language Policy )。
  • 请不要修改此模板 :) 并填写所有必需的字段。

Dify版本

0.6.11

云或自托管

自托管(Docker)

重现步骤

请求参数:

{"model":"gpt-4o","model_type":"llm","credentials":{"mode":"completion","context_size":"4096","max_tokens_to_sample":"4096","function_calling_type":"no_call","stream_function_calling":"not_supported","vision_support":"support","stream_mode_delimiter":"\\n\\n","api_key":"[__HIDDEN__]","endpoint_url":"XXXXXXXx"},"load_balancing":{"enabled":false,"configs":[]}}

错误响应:

{
    "code": "invalid_param",
    "message": "Credentials validation failed with status code 404",
    "status": 400
}

✔️ 预期行为

添加成功。

❌ 实际行为

错误响应

k10s72fa

k10s72fa1#

你好,@violinday!我在这里协助您解决在等待人工维护者时可能出现的任何错误、问题或贡献。让我们一起解决您面临的问题。
要解决在将OpenAI兼容API集成到您的自托管Dify示例(运行在Docker上的版本0.6.11)中出现的“凭据验证失败,状态码为404”错误,请确保以下几点:

  1. 检查 endpoint_url:确保提供的 endpoint_url 凭据是正确的且可访问的。它应该指向您正在使用的OpenAI兼容服务的正确API端点。URL不应以尾部斜杠结尾。
  2. 检查 mode:确保凭据中的 mode 已正确设置为 completionchat 。这决定了将使用的端点( completionschat/completions )。
  3. 验证API密钥:确保 api_key 是正确的,并且具有访问API所需的权限。
  4. 端点可用性:确保您试图访问的端点可用且未返回404错误。您可以使用诸如 curl 或Postman之类的工具直接向端点发出请求进行测试。
    以下是显示如何验证凭据的相关代码片段:
headers = {
    'Content-Type': 'application/json'
}

api_key = credentials.get('api_key')
if api_key:
    headers["Authorization"] = f"Bearer {api_key}"

endpoint_url = credentials['endpoint_url']
if not endpoint_url.endswith('/'):
    endpoint_url += '/'

# prepare the payload for a simple ping to the model
data = {
    'model': model,
    'max_tokens': 5
}

completion_type = LLMMode.value_of(credentials['mode'])

if completion_type is LLMMode.CHAT:
    data['messages'] = [
        {
            "role": "user",
            "content": "ping"
        },
    ]
    endpoint_url = urljoin(endpoint_url, 'chat/completions')
elif completion_type is LLMMode.COMPLETION:
    data['prompt'] = 'ping'
    endpoint_url = urljoin(endpoint_url, 'completions')
else:
    raise ValueError("Unsupported completion type for model configuration.")

# send a post request to validate the credentials
response = requests.post(
    endpoint_url,
    headers=headers,
    json=data,
    timeout=(10, 300)
)

if response.status_code != 200:
    raise CredentialsValidateFailedError(
        f'Credentials validation failed with status code {response.status_code}')

请确保 endpoint_url 正确设置并可访问,并且 mode 正确指定。

  • 要继续与 Dosu 交谈,请提及 @dosu。*
zbwhf8kr

zbwhf8kr2#

我遇到了同样的问题,这个问题解决了吗?

5us2dqdw

5us2dqdw3#

你好,@violinday!我在这里协助你解决在等待人工维护者时可能遇到的任何错误、问题或贡献。让我们一起解决你面临的问题。
要解决在将OpenAI兼容的API集成到您自托管的Dify示例(版本0.6.11,运行在Docker上)时遇到的“凭据验证失败,状态码为404”错误,请确保以下几点:

  1. 检查 endpoint_url:确保提供的 endpoint_url 凭据正确且可访问。它应该指向您正在使用的OpenAI兼容服务的正确API端点。URL不应以尾随斜杠结尾。
  2. 检查 mode:确保凭据中的 mode 已正确设置为 completionchat。这决定了将使用的端点( completionschat/completions )。
  3. 验证API密钥:确保 api_key 是正确的,并且具有访问API所需的权限。
  4. 端点可用性:确保您试图访问的端点可用且未返回404错误。您可以通过使用诸如 curl 或 Postman之类的工具直接向端点发出请求来测试这一点。
    以下是显示如何验证凭据的相关代码片段:
headers = {
    'Content-Type': 'application/json'
}

api_key = credentials.get('api_key')
if api_key:
    headers["Authorization"] = f"Bearer {api_key}"

endpoint_url = credentials['endpoint_url']
if not endpoint_url.endswith('/'):
    endpoint_url += '/'

# prepare the payload for a simple ping to the model
data = {
    'model': model,
    'max_tokens': 5
}

completion_type = LLMMode.value_of(credentials['mode'])

if completion_type is LLMMode.CHAT:
    data['messages'] = [
        {
            "role": "user",
            "content": "ping"
        },
    ]
    endpoint_url = urljoin(endpoint_url, 'chat/completions')
elif completion_type is LLMMode.COMPLETION:
    data['prompt'] = 'ping'
    endpoint_url = urljoin(endpoint_url, 'completions')
else:
    raise ValueError("Unsupported completion type for model configuration.")

# send a post request to validate the credentials
response = requests.post(
    endpoint_url,
    headers=headers,
    json=data,
    timeout=(10, 300)
)

if response.status_code != 200:
    raise CredentialsValidateFailedError(
        f'Credentials validation failed with status code {response.status_code}')

确保 endpoint_url 正确设置且可访问,并且 mode 正确指定了 1(https://github.com/langgenius/dify/blob/main/api/core/model_runtime/model_providers/openai_api_compatible/llm/llm.py) 。

  • 要继续与 Dosu 交谈,请提及 @dosu。*

我遇到了相同的问题,以下是日志文件中的错误:
[ERR] 2024/07/07 - 09:36:17 | 2024070709361741415654574061408 | getAndValidateTextRequest failed: field messages is required
[ERR] 2024/07/07 - 09:36:17 | 2024070709361741415654574061408 | relay error happen, status code is 400, won't retry in this case
[GIN] 2024/07/07 - 09:36:17 | 2024070709361741415654574061408 | 400 | 739.832μs | 172.19.0.1 | POST /v1/chat/completions
[ERR] 2024/07/07 - 09:36:17 | 2024070709361741415654574061408 | relay error (channel id 7, user id: 1): field messages is required (request id: 2024070709361741415654574061408)

相关问题