Skip to content

Commit

Permalink
Merge pull request #14 from reply2future/feature/issue#13_change_api
Browse files Browse the repository at this point in the history
Feature/issue#13 change api
  • Loading branch information
reply2future authored Aug 19, 2023
2 parents 00fe627 + 8db57e3 commit f44698d
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 40 deletions.
10 changes: 1 addition & 9 deletions Controllers/assistantController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

class FreshExtension_assistant_Controller extends Minz_ActionController
{
const DEFAULT_MODEL = 'text-davinci-003';
const DEFAULT_TEMPERATURE = 0.2;
const DEFAULT_MAX_TOKENS = 4096;
const NEWS_CATEGORY_TYPE = 'c';

private $config = array();
Expand Down Expand Up @@ -78,10 +75,10 @@ public function streamAction()

if (count($news) > 0) {
$content = self::buildNewsContent($this->config->field, $news);
$content = self::addSummaryPrompt($this->config->prompt, $content);

streamOpenAiApi(
$this->config,
$this->config->prompt,
$content,
function ($msg) {
if ($msg == null) return;
Expand Down Expand Up @@ -110,11 +107,6 @@ public static function buildNewsContent(string $field, array $news)
return implode('', array_map($pickTitleFn, $news));
}

public static function addSummaryPrompt(string $prompt, string $content)
{
return $prompt . "\n\n" . $content;
}

private function getNews(int $cat_id = 0, int $state = FreshRSS_Entry::STATE_NOT_READ, int $limit = 30)
{
$generator = $this->entryDAO->listWhere(self::NEWS_CATEGORY_TYPE, $cat_id, $state, 'DESC', $limit, '', null);
Expand Down
36 changes: 16 additions & 20 deletions configure.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
</div>
</div>
<div class="form-group">
<label class="group-name" for="max_tokens"><a href="https://platform.openai.com/docs/api-reference/completions/create#completions/create-max_tokens" target="_blank"><?php echo _t('ext.field.max_tokens') . " (?)"; ?></a></label>
<label class="group-name" for="max_tokens"><a href="https://platform.openai.com/docs/api-reference/chat/create#chat/create-max_tokens" target="_blank"><?php echo _t('ext.field.max_tokens') . " (?)"; ?></a></label>
<div class="group-controls">
<input type="range" name="max_tokens" id="max_tokens" min="7" max="4096" value="<?php echo FreshRSS_Context::$system_conf->max_tokens ?? 2048; ?>">
<output for="max_tokens"><?php echo FreshRSS_Context::$system_conf->max_tokens ?? 2048; ?></output>
</div>
</div>
<div class="form-group">
<label class="group-name" for="temperature"><a href="https://platform.openai.com/docs/api-reference/completions/create#completions/create-temperature" target="_blank" ><?php echo _t('ext.field.temperature') . " (?)"; ?></a></label>
<label class="group-name" for="temperature"><a href="https://platform.openai.com/docs/api-reference/chat/create#chat/create-temperature" target="_blank"><?php echo _t('ext.field.temperature') . " (?)"; ?></a></label>
<div class="group-controls">
<input type="range" name="temperature" id="temperature" min="0" max="2" step="0.1" value="<?php echo FreshRSS_Context::$system_conf->temperature ?? 1.0; ?>">
<output for="temperature"><?php echo FreshRSS_Context::$system_conf->temperature ?? 1.0; ?></output>
Expand All @@ -37,30 +37,26 @@
</div>
</div>
<div class="form-group">
<label class="group-name" for="prompt"><a href="https://platform.openai.com/docs/api-reference/completions/create#completions/create-prompt" target="_blank" ><?php echo _t('ext.field.prompt') . " (?)"; ?></a></label>
<label class="group-name" for="prompt"><a href="https://platform.openai.com/docs/api-reference/chat/create#chat/create-content" target="_blank"><?php echo _t('ext.field.prompt') . " (?)"; ?></a></label>
<div class="group-controls">
<textarea id="prompt" name="prompt" rows="4" cols="50"><?php echo FreshRSS_Context::$system_conf->prompt ?? 'Summarize this as you are news editor, you should merge the similar topic.'; ?></textarea>
</div>
</div>
<div class="form-group">
<label class="group-name" for="model"><a href="https://platform.openai.com/docs/api-reference/completions/create#completions/create-model" target="_blank"><?php echo _t('ext.field.model') . " (?)"; ?></a></label>
<!-- make the input element to support input and select -->
<label class="group-name" for="model"><a href="https://platform.openai.com/docs/api-reference/chat/create#chat/create-model" target="_blank"><?php echo _t('ext.field.model') . " (?)"; ?></a></label>
<div class="group-controls">
<select name="model" id="model">
<option value="<?php echo htmlentities(FreshRSS_Context::$system_conf->model ?? 'text-davinci-003'); ?>" selected="selected"><?php echo htmlentities(FreshRSS_Context::$system_conf->model ?? 'text-davinci-003'); ?></option>
<option value="-">-</option>
<option value="babbage">babbage</option>
<option value="davinci">davinci</option>
<option value="text-davinci-001">text-davinci-001</option>
<option value="ada">ada</option>
<option value="text-curie-001">text-curie-001</option>
<option value="text-davinci-003">text-davinci-003</option>
<option value="text-ada-001">text-ada-001</option>
<option value="curie-instruct-beta">curie-instruct-beta</option>
<option value="davinci-instruct-beta">davinci-instruct-beta</option>
<option value="text-babbage-001">text-babbage-001</option>
<option value="curie">curie</option>
<option value="text-davinci-002">text-davinci-002</option>
</select>
<input list="select-models" id="model" name="model" value="<?php echo htmlentities(FreshRSS_Context::$system_conf->model ?? 'gpt-3.5-turbo-16k'); ?>">
<datalist id="select-models">
<option value="gpt-4">
<option value="gpt-4-0613">
<option value="gpt-4-32k">
<option value="gpt-4-32k-0613">
<option value="gpt-3.5-turbo">
<option value="gpt-3.5-turbo-0613">
<option value="gpt-3.5-turbo-16k">
<option value="gpt-3.5-turbo-16k-0613">
</datalist>
</div>
</div>

Expand Down
2 changes: 1 addition & 1 deletion extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function handleConfigureAction()
FreshRSS_Context::$system_conf->max_tokens = filter_var(Minz_Request::param('max_tokens', 7), FILTER_VALIDATE_INT);
FreshRSS_Context::$system_conf->temperature = filter_var(Minz_Request::param('temperature', 1), FILTER_VALIDATE_FLOAT);;
FreshRSS_Context::$system_conf->limit = filter_var(Minz_Request::param('limit', 30), FILTER_VALIDATE_FLOAT);;
FreshRSS_Context::$system_conf->model = Minz_Request::param('model', 'text-davinci-003');
FreshRSS_Context::$system_conf->model = Minz_Request::param('model', 'gpt-3.5-turbo-16k');
FreshRSS_Context::$system_conf->prompt = Minz_Request::param('prompt', 'Summarize this as you are news editor, you should merge the similar topic.');
FreshRSS_Context::$system_conf->field = Minz_Request::param('field', 'content');
FreshRSS_Context::$system_conf->save();
Expand Down
17 changes: 13 additions & 4 deletions helper.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

const OPENAI_API_COMPLETIONS_URL = 'https://api.openai.com/v1/completions';
const OPENAI_API_COMPLETIONS_URL = 'https://api.openai.com/v1/chat/completions';
function endsWithPunctuation($str)
{
$pattern = '/\p{P}$/u'; // regex pattern for ending with punctuation marks
Expand All @@ -9,19 +9,28 @@ function endsWithPunctuation($str)

function _dealResponse($openai_response)
{
return $openai_response->choices[0]->text;
return $openai_response->choices[0]->delta->content ?? '';
}

function _errorHtmlSuffix($error_msg)
{
return 'Ooooops!!!!<br><br>' . $error_msg;
}

function streamOpenAiApi(object $config, string $prompt, callable $task_callback, callable $finish_callback)
function streamOpenAiApi(object $config, string $prompt, string $content, callable $task_callback, callable $finish_callback)
{
$post_fields = json_encode(array(
"model" => $config->model,
"prompt" => $prompt,
"messages" => array(
array(
"role" => "system",
"content" => $prompt,
),
array(
"role" => "user",
"content" => $content,
),
),
"max_tokens" => $config->max_tokens,
"temperature" => $config->temperature,
"stream" => true,
Expand Down
2 changes: 1 addition & 1 deletion metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "News Assistant",
"author": "Mervyn Zhan",
"description": "Use the api of `OpenAI` to summary the news.",
"version": 0.4,
"version": 0.5,
"entrypoint": "NewsAssistant",
"type": "system"
}
6 changes: 1 addition & 5 deletions static/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@
const evtSource = new EventSource(`/i/?c=assistant&a=stream&cat_id=${urlParams.get('cat_id')}&state=${urlParams.get('state')}`);

function dealWithEventData(data) {
if (data == null) return '';

if (data.trim() == '') {
return '<br>';
}
if (data == null || data.trim() == '') return '';

return data;
}
Expand Down

0 comments on commit f44698d

Please sign in to comment.