api_func_llm.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package route
  2. import (
  3. "context"
  4. "database/sql"
  5. "encoding/json"
  6. "fmt"
  7. "log"
  8. "opennamu/route/tool"
  9. "github.com/google/generative-ai-go/genai"
  10. "google.golang.org/api/option"
  11. )
  12. func Api_func_llm(call_arg []string) {
  13. db_set := map[string]string{}
  14. json.Unmarshal([]byte(call_arg[0]), &db_set)
  15. other_set := map[string]string{}
  16. json.Unmarshal([]byte(call_arg[1]), &other_set)
  17. db := tool.DB_connect(db_set)
  18. if db == nil {
  19. return
  20. }
  21. defer db.Close()
  22. var api_key string
  23. stmt, err := db.Prepare(tool.DB_change(db_set, "select data from user_set where name = 'llm_api_key' and id = ?"))
  24. if err != nil {
  25. log.Fatal(err)
  26. }
  27. defer stmt.Close()
  28. err = stmt.QueryRow(other_set["ip"]).Scan(api_key)
  29. if err != nil {
  30. if err == sql.ErrNoRows {
  31. api_key = ""
  32. } else {
  33. log.Fatal(err)
  34. }
  35. }
  36. ctx := context.Background()
  37. client, err := genai.NewClient(ctx, option.WithAPIKey(api_key))
  38. if err != nil {
  39. log.Fatal(err)
  40. }
  41. defer client.Close()
  42. model := client.GenerativeModel("gemini-pro")
  43. resp, err := model.GenerateContent(ctx, genai.Text(other_set["prompt"]))
  44. if err != nil {
  45. log.Fatal(err)
  46. }
  47. text := resp.Candidates[0].Content.Parts[0]
  48. json_data, _ := json.Marshal(map[string]genai.Part{"data": text})
  49. fmt.Print(string(json_data))
  50. }