api_func_llm.go 1012 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package route
  2. import (
  3. "context"
  4. "opennamu/route/tool"
  5. "github.com/google/generative-ai-go/genai"
  6. "google.golang.org/api/option"
  7. )
  8. func Api_func_llm(config tool.Config) string {
  9. db := tool.DB_connect()
  10. defer tool.DB_close(db)
  11. other_set := map[string]string{}
  12. json.Unmarshal([]byte(config.Other_set), &other_set)
  13. api_key := ""
  14. tool.QueryRow_DB(
  15. db,
  16. "select data from user_set where name = 'llm_api_key' and id = ?",
  17. []any{ &api_key },
  18. config.IP,
  19. )
  20. ctx := context.Background()
  21. client, err := genai.NewClient(ctx, option.WithAPIKey(api_key))
  22. if err != nil {
  23. panic(err)
  24. }
  25. defer client.Close()
  26. model := client.GenerativeModel("gemini-pro")
  27. resp, err := model.GenerateContent(ctx, genai.Text(other_set["prompt"]))
  28. if err != nil {
  29. panic(err)
  30. }
  31. text := resp.Candidates[0].Content.Parts[0]
  32. json_data, _ := json.Marshal(map[string]genai.Part{"data": text})
  33. return string(json_data)
  34. }