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
38.87k stars 5.33k forks source link

How to implement custom LLM model and use it in Dify? #2509

Closed ntvuongg closed 5 months ago

ntvuongg commented 5 months ago

Self Checks

Provide a description of the help you need

Hi everyone, I have successfully self-hosted Dify. I want to use my custom llm model instead of using existing model which access through API. Any help?

dosubot[bot] commented 5 months ago

🤖

Hey @ntvuongg, nice to see you diving into new adventures with Dify! How's everything going?

Based on the information you've provided and the context from the Dify repository, it seems like you want to use your custom LLM model in a self-hosted Dify instead of the existing model accessed through API.

To achieve this, you would need to modify the _invoke method in the HuggingfaceHubLargeLanguageModel class. This method currently uses the InferenceClient from the huggingface_hub package to make requests to the Hugging Face API. If you want to use your own model, you need to replace this client with your own implementation that loads and uses your model.

Here's a simplified example of how you might do this:

def _invoke(self, model: str, credentials: dict, prompt_messages: list[PromptMessage], model_parameters: dict,
            tools: Optional[list[PromptMessageTool]] = None, stop: Optional[list[str]] = None, stream: bool = True,
            user: Optional[str] = None) -> Union[LLMResult, Generator]:

    # Load your custom model here
    my_model = load_my_model(model)

    # Convert the prompt messages to a single string
    prompt = self._convert_messages_to_prompt(prompt_messages)

    # Use your model to generate a response
    response = my_model.generate(prompt, **model_parameters)

    # Handle the response
    if stream:
        return self._handle_generate_stream_response(model, credentials, prompt_messages, response)

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

In this example, load_my_model is a function that loads your custom model, and my_model.generate is a method that generates a response from your model. You'll need to replace these with your actual model loading and generation code.

Please note that this is a simplified example and you might need to adjust it to fit your specific needs, especially if your model's generation method requires different parameters or returns a different type of response.

You can find more details about the HuggingfaceHubLargeLanguageModel class and its methods in the api/core/model_runtime/model_providers/huggingface_hub/llm/llm.py file.

I hope this helps! If you have any other questions or need further clarification, feel free to ask.

Sources

#### About Dosu This response is meant to be useful and save you time. It is **not meant to be a precise solution**, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot.
crazywoola commented 5 months ago

Please refer the page https://github.com/langgenius/dify/tree/main/api/core/model_runtime to see how to implement your own provider. Or you can use xinference and localai to deploy yours. See this https://docs.dify.ai/tutorials/model-configuration/xinference for details.