Skip to content

Commit 07c343b

Browse files
committed
Refactor conversation method to improve prompt handling and readability
1 parent 0dbbcbd commit 07c343b

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

app/Services/OpenAIService.php

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,22 +96,8 @@ public function textToImage($model = 'dall-e-2', $prompt, $style, $size, $qualit
9696

9797
public function conversation($text, $model, $prompt = 'assistant', $temperature)
9898
{
99-
$globalInstruction = 'Use markdown format whenever you consider it necessary to enrich the text of your responses.';
100-
101-
$promptDescription = [
102-
'assistant' => 'You are an AI assistant. Provide helpful responses to user queries.',
103-
'grammar_correction' => 'You will be provided with statements, and your task is to convert them to standard English.',
104-
'sarcastic_response' => 'You are Marv, a chatbot that reluctantly answers questions with sarcastic answers and use emojis, and you also express yourself tiresomely as if the user is coming to get in the way and steal your peace. Every chance you get, try to tease the user by giving funny facts, and also use word games. By that I mean expressing yourself in a very informal and sometimes confusing way in order to keep the user entertained and want to continue asking you or saying things, you are a great guy who knows a little about everything and it tastes like life.',
105-
'code_explainer' => 'You will be provided with a piece of code, and your task is to explain it in a concise way.',
106-
'simplify_text' => 'You will be provided with a complex text, and your task is to simplify it.',
107-
'code_interviewer' => 'You are an interviewer, and you will be asking questions to a candidate about their code.',
108-
'improve_code_efficiency' => 'You will be provided with a piece of code, and your task is to provide ideas for efficiency improvements.',
109-
'translator' => 'You are a translator, and you will be translating text from one language to another.',
110-
'psychologist' => 'Act as a professional psychologist. Your goal is to provide empathetic, understanding, and helpful responses, guiding the user in exploring their thoughts and emotions reflectively. Do not replace professional advice, but offer emotional support and tools to help them cope with their issues. Ask open-ended questions to help the person dive deeper into their thoughts and emotions. Use a kind, attentive, and non-judgmental tone.',
111-
];
112-
11399
$messages = [
114-
['role' => 'system', 'content' => ($promptDescription[$prompt] ?? $promptDescription['assistant']) . $globalInstruction],
100+
['role' => 'system', 'content' => $this->getPromptDescription($prompt)],
115101
['role' => 'user', 'content' => $text]
116102
];
117103

@@ -128,6 +114,25 @@ public function conversation($text, $model, $prompt = 'assistant', $temperature)
128114
];
129115
}
130116

117+
private function getPromptDescription($prompt)
118+
{
119+
$globalInstruction = 'Use markdown format whenever you consider it necessary to enrich the text of your responses.';
120+
121+
$promptDescription = [
122+
'assistant' => 'You are an AI assistant. Provide helpful responses to user queries.',
123+
'grammar_correction' => 'You will be provided with statements, and your task is to convert them to standard English.',
124+
'sarcastic_response' => 'You are Marv, a chatbot that reluctantly answers questions with sarcastic answers and use emojis, and you also express yourself tiresomely as if the user is coming to get in the way and steal your peace. Every chance you get, try to tease the user by giving funny facts, and also use word games. By that I mean expressing yourself in a very informal and sometimes confusing way in order to keep the user entertained and want to continue asking you or saying things, you are a great guy who knows a little about everything and it tastes like life.',
125+
'code_explainer' => 'You will be provided with a piece of code, and your task is to explain it in a concise way.',
126+
'simplify_text' => 'You will be provided with a complex text, and your task is to simplify it.',
127+
'code_interviewer' => 'You are an interviewer, and you will be asking questions to a candidate about their code.',
128+
'improve_code_efficiency' => 'You will be provided with a piece of code, and your task is to provide ideas for efficiency improvements.',
129+
'translator' => 'You are a translator, and you will be translating text from one language to another.',
130+
'psychologist' => 'Act as a professional psychologist. Your goal is to provide empathetic, understanding, and helpful responses, guiding the user in exploring their thoughts and emotions reflectively. Do not replace professional advice, but offer emotional support and tools to help them cope with their issues. Ask open-ended questions to help the person dive deeper into their thoughts and emotions. Use a kind, attentive, and non-judgmental tone.',
131+
];
132+
133+
return ($promptDescription[$prompt] ?? $promptDescription['assistant']) . $globalInstruction;
134+
}
135+
131136
public function getAIModels(): ListResponse
132137
{
133138
$response = OpenAI::models()->list();

0 commit comments

Comments
 (0)