Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Downstream pull #6

Merged
merged 4 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ Notes:
1. 30 billion parameters require 22Gb ram minumum, 13b ~= 13Gb RAM min
2. You can download models directly from hugginface
3. You need .gguf format and optimised quntisation choice
4. Wizard Uncensored LLM's are basically the same wizard-vicuna models but they are was trained at edited dataset, in which was removed biased answers
5. I am using stable-diffusion for image generation, for more info see localai stablediffusion
https://localai.io/features/image-generation/
6. If there are necessity of using embedded generations, you should also download bert model
https://gpt4all.io/models/gguf/all-MiniLM-L6-v2-f16.gguf

2. Setup template
```
Expand Down Expand Up @@ -60,6 +65,9 @@ AI_ENDPOINT=http://localhost:8080/v1/chat/completions

In case if you need to change url/port just change it in .env file

# Build bot
` go build`



# Example:
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ require (
)

require (
github.com/StarkBotsIndustries/telegraph v1.0.0 // indirect
github.com/anaskhan96/soup v1.2.5 // indirect
github.com/dlclark/regexp2 v1.8.1 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/mitchellh/hashstructure v1.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pkoukk/tiktoken-go v0.1.2 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
)

require (
Expand Down
13 changes: 13 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
github.com/StarkBotsIndustries/telegraph v1.0.0 h1:N/QcOyH3Ue/PbaZypqboH0MyjCoam7WvcJNBgYHcQpY=
github.com/StarkBotsIndustries/telegraph v1.0.0/go.mod h1:B4ry4Ye0sTE5DgxAvH/6MZU9yAmvyqoq0whPwGSqLms=
github.com/anaskhan96/soup v1.2.5 h1:V/FHiusdTrPrdF4iA1YkVxsOpdNcgvqT1hG+YtcZ5hM=
github.com/anaskhan96/soup v1.2.5/go.mod h1:6YnEp9A2yywlYdM4EgDz9NEHclocMepEtku7wg6Cq3s=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -23,6 +27,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
Expand All @@ -32,6 +37,14 @@ github.com/tmc/langchaingo v0.1.4 h1:skgT2G6D+BzbRFWg8bGE6MQgLPQ10LpyHxQ4vIhD/8k
github.com/tmc/langchaingo v0.1.4/go.mod h1:PKtJMXizDxJnT86q7lsVsyzTJqd0P2QKF7wt2jF6Lxk=
github.com/tucnak/telebot v2.0.0+incompatible h1:Amnb+h23aEnfKSDqFKU/R1qGSGgnS78Hm56lLVVQL2A=
github.com/tucnak/telebot v2.0.0+incompatible/go.mod h1:TCLoYDyssqVcjhkdyYu+He6eldK40im537vXoex2LM0=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
166 changes: 73 additions & 93 deletions internal/bot/command/cases.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package command

import (
"log"
"strings"
"net/url"
"path"

"github.com/JackBekket/uncensoredgpt_tgbot/internal/localai"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
Expand All @@ -25,7 +26,6 @@ func (c *Commander) InputYourAPIKey(updateMessage *tgbotapi.Message) {
c.usersDb[chatID] = user
}


/*
func (c *Commander) ChooseNetwork(updateMessage *tgbotapi.Message) {
chatID := updateMessage.From.ID
Expand All @@ -44,7 +44,7 @@ func (c *Commander) ChooseNetwork(updateMessage *tgbotapi.Message) {
tgbotapi.NewKeyboardButtonRow(
tgbotapi.NewKeyboardButton("openai"),
tgbotapi.NewKeyboardButton("localai")),

)
c.bot.Send(msg)

Expand All @@ -54,7 +54,6 @@ func (c *Commander) ChooseNetwork(updateMessage *tgbotapi.Message) {
}
*/


// Message: case1 - "Choose model to use. GPT3 is for text-based tasks, Codex for codegeneration.".
//
// update Dialog_Status = 2
Expand All @@ -81,15 +80,15 @@ func (c *Commander) HandleModelChoose(updateMessage *tgbotapi.Message) {
user := c.usersDb[chatID]
switch model_name {
case "wizard-uncensored-13b":
c.attachModel(model_name,chatID)
c.attachModel(model_name, chatID)
//c.ChangeDialogStatus(chatID,3)
user.AiSession.GptModel = model_name
c.RenderLanguage(chatID)

user.DialogStatus = 3
c.usersDb[chatID] = user
case "wizard-uncensored-30b":
c.attachModel(model_name,chatID)
c.attachModel(model_name, chatID)
//c.ChangeDialogStatus(chatID,3)
user.AiSession.GptModel = model_name
c.RenderLanguage(chatID)
Expand All @@ -102,8 +101,6 @@ func (c *Commander) HandleModelChoose(updateMessage *tgbotapi.Message) {

}



// Message: "Choose language. If you have different languages then listed, then just send 'Hello' at your desired language".
//
// update Dialog_Status = 3
Expand Down Expand Up @@ -133,11 +130,6 @@ func (c *Commander) ModelGPT3DOT5(updateMessage *tgbotapi.Message) {
c.usersDb[chatID] = user
}






/*
// ModelGPT and ModelLL codes are the same.
// TODO
Expand Down Expand Up @@ -168,51 +160,45 @@ func (c *Commander) ModelGPT4(updateMessage *tgbotapi.Message) {
*/

// render language menu
func (c *Commander) RenderLanguage(chat_id int64) {
// TODO: Write down user choise
//log.Printf("Model selected: %s\n", updateMessage.Text)

chatID := chat_id
//user := c.usersDb[chatID]
/*
func (c *Commander) RenderLanguage(chat_id int64) {
// TODO: Write down user choise
//log.Printf("Model selected: %s\n", updateMessage.Text)

chatID := chat_id
//user := c.usersDb[chatID]

/*
modelName := openai.GPT4 // gpt-4
user.AiSession.GptModel = modelName
msg := tgbotapi.NewMessage(user.ID, "your session model: "+modelName)
c.bot.Send(msg)
*/


msg := tgbotapi.NewMessage(chatID, "Choose a language or send 'Hello' in your desired language.")
msg.ReplyMarkup = tgbotapi.NewOneTimeReplyKeyboard(
tgbotapi.NewKeyboardButtonRow(
tgbotapi.NewKeyboardButton("English"),
tgbotapi.NewKeyboardButton("Russian")),
)
c.bot.Send(msg)

//user.DialogStatus = 3
//c.usersDb[chatID] = user
}
*/

msg := tgbotapi.NewMessage(chatID, "Choose a language or send 'Hello' in your desired language.")
msg.ReplyMarkup = tgbotapi.NewOneTimeReplyKeyboard(
tgbotapi.NewKeyboardButtonRow(
tgbotapi.NewKeyboardButton("English"),
tgbotapi.NewKeyboardButton("Russian")),
)
c.bot.Send(msg)

//user.DialogStatus = 3
//c.usersDb[chatID] = user
}

// low level attach model name to user profile
func (c *Commander) attachModel(model_name string, chatID int64) {
// TODO: Write down user choise
log.Printf("Model selected: %s\n", model_name)


user := c.usersDb[chatID]

modelName := model_name
user.AiSession.GptModel = modelName
msg := tgbotapi.NewMessage(user.ID, "your session model: "+modelName)
c.bot.Send(msg)
c.usersDb[chatID] = user
}
// TODO: Write down user choise
log.Printf("Model selected: %s\n", model_name)

user := c.usersDb[chatID]

modelName := model_name
user.AiSession.GptModel = modelName
msg := tgbotapi.NewMessage(user.ID, "your session model: "+modelName)
c.bot.Send(msg)
c.usersDb[chatID] = user
}

// internal for attach api key to a user
func (c *Commander) AttachKey(gpt_key string, chatID int64) {
Expand All @@ -222,7 +208,6 @@ func (c *Commander) AttachKey(gpt_key string, chatID int64) {
c.usersDb[chatID] = user
}


// Dangerouse! NOTE -- probably work only internal
func (c *Commander) ChangeDialogStatus(chatID int64, ds int8) {
user := c.usersDb[chatID]
Expand All @@ -232,24 +217,21 @@ func (c *Commander) ChangeDialogStatus(chatID int64, ds int8) {
user.DialogStatus = ds
}



func (c *Commander) RenderModelMenuOAI(chatID int64) {
msg := tgbotapi.NewMessage(chatID, msgTemplates["case1"])
msg.ReplyMarkup = tgbotapi.NewOneTimeReplyKeyboard(
tgbotapi.NewKeyboardButtonRow(
tgbotapi.NewKeyboardButton("GPT-3.5")),
tgbotapi.NewKeyboardButton("GPT-3.5")),
//tgbotapi.NewKeyboardButton("GPT-4"),
)
c.bot.Send(msg)
}


func (c *Commander) RenderModelMenuLAI(chatID int64) {
msg := tgbotapi.NewMessage(chatID, msgTemplates["case1"])
msg.ReplyMarkup = tgbotapi.NewOneTimeReplyKeyboard(
tgbotapi.NewKeyboardButtonRow(
tgbotapi.NewKeyboardButton("wizard-uncensored-13b")),
tgbotapi.NewKeyboardButton("wizard-uncensored-13b")),
// tgbotapi.NewKeyboardButton("wizard-uncensored-30b")),
)
c.bot.Send(msg)
Expand Down Expand Up @@ -278,8 +260,8 @@ func (c *Commander) ConnectingToAiWithLanguage(updateMessage *tgbotapi.Message,

msg := tgbotapi.NewMessage(user.ID, "connecting to local ai node")
c.bot.Send(msg)
go localai.SetupSequenceWithKey(c.bot, user, language, c.ctx, lpwd,ai_endpoint)

go localai.SetupSequenceWithKey(c.bot, user, language, c.ctx, lpwd, ai_endpoint)
}

// Generates an image with the /image command.
Expand All @@ -292,13 +274,13 @@ func (c *Commander) DialogSequence(updateMessage *tgbotapi.Message, ai_endpoint
//user := c.usersDb[chatID]
switch updateMessage.Command() {
/*
case "image":
msg := tgbotapi.NewMessage(user.ID, "Image link generation...")
c.bot.Send(msg)
case "image":
msg := tgbotapi.NewMessage(user.ID, "Image link generation...")
c.bot.Send(msg)

promt := updateMessage.CommandArguments()
log.Printf("Command /image arg: %s\n", promt)
go openaibot.StartImageSequence(c.bot, updateMessage, chatID, promt, c.ctx)
promt := updateMessage.CommandArguments()
log.Printf("Command /image arg: %s\n", promt)
go openaibot.StartImageSequence(c.bot, updateMessage, chatID, promt, c.ctx)
*/
default:
promt := updateMessage.Text
Expand All @@ -307,54 +289,52 @@ func (c *Commander) DialogSequence(updateMessage *tgbotapi.Message, ai_endpoint
}

// stable diffusion
func(c *Commander) GenerateNewImageLAI_SD(promt string, chatID int64, bot *tgbotapi.BotAPI){
func (c *Commander) GenerateNewImageLAI_SD(promt string, chatID int64, bot *tgbotapi.BotAPI) {
size := "256x256"
filepath,err := localai.GenerateImageStableDissusion(promt,size)
filepath, err := localai.GenerateImageStableDissusion(promt, size)
if err != nil {
//return nil, err
log.Println(err)
}
log.Println("url_path: ", filepath)
sendImage(bot,chatID,filepath)
sendImage(bot, chatID, filepath)
}

func sendImage(bot *tgbotapi.BotAPI, chatID int64, path string) {
// Prepare a photo message
local_path := transformURL(path)
log.Println("local path: ", local_path)
// Path to the image/file locally
// filePath := "/path/to/image.png" + local_path
fileName := transformURL(path)
log.Println("local file name: ", fileName)

telegraphLink := localai.UploadToTelegraph(fileName)
log.Println("uploaded to telegraph successfully, link is: ", telegraphLink)

// Path to the image/file locally
// filePath := "/path/to/image.png" + local_path
/*
// Creating a LocalFile object from the local path
photoBytes, err := ioutil.ReadFile(filePath)
if err != nil {
log.Println(err)
// Creating a LocalFile object from the local path
photoBytes, err := ioutil.ReadFile(filePath)
if err != nil {
log.Println(err)
}
photoFileBytes := tgbotapi.FileBytes{
Name: "picture",
Bytes: photoBytes,
}
photoFileBytes := tgbotapi.FileBytes{
Name: "picture",
Bytes: photoBytes,
}
*/


//message, err := bot.Send(tgbotapi.NewPhotoUpload(int64(chatID), photoFileBytes))

photo := tgbotapi.NewPhoto(chatID, tgbotapi.FilePath(local_path))
/* photo := tgbotapi.NewPhoto(chatID, tgbotapi.FilePath(local_path))
if _, err := bot.Send(photo); err != nil {
log.Fatalln(err)
}

}

func transformURL(inputURL string) string {
// Replace "http://localhost:8080" with "/tmp" using strings.Replace
transformedURL := strings.Replace(inputURL, "http://localhost:8080/", "/tmp/", 1)
transformedURL = strings.Replace(transformedURL,"/generated-images/","/generated/images/",1)

return transformedURL
}


} */
msg := tgbotapi.NewMessage(chatID, telegraphLink)
bot.Send(msg)
}

func transformURL(inputURL string) string {
// Replace "http://localhost:8080" with "/tmp" using strings.Replace
parsedURL, _ := url.Parse(inputURL)

// Use path.Base to get the filename from the URL path
fileName := path.Base(parsedURL.Path)
return fileName
}
Loading