Skip to content

Commit

Permalink
Added new provider opengpts
Browse files Browse the repository at this point in the history
  • Loading branch information
aandrew-me committed Jan 6, 2024
1 parent 8640731 commit f330a45
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 6 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

tgpt is a cross-platform command-line interface (CLI) tool that allows you to use AI chatbot in your Terminal without requiring API keys.

**Currently available providers**: Brave Leo (llama-2-13b-chat), FakeOpen (GPT-3.5, GPT-4) and OpenAI (Requires API Key)
**Currently available providers**: Brave Leo (llama-2-13b-chat), FakeOpen (GPT-3.5, GPT-4), OpenGPTs (GPT-3.5) and OpenAI (Requires API Key)

**Image Generation Model**: Craiyon V3

Expand Down Expand Up @@ -37,7 +37,7 @@ Options:
Providers:
The default provider is Brave Leo which uses 'llama-2-13b-chat' model.
Available providers to use: leo, fakeopen, openai
Available providers to use: leo, fakeopen, openai, opengpts
Provider: leo
Supports personal API Key and custom models
Expand All @@ -48,6 +48,9 @@ No support for API Key, but supports models
Provider: openai
Needs API key to work and supports various models
Provider: opengpts
Uses gpt-3.5-turbo only. Do not use with sensitive data
Examples:
tgpt "What is internet?"
tgpt -m
Expand Down
11 changes: 7 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/olekukonko/ts"
)

const localVersion = "2.3.3"
const localVersion = "2.4.0"

var bold = color.New(color.Bold)
var boldBlue = color.New(color.Bold, color.FgBlue)
Expand Down Expand Up @@ -344,17 +344,20 @@ func showHelpMessage() {

boldBlue.Println("\nProviders:")
fmt.Println("The default provider is Brave Leo which uses 'llama-2-13b-chat' model.")
fmt.Println("Available providers to use: leo, fakeopen, openai")
fmt.Println("Available providers to use: leo, fakeopen, openai, opengpts")

bold.Println("\nProvider: leo")
fmt.Println("Supports personal API Key and custom models")
fmt.Println("Supports personal API Key and custom models.")

bold.Println("\nProvider: fakeopen")
fmt.Println("No support for API Key, but supports models")
fmt.Println("No support for API Key, but supports models. Default model is gpt-3.5-turbo. Supports gpt-4")

bold.Println("\nProvider: openai")
fmt.Println("Needs API key to work and supports various models")

bold.Println("\nProvider: opengpts")
fmt.Println("Uses gpt-3.5-turbo only. Do not use with sensitive data")

boldBlue.Println("\nExamples:")
fmt.Println(`tgpt "What is internet?"`)
fmt.Println(`tgpt -m`)
Expand Down
101 changes: 101 additions & 0 deletions providers/opengpts/opengpts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package opengpts

import (
"encoding/json"
"fmt"
"math/rand"
"os"
"strings"

http "github.com/bogdanfinn/fhttp"

"github.com/aandrew-me/tgpt/v2/client"
"github.com/aandrew-me/tgpt/v2/structs"
)

type Message struct {
Content string `json:"content"`
}

type Response struct {
Messages []Message `json:"messages"`
}

func RandomString(length int) string {
characters := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-"
result := make([]byte, length)

for i := 0; i < length; i++ {
result[i] = characters[rand.Intn(len(characters))]
}
return string(result)
}

func NewRequest(input string, params structs.Params, prevMessages string) (*http.Response, error) {
client, err := client.NewClient()
if err != nil {
fmt.Println(err)
os.Exit(0)
}
safeInput, _ := json.Marshal(input)

uuid := RandomString(36)

var data = strings.NewReader(fmt.Sprintf(`{
"input": {
"messages": [
%v
{
"content": %v,
"additional_kwargs": {},
"type": "human",
"example": false
}
]
},
"assistant_id": "d50a5d6c-2598-437b-940e-e6918d19810c",
"thread_id": ""
}
`, prevMessages, string(safeInput)))

req, err := http.NewRequest("POST", "https://opengpts-example-vz4y4ooboq-uc.a.run.app/runs/stream", data)
if err != nil {
fmt.Println("\nSome error has occurred.")
fmt.Println("Error:", err)
os.Exit(0)
}
// Setting all the required headers
req.Header.Add("authority", "opengpts-example-vz4y4ooboq-uc.a.run.app")
req.Header.Add("accept", "text/event-stream")
req.Header.Add("accept-language", "en-US,en;q=0.7")
req.Header.Add("cache-control", "no-cache")
req.Header.Add("content-type", "application/json")
req.Header.Add("cookie", "opengpts_user_id="+uuid)
req.Header.Add("origin", "https://opengpts-example-vz4y4ooboq-uc.a.run.app")
req.Header.Add("pragma", "no-cache")
req.Header.Add("referer", "https://opengpts-example-vz4y4ooboq-uc.a.run.app/")
req.Header.Add("sec-fetch-site", "same-origin")
req.Header.Add("sec-gpc", "1")
req.Header.Add("user-agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")

// Return response
return (client.Do(req))
}

func GetMainText(line string) (mainText string) {
var obj = "{}"
if len(line) > 1 && strings.Contains(line, "data:") {
obj = strings.Split(line, "data: ")[1]
}

var d Response
if err := json.Unmarshal([]byte(obj), &d); err != nil {
return ""
}

if d.Messages != nil {
mainText = d.Messages[len(d.Messages)-1].Content
return mainText
}
return ""
}
5 changes: 5 additions & 0 deletions providers/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/aandrew-me/tgpt/v2/providers/fakeopen"
"github.com/aandrew-me/tgpt/v2/providers/leo"
"github.com/aandrew-me/tgpt/v2/providers/openai"
"github.com/aandrew-me/tgpt/v2/providers/opengpts"
"github.com/aandrew-me/tgpt/v2/structs"
http "github.com/bogdanfinn/fhttp"
)
Expand All @@ -13,6 +14,8 @@ func GetMainText(line string, provider string) string {
return fakeopen.GetMainText(line)
} else if provider == "openai" {
return openai.GetMainText(line)
} else if provider == "opengpts" {
return opengpts.GetMainText(line)
}

return leo.GetMainText(line)
Expand All @@ -23,6 +26,8 @@ func NewRequest(input string, params structs.Params, prevMessages string) (*http
return fakeopen.NewRequest(input, params, prevMessages)
} else if params.Provider == "openai" {
return openai.NewRequest(input, params, prevMessages)
} else if params.Provider == "opengpts" {
return opengpts.NewRequest(input, params, prevMessages)
}

return leo.NewRequest(input, params)
Expand Down

0 comments on commit f330a45

Please sign in to comment.