langchaingo
langchaingo是一个非官方的go版本langchain,通过它可以快速构建起llm应用。支持ChatGPT,Gemini,同时也支持Ollama。github:github.com/tmc/langcha… 文档:tmc.github.io/langchaingo…
使用langchaingo调用Ollama
前提:本地部署了Ollama,部署教程可直接访问github.com/ollama/olla…
示例代码
package llm
import (
"context"
"fmt"
"github.com/tmc/langchaingo/llms"
"github.com/tmc/langchaingo/llms/ollama"
"log"
"testing"
)
func TestLocalLlm(T *testing.T) {
llm, err := ollama.New(ollama.WithModel("llama2"))
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
completion, err := llms.GenerateFromSinglePrompt(
ctx,
llm,
"Who is Xabi Alonso?\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
}
输出
=== RUN TestLocalLlm
Xabi Alonso is a former Spanish professional footballer who played as a central midfielder for various clubs, including Liverpool and Real Madrid. He was born on November 25, 1981, in Tolosa, Spain, and began his professional career with Real Sociedad in 1998.
Alonso was known for his exceptional passing ability, vision, and control on the pitch. He was also an excellent tackler and interceptor, which made him a valuable asset in both attacking and defending phases of the game. Throughout his career, he gained a reputation as one of the most consistent and reliable midfielders in the world.
Alonso played for Liverpool from 2004 to 2014, helping the club win the Champions League, FA Cup, and League Cup during his time there. He then moved to Real Madrid, where he won two more Champions Leagues before retiring in 2017.
In addition to his club success, Alonso was a key player for Spain throughout his career, winning the World Cup in 2010 and the European Championship in 2008 and 2012. He retired from international football in 2014 with 113 caps for his country.
Overall, Xabi Alonso is considered one of the greatest midfielders of his generation, known for his exceptional technical ability, work rate, and leadership qualities on the pitch.--- PASS: TestLocalLlm (33.88s)
PASS
Process finished with the exit code 0
可以看出成功返回!