chatglm

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 19, 2023 License: MIT Imports: 5 Imported by: 0

README

go-chatglm.cpp

GoDoc Go Report Card License

chatglm.cpp golang bindings.

The go-chatglm.cpp bindings are high level, as such most of the work is kept into the C/C++ code to avoid any extra computational cost, be more performant and lastly ease out maintenance, while keeping the usage as simple as possible.

Attention!

Environment

You need to make sure there are make, cmake, gcc command in your machine, otherwise should support C++17.

If you want to run on Windows OS, you can use cygwin or MinGW.

cmake > 3.8 and gcc > 5.1.0 (support C++17)

Not Support LoRA model

go-chatglm.cpp is not anymore compatible with LoRA model, but it woks ONLY with the model which merged by LoRA model and base model.

You can use convert.py in chatglm.cpp to merge LoRA model into base model.

Usage

Note: This repository uses git submodules to keep track of chatglm.cpp .

Clone the repository locally:

git clone --recurse-submodules https://github.com/Weaxs/go-chatglm.cpp

To build the bindings locally, run:

cd go-chatglm.cpp
make libbinding.a

Now you can run the example with:

go run ./examples -m "/model/path/here"
                    ____ _           _    ____ _     __  __                   
  __ _  ___        / ___| |__   __ _| |_ / ___| |   |  \/  |  ___ _ __  _ __  
 / _` |/ _ \ _____| |   | '_ \ / _` | __| |  _| |   | |\/| | / __| '_ \| '_ \ 
| (_| | (_) |_____| |___| | | | (_| | |_| |_| | |___| |  | || (__| |_) | |_) |
 \__, |\___/       \____|_| |_|\__,_|\__|\____|_____|_|  |_(_)___| .__/| .__/ 
 |___/                                                           |_|   |_|    

>>> 你好

Sending 你好


你好👋!我是人工智能助手 ChatGLM3-6B,很高兴见到你,欢迎问我任何问题。

Acceleration

Metal (Apple Silicon)

MPS (Metal Performance Shaders) allows computation to run on powerful Apple Silicon GPU.

BUILD_TYPE=metal make libbinding.a
go build -tags metal ./examples/main.go
./main -m "/model/path/here"

OpenBLAS

OpenBLAS provides acceleration on CPU.

BUILD_TYPE=openblas make libbinding.a
go build -tags openblas ./examples/main.go
./main -m "/model/path/here"

cuBLAS

cuBLAS uses NVIDIA GPU to accelerate BLAS.

BUILD_TYPE=cublas make libbinding.a
go build -tags cublas ./examples/main.go
./main -m "/model/path/here"

Acknowledgements

Documentation

Index

Constants

View Source
const (
	RoleUser        = "user"
	RoleAssistant   = "assistant"
	RoleSystem      = "system"
	RoleObservation = "observation"

	TypeFunction = "function"
	TypeCode     = "code"

	DELIMITER = "<|delimiter|>"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ChatMessage added in v0.1.1

type ChatMessage struct {
	Role      string
	Content   string
	ToolCalls []*ToolCallMessage
}

func NewAssistantMsg added in v0.1.1

func NewAssistantMsg(input string, modelType string) *ChatMessage

func NewObservationMsg added in v0.1.1

func NewObservationMsg(content string) *ChatMessage

func NewSystemMsg added in v0.1.1

func NewSystemMsg(content string) *ChatMessage

func NewUserMsg added in v0.1.1

func NewUserMsg(content string) *ChatMessage

type Chatglm

type Chatglm struct {
	// contains filtered or unexported fields
}

func New

func New(model string) (*Chatglm, error)

New create llm struct

func (*Chatglm) Chat

func (llm *Chatglm) Chat(messages []*ChatMessage, opts ...GenerationOption) (string, error)

Chat by history [synchronous]

func (*Chatglm) Embeddings

func (llm *Chatglm) Embeddings(text string, opts ...GenerationOption) ([]int, error)

Embeddings get text input_ids,

func (*Chatglm) Free

func (llm *Chatglm) Free()

func (*Chatglm) Generate

func (llm *Chatglm) Generate(prompt string, opts ...GenerationOption) (string, error)

Generate by prompt [synchronous]

func (*Chatglm) ModelType added in v0.1.1

func (llm *Chatglm) ModelType() string

func (*Chatglm) StreamChat

func (llm *Chatglm) StreamChat(messages []*ChatMessage, opts ...GenerationOption) (string, error)

StreamChat chat with stream output by StreamCallback

func (*Chatglm) StreamGenerate

func (llm *Chatglm) StreamGenerate(prompt string, opts ...GenerationOption) (string, error)

StreamGenerate with stream output by StreamCallback

type CodeMessage added in v0.1.1

type CodeMessage struct {
	Input string
}

type FunctionMessage added in v0.1.1

type FunctionMessage struct {
	Name      string
	Arguments string
}

type GenerationOption

type GenerationOption func(g *GenerationOptions)

func SetDoSample

func SetDoSample(doSample bool) GenerationOption

func SetMaxContextLength

func SetMaxContextLength(maxContextLength int) GenerationOption

func SetMaxLength

func SetMaxLength(maxLength int) GenerationOption

func SetNumThreads

func SetNumThreads(numThreads int) GenerationOption

func SetRepetitionPenalty

func SetRepetitionPenalty(repetitionPenalty float32) GenerationOption

func SetStreamCallback

func SetStreamCallback(callback func(string) bool) GenerationOption

func SetTemperature

func SetTemperature(temperature float32) GenerationOption

func SetTopK

func SetTopK(topK int) GenerationOption

func SetTopP

func SetTopP(topP float32) GenerationOption

type GenerationOptions

type GenerationOptions struct {
	MaxLength         int
	MaxContextLength  int
	DoSample          bool
	TopK              int
	TopP              float32
	Temperature       float32
	RepetitionPenalty float32
	NumThreads        int
	StreamCallback    func(string) bool
}
var DefaultGenerationOptions GenerationOptions = GenerationOptions{
	MaxLength:         2048,
	MaxContextLength:  512,
	DoSample:          true,
	TopK:              0,
	TopP:              0.7,
	Temperature:       0.95,
	RepetitionPenalty: 1.0,
	NumThreads:        0,
	StreamCallback:    nil,
}

func NewGenerationOptions

func NewGenerationOptions(opts ...GenerationOption) *GenerationOptions

type ToolCallMessage added in v0.1.1

type ToolCallMessage struct {
	Type     string
	Function *FunctionMessage
	Code     *CodeMessage
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL