yandexgpt

package module
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2025 License: MIT Imports: 6 Imported by: 3

README

✨ Go YandexGPT ✨

Release CI Go Reference Go Report Card

This library provides unofficial Go client for YandexGPT API.

Installation

go get github.com/sheeiavellie/go-yandexgpt@latest

Currently, go-yandexgpt requires Go version 1.22 or greater.

Usage

package main

import (
	"context"
	"fmt"
	"github.com/sheeiavellie/go-yandexgpt"
)

func main() {
	client := yandexgpt.NewYandexGPTClientWithAPIKey("apiKey")
	request := yandexgpt.YandexGPTRequest{
		ModelURI: yandexgpt.MakeModelURI("catalogID", yandexgpt.YandexGPTModelLite),
		CompletionOptions: yandexgpt.YandexGPTCompletionOptions{
			Stream:      false,
			Temperature: 0.7,
			MaxTokens:   2000,
		},
		Messages: []yandexgpt.YandexGPTMessage{
			{
				Role: yandexgpt.YandexGPTMessageRoleSystem,
				Text: "Every time you get ONE you answer just TWO",
			},
			{
				Role: yandexgpt.YandexGPTMessageRoleUser,
				Text: "ONE",
			},
		},
	}

	response, err := client.GetCompletion(context.Background(), request)
	if err != nil {
		fmt.Println("Request error")
		return
	}

	fmt.Println(response.Result.Alternatives[0].Message.Text)
}

Getting an API Key/IAM token:

You can get all the necessary information from the official documentation.

Contribution guideline

Contributing

You can contribute by:

  • Reporting issues
  • Suggesting new features and enhancements
  • Improving documentation

For minor changes you can just send a PR without opening linked issue.

For major changes open an issue.

Commits and PRs

I highly encourage using conventional commits style in commit messages.

For PR titles it is required to use conventional commits style titles.

You can use any of these prefixes:

  • fix
  • feat
  • chore
  • refactor
  • test
  • ci

Credits 🖼️

Acknowledgement

Thank you very much

This project was highly inspired by go-openai by sashabaranov


Made with 💖 and some wizardry 🧙🔮

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	TextSearchDoc   = embeddingsModel{/* contains filtered or unexported fields */}
	TextSearchQuery = embeddingsModel{/* contains filtered or unexported fields */}
)
View Source
var (
	// Yandex GPT Pro 3rd generation
	YandexGPTModel = yandexGPTModel{/* contains filtered or unexported fields */}
	// Yandex GPT Lite 3rd generation
	YandexGPTModelLite = yandexGPTModel{/* contains filtered or unexported fields */}
	// Yandex GPT Pro 4th generation
	YandexGPT4Model = yandexGPTModel{/* contains filtered or unexported fields */}
	// Yandex GPT Lite 4th generation
	YandexGPT4ModelLite = yandexGPTModel{/* contains filtered or unexported fields */}
	// Yandex GPT Pro 32k 4th generation
	YandexGPT4Model32k = yandexGPTModel{/* contains filtered or unexported fields */}
	// Llama Lite 3rd generation
	LLAMA3Lite = yandexGPTModel{/* contains filtered or unexported fields */}
	// Llama  3rd generation
	LLAMA3 = yandexGPTModel{/* contains filtered or unexported fields */}
)
View Source
var (
	YandexGPTMessageRoleSystem    = yandexGPTRole{Role: "system"}
	YandexGPTMessageRoleUser      = yandexGPTRole{Role: "user"}
	YandexGPTMessageRoleAssistant = yandexGPTRole{Role: "assistant"}
)

Functions

func MakeEmbModelURI added in v1.6.0

func MakeEmbModelURI(catalogID string, model embeddingsModel) string

Use this for creating model uri for embeddings.

func MakeModelURI

func MakeModelURI(catalogID string, model yandexGPTModel) string

Use this for creating model uri.

Types

type DetailsResponse added in v1.5.0

type DetailsResponse struct {
	Type      string `json:"@type"`
	RequestID string `json:"requestId"`
}

type EmbeddingResponse added in v1.6.0

type EmbeddingResponse struct {
	Embedding    []float64 `json:"embedding"`
	NumTokens    string    `json:"numTokens"`
	ModelVersion string    `json:"modelVersion"`
	// contains filtered or unexported fields
}

func (*EmbeddingResponse) SetHeader added in v1.6.0

func (h *EmbeddingResponse) SetHeader(header http.Header)

type FunctionCall added in v1.5.0

type FunctionCall struct {
	Name      string `json:"name"`
	Arguments any    `json:"arguments"`
}

type MetadataResponse added in v1.5.0

type MetadataResponse struct {
	Type   string `json:"@type"`
	DiskID string `json:"diskId"`
}

type OperationResponse added in v1.5.0

type OperationResponse struct {
	ID          string            `json:"id"`
	Description string            `json:"description"`
	CreatedAt   string            `json:"createdAt"`
	CreatedBy   string            `json:"createdBy"`
	ModifiedAt  string            `json:"modifiedAt"`
	Done        bool              `json:"done"`
	Metadata    *MetadataResponse `json:"metadata"`
	Error       *StatusResponse   `json:"error"`
	Response    *YandexResponse   `json:"response"`
	// contains filtered or unexported fields
}

func (*OperationResponse) SetHeader added in v1.5.0

func (h *OperationResponse) SetHeader(header http.Header)

type Response

type Response interface {
	SetHeader(http.Header)
}

type StatusResponse added in v1.5.0

type StatusResponse struct {
	Code    int             `json:"code"`
	Message string          `json:"message"`
	Details DetailsResponse `json:"details"`
}

type ToolCallList added in v1.5.0

type ToolCallList struct {
	ToolCalls []FunctionCall `json:"toolCalls"`
}

type YandexCompletionResponse added in v1.5.0

type YandexCompletionResponse struct {
	ID          string            `json:"id"`
	Description string            `json:"description"`
	CreatedAt   string            `json:"createdAt"`
	ModifiedAt  string            `json:"modifiedAt"`
	Done        bool              `json:"done"`
	Metadata    *MetadataResponse `json:"metadata"`
	Error       *StatusResponse   `json:"error"`
	Response    string            `json:"response"`
	// contains filtered or unexported fields
}

func (*YandexCompletionResponse) SetHeader added in v1.5.0

func (h *YandexCompletionResponse) SetHeader(header http.Header)

type YandexFunctionResult added in v1.5.0

type YandexFunctionResult struct {
	Name    string `json:"name"`
	Content string `json:"content"`
}

type YandexGPTAlternative

type YandexGPTAlternative struct {
	Message        YandexGPTMessage     `json:"message"`
	ToolResultList YandexToolResultList `json:"toolResultList"`
	Status         string               `json:"status"`
}

type YandexGPTClient

type YandexGPTClient struct {
	// contains filtered or unexported fields
}

YandexGPT Client

func NewYandexGPTClient

func NewYandexGPTClient() *YandexGPTClient

Creates new YandexGPT Client.

func NewYandexGPTClientWithAPIKey

func NewYandexGPTClientWithAPIKey(
	apiKey string,
) *YandexGPTClient

Creates new YandexGPT Client.

You will need to specify your own API key.

func NewYandexGPTClientWithIAMToken

func NewYandexGPTClientWithIAMToken(
	iamToken string,
) *YandexGPTClient

Creates new YandexGPT Client.

If you're using this option, keep in mind that you will need to generate IAM token yourself.

func NewYandexGPTClientWithOAuthToken added in v1.5.0

func NewYandexGPTClientWithOAuthToken(
	oauthToken string,
) *YandexGPTClient

Creates new YandexGPT Client.

You will need to specify your own OAuth key.

func (*YandexGPTClient) GetCompletion added in v1.5.0

func (c *YandexGPTClient) GetCompletion(
	ctx context.Context,
	request YandexGPTRequest,
) (response YandexGPTResponse, err error)

Get completion from YandexGPT.

If you're using IAM token, make sure to update client's IAM token by calling GetIAMToken(iamToken string) method first.

Keep in mind that if for some strange reason you provided API key and IAM token to the client, this method will use API key.

func (*YandexGPTClient) GetEmbedding added in v1.6.0

func (c *YandexGPTClient) GetEmbedding(
	ctx context.Context,
	request YandexGPTEmbeddingsRequest,
) (response EmbeddingResponse, err error)

Get embeddings from Yandex foundation models.

If you're using IAM token, make sure to update client's IAM token by calling GetIAMToken(iamToken string) method first.

Keep in mind that if for some strange reason you provided API key and IAM token to the client, this method will use API key.

func (*YandexGPTClient) GetIAMToken added in v1.5.0

func (c *YandexGPTClient) GetIAMToken(ctx context.Context) error

Updates IAM token.

Always call it before creating a request.

If you will use it when API key is specified, method GetCompletion(...) will always use API key.

func (*YandexGPTClient) GetOperationStatus added in v1.5.0

func (c *YandexGPTClient) GetOperationStatus(
	ctx context.Context,
	operationID string,
) (response OperationResponse, err error)

Get operation status from yandex cloud. Use it if you are working with yandex api via async methods.

If you're using IAM token, make sure to update client's IAM token by calling GetIAMToken(iamToken string) method first.

Keep in mind that if for some strange reason you provided API key and IAM token to the client, this method will use API key.

func (*YandexGPTClient) RunCompletionAsync added in v1.5.0

func (c *YandexGPTClient) RunCompletionAsync(
	ctx context.Context,
	request YandexGPTRequest,
) (response YandexCompletionResponse, err error)

Get completion from YandexGPT with async method.

If you're using IAM token, make sure to update client's IAM token by calling GetIAMToken(iamToken string) method first.

Keep in mind that if for some strange reason you provided API key and IAM token to the client, this method will use API key.

type YandexGPTClientConfig

type YandexGPTClientConfig struct {
	OAuthToken string
	ApiKey     string
	IAMToken   string
	HTTPClient *http.Client
}

func NewYandexGPTClientConfig

func NewYandexGPTClientConfig() *YandexGPTClientConfig

func NewYandexGPTClientConfigWithAPIKey

func NewYandexGPTClientConfigWithAPIKey(
	apiKey string,
) *YandexGPTClientConfig

func NewYandexGPTClientConfigWithIAMToken

func NewYandexGPTClientConfigWithIAMToken(
	iamToken string,
) *YandexGPTClientConfig

func NewYandexGPTClientConfigWithOAuthToken added in v1.5.0

func NewYandexGPTClientConfigWithOAuthToken(
	oauthToken string,
) *YandexGPTClientConfig

func (*YandexGPTClientConfig) SetIAMToken added in v1.5.0

func (c *YandexGPTClientConfig) SetIAMToken(iamToken string)

Setter for IAM token in config.

Use it for manually updating token in config.

type YandexGPTCompletionOptions

type YandexGPTCompletionOptions struct {
	Stream      bool    `json:"stream"`
	Temperature float32 `json:"temperature"`
	MaxTokens   int     `json:"maxTokens"`
}

type YandexGPTEmbeddingsRequest added in v1.6.0

type YandexGPTEmbeddingsRequest struct {
	ModelURI string `json:"modelUri"`
	Text     string `json:"text"`
}

type YandexGPTError

type YandexGPTError struct {
	HTTPCode   int             `json:"httpCode"`
	Message    string          `json:"message"`
	HTTPStatus string          `json:"httpStatus"`
	Details    DetailsResponse `json:"error.details"`
}

type YandexGPTMessage

type YandexGPTMessage struct {
	Role         yandexGPTRole `json:"role"`
	Text         string        `json:"text"`
	ToolCallList *ToolCallList `json:"toolCallList"`
}

type YandexGPTRequest

type YandexGPTRequest struct {
	ModelURI          string                     `json:"modelUri"`
	CompletionOptions YandexGPTCompletionOptions `json:"completionOptions"`
	Messages          []YandexGPTMessage         `json:"messages"`
}

type YandexGPTResponse

type YandexGPTResponse struct {
	Result YandexGPTResult `json:"result"`
	// contains filtered or unexported fields
}

func (*YandexGPTResponse) SetHeader

func (h *YandexGPTResponse) SetHeader(header http.Header)

type YandexGPTResponseBad

type YandexGPTResponseBad struct {
	Error YandexGPTError `json:"error"`
}

type YandexGPTResult

type YandexGPTResult struct {
	Alternatives []YandexGPTAlternative `json:"alternatives"`
	Usage        YandexGPTUsage         `json:"usage"`
	ModelVersion string                 `json:"modelVersion"`
}

type YandexGPTUsage

type YandexGPTUsage struct {
	InputTokens      string `json:"inputTextTokens"`
	CompletionTokens string `json:"completionTokens"`
	TotalTokens      string `json:"totalTokens"`
}

type YandexIAMRequest added in v1.5.0

type YandexIAMRequest struct {
	OAuthToken string `json:"yandexPassportOauthToken"`
}

type YandexIAMResponse added in v1.5.0

type YandexIAMResponse struct {
	IAMToken  string    `json:"iamToken"`
	ExpiresAt time.Time `json:"expiresAt"`
	// contains filtered or unexported fields
}

func (*YandexIAMResponse) SetHeader added in v1.5.0

func (h *YandexIAMResponse) SetHeader(header http.Header)

type YandexResponse added in v1.5.0

type YandexResponse struct {
	Type         string                 `json:"@type"`
	Alternatives []YandexGPTAlternative `json:"alternatives"`
}

type YandexToolResultList added in v1.5.0

type YandexToolResultList struct {
	ToolResults []YandexFunctionResult `json:"toolResults"`
}

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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