update
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -3,3 +3,4 @@ __debug_bin
|
|||||||
.env
|
.env
|
||||||
.idea
|
.idea
|
||||||
output
|
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/caarlos0/env/v6"
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
"github.com/sashabaranov/go-openai"
|
|
||||||
|
|
||||||
"gitlab.com/DeveloperDurp/DurpAPI/model"
|
"gitlab.com/DeveloperDurp/DurpAPI/model"
|
||||||
"gitlab.com/DeveloperDurp/DurpAPI/storage"
|
"gitlab.com/DeveloperDurp/DurpAPI/storage"
|
||||||
@@ -38,8 +37,6 @@ func NewController() *Controller {
|
|||||||
log.Fatalf("unable to parse database variables: %e", err)
|
log.Fatalf("unable to parse database variables: %e", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
controller.Cfg.OpenaiClient = *openai.NewClient(controller.Cfg.OpenaiApiKey)
|
|
||||||
|
|
||||||
Db, err := storage.Connect(controller.Dbcfg)
|
Db, err := storage.Connect(controller.Dbcfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("Failed to connect to database")
|
panic("Failed to connect to database")
|
||||||
|
|||||||
@@ -1,18 +1,24 @@
|
|||||||
package controller
|
package controller
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
openai "github.com/sashabaranov/go-openai"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type ChatRequest struct {
|
type ChatRequest struct {
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Response struct to unmarshal the JSON response
|
||||||
|
type Response struct {
|
||||||
|
Response string `json:"response"`
|
||||||
|
}
|
||||||
|
|
||||||
// GeneralOpenAI godoc
|
// GeneralOpenAI godoc
|
||||||
//
|
//
|
||||||
// @Summary Gerneral ChatGPT
|
// @Summary Gerneral ChatGPT
|
||||||
@@ -33,7 +39,7 @@ func (c *Controller) GeneralOpenAI(ctx *gin.Context) {
|
|||||||
req.Message = ctx.Query("message")
|
req.Message = ctx.Query("message")
|
||||||
}
|
}
|
||||||
|
|
||||||
result, err := c.createChatCompletion(req.Message)
|
result, err := c.createChatCompletion(req.Message, "openchat")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err := ctx.AbortWithError(http.StatusInternalServerError, err)
|
err := ctx.AbortWithError(http.StatusInternalServerError, err)
|
||||||
if err != nil {
|
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
|
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 {
|
if err != nil {
|
||||||
err := ctx.AbortWithError(http.StatusInternalServerError, err)
|
err := ctx.AbortWithError(http.StatusInternalServerError, err)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -74,23 +80,43 @@ func (c *Controller) TravelAgentOpenAI(ctx *gin.Context) {
|
|||||||
ctx.JSON(http.StatusOK, gin.H{"message": result})
|
ctx.JSON(http.StatusOK, gin.H{"message": result})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Controller) createChatCompletion(message string) (string, error) {
|
func (c *Controller) createChatCompletion(message string, model string) (string, error) {
|
||||||
client := c.Cfg.OpenaiClient
|
// Define the request body
|
||||||
resp, err := client.CreateChatCompletion(
|
requestBody := map[string]interface{}{
|
||||||
context.Background(),
|
"model": model,
|
||||||
openai.ChatCompletionRequest{
|
"prompt": message,
|
||||||
Model: openai.GPT3Dot5Turbo,
|
"stream": false,
|
||||||
Messages: []openai.ChatCompletionMessage{
|
|
||||||
{
|
|
||||||
Role: openai.ChatMessageRoleUser,
|
|
||||||
Content: message,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
6
main.go
6
main.go
@@ -51,10 +51,6 @@ func main() {
|
|||||||
openai.GET("general", c.GeneralOpenAI)
|
openai.GET("general", c.GeneralOpenAI)
|
||||||
openai.GET("travelagent", c.TravelAgentOpenAI)
|
openai.GET("travelagent", c.TravelAgentOpenAI)
|
||||||
}
|
}
|
||||||
unraid := v1.Group("/unraid")
|
|
||||||
{
|
|
||||||
unraid.GET("powerusage", c.UnraidPowerUsage)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
||||||
|
|
||||||
@@ -62,4 +58,4 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Failed to start server")
|
fmt.Println("Failed to start server")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,19 +1,15 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/sashabaranov/go-openai"
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
OpenaiClient openai.Client
|
Host string `env:"host"`
|
||||||
OpenaiApiKey string `env:"openai_api_key"`
|
Version string `env:"version"`
|
||||||
UnraidAPIKey string `env:"unraid_api_key"`
|
Groupsenv string `env:"groupsenv"`
|
||||||
UnraidURI string `env:"unraid_uri"`
|
JwksURL string `env:"jwksurl"`
|
||||||
Host string `env:"host"`
|
LlamaURL string `env:"llamaurl"`
|
||||||
Version string `env:"version"`
|
|
||||||
Groupsenv string `env:"groupsenv"`
|
|
||||||
JwksURL string `env:"jwksurl"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type DBConfig struct {
|
type DBConfig struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user