EdgeGPT

package module
v0.0.0-...-4d66b27 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2024 License: MIT Imports: 14 Imported by: 0

README

🪄 EdgeGPT-Go

Go version Release Go Reference

EdgeGPT-GO preview

EdgeGPT-Go is a New Bing unofficial API developed using Golang. You can use it as a library, microservice or standalone cli application. The package supports multiple cookies. As well as rapid deployment as a microservice via docker.

Feature:

  • GRPC interface
  • Library interface
  • Sync/Async request
  • CLI interface
  • Refresh session

How to use it:

Use as a chat cli app

docker run -it -v ./cookies:/app/cookies ghcr.io/pavel-one/edgegpt-chat

Or download binary in release page

Use as a library

go get github.com/Xpl0itU/EdgeGPT-Go
package main

import (
	"github.com/Xpl0itU/EdgeGPT-Go"
	"log"
)

func main() {
	s := EdgeGPT.NewStorage()

	gpt, err := s.GetOrSet("any-key")
	if err != nil {
		log.Fatalln(err)
	}

	// send ask async
	mw, err := gpt.AskAsync("Hi, you're alive?")
	if err != nil {
		log.Fatalln(err)
	}

	go mw.Worker() // start worker

	for range mw.Chan {
		// update answer
		log.Println(mw.Answer.GetAnswer())
		log.Println(mw.Answer.GetType())
		log.Println(mw.Answer.GetSuggestions())
		log.Println(mw.Answer.GetMaxUnit())
		log.Println(mw.Answer.GetUserUnit())
	}

	// send sync ask
	as, err := gpt.AskSync("Show an example of sockets in golang gorilla")
	if err != nil {
		log.Fatalln(err)
	}

	log.Println(as.Answer.GetAnswer())
}

Use as a docker microservice

docker run -v ./cookies:/app/cookies -p 8080:8080 ghcr.io/pavel-one/edgegpt-grpc:latest

Use as a docker-compose

version: "3"
services:
  gpt:
    image: ghcr.io/pavel-one/edgegpt-grpc:latest
    restart: unless-stopped
    ports:
      - "8080:8080"
    volumes:
      - ./cookies:/app/cookies

Available environment

Name Default Description
CONVERSATION_ENDPOINT https://edgeservices.bing.com/edgesvc/turing/conversation/create Endpoint for getting
conversation
LOG_LEVEL INFO Log level. Avalible:
  • DEBUG
  • INFO
  • WARN
  • ERROR
  • DPANIC
  • PANIC
  • FATAL

Example service

Work progress...

FAQ:

Change protoc

If you change protoc file, use protoc --go_out=. --go-grpc_out=. proto/gpt.proto

Stopped after 10 redirects

If you use this library in China set environment:

CONVERSATION_ENDPOINT=https://edge.churchless.tech/edgesvc/turing/conversation/create

Example:

docker run -e CONVERSATION_ENDPOINT=https://edge.churchless.tech/edgesvc/turing/conversation/create -v ./cookies:/app/cookies -p 8080:8080 ghcr.io/pavel-one/edgegpt-grpc:latest

Or docker-compose:

version: "3"
services:
  gpt:
    image: ghcr.io/pavel-one/edgegpt-grpc:latest
    restart: unless-stopped
    environment:
      - CONVERSATION_ENDPOINT=https://edge.churchless.tech/edgesvc/turing/conversation/create
    ports:
      - "8080:8080"
    volumes:
      - ./cookies:/app/cookies

Documentation

Index

Constants

View Source
const (
	StyleCreative      = "h3imaginative,clgalileo,gencontentv3"
	StyleBalanced      = "galileo"
	StylePrecise       = "h3precise,clgalileo"
	DelimiterByte      = uint8(30)
	Delimiter          = "\x1e"
	MaxMessageSizeGPT3 = 2000
	MaxMessageSizeGPT4 = 4000
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Conversation

type Conversation struct {
	ConversationId        string `json:"conversationId,omitempty"`
	ClientId              string `json:"clientId,omitempty"`
	ConversationSignature string `json:"conversationSignature,omitempty"`
	Result                struct {
		Value   null.String `json:"value"`
		Message null.String `json:"message"`
	} `json:"result"`
}

type GPT

type GPT struct {
	Config *config.GPT

	Conversation *Conversation
	ExpiredAt    time.Time
	Hub          *Hub
	// contains filtered or unexported fields
}

func NewGPT

func NewGPT(conf *config.GPT) (*GPT, error)

NewGPT create new service

func (*GPT) AskAsync

func (g *GPT) AskAsync(style, message string) (*responses.MessageWrapper, error)

AskAsync getting answer async: Example:

gpt, err := EdgeGPT.NewGPT(conf) //create service
if err != nil {
	log.Fatalln(err)
}

mw, err := gpt.AskAsync("Привет, ты живой?") // send ask to gpt
if err != nil {
	log.Fatalln(err)
}

go mw.Worker() // Run reading websocket messages

for _ = range mw.Chan {
	// update answer
	log.Println(mw.Answer.GetAnswer())
}

func (*GPT) AskSync

func (g *GPT) AskSync(style, message string) (*responses.MessageWrapper, error)

AskSync getting answer sync

type Hub

type Hub struct {
	InvocationId int
	// contains filtered or unexported fields
}

func NewHub

func NewHub(conversation *Conversation, config *config.GPT) (*Hub, error)

func (*Hub) CheckAndReconnect

func (h *Hub) CheckAndReconnect() error

CheckAndReconnect check active connection and reconnect

func (*Hub) Close

func (h *Hub) Close()

Close hub and connection TODO: Use this!

func (*Hub) NewConnect

func (h *Hub) NewConnect() (*websocket.Conn, error)

NewConnect create new websocket connection

type Storage

type Storage map[string]*GPT

Storage for GPT sessions. Use ony for servers

func NewStorage

func NewStorage() *Storage

func (*Storage) Add

func (s *Storage) Add(gpt *GPT, key string)

Add new session

func (*Storage) Get

func (s *Storage) Get(key string) (*GPT, error)

Get get current session, or error

func (*Storage) GetOrSet

func (s *Storage) GetOrSet(key string) (*GPT, error)

GetOrSet get current session, or create new

func (*Storage) Remove

func (s *Storage) Remove(key string) error

Remove session

Directories

Path Synopsis
cli
cmd
internal
pkg

Jump to

Keyboard shortcuts

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