Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: 不同的LLM模型,代码要以怎样的方式融合到项目里 #739

Open
dogvane opened this issue May 14, 2024 · 3 comments

Comments

@dogvane
Copy link

dogvane commented May 14, 2024

Background & Description

经过测试,默认模板是无法作用应用于 Qwen1.5 模型里的,会出现结果混乱。
在增加自定义模板后,可以正常输出结果。
现在的想法是,类似常见的模型的问答模板,要怎样整合到 LLamaSharp 基础项目里。

     using var model = LLamaWeights.LoadFromFile(parameters);
      using var context = model.CreateContext(parameters);
      var ex = new InteractiveExecutor(context);
      ChatSession session = new ChatSession(ex);
      session.AddSystemMessage("You are a helpful assistant.");
      session.WithHistoryTransform(new QwenHistoryTransform());

      InferenceParams inferenceParams = new InferenceParams()
      {
          MaxTokens = 1024 * 8, // No more than 256 tokens should appear in answer. Remove it if antiprompt is enough for control.
          AntiPrompts = new List<string> { "<|im_end|>", "<|endoftext|>" } // Stop generation once antiprompts appear.
      };

class QwenHistoryTransform : IHistoryTransform
{
    private const string StartHeaderId = "<|im_start|>";
    private const string EndHeaderId = "<|im_end|>";
    private const string Bos = "<|im_start|>";
    private const string Eos = "<|im_end|>";
    private const string EndofTurn = "<|endoftext|>";

    /// <summary>
    /// Convert a ChatHistory instance to plain text.
    /// </summary>
    /// <param name="history">The ChatHistory instance</param>
    /// <returns></returns>
    public string HistoryToText(ChatHistory history)
    {
        StringBuilder sb = new StringBuilder(1024);

        foreach (var message in history.Messages)
        {
            BuildMessage(sb, GetRoleName(message.AuthorRole), message.Content);
        }
        
        return sb.ToString();
    }

    private string GetRoleName(AuthorRole authorRole)
    {
        switch (authorRole)
        {
            case AuthorRole.User:
                return "user";
            case AuthorRole.Assistant:
                return "assistant";
            case AuthorRole.System:
                return "system";
            default:
                // 这里需要考虑如何设置自定义用户的角色的输入参数情况
                throw new Exception("Unsupported role: " + authorRole);
        }
    }

    private static void BuildMessage(StringBuilder sb, string roleName, string message)
    {
        sb.Append(Bos);
        sb.Append(roleName);
        sb.Append("\n");
        sb.Append(message);
        sb.Append(Eos);
        sb.Append("\n");
    }

    /// <summary>
    /// Converts plain text to a ChatHistory instance.
    /// </summary>
    /// <param name="role">The role for the author.</param>
    /// <param name="text">The chat history as plain text.</param>
    /// <returns>The updated history.</returns>
    public ChatHistory TextToHistory(AuthorRole role, string text)
    {
        return new ChatHistory(new ChatHistory.Message[] { new ChatHistory.Message(role, text) });
    }

    /// <summary>
    /// Copy the transform.
    /// </summary>
    /// <returns></returns>
    public IHistoryTransform Clone()
    {
        return new QwenHistoryTransform();
    }
}

API & Usage

No response

How to implement

No response

@AsakusaRinne
Copy link
Collaborator

这个的确是缺失的重要特性之一,如果希望进行贡献,建议可以先看一下#715中的LLama text template,然后在这里提出你希望增加的API,我们会与你讨论并协助完善方案,随后就可以发起PR并合并。大概来讲,我觉得思路是设置一个Interface然后衍生出多种模型的模板实现,需要考虑到ChatCompletion和TextCompletion两种情况,以及与ChatSession的整合。在一开始可以只引入最基本的功能,但是需要注意实现的可扩展性。如果这一过程中有任何困难,请随时向我们寻求帮助。

请尽量使用英语在这里交流,LLamaSharp的绝大多数贡献者都不是中国人,使用英文会让更多的开发者可以参与讨论。

@dogvane
Copy link
Author

dogvane commented May 14, 2024

看了llama.cpp里关于模板的代码,在使用上,能开放给外部使用的空间和灵活度还是不够,特别是它内部未实现的模型。
并且,目前一个新模型,必定会存在与之对应着它的一个模板方案。
我的想法是,在llamaSharp的基础上,针对不同的模型,增加一个自定义包,例如:LlamaSharp.Phi3 LlamaSharp.Qwen 等。
包里继承了模型的下载(基于huggingface.co)针对它的模型文本的配置,以及返回结果的一些个性操作。

After reviewing the template code in llama.cpp, I found that the space and flexibility for external use are still insufficient, particularly for the internally unimplemented models. Additionally, each new model will inevitably have a corresponding template solution. My idea is to create a custom package based on llamaSharp for different models, such as LlamaSharp.Phi3 and LlamaSharp.Qwen. These packages would include the model download (based on huggingface.co), configuration of the model text, and some personalized operations for the returned results.

@AsakusaRinne
Copy link
Collaborator

My idea is to create a custom package based on llamaSharp for different models, such as LlamaSharp.Phi3 and LlamaSharp.Qwen.

I'm not sure of it because I don't know how you would like to design the APIs, but integrating these functionalities in a separate package is certainly an option. If doing so, I'd prefer to add one package LLamaSharp.Models instead of lots of packages of each supported model.

These packages would include the model download (based on huggingface.co), configuration of the model text, and some personalized operations for the returned results.

I've already made a library for downloading models from Huggingface. Hope it will help you if you want to support model downloading. https://github.com/AsakusaRinne/HuggingfaceHub

If you find any problem when making the contribution, please feel free to ask for help here or in the QQ/Discord group.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants