api_func_llm.go 1.2 KB

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