Skip to content
Artem Gafarov edited this page Oct 2, 2024 · 10 revisions

Yandex GPT Go framework

Neuron nexus YandexGPT is the public Go framework for Yandex GPT API, which is part of Yandex Foundation Models. Version 0.1.0 allows you to send requests to the synchronous Yandex GPT model and receive responses if you have an API key or Bearer key.

Usage (v2.0.0)

Single Message Mode

This mode is for sending a system pompt and a single message. It is NOT intended to create a dialog with Yandex GPT.

package main

import (
	"fmt"
	"github.com/neuron-nexus/yandexgpt/v2"
)

const (
	GPT_API_KEY = "AQVN***************"
	STORAGE_ID  = "b1*****************"
)

func main() {

    app := yandexgpt.NewYandexGPTSyncApp(
		GPT_API_KEY,
		yandexgpt.API_KEY,
		STORAGE_ID,
		yandexgpt.GPTModelPRO,
	)

	configs := []yandexgpt.GPTParameter{
		{
			Name:  yandexgpt.ParameterPrompt, // Important!
			Value: "You are professional Go programmer",
		},
		{
			Name:  yandexgpt.ParameterTemperature, // Default: 0.3
			Value: "0.7",
		},
		{
			Name:  yandexgpt.ParameterMaxTokens, // Default: 2000
			Value: "1000",
		},
	}

	app.Configure(configs...)
	message := yandexgpt.GPTMessage{
		Role: yandexgpt.RoleUser,
		Text: "Write a programm that prints 'Hello World'",
	}

	app.AddMessage(message)
	res, err := app.SendRequest()
	println(res.Text)
}

Result:

package main

import "fmt"

func main() {
    fmt.Println("Hello World")
}

Dialog Mode

A simple example of creating a dialog with Yandex GPT. It will allow you to communicate with the model inside the console.

package main

import (
	"bufio"
	"fmt"
	"github.com/neuron-nexus/yandexgpt/v2"
	"os"
	"strings"
)

const (
	GPT_API_KEY = "AQVN***************"
	STORAGE_ID  = "b1*****************"
)

func main() {
	app := yandexgpt.NewYandexGPTSyncApp(
		GPT_API_KEY,
		yandexgpt.API_KEY,
		STORAGE_ID,
		yandexgpt.GPTModelPRO,
	)

	configs := []yandexgpt.GPTParameter{
		{
			Name:  yandexgpt.ParameterPrompt, // Important!
			Value: "You are professional assistant",
		},
	}

	err := app.Configure(configs...)
	if err != nil {
		panic(err)
	}

	for {
		//read text from console
		fmt.Print("You: ")
		text, _ := bufio.NewReader(os.Stdin).ReadString('\n')

		if text == "exit" {
			return
		}

		app.AddMessage(yandexgpt.GPTMessage{
			Role: yandexgpt.RoleUser,
			Text: strings.TrimSpace(text),
		})
		res, _ := app.SendRequest()
		fmt.Println("Assistant: ", res.Text)
		app.AddRawMessage(res.Result.Alternatives[0].Message)
	}
}

Versions

v1.0.0 (same is v0.1.0)

Version 0.1.0 gives users the ability to use two models:

  • one message and one reply mode
  • dialog mode

v2.0.0

Version 2.0.0 reworked the structure of files inside the package, fixed some bugs and changed the structure of instance customization.

Also:

  • Default temperature is set to 0.3
  • Default maximum response length is set to 2000 tokens
  • Model selection moved to instance initialization
  • Removed “dialog mode”. Now the library does NOT automatically save the response from Yandex GPT to the message structure. This must be done independently
Clone this wiki locally