Skip to content
Draft
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
74 changes: 50 additions & 24 deletions src/providers/pollinations/pollinations.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"os"
"strconv"
"strings"

http "github.com/bogdanfinn/fhttp"
Expand All @@ -24,37 +25,62 @@ func NewRequest(input string, params structs.Params) (*http.Response, error) {
model = params.ApiModel
}

temperature := "0.6"
temperature := 0.6
if params.Temperature != "" {
temperature = params.Temperature
if parsedTemp, err := strconv.ParseFloat(params.Temperature, 64); err == nil {
temperature = parsedTemp
}
}

top_p := "1"
top_p := 1.0
if params.Top_p != "" {
top_p = params.Top_p
if parsedTopP, err := strconv.ParseFloat(params.Top_p, 64); err == nil {
top_p = parsedTopP
}
}
Comment on lines +35 to 40
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

According to Go's naming conventions, variables should be named using camelCase. The variable top_p should be renamed to topP for consistency with the pollinationsRequest struct field TopP and idiomatic Go style. After applying this change, please also update the usage of this variable on line 72 to TopP: topP.

Suggested change
top_p := 1.0
if params.Top_p != "" {
top_p = params.Top_p
if parsedTopP, err := strconv.ParseFloat(params.Top_p, 64); err == nil {
top_p = parsedTopP
}
}
topP := 1.0
if params.Top_p != "" {
if parsedTopP, err := strconv.ParseFloat(params.Top_p, 64); err == nil {
topP = parsedTopP
}
}


safeInput, _ := json.Marshal(input)

var data = strings.NewReader(fmt.Sprintf(`{
"messages": [
{
"content": "%s",
"role": "system"
},
%v
{
"content": %v,
"role": "user"
}
],
"model": "%v",
"stream": true,
"temperature": %v,
"top_p": %v,
"referrer": "tgpt"
type pollinationsMessage struct {
Content string `json:"content"`
Role string `json:"role"`
}
`, params.SystemPrompt, params.PrevMessages, string(safeInput), model, temperature, top_p))

type pollinationsRequest struct {
Messages []pollinationsMessage `json:"messages"`
Model string `json:"model"`
Stream bool `json:"stream"`
Temperature float64 `json:"temperature"`
TopP float64 `json:"top_p"`
Referrer string `json:"referrer"`
}

messages := []pollinationsMessage{
{
Content: params.SystemPrompt,
Role: "system",
},
{
Content: input,
Role: "user",
},
}

reqBody := pollinationsRequest{
Messages: messages,
Model: model,
Stream: true,
Temperature: temperature,
TopP: top_p,
Referrer: "tgpt",
}

bodyBytes, err := json.Marshal(reqBody)
if err != nil {
fmt.Println("\nFailed to marshal request body.")
fmt.Println("Error:", err)
os.Exit(0)
}
Comment on lines +77 to +81
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This function is declared to return an error (... (*http.Response, error)), but this error handling block calls os.Exit(0) on failure. This terminates the entire application with a success code, which is incorrect. The function should instead return the error to its caller, allowing for more graceful error handling.

if err != nil {
		return nil, fmt.Errorf("failed to marshal request body: %w", err)
	}


var data = strings.NewReader(string(bodyBytes))

req, err := http.NewRequest("POST", "https://text.pollinations.ai/openai", data)
if err != nil {
Expand Down