Quickstart: LangChainGo with Mistral
Get started by running your first program with LangChainGo and the Mistral Platform.
Prerequisites
- Mistral API Key: Sign up on Mistral and retrieve your API key.
- Go: Download and install Go.
Setup
Before interacting with the Mistral API, you need to set up your API key as an environment variable.
Linux/macOS (bash/zsh)
export MISTRAL_API_KEY="your_mistral_api_key_here"
Windows (Command Prompt)
set MISTRAL_API_KEY=your_mistral_api_key_here
Windows (PowerShell)
$env:MISTRAL_API_KEY="your_mistral_api_key_here"
For permanent setup, add the environment variable to your shell's profile file (~/.bashrc
, ~/.zshrc
, etc.) or system environment variables on Windows.
Steps
-
Set up your Mistral API Key: Follow the setup instructions above to configure your API key.
-
Run the example: Execute the following command:
go run github.com/tmc/langchaingo/examples/mistral-completion-example@main
You should see output similar to the following:
The first man to walk on the moon was Neil Armstrong on July 20, 1969. He made this historic step during the Apollo 11 mission. Armstrong's famous quote upon setting foot on the lunar surface was, "That's one small step for man, one giant leap for mankind."
The first human to go to space was Yuri Gagarin, a Soviet cosmonaut. He completed an orbit around the Earth in the spacecraft Vostok 1 on April 12, 1961. This historic event marked the beginning of human space exploration.
Congratulations! You have successfully built and executed your first LangChainGo LLM-backed program using Mistral's cloud-based inference.
Here is the entire program (from mistral-completion-example):
package main
import (
"context"
"fmt"
"log"
"github.com/tmc/langchaingo/llms"
"github.com/tmc/langchaingo/llms/mistral"
)
func main() {
llm, err := mistral.New(mistral.WithModel("open-mistral-7b"))
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
completionWithStreaming, err := llms.GenerateFromSinglePrompt(ctx, llm, "Who was the first man to walk on the moon?",
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)
}
// The full string response will be available in completionWithStreaming after the streaming is complete.
// (The Go compiler mandates declared variables be used at least once, hence the `_` assignment. https://go.dev/ref/spec#Blank_identifier)
_ = completionWithStreaming
completionWithoutStreaming, 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" + completionWithoutStreaming)
}