api_func_llm.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. other_set := map[string]string{}
  13. json.Unmarshal([]byte(call_arg[0]), &other_set)
  14. db := tool.DB_connect()
  15. defer db.Close()
  16. var api_key string
  17. stmt, err := db.Prepare(tool.DB_change("select data from user_set where name = 'llm_api_key' and id = ?"))
  18. if err != nil {
  19. log.Fatal(err)
  20. }
  21. defer stmt.Close()
  22. err = stmt.QueryRow(other_set["ip"]).Scan(api_key)
  23. if err != nil {
  24. if err == sql.ErrNoRows {
  25. api_key = ""
  26. } else {
  27. log.Fatal(err)
  28. }
  29. }
  30. ctx := context.Background()
  31. client, err := genai.NewClient(ctx, option.WithAPIKey(api_key))
  32. if err != nil {
  33. log.Fatal(err)
  34. }
  35. defer client.Close()
  36. model := client.GenerativeModel("gemini-pro")
  37. resp, err := model.GenerateContent(ctx, genai.Text(other_set["prompt"]))
  38. if err != nil {
  39. log.Fatal(err)
  40. }
  41. text := resp.Candidates[0].Content.Parts[0]
  42. json_data, _ := json.Marshal(map[string]genai.Part{"data": text})
  43. return string(json_data)
  44. }