mirror of
https://github.com/vxcontrol/langchaingo.git
synced 2026-08-24 21:31:28 -04:00
064f09155c
- Raised the `go` directive from 1.24.1 to 1.26.5 to satisfy pgx/v5 and ollama's minimum Go version requirements and to pick up 15 patched standard library CVEs. - Updated `pgx/v5`, `otel`/`otel/sdk`/`otlptracehttp`, `x/net`, and the AWS `eventstream` protocol package to their latest requested versions. - Remediated govulncheck-flagged CVEs in `grpc`, `x/text`, `x/crypto`, `antchfx/xpath`, AWS SDK `bedrock*`/`s3` services, `etcd/server`, and `mongo-driver` v1/v2. - Fixed ollama's `MainGPU` API type change (`int` -> `*int`) in `llms/ollama/options.go` without breaking the public `WithRunnerMainGPU` signature.
OpenRouter LLM Example
This example demonstrates how to use OpenRouter with langchaingo. OpenRouter provides a unified API to access various LLM models through a single endpoint.
About OpenRouter
OpenRouter is an AI routing service that provides:
- Access to multiple LLM providers (OpenAI, Anthropic, Google, Meta, etc.) through a single API
- Automatic failover and load balancing
- Usage tracking and analytics
- Support for both free and premium models
Setup
- Get an OpenRouter API key from https://openrouter.ai/
- Set the environment variable:
export OPENROUTER_API_KEY="your-api-key-here"
Usage
OpenRouter uses an OpenAI-compatible API, so you can use the OpenAI client with a custom base URL:
llm, err := openai.New(
openai.WithModel("meta-llama/llama-3.2-3b-instruct:free"),
openai.WithBaseURL("https://openrouter.ai/api/v1"),
openai.WithToken(apiKey),
)
Available Models
OpenRouter provides access to many models including:
- Free tier:
meta-llama/llama-3.2-3b-instruct:free,google/gemma-2-9b-it:free - Premium: GPT-4, Claude, Gemini Pro, and many more
- Check https://openrouter.ai/models for the full list
Running the Example
go run openrouter_llm_example.go
Features
- ✅ Streaming responses supported
- ✅ Compatible with OpenAI client
- ✅ Access to multiple model providers
- ✅ Automatic handling of provider-specific quirks