PromptBuilder.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * PromptBuilder class to build prompts for a chatbot.
  4. * It allows setting roles, questions, and context, and builds a prompt string or data for API requests.
  5. */
  6. class PromptBuilder
  7. {
  8. /**
  9. * Role of the chatbot, e.g., "You are a helpful assistant."
  10. * @var string
  11. */
  12. private $role = "";
  13. /**
  14. * Question asked by the user, e.g., "What is the weather today?"
  15. * @var string
  16. */
  17. private $question = "";
  18. /**
  19. * Context for the chatbot, e.g., additional information or data to consider.
  20. * @var string
  21. */
  22. private $context = "";
  23. /**
  24. * Allow context to be used in the prompt.
  25. * If false, the context will not be included in the prompt.
  26. * @var bool
  27. */
  28. private $allowContext = true;
  29. /**
  30. * Returns the role of the chatbot.
  31. * @return string The role of the chatbot.
  32. */
  33. public function getRole(){
  34. return $this->role;
  35. }
  36. /**
  37. * Returns the question of the chatbot.
  38. * @return string The question of the chatbot.
  39. */
  40. public function getQuestion(){
  41. return $this->question;
  42. }
  43. /**
  44. * Returns the context of the chatbot.
  45. * @return string The context of the chatbot.
  46. */
  47. public function getContext(){
  48. return $this->context;
  49. }
  50. /**
  51. * Returns whether the context is allowed in the prompt.
  52. * @return bool True if context is allowed, false otherwise.
  53. */
  54. public function getContextAllowness(){
  55. return $this->allowContext;
  56. }
  57. /**
  58. * Sets the role of the chatbot.
  59. * @param string $role The role to set for the chatbot.
  60. */
  61. public function setRole($role){
  62. $this->role = $role;
  63. }
  64. /**
  65. * Sets the question for the chatbot.
  66. * @param string $question The question to set for the chatbot.
  67. */
  68. public function setQuestion($question){
  69. $this->question = $question;
  70. }
  71. /**
  72. * Sets the context for the chatbot.
  73. * @param string $context The context to set for the chatbot.
  74. */
  75. public function setContext($context){
  76. //TODO: Better context handling in order to bypass token limits
  77. // Maybe require an Array of context items? (MCP Like?)
  78. $this->context = $context;
  79. }
  80. /**
  81. * Sets whether the context is allowed in the prompt.
  82. * @param bool $allowContext True to allow context, false to disallow.
  83. */
  84. public function setContextAllowness($allowContext){
  85. $this->allowContext = $allowContext;
  86. }
  87. /**
  88. * Builds the prompt string for the chatbot.
  89. * This includes the role, context (if allowed), and question.
  90. * @return string The complete prompt string.
  91. */
  92. public function buildPromptString(){
  93. $prompt = "Deine Rolle ist: " . $this->role;
  94. if ($this->allowContext) {
  95. $prompt .= "\nDu hast folgenden Kontext, auf den du dich NUR fokussierst: " . $this->context;
  96. }
  97. $prompt .= "\nDies ist die Frage des Nutzers: " . $this->question;
  98. $prompt .= "\nBeantworte die Frage so gut es geht\n";
  99. return $prompt;
  100. }
  101. /**
  102. * Builds the prompt data for API requests.
  103. * This includes the model, messages (role, question, context), and other parameters.
  104. * @return string JSON encoded prompt data.
  105. */
  106. public function buildPromptData(){
  107. $messages = [
  108. ["role" => "system", "content" => $this->getRole()],
  109. ["role" => "user", "content" => $this->getQuestion()],
  110. ];
  111. // add context if allowed
  112. if ($this->allowContext) {
  113. $messages[] = ["role" => "context", "content" => $this->getContext()];
  114. }
  115. return json_encode([
  116. "model" => "deepseek-llm-7b-chat",
  117. "messages" => $messages,
  118. "temperature" => 0.5,
  119. "max_tokens" => -1,
  120. "stream" => false
  121. ]);
  122. }
  123. }