Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // RoleSystem is the role of a system message in a chat completion. RoleSystem = chatCompletionRole{"system"} // RoleDeveloper is the role of a developer message in a chat completion. RoleDeveloper = chatCompletionRole{"developer"} // RoleUser is the role of a user message in a chat completion. RoleUser = chatCompletionRole{"user"} // RoleAssistant is the role of an assistant message in a chat completion. RoleAssistant = chatCompletionRole{"assistant"} // RoleTool is the role of a tool message in a chat completion. RoleTool = chatCompletionRole{"tool"} )
View Source
var ( // FinishReasonStop is when the model hit a natural stop point or a provided stop sequence. FinishReasonStop = chatCompletionFinishReason{"stop"} // FinishReasonLength is when the maximum number of tokens specified in the request was reached. FinishReasonLength = chatCompletionFinishReason{"length"} // FinishReasonContentFilter is when content was omitted due to a flag from our content filters. FinishReasonContentFilter = chatCompletionFinishReason{"content_filter"} // FinishReasonToolCalls is when the model called a tool. FinishReasonToolCalls = chatCompletionFinishReason{"tool_calls"} // FinishReasonError is when the model returned an error. FinishReasonError = chatCompletionFinishReason{"error"} )
View Source
var ( // ErrBaseURLRequired is returned when the base URL is needed but not provided. ErrBaseURLRequired = errors.New("the base URL is required") // ErrAPIKeyRequired is returned when the API key is needed but not provided. ErrAPIKeyRequired = errors.New("the API key is required") // ErrMessagesRequired is returned when no messages are found and they are needed. ErrMessagesRequired = errors.New("at least one message is required") // ErrAlreadyExecuting is returned when the user tries to execute an action while // there is already an action in progress. ErrAlreadyExecuting = errors.New("race condition: the client is currently executing an action") )
Functions ¶
Types ¶
type ChatCompletionMessage ¶
type ChatCompletionMessage struct { // The ID of the tool call, if the message is a tool call. ToolCallID string `json:"tool_call_id,omitempty,omitzero"` // The name of the entity that sent the message or the name of the tool, if the message is a tool call. Name string `json:"name,omitempty,omitzero"` // Who the message is from. Must be one of openroutergo.RoleSystem, openroutergo.RoleUser, or openroutergo.RoleAssistant. Role chatCompletionRole `json:"role"` // The content of the message Content string `json:"content"` // When the model decided to call a tool ToolCalls []ChatCompletionMessageToolCall `json:"tool_calls,omitempty,omitzero"` }
func (ChatCompletionMessage) HasToolCalls ¶
func (c ChatCompletionMessage) HasToolCalls() bool
HasToolCalls returns true if the message has tool calls.
type ChatCompletionMessageToolCall ¶
type ChatCompletionMessageToolCall struct { // The ID of the tool call. ID string `json:"id"` // The type of tool call. Always "function". Type string `json:"type"` // Function is the function that the model wants to call. Function ChatCompletionMessageToolCallFunction `json:"function,omitempty,omitzero"` }
type ChatCompletionMessageToolCallFunction ¶
type ChatCompletionMessageToolCallFunction struct { // The name of the function to call. Name string `json:"name"` // The arguments to call the function with, as generated by the model in JSON // format. Note that the model does not always generate valid JSON, and may // hallucinate parameters not defined by your function schema. Validate the // arguments in your code before calling your function. // // You have to unmarshal the arguments to the correct type yourself. Arguments string `json:"arguments"` }
type ChatCompletionResponse ¶
type ChatCompletionResponse struct { // A unique identifier for the chat completion. ID string `json:"id"` // A list of chat completion choices (the responses from the model). Choices []ChatCompletionResponseChoice `json:"choices"` // Usage statistics for the completion request. Usage ChatCompletionResponseUsage `json:"usage"` // The Unix timestamp (in seconds) of when the chat completion was created. Created int `json:"created"` // The model used for the chat completion. Model string `json:"model"` // The provider used for the chat completion. Provider string `json:"provider"` // The object type, which is always "chat.completion" Object string `json:"object"` }
ChatCompletionResponse is the response from the OpenRouter API for a chat completion request.
- https://openrouter.ai/docs/api-reference/overview#responses
- https://platform.openai.com/docs/api-reference/chat/object
func (ChatCompletionResponse) HasChoices ¶
func (c ChatCompletionResponse) HasChoices() bool
HasChoices returns true if the chat completion has choices.
type ChatCompletionResponseChoice ¶
type ChatCompletionResponseChoice struct { // The reason the model stopped generating tokens. This will be `stop` if the model hit a // natural stop point or a provided stop sequence, `length` if the maximum number of // tokens specified in the request was reached, `content_filter` if content was omitted // due to a flag from our content filters, `tool_calls` if the model called a tool, or // `error` if the model returned an error. FinishReason chatCompletionFinishReason `json:"finish_reason"` // A chat completion message generated by the model. Message ChatCompletionMessage `json:"message"` }
type ChatCompletionResponseUsage ¶
type ChatCompletionResponseUsage struct { // The number of tokens in the prompt. PromptTokens int `json:"prompt_tokens"` // The number of tokens in the generated completion. CompletionTokens int `json:"completion_tokens"` // The total number of tokens used in the request (prompt + completion). TotalTokens int `json:"total_tokens"` }
type ChatCompletionTool ¶
type ChatCompletionTool struct { // The name of the tool, when the model calls this tool, it will return this name so // you can identify it. Name string `json:"name"` // The description of the tool, make sure to give a good description so the model knows // when to use it. Description string `json:"description,omitempty,omitzero"` // Make sure to define your tool's parameters using map[string]any and following the // JSON Schema format. // // - Format example: https://platform.openai.com/docs/guides/function-calling // - JSON Schema reference: https://json-schema.org/understanding-json-schema/reference Parameters map[string]any `json:"parameters"` }
ChatCompletionTool is a tool that can be used in a chat completion request.
- Models supporting tool calling: https://openrouter.ai/models?supported_parameters=tools
- JSON Schema reference: https://json-schema.org/understanding-json-schema/reference
- Tool calling example: https://platform.openai.com/docs/guides/function-calling
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client represents a client for the OpenRouter API.
func (*Client) NewChatCompletion ¶
func (c *Client) NewChatCompletion() *chatCompletionBuilder
NewChatCompletion creates a new chat completion request builder for the OpenRouter API.
Docs:
- Reference: https://openrouter.ai/docs/api-reference/chat-completion
- Request: https://openrouter.ai/docs/api-reference/overview#completions-request-format
- Parameters: https://openrouter.ai/docs/api-reference/parameters
- Response: https://openrouter.ai/docs/api-reference/overview#completionsresponse-format
Source Files
¶
Click to show internal directories.
Click to hide internal directories.