update
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -3,3 +3,4 @@ __debug_bin
|
||||
.env
|
||||
.idea
|
||||
output
|
||||
docs
|
||||
|
||||
4
Makefile
Normal file
4
Makefile
Normal file
@@ -0,0 +1,4 @@
|
||||
start:
|
||||
docker run --name postgres-db -e POSTGRES_PASSWORD=docker -p 5432:5432 -d postgres
|
||||
stop:
|
||||
docker rm postgres-db -f
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
|
||||
"github.com/caarlos0/env/v6"
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/sashabaranov/go-openai"
|
||||
|
||||
"gitlab.com/DeveloperDurp/DurpAPI/model"
|
||||
"gitlab.com/DeveloperDurp/DurpAPI/storage"
|
||||
@@ -38,8 +37,6 @@ func NewController() *Controller {
|
||||
log.Fatalf("unable to parse database variables: %e", err)
|
||||
}
|
||||
|
||||
controller.Cfg.OpenaiClient = *openai.NewClient(controller.Cfg.OpenaiApiKey)
|
||||
|
||||
Db, err := storage.Connect(controller.Dbcfg)
|
||||
if err != nil {
|
||||
panic("Failed to connect to database")
|
||||
|
||||
@@ -1,18 +1,24 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
openai "github.com/sashabaranov/go-openai"
|
||||
)
|
||||
|
||||
type ChatRequest struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
// Response struct to unmarshal the JSON response
|
||||
type Response struct {
|
||||
Response string `json:"response"`
|
||||
}
|
||||
|
||||
// GeneralOpenAI godoc
|
||||
//
|
||||
// @Summary Gerneral ChatGPT
|
||||
@@ -33,7 +39,7 @@ func (c *Controller) GeneralOpenAI(ctx *gin.Context) {
|
||||
req.Message = ctx.Query("message")
|
||||
}
|
||||
|
||||
result, err := c.createChatCompletion(req.Message)
|
||||
result, err := c.createChatCompletion(req.Message, "openchat")
|
||||
if err != nil {
|
||||
err := ctx.AbortWithError(http.StatusInternalServerError, err)
|
||||
if err != nil {
|
||||
@@ -63,7 +69,7 @@ func (c *Controller) TravelAgentOpenAI(ctx *gin.Context) {
|
||||
|
||||
req.Message = "I want you to act as a travel guide. I will give you my location and you will give me suggestions. " + req.Message
|
||||
|
||||
result, err := c.createChatCompletion(req.Message)
|
||||
result, err := c.createChatCompletion(req.Message, "openchat")
|
||||
if err != nil {
|
||||
err := ctx.AbortWithError(http.StatusInternalServerError, err)
|
||||
if err != nil {
|
||||
@@ -74,23 +80,43 @@ func (c *Controller) TravelAgentOpenAI(ctx *gin.Context) {
|
||||
ctx.JSON(http.StatusOK, gin.H{"message": result})
|
||||
}
|
||||
|
||||
func (c *Controller) createChatCompletion(message string) (string, error) {
|
||||
client := c.Cfg.OpenaiClient
|
||||
resp, err := client.CreateChatCompletion(
|
||||
context.Background(),
|
||||
openai.ChatCompletionRequest{
|
||||
Model: openai.GPT3Dot5Turbo,
|
||||
Messages: []openai.ChatCompletionMessage{
|
||||
{
|
||||
Role: openai.ChatMessageRoleUser,
|
||||
Content: message,
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return "", err
|
||||
func (c *Controller) createChatCompletion(message string, model string) (string, error) {
|
||||
// Define the request body
|
||||
requestBody := map[string]interface{}{
|
||||
"model": model,
|
||||
"prompt": message,
|
||||
"stream": false,
|
||||
}
|
||||
|
||||
return resp.Choices[0].Message.Content, nil
|
||||
// Convert the request body to JSON
|
||||
requestBodyBytes, err := json.Marshal(requestBody)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error encoding request body: %v", err)
|
||||
}
|
||||
|
||||
// Send a POST request to the specified URL with the request body
|
||||
response, err := http.Post(
|
||||
"http://"+c.Cfg.LlamaURL+"/api/generate",
|
||||
"application/json",
|
||||
bytes.NewBuffer(requestBodyBytes),
|
||||
)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error sending POST request: %v", err)
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
// Read the response body
|
||||
responseBody, err := ioutil.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error reading response body: %v", err)
|
||||
}
|
||||
|
||||
// Unmarshal the JSON response
|
||||
var resp Response
|
||||
if err := json.Unmarshal(responseBody, &resp); err != nil {
|
||||
return "", fmt.Errorf("error decoding response body: %v", err)
|
||||
}
|
||||
|
||||
// Return the response
|
||||
return resp.Response, nil
|
||||
}
|
||||
|
||||
@@ -1,88 +0,0 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/cookiejar"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// UnraidPowerUsage godoc
|
||||
//
|
||||
// @Summary Unraid PSU Stats
|
||||
// @Description Gets the PSU Data from unraid
|
||||
// @Tags unraid
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} model.PowerSupply "response"
|
||||
// @failure 412 {object} model.Message "error"
|
||||
// @Router /unraid/powerusage [get]
|
||||
func (c *Controller) UnraidPowerUsage(ctx *gin.Context) {
|
||||
jar, err := cookiejar.New(nil)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
client := &http.Client{
|
||||
Jar: jar,
|
||||
}
|
||||
|
||||
form := url.Values{
|
||||
"username": {"root"},
|
||||
"password": {c.Cfg.UnraidAPIKey},
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(
|
||||
"POST",
|
||||
"https://"+c.Cfg.UnraidURI+"/login",
|
||||
strings.NewReader(form.Encode()),
|
||||
)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
fmt.Println("Login failed!")
|
||||
return
|
||||
}
|
||||
|
||||
req, err = http.NewRequest(
|
||||
"GET",
|
||||
"https://"+c.Cfg.UnraidURI+"/plugins/corsairpsu/status.php",
|
||||
nil,
|
||||
)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
resp, err = client.Do(req)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
var responseJSON map[string]interface{}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&responseJSON); err != nil {
|
||||
fmt.Println(err)
|
||||
ctx.JSON(http.StatusPreconditionFailed, gin.H{"message": "Bad Response from Unraid"})
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, responseJSON)
|
||||
}
|
||||
4
main.go
4
main.go
@@ -51,10 +51,6 @@ func main() {
|
||||
openai.GET("general", c.GeneralOpenAI)
|
||||
openai.GET("travelagent", c.TravelAgentOpenAI)
|
||||
}
|
||||
unraid := v1.Group("/unraid")
|
||||
{
|
||||
unraid.GET("powerusage", c.UnraidPowerUsage)
|
||||
}
|
||||
}
|
||||
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
||||
|
||||
|
||||
@@ -1,19 +1,15 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"github.com/sashabaranov/go-openai"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
OpenaiClient openai.Client
|
||||
OpenaiApiKey string `env:"openai_api_key"`
|
||||
UnraidAPIKey string `env:"unraid_api_key"`
|
||||
UnraidURI string `env:"unraid_uri"`
|
||||
Host string `env:"host"`
|
||||
Version string `env:"version"`
|
||||
Groupsenv string `env:"groupsenv"`
|
||||
JwksURL string `env:"jwksurl"`
|
||||
Host string `env:"host"`
|
||||
Version string `env:"version"`
|
||||
Groupsenv string `env:"groupsenv"`
|
||||
JwksURL string `env:"jwksurl"`
|
||||
LlamaURL string `env:"llamaurl"`
|
||||
}
|
||||
|
||||
type DBConfig struct {
|
||||
|
||||
Reference in New Issue
Block a user