Quickstart: LangChainGo with OpenAI
Dive right into executing your first program utilizing LangChainGo in tandem with OpenAI. OpenAI's GPT models are renowned for their proficiency and expansive capabilities.
Pre-requisites
- OpenAI API Key: Sign up on OpenAI and retrieve your API key.
- Go: Download and install Go.
Steps
- Set up your OpenAI API Key: Before interacting with the OpenAI API, ensure that you've set up your API key. Typically, this is done by setting an environment variable. In your terminal, run the command:
export OPENAI_API_KEY=your_openai_api_key_here
- Run the Example: Execute the following command:
go run github.com/tmc/langchaingo/examples/openai-completion-example@main
Anticipate an output similar to the one below:
The first man to walk on the moon was Neil
Congratulations! You've successfully built and executed your first LangChainGo LLM-backed program using OpenAI's cloud-based inference.
Here is the entire program (from openai-completion-example).
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)
}