api_func_llm.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package route
  2. import (
  3. "context"
  4. "database/sql"
  5. "encoding/json"
  6. "fmt"
  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) {
  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. if db == nil {
  18. return
  19. }
  20. defer db.Close()
  21. var api_key string
  22. stmt, err := db.Prepare(tool.DB_change(db_set, "select data from user_set where name = 'llm_api_key' and id = ?"))
  23. if err != nil {
  24. return
  25. }
  26. defer stmt.Close()
  27. err = stmt.QueryRow(other_set["ip"]).Scan(api_key)
  28. if err != nil {
  29. if err == sql.ErrNoRows {
  30. api_key = ""
  31. } else {
  32. return
  33. }
  34. }
  35. ctx := context.Background()
  36. client, err := genai.NewClient(ctx, option.WithAPIKey(api_key))
  37. if err != nil {
  38. return
  39. }
  40. defer client.Close()
  41. model := client.GenerativeModel("gemini-pro")
  42. resp, err := model.GenerateContent(ctx, genai.Text(other_set["prompt"]))
  43. if err != nil {
  44. return
  45. }
  46. text := resp.Candidates[0].Content.Parts[0]
  47. json_data, _ := json.Marshal(map[string]genai.Part{"data": text})
  48. fmt.Print(string(json_data))
  49. }