Gemini models in Go via LangChainGo

Following an earlier guide on accessing Google's multimodal Gemini models from Go—which notably includes a generous free tier—Google's SDKs have now been integrated into LangChainGo as providers. This enables developers to combine LangChain's framework capabilities with Gemini models as the underlying LLM providers.

The new providers simplify usage across both Google AI, which relies on API keys, and Vertex, which requires a GCP project. With the latest release of langchaingo, a basic textual query can be handled through the llms.GenerateFromSinglePrompt convenience function, which takes a single string input and returns a single string output.

For more complex scenarios, the general Model.GenerateContent method supports multiple messages and various message types, including text and images. Developers can specify the desired model using the WithModel option passed to llm.GenerateContent, as demonstrated in an example reproducing a prior question about differences between turtle images. Additionally, embedding models are available via the same interface for calculating text embeddings.

Switching between providers is straightforward, as both implement identical LangChainGo interfaces. The transition from Google AI to Vertex requires only changing the import line, swapping the llm value creation, and leaving all other code untouched.

Full runnable code examples for both Google AI and Vertex are available on GitHub, providing a practical reference for those looking to integrate Gemini with LangChainGo.