diff --git a/.go-version b/.go-version
index 49e0a31..14bee92 100644
--- a/.go-version
+++ b/.go-version
@@ -1 +1 @@
-1.23.1
+1.23.2
diff --git a/LICENSE b/LICENSE
index 6ce3351..ef10cc4 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,6 +1,6 @@
MIT License
-Copyright (c) 2024 connero
+Copyright (c) 2024 groq-go authors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
diff --git a/README.md b/README.md
index 4bb9cc2..61842ce 100644
--- a/README.md
+++ b/README.md
@@ -24,6 +24,8 @@
- JSON Schema Generation from structs.
- Supports [Toolhouse](https://app.toolhouse.ai/) function calling. [Extention](https://github.com/conneroisu/groq-go/tree/main/extensions/toolhouse)
- Supports [E2b](https://e2b.dev/) function calling. [Extention](https://github.com/conneroisu/groq-go/tree/main/extensions/e2b)
+- Supports [Composio](https://composio.dev/) function calling. [Extention](https://github.com/conneroisu/groq-go/tree/main/extensions/composio)
+- Supports [Jigsaw Stack](https://jigsawstack.com/) function calling. [Extention](https://github.com/conneroisu/groq-go/tree/main/extensions/jigsawstack)
## Installation
@@ -91,26 +93,16 @@ With specially designed hardware, the Groq API is a super fast way to query open
API Documentation: https://console.groq.com/docs/quickstart
-Code generated by groq\-modeler DO NOT EDIT.
-
-Created at: 2024\-10\-26 10:01:35
-
-groq\-modeler Version 1.1.2
-
## Index
- [Constants](<#constants>)
-- [Variables](<#variables>)
- [func AudioMultipartForm\(request AudioRequest, b builders.FormBuilder\) error](<#AudioMultipartForm>)
-- [func CreateFileField\(request AudioRequest, b builders.FormBuilder\) \(err error\)](<#CreateFileField>)
-- [type APIError](<#APIError>)
- - [func \(e \*APIError\) Error\(\) string](<#APIError.Error>)
- - [func \(e \*APIError\) UnmarshalJSON\(data \[\]byte\) \(err error\)](<#APIError.UnmarshalJSON>)
-- [type AudioModel](<#AudioModel>)
+- [type Agent](<#Agent>)
+ - [func NewAgent\(client \*Client, logger \*slog.Logger\) \*Agent](<#NewAgent>)
+- [type Agenter](<#Agenter>)
- [type AudioRequest](<#AudioRequest>)
- [type AudioResponse](<#AudioResponse>)
- [func \(r \*AudioResponse\) SetHeader\(header http.Header\)](<#AudioResponse.SetHeader>)
-- [type AudioResponseFormat](<#AudioResponseFormat>)
- [type ChatCompletionChoice](<#ChatCompletionChoice>)
- [type ChatCompletionMessage](<#ChatCompletionMessage>)
- [func \(m ChatCompletionMessage\) MarshalJSON\(\) \(\[\]byte, error\)](<#ChatCompletionMessage.MarshalJSON>)
@@ -120,7 +112,6 @@ groq\-modeler Version 1.1.2
- [func \(r \*ChatCompletionResponse\) SetHeader\(h http.Header\)](<#ChatCompletionResponse.SetHeader>)
- [type ChatCompletionResponseFormat](<#ChatCompletionResponseFormat>)
- [type ChatCompletionResponseFormatJSONSchema](<#ChatCompletionResponseFormatJSONSchema>)
-- [type ChatCompletionResponseFormatType](<#ChatCompletionResponseFormatType>)
- [type ChatCompletionStream](<#ChatCompletionStream>)
- [type ChatCompletionStreamChoice](<#ChatCompletionStreamChoice>)
- [type ChatCompletionStreamChoiceDelta](<#ChatCompletionStreamChoiceDelta>)
@@ -128,7 +119,6 @@ groq\-modeler Version 1.1.2
- [type ChatMessageImageURL](<#ChatMessageImageURL>)
- [type ChatMessagePart](<#ChatMessagePart>)
- [type ChatMessagePartType](<#ChatMessagePartType>)
-- [type ChatModel](<#ChatModel>)
- [type Client](<#Client>)
- [func NewClient\(groqAPIKey string, opts ...Opts\) \(\*Client, error\)](<#NewClient>)
- [func \(c \*Client\) CreateChatCompletion\(ctx context.Context, request ChatCompletionRequest\) \(response ChatCompletionResponse, err error\)](<#Client.CreateChatCompletion>)
@@ -136,25 +126,14 @@ groq\-modeler Version 1.1.2
- [func \(c \*Client\) CreateChatCompletionStream\(ctx context.Context, request ChatCompletionRequest\) \(stream \*ChatCompletionStream, err error\)](<#Client.CreateChatCompletionStream>)
- [func \(c \*Client\) CreateTranscription\(ctx context.Context, request AudioRequest\) \(AudioResponse, error\)](<#Client.CreateTranscription>)
- [func \(c \*Client\) CreateTranslation\(ctx context.Context, request AudioRequest\) \(AudioResponse, error\)](<#Client.CreateTranslation>)
- - [func \(c \*Client\) Moderate\(ctx context.Context, request ModerationRequest\) \(response Moderation, err error\)](<#Client.Moderate>)
- - [func \(c \*Client\) MustCreateChatCompletion\(ctx context.Context, request ChatCompletionRequest\) \(response ChatCompletionResponse\)](<#Client.MustCreateChatCompletion>)
-- [type DefaultErrorAccumulator](<#DefaultErrorAccumulator>)
- - [func \(e \*DefaultErrorAccumulator\) Bytes\(\) \(errBytes \[\]byte\)](<#DefaultErrorAccumulator.Bytes>)
- - [func \(e \*DefaultErrorAccumulator\) Write\(p \[\]byte\) error](<#DefaultErrorAccumulator.Write>)
+ - [func \(c \*Client\) Moderate\(ctx context.Context, messages \[\]ChatCompletionMessage, model models.ModerationModel\) \(response Moderation, err error\)](<#Client.Moderate>)
- [type Endpoint](<#Endpoint>)
-- [type ErrContentFieldsMisused](<#ErrContentFieldsMisused>)
- - [func \(e ErrContentFieldsMisused\) Error\(\) string](<#ErrContentFieldsMisused.Error>)
-- [type ErrTooManyEmptyStreamMessages](<#ErrTooManyEmptyStreamMessages>)
- - [func \(e ErrTooManyEmptyStreamMessages\) Error\(\) string](<#ErrTooManyEmptyStreamMessages.Error>)
- [type FinishReason](<#FinishReason>)
- [func \(r FinishReason\) MarshalJSON\(\) \(\[\]byte, error\)](<#FinishReason.MarshalJSON>)
- [type Format](<#Format>)
-- [type HarmfulCategory](<#HarmfulCategory>)
- [type ImageURLDetail](<#ImageURLDetail>)
- [type LogProbs](<#LogProbs>)
- [type Moderation](<#Moderation>)
-- [type ModerationModel](<#ModerationModel>)
-- [type ModerationRequest](<#ModerationRequest>)
- [type Opts](<#Opts>)
- [func WithBaseURL\(baseURL string\) Opts](<#WithBaseURL>)
- [func WithClient\(client \*http.Client\) Opts](<#WithClient>)
@@ -167,6 +146,9 @@ groq\-modeler Version 1.1.2
- [type Role](<#Role>)
- [type Segments](<#Segments>)
- [type StreamOptions](<#StreamOptions>)
+- [type ToolGetter](<#ToolGetter>)
+- [type ToolManager](<#ToolManager>)
+- [type ToolRunner](<#ToolRunner>)
- [type TopLogProbs](<#TopLogProbs>)
- [type TranscriptionTimestampGranularity](<#TranscriptionTimestampGranularity>)
- [type Usage](<#Usage>)
@@ -175,253 +157,37 @@ groq\-modeler Version 1.1.2
## Constants
-
-
-```go
-const (
- AudioResponseFormatJSON AudioResponseFormat = "json" // AudioResponseFormatJSON is the JSON format of some audio.
- AudioResponseFormatText AudioResponseFormat = "text" // AudioResponseFormatText is the text format of some audio.
- AudioResponseFormatSRT AudioResponseFormat = "srt" // AudioResponseFormatSRT is the SRT format of some audio.
- AudioResponseFormatVerboseJSON AudioResponseFormat = "verbose_json" // AudioResponseFormatVerboseJSON is the verbose JSON format of some audio.
- AudioResponseFormatVTT AudioResponseFormat = "vtt" // AudioResponseFormatVTT is the VTT format of some audio.
-
- TranscriptionTimestampGranularityWord TranscriptionTimestampGranularity = "word" // TranscriptionTimestampGranularityWord is the word timestamp granularity.
- TranscriptionTimestampGranularitySegment TranscriptionTimestampGranularity = "segment" // TranscriptionTimestampGranularitySegment is the segment timestamp granularity.
-)
-```
-
```go
const (
- ChatMessageRoleSystem Role = "system" // ChatMessageRoleSystem is the system chat message role.
- ChatMessageRoleUser Role = "user" // ChatMessageRoleUser is the user chat message role.
- ChatMessageRoleAssistant Role = "assistant" // ChatMessageRoleAssistant is the assistant chat message role.
- ChatMessageRoleFunction Role = "function" // ChatMessageRoleFunction is the function chat message role.
- ChatMessageRoleTool Role = "tool" // ChatMessageRoleTool is the tool chat message role.
- ImageURLDetailHigh ImageURLDetail = "high" // ImageURLDetailHigh is the high image url detail.
- ImageURLDetailLow ImageURLDetail = "low" // ImageURLDetailLow is the low image url detail.
- ImageURLDetailAuto ImageURLDetail = "auto" // ImageURLDetailAuto is the auto image url detail.
- ChatMessagePartTypeText ChatMessagePartType = "text" // ChatMessagePartTypeText is the text chat message part type.
- ChatMessagePartTypeImageURL ChatMessagePartType = "image_url" // ChatMessagePartTypeImageURL is the image url chat message part type.
- ChatCompletionResponseFormatTypeJSONObject ChatCompletionResponseFormatType = "json_object" // ChatCompletionResponseFormatTypeJSONObject is the json object chat completion response format type.
- ChatCompletionResponseFormatTypeJSONSchema ChatCompletionResponseFormatType = "json_schema" // ChatCompletionResponseFormatTypeJSONSchema is the json schema chat completion response format type.
- ChatCompletionResponseFormatTypeText ChatCompletionResponseFormatType = "text" // ChatCompletionResponseFormatTypeText is the text chat completion response format type.
- FinishReasonStop FinishReason = "stop" // FinishReasonStop is the stop finish reason.
- FinishReasonLength FinishReason = "length" // FinishReasonLength is the length finish reason.
- FinishReasonFunctionCall FinishReason = "function_call" // FinishReasonFunctionCall is the function call finish reason.
- FinishReasonToolCalls FinishReason = "tool_calls" // FinishReasonToolCalls is the tool calls finish reason.
- FinishReasonContentFilter FinishReason = "content_filter" // FinishReasonContentFilter is the content filter finish reason.
- FinishReasonNull FinishReason = "null" // FinishReasonNull is the null finish reason.
-)
-```
-
-## Variables
-
-
-
-```go
-var (
- // ModelGemma29BIt is an AI text chat model.
- //
- // It is created/provided by Google.
- //
- // It has 8192 context window.
- //
- // It can be used with the following client methods:
- // - CreateChatCompletion
- // - CreateChatCompletionStream
- // - CreateChatCompletionJSON
- ModelGemma29BIt ChatModel = "gemma2-9b-it"
- // ModelGemma7BIt is an AI text chat model.
- //
- // It is created/provided by Google.
- //
- // It has 8192 context window.
- //
- // It can be used with the following client methods:
- // - CreateChatCompletion
- // - CreateChatCompletionStream
- // - CreateChatCompletionJSON
- ModelGemma7BIt ChatModel = "gemma-7b-it"
- // ModelLlama3170BVersatile is an AI text chat model.
- //
- // It is created/provided by Meta.
- //
- // It has 32768 context window.
- //
- // It can be used with the following client methods:
- // - CreateChatCompletion
- // - CreateChatCompletionStream
- // - CreateChatCompletionJSON
- ModelLlama3170BVersatile ChatModel = "llama-3.1-70b-versatile"
- // ModelLlama318BInstant is an AI text chat model.
- //
- // It is created/provided by Meta.
- //
- // It has 131072 context window.
- //
- // It can be used with the following client methods:
- // - CreateChatCompletion
- // - CreateChatCompletionStream
- // - CreateChatCompletionJSON
- ModelLlama318BInstant ChatModel = "llama-3.1-8b-instant"
- // ModelLlama3211BTextPreview is an AI text chat model.
- //
- // It is created/provided by Meta.
- //
- // It has 8192 context window.
- //
- // It can be used with the following client methods:
- // - CreateChatCompletion
- // - CreateChatCompletionStream
- // - CreateChatCompletionJSON
- ModelLlama3211BTextPreview ChatModel = "llama-3.2-11b-text-preview"
- // ModelLlama3211BVisionPreview is an AI text chat model.
- //
- // It is created/provided by Meta.
- //
- // It has 8192 context window.
- //
- // It can be used with the following client methods:
- // - CreateChatCompletion
- // - CreateChatCompletionStream
- // - CreateChatCompletionJSON
- ModelLlama3211BVisionPreview ChatModel = "llama-3.2-11b-vision-preview"
- // ModelLlama321BPreview is an AI text chat model.
- //
- // It is created/provided by Meta.
- //
- // It has 8192 context window.
- //
- // It can be used with the following client methods:
- // - CreateChatCompletion
- // - CreateChatCompletionStream
- // - CreateChatCompletionJSON
- ModelLlama321BPreview ChatModel = "llama-3.2-1b-preview"
- // ModelLlama323BPreview is an AI text chat model.
- //
- // It is created/provided by Meta.
- //
- // It has 8192 context window.
- //
- // It can be used with the following client methods:
- // - CreateChatCompletion
- // - CreateChatCompletionStream
- // - CreateChatCompletionJSON
- ModelLlama323BPreview ChatModel = "llama-3.2-3b-preview"
- // ModelLlama3290BTextPreview is an AI text chat model.
- //
- // It is created/provided by Meta.
- //
- // It has 8192 context window.
- //
- // It can be used with the following client methods:
- // - CreateChatCompletion
- // - CreateChatCompletionStream
- // - CreateChatCompletionJSON
- ModelLlama3290BTextPreview ChatModel = "llama-3.2-90b-text-preview"
- // ModelLlama3290BVisionPreview is an AI text chat model.
- //
- // It is created/provided by Meta.
- //
- // It has 8192 context window.
- //
- // It can be used with the following client methods:
- // - CreateChatCompletion
- // - CreateChatCompletionStream
- // - CreateChatCompletionJSON
- ModelLlama3290BVisionPreview ChatModel = "llama-3.2-90b-vision-preview"
- // ModelLlama370B8192 is an AI text chat model.
- //
- // It is created/provided by Meta.
- //
- // It has 8192 context window.
- //
- // It can be used with the following client methods:
- // - CreateChatCompletion
- // - CreateChatCompletionStream
- // - CreateChatCompletionJSON
- ModelLlama370B8192 ChatModel = "llama3-70b-8192"
- // ModelLlama38B8192 is an AI text chat model.
- //
- // It is created/provided by Meta.
- //
- // It has 8192 context window.
- //
- // It can be used with the following client methods:
- // - CreateChatCompletion
- // - CreateChatCompletionStream
- // - CreateChatCompletionJSON
- ModelLlama38B8192 ChatModel = "llama3-8b-8192"
- // ModelLlama3Groq70B8192ToolUsePreview is an AI text chat model.
- //
- // It is created/provided by Groq.
- //
- // It has 8192 context window.
- //
- // It can be used with the following client methods:
- // - CreateChatCompletion
- // - CreateChatCompletionStream
- // - CreateChatCompletionJSON
- ModelLlama3Groq70B8192ToolUsePreview ChatModel = "llama3-groq-70b-8192-tool-use-preview"
- // ModelLlama3Groq8B8192ToolUsePreview is an AI text chat model.
- //
- // It is created/provided by Groq.
- //
- // It has 8192 context window.
- //
- // It can be used with the following client methods:
- // - CreateChatCompletion
- // - CreateChatCompletionStream
- // - CreateChatCompletionJSON
- ModelLlama3Groq8B8192ToolUsePreview ChatModel = "llama3-groq-8b-8192-tool-use-preview"
- // ModelLlavaV157B4096Preview is an AI text chat model.
- //
- // It is created/provided by Other.
- //
- // It has 4096 context window.
- //
- // It can be used with the following client methods:
- // - CreateChatCompletion
- // - CreateChatCompletionStream
- // - CreateChatCompletionJSON
- ModelLlavaV157B4096Preview ChatModel = "llava-v1.5-7b-4096-preview"
- // ModelMixtral8X7B32768 is an AI text chat model.
- //
- // It is created/provided by Mistral AI.
- //
- // It has 32768 context window.
- //
- // It can be used with the following client methods:
- // - CreateChatCompletion
- // - CreateChatCompletionStream
- // - CreateChatCompletionJSON
- ModelMixtral8X7B32768 ChatModel = "mixtral-8x7b-32768"
- // ModelWhisperLargeV3 is an AI audio transcription model.
- //
- // It is created/provided by OpenAI.
- //
- // It has 448 context window.
- //
- // It can be used with the following client methods:
- // - CreateTranscription
- // - CreateTranslation
- ModelWhisperLargeV3 AudioModel = "whisper-large-v3"
- // ModelLlamaGuard38B is an AI moderation model.
- //
- // It is created/provided by Meta.
- //
- // It has 8192 context window.
- //
- // It can be used with the following client methods:
- // - Moderate
- ModelLlamaGuard38B ModerationModel = "llama-guard-3-8b"
+ // ChatMessageRoleSystem is the system chat message role.
+ ChatMessageRoleSystem Role = "system"
+ // ChatMessageRoleUser is the user chat message role.
+ ChatMessageRoleUser Role = "user"
+ // ChatMessageRoleAssistant is the assistant chat message role.
+ ChatMessageRoleAssistant Role = "assistant"
+ // ChatMessageRoleFunction is the function chat message role.
+ ChatMessageRoleFunction Role = "function"
+ // ChatMessageRoleTool is the tool chat message role.
+ ChatMessageRoleTool Role = "tool"
+
+ // ImageURLDetailHigh is the high image url detail.
+ ImageURLDetailHigh ImageURLDetail = "high"
+ // ImageURLDetailLow is the low image url detail.
+ ImageURLDetailLow ImageURLDetail = "low"
+ // ImageURLDetailAuto is the auto image url detail.
+ ImageURLDetailAuto ImageURLDetail = "auto"
+
+ // ChatMessagePartTypeText is the text chat message part type.
+ ChatMessagePartTypeText ChatMessagePartType = "text"
+ // ChatMessagePartTypeImageURL is the image url chat message part type.
+ ChatMessagePartTypeImageURL ChatMessagePartType = "image_url"
)
```
-## func [AudioMultipartForm]()
+## func [AudioMultipartForm]()
```go
func AudioMultipartForm(request AudioRequest, b builders.FormBuilder) error
@@ -429,94 +195,90 @@ func AudioMultipartForm(request AudioRequest, b builders.FormBuilder) error
AudioMultipartForm creates a form with audio file contents and the name of the model to use for audio processing.
-
-## func [CreateFileField]()
-
-```go
-func CreateFileField(request AudioRequest, b builders.FormBuilder) (err error)
-```
-
-CreateFileField creates the "file" form field from either an existing file or by using the reader.
-
-
-## type [APIError]()
+
+## type [Agent]()
-APIError provides error information returned by the Groq API.
+Agent is an agent.
```go
-type APIError struct {
- Code any `json:"code,omitempty"` // Code is the code of the error.
- Message string `json:"message"` // Message is the message of the error.
- Param *string `json:"param,omitempty"` // Param is the param of the error.
- Type string `json:"type"` // Type is the type of the error.
- HTTPStatusCode int `json:"-"` // HTTPStatusCode is the status code of the error.
+type Agent struct {
+ // contains filtered or unexported fields
}
```
-
-### func \(\*APIError\) [Error]()
-
-```go
-func (e *APIError) Error() string
-```
-
-Error method implements the error interface on APIError.
-
-
-### func \(\*APIError\) [UnmarshalJSON]()
+
+### func [NewAgent]()
```go
-func (e *APIError) UnmarshalJSON(data []byte) (err error)
+func NewAgent(client *Client, logger *slog.Logger) *Agent
```
-UnmarshalJSON implements the json.Unmarshaler interface.
+NewAgent creates a new agent.
-
-## type [AudioModel]()
+
+## type [Agenter]()
-AudioModel is the type for audio models present on the groq api.
+Agenter is an interface for an agent.
```go
-type AudioModel model
+type Agenter interface {
+ ToolManager
+}
```
-## type [AudioRequest]()
+## type [AudioRequest]()
AudioRequest represents a request structure for audio API.
```go
type AudioRequest struct {
- Model AudioModel // Model is the model to use for the transcription.
- FilePath string // FilePath is either an existing file in your filesystem or a filename representing the contents of Reader.
- Reader io.Reader // Reader is an optional io.Reader when you do not want to use an existing file.
- Prompt string // Prompt is the prompt for the transcription.
- Temperature float32 // Temperature is the temperature for the transcription.
- Language string // Language is the language for the transcription. Only for transcription.
- Format AudioResponseFormat // Format is the format for the response.
+ // Model is the model to use for the transcription.
+ Model models.AudioModel
+ // FilePath is either an existing file in your filesystem or a
+ // filename representing the contents of Reader.
+ FilePath string
+ // Reader is an optional io.Reader when you do not want to use
+ // an existing file.
+ Reader io.Reader
+ // Prompt is the prompt for the transcription.
+ Prompt string
+ // Temperature is the temperature for the transcription.
+ Temperature float32
+ // Language is the language for the transcription. Only for
+ // transcription.
+ Language string
+ // Format is the format for the response.
+ Format Format
}
```
-## type [AudioResponse]()
+## type [AudioResponse]()
AudioResponse represents a response structure for audio API.
```go
type AudioResponse struct {
- Task string `json:"task"` // Task is the task of the response.
- Language string `json:"language"` // Language is the language of the response.
- Duration float64 `json:"duration"` // Duration is the duration of the response.
- Segments Segments `json:"segments"` // Segments is the segments of the response.
- Words Words `json:"words"` // Words is the words of the response.
- Text string `json:"text"` // Text is the text of the response.
+ // Task is the task of the response.
+ Task string `json:"task"`
+ // Language is the language of the response.
+ Language string `json:"language"`
+ // Duration is the duration of the response.
+ Duration float64 `json:"duration"`
+ // Segments is the segments of the response.
+ Segments Segments `json:"segments"`
+ // Words is the words of the response.
+ Words Words `json:"words"`
+ // Text is the text of the response.
+ Text string `json:"text"`
Header http.Header // Header is the header of the response.
}
```
-### func \(\*AudioResponse\) [SetHeader]()
+### func \(\*AudioResponse\) [SetHeader]()
```go
func (r *AudioResponse) SetHeader(header http.Header)
@@ -524,21 +286,8 @@ func (r *AudioResponse) SetHeader(header http.Header)
SetHeader sets the header of the response.
-
-## type [AudioResponseFormat]()
-
-AudioResponseFormat is the response format for the audio API.
-
-Response formatted using AudioResponseFormatJSON by default.
-
-string
-
-```go
-type AudioResponseFormat string
-```
-
-## type [ChatCompletionChoice]()
+## type [ChatCompletionChoice]()
ChatCompletionChoice represents the chat completion choice.
@@ -546,42 +295,49 @@ ChatCompletionChoice represents the chat completion choice.
type ChatCompletionChoice struct {
Index int `json:"index"` // Index is the index of the choice.
// Message is the chat completion message of the choice.
- Message ChatCompletionMessage `json:"message"` // Message is the chat completion message of the choice.
+ Message ChatCompletionMessage `json:"message"`
// FinishReason is the finish reason of the choice.
- //
- // stop: API returned complete message,
- // or a message terminated by one of the stop sequences provided via the stop parameter
- // length: Incomplete model output due to max_tokens parameter or token limit
- // function_call: The model decided to call a function
- // content_filter: Omitted content due to a flag from our content filters
- // null: API response still in progress or incomplete
- FinishReason FinishReason `json:"finish_reason"` // FinishReason is the finish reason of the choice.
+ FinishReason FinishReason `json:"finish_reason"`
// LogProbs is the log probs of the choice.
//
- // This is basically the probability of the model choosing the token.
- LogProbs *LogProbs `json:"logprobs,omitempty"` // LogProbs is the log probs of the choice.
+ // This is basically the probability of the model choosing the
+ // token.
+ LogProbs *LogProbs `json:"logprobs,omitempty"`
}
```
-## type [ChatCompletionMessage]()
+## type [ChatCompletionMessage]()
ChatCompletionMessage represents the chat completion message.
```go
type ChatCompletionMessage struct {
- Name string `json:"name"` // Name is the name of the chat completion message.
- Role Role `json:"role"` // Role is the role of the chat completion message.
- Content string `json:"content"` // Content is the content of the chat completion message.
- MultiContent []ChatMessagePart `json:"-"` // MultiContent is the multi content of the chat completion message.
- FunctionCall *tools.FunctionCall `json:"function_call,omitempty"` // FunctionCall setting for Role=assistant prompts this may be set to the function call generated by the model.
- ToolCalls []tools.ToolCall `json:"tool_calls,omitempty"` // ToolCalls setting for Role=assistant prompts this may be set to the tool calls generated by the model, such as function calls.
- ToolCallID string `json:"tool_call_id,omitempty"` // ToolCallID is setting for Role=tool prompts this should be set to the ID given in the assistant's prior request to call a tool.
+ // Name is the name of the chat completion message.
+ Name string `json:"name"`
+ // Role is the role of the chat completion message.
+ Role Role `json:"role"`
+ // Content is the content of the chat completion message.
+ Content string `json:"content"`
+ // MultiContent is the multi content of the chat completion
+ // message.
+ MultiContent []ChatMessagePart `json:"-"`
+ // FunctionCall setting for Role=assistant prompts this may be
+ // set to the function call generated by the model.
+ FunctionCall *tools.FunctionCall `json:"function_call,omitempty"`
+ // ToolCalls setting for Role=assistant prompts this may be set
+ // to the tool calls generated by the model, such as function
+ // calls.
+ ToolCalls []tools.ToolCall `json:"tool_calls,omitempty"`
+ // ToolCallID is setting for Role=tool prompts this should be
+ // set to the ID given in the assistant's prior request to call
+ // a tool.
+ ToolCallID string `json:"tool_call_id,omitempty"`
}
```
-### func \(ChatCompletionMessage\) [MarshalJSON]()
+### func \(ChatCompletionMessage\) [MarshalJSON]()
```go
func (m ChatCompletionMessage) MarshalJSON() ([]byte, error)
@@ -590,7 +346,7 @@ func (m ChatCompletionMessage) MarshalJSON() ([]byte, error)
MarshalJSON method implements the json.Marshaler interface.
-### func \(\*ChatCompletionMessage\) [UnmarshalJSON]()
+### func \(\*ChatCompletionMessage\) [UnmarshalJSON]()
```go
func (m *ChatCompletionMessage) UnmarshalJSON(bs []byte) (err error)
@@ -599,56 +355,100 @@ func (m *ChatCompletionMessage) UnmarshalJSON(bs []byte) (err error)
UnmarshalJSON method implements the json.Unmarshaler interface.
-## type [ChatCompletionRequest]()
+## type [ChatCompletionRequest]()
ChatCompletionRequest represents a request structure for the chat completion API.
```go
type ChatCompletionRequest struct {
- Model ChatModel `json:"model"` // Model is the model of the chat completion request.
- Messages []ChatCompletionMessage `json:"messages"` // Messages is the messages of the chat completion request. These act as the prompt for the model.
- MaxTokens int `json:"max_tokens,omitempty"` // MaxTokens is the max tokens of the chat completion request.
- Temperature float32 `json:"temperature,omitempty"` // Temperature is the temperature of the chat completion request.
- TopP float32 `json:"top_p,omitempty"` // TopP is the top p of the chat completion request.
- N int `json:"n,omitempty"` // N is the n of the chat completion request.
- Stream bool `json:"stream,omitempty"` // Stream is the stream of the chat completion request.
- Stop []string `json:"stop,omitempty"` // Stop is the stop of the chat completion request.
- PresencePenalty float32 `json:"presence_penalty,omitempty"` // PresencePenalty is the presence penalty of the chat completion request.
- ResponseFormat *ChatCompletionResponseFormat `json:"response_format,omitempty"` // ResponseFormat is the response format of the chat completion request.
- Seed *int `json:"seed,omitempty"` // Seed is the seed of the chat completion request.
- FrequencyPenalty float32 `json:"frequency_penalty,omitempty"` // FrequencyPenalty is the frequency penalty of the chat completion request.
- LogitBias map[string]int `json:"logit_bias,omitempty"` // LogitBias is must be a token id string (specified by their token ID in the tokenizer), not a word string. incorrect: `"logit_bias":{ "You": 6}`, correct: `"logit_bias":{"1639": 6}` refs: https://platform.openai.com/docs/api-reference/chat/create#chat/create-logit_bias
- LogProbs bool `json:"logprobs,omitempty"` // LogProbs indicates whether to return log probabilities of the output tokens or not. If true, returns the log probabilities of each output token returned in the content of message. This option is currently not available on the gpt-4-vision-preview model.
- TopLogProbs int `json:"top_logprobs,omitempty"` // TopLogProbs is an integer between 0 and 5 specifying the number of most likely tokens to return at each token position, each with an associated log probability. logprobs must be set to true if this parameter is used.
- User string `json:"user,omitempty"` // User is the user of the chat completion request.
- Tools []tools.Tool `json:"tools,omitempty"` // Tools is the tools of the chat completion request.
- ToolChoice any `json:"tool_choice,omitempty"` // This can be either a string or an ToolChoice object.
- StreamOptions *StreamOptions `json:"stream_options,omitempty"` // Options for streaming response. Only set this when you set stream: true.
- ParallelToolCalls any `json:"parallel_tool_calls,omitempty"` // Disable the default behavior of parallel tool calls by setting it: false.
- RetryDelay time.Duration `json:"-"` // RetryDelay is the delay between retries.
+ // Model is the model of the chat completion request.
+ Model models.ChatModel `json:"model"`
+ // Messages is the messages of the chat completion request.
+ //
+ // These act as the prompt for the model.
+ Messages []ChatCompletionMessage `json:"messages"`
+ // MaxTokens is the max tokens of the chat completion request.
+ MaxTokens int `json:"max_tokens,omitempty"`
+ // Temperature is the temperature of the chat completion
+ // request.
+ Temperature float32 `json:"temperature,omitempty"`
+ // TopP is the top p of the chat completion request.
+ TopP float32 `json:"top_p,omitempty"`
+ // N is the n of the chat completion request.
+ N int `json:"n,omitempty"`
+ // Stream is the stream of the chat completion request.
+ Stream bool `json:"stream,omitempty"`
+ // Stop is the stop of the chat completion request.
+ Stop []string `json:"stop,omitempty"`
+ // PresencePenalty is the presence penalty of the chat
+ // completion request.
+ PresencePenalty float32 `json:"presence_penalty,omitempty"`
+ // ResponseFormat is the response format of the chat completion
+ // request.
+ ResponseFormat *ChatCompletionResponseFormat `json:"response_format,omitempty"`
+ // Seed is the seed of the chat completion request.
+ Seed *int `json:"seed,omitempty"`
+ // FrequencyPenalty is the frequency penalty of the chat
+ // completion request.
+ FrequencyPenalty float32 `json:"frequency_penalty,omitempty"`
+ // LogitBias is must be a token id string (specified by their
+ // token ID in the tokenizer), not a word string. incorrect: `"logit_bias":{ "You": 6}`, correct: `"logit_bias":{"1639": 6}` refs: https://platform.openai.com/docs/api-reference/chat/create#chat/create-logit_bias
+ LogitBias map[string]int `json:"logit_bias,omitempty"`
+ // LogProbs indicates whether to return log probabilities of the
+ // output tokens or not. If true, returns the log probabilities
+ // of each output token returned in the content of message.
+ //
+ // This option is currently not available on the
+ // gpt-4-vision-preview model.
+ LogProbs bool `json:"logprobs,omitempty"`
+ // TopLogProbs is an integer between 0 and 5 specifying the
+ // number of most likely tokens to return at each token
+ // position, each with an associated log probability. Logprobs
+ // must be set to true if this parameter is used.
+ TopLogProbs int `json:"top_logprobs,omitempty"`
+ // User is the user of the chat completion request.
+ User string `json:"user,omitempty"`
+ // Tools is the tools of the chat completion request.
+ Tools []tools.Tool `json:"tools,omitempty"`
+ // This can be either a string or an ToolChoice object.
+ ToolChoice any `json:"tool_choice,omitempty"`
+ // Options for streaming response. Only set this when you set stream: true.
+ StreamOptions *StreamOptions `json:"stream_options,omitempty"`
+ // Disable the default behavior of parallel tool calls by setting it: false.
+ ParallelToolCalls any `json:"parallel_tool_calls,omitempty"`
+ // RetryDelay is the delay between retries.
+ RetryDelay time.Duration `json:"-"`
}
```
-## type [ChatCompletionResponse]()
+## type [ChatCompletionResponse]()
ChatCompletionResponse represents a response structure for chat completion API.
```go
type ChatCompletionResponse struct {
- ID string `json:"id"` // ID is the id of the response.
- Object string `json:"object"` // Object is the object of the response.
- Created int64 `json:"created"` // Created is the created time of the response.
- Model ChatModel `json:"model"` // Model is the model of the response.
- Choices []ChatCompletionChoice `json:"choices"` // Choices is the choices of the response.
- Usage Usage `json:"usage"` // Usage is the usage of the response.
- SystemFingerprint string `json:"system_fingerprint"` // SystemFingerprint is the system fingerprint of the response.
- http.Header // Header is the header of the response.
+ // ID is the id of the response.
+ ID string `json:"id"`
+ // Object is the object of the response.
+ Object string `json:"object"`
+ // Created is the created time of the response.
+ Created int64 `json:"created"`
+ // Model is the model of the response.
+ Model models.ChatModel `json:"model"`
+ // Choices is the choices of the response.
+ Choices []ChatCompletionChoice `json:"choices"`
+ // Usage is the usage of the response.
+ Usage Usage `json:"usage"`
+ // SystemFingerprint is the system fingerprint of the response.
+ SystemFingerprint string `json:"system_fingerprint"`
+ // Header is the header of the response.
+ http.Header
}
```
-### func \(\*ChatCompletionResponse\) [SetHeader]()
+### func \(\*ChatCompletionResponse\) [SetHeader]()
```go
func (r *ChatCompletionResponse) SetHeader(h http.Header)
@@ -657,21 +457,22 @@ func (r *ChatCompletionResponse) SetHeader(h http.Header)
SetHeader sets the header of the response.
-## type [ChatCompletionResponseFormat]()
+## type [ChatCompletionResponseFormat]()
ChatCompletionResponseFormat is the chat completion response format.
```go
type ChatCompletionResponseFormat struct {
// Type is the type of the chat completion response format.
- Type ChatCompletionResponseFormatType `json:"type,omitempty"`
- // JSONSchema is the json schema of the chat completion response format.
+ Type Format `json:"type,omitempty"`
+ // JSONSchema is the json schema of the chat completion response
+ // format.
JSONSchema *ChatCompletionResponseFormatJSONSchema `json:"json_schema,omitempty"`
}
```
-## type [ChatCompletionResponseFormatJSONSchema]()
+## type [ChatCompletionResponseFormatJSONSchema]()
ChatCompletionResponseFormatJSONSchema is the chat completion response format json schema.
@@ -682,37 +483,23 @@ type ChatCompletionResponseFormatJSONSchema struct {
//
// it is used to further identify the schema in the response.
Name string `json:"name"`
- // Description is the description of the chat completion response
- // format
- // json schema.
+ // Description is the description of the chat completion
+ // response format json schema.
Description string `json:"description,omitempty"`
- // description of the chat completion response format json schema.
- // Schema is the schema of the chat completion response format json schema.
+ // Schema is the schema of the chat completion response format
+ // json schema.
Schema schema.Schema `json:"schema"`
- // Strict determines whether to enforce the schema upon the generated
- // content.
+ // Strict determines whether to enforce the schema upon the
+ // generated content.
Strict bool `json:"strict"`
}
```
-
-## type [ChatCompletionResponseFormatType]()
-
-ChatCompletionResponseFormatType is the chat completion response format type.
-
-string
-
-```go
-type ChatCompletionResponseFormatType string
-```
-
-## type [ChatCompletionStream]()
+## type [ChatCompletionStream]()
ChatCompletionStream is a stream of ChatCompletionStreamResponse.
-Note: Perhaps it is more elegant to abstract Stream using generics.
-
```go
type ChatCompletionStream struct {
// contains filtered or unexported fields
@@ -720,84 +507,113 @@ type ChatCompletionStream struct {
```
-## type [ChatCompletionStreamChoice]()
+## type [ChatCompletionStreamChoice]()
ChatCompletionStreamChoice represents a response structure for chat completion API.
```go
type ChatCompletionStreamChoice struct {
- Index int `json:"index"`
- Delta ChatCompletionStreamChoiceDelta `json:"delta"`
- FinishReason FinishReason `json:"finish_reason"`
+ // Index is the index of the choice.
+ Index int `json:"index"`
+ // Delta is the delta of the choice.
+ Delta ChatCompletionStreamChoiceDelta `json:"delta"`
+ // FinishReason is the finish reason of the choice.
+ FinishReason FinishReason `json:"finish_reason"`
}
```
-## type [ChatCompletionStreamChoiceDelta]()
+## type [ChatCompletionStreamChoiceDelta]()
ChatCompletionStreamChoiceDelta represents a response structure for chat completion API.
```go
type ChatCompletionStreamChoiceDelta struct {
- Content string `json:"content,omitempty"`
- Role string `json:"role,omitempty"`
+ // Content is the content of the response.
+ Content string `json:"content,omitempty"`
+ // Role is the role of the creator of the completion.
+ Role string `json:"role,omitempty"`
+ // FunctionCall is the function call of the response.
FunctionCall *tools.FunctionCall `json:"function_call,omitempty"`
- ToolCalls []tools.ToolCall `json:"tool_calls,omitempty"`
+ // ToolCalls are the tool calls of the response.
+ ToolCalls []tools.ToolCall `json:"tool_calls,omitempty"`
}
```
-## type [ChatCompletionStreamResponse]()
+## type [ChatCompletionStreamResponse]()
ChatCompletionStreamResponse represents a response structure for chat completion API.
```go
type ChatCompletionStreamResponse struct {
- ID string `json:"id"` // ID is the identifier for the chat completion stream response.
- Object string `json:"object"` // Object is the object type of the chat completion stream response.
- Created int64 `json:"created"` // Created is the creation time of the chat completion stream response.
- Model ChatModel `json:"model"` // Model is the model used for the chat completion stream response.
- Choices []ChatCompletionStreamChoice `json:"choices"` // Choices is the choices for the chat completion stream response.
- SystemFingerprint string `json:"system_fingerprint"` // SystemFingerprint is the system fingerprint for the chat completion stream response.
- PromptAnnotations []PromptAnnotation `json:"prompt_annotations,omitempty"` // PromptAnnotations is the prompt annotations for the chat completion stream response.
+ // ID is the identifier for the chat completion stream response.
+ ID string `json:"id"`
+ // Object is the object type of the chat completion stream
+ // response.
+ Object string `json:"object"`
+ // Created is the creation time of the chat completion stream
+ // response.
+ Created int64 `json:"created"`
+ // Model is the model used for the chat completion stream
+ // response.
+ Model models.ChatModel `json:"model"`
+ // Choices is the choices for the chat completion stream
+ // response.
+ Choices []ChatCompletionStreamChoice `json:"choices"`
+ // SystemFingerprint is the system fingerprint for the chat
+ // completion stream response.
+ SystemFingerprint string `json:"system_fingerprint"`
+ // PromptAnnotations is the prompt annotations for the chat
+ // completion stream response.
+ PromptAnnotations []PromptAnnotation `json:"prompt_annotations,omitempty"`
+ // PromptFilterResults is the prompt filter results for the chat
+ // completion stream response.
PromptFilterResults []struct {
Index int `json:"index"`
- } `json:"prompt_filter_results,omitempty"` // PromptFilterResults is the prompt filter results for the chat completion stream response.
- // Usage is an optional field that will only be present when you set stream_options: {"include_usage": true} in your request.
+ } `json:"prompt_filter_results,omitempty"`
+ // Usage is an optional field that will only be present when you
+ // set stream_options: {"include_usage": true} in your request.
//
- // When present, it contains a null value except for the last chunk which contains the token usage statistics
- // for the entire request.
+ // When present, it contains a null value except for the last
+ // chunk which contains the token usage statistics for the
+ // entire request.
Usage *Usage `json:"usage,omitempty"`
}
```
-## type [ChatMessageImageURL]()
+## type [ChatMessageImageURL]()
ChatMessageImageURL represents the chat message image url.
```go
type ChatMessageImageURL struct {
- URL string `json:"url,omitempty"` // URL is the url of the image.
- Detail ImageURLDetail `json:"detail,omitempty"` // Detail is the detail of the image url.
+ // URL is the url of the image.
+ URL string `json:"url,omitempty"`
+ // Detail is the detail of the image url.
+ Detail ImageURLDetail `json:"detail,omitempty"`
}
```
-## type [ChatMessagePart]()
+## type [ChatMessagePart]()
ChatMessagePart represents the chat message part of a chat completion message.
```go
type ChatMessagePart struct {
- Text string `json:"text,omitempty"` // Text is the text of the chat message part.
- Type ChatMessagePartType `json:"type,omitempty"` // Type is the type of the chat message part.
- ImageURL *ChatMessageImageURL `json:"image_url,omitempty"` // ImageURL is the image url of the chat message part.
+ // Text is the text of the chat message part.
+ Text string `json:"text,omitempty"`
+ // Type is the type of the chat message part.
+ Type ChatMessagePartType `json:"type,omitempty"`
+ // ImageURL is the image url of the chat message part.
+ ImageURL *ChatMessageImageURL `json:"image_url,omitempty"`
}
```
-## type [ChatMessagePartType]()
+## type [ChatMessagePartType]()
ChatMessagePartType is the chat message part type.
@@ -807,17 +623,8 @@ string
type ChatMessagePartType string
```
-
-## type [ChatModel]()
-
-ChatModel is the type for chat models present on the groq api.
-
-```go
-type ChatModel model
-```
-
-## type [Client]()
+## type [Client]()
Client is a Groq api client.
@@ -828,7 +635,7 @@ type Client struct {
```
-### func [NewClient]()
+### func [NewClient]()
```go
func NewClient(groqAPIKey string, opts ...Opts) (*Client, error)
@@ -837,7 +644,7 @@ func NewClient(groqAPIKey string, opts ...Opts) (*Client, error)
NewClient creates a new Groq client.
-### func \(\*Client\) [CreateChatCompletion]()
+### func \(\*Client\) [CreateChatCompletion]()
```go
func (c *Client) CreateChatCompletion(ctx context.Context, request ChatCompletionRequest) (response ChatCompletionResponse, err error)
@@ -846,7 +653,7 @@ func (c *Client) CreateChatCompletion(ctx context.Context, request ChatCompletio
CreateChatCompletion method is an API call to create a chat completion.
-### func \(\*Client\) [CreateChatCompletionJSON]()
+### func \(\*Client\) [CreateChatCompletionJSON]()
```go
func (c *Client) CreateChatCompletionJSON(ctx context.Context, request ChatCompletionRequest, output any) (err error)
@@ -855,7 +662,7 @@ func (c *Client) CreateChatCompletionJSON(ctx context.Context, request ChatCompl
CreateChatCompletionJSON method is an API call to create a chat completion w/ object output.
-### func \(\*Client\) [CreateChatCompletionStream]()
+### func \(\*Client\) [CreateChatCompletionStream]()
```go
func (c *Client) CreateChatCompletionStream(ctx context.Context, request ChatCompletionRequest) (stream *ChatCompletionStream, err error)
@@ -866,7 +673,7 @@ CreateChatCompletionStream method is an API call to create a chat completion w/
If set, tokens will be sent as data\-only server\-sent events as they become available, with the stream terminated by a data: \[DONE\] message.
-### func \(\*Client\) [CreateTranscription]()
+### func \(\*Client\) [CreateTranscription]()
```go
func (c *Client) CreateTranscription(ctx context.Context, request AudioRequest) (AudioResponse, error)
@@ -877,7 +684,7 @@ CreateTranscription calls the transcriptions endpoint with the given request.
Returns transcribed text in the response\_format specified in the request.
-### func \(\*Client\) [CreateTranslation]()
+### func \(\*Client\) [CreateTranslation]()
```go
func (c *Client) CreateTranslation(ctx context.Context, request AudioRequest) (AudioResponse, error)
@@ -888,112 +695,58 @@ CreateTranslation calls the translations endpoint with the given request.
Returns the translated text in the response\_format specified in the request.
-### func \(\*Client\) [Moderate]()
+### func \(\*Client\) [Moderate]()
```go
-func (c *Client) Moderate(ctx context.Context, request ModerationRequest) (response Moderation, err error)
+func (c *Client) Moderate(ctx context.Context, messages []ChatCompletionMessage, model models.ModerationModel) (response Moderation, err error)
```
Moderate performs a moderation api call over a string. Input can be an array or slice but a string will reduce the complexity.
-
-### func \(\*Client\) [MustCreateChatCompletion]()
-
-```go
-func (c *Client) MustCreateChatCompletion(ctx context.Context, request ChatCompletionRequest) (response ChatCompletionResponse)
-```
-
-MustCreateChatCompletion method is an API call to create a chat completion.
-
-It panics if an error occurs.
-
-
-## type [DefaultErrorAccumulator]()
-
-DefaultErrorAccumulator is a default implementation of ErrorAccumulator
-
-```go
-type DefaultErrorAccumulator struct {
- Buffer errorBuffer
-}
-```
-
-
-### func \(\*DefaultErrorAccumulator\) [Bytes]()
-
-```go
-func (e *DefaultErrorAccumulator) Bytes() (errBytes []byte)
-```
-
-Bytes method returns the bytes of the error accumulator.
-
-
-### func \(\*DefaultErrorAccumulator\) [Write]()
-
-```go
-func (e *DefaultErrorAccumulator) Write(p []byte) error
-```
-
-Write method writes bytes to the error accumulator.
-
-## type [Endpoint]()
+## type [Endpoint]()
-Endpoint is the endpoint for the groq api. string
+Endpoint is an endpoint for the groq api.
```go
type Endpoint string
```
-
-## type [ErrContentFieldsMisused]()
-
-ErrContentFieldsMisused is an error that occurs when both Content and MultiContent properties are set.
-
-```go
-type ErrContentFieldsMisused struct {
- // contains filtered or unexported fields
-}
-```
-
-
-### func \(ErrContentFieldsMisused\) [Error]()
-
-```go
-func (e ErrContentFieldsMisused) Error() string
-```
-
-Error implements the error interface.
-
-
-## type [ErrTooManyEmptyStreamMessages]()
-
-ErrTooManyEmptyStreamMessages is returned when the stream has sent too many empty messages.
+
+## type [FinishReason]()
-```go
-type ErrTooManyEmptyStreamMessages struct{}
-```
+FinishReason is the finish reason.
-
-### func \(ErrTooManyEmptyStreamMessages\) [Error]()
+string
```go
-func (e ErrTooManyEmptyStreamMessages) Error() string
+type FinishReason string
```
-Error returns the error message.
-
-
-## type [FinishReason]()
-
-FinishReason is the finish reason. string
+
```go
-type FinishReason string
+const (
+ // ReasonStop is the stop finish reason for a chat completion.
+ ReasonStop FinishReason = "stop"
+ // ReasonLength is the length finish reason for a chat completion.
+ ReasonLength FinishReason = "length"
+ // ReasonFunctionCall is the function call finish reason for a chat
+ // completion.
+ ReasonFunctionCall FinishReason = "function_call"
+ // ReasonToolCalls is the tool calls finish reason for a chat
+ // completion.
+ ReasonToolCalls FinishReason = "tool_calls"
+ // ReasonContentFilter is the content filter finish reason for a chat
+ // completion.
+ ReasonContentFilter FinishReason = "content_filter"
+ // ReasonNull is the null finish reason for a chat completion.
+ ReasonNull FinishReason = "null"
+)
```
-### func \(FinishReason\) [MarshalJSON]()
+### func \(FinishReason\) [MarshalJSON]()
```go
func (r FinishReason) MarshalJSON() ([]byte, error)
@@ -1002,7 +755,7 @@ func (r FinishReason) MarshalJSON() ([]byte, error)
MarshalJSON implements the json.Marshaler interface.
-## type [Format]()
+## type [Format]()
Format is the format of a response. string
@@ -1020,101 +773,29 @@ const (
// FormatJSON is the JSON format. There is no support for streaming with
// JSON format selected.
FormatJSON Format = "json"
-)
-```
-
-
-## type [HarmfulCategory]()
-
-HarmfulCategory is a category of harmful content that can be used to categorize a chat history using Llama Guard 3.
-
-\[Llama Guard 3\]\(https://llama.meta.com/docs/model-cards-and-prompt-formats/llama-guard-3/\)
-
-Llama Guard 3 provides content safety support for the following languages: English, French, German, Hindi, Italian, Portuguese, Spanish, and Thai.
-
-string
-
-```go
-type HarmfulCategory string
-```
-
-
-
-```go
-const (
- // CategoryViolentCrimes (S1) is the violent crimes category.
- //
- // Responses that enable, encourage, or endorse the commission of
- // violent crimes, including: (1) unlawful violence toward people
- // (ex: terrorism, genocide, murder, hate-motivated violent crimes,
- // child abuse, assault, battery, kidnapping) and (2) unlawful violence
- // toward animals (ex: animal abuse)
- CategoryViolentCrimes HarmfulCategory = "violent_crimes"
- // CategoryNonviolentCrimes (S2) is the non-violent crimes category.
- CategoryNonviolentCrimes HarmfulCategory = "non_violent_crimes"
- // CategorySexRelatedCrimes (S3) is the sex-related crimes category.
- CategorySexRelatedCrimes HarmfulCategory = "sex_related_crimes"
- // CategoryChildSexualExploitation (S4) is the child sexual exploitation
- // category.
- //
- // Responses that contain, describe, enable, encourage, or endorse the
- // sexual abuse of children.
- CategoryChildSexualExploitation HarmfulCategory = "child_sexual_exploitation"
- // CategoryDefamation (S5) is the defamation category.
- //
- // Signifies responses that contain, describe, enable, encourage, or
- // endorse defamation.
- CategoryDefamation HarmfulCategory = "defamation"
- // CategorySpecializedAdvice (S6) is the specialized advice category.
- //
- // Signifies responses contain, describe, enable, encourage, or endorse specialized advice.
- CategorySpecializedAdvice HarmfulCategory = "specialized_advice"
- // CategoryPrivacy (S7) is the privacy category.
- //
- // Responses contain, describe, enable, encourage, or endorse privacy.
- CategoryPrivacy HarmfulCategory = "privacy"
- // CategoryIntellectualProperty (S8) is the intellectual property
- // category. Responses that contain, describe, enable, encourage, or
- // endorse intellectual property.
- CategoryIntellectualProperty HarmfulCategory = "intellectual_property"
- // CategoryIndiscriminateWeapons (S9) is the indiscriminate weapons
- // category.
- //
- // Responses that contain, describe, enable, encourage, or endorse
- // indiscriminate weapons.
- CategoryIndiscriminateWeapons HarmfulCategory = "indiscriminate_weapons"
- // CategoryHate (S10) is the hate category.
- //
- // Responses contain, describe, enable, encourage, or endorse hate.
- CategoryHate HarmfulCategory = "hate"
- // CategorySuicideAndSelfHarm (S11) is the suicide/self-harm category.
- //
- // Responses contain, describe, enable, encourage, or endorse suicide or self-harm.
- CategorySuicideAndSelfHarm HarmfulCategory = "suicide_and_self_harm"
- // CategorySexualContent (S12) is the sexual content category.
- //
- // Responses contain, describe, enable, encourage, or endorse
- // sexual content.
- CategorySexualContent HarmfulCategory = "sexual_content"
- // CategoryElections (S13) is the elections category.
- //
- // Responses contain factually incorrect information about electoral
- // systems and processes, including in the time, place, or manner of
- // voting in civic elections.
- CategoryElections HarmfulCategory = "elections"
- // CategoryCodeInterpreterAbuse (S14) is the code interpreter abuse
- // category.
- //
- // Responses that contain, describe, enable, encourage, or
- // endorse code interpreter abuse.
- CategoryCodeInterpreterAbuse HarmfulCategory = "code_interpreter_abuse"
+ // FormatSRT is the SRT format. This is a text format that is only
+ // supported for the transcription API.
+ // SRT format selected.
+ FormatSRT Format = "srt"
+ // FormatVTT is the VTT format. This is a text format that is only
+ // supported for the transcription API.
+ FormatVTT Format = "vtt"
+ // FormatVerboseJSON is the verbose JSON format. This is a JSON format
+ // that is only supported for the transcription API.
+ FormatVerboseJSON Format = "verbose_json"
+ // FormatJSONObject is the json object chat
+ // completion response format type.
+ FormatJSONObject Format = "json_object"
+ // FormatJSONSchema is the json schema chat
+ // completion response format type.
+ FormatJSONSchema Format = "json_schema"
)
```
-## type [ImageURLDetail]()
+## type [ImageURLDetail]()
-ImageURLDetail is the image url detail.
+ImageURLDetail is the detail of the image at the URL.
string
@@ -1123,57 +804,46 @@ type ImageURLDetail string
```
-## type [LogProbs]()
+## type [LogProbs]()
LogProbs is the top\-level structure containing the log probability information.
```go
type LogProbs struct {
+ // Content is a list of message content tokens with log
+ // probability information.
Content []struct {
- Token string `json:"token"` // Token is the token of the log prob.
- LogProb float64 `json:"logprob"` // LogProb is the log prob of the log prob.
- Bytes []byte `json:"bytes,omitempty"` // Omitting the field if it is null
- TopLogProbs []TopLogProbs `json:"top_logprobs"` // TopLogProbs is a list of the most likely tokens and their log probability, at this token position. In rare cases, there may be fewer than the number of requested top_logprobs returned.
- } `json:"content"` // Content is a list of message content tokens with log probability information.
+ // Token is the token of the log prob.
+ Token string `json:"token"`
+ // LogProb is the log prob of the log prob.
+ LogProb float64 `json:"logprob"`
+ // Omitting the field if it is null
+ Bytes []byte `json:"bytes,omitempty"`
+ // TopLogProbs is a list of the most likely tokens and
+ // their log probability, at this token position. In
+ // rare cases, there may be fewer than the number of
+ // requested top_logprobs returned.
+ TopLogProbs []TopLogProbs `json:"top_logprobs"`
+ } `json:"content"`
}
```
-## type [Moderation]()
+## type [Moderation]()
-Moderation represents one of possible moderation results.
+Moderation represents the response of a moderation request.
```go
type Moderation struct {
- Categories []HarmfulCategory `json:"categories"` // Categories is the categories of the result.
- Flagged bool `json:"flagged"` // Flagged is the flagged of the result.
-}
-```
-
-
-## type [ModerationModel]()
-
-ModerationModel is the type for moderation models present on the groq api.
-
-```go
-type ModerationModel model
-```
-
-
-## type [ModerationRequest]()
-
-ModerationRequest represents a request structure for moderation API.
-
-```go
-type ModerationRequest struct {
- // Input string `json:"input,omitempty"` // Input is the input text to be moderated.
- Messages []ChatCompletionMessage `json:"messages"` // Messages is the messages of the chat completion request. These act as the prompt for the model.
- Model ModerationModel `json:"model,omitempty"` // Model is the model to use for the moderation.
+ // Categories is the categories of the result.
+ Categories []moderation.HarmfulCategory `json:"categories"`
+ // Flagged is the flagged status of the result.
+ Flagged bool `json:"flagged"`
}
```
-## type [Opts]()
+## type [Opts]()
Opts is a function that sets options for a Groq client.
@@ -1182,7 +852,7 @@ type Opts func(*Client)
```
-### func [WithBaseURL]()
+### func [WithBaseURL]()
```go
func WithBaseURL(baseURL string) Opts
@@ -1191,7 +861,7 @@ func WithBaseURL(baseURL string) Opts
WithBaseURL sets the base URL for the Groq client.
-### func [WithClient]()
+### func [WithClient]()
```go
func WithClient(client *http.Client) Opts
@@ -1200,7 +870,7 @@ func WithClient(client *http.Client) Opts
WithClient sets the client for the Groq client.
-### func [WithLogger]()
+### func [WithLogger]()
```go
func WithLogger(logger *slog.Logger) Opts
@@ -1209,7 +879,7 @@ func WithLogger(logger *slog.Logger) Opts
WithLogger sets the logger for the Groq client.
-## type [PromptAnnotation]()
+## type [PromptAnnotation]()
PromptAnnotation represents the prompt annotation.
@@ -1220,23 +890,33 @@ type PromptAnnotation struct {
```
-## type [RateLimitHeaders]()
+## type [RateLimitHeaders]()
RateLimitHeaders struct represents Groq rate limits headers.
```go
type RateLimitHeaders struct {
- LimitRequests int `json:"x-ratelimit-limit-requests"` // LimitRequests is the limit requests of the rate limit headers.
- LimitTokens int `json:"x-ratelimit-limit-tokens"` // LimitTokens is the limit tokens of the rate limit headers.
- RemainingRequests int `json:"x-ratelimit-remaining-requests"` // RemainingRequests is the remaining requests of the rate limit headers.
- RemainingTokens int `json:"x-ratelimit-remaining-tokens"` // RemainingTokens is the remaining tokens of the rate limit headers.
- ResetRequests ResetTime `json:"x-ratelimit-reset-requests"` // ResetRequests is the reset requests of the rate limit headers.
- ResetTokens ResetTime `json:"x-ratelimit-reset-tokens"` // ResetTokens is the reset tokens of the rate limit headers.
+ // LimitRequests is the limit requests of the rate limit
+ // headers.
+ LimitRequests int `json:"x-ratelimit-limit-requests"`
+ // LimitTokens is the limit tokens of the rate limit headers.
+ LimitTokens int `json:"x-ratelimit-limit-tokens"`
+ // RemainingRequests is the remaining requests of the rate
+ // limit headers.
+ RemainingRequests int `json:"x-ratelimit-remaining-requests"`
+ // RemainingTokens is the remaining tokens of the rate limit
+ // headers.
+ RemainingTokens int `json:"x-ratelimit-remaining-tokens"`
+ // ResetRequests is the reset requests of the rate limit
+ // headers.
+ ResetRequests ResetTime `json:"x-ratelimit-reset-requests"`
+ // ResetTokens is the reset tokens of the rate limit headers.
+ ResetTokens ResetTime `json:"x-ratelimit-reset-tokens"`
}
```
-## type [ResetTime]()
+## type [ResetTime]()
ResetTime is a time.Time wrapper for the rate limit reset time. string
@@ -1245,7 +925,7 @@ type ResetTime string
```
-### func \(ResetTime\) [String]()
+### func \(ResetTime\) [String]()
```go
func (r ResetTime) String() string
@@ -1254,7 +934,7 @@ func (r ResetTime) String() string
String returns the string representation of the ResetTime.
-### func \(ResetTime\) [Time]()
+### func \(ResetTime\) [Time]()
```go
func (r ResetTime) Time() time.Time
@@ -1263,7 +943,7 @@ func (r ResetTime) Time() time.Time
Time returns the time.Time representation of the ResetTime.
-## type [Role]()
+## type [Role]()
Role is the role of the chat completion message.
@@ -1274,56 +954,116 @@ type Role string
```
-## type [Segments]()
+## type [Segments]()
Segments is the segments of the response.
```go
type Segments []struct {
- ID int `json:"id"` // ID is the ID of the segment.
- Seek int `json:"seek"` // Seek is the seek of the segment.
- Start float64 `json:"start"` // Start is the start of the segment.
- End float64 `json:"end"` // End is the end of the segment.
- Text string `json:"text"` // Text is the text of the segment.
- Tokens []int `json:"tokens"` // Tokens is the tokens of the segment.
- Temperature float64 `json:"temperature"` // Temperature is the temperature of the segment.
- AvgLogprob float64 `json:"avg_logprob"` // AvgLogprob is the avg log prob of the segment.
- CompressionRatio float64 `json:"compression_ratio"` // CompressionRatio is the compression ratio of the segment.
- NoSpeechProb float64 `json:"no_speech_prob"` // NoSpeechProb is the no speech prob of the segment.
- Transient bool `json:"transient"` // Transient is the transient of the segment.
+ // ID is the ID of the segment.
+ ID int `json:"id"`
+ // Seek is the seek of the segment.
+ Seek int `json:"seek"`
+ // Start is the start of the segment.
+ Start float64 `json:"start"`
+ // End is the end of the segment.
+ End float64 `json:"end"`
+ // Text is the text of the segment.
+ Text string `json:"text"`
+ // Tokens is the tokens of the segment.
+ Tokens []int `json:"tokens"`
+ // Temperature is the temperature of the segment.
+ Temperature float64 `json:"temperature"`
+ // AvgLogprob is the avg log prob of the segment.
+ AvgLogprob float64 `json:"avg_logprob"`
+ // CompressionRatio is the compression ratio of the segment.
+ CompressionRatio float64 `json:"compression_ratio"`
+ // NoSpeechProb is the no speech prob of the segment.
+ NoSpeechProb float64 `json:"no_speech_prob"`
+ // Transient is the transient of the segment.
+ Transient bool `json:"transient"`
}
```
-## type [StreamOptions]()
+## type [StreamOptions]()
StreamOptions represents the stream options.
```go
type StreamOptions struct {
- // If set, an additional chunk will be streamed before the data: [DONE] message.
- // The usage field on this chunk shows the token usage statistics for the entire request,
- // and the choices field will always be an empty array.
- // All other chunks will also include a usage field, but with a null value.
+ // IncludeUsage is the include usage option of the stream
+ // options.
+ //
+ // If set, an additional chunk will be streamed before the data:
+ // [DONE] message.
+ // The usage field on this chunk shows the token usage
+ // statistics for the entire request, and the choices field will
+ // always be an empty array.
+ //
+ // All other chunks will also include a usage field, but with a
+ // null value.
IncludeUsage bool `json:"include_usage,omitempty"`
}
```
+
+## type [ToolGetter]()
+
+ToolGetter is an interface for a tool getter.
+
+```go
+type ToolGetter interface {
+ Get(
+ ctx context.Context,
+ ) ([]tools.Tool, error)
+}
+```
+
+
+## type [ToolManager]()
+
+ToolManager is an interface for a tool manager.
+
+```go
+type ToolManager interface {
+ ToolGetter
+ ToolRunner
+}
+```
+
+
+## type [ToolRunner]()
+
+ToolRunner is an interface for a tool runner.
+
+```go
+type ToolRunner interface {
+ Run(
+ ctx context.Context,
+ response ChatCompletionResponse,
+ ) ([]ChatCompletionMessage, error)
+}
+```
+
-## type [TopLogProbs]()
+## type [TopLogProbs]()
TopLogProbs represents the top log probs.
```go
type TopLogProbs struct {
- Token string `json:"token"` // Token is the token of the top log probs.
- LogProb float64 `json:"logprob"` // LogProb is the log prob of the top log probs.
- Bytes []byte `json:"bytes,omitempty"` // Bytes is the bytes of the top log probs.
+ // Token is the token of the top log probs.
+ Token string `json:"token"`
+ // LogProb is the log prob of the top log probs.
+ LogProb float64 `json:"logprob"`
+ // Bytes is the bytes of the top log probs.
+ Bytes []byte `json:"bytes,omitempty"`
}
```
-## type [TranscriptionTimestampGranularity]()
+## type [TranscriptionTimestampGranularity]()
TranscriptionTimestampGranularity is the timestamp granularity for the transcription.
@@ -1333,8 +1073,21 @@ string
type TranscriptionTimestampGranularity string
```
+
+
+```go
+const (
+ // TranscriptionTimestampGranularityWord is the word timestamp
+ // granularity.
+ TranscriptionTimestampGranularityWord TranscriptionTimestampGranularity = "word"
+ // TranscriptionTimestampGranularitySegment is the segment timestamp
+ // granularity.
+ TranscriptionTimestampGranularitySegment TranscriptionTimestampGranularity = "segment"
+)
+```
+
-## type [Usage]()
+## type [Usage]()
Usage Represents the total token usage per request to Groq.
@@ -1347,15 +1100,19 @@ type Usage struct {
```
-## type [Words]()
+## type [Words]()
-Words is the words of the response.
+Words is the words of the audio response.
```go
type Words []struct {
- Word string `json:"word"` // Word is the word of the words.
- Start float64 `json:"start"` // Start is the start of the words.
- End float64 `json:"end"` // End is the end of the words.
+ // Word is the textual representation of a word in the audio
+ // response.
+ Word string `json:"word"`
+ // Start is the start of the words in seconds.
+ Start float64 `json:"start"`
+ // End is the end of the words in seconds.
+ End float64 `json:"end"`
}
```
diff --git a/agents.go b/agents.go
new file mode 100644
index 0000000..df5693e
--- /dev/null
+++ b/agents.go
@@ -0,0 +1,63 @@
+package groq
+
+import (
+ "context"
+ "log/slog"
+
+ "github.com/conneroisu/groq-go/pkg/tools"
+)
+
+type (
+ // Agenter is an interface for an agent.
+ Agenter interface {
+ ToolManager
+ }
+ // ToolManager is an interface for a tool manager.
+ ToolManager interface {
+ ToolGetter
+ ToolRunner
+ }
+ // ToolGetter is an interface for a tool getter.
+ ToolGetter interface {
+ Get(
+ ctx context.Context,
+ params ToolGetParams,
+ ) ([]tools.Tool, error)
+ }
+ // ToolRunner is an interface for a tool runner.
+ ToolRunner interface {
+ Run(
+ ctx context.Context,
+ response ChatCompletionResponse,
+ ) ([]ChatCompletionMessage, error)
+ }
+ // ToolGetParams are the parameters for getting tools.
+ ToolGetParams struct {
+ }
+ // Router is an agent router.
+ //
+ // It is used to route messages to the appropriate model.
+ Router struct {
+ // Agents is the agents of the router.
+ Agents []Agent
+ // Logger is the logger of the router.
+ Logger *slog.Logger
+ }
+)
+
+// Agent is an agent.
+type Agent struct {
+ client *Client
+ logger *slog.Logger
+}
+
+// NewAgent creates a new agent.
+func NewAgent(
+ client *Client,
+ logger *slog.Logger,
+) *Agent {
+ return &Agent{
+ client: client,
+ logger: logger,
+ }
+}
diff --git a/audio.go b/audio.go
index aecffee..a889b00 100644
--- a/audio.go
+++ b/audio.go
@@ -9,76 +9,103 @@ import (
"os"
"github.com/conneroisu/groq-go/pkg/builders"
+ "github.com/conneroisu/groq-go/pkg/models"
)
const (
- AudioResponseFormatJSON AudioResponseFormat = "json" // AudioResponseFormatJSON is the JSON response format of some audio.
- AudioResponseFormatText AudioResponseFormat = "text" // AudioResponseFormatText is the text response format of some audio.
- AudioResponseFormatSRT AudioResponseFormat = "srt" // AudioResponseFormatSRT is the SRT response format of some audio.
- AudioResponseFormatVerboseJSON AudioResponseFormat = "verbose_json" // AudioResponseFormatVerboseJSON is the verbose JSON response format of some audio.
- AudioResponseFormatVTT AudioResponseFormat = "vtt" // AudioResponseFormatVTT is the VTT response format of some audio.
-
- TranscriptionTimestampGranularityWord TranscriptionTimestampGranularity = "word" // TranscriptionTimestampGranularityWord is the word timestamp granularity.
- TranscriptionTimestampGranularitySegment TranscriptionTimestampGranularity = "segment" // TranscriptionTimestampGranularitySegment is the segment timestamp granularity.
+ // TranscriptionTimestampGranularityWord is the word timestamp
+ // granularity.
+ TranscriptionTimestampGranularityWord TranscriptionTimestampGranularity = "word"
+ // TranscriptionTimestampGranularitySegment is the segment timestamp
+ // granularity.
+ TranscriptionTimestampGranularitySegment TranscriptionTimestampGranularity = "segment"
)
type (
- // AudioResponseFormat is the response format for the audio API.
- //
- // Response formatted using AudioResponseFormatJSON by default.
- //
- // string
- AudioResponseFormat string
- // TranscriptionTimestampGranularity is the timestamp granularity for the transcription.
+ // TranscriptionTimestampGranularity is the timestamp granularity for
+ // the transcription.
//
// string
TranscriptionTimestampGranularity string
// AudioRequest represents a request structure for audio API.
AudioRequest struct {
- Model AudioModel // Model is the model to use for the transcription.
- FilePath string // FilePath is either an existing file in your filesystem or a filename representing the contents of Reader.
- Reader io.Reader // Reader is an optional io.Reader when you do not want to use an existing file.
- Prompt string // Prompt is the prompt for the transcription.
- Temperature float32 // Temperature is the temperature for the transcription.
- Language string // Language is the language for the transcription. Only for transcription.
- Format AudioResponseFormat // Format is the format for the response.
+ // Model is the model to use for the transcription.
+ Model models.AudioModel
+ // FilePath is either an existing file in your filesystem or a
+ // filename representing the contents of Reader.
+ FilePath string
+ // Reader is an optional io.Reader when you do not want to use
+ // an existing file.
+ Reader io.Reader
+ // Prompt is the prompt for the transcription.
+ Prompt string
+ // Temperature is the temperature for the transcription.
+ Temperature float32
+ // Language is the language for the transcription. Only for
+ // transcription.
+ Language string
+ // Format is the format for the response.
+ Format Format
}
// AudioResponse represents a response structure for audio API.
AudioResponse struct {
- Task string `json:"task"` // Task is the task of the response.
- Language string `json:"language"` // Language is the language of the response.
- Duration float64 `json:"duration"` // Duration is the duration of the response.
- Segments Segments `json:"segments"` // Segments is the segments of the response.
- Words Words `json:"words"` // Words is the words of the response.
- Text string `json:"text"` // Text is the text of the response.
+ // Task is the task of the response.
+ Task string `json:"task"`
+ // Language is the language of the response.
+ Language string `json:"language"`
+ // Duration is the duration of the response.
+ Duration float64 `json:"duration"`
+ // Segments is the segments of the response.
+ Segments Segments `json:"segments"`
+ // Words is the words of the response.
+ Words Words `json:"words"`
+ // Text is the text of the response.
+ Text string `json:"text"`
Header http.Header // Header is the header of the response.
}
- // Words is the words of the response.
+ // Words is the words of the audio response.
Words []struct {
- Word string `json:"word"` // Word is the word of the words.
- Start float64 `json:"start"` // Start is the start of the words.
- End float64 `json:"end"` // End is the end of the words.
+ // Word is the textual representation of a word in the audio
+ // response.
+ Word string `json:"word"`
+ // Start is the start of the words in seconds.
+ Start float64 `json:"start"`
+ // End is the end of the words in seconds.
+ End float64 `json:"end"`
}
// Segments is the segments of the response.
Segments []struct {
- ID int `json:"id"` // ID is the ID of the segment.
- Seek int `json:"seek"` // Seek is the seek of the segment.
- Start float64 `json:"start"` // Start is the start of the segment.
- End float64 `json:"end"` // End is the end of the segment.
- Text string `json:"text"` // Text is the text of the segment.
- Tokens []int `json:"tokens"` // Tokens is the tokens of the segment.
- Temperature float64 `json:"temperature"` // Temperature is the temperature of the segment.
- AvgLogprob float64 `json:"avg_logprob"` // AvgLogprob is the avg log prob of the segment.
- CompressionRatio float64 `json:"compression_ratio"` // CompressionRatio is the compression ratio of the segment.
- NoSpeechProb float64 `json:"no_speech_prob"` // NoSpeechProb is the no speech prob of the segment.
- Transient bool `json:"transient"` // Transient is the transient of the segment.
+ // ID is the ID of the segment.
+ ID int `json:"id"`
+ // Seek is the seek of the segment.
+ Seek int `json:"seek"`
+ // Start is the start of the segment.
+ Start float64 `json:"start"`
+ // End is the end of the segment.
+ End float64 `json:"end"`
+ // Text is the text of the segment.
+ Text string `json:"text"`
+ // Tokens is the tokens of the segment.
+ Tokens []int `json:"tokens"`
+ // Temperature is the temperature of the segment.
+ Temperature float64 `json:"temperature"`
+ // AvgLogprob is the avg log prob of the segment.
+ AvgLogprob float64 `json:"avg_logprob"`
+ // CompressionRatio is the compression ratio of the segment.
+ CompressionRatio float64 `json:"compression_ratio"`
+ // NoSpeechProb is the no speech prob of the segment.
+ NoSpeechProb float64 `json:"no_speech_prob"`
+ // Transient is the transient of the segment.
+ Transient bool `json:"transient"`
}
// audioTextResponse is the response structure for the audio API when the
// response format is text.
audioTextResponse struct {
- Text string `json:"text"` // Text is the text of the response.
- header http.Header `json:"-"` // Header is the response header.
+ // Text is the text of the response.
+ Text string `json:"text"`
+ // Header is the response header.
+ header http.Header `json:"-"`
}
)
@@ -122,7 +149,7 @@ func (c *Client) callAudioAPI(
endpointSuffix Endpoint,
) (response AudioResponse, err error) {
var formBody bytes.Buffer
- c.requestFormBuilder = c.createFormBuilder(&formBody)
+ c.requestFormBuilder = builders.NewFormBuilder(&formBody)
err = AudioMultipartForm(request, c.requestFormBuilder)
if err != nil {
return AudioResponse{}, err
@@ -131,7 +158,7 @@ func (c *Client) callAudioAPI(
ctx,
c.header,
http.MethodPost,
- c.fullURL(endpointSuffix, withModel(model(request.Model))),
+ c.fullURL(endpointSuffix, withModel(request.Model)),
builders.WithBody(&formBody),
builders.WithContentType(c.requestFormBuilder.FormDataContentType()),
)
@@ -153,14 +180,14 @@ func (c *Client) callAudioAPI(
}
func (r AudioRequest) hasJSONResponse() bool {
- return r.Format == "" || r.Format == AudioResponseFormatJSON ||
- r.Format == AudioResponseFormatVerboseJSON
+ return r.Format == "" || r.Format == FormatJSON ||
+ r.Format == FormatVerboseJSON
}
// AudioMultipartForm creates a form with audio file contents and the name of
// the model to use for audio processing.
func AudioMultipartForm(request AudioRequest, b builders.FormBuilder) error {
- err := CreateFileField(request, b)
+ err := createFileField(request, b)
if err != nil {
return err
}
@@ -202,9 +229,7 @@ func AudioMultipartForm(request AudioRequest, b builders.FormBuilder) error {
return b.Close()
}
-// CreateFileField creates the "file" form field from either an existing file
-// or by using the reader.
-func CreateFileField(
+func createFileField(
request AudioRequest,
b builders.FormBuilder,
) (err error) {
diff --git a/audio_test.go b/audio_test.go
index dabb781..02c126c 100644
--- a/audio_test.go
+++ b/audio_test.go
@@ -26,7 +26,7 @@ func TestAudioWithFailingFormBuilder(t *testing.T) {
Prompt: "test",
Temperature: 0.5,
Language: "en",
- Format: groq.AudioResponseFormatSRT,
+ Format: groq.FormatSRT,
}
mockFailedErr := fmt.Errorf("mock form builder fail")
@@ -94,7 +94,7 @@ func TestCreateFileField(t *testing.T) {
return mockFailedErr
},
}
- err := groq.CreateFileField(req, mockBuilder)
+ err := groq.AudioMultipartForm(req, mockBuilder)
a.ErrorIs(
err,
mockFailedErr,
@@ -116,7 +116,7 @@ func TestCreateFileField(t *testing.T) {
},
}
- err := groq.CreateFileField(req, mockBuilder)
+ err := groq.AudioMultipartForm(req, mockBuilder)
a.ErrorIs(
err,
mockFailedErr,
@@ -130,7 +130,7 @@ func TestCreateFileField(t *testing.T) {
FilePath: "non_existing_file.wav",
}
mockBuilder := builders.NewFormBuilder(&test.FailingErrorBuffer{})
- err := groq.CreateFileField(req, mockBuilder)
+ err := groq.AudioMultipartForm(req, mockBuilder)
a.Error(
err,
"createFileField using file should return error when open file fails",
diff --git a/chat.go b/chat.go
index 04a4a12..0b9e26e 100644
--- a/chat.go
+++ b/chat.go
@@ -1,46 +1,74 @@
package groq
import (
- "bufio"
- "bytes"
"context"
"encoding/json"
"fmt"
- "io"
"net/http"
"reflect"
"strings"
"time"
"github.com/conneroisu/groq-go/pkg/builders"
+ "github.com/conneroisu/groq-go/pkg/groqerr"
+ "github.com/conneroisu/groq-go/pkg/models"
"github.com/conneroisu/groq-go/pkg/schema"
+ "github.com/conneroisu/groq-go/pkg/streams"
"github.com/conneroisu/groq-go/pkg/tools"
)
const (
- ChatMessageRoleSystem Role = "system" // ChatMessageRoleSystem is the system chat message role.
- ChatMessageRoleUser Role = "user" // ChatMessageRoleUser is the user chat message role.
- ChatMessageRoleAssistant Role = "assistant" // ChatMessageRoleAssistant is the assistant chat message role.
- ChatMessageRoleFunction Role = "function" // ChatMessageRoleFunction is the function chat message role.
- ChatMessageRoleTool Role = "tool" // ChatMessageRoleTool is the tool chat message role.
- ImageURLDetailHigh ImageURLDetail = "high" // ImageURLDetailHigh is the high image url detail.
- ImageURLDetailLow ImageURLDetail = "low" // ImageURLDetailLow is the low image url detail.
- ImageURLDetailAuto ImageURLDetail = "auto" // ImageURLDetailAuto is the auto image url detail.
- ChatMessagePartTypeText ChatMessagePartType = "text" // ChatMessagePartTypeText is the text chat message part type.
- ChatMessagePartTypeImageURL ChatMessagePartType = "image_url" // ChatMessagePartTypeImageURL is the image url chat message part type.
- ChatCompletionResponseFormatTypeJSONObject ChatCompletionResponseFormatType = "json_object" // ChatCompletionResponseFormatTypeJSONObject is the json object chat completion response format type.
- ChatCompletionResponseFormatTypeJSONSchema ChatCompletionResponseFormatType = "json_schema" // ChatCompletionResponseFormatTypeJSONSchema is the json schema chat completion response format type.
- ChatCompletionResponseFormatTypeText ChatCompletionResponseFormatType = "text" // ChatCompletionResponseFormatTypeText is the text chat completion response format type.
- FinishReasonStop FinishReason = "stop" // FinishReasonStop is the stop finish reason.
- FinishReasonLength FinishReason = "length" // FinishReasonLength is the length finish reason.
- FinishReasonFunctionCall FinishReason = "function_call" // FinishReasonFunctionCall is the function call finish reason.
- FinishReasonToolCalls FinishReason = "tool_calls" // FinishReasonToolCalls is the tool calls finish reason.
- FinishReasonContentFilter FinishReason = "content_filter" // FinishReasonContentFilter is the content filter finish reason.
- FinishReasonNull FinishReason = "null" // FinishReasonNull is the null finish reason.
+ // ChatMessageRoleSystem is the system chat message role.
+ ChatMessageRoleSystem Role = "system"
+ // ChatMessageRoleUser is the user chat message role.
+ ChatMessageRoleUser Role = "user"
+ // ChatMessageRoleAssistant is the assistant chat message role.
+ ChatMessageRoleAssistant Role = "assistant"
+ // ChatMessageRoleFunction is the function chat message role.
+ ChatMessageRoleFunction Role = "function"
+ // ChatMessageRoleTool is the tool chat message role.
+ ChatMessageRoleTool Role = "tool"
+
+ // ImageURLDetailHigh is the high image url detail.
+ ImageURLDetailHigh ImageURLDetail = "high"
+ // ImageURLDetailLow is the low image url detail.
+ ImageURLDetailLow ImageURLDetail = "low"
+ // ImageURLDetailAuto is the auto image url detail.
+ ImageURLDetailAuto ImageURLDetail = "auto"
+
+ // ChatMessagePartTypeText is the text chat message part type.
+ ChatMessagePartTypeText ChatMessagePartType = "text"
+ // ChatMessagePartTypeImageURL is the image url chat message part type.
+ ChatMessagePartTypeImageURL ChatMessagePartType = "image_url"
)
type (
- // ImageURLDetail is the image url detail.
+ // FinishReason is the finish reason.
+ //
+ // string
+ FinishReason string
+)
+
+const (
+ // ReasonStop is the stop finish reason for a chat completion.
+ ReasonStop FinishReason = "stop"
+ // ReasonLength is the length finish reason for a chat completion.
+ ReasonLength FinishReason = "length"
+ // ReasonFunctionCall is the function call finish reason for a chat
+ // completion.
+ ReasonFunctionCall FinishReason = "function_call"
+ // ReasonToolCalls is the tool calls finish reason for a chat
+ // completion.
+ ReasonToolCalls FinishReason = "tool_calls"
+ // ReasonContentFilter is the content filter finish reason for a chat
+ // completion.
+ ReasonContentFilter FinishReason = "content_filter"
+ // ReasonNull is the null finish reason for a chat completion.
+ ReasonNull FinishReason = "null"
+)
+
+type (
+ // ImageURLDetail is the detail of the image at the URL.
//
// string
ImageURLDetail string
@@ -58,35 +86,50 @@ type (
}
// ChatMessageImageURL represents the chat message image url.
ChatMessageImageURL struct {
- URL string `json:"url,omitempty"` // URL is the url of the image.
- Detail ImageURLDetail `json:"detail,omitempty"` // Detail is the detail of the image url.
+ // URL is the url of the image.
+ URL string `json:"url,omitempty"`
+ // Detail is the detail of the image url.
+ Detail ImageURLDetail `json:"detail,omitempty"`
}
// ChatMessagePart represents the chat message part of a chat completion
// message.
ChatMessagePart struct {
- Text string `json:"text,omitempty"` // Text is the text of the chat message part.
- Type ChatMessagePartType `json:"type,omitempty"` // Type is the type of the chat message part.
- ImageURL *ChatMessageImageURL `json:"image_url,omitempty"` // ImageURL is the image url of the chat message part.
+ // Text is the text of the chat message part.
+ Text string `json:"text,omitempty"`
+ // Type is the type of the chat message part.
+ Type ChatMessagePartType `json:"type,omitempty"`
+ // ImageURL is the image url of the chat message part.
+ ImageURL *ChatMessageImageURL `json:"image_url,omitempty"`
}
// ChatCompletionMessage represents the chat completion message.
ChatCompletionMessage struct {
- Name string `json:"name"` // Name is the name of the chat completion message.
- Role Role `json:"role"` // Role is the role of the chat completion message.
- Content string `json:"content"` // Content is the content of the chat completion message.
- MultiContent []ChatMessagePart `json:"-"` // MultiContent is the multi content of the chat completion message.
- FunctionCall *tools.FunctionCall `json:"function_call,omitempty"` // FunctionCall setting for Role=assistant prompts this may be set to the function call generated by the model.
- ToolCalls []tools.ToolCall `json:"tool_calls,omitempty"` // ToolCalls setting for Role=assistant prompts this may be set to the tool calls generated by the model, such as function calls.
- ToolCallID string `json:"tool_call_id,omitempty"` // ToolCallID is setting for Role=tool prompts this should be set to the ID given in the assistant's prior request to call a tool.
+ // Name is the name of the chat completion message.
+ Name string `json:"name"`
+ // Role is the role of the chat completion message.
+ Role Role `json:"role"`
+ // Content is the content of the chat completion message.
+ Content string `json:"content"`
+ // MultiContent is the multi content of the chat completion
+ // message.
+ MultiContent []ChatMessagePart `json:"-"`
+ // FunctionCall setting for Role=assistant prompts this may be
+ // set to the function call generated by the model.
+ FunctionCall *tools.FunctionCall `json:"function_call,omitempty"`
+ // ToolCalls setting for Role=assistant prompts this may be set
+ // to the tool calls generated by the model, such as function
+ // calls.
+ ToolCalls []tools.ToolCall `json:"tool_calls,omitempty"`
+ // ToolCallID is setting for Role=tool prompts this should be
+ // set to the ID given in the assistant's prior request to call
+ // a tool.
+ ToolCallID string `json:"tool_call_id,omitempty"`
}
- // ChatCompletionResponseFormatType is the chat completion response format type.
- //
- // string
- ChatCompletionResponseFormatType string
// ChatCompletionResponseFormat is the chat completion response format.
ChatCompletionResponseFormat struct {
// Type is the type of the chat completion response format.
- Type ChatCompletionResponseFormatType `json:"type,omitempty"`
- // JSONSchema is the json schema of the chat completion response format.
+ Type Format `json:"type,omitempty"`
+ // JSONSchema is the json schema of the chat completion response
+ // format.
JSONSchema *ChatCompletionResponseFormatJSONSchema `json:"json_schema,omitempty"`
}
// ChatCompletionResponseFormatJSONSchema is the chat completion
@@ -97,143 +140,220 @@ type (
//
// it is used to further identify the schema in the response.
Name string `json:"name"`
- // Description is the description of the chat completion response
- // format
- // json schema.
+ // Description is the description of the chat completion
+ // response format json schema.
Description string `json:"description,omitempty"`
- // description of the chat completion response format json schema.
- // Schema is the schema of the chat completion response format json schema.
+ // Schema is the schema of the chat completion response format
+ // json schema.
Schema schema.Schema `json:"schema"`
- // Strict determines whether to enforce the schema upon the generated
- // content.
+ // Strict determines whether to enforce the schema upon the
+ // generated content.
Strict bool `json:"strict"`
}
- // ChatCompletionRequest represents a request structure for the chat completion API.
+ // ChatCompletionRequest represents a request structure for the chat
+ // completion API.
ChatCompletionRequest struct {
- Model ChatModel `json:"model"` // Model is the model of the chat completion request.
- Messages []ChatCompletionMessage `json:"messages"` // Messages is the messages of the chat completion request. These act as the prompt for the model.
- MaxTokens int `json:"max_tokens,omitempty"` // MaxTokens is the max tokens of the chat completion request.
- Temperature float32 `json:"temperature,omitempty"` // Temperature is the temperature of the chat completion request.
- TopP float32 `json:"top_p,omitempty"` // TopP is the top p of the chat completion request.
- N int `json:"n,omitempty"` // N is the n of the chat completion request.
- Stream bool `json:"stream,omitempty"` // Stream is the stream of the chat completion request.
- Stop []string `json:"stop,omitempty"` // Stop is the stop of the chat completion request.
- PresencePenalty float32 `json:"presence_penalty,omitempty"` // PresencePenalty is the presence penalty of the chat completion request.
- ResponseFormat *ChatCompletionResponseFormat `json:"response_format,omitempty"` // ResponseFormat is the response format of the chat completion request.
- Seed *int `json:"seed,omitempty"` // Seed is the seed of the chat completion request.
- FrequencyPenalty float32 `json:"frequency_penalty,omitempty"` // FrequencyPenalty is the frequency penalty of the chat completion request.
- LogitBias map[string]int `json:"logit_bias,omitempty"` // LogitBias is must be a token id string (specified by their token ID in the tokenizer), not a word string. incorrect: `"logit_bias":{ "You": 6}`, correct: `"logit_bias":{"1639": 6}` refs: https://platform.openai.com/docs/api-reference/chat/create#chat/create-logit_bias
- LogProbs bool `json:"logprobs,omitempty"` // LogProbs indicates whether to return log probabilities of the output tokens or not. If true, returns the log probabilities of each output token returned in the content of message. This option is currently not available on the gpt-4-vision-preview model.
- TopLogProbs int `json:"top_logprobs,omitempty"` // TopLogProbs is an integer between 0 and 5 specifying the number of most likely tokens to return at each token position, each with an associated log probability. logprobs must be set to true if this parameter is used.
- User string `json:"user,omitempty"` // User is the user of the chat completion request.
- Tools []tools.Tool `json:"tools,omitempty"` // Tools is the tools of the chat completion request.
- ToolChoice any `json:"tool_choice,omitempty"` // This can be either a string or an ToolChoice object.
- StreamOptions *StreamOptions `json:"stream_options,omitempty"` // Options for streaming response. Only set this when you set stream: true.
- ParallelToolCalls any `json:"parallel_tool_calls,omitempty"` // Disable the default behavior of parallel tool calls by setting it: false.
- RetryDelay time.Duration `json:"-"` // RetryDelay is the delay between retries.
+ // Model is the model of the chat completion request.
+ Model models.ChatModel `json:"model"`
+ // Messages is the messages of the chat completion request.
+ //
+ // These act as the prompt for the model.
+ Messages []ChatCompletionMessage `json:"messages"`
+ // MaxTokens is the max tokens of the chat completion request.
+ MaxTokens int `json:"max_tokens,omitempty"`
+ // Temperature is the temperature of the chat completion
+ // request.
+ Temperature float32 `json:"temperature,omitempty"`
+ // TopP is the top p of the chat completion request.
+ TopP float32 `json:"top_p,omitempty"`
+ // N is the n of the chat completion request.
+ N int `json:"n,omitempty"`
+ // Stream is the stream of the chat completion request.
+ Stream bool `json:"stream,omitempty"`
+ // Stop is the stop of the chat completion request.
+ Stop []string `json:"stop,omitempty"`
+ // PresencePenalty is the presence penalty of the chat
+ // completion request.
+ PresencePenalty float32 `json:"presence_penalty,omitempty"`
+ // ResponseFormat is the response format of the chat completion
+ // request.
+ ResponseFormat *ChatCompletionResponseFormat `json:"response_format,omitempty"`
+ // Seed is the seed of the chat completion request.
+ Seed *int `json:"seed,omitempty"`
+ // FrequencyPenalty is the frequency penalty of the chat
+ // completion request.
+ FrequencyPenalty float32 `json:"frequency_penalty,omitempty"`
+ // LogitBias is must be a token id string (specified by their
+ // token ID in the tokenizer), not a word string. incorrect: `"logit_bias":{ "You": 6}`, correct: `"logit_bias":{"1639": 6}` refs: https://platform.openai.com/docs/api-reference/chat/create#chat/create-logit_bias
+ LogitBias map[string]int `json:"logit_bias,omitempty"`
+ // LogProbs indicates whether to return log probabilities of the
+ // output tokens or not. If true, returns the log probabilities
+ // of each output token returned in the content of message.
+ //
+ // This option is currently not available on the
+ // gpt-4-vision-preview model.
+ LogProbs bool `json:"logprobs,omitempty"`
+ // TopLogProbs is an integer between 0 and 5 specifying the
+ // number of most likely tokens to return at each token
+ // position, each with an associated log probability. Logprobs
+ // must be set to true if this parameter is used.
+ TopLogProbs int `json:"top_logprobs,omitempty"`
+ // User is the user of the chat completion request.
+ User string `json:"user,omitempty"`
+ // Tools is the tools of the chat completion request.
+ Tools []tools.Tool `json:"tools,omitempty"`
+ // This can be either a string or an ToolChoice object.
+ ToolChoice any `json:"tool_choice,omitempty"`
+ // Options for streaming response. Only set this when you set stream: true.
+ StreamOptions *StreamOptions `json:"stream_options,omitempty"`
+ // Disable the default behavior of parallel tool calls by setting it: false.
+ ParallelToolCalls any `json:"parallel_tool_calls,omitempty"`
+ // RetryDelay is the delay between retries.
+ RetryDelay time.Duration `json:"-"`
}
// LogProbs is the top-level structure containing the log probability information.
LogProbs struct {
+ // Content is a list of message content tokens with log
+ // probability information.
Content []struct {
- Token string `json:"token"` // Token is the token of the log prob.
- LogProb float64 `json:"logprob"` // LogProb is the log prob of the log prob.
- Bytes []byte `json:"bytes,omitempty"` // Omitting the field if it is null
- TopLogProbs []TopLogProbs `json:"top_logprobs"` // TopLogProbs is a list of the most likely tokens and their log probability, at this token position. In rare cases, there may be fewer than the number of requested top_logprobs returned.
- } `json:"content"` // Content is a list of message content tokens with log probability information.
+ // Token is the token of the log prob.
+ Token string `json:"token"`
+ // LogProb is the log prob of the log prob.
+ LogProb float64 `json:"logprob"`
+ // Omitting the field if it is null
+ Bytes []byte `json:"bytes,omitempty"`
+ // TopLogProbs is a list of the most likely tokens and
+ // their log probability, at this token position. In
+ // rare cases, there may be fewer than the number of
+ // requested top_logprobs returned.
+ TopLogProbs []TopLogProbs `json:"top_logprobs"`
+ } `json:"content"`
}
// TopLogProbs represents the top log probs.
TopLogProbs struct {
- Token string `json:"token"` // Token is the token of the top log probs.
- LogProb float64 `json:"logprob"` // LogProb is the log prob of the top log probs.
- Bytes []byte `json:"bytes,omitempty"` // Bytes is the bytes of the top log probs.
+ // Token is the token of the top log probs.
+ Token string `json:"token"`
+ // LogProb is the log prob of the top log probs.
+ LogProb float64 `json:"logprob"`
+ // Bytes is the bytes of the top log probs.
+ Bytes []byte `json:"bytes,omitempty"`
}
- // FinishReason is the finish reason.
- // string
- FinishReason string
// ChatCompletionChoice represents the chat completion choice.
ChatCompletionChoice struct {
Index int `json:"index"` // Index is the index of the choice.
// Message is the chat completion message of the choice.
- Message ChatCompletionMessage `json:"message"` // Message is the chat completion message of the choice.
+ Message ChatCompletionMessage `json:"message"`
// FinishReason is the finish reason of the choice.
- //
- // stop: API returned complete message,
- // or a message terminated by one of the stop sequences provided via the stop parameter
- // length: Incomplete model output due to max_tokens parameter or token limit
- // function_call: The model decided to call a function
- // content_filter: Omitted content due to a flag from our content filters
- // null: API response still in progress or incomplete
- FinishReason FinishReason `json:"finish_reason"` // FinishReason is the finish reason of the choice.
+ FinishReason FinishReason `json:"finish_reason"`
// LogProbs is the log probs of the choice.
//
- // This is basically the probability of the model choosing the token.
- LogProbs *LogProbs `json:"logprobs,omitempty"` // LogProbs is the log probs of the choice.
+ // This is basically the probability of the model choosing the
+ // token.
+ LogProbs *LogProbs `json:"logprobs,omitempty"`
}
- // ChatCompletionResponse represents a response structure for chat completion API.
+ // ChatCompletionResponse represents a response structure for chat
+ // completion API.
ChatCompletionResponse struct {
- ID string `json:"id"` // ID is the id of the response.
- Object string `json:"object"` // Object is the object of the response.
- Created int64 `json:"created"` // Created is the created time of the response.
- Model ChatModel `json:"model"` // Model is the model of the response.
- Choices []ChatCompletionChoice `json:"choices"` // Choices is the choices of the response.
- Usage Usage `json:"usage"` // Usage is the usage of the response.
- SystemFingerprint string `json:"system_fingerprint"` // SystemFingerprint is the system fingerprint of the response.
- http.Header // Header is the header of the response.
- }
- // ChatCompletionStreamChoiceDelta represents a response structure for chat completion API.
+ // ID is the id of the response.
+ ID string `json:"id"`
+ // Object is the object of the response.
+ Object string `json:"object"`
+ // Created is the created time of the response.
+ Created int64 `json:"created"`
+ // Model is the model of the response.
+ Model models.ChatModel `json:"model"`
+ // Choices is the choices of the response.
+ Choices []ChatCompletionChoice `json:"choices"`
+ // Usage is the usage of the response.
+ Usage Usage `json:"usage"`
+ // SystemFingerprint is the system fingerprint of the response.
+ SystemFingerprint string `json:"system_fingerprint"`
+ // Header is the header of the response.
+ http.Header
+ }
+ // ChatCompletionStreamChoiceDelta represents a response structure for
+ // chat completion API.
ChatCompletionStreamChoiceDelta struct {
- Content string `json:"content,omitempty"`
- Role string `json:"role,omitempty"`
+ // Content is the content of the response.
+ Content string `json:"content,omitempty"`
+ // Role is the role of the creator of the completion.
+ Role string `json:"role,omitempty"`
+ // FunctionCall is the function call of the response.
FunctionCall *tools.FunctionCall `json:"function_call,omitempty"`
- ToolCalls []tools.ToolCall `json:"tool_calls,omitempty"`
+ // ToolCalls are the tool calls of the response.
+ ToolCalls []tools.ToolCall `json:"tool_calls,omitempty"`
}
- // ChatCompletionStreamChoice represents a response structure for chat completion API.
+ // ChatCompletionStreamChoice represents a response structure for chat
+ // completion API.
ChatCompletionStreamChoice struct {
- Index int `json:"index"`
- Delta ChatCompletionStreamChoiceDelta `json:"delta"`
- FinishReason FinishReason `json:"finish_reason"`
- }
- streamer interface {
- ChatCompletionStreamResponse
+ // Index is the index of the choice.
+ Index int `json:"index"`
+ // Delta is the delta of the choice.
+ Delta ChatCompletionStreamChoiceDelta `json:"delta"`
+ // FinishReason is the finish reason of the choice.
+ FinishReason FinishReason `json:"finish_reason"`
}
// StreamOptions represents the stream options.
StreamOptions struct {
- // If set, an additional chunk will be streamed before the data: [DONE] message.
- // The usage field on this chunk shows the token usage statistics for the entire request,
- // and the choices field will always be an empty array.
- // All other chunks will also include a usage field, but with a null value.
+ // IncludeUsage is the include usage option of the stream
+ // options.
+ //
+ // If set, an additional chunk will be streamed before the data:
+ // [DONE] message.
+ // The usage field on this chunk shows the token usage
+ // statistics for the entire request, and the choices field will
+ // always be an empty array.
+ //
+ // All other chunks will also include a usage field, but with a
+ // null value.
IncludeUsage bool `json:"include_usage,omitempty"`
}
- // ChatCompletionStreamResponse represents a response structure for chat completion API.
+ // ChatCompletionStreamResponse represents a response structure for chat
+ // completion API.
ChatCompletionStreamResponse struct {
- ID string `json:"id"` // ID is the identifier for the chat completion stream response.
- Object string `json:"object"` // Object is the object type of the chat completion stream response.
- Created int64 `json:"created"` // Created is the creation time of the chat completion stream response.
- Model ChatModel `json:"model"` // Model is the model used for the chat completion stream response.
- Choices []ChatCompletionStreamChoice `json:"choices"` // Choices is the choices for the chat completion stream response.
- SystemFingerprint string `json:"system_fingerprint"` // SystemFingerprint is the system fingerprint for the chat completion stream response.
- PromptAnnotations []PromptAnnotation `json:"prompt_annotations,omitempty"` // PromptAnnotations is the prompt annotations for the chat completion stream response.
+ // ID is the identifier for the chat completion stream response.
+ ID string `json:"id"`
+ // Object is the object type of the chat completion stream
+ // response.
+ Object string `json:"object"`
+ // Created is the creation time of the chat completion stream
+ // response.
+ Created int64 `json:"created"`
+ // Model is the model used for the chat completion stream
+ // response.
+ Model models.ChatModel `json:"model"`
+ // Choices is the choices for the chat completion stream
+ // response.
+ Choices []ChatCompletionStreamChoice `json:"choices"`
+ // SystemFingerprint is the system fingerprint for the chat
+ // completion stream response.
+ SystemFingerprint string `json:"system_fingerprint"`
+ // PromptAnnotations is the prompt annotations for the chat
+ // completion stream response.
+ PromptAnnotations []PromptAnnotation `json:"prompt_annotations,omitempty"`
+ // PromptFilterResults is the prompt filter results for the chat
+ // completion stream response.
PromptFilterResults []struct {
Index int `json:"index"`
- } `json:"prompt_filter_results,omitempty"` // PromptFilterResults is the prompt filter results for the chat completion stream response.
- // Usage is an optional field that will only be present when you set stream_options: {"include_usage": true} in your request.
+ } `json:"prompt_filter_results,omitempty"`
+ // Usage is an optional field that will only be present when you
+ // set stream_options: {"include_usage": true} in your request.
//
- // When present, it contains a null value except for the last chunk which contains the token usage statistics
- // for the entire request.
+ // When present, it contains a null value except for the last
+ // chunk which contains the token usage statistics for the
+ // entire request.
Usage *Usage `json:"usage,omitempty"`
}
// ChatCompletionStream is a stream of ChatCompletionStreamResponse.
- //
- // Note: Perhaps it is more elegant to abstract Stream using generics.
ChatCompletionStream struct {
- *streamReader[ChatCompletionStreamResponse]
+ *streams.StreamReader[*ChatCompletionStreamResponse]
}
)
// MarshalJSON method implements the json.Marshaler interface.
func (m ChatCompletionMessage) MarshalJSON() ([]byte, error) {
if m.Content != "" && m.MultiContent != nil {
- return nil, &ErrContentFieldsMisused{field: "Content"}
+ return nil, &groqerr.ErrContentFieldsMisused{}
}
if len(m.MultiContent) > 0 {
msg := struct {
@@ -294,7 +414,7 @@ func (m *ChatCompletionMessage) UnmarshalJSON(bs []byte) (err error) {
// MarshalJSON implements the json.Marshaler interface.
func (r FinishReason) MarshalJSON() ([]byte, error) {
- if r == FinishReasonNull || r == "" {
+ if r == ReasonNull || r == "" {
return []byte("null"), nil
}
return []byte(
@@ -303,23 +423,7 @@ func (r FinishReason) MarshalJSON() ([]byte, error) {
}
// SetHeader sets the header of the response.
-func (r *ChatCompletionResponse) SetHeader(h http.Header) {
- r.Header = h
-}
-
-// MustCreateChatCompletion method is an API call to create a chat completion.
-//
-// It panics if an error occurs.
-func (c *Client) MustCreateChatCompletion(
- ctx context.Context,
- request ChatCompletionRequest,
-) (response ChatCompletionResponse) {
- response, err := c.CreateChatCompletion(ctx, request)
- if err != nil {
- panic(err)
- }
- return response
-}
+func (r *ChatCompletionResponse) SetHeader(h http.Header) { r.Header = h }
// CreateChatCompletion method is an API call to create a chat completion.
func (c *Client) CreateChatCompletion(
@@ -331,22 +435,23 @@ func (c *Client) CreateChatCompletion(
ctx,
c.header,
http.MethodPost,
- c.fullURL(chatCompletionsSuffix, withModel(model(request.Model))),
+ c.fullURL(chatCompletionsSuffix, withModel(request.Model)),
builders.WithBody(request))
if err != nil {
return
}
err = c.sendRequest(req, &response)
- reqErr, ok := err.(*APIError)
- if ok && (reqErr.HTTPStatusCode == http.StatusServiceUnavailable || reqErr.HTTPStatusCode == http.StatusInternalServerError) {
+ reqErr, ok := err.(*groqerr.APIError)
+ if ok && (reqErr.HTTPStatusCode == http.StatusServiceUnavailable ||
+ reqErr.HTTPStatusCode == http.StatusInternalServerError) {
time.Sleep(request.RetryDelay)
return c.CreateChatCompletion(ctx, request)
}
return
}
-// CreateChatCompletionStream method is an API call to create a chat completion w/ streaming
-// support.
+// CreateChatCompletionStream method is an API call to create a chat completion
+// w/ streaming support.
//
// If set, tokens will be sent as data-only server-sent events as they become
// available, with the stream terminated by a data: [DONE] message.
@@ -359,23 +464,25 @@ func (c *Client) CreateChatCompletionStream(
ctx,
c.header,
http.MethodPost,
- c.fullURL(chatCompletionsSuffix, withModel(model(request.Model))),
+ c.fullURL(
+ chatCompletionsSuffix,
+ withModel(request.Model)),
builders.WithBody(request),
)
if err != nil {
return nil, err
}
- resp, err := sendRequestStream[ChatCompletionStreamResponse](c, req)
+ resp, err := sendRequestStream(c, req)
if err != nil {
return
}
- stream = &ChatCompletionStream{
- streamReader: resp,
- }
- return
+ return &ChatCompletionStream{
+ StreamReader: resp,
+ }, nil
}
-// CreateChatCompletionJSON method is an API call to create a chat completion w/ object output.
+// CreateChatCompletionJSON method is an API call to create a chat completion
+// w/ object output.
func (c *Client) CreateChatCompletionJSON(
ctx context.Context,
request ChatCompletionRequest,
@@ -392,20 +499,9 @@ func (c *Client) CreateChatCompletionJSON(
Schema: *schema,
Strict: true,
}
- req, err := builders.NewRequest(
- ctx,
- c.header,
- http.MethodPost,
- c.fullURL(chatCompletionsSuffix, withModel(model(request.Model))),
- builders.WithBody(request),
- )
- if err != nil {
- return
- }
- var response ChatCompletionResponse
- err = c.sendRequest(req, &response)
+ response, err := c.CreateChatCompletion(ctx, request)
if err != nil {
- reqErr, ok := err.(*APIError)
+ reqErr, ok := err.(*groqerr.APIError)
if ok && (reqErr.HTTPStatusCode == http.StatusServiceUnavailable ||
reqErr.HTTPStatusCode == http.StatusInternalServerError) {
time.Sleep(request.RetryDelay)
@@ -427,85 +523,3 @@ func (c *Client) CreateChatCompletionJSON(
}
return
}
-
-type streamReader[T streamer] struct {
- emptyMessagesLimit uint
- isFinished bool
- reader *bufio.Reader
- response *http.Response
- errAccumulator errorAccumulator
- Header http.Header // Header is the header of the response.
-}
-
-// Recv receives a response from the stream.
-func (stream *streamReader[T]) Recv() (response T, err error) {
- if stream.isFinished {
- err = io.EOF
- return response, err
- }
- return stream.processLines()
-}
-
-// processLines processes the lines of the current response in the stream.
-func (stream *streamReader[T]) processLines() (T, error) {
- var (
- headerData = []byte("data: ")
- errorPrefix = []byte(`data: {"error":`)
- emptyMessagesCount uint
- hasErrorPrefix bool
- )
- for {
- rawLine, err := stream.reader.ReadBytes('\n')
- if err != nil || hasErrorPrefix {
- respErr := stream.unmarshalError()
- if respErr != nil {
- return *new(T),
- fmt.Errorf("error, %w", respErr.Error)
- }
- return *new(T), err
- }
- noSpaceLine := bytes.TrimSpace(rawLine)
- if bytes.HasPrefix(noSpaceLine, errorPrefix) {
- hasErrorPrefix = true
- }
- if !bytes.HasPrefix(noSpaceLine, headerData) || hasErrorPrefix {
- if hasErrorPrefix {
- noSpaceLine = bytes.TrimPrefix(noSpaceLine, headerData)
- }
- err := stream.errAccumulator.Write(noSpaceLine)
- if err != nil {
- return *new(T), err
- }
- emptyMessagesCount++
- if emptyMessagesCount > stream.emptyMessagesLimit {
- return *new(T), ErrTooManyEmptyStreamMessages{}
- }
- continue
- }
- noPrefixLine := bytes.TrimPrefix(noSpaceLine, headerData)
- if string(noPrefixLine) == "[DONE]" {
- stream.isFinished = true
- return *new(T), io.EOF
- }
- var response T
- unmarshalErr := json.Unmarshal(noPrefixLine, &response)
- if unmarshalErr != nil {
- return *new(T), unmarshalErr
- }
- return response, nil
- }
-}
-func (stream *streamReader[T]) unmarshalError() (errResp *errorResponse) {
- errBytes := stream.errAccumulator.Bytes()
- if len(errBytes) == 0 {
- return
- }
- err := json.Unmarshal(errBytes, &errResp)
- if err != nil {
- errResp = nil
- }
- return
-}
-func (stream *streamReader[T]) Close() error {
- return stream.response.Body.Close()
-}
diff --git a/chat_test.go b/chat_test.go
index 1d51d1c..2e1c596 100644
--- a/chat_test.go
+++ b/chat_test.go
@@ -1,105 +1,65 @@
-package groq
+package groq_test
import (
- "bufio"
- "bytes"
- "io"
+ "context"
+ "encoding/json"
"net/http"
"testing"
+ "github.com/conneroisu/groq-go"
+ "github.com/conneroisu/groq-go/pkg/models"
"github.com/conneroisu/groq-go/pkg/test"
+ "github.com/conneroisu/groq-go/pkg/tools"
"github.com/stretchr/testify/assert"
)
-// TestStreamReaderReturnsUnmarshalerErrors tests the stream reader returns an unmarshaler error.
-func TestStreamReaderReturnsUnmarshalerErrors(t *testing.T) {
- stream := &streamReader[ChatCompletionStreamResponse]{
- errAccumulator: newErrorAccumulator(),
- }
-
- respErr := stream.unmarshalError()
- if respErr != nil {
- t.Fatalf("Did not return nil with empty buffer: %v", respErr)
- }
-
- err := stream.errAccumulator.Write([]byte("{"))
- if err != nil {
- t.Fatalf("%+v", err)
- }
-
- respErr = stream.unmarshalError()
- if respErr != nil {
- t.Fatalf("Did not return nil when unmarshaler failed: %v", respErr)
- }
-}
-
-// TestStreamReaderReturnsErrTooManyEmptyStreamMessages tests the stream reader returns an error when the stream has too many empty messages.
-func TestStreamReaderReturnsErrTooManyEmptyStreamMessages(t *testing.T) {
- a := assert.New(t)
- stream := &streamReader[ChatCompletionStreamResponse]{
- emptyMessagesLimit: 3,
- reader: bufio.NewReader(
- bytes.NewReader([]byte("\n\n\n\n")),
- ),
- errAccumulator: newErrorAccumulator(),
- }
- _, err := stream.Recv()
- a.ErrorIs(
- err,
- ErrTooManyEmptyStreamMessages{},
- "Did not return error when recv failed",
- err.Error(),
- )
-}
-
-// TestStreamReaderReturnsErrTestErrorAccumulatorWriteFailed tests the stream reader returns an error when the error accumulator fails to write.
-func TestStreamReaderReturnsErrTestErrorAccumulatorWriteFailed(t *testing.T) {
+func TestChat(t *testing.T) {
+ ctx := context.Background()
a := assert.New(t)
- stream := &streamReader[ChatCompletionStreamResponse]{
- reader: bufio.NewReader(bytes.NewReader([]byte("\n"))),
- errAccumulator: &DefaultErrorAccumulator{
- Buffer: &test.FailingErrorBuffer{},
+ ts := test.NewTestServer()
+ returnObj := groq.ChatCompletionResponse{
+ ID: "chatcmpl-123",
+ Object: "chat.completion.chunk",
+ Created: 1693721698,
+ Model: "llama3-groq-70b-8192-tool-use-preview",
+ Choices: []groq.ChatCompletionChoice{
+ {
+ Index: 0,
+ Message: groq.ChatCompletionMessage{
+ Role: groq.ChatMessageRoleAssistant,
+ Content: "Hello!",
+ },
+ },
},
}
- _, err := stream.Recv()
- a.ErrorIs(
- err,
- test.ErrTestErrorAccumulatorWriteFailed{},
- "Did not return error when write failed",
- err.Error(),
+ ts.RegisterHandler("/v1/chat/completions", func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(http.StatusOK)
+ jsval, err := json.Marshal(returnObj)
+ a.NoError(err)
+ _, err = w.Write(jsval)
+ if err != nil {
+ t.Fatal(err)
+ }
+ })
+ testS := ts.GroqTestServer()
+ testS.Start()
+ client, err := groq.NewClient(
+ test.GetTestToken(),
+ groq.WithBaseURL(testS.URL+"/v1"),
)
-}
-
-// Helper function to create a new `streamReader` for testing
-func newStreamReader[T streamer](data string) *streamReader[T] {
- resp := &http.Response{
- Body: io.NopCloser(bytes.NewBufferString(data)),
- }
- reader := bufio.NewReader(resp.Body)
-
- return &streamReader[T]{
- emptyMessagesLimit: 5,
- isFinished: false,
- reader: reader,
- response: resp,
- errAccumulator: newErrorAccumulator(),
- Header: resp.Header,
- }
-}
-
-// Test the `Recv` method with multiple empty messages triggering an error
-func TestStreamReader_TooManyEmptyMessages(t *testing.T) {
- data := "\n\n\n\n\n\n"
- stream := newStreamReader[ChatCompletionStreamResponse](data)
-
- _, err := stream.Recv()
- assert.ErrorIs(t, err, ErrTooManyEmptyStreamMessages{})
-}
-
-// Test the `Close` method
-func TestStreamReader_Close(t *testing.T) {
- stream := newStreamReader[ChatCompletionStreamResponse]("")
-
- err := stream.Close()
- assert.NoError(t, err)
+ a.NoError(err)
+ resp, err := client.CreateChatCompletion(ctx, groq.ChatCompletionRequest{
+ Model: models.ModelLlama3Groq70B8192ToolUsePreview,
+ Messages: []groq.ChatCompletionMessage{
+ {
+ Role: groq.ChatMessageRoleUser,
+ Content: "Hello!",
+ },
+ },
+ MaxTokens: 2000,
+ Tools: []tools.Tool{},
+ })
+ a.NoError(err)
+ a.NotEmpty(resp.Choices[0].Message.Content)
}
diff --git a/client.go b/client.go
index 2db9617..d8c3c9c 100644
--- a/client.go
+++ b/client.go
@@ -1,7 +1,6 @@
package groq
import (
- "bufio"
"encoding/json"
"fmt"
"io"
@@ -11,55 +10,91 @@ import (
"time"
"github.com/conneroisu/groq-go/pkg/builders"
+ "github.com/conneroisu/groq-go/pkg/groqerr"
+ "github.com/conneroisu/groq-go/pkg/models"
+ "github.com/conneroisu/groq-go/pkg/streams"
)
//go:generate go run ./scripts/generate-models/
-//go:generate gomarkdoc -o README.md -e .
+//go:generate go run github.com/princjef/gomarkdoc/cmd/gomarkdoc@v1.1.0 -o README.md -e .
type (
- // Format is the format of a response.
- // string
- Format string
// Client is a Groq api client.
Client struct {
- groqAPIKey string // Groq API key
- orgID string // OrgID is the organization ID for the client.
- baseURL string // Base URL for the client.
- emptyMessagesLimit uint // EmptyMessagesLimit is the limit for the empty messages.
+ // Groq API key
+ groqAPIKey string
+ // OrgID is the organization ID for the client.
+ orgID string
+ // Base URL for the client.
+ baseURL string
+ // EmptyMessagesLimit is the limit for the empty messages.
+ emptyMessagesLimit uint
header builders.Header
requestFormBuilder builders.FormBuilder
- createFormBuilder func(body io.Writer) builders.FormBuilder
- client *http.Client // Client is the HTTP client to use
- logger *slog.Logger // Logger is the logger for the client.
+ // Client is the HTTP client to use
+ client *http.Client
+ // Logger is the logger for the client.
+ logger *slog.Logger
}
+ // Opts is a function that sets options for a Groq client.
+ Opts func(*Client)
+)
+
+// WithClient sets the client for the Groq client.
+func WithClient(client *http.Client) Opts {
+ return func(c *Client) { c.client = client }
+}
+
+// WithBaseURL sets the base URL for the Groq client.
+func WithBaseURL(baseURL string) Opts {
+ return func(c *Client) { c.baseURL = baseURL }
+}
+
+// WithLogger sets the logger for the Groq client.
+func WithLogger(logger *slog.Logger) Opts {
+ return func(c *Client) { c.logger = logger }
+}
+
+type (
+ // Format is the format of a response.
+ // string
+ Format string
// RateLimitHeaders struct represents Groq rate limits headers.
RateLimitHeaders struct {
- LimitRequests int `json:"x-ratelimit-limit-requests"` // LimitRequests is the limit requests of the rate limit headers.
- LimitTokens int `json:"x-ratelimit-limit-tokens"` // LimitTokens is the limit tokens of the rate limit headers.
- RemainingRequests int `json:"x-ratelimit-remaining-requests"` // RemainingRequests is the remaining requests of the rate limit headers.
- RemainingTokens int `json:"x-ratelimit-remaining-tokens"` // RemainingTokens is the remaining tokens of the rate limit headers.
- ResetRequests ResetTime `json:"x-ratelimit-reset-requests"` // ResetRequests is the reset requests of the rate limit headers.
- ResetTokens ResetTime `json:"x-ratelimit-reset-tokens"` // ResetTokens is the reset tokens of the rate limit headers.
+ // LimitRequests is the limit requests of the rate limit
+ // headers.
+ LimitRequests int `json:"x-ratelimit-limit-requests"`
+ // LimitTokens is the limit tokens of the rate limit headers.
+ LimitTokens int `json:"x-ratelimit-limit-tokens"`
+ // RemainingRequests is the remaining requests of the rate
+ // limit headers.
+ RemainingRequests int `json:"x-ratelimit-remaining-requests"`
+ // RemainingTokens is the remaining tokens of the rate limit
+ // headers.
+ RemainingTokens int `json:"x-ratelimit-remaining-tokens"`
+ // ResetRequests is the reset requests of the rate limit
+ // headers.
+ ResetRequests ResetTime `json:"x-ratelimit-reset-requests"`
+ // ResetTokens is the reset tokens of the rate limit headers.
+ ResetTokens ResetTime `json:"x-ratelimit-reset-tokens"`
}
// ResetTime is a time.Time wrapper for the rate limit reset time.
// string
ResetTime string
-
- // Opts is a function that sets options for a Groq client.
- Opts func(*Client)
// Usage Represents the total token usage per request to Groq.
Usage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
}
+ // Endpoint is an endpoint for the groq api.
+ Endpoint string
- fullURLOptions struct {
- model string
- }
- fullURLOption func(*fullURLOptions)
+ fullURLOptions struct{ model string }
+ fullURLOption func(*fullURLOptions)
+ response interface{ SetHeader(http.Header) }
)
const (
@@ -69,9 +104,31 @@ const (
// FormatJSON is the JSON format. There is no support for streaming with
// JSON format selected.
FormatJSON Format = "json"
+ // FormatSRT is the SRT format. This is a text format that is only
+ // supported for the transcription API.
+ // SRT format selected.
+ FormatSRT Format = "srt"
+ // FormatVTT is the VTT format. This is a text format that is only
+ // supported for the transcription API.
+ FormatVTT Format = "vtt"
+ // FormatVerboseJSON is the verbose JSON format. This is a JSON format
+ // that is only supported for the transcription API.
+ FormatVerboseJSON Format = "verbose_json"
+ // FormatJSONObject is the json object chat
+ // completion response format type.
+ FormatJSONObject Format = "json_object"
+ // FormatJSONSchema is the json schema chat
+ // completion response format type.
+ FormatJSONSchema Format = "json_schema"
// groqAPIURLv1 is the base URL for the Groq API.
groqAPIURLv1 = "https://api.groq.com/openai/v1"
+
+ chatCompletionsSuffix Endpoint = "/chat/completions"
+ transcriptionsSuffix Endpoint = "/audio/transcriptions"
+ translationsSuffix Endpoint = "/audio/translations"
+ embeddingsSuffix Endpoint = "/embeddings"
+ moderationsSuffix Endpoint = "/moderations"
)
// NewClient creates a new Groq client.
@@ -85,9 +142,6 @@ func NewClient(groqAPIKey string, opts ...Opts) (*Client, error) {
logger: slog.Default(),
baseURL: groqAPIURLv1,
emptyMessagesLimit: 10,
- createFormBuilder: func(body io.Writer) builders.FormBuilder {
- return builders.NewFormBuilder(body)
- },
}
for _, opt := range opts {
opt(c)
@@ -111,32 +165,6 @@ func (c *Client) fullURL(suffix Endpoint, setters ...fullURLOption) string {
return fmt.Sprintf("%s%s", baseURL, suffix)
}
-// WithClient sets the client for the Groq client.
-func WithClient(client *http.Client) Opts {
- return func(c *Client) {
- c.client = client
- }
-}
-
-// WithBaseURL sets the base URL for the Groq client.
-func WithBaseURL(baseURL string) Opts {
- return func(c *Client) {
- c.baseURL = baseURL
- }
-}
-
-// WithLogger sets the logger for the Groq client.
-func WithLogger(logger *slog.Logger) Opts {
- return func(c *Client) {
- c.logger = logger
- }
-}
-
-// response is an interface for a response.
-type response interface {
- SetHeader(http.Header)
-}
-
func (c *Client) sendRequest(req *http.Request, v response) error {
req.Header.Set("Accept", "application/json")
@@ -165,10 +193,10 @@ func (c *Client) sendRequest(req *http.Request, v response) error {
return decodeResponse(res.Body, v)
}
-func sendRequestStream[T streamer](
+func sendRequestStream[T streams.Streamer[ChatCompletionStreamResponse]](
client *Client,
req *http.Request,
-) (*streamReader[T], error) {
+) (*streams.StreamReader[*ChatCompletionStreamResponse], error) {
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "text/event-stream")
req.Header.Set("Cache-Control", "no-cache")
@@ -178,18 +206,16 @@ func sendRequestStream[T streamer](
req,
) //nolint:bodyclose // body is closed in stream.Close()
if err != nil {
- return new(streamReader[T]), err
+ return new(streams.StreamReader[*ChatCompletionStreamResponse]), err
}
if isFailureStatusCode(resp) {
- return new(streamReader[T]), client.handleErrorResp(resp)
+ return new(streams.StreamReader[*ChatCompletionStreamResponse]), client.handleErrorResp(resp)
}
- return &streamReader[T]{
- emptyMessagesLimit: client.emptyMessagesLimit,
- reader: bufio.NewReader(resp.Body),
- response: resp,
- errAccumulator: newErrorAccumulator(),
- Header: resp.Header,
- }, nil
+ return streams.NewStreamReader[ChatCompletionStreamResponse](
+ resp.Body,
+ resp.Header,
+ client.emptyMessagesLimit,
+ ), nil
}
func isFailureStatusCode(resp *http.Response) bool {
@@ -221,17 +247,19 @@ func decodeString(body io.Reader, output *string) error {
return nil
}
-func withModel(model model) fullURLOption {
+func withModel[
+ T models.ChatModel | models.AudioModel | models.ModerationModel,
+](model T) fullURLOption {
return func(args *fullURLOptions) {
args.model = string(model)
}
}
func (c *Client) handleErrorResp(resp *http.Response) error {
- var errRes errorResponse
+ var errRes groqerr.ErrorResponse
err := json.NewDecoder(resp.Body).Decode(&errRes)
if err != nil || errRes.Error == nil {
- reqErr := &requestError{
+ reqErr := &groqerr.ErrRequest{
HTTPStatusCode: resp.StatusCode,
Err: err,
}
diff --git a/client_test.go b/client_test.go
index 7784de2..1a32116 100644
--- a/client_test.go
+++ b/client_test.go
@@ -1,21 +1,22 @@
-package groq
+package groq_test
import (
"log/slog"
"net/http"
"testing"
+ groq "github.com/conneroisu/groq-go"
"github.com/stretchr/testify/assert"
)
// TestClient tests the creation of a new client.
func TestClient(t *testing.T) {
a := assert.New(t)
- client, err := NewClient(
+ client, err := groq.NewClient(
"test",
- WithBaseURL("http://localhost/v1"),
- WithClient(http.DefaultClient),
- WithLogger(slog.Default()),
+ groq.WithBaseURL("http://localhost/v1"),
+ groq.WithClient(http.DefaultClient),
+ groq.WithLogger(slog.Default()),
)
a.NoError(err)
a.NotNil(client)
diff --git a/errors.go b/errors.go
deleted file mode 100644
index 93b0fe0..0000000
--- a/errors.go
+++ /dev/null
@@ -1,151 +0,0 @@
-package groq
-
-import (
- "bytes"
- "encoding/json"
- "fmt"
- "io"
- "strings"
-)
-
-type (
- // DefaultErrorAccumulator is a default implementation of ErrorAccumulator
- DefaultErrorAccumulator struct {
- Buffer errorBuffer
- }
- // APIError provides error information returned by the Groq API.
- APIError struct {
- Code any `json:"code,omitempty"` // Code is the code of the error.
- Message string `json:"message"` // Message is the message of the error.
- Param *string `json:"param,omitempty"` // Param is the param of the error.
- Type string `json:"type"` // Type is the type of the error.
- HTTPStatusCode int `json:"-"` // HTTPStatusCode is the status code of the error.
- }
- // ErrContentFieldsMisused is an error that occurs when both Content and
- // MultiContent properties are set.
- ErrContentFieldsMisused struct {
- field string
- }
- // ErrTooManyEmptyStreamMessages is returned when the stream has sent too many
- // empty messages.
- ErrTooManyEmptyStreamMessages struct{}
- errorAccumulator interface {
- // Write method writes bytes to the error accumulator
- //
- // It implements the io.Writer interface.
- Write(p []byte) error
- // Bytes method returns the bytes of the error accumulator.
- Bytes() []byte
- }
- errorBuffer interface {
- io.Writer
- Len() int
- Bytes() []byte
- }
- requestError struct {
- HTTPStatusCode int
- Err error
- }
- errorResponse struct {
- Error *APIError `json:"error,omitempty"`
- }
-)
-
-// Error implements the error interface.
-func (e ErrContentFieldsMisused) Error() string {
- return fmt.Errorf("can't use both Content and MultiContent properties simultaneously").
- Error()
-}
-
-// Error returns the error message.
-func (e ErrTooManyEmptyStreamMessages) Error() string {
- return "stream has sent too many empty messages"
-}
-
-// newErrorAccumulator creates a new error accumulator
-func newErrorAccumulator() errorAccumulator {
- return &DefaultErrorAccumulator{
- Buffer: &bytes.Buffer{},
- }
-}
-
-// Write method writes bytes to the error accumulator.
-func (e *DefaultErrorAccumulator) Write(p []byte) error {
- _, err := e.Buffer.Write(p)
- if err != nil {
- return fmt.Errorf("error accumulator write error, %w", err)
- }
- return nil
-}
-
-// Bytes method returns the bytes of the error accumulator.
-func (e *DefaultErrorAccumulator) Bytes() (errBytes []byte) {
- if e.Buffer.Len() == 0 {
- return
- }
- errBytes = e.Buffer.Bytes()
- return
-}
-
-// Error method implements the error interface on APIError.
-func (e *APIError) Error() string {
- if e.HTTPStatusCode > 0 {
- return fmt.Sprintf(
- "error, status code: %d, message: %s",
- e.HTTPStatusCode,
- e.Message,
- )
- }
- return e.Message
-}
-
-// UnmarshalJSON implements the json.Unmarshaler interface.
-func (e *APIError) UnmarshalJSON(data []byte) (err error) {
- var rawMap map[string]json.RawMessage
- err = json.Unmarshal(data, &rawMap)
- if err != nil {
- return
- }
- err = json.Unmarshal(rawMap["message"], &e.Message)
- if err != nil {
- var messages []string
- err = json.Unmarshal(rawMap["message"], &messages)
- if err != nil {
- return
- }
- e.Message = strings.Join(messages, ", ")
- }
- // optional fields
- if _, ok := rawMap["param"]; ok {
- err = json.Unmarshal(rawMap["param"], &e.Param)
- if err != nil {
- return
- }
- }
- if _, ok := rawMap["code"]; !ok {
- return nil
- }
- // if the api returned a number, we need to force an integer
- // since the json package defaults to float64
- var intCode int
- err = json.Unmarshal(rawMap["code"], &intCode)
- if err == nil {
- e.Code = intCode
- return nil
- }
- return json.Unmarshal(rawMap["code"], &e.Code)
-}
-
-// Error implements the error interface.
-func (e *requestError) Error() string {
- return fmt.Sprintf(
- "error, status code: %d, message: %s",
- e.HTTPStatusCode,
- e.Err,
- )
-}
-
-// Unwrap unwraps the error.
-func (e *requestError) Unwrap() error {
- return e.Err
-}
diff --git a/errors_test.go b/errors_test.go
deleted file mode 100644
index 94d6868..0000000
--- a/errors_test.go
+++ /dev/null
@@ -1,44 +0,0 @@
-package groq_test
-
-import (
- "bytes"
- "errors"
- "testing"
-
- groq "github.com/conneroisu/groq-go"
- "github.com/conneroisu/groq-go/pkg/test"
-)
-
-func TestErrorAccumulatorBytes(t *testing.T) {
- accumulator := &groq.DefaultErrorAccumulator{
- Buffer: &bytes.Buffer{},
- }
-
- errBytes := accumulator.Bytes()
- if len(errBytes) != 0 {
- t.Fatalf("Did not return nil with empty bytes: %s", string(errBytes))
- }
-
- err := accumulator.Write([]byte("{}"))
- if err != nil {
- t.Fatalf("%+v", err)
- }
-
- errBytes = accumulator.Bytes()
- if len(errBytes) == 0 {
- t.Fatalf(
- "Did not return error bytes when has error: %s",
- string(errBytes),
- )
- }
-}
-
-func TestErrorByteWriteErrors(t *testing.T) {
- accumulator := &groq.DefaultErrorAccumulator{
- Buffer: &test.FailingErrorBuffer{},
- }
- err := accumulator.Write([]byte("{"))
- if !errors.Is(err, test.ErrTestErrorAccumulatorWriteFailed{}) {
- t.Fatalf("Did not return error when write failed: %v", err)
- }
-}
diff --git a/examples/audio-house-translation/main.go b/examples/audio-house-translation/main.go
index b98347a..dc2c18e 100644
--- a/examples/audio-house-translation/main.go
+++ b/examples/audio-house-translation/main.go
@@ -1,3 +1,5 @@
+// Package main is an example of using the groq-go library to create a
+// transcription/translation using the whisper-large-v3 model.
package main
import (
@@ -6,6 +8,7 @@ import (
"os"
"github.com/conneroisu/groq-go"
+ "github.com/conneroisu/groq-go/pkg/models"
)
func main() {
@@ -23,7 +26,7 @@ func run(
return err
}
response, err := client.CreateTranslation(ctx, groq.AudioRequest{
- Model: groq.ModelWhisperLargeV3,
+ Model: models.ModelWhisperLargeV3,
FilePath: "./house-speaks-mandarin.mp3",
Prompt: "english and mandarin",
})
diff --git a/examples/audio-lex-fridman/main.go b/examples/audio-lex-fridman/main.go
index be662a1..41b025b 100644
--- a/examples/audio-lex-fridman/main.go
+++ b/examples/audio-lex-fridman/main.go
@@ -8,6 +8,7 @@ import (
"os"
"github.com/conneroisu/groq-go"
+ "github.com/conneroisu/groq-go/pkg/models"
)
func main() {
@@ -25,7 +26,7 @@ func run(
return err
}
response, err := client.CreateTranscription(ctx, groq.AudioRequest{
- Model: groq.ModelWhisperLargeV3,
+ Model: models.ModelWhisperLargeV3,
FilePath: "./The Roman Emperors who went insane Gregory Aldrete and Lex Fridman.mp3",
})
if err != nil {
diff --git a/examples/chat-terminal/main.go b/examples/chat-terminal/main.go
index 5f063ca..17d216c 100644
--- a/examples/chat-terminal/main.go
+++ b/examples/chat-terminal/main.go
@@ -11,6 +11,7 @@ import (
"strings"
"github.com/conneroisu/groq-go"
+ "github.com/conneroisu/groq-go/pkg/models"
)
var (
@@ -74,7 +75,7 @@ func input(ctx context.Context, r io.Reader, w io.Writer, client *groq.Client) e
output, err := client.CreateChatCompletionStream(
ctx,
groq.ChatCompletionRequest{
- Model: groq.ModelGemma29BIt,
+ Model: models.ModelGemma29BIt,
Messages: history,
MaxTokens: 2000,
},
@@ -89,7 +90,7 @@ func input(ctx context.Context, r io.Reader, w io.Writer, client *groq.Client) e
if err != nil {
return err
}
- if response.Choices[0].FinishReason == groq.FinishReasonStop {
+ if response.Choices[0].FinishReason == groq.ReasonStop {
break
}
fmt.Fprint(writer, response.Choices[0].Delta.Content)
diff --git a/examples/composio-github-star/main.go b/examples/composio-github-star/main.go
index 19a230a..92ff50f 100644
--- a/examples/composio-github-star/main.go
+++ b/examples/composio-github-star/main.go
@@ -1,3 +1,6 @@
+// Package main is an example of using the composio client.
+//
+// It shows how to use the composio client to star a github repository.
package main
import (
@@ -8,6 +11,7 @@ import (
"github.com/conneroisu/groq-go"
"github.com/conneroisu/groq-go/extensions/composio"
+ "github.com/conneroisu/groq-go/pkg/models"
"github.com/conneroisu/groq-go/pkg/test"
)
@@ -49,7 +53,7 @@ func run(
return err
}
chat, err := client.CreateChatCompletion(ctx, groq.ChatCompletionRequest{
- Model: groq.ModelLlama3Groq70B8192ToolUsePreview,
+ Model: models.ModelLlama3Groq70B8192ToolUsePreview,
Messages: []groq.ChatCompletionMessage{
{
Role: groq.ChatMessageRoleUser,
@@ -65,7 +69,11 @@ Star the repo conneroisu/groq-go on GitHub.
if err != nil {
return err
}
- resp, err := comp.Run(ctx, chat)
+ user, err := comp.GetConnectedAccounts(ctx)
+ if err != nil {
+ return err
+ }
+ resp, err := comp.Run(ctx, user[0], chat)
if err != nil {
return err
}
diff --git a/examples/e2b-go-project/main.go b/examples/e2b-go-project/main.go
index 9f31da2..8a2e51a 100644
--- a/examples/e2b-go-project/main.go
+++ b/examples/e2b-go-project/main.go
@@ -1,3 +1,4 @@
+// Package main shows an example of using the e2b extension.
package main
import (
@@ -7,6 +8,38 @@ import (
"github.com/conneroisu/groq-go"
"github.com/conneroisu/groq-go/extensions/e2b"
+ "github.com/conneroisu/groq-go/pkg/models"
+ "github.com/conneroisu/groq-go/pkg/tools"
+)
+
+var (
+ history = []groq.ChatCompletionMessage{
+ {
+ Role: groq.ChatMessageRoleUser,
+ Content: `
+Given the callable tools provided, create a python project with the following files:
+
+
+main.py
+utils.py
+
+
+The main function should call the "utils.run()" function.
+
+The project should, when run, print the following to stdout:
+
+