hal

package module
v0.0.0-...-a435009 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2023 License: Apache-2.0 Imports: 16 Imported by: 0

README

HAL

HAL is a voice-enabled version of chatGPT that can run on command-line terminals, and multilingual support.

Note: the Azure Speech SDK for go only support linux, so this project only run on linux environment. The Speech SDK does not support OpenSSL 3.0, which is the default in Ubuntu 22.04. On Ubuntu 22.04 only, install the latest libssl1.1 either as a binary package, or by compiling it from sources. Read more about this.

Prerequisites

Get an OpenAI API key
Get an Azure Speech key

Quick Start

1. Install

require: go >= 1.18

git clone https://github.com/neotse/hal.git
cd hal
chmod +x install.sh
./install.sh
2. Configure

just run it, and following the guide to configure.

hal

If you want to reconfigure, run below command:

hal --init

Or, you can see the help:

hal --help
3. Use
Activate and Deactivate

Try to say Harold to Activate, and stopword (configured above) to Deactivate (or automatically deactivated if there is no speech for a while).

Talk

just talk to HAL in your language (configured above)

if you want custom keyword to activate

First, you need generate a keyword model file from Azure, and download the model file. Then, use hal keyword command to configure it.

Usage of keyword:
  -keyword string
        set the keyword for activate (case insensitive), path and lang must be set at same time.
  -lang string
        set the language of keyword. (see https://learn.microsoft.com/en-us/azure/cognitive-services/speech-service/language-support?tabs=stt)
  -path string
        set the path of model file of keyword.
  -show
        show the current config of keyword for activate.

Session

Most of the time, conversations have context. Therefore, retaining some context can improve the quality of chatGPT's responses. In addition, OpenAI also provides system type messages to reinforce chatGPT's attention to improve the quality of responses. Therefore, here, sessions are used to retain this information. You can manage sessions, including listing sessions, selecting sessions, editing sessions, and creating sessions. Session management can be done in two ways:

Pre-run configuration

use hal session command to configure sessions.

Usage of session:
  -config
        config the 'session' for talk. 
  -create
        create the 'session' for talk. 
  -delete
        delete the 'session' for talk. 
  -list
        list current chatgpt sessions.
  -select
        select the 'session' for start to talk. If not set, it will select the session recently used.
Configuration by voice

When running, you can say the keyword (or similar in meaning) to invoke corresponding the action.

action keyword for example (other languages are also supported)
list list sessions please list the sessions
select select session I want to select a session
delete delete session delete a session
create create session help me create a session
config configure session configure the session

Documentation

Index

Constants

Variables

View Source
var (
	ErrRepeatConfig = errors.New("found a same config")
	ErrNoSuchHook   = errors.New("hook not exists")
)
View Source
var (
	ErrSpeechRecognitionTimeout                    = errors.New("speech recognition get result timeout")
	MaxSpeechRecognitionDelay                      = time.Duration(30)
	SegmentationSilenceTimeoutMs                   = "1500" //1.5s
	SpeechServiceConnectionInitialSilenceTimeoutMs = "5000" //5s
)
View Source
var (
	ErrSpeechSynthesisTimeout = errors.New("speech synthesis get result timeout")
	MaxSpeechSynthesisDelay   = time.Duration(10)
)
View Source
var AUTO_DETECTED_LANGUAGE = map[string]string{

	"English": "en-US",
	"French":  "fr-FR",
	"Chinese": "zh-CN",
	"Spanish": "es-ES",
}

because azure stt go speech sdk only support 4 languages in auto detected by languages (not support full languages auto detected)

View Source
var CHATGPTS = newChatGPTs()
View Source
var HOOKS = newHooks()
View Source
var PARAMS = Params{MaxHistory: 4, Language: "en-US", Voice: "en-US-ElizabethNeural"}

Functions

func CheckOpenaiKey

func CheckOpenaiKey(key string) error

func ConfigSession

func ConfigSession()

func CreateASession

func CreateASession()

func DefaultLogger

func DefaultLogger(level Level) *defaultLog

func DeleteSession

func DeleteSession()

func FormatVoices

func FormatVoices(voices []*Voice) string

func GetAutoDetectedLanguages

func GetAutoDetectedLanguages() []string

func GetAutoDetectedLanguagesName

func GetAutoDetectedLanguagesName() []string

func Initialize

func Initialize(chatGPTModel, language, voice, stopWord string)

func ListSessions

func ListSessions()

func SelectSession

func SelectSession()

func SetLevel

func SetLevel(level Level)

func Showkeyword

func Showkeyword()

Types

type ChatGPT

type ChatGPT struct {
	System     *openai.ChatCompletionMessage   `json:"system"`
	History    []*openai.ChatCompletionMessage `json:"history"`
	MaxHistory int                             `json:"maxHistory"`
	Model      string                          `json:"model"`
	Key        string                          `json:"key"`
	IsDefault  bool                            `json:"default"`
	// contains filtered or unexported fields
}

func NewChatGPT

func NewChatGPT(key string, model string) *ChatGPT

func (*ChatGPT) DisableHistory

func (c *ChatGPT) DisableHistory()

func (*ChatGPT) Prompt

func (c *ChatGPT) Prompt(text string) (string, int, error)

func (*ChatGPT) PromptStream

func (c *ChatGPT) PromptStream(text string) (*StreamResult, error)

func (*ChatGPT) SetMaxHistory

func (c *ChatGPT) SetMaxHistory(n int)

func (*ChatGPT) SetModel

func (c *ChatGPT) SetModel(model string)

func (*ChatGPT) SetRole

func (c *ChatGPT) SetRole(text string)

func (*ChatGPT) String

func (c *ChatGPT) String() string

type ChatGPTs

type ChatGPTs struct {
	Clients map[string]*ChatGPT `json:"clients"`
}

func (ChatGPTs) DelSession

func (c ChatGPTs) DelSession(sessionName string)

func (ChatGPTs) GetDefaultGPT

func (c ChatGPTs) GetDefaultGPT() (string, *ChatGPT)

func (*ChatGPTs) LoadChatGPTs

func (c *ChatGPTs) LoadChatGPTs(file string) error

func (ChatGPTs) NewDefaultSession

func (c ChatGPTs) NewDefaultSession(key string, model string) *ChatGPT

func (ChatGPTs) NewSessionWithName

func (c ChatGPTs) NewSessionWithName(sessionName string, key string, model string) *ChatGPT

func (ChatGPTs) RenameSession

func (c ChatGPTs) RenameSession(oldName string, newName string) *ChatGPT

func (ChatGPTs) SaveChatGPTs

func (c ChatGPTs) SaveChatGPTs(file string) error

func (ChatGPTs) Sessions

func (c ChatGPTs) Sessions() []string

func (ChatGPTs) SessionsWithout

func (c ChatGPTs) SessionsWithout(name string) []string

func (ChatGPTs) SetDefaultGPT

func (c ChatGPTs) SetDefaultGPT(sessionName string)

type Hook

type Hook interface {
	New() Hook
	Check(stt string) bool
	SetKeyword(text string)
	Name() string
	Exec() error
}

type HookConfig

type HookConfig struct {
	Keyword  string `json:"keyword"`
	HookName string `json:"hook"`
	Enable   bool   `json:"enable"`
	// contains filtered or unexported fields
}

type Hooks

type Hooks struct {
	Configs map[string]*HookConfig `json:"hookConfigs"`
	// contains filtered or unexported fields
}

func (*Hooks) Add

func (h *Hooks) Add(keyword string, name string) error

func (*Hooks) Delete

func (h *Hooks) Delete(id string) bool

func (*Hooks) Disable

func (h *Hooks) Disable(id string)

func (*Hooks) Enable

func (h *Hooks) Enable(id string)

func (*Hooks) Exec

func (h *Hooks) Exec(hookName string) error

func (*Hooks) Get

func (h *Hooks) Get(id string) *HookConfig

func (*Hooks) IsExist

func (h *Hooks) IsExist(hookName string) bool

func (*Hooks) LoadHooks

func (h *Hooks) LoadHooks(file string) error

func (*Hooks) SaveHooks

func (h *Hooks) SaveHooks(file string) error

type KeywordRecognitionStandalone

type KeywordRecognitionStandalone struct {
	SpeechRecognitionStream

	KeyWord string
	// contains filtered or unexported fields
}

func NewKeywordRecognitionStandalone

func NewKeywordRecognitionStandalone(key, region string, languages []string, model, keyWord string) (*KeywordRecognitionStandalone, error)

func (*KeywordRecognitionStandalone) Close

func (*KeywordRecognitionStandalone) Result

func (s *KeywordRecognitionStandalone) Result() (string, error)

func (*KeywordRecognitionStandalone) SpeechToText

func (s *KeywordRecognitionStandalone) SpeechToText(data []byte) error

func (*KeywordRecognitionStandalone) Start

type Level

type Level int
const (
	DEBUG Level = iota
	INFO
	WARNING
	ERROR
	SLICENCE
)

type Logger

type Logger interface {
	Errorf(string, ...interface{})
	Warningf(string, ...interface{})
	Infof(string, ...interface{})
	Debugf(string, ...interface{})
}

type Params

type Params struct {
	Initialized  bool
	OpenaiKey    string
	ChatgptModel string
	MaxHistory   int

	SpeechKey       string
	SpeechRegion    string
	Language        string // BCP-47 code
	Voice           string
	StopWord        string
	Keyword         string
	KeywordModel    string
	KeywordLanguage string
}

func (*Params) LoadParams

func (p *Params) LoadParams(file string) error

func (Params) SaveParams

func (p Params) SaveParams(file string) error

func (Params) String

func (p Params) String() string

type SpeechRecognition

type SpeechRecognition interface {
	Start() error
	SpeechToText(data []byte) error
	Result() (string, error)
	Close() error
}

type SpeechRecognitionStandalone

type SpeechRecognitionStandalone struct {
	SpeechRecognitionStream
}

func NewAutoDetectedSpeechRecognitionStandalone

func NewAutoDetectedSpeechRecognitionStandalone(key, region string) (*SpeechRecognitionStandalone, error)

func NewSpeechRecognitionStandalone

func NewSpeechRecognitionStandalone(key, region string, languages []string) (*SpeechRecognitionStandalone, error)

func (*SpeechRecognitionStandalone) SpeechToText

func (s *SpeechRecognitionStandalone) SpeechToText(data []byte) error

type SpeechRecognitionStream

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

func NewAutoDetectedSpeechRecognitionStream

func NewAutoDetectedSpeechRecognitionStream(key, region string) (*SpeechRecognitionStream, error)

func NewSpeechRecognitionStream

func NewSpeechRecognitionStream(key, region string, languages []string) (*SpeechRecognitionStream, error)

func (*SpeechRecognitionStream) Close

func (s *SpeechRecognitionStream) Close() error

func (*SpeechRecognitionStream) Result

func (s *SpeechRecognitionStream) Result() (string, error)

func (*SpeechRecognitionStream) SpeechToText

func (s *SpeechRecognitionStream) SpeechToText(data []byte) error

func (*SpeechRecognitionStream) Start

func (s *SpeechRecognitionStream) Start() error

type SpeechSynthesis

type SpeechSynthesis interface {
	TextToSpeech(text string) error
	Result() (*WordBoundery, []byte, error)
	Error() error
	Close() error
}

type SpeechSynthesisStandalone

type SpeechSynthesisStandalone struct {
	SpeechSynthesisStream
}

func NewAutoDetectedSpeechSynthesisStandalone

func NewAutoDetectedSpeechSynthesisStandalone(key, region string) (*SpeechSynthesisStandalone, error)

func NewSpeechSynthesisStandalone

func NewSpeechSynthesisStandalone(key, region string, voice string) (*SpeechSynthesisStandalone, error)

type SpeechSynthesisStream

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

func NewAutoDetectedSpeechSynthesisStream

func NewAutoDetectedSpeechSynthesisStream(key, region string) (*SpeechSynthesisStream, error)

func NewSpeechSynthesisStream

func NewSpeechSynthesisStream(key, region string, voice string) (*SpeechSynthesisStream, error)

func (*SpeechSynthesisStream) Close

func (s *SpeechSynthesisStream) Close()

func (*SpeechSynthesisStream) Error

func (s *SpeechSynthesisStream) Error() error

func (*SpeechSynthesisStream) GetAllSupportLanguage

func (s *SpeechSynthesisStream) GetAllSupportLanguage() map[string]bool

func (*SpeechSynthesisStream) GetAllSupportVoicesForLanguage

func (s *SpeechSynthesisStream) GetAllSupportVoicesForLanguage(language string) []*Voice

func (*SpeechSynthesisStream) Result

func (s *SpeechSynthesisStream) Result() (*WordBoundery, []byte, error)

func (*SpeechSynthesisStream) TextToSpeech

func (s *SpeechSynthesisStream) TextToSpeech(text string) error

type StreamResult

type StreamResult struct {
	Err error
	// contains filtered or unexported fields
}

func (*StreamResult) Next

func (s *StreamResult) Next() string

type StreamSplitter

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

func NewStreamSplitter

func NewStreamSplitter(stream *StreamResult) StreamSplitter

func (StreamSplitter) Segment

func (ss StreamSplitter) Segment(echo bool) string

type Voice

type Voice struct {
	Name      string
	LocalName string
	Gender    string
	StyleList []string
}

func (*Voice) String

func (v *Voice) String() string

type WordBoundery

type WordBoundery struct {
	BounderyType common.SpeechSynthesisBoundaryType
	AudioOffset  float64
	Duration     float64
	TextOffset   uint
	WordLength   uint
	Text         string
}

func (WordBoundery) String

func (w WordBoundery) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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