feat 临时
This commit is contained in:
@@ -2,7 +2,11 @@ package backend
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fiction/backend/ai"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// App struct
|
||||
@@ -19,11 +23,67 @@ func (a *App) Startup(ctx context.Context) {
|
||||
a.ctx = ctx
|
||||
}
|
||||
|
||||
// Greet returns a greeting for the given name
|
||||
func (a *App) Greet(name string) string {
|
||||
return fmt.Sprintf("Hello %s, It's show time!", name)
|
||||
// GetNovelList returns the list of txt files in the source directory
|
||||
func (a *App) GetNovelList() []string {
|
||||
var novels []string
|
||||
sourceDir := "./source"
|
||||
|
||||
files, err := os.ReadDir(sourceDir)
|
||||
if err != nil {
|
||||
fmt.Printf("Error reading source directory: %v\n", err)
|
||||
return novels
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
if !file.IsDir() && filepath.Ext(file.Name()) == ".txt" {
|
||||
novels = append(novels, file.Name())
|
||||
}
|
||||
}
|
||||
|
||||
return novels
|
||||
}
|
||||
|
||||
func (a *App) Greet2(name string) string {
|
||||
return fmt.Sprintf("Hello %s, It's show time!", name)
|
||||
// GetNovelContent returns the content of a txt file
|
||||
func (a *App) GetNovelContent(filename string) string {
|
||||
sourceDir := "./source"
|
||||
filePath := filepath.Join(sourceDir, filename)
|
||||
|
||||
content, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
fmt.Printf("Error reading file %s: %v\n", filename, err)
|
||||
return "Error reading file"
|
||||
}
|
||||
|
||||
return string(content)
|
||||
}
|
||||
|
||||
func (a *App) WriteNovelContent(filename string) string {
|
||||
filePath := filepath.Join("./config", "config.json")
|
||||
|
||||
content, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
fmt.Printf("读取提示词文件错误: %v\n", err)
|
||||
return "Error reading file"
|
||||
}
|
||||
|
||||
var str []string
|
||||
if err = json.Unmarshal(content, &str); err != nil {
|
||||
fmt.Printf("读取提示词错误: %v\n", err)
|
||||
return "Error Unmarshal JSON"
|
||||
}
|
||||
|
||||
resp := ""
|
||||
err = ai.NewAIClient(false, "", 0.9).
|
||||
RequestAI(
|
||||
a.GetNovelContent(filename),
|
||||
str,
|
||||
func(content string, i int) error {
|
||||
resp = content
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
return resp
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user