Documentation
¶
Index ¶
- Constants
- Variables
- func HandleAPIError(resp *http.Response) error
- func HandleNormalRequest(c Client, req *http.Request) (*http.Response, error)
- func HandleSendChatCompletionRequest(c Client, req *http.Request) (*http.Response, error)
- func HandleTimeout() (time.Duration, error)
- type APIError
- type APIModels
- type APIType
- type BalanceInfo
- type BalanceResponse
- type ChatCompletionMessage
- type ChatCompletionRequest
- type ChatCompletionResponse
- type ChatCompletionStream
- type Choice
- type Client
- type Function
- type HTTPDoer
- type JSONExtractor
- type LogProbs
- type Message
- type Model
- type Option
- type Parameters
- type ResponseFormat
- type StreamChatCompletionMessage
- type StreamChatCompletionRequest
- type StreamChatCompletionResponse
- type StreamChoices
- type StreamDelta
- type StreamOptions
- type StreamUsage
- type TokenEstimate
- type Tools
- type Usage
Constants ¶
const ( DeepSeekChat = "deepseek-chat" DeepSeekCoder = "deepseek-coder" // not sure if this exists anymore DeepSeekReasoner = "deepseek-reasoner" )
Official DeepSeek Models
const ( AzureDeepSeekR1 = "DeepSeek-R1" // Azure model for DeepSeek R1 OpenRouterDeepSeekR1 = "deepseek/deepseek-r1" // OpenRouter model for DeepSeek R1 OpenRouterDeepSeekR1DistillLlama70B = "deepseek/deepseek-r1-distill-llama-70b" // DeepSeek R1 Distill Llama 70B OpenRouterDeepSeekR1DistillLlama8B = "deepseek/deepseek-r1-distill-llama-8b" // DeepSeek R1 Distill Llama 8B OpenRouterDeepSeekR1DistillQwen14B = "deepseek/deepseek-r1-distill-qwen-14b" // DeepSeek R1 Distill Qwen 14B OpenRouterDeepSeekR1DistillQwen1_5B = "deepseek/deepseek-r1-distill-qwen-1.5b" // DeepSeek R1 Distill Qwen 1.5B OpenRouterDeepSeekR1DistillQwen32B = "deepseek/deepseek-r1-distill-qwen-32b" // DeepSeek R1 Distill Qwen 32B )
External Models that can be used with the API
const BaseURL string = "https://api.deepseek.com/v1"
Variables ¶
var ( ErrChatCompletionStreamNotSupported = errors.New("streaming is not supported with this method") ErrUnexpectedResponseFormat = errors.New("unexpected response format") )
Functions ¶
func HandleAPIError ¶
func HandleNormalRequest ¶ added in v1.1.1
func HandleSendChatCompletionRequest ¶ added in v1.1.1
func HandleTimeout ¶ added in v1.1.1
Types ¶
type APIError ¶
type APIModels ¶ added in v0.1.1
type BalanceInfo ¶
type BalanceInfo struct {
Currency string `json:"currency"` //The currency of the balance.
TotalBalance string `json:"total_balance"` //The total available balance, including the granted balance and the topped-up balance.
GrantedBalance string `json:"granted_balance"` //The total not expired granted balance.
ToppedUpBalance string `json:"topped_up_balance"` //The total topped-up balance.
}
type BalanceResponse ¶
type BalanceResponse struct {
IsAvailable bool `json:"is_available"` //Whether the user's balance is sufficient for API calls.
BalanceInfos []BalanceInfo `json:"balance_infos"` //List of Balance infos
}
func GetBalance ¶
func GetBalance(c *Client, ctx context.Context) (*BalanceResponse, error)
type ChatCompletionMessage ¶
func MapMessageToChatCompletionMessage ¶
func MapMessageToChatCompletionMessage(m Message) (ChatCompletionMessage, error)
type ChatCompletionRequest ¶
type ChatCompletionRequest struct {
Model string `json:"model"` // Required: Model ID, e.g., "deepseek-chat"
Messages []ChatCompletionMessage `json:"messages"` // Required: List of messages
FrequencyPenalty float32 `json:"frequency_penalty,omitempty"` // Optional: Frequency penalty, >= -2 and <= 2
MaxTokens int `json:"max_tokens,omitempty"` // Optional: Maximum tokens, > 1
PresencePenalty float32 `json:"presence_penalty,omitempty"` // Optional: Presence penalty, >= -2 and <= 2
Temperature float32 `json:"temperature,omitempty"` // Optional: Sampling temperature, <= 2
TopP float32 `json:"top_p,omitempty"` // Optional: Nucleus sampling parameter, <= 1
ResponseFormat *ResponseFormat `json:"response_format,omitempty"` // Optional: Custom response format
Stop []string `json:"stop,omitempty"` // Optional: Stop signals
Tools []Tools `json:"tools,omitempty"` // Optional: List of tools
LogProbs bool `json:"logprobs,omitempty"` // Optional: Enable log probabilities
TopLogProbs int `json:"top_logprobs,omitempty"` // Optional: Number of top tokens with log probabilities, <= 20
JSONMode bool `json:"json,omitempty"` // Optional: Enable JSON mode. If you're using the JSON mode, please mention "json" anywhere in your prompt, and also include the JSON schema in the request.
}
make a different struct for streaming with streaming options parameter
type ChatCompletionResponse ¶ added in v1.1.1
type ChatCompletionResponse struct {
ID string `json:"id"` // Unique identifier for the chat completion.
Object string `json:"object"` // Type of the object, typically "chat.completion".
Created int64 `json:"created"` // Timestamp when the chat completion was created.
Model string `json:"model"` // The model used for generating the completion.
Choices []Choice `json:"choices"` // List of completion choices generated by the model.
Usage Usage `json:"usage"` // Token usage statistics.
SystemFingerprint *string `json:"system_fingerprint,omitempty"` // Fingerprint of the system configuration.
}
func HandleChatCompletionResponse ¶ added in v1.1.1
func HandleChatCompletionResponse(resp *http.Response) (*ChatCompletionResponse, error)
type ChatCompletionStream ¶
type ChatCompletionStream interface {
Recv() (*StreamChatCompletionResponse, error)
Close() error
}
ChatCompletionStream is an interface for receiving streaming chat completion responses.
type Choice ¶ added in v1.1.1
type Choice struct {
Index int `json:"index"` // Index of the choice in the list of choices.
Message Message `json:"message"` // The message generated by the model.
LogProbs *LogProbs `json:"logprobs,omitempty"` // Log probabilities of the tokens, if available.
FinishReason string `json:"finish_reason"` // Reason why the completion finished.
}
type Client ¶
type Client struct {
AuthToken string // The authentication token for the API
BaseURL string // The base URL for the API
Timeout time.Duration // The timeout for the current Client
}
func NewClient ¶
NewClient creates a new client with an authentication token and an optional custom baseURL. If no baseURL is provided, it defaults to "https://api.deepseek.com/".
func NewClientWithOptions ¶ added in v1.1.1
NewClient creates a new client with required authentication token and optional configurations. Defaults: - BaseURL: "https://api.deepseek.com/" - Timeout: 5 minutes
func (*Client) CreateChatCompletion ¶
func (c *Client) CreateChatCompletion( ctx context.Context, request *ChatCompletionRequest, ) (*ChatCompletionResponse, error)
CreateChatCompletion sends a chat completion request and returns the generated response.
func (*Client) CreateChatCompletionStream ¶
func (c *Client) CreateChatCompletionStream( ctx context.Context, request *StreamChatCompletionRequest, ) (ChatCompletionStream, error)
CreateStreamChatCompletion send a chat completion request with stream = true and returns the delta
type Function ¶
type Function struct {
Name string `json:"name"` // The name of the function (required)
Description string `json:"description"` // Description of the function (required)
Parameters *Parameters `json:"parameters,omitempty"` // Parameters schema (optional)
}
Function defines the structure of a function tool
type JSONExtractor ¶ added in v1.1.1
type JSONExtractor struct {
// contains filtered or unexported fields
}
JSONExtractor helps extract structured data from LLM responses
func NewJSONExtractor ¶ added in v1.1.1
func NewJSONExtractor(schema json.RawMessage) *JSONExtractor
NewJSONExtractor creates a new JSONExtractor instance
func (*JSONExtractor) ExtractJSON ¶ added in v1.1.1
func (je *JSONExtractor) ExtractJSON(response *ChatCompletionResponse, target interface{}) error
ExtractJSON attempts to extract and parse JSON from an LLM response
type Option ¶ added in v1.1.1
Option configures a Client instance
func WithBaseURL ¶ added in v1.1.1
WithBaseURL sets the base URL for the API client
func WithTimeout ¶ added in v1.1.1
WithTimeout sets the timeout for API requests
func WithTimeoutString ¶ added in v1.1.1
WithTimeoutString parses a duration string and sets the timeout Example valid values: "5s", "2m", "1h"
type Parameters ¶
type ResponseFormat ¶
type ResponseFormat struct {
Type string `json:"type"` //either text or json_object. If json_object, please mention "json" anywhere in your prompt.
}
type StreamChatCompletionMessage ¶
type StreamChatCompletionMessage struct {
Role string `json:"role"`
Content string `json:"content"`
}
StreamChatCompletionMessage represents a single message in a chat completion stream.
type StreamChatCompletionRequest ¶
type StreamChatCompletionRequest struct {
Stream bool `json:"stream,omitempty"` //Comments: Defaults to true, since it's "STREAM"
Model string `json:"model"` // Required: Model ID, e.g., "deepseek-chat"
Messages []ChatCompletionMessage `json:"messages"` // Required: List of messages
FrequencyPenalty float32 `json:"frequency_penalty,omitempty"` // Optional: Frequency penalty, >= -2 and <= 2
MaxTokens int `json:"max_tokens,omitempty"` // Optional: Maximum tokens, > 1
PresencePenalty float32 `json:"presence_penalty,omitempty"` // Optional: Presence penalty, >= -2 and <= 2
Temperature float32 `json:"temperature,omitempty"` // Optional: Sampling temperature, <= 2
TopP float32 `json:"top_p,omitempty"` // Optional: Nucleus sampling parameter, <= 1
ResponseFormat *ResponseFormat `json:"response_format,omitempty"` // Optional: Custom response format: just don't try, it breaks rn ;)
Stop []string `json:"stop,omitempty"` // Optional: Stop signals
Tools []Tools `json:"tools,omitempty"` // Optional: List of tools
LogProbs bool `json:"logprobs,omitempty"` // Optional: Enable log probabilities
TopLogProbs int `json:"top_logprobs,omitempty"` // Optional: Number of top tokens with log probabilities, <= 20
}
StreamChatCompletionRequest represents the request body for a streaming chat completion API call.
type StreamChatCompletionResponse ¶
type StreamChatCompletionResponse struct {
ID string `json:"id"` // ID of the response.
Object string `json:"object"` // Type of object.
Created int64 `json:"created"` // Creation timestamp.
Model string `json:"model"` // Model used.
Choices []StreamChoices `json:"choices"` // Choices generated.
Usage *StreamUsage `json:"usage,omitempty"` // Usage statistics (optional).
}
StreamChatCompletionResponse represents a single response from a streaming chat completion API call.
type StreamChoices ¶
type StreamChoices struct {
Index int `json:"index"` // Index of the choice.
Delta StreamDelta // Delta information.
FinishReason string `json:"finish_reason"` // Reason for finishing the generation.
}
StreamChoices represents a choice in the chat completion stream.
type StreamDelta ¶
type StreamDelta struct {
Role string `json:"role,omitempty"` // Role of the message.
Content string `json:"content"` // Content of the message.
}
StreamDelta represents a delta in the chat completion stream.
type StreamOptions ¶
type StreamOptions struct {
IncludeUsage bool
}
type StreamUsage ¶
type StreamUsage struct {
PromptTokens int `json:"prompt_tokens"` // Number of tokens in the prompt.
CompletionTokens int `json:"completion_tokens"` // Number of tokens in the completion.
TotalTokens int `json:"total_tokens"` // Total number of tokens used.
}
StreamUsage represents token usage statistics for a streaming chat completion response. You will get {0 0 0} up until the last stream delta.
type TokenEstimate ¶ added in v0.1.1
type TokenEstimate struct {
EstimatedTokens int `json:"estimated_tokens"` //the total estimated prompt tokens. These are different form total tokens used.
}
TokenEstimate represents an estimated token count
func EstimateTokenCount ¶ added in v0.1.1
func EstimateTokenCount(text string) *TokenEstimate
EstimateTokenCount estimates the number of tokens in a text based on character type ratios
func EstimateTokensFromMessages ¶ added in v0.1.1
func EstimateTokensFromMessages(messages *ChatCompletionRequest) *TokenEstimate
EstimateTokensFromMessages estimates the number of tokens in a list of chat messages
type Tools ¶
type Tools struct {
Type string `json:"type"` // Type of the tool, e.g., "function" (required)
Function Function `json:"function"` // The function details (required)
}
Tool defines the structure for a tool
type Usage ¶ added in v1.1.1
type Usage struct {
PromptTokens int `json:"prompt_tokens"` // Number of tokens used in the prompt.
CompletionTokens int `json:"completion_tokens"` // Number of tokens used in the completion.
TotalTokens int `json:"total_tokens"` // Total number of tokens used.
PromptCacheHitTokens int `json:"prompt_cache_hit_tokens"` // Number of tokens served from cache.
PromptCacheMissTokens int `json:"prompt_cache_miss_tokens"` // Number of tokens not served from cache.
}