groq

package module
v0.0.0-...-3936d6b Latest Latest
Warning

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

Go to latest
Published: May 15, 2024 License: MIT Imports: 10 Imported by: 0

README

groq.com golang library

Description

github.com/jpoz/groq is community maintained Go library for the groq.com API.

Installation

To install groq, run the following command in your terminal:

go get -u github.com/jpoz/groq
Features
  • Synchronous Chat completions
  • Streaming Chat completions
  • Zero dependencies
Examples
client := groq.NewClient() // will load API key from GROQ_API_KEY environment variable
client := groq.NewClient(WithAPIKey("YOUR_API_KEY"))

response, err := client.CreateChatCompletion(groq.CompletionCreateParams{
    Model: "llama3-8b-8192",
    Messages: []groq.Message{
        {
            Role:    "user",
            Content: "What is the meaning of life?",
        },
    },
})
if err != nil {
    panic(err)
}

println(response.Choices[0].Message.Content)
Streaming chat completions
client := groq.NewClient(WithAPIKey("YOUR_API_KEY"))

chatCompletion, err := client.CreateChatCompletion(groq.CompletionCreateParams{
    Model: "llama3-70b-8192",
    Messages: []groq.Message{
        {
            Role:    "user",
            Content: "What is the meaning of life?",
        },
    },
    Stream:      true,
})
if err != nil {
    panic(err)
}

for delta := range chatCompletion.Stream {
    fmt.Print(delta.Choices[0].Delta.Content)
}

fmt.Print("\n")

Documentation

Index

Constants

View Source
const (
	TimestampGranularity_Word    = "word"
	TimestampGranularity_Segment = "segment"
)
View Source
const (
	ResponseFormatString_JSON        = "json"
	ResponseFormatString_Text        = "text"
	ResponseFormatString_Srt         = "srt"
	ResponseFormatString_VerboseJSON = "verbose_json"
	ResponseFormatString_Vtt         = "vtt"
)
View Source
const TranslationModel_WhisperLargeV3 = "whisper-large-v3"

Variables

This section is empty.

Functions

This section is empty.

Types

type ChatChunkCompletion

type ChatChunkCompletion struct {
	ID                *string       `json:"id"`
	Choices           []ChoiceChunk `json:"choices"`
	Created           *int          `json:"created"`
	Model             *string       `json:"model"`
	Object            *string       `json:"object"`
	SystemFingerprint *string       `json:"systemFingerprint"`
	XGroq             *XGroq        `json:"xGroq,omitempty"`
	// contains filtered or unexported fields
}

ChatCompletion struct to represent the overall completion

type ChatCompletion

type ChatCompletion struct {
	ID                string   `json:"id"`
	Choices           []Choice `json:"choices"`
	Created           int      `json:"created"`
	Model             string   `json:"model"`
	Object            string   `json:"object"`
	SystemFingerprint string   `json:"systemFingerprint"`
	Usage             Usage    `json:"usage,omitempty"`

	Stream chan *ChatChunkCompletion `json:"-"`
}

ChatCompletion struct to represent the overall completion

type Choice

type Choice struct {
	FinishReason string         `json:"finishReason"`
	Index        int            `json:"index"`
	Logprobs     ChoiceLogprobs `json:"logprobs"`
	Message      ChoiceMessage  `json:"message"`
}

Choice struct to handle individual choices in a completion

type ChoiceChunk

type ChoiceChunk struct {
	Delta        ChoiceDelta    `json:"delta"`
	FinishReason string         `json:"finishReason"`
	Index        int            `json:"index"`
	Logprobs     ChoiceLogprobs `json:"logprobs"`
}

Choice struct for handling choices in completion chunks

type ChoiceDelta

type ChoiceDelta struct {
	Content      string                   `json:"content"`
	Role         string                   `json:"role"`
	FunctionCall *ChoiceDeltaFunctionCall `json:"functionCall,omitempty"`
	ToolCalls    []ChoiceDeltaToolCall    `json:"toolCalls,omitempty"`
}

ChoiceDelta struct for deltas within choices

type ChoiceDeltaFunctionCall

type ChoiceDeltaFunctionCall struct {
	Arguments *string `json:"arguments,omitempty"`
	Name      *string `json:"name,omitempty"`
}

ChoiceDeltaFunctionCall struct for details of function calls

type ChoiceDeltaToolCall

type ChoiceDeltaToolCall struct {
	Index    int                         `json:"index"`
	ID       *string                     `json:"id,omitempty"`
	Function ChoiceDeltaToolCallFunction `json:"function,omitempty"`
	Type     *string                     `json:"type,omitempty"`
}

ChoiceDeltaToolCall struct for tool call details within deltas

type ChoiceDeltaToolCallFunction

type ChoiceDeltaToolCallFunction struct {
	Arguments *string `json:"arguments,omitempty"`
	Name      *string `json:"name,omitempty"`
}

ChoiceDeltaToolCallFunction struct for tool call function details

type ChoiceLogprobs

type ChoiceLogprobs struct {
	Content []ChoiceLogprobsContent `json:"content,omitempty"`
}

ChoiceLogprobs struct to contain multiple log probability contents

type ChoiceLogprobsContent

type ChoiceLogprobsContent struct {
	Token       *string                           `json:"token,omitempty"`
	Bytes       []int                             `json:"bytes,omitempty"`
	Logprob     *float64                          `json:"logprob,omitempty"`
	TopLogprobs []ChoiceLogprobsContentTopLogprob `json:"topLogprobs,omitempty"`
}

ChoiceLogprobsContent struct to handle content log probabilities

type ChoiceLogprobsContentTopLogprob

type ChoiceLogprobsContentTopLogprob struct {
	Token   *string  `json:"token,omitempty"`
	Bytes   []int    `json:"bytes,omitempty"`
	Logprob *float64 `json:"logprob,omitempty"`
}

ChoiceLogprobsContentTopLogprob struct to represent detailed log probabilities

type ChoiceMessage

type ChoiceMessage struct {
	Content   string                  `json:"content"`
	Role      string                  `json:"role"`
	ToolCalls []ChoiceMessageToolCall `json:"toolCalls,omitempty"`
}

ChoiceMessage struct for messages within choices

type ChoiceMessageToolCall

type ChoiceMessageToolCall struct {
	ID       *string                       `json:"id,omitempty"`
	Function ChoiceMessageToolCallFunction `json:"function,omitempty"`
	Type     *string                       `json:"type,omitempty"`
}

ChoiceMessageToolCall struct to represent a tool call in a message

type ChoiceMessageToolCallFunction

type ChoiceMessageToolCallFunction struct {
	Arguments *string `json:"arguments,omitempty"`
	Name      *string `json:"name,omitempty"`
}

ChoiceMessageToolCallFunction struct to describe the function called by the tool

type Client

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

func NewClient

func NewClient(opts ...ClientOption) *Client

func (*Client) CreateChatCompletion

func (c *Client) CreateChatCompletion(params CompletionCreateParams) (*ChatCompletion, error)

func (*Client) CreateTranscription

func (c *Client) CreateTranscription(params TranscriptionCreateParams) (*ChatCompletion, error)

type ClientOption

type ClientOption func(*Client)

func WithAPIKey

func WithAPIKey(apiKey string) ClientOption

func WithDebug

func WithDebug(debug bool) ClientOption

type CompletionCreateParams

type CompletionCreateParams struct {
	Messages         []Message      `json:"messages"`
	Model            string         `json:"model"`
	FrequencyPenalty float32        `json:"frequency_penalty,omitempty"`
	LogitBias        map[string]int `json:"logit_bias,omitempty"`
	Logprobs         bool           `json:"logprobs,omitempty"`
	MaxTokens        int            `json:"max_tokens,omitempty"`
	N                int            `json:"n,omitempty"`
	PresencePenalty  float32        `json:"presence_penalty,omitempty"`
	ResponseFormat   ResponseFormat `json:"response_format,omitempty"`
	Seed             int            `json:"seed,omitempty"`
	Stop             []string       `json:"stop,omitempty"`
	Stream           bool           `json:"stream,omitempty"`
	Temperature      float32        `json:"temperature,omitempty"`
	ToolChoice       ToolChoice     `json:"tool_choice,omitempty"`
	Tools            []Tool         `json:"tools,omitempty"`
	TopLogprobs      int            `json:"top_logprobs,omitempty"`
	TopP             float32        `json:"top_p,omitempty"`
	User             string         `json:"user,omitempty"`
}

CompletionCreateParams struct to handle API parameters

type Error

type Error struct {
	Message          string `json:"message"`
	Type             string `json:"type"`
	FailedGeneration string `json:"failed_generation,omitempty"`
}

func (Error) Error

func (e Error) Error() string

type ErrorResponse

type ErrorResponse struct {
	Error Error `json:"error"`
}

type Message

type Message struct {
	Content    string            `json:"content"` // Required fields, not omitting in JSON
	Role       string            `json:"role"`    // Required fields, not omitting in JSON
	Name       string            `json:"name,omitempty"`
	ToolCallID string            `json:"tool_call_id,omitempty"`
	ToolCalls  []MessageToolCall `json:"tool_calls,omitempty"`
}

Message struct to handle messages

type MessageToolCall

type MessageToolCall struct {
	ID       string                  `json:"id,omitempty"`
	Function MessageToolCallFunction `json:"function,omitempty"`
	Type     string                  `json:"type,omitempty"`
}

MessageToolCall struct to handle tool calls in messages

type MessageToolCallFunction

type MessageToolCallFunction struct {
	Arguments string `json:"arguments,omitempty"`
	Name      string `json:"name,omitempty"`
}

MessageToolCallFunction struct to handle function details

type ResponseFormat

type ResponseFormat struct {
	Type string `json:"type,omitempty"`
}

ResponseFormat struct to handle response formatting

type ResponseFormatString

type ResponseFormatString string

type SSEvent

type SSEvent struct {
	ID    []byte
	Data  []byte
	Event []byte
	Retry []byte
}

func SSEventFromBytes

func SSEventFromBytes(event []byte) (*SSEvent, error)

func (*SSEvent) String

func (sse *SSEvent) String() string

type StreamReader

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

func NewStreamReader

func NewStreamReader(eventStream io.Reader) *StreamReader

func (*StreamReader) Next

func (e *StreamReader) Next() (*SSEvent, error)

type TimestampGranularity

type TimestampGranularity string

type Tool

type Tool struct {
	Function ToolFunction `json:"function,omitempty"`
	Type     string       `json:"type,omitempty"`
}

Tool struct to handle tools

type ToolChoice

type ToolChoice string
const (
	ToolChoiceAuto ToolChoice = "auto"
	ToolChoiceNone ToolChoice = "none"
)

type ToolChoiceToolChoice

type ToolChoiceToolChoice struct {
	Function ToolChoiceToolChoiceFunction `json:"function,omitempty"`
	Type     string                       `json:"type,omitempty"`
}

ToolChoiceToolChoice struct to handle nested tool choices

type ToolChoiceToolChoiceFunction

type ToolChoiceToolChoiceFunction struct {
	Name string `json:"name,omitempty"`
}

ToolChoiceToolChoiceFunction struct to handle tool choice functions

type ToolFunction

type ToolFunction struct {
	Description string                 `json:"description,omitempty"`
	Name        string                 `json:"name,omitempty"`
	Parameters  map[string]interface{} `json:"parameters,omitempty"`
}

ToolFunction struct to handle tool functions

type TranscriptionCreateParams

type TranscriptionCreateParams struct {
	File                   *os.File             `json:"file"`
	Model                  TranslationModel     `json:"model"`
	Language               string               `json:"language,omitempty"`
	Prompt                 string               `json:"prompt,omitempty"`
	ResponseFormat         ResponseFormatString `json:"response_format,omitempty"`
	Temperature            float32              `json:"temperature,omitempty"`
	TimestampGranularities TimestampGranularity `json:"timestamp_granularity,omitempty"`
}

type TranslationModel

type TranslationModel string

type Usage

type Usage struct {
	CompletionTime   *float64 `json:"completionTime,omitempty"`
	CompletionTokens *int     `json:"completionTokens,omitempty"`
	PromptTime       *float64 `json:"promptTime,omitempty"`
	PromptTokens     *int     `json:"promptTokens,omitempty"`
	QueueTime        *float64 `json:"queueTime,omitempty"`
	TotalTime        *float64 `json:"totalTime,omitempty"`
	TotalTokens      *int     `json:"totalTokens,omitempty"`
}

Usage struct to handle usage data

type XGroq

type XGroq struct {
	Usage Usage `json:"usage"`
}

XGroq struct for additional external data

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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