Files
langchain-go/main.go
T

56 lines
850 B
Go
Raw Normal View History

2026-03-22 20:54:09 +08:00
package main
import (
2026-03-22 21:12:29 +08:00
"context"
2026-03-22 20:54:09 +08:00
"fmt"
2026-03-22 21:12:29 +08:00
"log"
2026-03-22 20:54:09 +08:00
"os"
"github.com/joho/godotenv"
2026-03-22 21:12:29 +08:00
"github.com/tmc/langchaingo/llms"
"github.com/tmc/langchaingo/llms/openai"
)
var (
apiKey string
baseUrl string
model = "stepfun-ai/Step-3.5-Flash"
2026-03-22 20:54:09 +08:00
)
func init() {
err := godotenv.Load()
if err != nil {
fmt.Println(".env 文件加载失败")
}
2026-03-22 21:12:29 +08:00
apiKey = os.Getenv("API_KEY")
baseUrl = os.Getenv("BASE_URL")
2026-03-22 20:54:09 +08:00
fmt.Printf("API Key: %s\n", apiKey)
fmt.Printf("Base URL: %s\n", baseUrl)
}
func main() {
2026-03-22 21:12:29 +08:00
llm, err := openai.New(
openai.WithBaseURL(baseUrl),
openai.WithToken(apiKey),
openai.WithModel(model),
)
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
response, err := llms.GenerateFromSinglePrompt(
ctx,
llm,
"Hello! My name is wonder!",
)
if err != nil {
log.Fatal(err)
}
fmt.Println("AI:", response)
2026-03-22 20:54:09 +08:00
}