| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- <?php
- /**
- * PromptBuilder class to build prompts for a chatbot.
- * It allows setting roles, questions, and context, and builds a prompt string or data for API requests.
- */
- class PromptBuilder
- {
- /**
- * Role of the chatbot, e.g., "You are a helpful assistant."
- * @var string
- */
- private $role = "";
- /**
- * Question asked by the user, e.g., "What is the weather today?"
- * @var string
- */
- private $question = "";
- /**
- * Context for the chatbot, e.g., additional information or data to consider.
- * @var string
- */
- private $context = "";
- /**
- * Allow context to be used in the prompt.
- * If false, the context will not be included in the prompt.
- * @var bool
- */
- private $allowContext = true;
- /**
- * Returns the role of the chatbot.
- * @return string The role of the chatbot.
- */
- public function getRole(){
- return $this->role;
- }
- /**
- * Returns the question of the chatbot.
- * @return string The question of the chatbot.
- */
- public function getQuestion(){
- return $this->question;
- }
- /**
- * Returns the context of the chatbot.
- * @return string The context of the chatbot.
- */
- public function getContext(){
- return $this->context;
- }
- /**
- * Returns whether the context is allowed in the prompt.
- * @return bool True if context is allowed, false otherwise.
- */
- public function getContextAllowness(){
- return $this->allowContext;
- }
- /**
- * Sets the role of the chatbot.
- * @param string $role The role to set for the chatbot.
- */
- public function setRole($role){
- $this->role = $role;
- }
- /**
- * Sets the question for the chatbot.
- * @param string $question The question to set for the chatbot.
- */
- public function setQuestion($question){
- $this->question = $question;
- }
- /**
- * Sets the context for the chatbot.
- * @param string $context The context to set for the chatbot.
- */
- public function setContext($context){
- //TODO: Better context handling in order to bypass token limits
- // Maybe require an Array of context items? (MCP Like?)
- $this->context = $context;
- }
- /**
- * Sets whether the context is allowed in the prompt.
- * @param bool $allowContext True to allow context, false to disallow.
- */
- public function setContextAllowness($allowContext){
- $this->allowContext = $allowContext;
- }
- /**
- * Builds the prompt string for the chatbot.
- * This includes the role, context (if allowed), and question.
- * @return string The complete prompt string.
- */
- public function buildPromptString(){
- $prompt = "Deine Rolle ist: " . $this->role;
- if ($this->allowContext) {
- $prompt .= "\nDu hast folgenden Kontext, auf den du dich NUR fokussierst: " . $this->context;
- }
- $prompt .= "\nDies ist die Frage des Nutzers: " . $this->question;
- $prompt .= "\nBeantworte die Frage so gut es geht\n";
- return $prompt;
- }
- /**
- * Builds the prompt data for API requests.
- * This includes the model, messages (role, question, context), and other parameters.
- * @return string JSON encoded prompt data.
- */
- public function buildPromptData(){
- $messages = [
- ["role" => "system", "content" => $this->getRole()],
- ["role" => "user", "content" => $this->getQuestion()],
- ];
- // add context if allowed
- if ($this->allowContext) {
- $messages[] = ["role" => "context", "content" => $this->getContext()];
- }
- return json_encode([
- "model" => "deepseek-llm-7b-chat",
- "messages" => $messages,
- "temperature" => 0.5,
- "max_tokens" => -1,
- "stream" => false
- ]);
- }
- }
|