auth

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2023 License: BSD-3-Clause Imports: 9 Imported by: 0

README

openai-auth-go

An go library to get openai access token by username (email) and password.

Usage

Create an auther

auther, err := auth.NewAuthenticator()

Or you can create it with proxy

auther, err := auth.NewAuthenticator(auth.WithProxy("socks5://127.0.0.1:7890"))

Start authentication and get access_token

cred, err := auther.AuthUser("email@example.com", "passw0rd")
// ...
accessToken := cred.Session.AccessToken

Examples

package main

import (
        "context"
        "fmt"
        "log"

        gogpt "github.com/sashabaranov/go-gpt3"
        auth "github.com/wqyjh/openai-auth-go"
)

func main() {
        auther, err := auth.NewAuthenticator(auth.WithProxy("socks5://192.168.42.1:7890"))
        if err != nil {
                log.Fatalf("%+v\n", err)
        }
        cred, err := auther.AuthUser("email@example.com", "passw0rd")
        if err != nil {
                log.Fatalf("%+v\n", err)
        }
        log.Printf("%+v\n", cred)
        accessToken := cred.Session.AccessToken
        c := gogpt.NewClient(accessToken)

        log.Println("welcome")
        for {
                var prompt string
                fmt.Print("> ")
                fmt.Scanf("%s\n", &prompt)
                ctx := context.Background()
                req := gogpt.ChatCompletionRequest{
                        Model:       gogpt.GPT3Dot5Turbo,
                        MaxTokens:   1000,
                        Temperature: 0,
                        Messages: []gogpt.ChatCompletionMessage{
                                {
                                        Role:    "system",
                                        Content: "you are a translate assistant",
                                },
                                {
                                        Role:    "user",
                                        Content: prompt,
                                },
                        },
                }
                resp, err := c.CreateChatCompletion(ctx, req)
                if err != nil {
                        return
                }
                fmt.Printf("< %+v\n", resp.Choices[0].Message.Content)
        }
}

Documentation

Index

Constants

View Source
const (
	DefaultUserAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Authenticator

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

func NewAuthenticator

func NewAuthenticator(opts ...Option) (*Authenticator, error)

func (*Authenticator) AuthUser

func (a *Authenticator) AuthUser(email, password string) (Credential, error)

type Credential

type Credential struct {
	SessionToken string  `json:"accessToken"`
	Session      Session `json:"session"`
}

type Option

type Option func(*Authenticator)

func WithProxy

func WithProxy(proxy string) Option

func WithUserAgent

func WithUserAgent(ua string) Option

type Session

type Session struct {
	AccessToken string `json:"accessToken"`
	Expires     string `json:"expires"`
	User        User   `json:"user"`
}

type User

type User struct {
	ID      string `json:"id"`
	Email   string `json:"email"`
	Name    string `json:"name"`
	Image   string `json:"image"`
	Picture string `json:"picture"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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