|
| 1 | +package opengpts |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "math/rand" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + |
| 10 | + http "github.com/bogdanfinn/fhttp" |
| 11 | + |
| 12 | + "github.com/aandrew-me/tgpt/v2/client" |
| 13 | + "github.com/aandrew-me/tgpt/v2/structs" |
| 14 | + "github.com/aandrew-me/tgpt/v2/utils" |
| 15 | +) |
| 16 | + |
| 17 | +type Message struct { |
| 18 | + Content string `json:"content"` |
| 19 | +} |
| 20 | + |
| 21 | +func RandomString(length int) string { |
| 22 | + characters := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-" |
| 23 | + result := make([]byte, length) |
| 24 | + |
| 25 | + for i := 0; i < length; i++ { |
| 26 | + result[i] = characters[rand.Intn(len(characters))] |
| 27 | + } |
| 28 | + return string(result) |
| 29 | +} |
| 30 | + |
| 31 | +func NewRequest(input string, params structs.Params, extraOptions structs.ExtraOptions) (*http.Response, error) { |
| 32 | + client, err := client.NewClient() |
| 33 | + if err != nil { |
| 34 | + fmt.Println(err) |
| 35 | + os.Exit(0) |
| 36 | + } |
| 37 | + safeInput, _ := json.Marshal(input) |
| 38 | + |
| 39 | + randID := utils.RandomString(36) |
| 40 | + |
| 41 | + if len(extraOptions.ThreadID) > 1 { |
| 42 | + randID = extraOptions.ThreadID; |
| 43 | + } |
| 44 | + |
| 45 | + var data = strings.NewReader(fmt.Sprintf(`{ |
| 46 | + "input": [ |
| 47 | + { |
| 48 | + "content": %v, |
| 49 | + "additional_kwargs": {}, |
| 50 | + "type": "human", |
| 51 | + "example": false |
| 52 | + } |
| 53 | + ], |
| 54 | + "assistant_id": "daede84b-79b7-4166-a277-7ac162c74c11", |
| 55 | + "thread_id": "" |
| 56 | +} |
| 57 | + `, string(safeInput))) |
| 58 | + |
| 59 | + req, err := http.NewRequest("POST", "https://opengpts-example-vz4y4ooboq-uc.a.run.app/runs/stream", data) |
| 60 | + if err != nil { |
| 61 | + fmt.Println("\nSome error has occurred.") |
| 62 | + fmt.Println("Error:", err) |
| 63 | + os.Exit(0) |
| 64 | + } |
| 65 | + // Setting all the required headers |
| 66 | + req.Header.Add("authority", "opengpts-example-vz4y4ooboq-uc.a.run.app") |
| 67 | + req.Header.Add("accept", "text/event-stream") |
| 68 | + req.Header.Add("accept-language", "en-US,en;q=0.7") |
| 69 | + req.Header.Add("cache-control", "no-cache") |
| 70 | + req.Header.Add("content-type", "application/json") |
| 71 | + req.Header.Add("cookie", "opengpts_user_id="+randID) |
| 72 | + req.Header.Add("origin", "https://opengpts-example-vz4y4ooboq-uc.a.run.app") |
| 73 | + req.Header.Add("pragma", "no-cache") |
| 74 | + req.Header.Add("referer", "https://opengpts-example-vz4y4ooboq-uc.a.run.app/") |
| 75 | + req.Header.Add("sec-fetch-site", "same-origin") |
| 76 | + req.Header.Add("sec-gpc", "1") |
| 77 | + 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") |
| 78 | + |
| 79 | + // Return response |
| 80 | + return (client.Do(req)) |
| 81 | +} |
| 82 | + |
| 83 | +func GetMainText(line string, input string) string { |
| 84 | + var Messages []Message |
| 85 | + var obj = "{}" |
| 86 | + if len(line) > 1 && strings.Contains(line, "data:") { |
| 87 | + obj = strings.Split(line, "data: ")[1] |
| 88 | + } |
| 89 | + |
| 90 | + // var d Response |
| 91 | + if err := json.Unmarshal([]byte(obj), &Messages); err != nil { |
| 92 | + return "" |
| 93 | + } |
| 94 | + |
| 95 | + if len(Messages) > 1 { |
| 96 | + mainText := Messages[len(Messages)-1].Content |
| 97 | + |
| 98 | + if mainText == input { |
| 99 | + return "" |
| 100 | + } |
| 101 | + |
| 102 | + return mainText |
| 103 | + } |
| 104 | + return "" |
| 105 | +} |
0 commit comments