langgenius / dify

Dify is an open-source LLM app development platform. Dify's intuitive interface combines AI workflow, RAG pipeline, agent capabilities, model management, observability features and more, letting you quickly go from prototype to production.
https://dify.ai
Other
45.35k stars 6.38k forks source link

Error on using Anthropic’s API for all Agents on Dify #6446

Closed bguzela closed 1 month ago

bguzela commented 1 month ago

Self Checks

Dify version

0.6.14

Cloud or Self Hosted

Cloud

Steps to reproduce

All my agents that were running with the anthropic API (Claude Sonnet and Claude Haiku), now show the following error: “[anthropic] Error: Messages.create() got an unexpected keyword argument 'headers'”. All of them, that once were perfectly working, now show this error, does not matter what I write in the prompt. IMG_9759

✔️ Expected Behavior

I was just expecting the agents to work as they were before.

❌ Actual Behavior

The agents with anthropic’s LLM are not working.

dosubot[bot] commented 1 month ago

Hello @bguzela! I'm here to help you with any bugs, questions, or contributions you have regarding Dify. Let's tackle this issue together while we wait for a human maintainer.

The error “[anthropic] Error: Messages.create() got an unexpected keyword argument 'headers'” occurs because the Messages.create() method in the Anthropic SDK does not accept a headers argument, but it is being passed one.

To resolve this issue, you need to ensure that the headers argument is not being passed to the Messages.create() method. Here is the updated function:

def _generate_anthropic(self, model: str, credentials: dict, prompt_messages: list[PromptMessage], model_parameters: dict,
                        stop: Optional[list[str]] = None, stream: bool = True, user: Optional[str] = None) -> Union[LLMResult, Generator]:
    """
    Invoke Anthropic large language model

    :param model: model name
    :param credentials: model credentials
    :param prompt_messages: prompt messages
    :param model_parameters: model parameters
    :param stop: stop words
    :param stream: is stream response
    :return: full response or stream response chunk generator result
    """
    client = AnthropicBedrock(
        aws_access_key=credentials.get("aws_access_key_id"),
        aws_secret_key=credentials.get("aws_secret_access_key"),
        aws_region=credentials["aws_region"],
    )

    extra_model_kwargs = {}
    if stop:
        extra_model_kwargs['stop_sequences'] = stop

    system, prompt_message_dicts = self._convert_claude_prompt_messages(prompt_messages)

    if system:
        extra_model_kwargs['system'] = system

    response = client.messages.create(
        model=model,
        messages=prompt_message_dicts,
        stream=stream,
        **model_parameters,
        **extra_model_kwargs
    )

    if stream:
        return self._handle_claude_stream_response(model, credentials, response, prompt_messages)

    return self._handle_claude_response(model, credentials, response, prompt_messages)

By ensuring that the headers argument is not included in the call to client.messages.create(), the function will now work without raising the error [1][2].

To continue talking to Dosu, mention @dosu.

ottoradiologia commented 1 month ago

I also encountered an issue when attempting to insert a new Anthropic API key on the Dify platform. Upon trying to save the new key, the following error message appears:

"Messages.create() got an unexpected keyword argument 'headers'"

Steps to Reproduce:

Navigate to the model configuration section. Select "Model Provider" and choose Anthropic. Enter the new API key and click "Save." The error message appears.

crazywoola commented 1 month ago

Fixed we will upgrade the cloud later.

bguzela commented 1 month ago

Fixed we will upgrade the cloud later.

Sure! Thank you very much! If possible, please prioritize this, as we are running many of the company's applications on Dify using Anthropic's LLM, and at the moment they are on hold.

Thank you very much!

crazywoola commented 1 month ago

If this is resolved, please close this PR. :)

crazywoola commented 1 month ago

Fixed in https://github.com/langgenius/dify/pull/6843. Will release it tonight.

briangladu commented 1 month ago

I'm not able to add Anthropic on the hosted version. Get this error:

Error code: 404 - {'type': 'error', 'error': {'type': 'not_found_error', 'message': 'Not Found'}}