Skip to content

Commit

Permalink
merge llm- binaries into llm (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
ling0322 authored Jul 30, 2024
1 parent a57a344 commit 7e88156
Show file tree
Hide file tree
Showing 14 changed files with 304 additions and 262 deletions.
78 changes: 78 additions & 0 deletions go/bin/args.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// The MIT License (MIT)
//
// Copyright (c) 2024 Xiaoyang Chen
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
// and associated documentation files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package main

import (
"flag"
"log"
"os"
"strings"

"github.com/ling0322/libllm/go/llm"
)

var gModelPath string
var gDevice string
var gInputFile string

func addDeviceFlag(fs *flag.FlagSet) {
fs.StringVar(&gDevice, "device", "auto", "inference device, either cpu, cuda or auto")
}

func getDeviceArg() llm.Device {
var device llm.Device
if strings.ToLower(gDevice) == "cpu" {
device = llm.Cpu
} else if strings.ToLower(gDevice) == "cuda" {
device = llm.Cuda
} else if strings.ToLower(gDevice) == "auto" {
device = llm.Auto
} else {
log.Fatalf("unexpected device %s", gDevice)
}

return device
}

func addModelFlag(fs *flag.FlagSet) {
fs.StringVar(&gModelPath, "model", "", "the libllm model file *.llmpkg")
}

func getModelArg(fs *flag.FlagSet) string {
if gModelPath == "" {
fs.Usage()
os.Exit(1)
}

return gModelPath
}

func addInputFlag(fs *flag.FlagSet) {
fs.StringVar(&gInputFile, "input", "", "the input file.")
}

func getInputArg(fs *flag.FlagSet) string {
if gInputFile == "" {
fs.Usage()
os.Exit(1)
}

return gInputFile
}
31 changes: 30 additions & 1 deletion go/bin/llm/chat.go → go/bin/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package main
import (
"bufio"
"errors"
"flag"
"fmt"
"io"
"log"
Expand All @@ -33,7 +34,35 @@ import (
"github.com/ling0322/libllm/go/skill"
)

func chatMain(model llm.Model) {
func printChatUsage(fs *flag.FlagSet) {
fmt.Fprintln(os.Stderr, "Usage: llm chat [OPTIONS]")
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, "Options:")
fs.PrintDefaults()
fmt.Fprintln(os.Stderr, "")
}

func chatMain(args []string) {
fs := flag.NewFlagSet("", flag.ExitOnError)
fs.Usage = func() {
printChatUsage(fs)
}

addModelFlag(fs)
addDeviceFlag(fs)
_ = fs.Parse(args)

if fs.NArg() != 0 {
fs.Usage()
os.Exit(1)
}

modelName := getModelArg(fs)
model, err := llm.NewModel(modelName, getDeviceArg())
if err != nil {
log.Fatal(err)
}

llmChat, err := skill.NewChat(model)
if err != nil {
log.Fatal(err)
Expand Down
2 changes: 0 additions & 2 deletions go/bin/go.sum

This file was deleted.

32 changes: 25 additions & 7 deletions go/bin/i18n.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
package bin
// The MIT License (MIT)
//
// Copyright (c) 2024 Xiaoyang Chen
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
// and associated documentation files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package main

import (
"errors"
"log"
)

Expand All @@ -10,23 +28,23 @@ type Localizer struct {
msgMap map[int]string
}

func NewLocalizer(localeMsgMap map[string]map[int]string) (*Localizer, error) {
func NewLocalizer(localeMsgMap map[string]map[int]string) *Localizer {
locale, err := getLocale()
if err != nil {
locale = ""
locale = "en_us"
}

msgMap, ok := localeMsgMap[locale]
if ok {
return &Localizer{locale, msgMap}, nil
return &Localizer{locale, msgMap}
}

msgMap, ok = localeMsgMap["en_us"]
if !ok {
return nil, errors.New("en_us not found in the localeMsgMap")
log.Fatal("en_us not found in the localeMsgMap")
}

return &Localizer{locale, msgMap}, nil
return &Localizer{locale, msgMap}
}

func (l *Localizer) Get(messageID int) string {
Expand Down
61 changes: 61 additions & 0 deletions go/bin/i18n_table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// The MIT License (MIT)
//
// Copyright (c) 2024 Xiaoyang Chen
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
// and associated documentation files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package main

var gLocalizer = NewLocalizer(map[string]map[int]string{
"en_us": gMsgEnUs,
"zh_cn": gMsgZhCn,
})

const (
MsgErrMoreThanOnePkg = iota
MsgErrPkgNotExist
MsgErrError
MsgPressEnter
MsgInputQuestion
MsgInputQuestionNew
MsgInputQuestionSys
MsgNewSession
MsgStat
)

var gMsgZhCn = map[int]string{
MsgErrMoreThanOnePkg: "请不要在当前文件夹放多于1个的.llmpkg文件",
MsgErrPkgNotExist: "找不到.llmpkg文件",
MsgErrError: "错误: ",
MsgPressEnter: "按回车键继续...",
MsgInputQuestion: "请输入问题:",
MsgInputQuestionNew: " 输入 ':new' 重新开始一个新的对话 (清除历史).",
MsgInputQuestionSys: " 输入 ':sys <系统指令>' 设置对话的系统指令,并重新开始一个新的对话.",
MsgNewSession: "===== 新的对话 =====",
MsgStat: "(%d个Token, 总共耗时%.2f秒, 平均每个Token耗时%.2f毫秒)\n",
}

var gMsgEnUs = map[int]string{
MsgErrMoreThanOnePkg: "More than one .llmpkg in current directory. Please keep only one of them",
MsgErrPkgNotExist: "Unable to find .llmpkg model file in current directory",
MsgErrError: "Error: ",
MsgPressEnter: "Press [Enter] to continue ...",
MsgInputQuestion: "Please input your question.",
MsgInputQuestionNew: " Type ':new' to start a new session (clean history).",
MsgInputQuestionSys: " Type ':sys <system_prompt>' to set the system prompt and start a new session .",
MsgNewSession: "===== new session =====",
MsgStat: "(%d tokens, time=%.2fs, %.2fms per token)\n",
}
83 changes: 0 additions & 83 deletions go/bin/llm/main.go

This file was deleted.

Loading

0 comments on commit 7e88156

Please sign in to comment.