Skip to content

Commit

Permalink
upgraded to ChatGPT API and made settings more secure
Browse files Browse the repository at this point in the history
  • Loading branch information
unconv committed Mar 16, 2023
1 parent 1194f9a commit 066c33b
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 60 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
api_key.txt
vendor/
composer.phar
settings.php
2 changes: 1 addition & 1 deletion php/composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"require": {
"orhanerday/open-ai": "^3.4",
"orhanerday/open-ai": "*",
"league/commonmark": "^2.3"
}
}
59 changes: 30 additions & 29 deletions php/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 31 additions & 30 deletions php/message.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
require_once(__DIR__."/vendor/autoload.php");

$settings = require( __DIR__ . "/settings.php" );

use Orhanerday\OpenAi\OpenAi;
use League\CommonMark\CommonMarkConverter;

Expand All @@ -9,47 +11,46 @@
$context = json_decode( $_POST['context'] ?? "[]" ) ?: [];

// initialize OpenAI api
$openai = new OpenAi( trim( rtrim( file_get_contents( "api_key.txt" ) ) ) );
$openai = new OpenAi( $settings['api_key'] );

// set default prompt
$prompt = "Act as an AI mentor for a programmer by answering the questions provided. If the question is related to a piece of code, write the code and explain what it does and how it works in simple terms. Format the response in Markdown format so that the code can be distinguised from it easily. Please also explain the steps involved, don't only tell the code to use. Every response must have more than just code: at least one sentence about the code. If you're asked for your identity, say that your name is the magnificent ChatWTF.\n\n";
$messages = [];

// add context to prompt
if( empty( $context ) ) {
// if there is no context, use a default example question and answer
$prompt .= "Question:\n'How do you write a hello world script in PHP?'\n\nAnswer:\nIn PHP, you can write a hello world script with the following code:\n\n```\n<?php\necho 'Hello world';\n?>\n```\n\nYou need to put this code into a file with the .php extension and then run it with PHP or with a web server.\n\nQuestion:\n'Can you use the print function instead?'\n\nAnswer:\nCertainly! Here's how you would use the `print` function insted:\n\n```\n<?php\nprint('Hello world');\n?>\n```\n\n";
$please_use_above = "";
} else {
// add old questions and answers to prompt
$prompt .= "";
$context = array_slice( $context, -5 );
foreach( $context as $message ) {
$prompt .= "Question:\n" . $message[0] . "\n\nAnswer:\n" . $message[1] . "\n\n";
}
$please_use_above = ". Please use the questions and answers above as context for the answer.";
if( ! empty( $settings['system_message'] ) ) {
$messages[] = [
"role" => "system",
"content" => $settings['system_message'],
];
}

foreach( $context as $msg ) {
$messages[] = [
"role" => "user",
"content" => $msg[0],
];
$messages[] = [
"role" => "assistant",
"content" => $msg[1],
];
}

// add new question to prompt
$prompt = $prompt . "Question:\n" . $_POST['message'] . $please_use_above . "\n\nAnswer:\n\n";
$messages[] = [
"role" => "user",
"content" => $_POST['message'],
];

// create a new completion
$complete = json_decode( $openai->completion( [
'model' => 'text-davinci-003',
'prompt' => $prompt,
'temperature' => 0.9,
$complete = json_decode( $openai->chat( [
'model' => 'gpt-3.5-turbo',
'messages' => $messages,
'temperature' => 1.0,
'max_tokens' => 2000,
'top_p' => 1,
'frequency_penalty' => 0,
'presence_penalty' => 0,
'stop' => [
"\nNote:",
"\nQuestion:"
]
] ) );
] ) );

// get message text
if( isset( $complete->choices[0]->text ) ) {
$text = str_replace( "\\n", "\n", $complete->choices[0]->text );
if( isset( $complete->choices[0]->message->content ) ) {
$text = str_replace( "\\n", "\n", $complete->choices[0]->message->content );
} elseif( isset( $complete->error->message ) ) {
$text = $complete->error->message;
} else {
Expand Down
17 changes: 17 additions & 0 deletions php/settings.sample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* Rename this file to "settings.php" and add your OpenAI API key
*
* You can also add a system message if you want. A system message
* will change the behavior of ChatGPT. You can tell it to
* answer messages in a specific manner, act as someone else
* or provide any other context for the chat.
*/

return [
// add your OpenAI API key here
"api_key" => "",

// add an optional system message here
"system_message" => "",
];

0 comments on commit 066c33b

Please sign in to comment.