LangChainGo + Ollama, OpenAI, Mistral 快速构建大模型应用

本文带你使用 LangChainGo 和 Ollama[1]OpenAI[2] , Mistra[3]构建大模型应用。Ollama 提供了在所有计算机平台上进行本地 LLM 推理的最简单方法。

Pre-requisites

  • Ollama: 下载并安装 Ollama[4].
  • OpenAI: 准备 OpenAI 的 API key
  • Go: 下载并安装 Go[5].

Steps

  1. 初始化 Ollama:在 Terminal中执行命令 ollama run llama2。第一次运行可能需要一些时间,因为模型需要从网络上下载到本地。

  2. 运行测试

go run github.com/tmc/langchaingo/examples/ollama-completion-example@main

一切正常的话会输出:

The first human to set foot on the moon was Neil Armstrong, an American as
tronaut, who stepped onto the lunar surface during the Apollo 11 mission 
on July 20, 1969.

至此,我们已成功构建并执行了使用本地推理的第一个开源 LLM(Language Model)程序。

langchaingo + ollama

现在我们就可以用 langchaingo 来调用 llama 推力模型 来实现大模型的应用了


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("llama2"))
    if err != nil {
        log.Fatal(err)
    }
    ctx := context.Background()
    completion, err := llm.Call(ctx, "Human: Who was the first man to walk on the moon?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
}

langchaingo + OpenAI

如果你有 OpenAI 的 Key ,可以直接将模型换成 OpenAI:

package main

import (
    "context"
    "fmt"
    "log"

    "github.com/tmc/langchaingo/llms"
    "github.com/tmc/langchaingo/llms/openai"
)

func main() {
    llm, err := openai.New()
    if err != nil {
        log.Fatal(err)
    }
    ctx := context.Background()
    completion, err := llm.Call(ctx, "The first man to walk on the moon",
        llms.WithTemperature(0.8),
        llms.WithStopWords([]string{"Armstrong"}),
    )
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(completion)
}

langchaingo + Mistral

配置 MISTRAL_API_KEY 之后,我们可以快速把模型换成 Mistra[6]:

package main

import (
    "context"
    "fmt"
    "log"

    "github.com/tmc/langchaingo/llms"
    "github.com/tmc/langchaingo/llms/mistral"
)

func main() {
    llm, err := mistral.New()
    if err != nil {
        log.Fatal(err)
    }
    ctx := context.Background()
    completion, err := llms.GenerateFromSinglePrompt(ctx, llm, "Who was the first man to go to space?",
        llms.WithTemperature(0.2),
        llms.WithModel("mistral-small-latest"),
    )
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("n" + completion)
}
参考资料
[1]

ollama: https://ollama.ai/

[2]

OpenAI: https://openai.com/

[3]

mistra: https://mistral.ai/

[4]

install ollama: https://ollama.ai/

[5]

install go: https://go.dev/doc/install

[6]

Mistra: https://mistral.ai/


原文始发于微信公众号(Go Official Blog):LangChainGo + Ollama, OpenAI, Mistral 快速构建大模型应用

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/286990.html

(0)
土豆大侠的头像土豆大侠

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!