Skip to content

Commit

Permalink
Merge pull request #864 from terminaldweller/tongeworm
Browse files Browse the repository at this point in the history
[tongueworm.pl] added a new provider, ollama
  • Loading branch information
ailin-nemui authored Nov 28, 2023
2 parents 34f6c9a + 26b8c39 commit d84213b
Showing 1 changed file with 32 additions and 15 deletions.
47 changes: 32 additions & 15 deletions scripts/tongueworm.pl
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
use strict;
use warnings;

our $VERSION = "0.1";
our $VERSION = "1.0.0";
our %IRSSI = (
authors => 'terminaldweller',
contact => 'https://terminaldweller.com',
name => 'tongueworm',
description => 'rewrites the input line using openai chatgpt',
license => 'GPL3 or newer',
url => 'https://github.com/irssi/scripts.irssi.org',
authors => 'terminaldweller',
contact => 'https://terminaldweller.com',
name => 'tongueworm',
description => 'rewrites the input line using openai chatgpt',
license => 'GPL3 or newer',
url => 'https://github.com/irssi/scripts.irssi.org',
);

# adds the tongueworm command. the default question will just rephrase your input line
Expand All @@ -29,40 +29,57 @@
# the temperature value is divided by 1000, so a value of 700 would become 700/1000, i.e. 0.7
# /set wormtongue_debug 0
# /set wormtongue_request my_awesome_request
# NOTE: if we get a good FOSS and or self-hosted option in the future, we can switch to that.
# if you find one let me know.
Irssi::settings_add_str('misc','wormtongue_openai_api_key', '');
Irssi::settings_add_str('misc','wormtongue_model', 'gpt-3.5-turbo');
Irssi::settings_add_str('misc','wormtongue_role', 'user');
Irssi::settings_add_int('misc','wormtongue_temperature', 700);
Irssi::settings_add_bool('misc','wormtongue_debug', 0);
Irssi::settings_add_str('misc', 'wormtongue_request', 'rephrase the sentence that comes after the question mark and dont change its language?');
Irssi::settings_add_str('misc', 'wormtongue_request', 'rephrase this: ');
Irssi::settings_add_str('misc', 'wormtongue_provider', 'ollama');
Irssi::settings_add_str('misc', 'wormtongue_server_endpoint', '');
Irssi::settings_add_int('misc', 'wormtongue_timeout', 5000);
Irssi::settings_add_str('misc', 'wormtongue_system_prompt', 'you are good at rephrasing sentences');

sub wormtongue {
my $debug = Irssi::settings_get_bool('wormtongue_debug');
my $ua = LWP::UserAgent->new;
my $server_endpoint = "https://api.openai.com/v1/chat/completions";
my $timeout = Irssi::settings_get_int('wormtongue_timeout')/1000;
my $ua = LWP::UserAgent->new(timeout => $timeout);
my $server_endpoint = Irssi::settings_get_str('wormtongue_server_endpoint');
my $req = HTTP::Request->new(POST => $server_endpoint);
$req->header('Content-Type'=>'application/json');
my $openai_api_key = Irssi::settings_get_str('wormtongue_openai_api_key');
$req->header('Authorization'=>"Bearer $openai_api_key");
my $ai_model = Irssi::settings_get_str('wormtongue_model');
my $ai_role = Irssi::settings_get_str('wormtongue_role');
my $ai_temp = Irssi::settings_get_int('wormtongue_temperature')/1000;
my $provider = Irssi::settings_get_str('wormtongue_provider');
my $question = Irssi::settings_get_str('wormtongue_request');
my $content = $question.Irssi::parse_special('$L', 0, 0);
my $post_data = '{"model" : "'.$ai_model.'", "temperature" : '.$ai_temp.', "messages" : [{"role" : "'.$ai_role.'","content" : "'.$content.'"}]}';
Irssi::print($post_data) if ($debug == 1);
my $system_prompt = Irssi::settings_get_str('wormtongue_system_prompt');
my $post_data = "";

if ($provider eq "chatgpt") {
$post_data = '{"model" : "'.$ai_model.'", "temperature" : '.$ai_temp.', "messages" : [{"role" : "'.$ai_role.'","content" : "'.$content.'"}]}';
Irssi::print($post_data) if ($debug == 1);
} elsif ($provider eq "ollama") {
$post_data = '{"model" : "'.$ai_model.'", "format" : "json", "prompt" : "'.$content.'" , "system" : "'.$system_prompt.'", "stream": false, "options" : {"temperature" : '.$ai_temp.'}}';
Irssi::print($post_data) if ($debug == 1);
}

$req->content($post_data);
my $resp = $ua->request($req);
my $result = "";
Irssi::print($resp) if ($debug == 1);
if ($resp->is_success) {
my $message = $resp->decoded_content;
Irssi::print("Received reply: $message") if ($debug == 1);
my $json_parser = JSON::PP->new;
my $data = $json_parser->decode($message);
my $result = $data->{choices}[0]{message}{content};
if ($provider eq "chatgpt"){
$result = $data->{choices}[0]{message}{content};
}elsif ($provider eq "ollama") {
$result = $data->{response};
}
Irssi::print($result) if ($debug == 1);
Irssi::gui_input_set($result);
}
Expand Down

0 comments on commit d84213b

Please sign in to comment.