Skip to content

Commit

Permalink
TODEL
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Veyssier <[email protected]>
  • Loading branch information
julien-nc committed Jan 15, 2025
1 parent eb16453 commit 2bdb2f1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
6 changes: 5 additions & 1 deletion lib/Service/OpenAiAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ public function createChatCompletion(
$params = [
'model' => $model === Application::DEFAULT_MODEL_ID ? Application::DEFAULT_COMPLETION_MODEL_ID : $model,
'messages' => $messages,
'n' => $n,
// 'n' => $n,
];

$maxTokensLimit = $this->openAiSettingsService->getMaxTokens();
Expand All @@ -428,6 +428,8 @@ public function createChatCompletion(
$params['max_tokens'] = $maxTokens;
}

$params['n'] = $n;

if ($tools !== null) {
$params['tools'] = $tools;
}
Expand Down Expand Up @@ -806,6 +808,8 @@ public function request(?string $userId, string $endPoint, array $params = [], s
if ($method === 'GET') {
$response = $this->client->get($url, $options);
} elseif ($method === 'POST') {
error_log('actual URL '.$url);
error_log('actual PARAMS '. json_encode($params));
$response = $this->client->post($url, $options);
} elseif ($method === 'PUT') {
$response = $this->client->put($url, $options);
Expand Down
45 changes: 39 additions & 6 deletions tests/unit/Providers/OpenAiProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,15 @@ public function testFreePromptProvider(): void {

$url = self::OPENAI_API_BASE . 'chat/completions';
$options = ['timeout' => Application::OPENAI_DEFAULT_REQUEST_TIMEOUT, 'headers' => ['User-Agent' => Application::USER_AGENT, 'Authorization' => self::AUTHORIZATION_HEADER, 'Content-Type' => 'application/json']];
$options['body'] = json_encode(['model' => Application::DEFAULT_COMPLETION_MODEL_ID, 'messages' => [['role' => 'user', 'content' => $prompt]], 'max_tokens' => Application::DEFAULT_MAX_NUM_OF_TOKENS, 'n' => $n, 'user' => self::TEST_USER1]);
$options['body'] = json_encode([
'model' => Application::DEFAULT_COMPLETION_MODEL_ID,
'messages' => [['role' => 'user', 'content' => $prompt]],
'max_completion_tokens' => Application::DEFAULT_MAX_NUM_OF_TOKENS,
'n' => $n,
'user' => self::TEST_USER1,
]);
error_log('EXPECTED URL '. $url);
error_log('EXPECTED PARAMS '. $options['body']);

$iResponse = $this->createMock(\OCP\Http\Client\IResponse::class);
$iResponse->method('getBody')->willReturn($response);
Expand Down Expand Up @@ -194,7 +202,13 @@ public function testHeadlineProvider(): void {

$options = ['timeout' => Application::OPENAI_DEFAULT_REQUEST_TIMEOUT, 'headers' => ['User-Agent' => Application::USER_AGENT, 'Authorization' => self::AUTHORIZATION_HEADER, 'Content-Type' => 'application/json']];
$message = 'Give me the headline of the following text in its original language. Do not output the language. Output only the headline without any quotes or additional punctuation.' . "\n\n" . $prompt;
$options['body'] = json_encode(['model' => Application::DEFAULT_COMPLETION_MODEL_ID, 'messages' => [['role' => 'user', 'content' => $message]], 'max_tokens' => Application::DEFAULT_MAX_NUM_OF_TOKENS, 'n' => $n, 'user' => self::TEST_USER1]);
$options['body'] = json_encode([
'model' => Application::DEFAULT_COMPLETION_MODEL_ID,
'messages' => [['role' => 'user', 'content' => $message]],
'max_completion_tokens' => Application::DEFAULT_MAX_NUM_OF_TOKENS,
'n' => $n,
'user' => self::TEST_USER1,
]);

$iResponse = $this->createMock(\OCP\Http\Client\IResponse::class);
$iResponse->method('getBody')->willReturn($response);
Expand Down Expand Up @@ -252,7 +266,13 @@ public function testChangeToneProvider(): void {

$options = ['timeout' => Application::OPENAI_DEFAULT_REQUEST_TIMEOUT, 'headers' => ['User-Agent' => Application::USER_AGENT, 'Authorization' => self::AUTHORIZATION_HEADER, 'Content-Type' => 'application/json']];
$message = "Reformulate the following text in a $toneInput tone in its original language. Output only the reformulation. Here is the text:" . "\n\n" . $textInput . "\n\n" . 'Do not mention the used language in your reformulation. Here is your reformulation in the same language:';
$options['body'] = json_encode(['model' => Application::DEFAULT_COMPLETION_MODEL_ID, 'messages' => [['role' => 'user', 'content' => $message]], 'max_tokens' => Application::DEFAULT_MAX_NUM_OF_TOKENS, 'n' => $n, 'user' => self::TEST_USER1]);
$options['body'] = json_encode([
'model' => Application::DEFAULT_COMPLETION_MODEL_ID,
'messages' => [['role' => 'user', 'content' => $message]],
'max_completion_tokens' => Application::DEFAULT_MAX_NUM_OF_TOKENS,
'n' => $n,
'user' => self::TEST_USER1,
]);

$iResponse = $this->createMock(\OCP\Http\Client\IResponse::class);
$iResponse->method('getBody')->willReturn($response);
Expand Down Expand Up @@ -310,7 +330,14 @@ public function testSummaryProvider(): void {

$options = ['timeout' => Application::OPENAI_DEFAULT_REQUEST_TIMEOUT, 'headers' => ['User-Agent' => Application::USER_AGENT, 'Authorization' => self::AUTHORIZATION_HEADER, 'Content-Type' => 'application/json']];
$systemPrompt = 'Summarize the following text in the same language as the text.';
$options['body'] = json_encode(['model' => Application::DEFAULT_COMPLETION_MODEL_ID, 'messages' => [['role' => 'system', 'content' => $systemPrompt], ['role' => 'user', 'content' => $prompt]], 'max_tokens' => Application::DEFAULT_MAX_NUM_OF_TOKENS, 'n' => $n, 'user' => self::TEST_USER1]);
$options['body'] = json_encode([
'model' => Application::DEFAULT_COMPLETION_MODEL_ID,
'messages' => [['role' => 'system', 'content' => $systemPrompt],
['role' => 'user', 'content' => $prompt]],
'max_completion_tokens' => Application::DEFAULT_MAX_NUM_OF_TOKENS,
'n' => $n,
'user' => self::TEST_USER1,
]);

$iResponse = $this->createMock(\OCP\Http\Client\IResponse::class);
$iResponse->method('getBody')->willReturn($response);
Expand Down Expand Up @@ -370,7 +397,7 @@ public function testProofreadProvider(): void {
$options['body'] = json_encode([
'model' => Application::DEFAULT_COMPLETION_MODEL_ID,
'messages' => [['role' => 'system', 'content' => $systemPrompt],['role' => 'user', 'content' => $prompt]],
'max_tokens' => Application::DEFAULT_MAX_NUM_OF_TOKENS,
'max_completion_tokens' => Application::DEFAULT_MAX_NUM_OF_TOKENS,
'n' => $n,
'user' => self::TEST_USER1,
]);
Expand Down Expand Up @@ -434,7 +461,13 @@ public function testTranslationProvider(): void {
$url = self::OPENAI_API_BASE . 'chat/completions';

$options = ['timeout' => Application::OPENAI_DEFAULT_REQUEST_TIMEOUT, 'headers' => ['User-Agent' => Application::USER_AGENT, 'Authorization' => self::AUTHORIZATION_HEADER, 'Content-Type' => 'application/json']];
$options['body'] = json_encode(['model' => Application::DEFAULT_COMPLETION_MODEL_ID, 'messages' => [['role' => 'user', 'content' => 'Translate from ' . $fromLang . ' to English (US): ' . $inputText]], 'max_tokens' => Application::DEFAULT_MAX_NUM_OF_TOKENS, 'n' => $n, 'user' => self::TEST_USER1]);
$options['body'] = json_encode([
'model' => Application::DEFAULT_COMPLETION_MODEL_ID,
'messages' => [['role' => 'user', 'content' => 'Translate from ' . $fromLang . ' to English (US): ' . $inputText]],
'max_completion_tokens' => Application::DEFAULT_MAX_NUM_OF_TOKENS,
'n' => $n,
'user' => self::TEST_USER1,
]);

$iResponse = $this->createMock(\OCP\Http\Client\IResponse::class);
$iResponse->method('getBody')->willReturn($response);
Expand Down

0 comments on commit 2bdb2f1

Please sign in to comment.