ai work started
This commit is contained in:
1
.env.ai
Normal file
1
.env.ai
Normal file
@@ -0,0 +1 @@
|
||||
PAWAN_KEY=pk-SiLlpaMcxHaTomcSbJkwEUQnxoKBWSVPQAcrKsgIwIlVvdoy
|
||||
@@ -19,7 +19,7 @@ services:
|
||||
- ./scripts/postgres:/docker-entrypoint-initdb.d
|
||||
- postgres_data:/var/lib/postgresql/data
|
||||
mongo:
|
||||
image: mongo:8.0.14-noble
|
||||
image: mongo:noble
|
||||
networks:
|
||||
- bot
|
||||
restart: always
|
||||
|
||||
@@ -6,6 +6,7 @@ services:
|
||||
env_file:
|
||||
- .env
|
||||
- .env.production
|
||||
- .env.ai
|
||||
build:
|
||||
context: ./
|
||||
dockerfile: ./Dockerfile
|
||||
|
||||
3
main.go
3
main.go
@@ -10,7 +10,7 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
err := godotenv.Load(".env", ".env.production")
|
||||
err := godotenv.Load(".env", ".env.production", ".env.ai")
|
||||
if err != nil {
|
||||
log.Println("Error loading .env file")
|
||||
}
|
||||
@@ -38,6 +38,7 @@ func main() {
|
||||
plugins.RegisterEconomy(bot)
|
||||
plugins.RegisterWaifus(bot)
|
||||
plugins.RegisterAdmin(bot)
|
||||
plugins.RegisterTestRP(bot)
|
||||
|
||||
//ctx := context.Background()
|
||||
//go func() {
|
||||
|
||||
34
plugins/testrp.go
Normal file
34
plugins/testrp.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"kurumibot/laniakea"
|
||||
"kurumibot/utils/ai"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func RegisterTestRP(bot *laniakea.Bot) {
|
||||
rp := laniakea.NewPlugin("RP")
|
||||
rp = rp.Command(generate, "g", "gen")
|
||||
|
||||
bot.AddPlugins(rp.Build())
|
||||
}
|
||||
|
||||
func generate(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
||||
api := ai.NewOpenAIAPI(ai.CosmoRPUrl, os.Getenv("PAWAN_KEY"), "cosmorp-2.5")
|
||||
res, err := api.CreateCompletion(ai.CreateCompletionReq{
|
||||
Model: "cosmorp-2.5",
|
||||
Messages: []ai.Message{
|
||||
{
|
||||
Role: "developer",
|
||||
Content: "123",
|
||||
},
|
||||
{
|
||||
Role: "user",
|
||||
Content: strings.Join(ctx.Args, " "),
|
||||
},
|
||||
},
|
||||
})
|
||||
log.Println(res, err)
|
||||
}
|
||||
99
utils/ai/openai.go
Normal file
99
utils/ai/openai.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package ai
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type OpenAIResponse struct {
|
||||
ID string `json:"id"`
|
||||
Object string `json:"object"`
|
||||
Created int64 `json:"created"`
|
||||
Model string `json:"model"`
|
||||
Choices []Choice `json:"choices"`
|
||||
Usage Usage `json:"usage"`
|
||||
ServiceTier string `json:"service_tier"`
|
||||
}
|
||||
|
||||
type Choice struct {
|
||||
Index int64 `json:"index"`
|
||||
Message Message `json:"message"`
|
||||
Logprobs interface{} `json:"logprobs"`
|
||||
FinishReason string `json:"finish_reason"`
|
||||
}
|
||||
|
||||
type Message struct {
|
||||
Role string `json:"role"`
|
||||
Content string `json:"content"`
|
||||
Refusal interface{} `json:"refusal"`
|
||||
Annotations []interface{} `json:"annotations"`
|
||||
}
|
||||
|
||||
type Usage struct {
|
||||
PromptTokens int64 `json:"prompt_tokens"`
|
||||
CompletionTokens int64 `json:"completion_tokens"`
|
||||
TotalTokens int64 `json:"total_tokens"`
|
||||
PromptTokensDetails PromptTokensDetails `json:"prompt_tokens_details"`
|
||||
CompletionTokensDetails CompletionTokensDetails `json:"completion_tokens_details"`
|
||||
}
|
||||
|
||||
type CompletionTokensDetails struct {
|
||||
ReasoningTokens int64 `json:"reasoning_tokens"`
|
||||
AudioTokens int64 `json:"audio_tokens"`
|
||||
AcceptedPredictionTokens int64 `json:"accepted_prediction_tokens"`
|
||||
RejectedPredictionTokens int64 `json:"rejected_prediction_tokens"`
|
||||
}
|
||||
|
||||
type PromptTokensDetails struct {
|
||||
CachedTokens int64 `json:"cached_tokens"`
|
||||
AudioTokens int64 `json:"audio_tokens"`
|
||||
}
|
||||
|
||||
type OpenAIAPI struct {
|
||||
Token string
|
||||
Model string
|
||||
BaseURL string
|
||||
}
|
||||
|
||||
func NewOpenAIAPI(baseURL, token, model string) *OpenAIAPI {
|
||||
return &OpenAIAPI{
|
||||
Token: token,
|
||||
Model: model,
|
||||
BaseURL: baseURL,
|
||||
}
|
||||
}
|
||||
|
||||
type CreateCompletionReq struct {
|
||||
Model string `json:"model"`
|
||||
Messages []Message `json:"messages"`
|
||||
}
|
||||
|
||||
func (o *OpenAIAPI) CreateCompletion(request CreateCompletionReq) (*OpenAIResponse, error) {
|
||||
url := fmt.Sprintf("%s/v1/chat/completions", o.BaseURL)
|
||||
data, err := json.Marshal(request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
buf := bytes.NewBuffer(data)
|
||||
req, err := http.NewRequest("POST", url, buf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", o.Token))
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
response := new(OpenAIResponse)
|
||||
err = json.Unmarshal(body, response)
|
||||
return response, err
|
||||
}
|
||||
7
utils/ai/pawan.go
Normal file
7
utils/ai/pawan.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package ai
|
||||
|
||||
import "fmt"
|
||||
|
||||
const BaseURL = "https://api.pawan.krd"
|
||||
|
||||
var CosmoRPUrl = fmt.Sprintf("%s/cosmosrp-2.5", BaseURL)
|
||||
Reference in New Issue
Block a user