Sitemap

🚀 Exploring the Universe of LangchainGo with Hugging Face: A Go Adventure! 🌌

2 min readMar 2, 2024
Press enter or click to view image in full size

Hey there, folks! 🎉 Today, we’re diving into the magical world of AI models with a cool twist: we’re talking about using LangchainGo with the famous Hugging Face! Yes, that’s right, we’re bringing artificial intelligence to the realm of Go!

First things first: you’re going to need a secret key! 🗝️ Make sure you have your Hugging Face token ready and stored in the HUGGINGFACEHUB_API_TOKEN variable. I mean, better to have everything set up to avoid any headaches later!

Now, hold onto your hats: we’re going to add some libraries to our Go project. Open the terminal and let’s go:

go get github.com/tmc/langchaingo

All set, now that we have what we need, let’s dive into the code! 🧑‍💻

package main

import (
"context"
"fmt"
"log"

"github.com/tmc/langchaingo/llms"
"github.com/tmc/langchaingo/llms/huggingface"
)

func main() {
llm, err := huggingface.New(huggingface.WithModel("google/gemma-7b"))

Here, we’re bringing our Hugging Face model to life, choosing the model we’re going to use. Now, let’s move on to the fun part: making magic happen with a simple prompt!

And here’s the grand finale, the code that will bring answers from beyond:

package main

import (
"context"
"fmt"
"log"

"github.com/tmc/langchaingo/llms"
"github.com/tmc/langchaingo/llms/huggingface"
)

func main() {

llm, err := huggingface.New(huggingface.WithModel("google/gemma"))
if err != nil {
log.Fatal(err)
}

ctx := context.Background()

prompt := "What is Golang?"
completion, err := llms.GenerateFromSinglePrompt(ctx, llm, prompt)

if err != nil {
log.Fatal(err)
}

fmt.Println(completion)

}

And that’s all, folks! 🎈 With these steps, you’ll have everything you need to start playing with LangchainGo and Hugging Face. Get ready to create amazing things and have a lot of fun along the way! 🌟

Remember: programming is a journey, so enjoy every step and don’t be afraid to experiment. Until our next adventure! 🚀

Linkedin

--

--

Alexandre E Souza
Alexandre E Souza

Written by Alexandre E Souza

Microservice Evangelist | Go Lover | JS Lover

Responses (1)