From fc37fbbee98ebf46fc85f84fb10ba9603f702fa2 Mon Sep 17 00:00:00 2001 From: Yuchao Zhang <418121364@qq.com> Date: Fri, 15 Mar 2024 00:21:38 +0800 Subject: [PATCH] Fix baichuan template (#28) --- example/history_template_baichuan.liquid | 4 +- src/config.rs | 2 +- src/history/mod.rs | 204 ++++++++++++++++------- src/routes/chat.rs | 15 +- src/routes/completions.rs | 6 +- src/startup.rs | 8 +- src/state.rs | 6 +- 7 files changed, 172 insertions(+), 73 deletions(-) diff --git a/example/history_template_baichuan.liquid b/example/history_template_baichuan.liquid index d68e48a..86a4cca 100644 --- a/example/history_template_baichuan.liquid +++ b/example/history_template_baichuan.liquid @@ -1,6 +1,8 @@ {% for item in items -%} {%- capture identity -%} {%- case item.identity -%} + {%- when "System", "Tool" -%} + System {%- when "User" -%} {%- when "Assistant" -%} @@ -11,4 +13,4 @@ {{- identity }}{% if item.name %} {{ item.name }}{% endif %}: {{ item.content }} {% endfor -%} -: +: \ No newline at end of file diff --git a/src/config.rs b/src/config.rs index 5859d89..c18229f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -26,5 +26,5 @@ pub struct Config { /// File containing the history template string #[arg(long)] #[serde(skip_serializing_if = "Option::is_none")] - pub history_template_file: Option + pub history_template_file: Option, } diff --git a/src/history/mod.rs b/src/history/mod.rs index 37e5075..30e5b39 100644 --- a/src/history/mod.rs +++ b/src/history/mod.rs @@ -1,20 +1,19 @@ -use std::fs::File; -use std::io::Read; -use std::sync::Arc; +use crate::routes::chat::ChatCompletionMessageParams; use anyhow::bail; use liquid::{ParserBuilder, Template}; use serde::Serialize; -use crate::routes::chat::ChatCompletionMessageParams; +use std::fs::File; +use std::io::Read; +use std::sync::Arc; -const DEFAULT_TEMPLATE: &str = -"{% for item in items %}\ +const DEFAULT_TEMPLATE: &str = "{% for item in items %}\ {{ item.identity }}{% if item.name %} {{ item.name }}{% endif %}: {{ item.content }} {% endfor %}\ ASSISTANT:"; #[derive(Clone)] pub struct HistoryBuilder { - history_template: Arc