yandexgpt

package module
v2.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 2, 2024 License: MIT Imports: 6 Imported by: 0

README

Neuron Nexus Yandex GPT

Neuron Nexus Yandex GPT is a Go framework for interacting with the Yandex GPT API. Read More in Wiki.

Installation

go get -u github.com/neuron-nexus/yandexgpt/v2

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)
	}
}

Templates

For using templates import github.com/neuron-nexus/yandexgpt/v2/templates

Usage
	templates := templates.NewTemplateList()
	filepath := "templates.csv"

	templates.Add("test3",
		yandexgpt.GPTMessage{
			Role: yandexgpt.RoleUser,
			Text: "test3",
		})
	templates.Add("test4",
		yandexgpt.GPTMessage{
			Role: yandexgpt.RoleUser,
			Text: "test4",
		})
	templates.Add("test5",
		yandexgpt.GPTMessage{
			Role: yandexgpt.RoleUser,
			Text: "test5",
		})
	templates.ToCSV(filepath) // Save to CSV file
	templates.FromCSV(filepath) // Load from CSV file

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	GPTModelLite = GPTModel{ModelName: "yandexgpt-lite/latest"}
	GPTModelPRO  = GPTModel{ModelName: "yandexgpt/latest"}

	API_KEY = KeyType{"Api-Key"}
	Bearer  = KeyType{"Bearer"}

	RoleUser      = RoleModel{RoleName: "user"}
	RoleAssistant = RoleModel{RoleName: "assistant"}

	ParameterTemperature = GPTParameterName{Name: "temperature"}
	ParameterPrompt      = GPTParameterName{Name: "prompt"}
	ParameterMaxTokens   = GPTParameterName{Name: "max_tokens"}
)

Functions

This section is empty.

Types

type GPTMessage

type GPTMessage struct {
	Role RoleModel
	Text string
}

type GPTModel

type GPTModel struct {
	ModelName string
}

GPTModel - models of yandexGPT

func (*GPTModel) String

func (t *GPTModel) String() string

type GPTParameter

type GPTParameter struct {
	Name  GPTParameterName
	Value string
}

type GPTParameterName

type GPTParameterName struct {
	Name string
}

func (*GPTParameterName) String

func (n *GPTParameterName) String() string

type KeyType

type KeyType struct {
	KeyType string
}

KeyType - model of key types

func (*KeyType) String

func (t *KeyType) String() string

type Response

type Response struct {
	Result model.Result
	Text   string
}

type RoleModel

type RoleModel struct {
	RoleName string
}

RoleModel - model of roles

func (*RoleModel) String

func (m *RoleModel) String() string

type YandexGPTSyncApp

type YandexGPTSyncApp struct {
	App           *endpoint.App
	SystemMessage model.Message
	Message       []model.Message

	Response Response

	DebugMode bool
}

func NewYandexGPTSyncApp

func NewYandexGPTSyncApp(Key string, KeyType KeyType, StorageID string, Model GPTModel) *YandexGPTSyncApp

func (*YandexGPTSyncApp) AddMessage

func (p *YandexGPTSyncApp) AddMessage(Message GPTMessage) error

func (*YandexGPTSyncApp) AddRawMessage

func (p *YandexGPTSyncApp) AddRawMessage(Message model.Message) error

Unsafe: AddRawMessage is unsafe function. Use AddMessage(Message GPTMessage)

func (*YandexGPTSyncApp) ChangeCredentials

func (p *YandexGPTSyncApp) ChangeCredentials(Key string, KeyType KeyType)

func (*YandexGPTSyncApp) ClearMessages

func (p *YandexGPTSyncApp) ClearMessages()

func (*YandexGPTSyncApp) Configure

func (p *YandexGPTSyncApp) Configure(Parameters ...GPTParameter) error

func (*YandexGPTSyncApp) SendRequest

func (p *YandexGPTSyncApp) SendRequest() (Response, error)

func (*YandexGPTSyncApp) SetMessages

func (p *YandexGPTSyncApp) SetMessages(Messages ...GPTMessage) error

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL