Skip to content

Commit 82f2b10

Browse files
committed
Refactor OpenAIService methods for improved structure and Markdown formatting
1 parent b22ef04 commit 82f2b10

File tree

1 file changed

+69
-57
lines changed

1 file changed

+69
-57
lines changed

app/Services/OpenAIService.php

Lines changed: 69 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,6 @@ public function transcribe($filePath, $language): TranscriptionResponse
2020
return $response;
2121
}
2222

23-
public function enrichWithMarkdown($text, $context = '')
24-
{
25-
$prompt = "Take the following text and format it using Markdown:\n\n" .
26-
"Text:\n$text\n\n" .
27-
($context ? "Additional context: $context\n\n" : "") .
28-
"Instructions:\n- Use headings for relevant titles.\n- Use lists if there are enumerated items.\n- Use bold to highlight important words.\n- Use code blocks if there are technical snippets.\n- If it's a song lyric, format verses, paragraphs, separate the text into introduction, verses, chorus, bridge, and ending.\n- Adapt the formatting depending on whether it's a story, narration, dictation, etc.\n\nReturn only the text in Markdown.";
29-
30-
$response = OpenAI::chat()->create([
31-
'model' => 'gpt-3.5-turbo',
32-
'messages' => [
33-
['role' => 'system', 'content' => 'You are an assistant that formats text in Markdown.'],
34-
['role' => 'user', 'content' => $prompt],
35-
],
36-
]);
37-
38-
return $response['choices'][0]['message']['content'];
39-
}
40-
4123
public function translate($text, $sourceLanguage, $targetLanguage)
4224
{
4325
$messages = [
@@ -55,45 +37,6 @@ public function translate($text, $sourceLanguage, $targetLanguage)
5537
return trim($response['choices'][0]['message']['content']);
5638
}
5739

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-
9740
public function textToSpeech($text, $voice, $model = 'tts-1', $responseFormat = 'mp3')
9841
{
9942
$response = OpenAI::audio()->speech([
@@ -134,6 +77,75 @@ public function textToImage($model = 'dall-e-2', $prompt, $style, $size, $qualit
13477
];
13578
}
13679

80+
public function conversation($text, $model, $prompt = 'assistant', $temperature)
81+
{
82+
$messages = [
83+
['role' => 'system', 'content' => $this->getPromptDescription($prompt)],
84+
['role' => 'user', 'content' => $text]
85+
];
86+
87+
$response = OpenAI::chat()->create([
88+
'model' => $model,
89+
'messages' => $messages,
90+
'temperature' => $temperature,
91+
]);
92+
93+
return [
94+
'bot_message' => $response['choices'][0]['message']['content'],
95+
'input_tokens' => $response['usage']['prompt_tokens'],
96+
'output_tokens' => $response['usage']['completion_tokens']
97+
];
98+
}
99+
100+
private function sendChatRequest(string $model,array $messages,float $temperature = 0.7, int $maxTokens = 256) {
101+
102+
$response = OpenAI::chat()->create([
103+
'model' => $model,
104+
'messages' => $messages,
105+
'temperature' => $temperature,
106+
'max_tokens' => $maxTokens,
107+
]);
108+
109+
return $response;
110+
}
111+
112+
public function enrichWithMarkdown($text, $context = '')
113+
{
114+
$prompt = "Take the following text and format it using Markdown:\n\n" .
115+
"Text:\n$text\n\n" .
116+
($context ? "Additional context: $context\n\n" : "") .
117+
"Instructions:\n- Use headings for relevant titles.\n- Use lists if there are enumerated items.\n- Use bold to highlight important words.\n- Use code blocks if there are technical snippets.\n- If it's a song lyric, format verses, paragraphs, separate the text into introduction, verses, chorus, bridge, and ending.\n- Adapt the formatting depending on whether it's a story, narration, dictation, etc.\n\nReturn only the text in Markdown.";
118+
119+
$response = OpenAI::chat()->create([
120+
'model' => 'gpt-3.5-turbo',
121+
'messages' => [
122+
['role' => 'system', 'content' => 'You are an assistant that formats text in Markdown.'],
123+
['role' => 'user', 'content' => $prompt],
124+
],
125+
]);
126+
127+
return $response['choices'][0]['message']['content'];
128+
}
129+
130+
private function getPromptDescription($prompt)
131+
{
132+
$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.';
133+
134+
$promptDescription = [
135+
'assistant' => 'You are an AI assistant. Provide helpful responses to user queries.',
136+
'grammar_correction' => 'You will be provided with statements, and your task is to convert them to standard English.',
137+
'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.',
138+
'code_explainer' => 'You will be provided with a piece of code, and your task is to explain it in a concise way.',
139+
'simplify_text' => 'You will be provided with a complex text, and your task is to simplify it.',
140+
'code_interviewer' => 'You are an interviewer, and you will be asking questions to a candidate about their code.',
141+
'improve_code_efficiency' => 'You will be provided with a piece of code, and your task is to provide ideas for efficiency improvements.',
142+
'translator' => 'You are a translator, and you will be translating text from one language to another.',
143+
'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.',
144+
];
145+
146+
return ($promptDescription[$prompt] ?? $promptDescription['assistant']) . $globalInstruction;
147+
}
148+
137149
public function getAIModels(): ListResponse
138150
{
139151
$response = OpenAI::models()->list();

0 commit comments

Comments
 (0)