added ai client for current version ai api
This commit is contained in:
parent
be718cfde3
commit
3d294ea1d6
@ -153,6 +153,7 @@ func New(ctx context.Context, opts interface{}, ver appInit.Version) (appInit.Co
|
||||
WorkerRespondentCh: workerRespondentCh,
|
||||
Encrypt: encrypt,
|
||||
RedirectURl: options.RedirectURL,
|
||||
AiClient: clients.NewAiClient("http://35.206.94.7:80/api/engine/generate_question_by_answer/"),
|
||||
})
|
||||
|
||||
saveRespWcData := savewc.DepsForResp{
|
||||
|
@ -1,6 +1,8 @@
|
||||
package clients
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
@ -24,13 +26,44 @@ func NewAiClient(baseURL string) *AIClient {
|
||||
}
|
||||
}
|
||||
|
||||
func (client *AIClient) SendAnswerer(final bool, tipe, message, session string) error {
|
||||
req := SendAnswerRequest{
|
||||
Tipe: tipe,
|
||||
Message: message,
|
||||
Final: final,
|
||||
Session: session,
|
||||
func (client *AIClient) SendAnswerer(final bool, tipe, message, session string) (string, error) {
|
||||
//req := SendAnswerRequest{
|
||||
// Tipe: tipe,
|
||||
// Message: message,
|
||||
// Final: final,
|
||||
// Session: session,
|
||||
//}
|
||||
|
||||
clownRequest := struct {
|
||||
Text string `json:"text"`
|
||||
}{
|
||||
Text: message,
|
||||
}
|
||||
fmt.Println(req)
|
||||
return nil
|
||||
|
||||
body, err := json.Marshal(clownRequest)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
agent := client.httpClient.Post(client.baseURL)
|
||||
agent.Set("Content-Type", "application/json").Body(body)
|
||||
|
||||
statusCode, respBody, errs := agent.Bytes()
|
||||
if len(errs) > 0 {
|
||||
return "", errors.Join(errs...)
|
||||
}
|
||||
|
||||
if statusCode != fiber.StatusCreated {
|
||||
return "", fmt.Errorf("invalid response status code: %d", statusCode)
|
||||
}
|
||||
|
||||
resp := struct {
|
||||
Text string `json:"text"`
|
||||
}{}
|
||||
|
||||
if err := json.Unmarshal(respBody, &resp); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return resp.Text, nil
|
||||
}
|
||||
|
17
clients/aiClient_test.go
Normal file
17
clients/aiClient_test.go
Normal file
@ -0,0 +1,17 @@
|
||||
package clients
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_AiClient(t *testing.T) {
|
||||
aiClient := NewAiClient("http://35.206.94.7:80/api/engine/generate_question_by_answer/")
|
||||
for i := 0; i < 10; i++ {
|
||||
question, err := aiClient.SendAnswerer(false, "type", "Дартвейдер был хорошим.", "1234")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
fmt.Println(question)
|
||||
}
|
||||
}
|
0
go.sum
Normal file
0
go.sum
Normal file
@ -384,12 +384,19 @@ func (s *Service) PutAnswersOnePiece(c *fiber.Ctx) error {
|
||||
return c.Status(fiber.StatusNotFound).SendString("no questions found")
|
||||
}
|
||||
|
||||
err = s.aiClient.SendAnswerer(final, question[0].Type, ans.Content, cs)
|
||||
questionText, err := s.aiClient.SendAnswerer(final, question[0].Type, ans.Content, cs)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).SendString(fmt.Sprintf("can not send answer to ai, err: %s", err.Error()))
|
||||
}
|
||||
|
||||
// todo тут надо создать вопрос по ответу от ai
|
||||
_, err = s.dal.QuestionRepo.CreateQuestion(c.Context(), &model.Question{
|
||||
QuizId: quiz.Id,
|
||||
Title: questionText,
|
||||
Session: cs,
|
||||
})
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).SendString(fmt.Sprintf("can not create question type ai, err: %s", err.Error()))
|
||||
}
|
||||
}
|
||||
|
||||
ans.DeviceType = deviceType
|
||||
|
Loading…
Reference in New Issue
Block a user