Documentation
¶
Index ¶
- Constants
- type AskOpts
- type Auth
- type ChatError
- type ChatResponse
- type Choice
- type Client
- func (c *Client) Ask(ctx context.Context, prompt string, askOpts ...AskOpts) (*ChatResponse, error)
- func (c *Client) AskInternet(ctx context.Context, prompt string) (*ChatResponse, error)
- func (c *Client) AskStream(ctx context.Context, prompt string, askOpts ...AskOpts) (chan *ChatResponse, error)
- func (c *Client) GetAPIKey() string
- func (c *Client) GetAccessToken() string
- func (c *Client) GetConversation(id string) (*Conversation, error)
- func (c *Client) GetConversations() map[string]Conversation
- func (c *Client) GetEnableInternet() bool
- func (c *Client) GetEngine() string
- func (c *Client) GetProxy() *url.URL
- func (c *Client) GetStream() bool
- func (c *Client) ResetConversation(id string) error
- func (c *Client) ResetConversations()
- func (c *Client) SetAPIKey(apiKey string)
- func (c *Client) SetAccessToken(accessToken string)
- func (c *Client) SetConversation(id string, conv Conversation)
- func (c *Client) SetEmailAndPassword(email, password string)
- func (c *Client) SetEngine(engine string)
- func (c *Client) SetProxy(proxy *url.URL)
- func (c *Client) SetSessionName(sessionName string)
- func (c *Client) Start() error
- func (c *Client) ToggleInternet(t bool)
- func (c *Client) ToggleStream(t bool)
- type Config
- type Conversation
- type InternetResponse
- type LogLevel
- type Logger
- type Message
- type OpenAIError
- type OpenAIResponse
- type Payload
- type TokenGen
Constants ¶
const ( ApiKeyMode = iota // Set a value of 0 to ApiKeyMode. This indicates that the user of the program has set up API key properly. AccessTokenMode // Set a value of 1 to AccessTokenMode. This indicates that the user of the program has set up access token properly. )
const DEFAULT_INIT_MESSAGE = "You are chatGPT, trained on a very huge dataset of conversations. Act conversationally"
The default "system" message when starting a new conversation.
const OPENAI_HOST = "https://api.openai.com/v1/chat/completions"
The OpenAI API endpoint for chat completions.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Auth ¶
type Auth struct {
// contains filtered or unexported fields
}
func (*Auth) GetAccessToken ¶
GetAccessToken generates and retrieves the OpenAI API access token by performing a series of authentication steps.
type ChatError ¶
type ChatError struct {
Message string `json:"message,omitempty"`
Code int `json:"code,omitempty"`
}
ChatError represents a chat/auth-specific error returned by this client.
type ChatResponse ¶
type ChatResponse struct {
Message string `json:"message,omitempty"`
ConversationID string `json:"conversation_id,omitempty"`
ParentID string `json:"parent_id,omitempty"`
Model string `json:"model,omitempty"`
}
ChatResponse represents the response returned by this client.
type Choice ¶
type Choice struct {
Message Message `json:"message,omitempty"`
FinishReason string `json:"finish_reason,omitempty"`
}
Choice represents a possible response and its finish reason from OpenAI's API.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client represents a connection to the OpenAI API. It contains the client's API key, access token, HTTP client, conversation history, settings, and stream details.
func NewClient ¶
NewClient creates a new OpenAI API client with the given configuration. sessionName is an optional parameter that can be used to identify the client in case of multiple clients.
func (*Client) Ask ¶
Ask sends a question to OpenAI API using the specified conversation ID or the default one.
func (*Client) AskInternet ¶
AskInternet sends a question to the specified internet engine and returns the response/error.
func (*Client) AskStream ¶
func (c *Client) AskStream(ctx context.Context, prompt string, askOpts ...AskOpts) (chan *ChatResponse, error)
AskStream sends a question to OpenAI API using the specified conversation ID or the default one and streams the response.
func (*Client) GetAccessToken ¶
GetAccessToken returns the access token used for conversations.
func (*Client) GetConversation ¶
func (c *Client) GetConversation(id string) (*Conversation, error)
GetConversation returns a specific conversation by ID, or an error if it doesn't exist.
func (*Client) GetConversations ¶
func (c *Client) GetConversations() map[string]Conversation
GetConversations returns a map of all conversations currently stored in memory.
func (*Client) GetEnableInternet ¶
GetEnableInternet returns true if external websites can be accessed in responses.
func (*Client) GetStream ¶
GetStream returns true if response messages are streamed as they come in.
func (*Client) ResetConversation ¶
ResetConversation deletes a specific conversation by ID, or returns an error if it doesn't exist.
func (*Client) ResetConversations ¶
func (c *Client) ResetConversations()
ResetConversations deletes all conversations from memory.
func (*Client) SetAccessToken ¶
SetAccessToken sets the access token used for conversations.
func (*Client) SetConversation ¶
func (c *Client) SetConversation(id string, conv Conversation)
SetConversation sets a specific conversation by ID.
func (*Client) SetEmailAndPassword ¶
SetEmailAndPassword sets the email and password used for authentication.
func (*Client) SetSessionName ¶
SetSessionName sets the session name used for authentication.
func (*Client) Start ¶
Start initializes the client by checking credentials and authenticating with the OpenAI API.
func (*Client) ToggleInternet ¶
ToggleInternet toggles whether or not to allow the use of external websites in responses.
func (*Client) ToggleStream ¶
ToggleStream toggles whether or not to stream response messages as they come in.
type Config ¶
type Config struct {
ApiKey string `json:"api_key,omitempty"` // The API key used for authentication with OpenAI.
Email string `json:"email,omitempty"` // The email used for authentication with OpenAI.
Password string `json:"password,omitempty"` // The password used for authentication with OpenAI.
AccessToken string `json:"access_token,omitempty"` // The access token used for conversations with OpenAI.
Engine string `json:"engine,omitempty"` // The name of the GPT model being used.
InitMessage string `json:"init_message,omitempty"` // The initial message sent to start a new conversation.
BaseURL string `json:"base_url,omitempty"` // Custom base URL for the OpenAI API.
Temperature float64 `json:"temperature,omitempty"` // The sampling temperature for generating text.
LogLevel LogLevel `json:"log_level,omitempty"` // The log level to use for logging messages.
IsPaid bool `json:"is_paid,omitempty"` // Whether or not the account is a paid account.
EnableInternet bool `json:"enable_internet,omitempty"` // Whether or not to allow the use of external websites in responses.
Stream bool `json:"stream,omitempty"` // Whether or not to stream response messages as they come in.
DisableCache bool `json:"disable_cache,omitempty"` // Whether or not to disable caching of access tokens.
Proxy *url.URL `json:"proxy,omitempty"` // The URL of the proxy server to use for requests.
}
Config represents the configuration options for a connection to the OpenAI API. Each field is optional and can be omitted from the JSON representation of the config object.
type Conversation ¶
type Conversation struct {
InitMessage string // First message sent in the conversation.
LastMessage string // Most recent message sent in the conversation.
Messages []Message // Slice of Message structs representing all messages sent in the conversation.
}
Conversation represents a struct with three fields: InitMessage, LastMessage, and Messages.
func (*Conversation) Marshal ¶
func (c *Conversation) Marshal() string
type InternetResponse ¶
type InternetResponse []struct {
// The title of the search result.
Title string `json:"title"`
// The URL of the search result.
Link string `json:"link"`
// A brief snippet of text from the search result.
Snippet string `json:"snippet"`
}
InternetResponse is a slice of structs representing search results obtained from an internet search engine.
type Logger ¶
type Logger struct {
// The minimum level of messages to log.
Level LogLevel
// contains filtered or unexported fields
}
Logger is a simple logger that can be used to log messages to the console.
func (*Logger) SessionName ¶
SessionName returns the name of the session, or an empty string if it's the default session.
type Message ¶
type Message struct {
Role string `json:"role,omitempty"` // Tag defies the JSON key name as "role" or omits the key if the value is empty.
Content string `json:"content,omitempty"` // Tag defies the JSON key name as "content" or omits the key if the value is empty.
}
Message represents a struct with two fields: Role and Content.
type OpenAIError ¶
type OpenAIError struct {
ErrorData struct {
Message string `json:"message"`
Type string `json:"type"`
Param string `json:"param"`
Code int `json:"code"`
} `json:"error"`
}
OpenAIError represents an error returned by OpenAI's API.
type OpenAIResponse ¶
type OpenAIResponse struct {
ID string `json:"id"`
Object string `json:"object"`
Created int `json:"created"`
Model string `json:"model"`
Usage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
} `json:"usage"`
Choices []Choice `json:"choices"`
}
OpenAIResponse represents the response returned by OpenAI's API.
func (*OpenAIResponse) GetResponse ¶
func (r *OpenAIResponse) GetResponse() string
type Payload ¶
type Payload struct {
// The ID of the OpenAI model to use for the request.
Model string `json:"model"`
// An array of Message structs representing the conversation so far.
Messages []Message `json:"messages"`
// The "temperature" parameter controls the "creativity" of the AI's responses. Higher values will generate more diverse and unexpected responses.
Temperature float64 `json:"temperature"`
// The "top-p" parameter controls the "conservatism" of the AI's responses. Lower values will generate more predictable and "safe" responses.
TopP float64 `json:"top_p"`
}
Payload represents the structure of the JSON payload sent by askOpenAI.
type TokenGen ¶
type TokenGen struct {
// contains filtered or unexported fields
}
func NewTokenGen ¶
func NewTokenGen() *TokenGen
func (*TokenGen) CloseChrome ¶
func (t *TokenGen) CloseChrome()