用 Go 玩转大语言模型:LangChainGo 一个 LangChain 的 Go 语言扩展
LangChainGo 是一个开源库(LangChain Go 语言的扩展,非官方库),提供了一系列工具和接口,方便开发者在 Go 语言环境中集成和操作大型语言模型(LLM)。其主要功能包括模型管理、上下文处理、对话管理等,旨在简化 LLM 应用的开发流程。
在使用 LangChainGo 时,需要了解以下核心概念:
- LLM(Large Language Model):大型语言模型,如 GPT-3、Ollama 等,能够理解和生成类似人类的文本。
- Prompt(提示):向模型提供的输入,指导模型生成期望的输出。
- Chain(链):将多个处理步骤串联起来,形成一个完整的处理流程。
安装 Ollama
因为下面的例子需要使用到 Ollama 所以需要安装对应的工具软件,具体操作如下所示:
打开浏览器,输入网址 https://ollama.com/download,下载对应平台的安装包进行安装。
拉取 Ollama 模型,根据自己的电脑配置拉取对应的模型,操作如下:
ollama pull llama3:8b
运行大模型服务,具体操作如下:
ollama run llama3:8b
看到下面输出说明成功运行:
⚠️ 注意: Ollama 的服务端口是 11434。
快速上手
首先创建项目,并添加依赖,操作如下所示:
mkdir llm
cd llm
go mod init llm
go get github.com/tmc/langchaingo
go get github.com/tmc/langchaingo/llms@v0.1.12
1. 基础问答
package main
import (
"context"
"fmt"
"github.com/tmc/langchaingo/llms/ollama"
)
func main() {
llm, err := ollama.New(ollama.WithModel("llama3:8b"))
if err != nil {
panic(err)
}
ctx := context.Background()
completion, err := llm.Call(ctx, "请用50字解释量子计算")
if err != nil {
panic(err)
}
fmt.Println("回答:", completion)
}
输出结果,如下所示:
go run qa.go
回答: Quantum computing is a new way of processing information that uses the principles of quantum mechanics to perform calculations. It's like a super-powerful calculator that can solve complex problems much faster than regular computers. Quantum computers use "qubits" (quantum bits) instead of regular bits, which allows them to process vast amounts of data simultaneously and make predictions with incredible accuracy.
2. 带参数的复杂调用
package main
import (
"context"
"fmt"
"github.com/tmc/langchaingo/llms"
"github.com/tmc/langchaingo/llms/ollama"
)
func main() {
llm, _ := ollama.New(ollama.WithModel("llama3:8b"))
// 使用 GenerateFromSinglePrompt
completion, err := llms.GenerateFromSinglePrompt(
context.Background(),
llm,
"请用50字解释量子计算",
llms.WithTemperature(0.7),
llms.WithMaxTokens(200),
)
if err != nil {
panic(err)
}
fmt.Println(completion)
}
输出结果,如下所示:
Quantum computing is a new way of processing information that uses the principles of quantum mechanics. It's like a super-powerful calculator that can solve complex problems much faster than regular computers. Quantum bits (qubits) are used to store and process information, allowing for exponential increases in processing power and memory.
3. 流式输出
package main
import (
"context"
"fmt"
"log"
"github.com/tmc/langchaingo/llms"
"github.com/tmc/langchaingo/llms/ollama"
)
func main() {
llm, err := ollama.New(ollama.WithModel("llama3:8b"))
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
completion, err := llm.Call(ctx, "Human: 什么是go语言?\nAssistant:",
llms.WithTemperature(0.8),
llms.WithStreamingFunc(func(ctx context.Context, chunk []byte) error {
fmt.Print(string(chunk))
return nil
}),
)
if err != nil {
log.Fatal(err)
}
_ = completion
}
输出结果,如下所示:
🚀 Go, also known as Golang, is a statically typed, compiled, and modern programming language developed by Google in 2009. It was designed to be efficient, simple, and easy to use.
Here are some key features of the Go language:
1. **Concurrency**: Go has built-in support for concurrency, making it easy to write programs that can handle multiple tasks simultaneously.
2. **Simple syntax**: Go's syntax is designed to be readable and easy to learn, with a focus on simplicity and minimal ceremony.
3. **Statically typed**: Go is statically typed, which means that the type of each variable is known at compile time, reducing errors and making it easier to maintain code.
4. **Compiled language**: Go programs are compiled to machine code, making them fast and efficient.
5. **Error handling**: Go has a robust error handling system, with built-in support for checking and propagating errors throughout the program.
6. **Libraries and frameworks**: Go has a growing ecosystem of libraries and frameworks that make it easy to build web applications, network protocols, and more.
Some popular use cases for Go include:
1. **Cloud computing**: Go is widely used in cloud computing environments, such as Google Cloud Platform, Amazon Web Services (AWS), and Microsoft Azure.
2. **Web development**: Go's concurrency features make it a great choice for building scalable web applications, APIs, and microservices.
3. **Network programming**: Go's networking libraries and concurrency features make it well-suited for building network protocols, proxies, and more.
Overall, Go is a modern language that emphasizes simplicity, concurrency, and performance, making it an attractive choice for building scalable and maintainable software systems. 💻
总结
LangChainGo 为 Go 语言开发者提供了一个简洁、高效的接口,方便地集成和操作大型语言模型。通过本文的介绍和示例,您可以快速上手,利用 Ollama 模型构建智能应用。未来,随着人工智能技术的不断发展,掌握并应用这些工具将为您的开发工作带来更多可能性。