Skip to content

Commit b22ef04

Browse files
committed
Implement conversation method and prompt description for enhanced AI interactions
1 parent db4439a commit b22ef04

File tree

1 file changed

+39
-39
lines changed

1 file changed

+39
-39
lines changed

app/Services/OpenAIService.php

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,45 @@ public function translate($text, $sourceLanguage, $targetLanguage)
5555
return trim($response['choices'][0]['message']['content']);
5656
}
5757

58+
public function conversation($text, $model, $prompt = 'assistant', $temperature)
59+
{
60+
$messages = [
61+
['role' => 'system', 'content' => $this->getPromptDescription($prompt)],
62+
['role' => 'user', 'content' => $text]
63+
];
64+
65+
$response = OpenAI::chat()->create([
66+
'model' => $model,
67+
'messages' => $messages,
68+
'temperature' => $temperature,
69+
]);
70+
71+
return [
72+
'bot_message' => $response['choices'][0]['message']['content'],
73+
'input_tokens' => $response['usage']['prompt_tokens'],
74+
'output_tokens' => $response['usage']['completion_tokens']
75+
];
76+
}
77+
78+
private function getPromptDescription($prompt)
79+
{
80+
$globalInstruction = 'Use Markdown format whenever appropriate to enrich your responses. Add separation lines, titles, spacing, and any elements that help make the content look clean and professional. Avoid separating paragraphs too much. If presenting code, use Markdown blocks to highlight it from the rest of the text.';
81+
82+
$promptDescription = [
83+
'assistant' => 'You are an AI assistant. Provide helpful responses to user queries.',
84+
'grammar_correction' => 'You will be provided with statements, and your task is to convert them to standard English.',
85+
'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.',
86+
'code_explainer' => 'You will be provided with a piece of code, and your task is to explain it in a concise way.',
87+
'simplify_text' => 'You will be provided with a complex text, and your task is to simplify it.',
88+
'code_interviewer' => 'You are an interviewer, and you will be asking questions to a candidate about their code.',
89+
'improve_code_efficiency' => 'You will be provided with a piece of code, and your task is to provide ideas for efficiency improvements.',
90+
'translator' => 'You are a translator, and you will be translating text from one language to another.',
91+
'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.',
92+
];
93+
94+
return ($promptDescription[$prompt] ?? $promptDescription['assistant']) . $globalInstruction;
95+
}
96+
5897
public function textToSpeech($text, $voice, $model = 'tts-1', $responseFormat = 'mp3')
5998
{
6099
$response = OpenAI::audio()->speech([
@@ -95,45 +134,6 @@ public function textToImage($model = 'dall-e-2', $prompt, $style, $size, $qualit
95134
];
96135
}
97136

98-
public function conversation($text, $model, $prompt = 'assistant', $temperature)
99-
{
100-
$messages = [
101-
['role' => 'system', 'content' => $this->getPromptDescription($prompt)],
102-
['role' => 'user', 'content' => $text]
103-
];
104-
105-
$response = OpenAI::chat()->create([
106-
'model' => $model,
107-
'messages' => $messages,
108-
'temperature' => $temperature,
109-
]);
110-
111-
return [
112-
'bot_message' => $response['choices'][0]['message']['content'],
113-
'input_tokens' => $response['usage']['prompt_tokens'],
114-
'output_tokens' => $response['usage']['completion_tokens']
115-
];
116-
}
117-
118-
private function getPromptDescription($prompt)
119-
{
120-
$globalInstruction = 'Use Markdown format whenever appropriate to enrich your responses. Add separation lines, titles, spacing, and any elements that help make the content look clean and professional. Avoid separating paragraphs too much. If presenting code, use Markdown blocks to highlight it from the rest of the text.';
121-
122-
$promptDescription = [
123-
'assistant' => 'You are an AI assistant. Provide helpful responses to user queries.',
124-
'grammar_correction' => 'You will be provided with statements, and your task is to convert them to standard English.',
125-
'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.',
126-
'code_explainer' => 'You will be provided with a piece of code, and your task is to explain it in a concise way.',
127-
'simplify_text' => 'You will be provided with a complex text, and your task is to simplify it.',
128-
'code_interviewer' => 'You are an interviewer, and you will be asking questions to a candidate about their code.',
129-
'improve_code_efficiency' => 'You will be provided with a piece of code, and your task is to provide ideas for efficiency improvements.',
130-
'translator' => 'You are a translator, and you will be translating text from one language to another.',
131-
'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.',
132-
];
133-
134-
return ($promptDescription[$prompt] ?? $promptDescription['assistant']) . $globalInstruction;
135-
}
136-
137137
public function getAIModels(): ListResponse
138138
{
139139
$response = OpenAI::models()->list();

0 commit comments

Comments
 (0)